EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
422 stars 58 forks source link

GPUCompiler code typed err #1528

Closed wsmoses closed 3 weeks ago

wsmoses commented 3 weeks ago
julia> fn = sprint() do io
          Enzyme.Compiler.enzyme_code_typed(io, Enzyme.Compiler.runtime_mixed_call, Const, Tuple{Const{Val{(false, true)}}, Const{typeof(Base.Threads.threading_run)}, Duplicated{Ref{typeof(m)}}})
       end^C

julia> Enzyme.Compiler.enzyme_code_typed(Enzyme.Compiler.runtime_mixed_call, Const, Tuple{Const{Val{(false, true)}}, Const{typeof(Base.Threads.threading_run)}, Duplicated{Ref{typeof(m)}}})
1-element Vector{Any}:
 CodeInfo(
1 ─ %1 = (isa)(arg_1, Base.RefValue{var"#2#threadsfor_fun#1"{Vector{Float64}, Float64, UnitRange{Int64}}})::Bool
└──      goto #3 if not %1
2 ─ %3 = π (arg_1, Base.RefValue{var"#2#threadsfor_fun#1"{Vector{Float64}, Float64, UnitRange{Int64}}})
│   %4 = Base.getfield(%3, :x)::var"#2#threadsfor_fun#1"{Vector{Float64}, Float64, UnitRange{Int64}}
└──      goto #4
3 ─ %6 = Base.getindex(arg_1)::Any
└──      goto #4
4 ┄ %8 = φ (#2 => %4, #3 => %6)::Any
│   %9 = (f)(%8)::Core.Const(nothing)
└──      return %9
) => Nothing

julia> Enzyme.Compiler.enzyme_code_lowered(Enzyme.Compiler.runtime_mixed_call, Const, Tuple{Const{Val{(false, true)}}, Const{typeof(Base.Threads.threading_run)}, Duplicated{Ref{typeof(m)}}})
ERROR: UndefVarError: enzyme_code_lowered not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base ./Base.jl:26
 [2] top-level scope
   @ REPL[14]:1

julia> @code_lowered Enzyme.Compiler.runtime_mixed_call(Val((false,true)), Base.Threads.threading_run, Ref(m))
CodeInfo(
    @ /home/wmoses/git/Enzyme.jl/src/rules/jitrules.jl:23 within `runtime_mixed_call'
   ┌ @ /home/wmoses/git/Enzyme.jl/src/rules/jitrules.jl:45 within `macro expansion'
1 ─│      nothing
│  └
│  ┌ @ /home/wmoses/git/Enzyme.jl/src/rules/jitrules.jl:46 within `macro expansion'
│  │ %2 = Base.getindex(arg_1)
│  │ %3 = (f)(%2)
└──│      return %3
   └
)

julia> @code_typed Enzyme.Compiler.runtime_mixed_call(Val((false,true)), Base.Threads.threading_run, Ref(m))
CodeInfo(
1 ─ %1 = Base.getfield(arg_1, :x)::var"#2#threadsfor_fun#1"{Vector{Float64}, Float64, UnitRange{Int64}}
│   %2 = invoke f(%1::Function)::Core.Const(nothing)
└──      return %2
) => Nothing
wsmoses commented 3 weeks ago

setup shenanigans:


z(x) = Base.unsafe_pointer_to_objref(Base.reinterpret(Ptr{Cvoid}, x))
@generated function make()
     return quote
         Base.@_inline_meta
         $(Expr(:new, z(138254807239984), ones(10), 1.0, UnitRange{Int64}(1:3)))
     end
end
m = make()

Enzyme.Compiler.runtime_mixed_call(Val((false,true)), Base.Threads.threading_run, Ref(m))

Enzyme.Compiler.enzyme_code_typed(Enzyme.Compiler.runtime_mixed_call, Const, Tuple{Const{Val{(false, true)}}, Const{typeof(Base.Threads.threading_run)}, Duplicated{Ref{typeof(m)}}})

 fn = sprint() do io
    Enzyme.Compiler.enzyme_code_llvm(io, Enzyme.Compiler.runtime_mixed_call, Const, Tuple{Const{Val{(false, true)}}, Const{typeof(Base.Threads.threading_run)}, Duplicated{Ref{typeof(m)}}})
 end

 fn = sprint() do io
    Enzyme.Compiler.enzyme_code_llvm(io, Enzyme.Compiler.runtime_mixed_call, Const, Tuple{Const{Val{(false, true)}}, Duplicated{Ref{typeof(m)}}}; dump_module=true, run_enzyme=false)
 end
wsmoses commented 3 weeks ago

Moved here: https://github.com/JuliaGPU/GPUCompiler.jl/issues/587