dfdx / Ghost.jl

The Code Tracer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception on Tracing

mharradon opened this issue · comments

On main branch:

julia> using Ghost: trace, play!, compile

julia> function loop6(n)
           a = 0
           i = 1
           while true
               a += i
               if a > n
                   break
               end
           end
           return a
       end
loop6 (generic function with 1 method)

julia> v, tape = trace(loop6, 3)
ERROR: Error compiling @dynamo Ghost.IRTracer on (typeof(loop6), Int64):
type Bool has no field id
Stacktrace:
  [1] getproperty(x::Bool, f::Symbol)
    @ Base ./Base.jl:33
  [2] loop_condition_ir_id(block::IRTools.Inner.Block)
    @ Ghost ~/Ghost.jl/src/trace.jl:276
  [3] trace_loops!(ir::IRTools.Inner.IR)
    @ Ghost ~/Ghost.jl/src/trace.jl:561
  [4] macro expansion
    @ ~/Ghost.jl/src/trace.jl:631 [inlined]
  [5] transform(::Type{Ghost.IRTracer}, ::Type, ::Vararg{Type, N} where N)
    @ Ghost ~/.julia/packages/IRTools/isLV2/src/reflection/dynamo.jl:122
  [6] dynamo(::Any, ::Any, ::Any, ::Vararg{Any, N} where N)
    @ IRTools.Inner ~/.julia/packages/IRTools/isLV2/src/reflection/dynamo.jl:62
  [7] #s22#77
    @ ~/.julia/packages/IRTools/isLV2/src/reflection/dynamo.jl:114 [inlined]
  [8] var"#s22#77"(t::Any, args::Any)
    @ Ghost ./none:0
  [9] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any, N} where N)
    @ Core ./boot.jl:571
 [10] trace(f::Function, args::Int64; is_primitive::Function, primitives::Nothing, ctx::Dict{Any, Any})
    @ Ghost ~/Ghost.jl/src/trace.jl:661
 [11] trace(f::Function, args::Int64)
    @ Ghost ~/Ghost.jl/src/trace.jl:651
 [12] top-level scope
    @ REPL[7]:1

Commented out test has been added to this branch: https://github.com/dfdx/Ghost.jl/pull/24/files#diff-9cfffbdde1240d3bbc48e12d9b70b84e4e53674af55fb22fabc01f465af7991cR235

This is because

Ghost.jl/src/trace.jl

Lines 290 to 293 in 13c283a

function loop_condition_ir_id(block::IRTools.Block)
br = loop_exit_branch(block)
return br !== nothing ? br.condition.id : nothing
end
branches can have Any conditions: https://github.com/FluxML/IRTools.jl/blob/948773227955e29a6caae44d109e8be56db6e605/src/ir/ir.jl#L39-L43

I suspect a false or true propagates to the condition.

Yeah, looks like while true breaks this function. Intuitively, we need to return nothing in this case because there's no block designating the condition, but we need to test it thoroughly. I'm putting it into my backlog, please ping me if this is something blocking you.