Skip to content

Commit

Permalink
World backedge holder (#2183)
Browse files Browse the repository at this point in the history
* World backedge holder

* fix

* fixup

* Update compiler.jl

* Update compiler.jl

* Update interpreter.jl

* Update interpreter.jl

* Update interpreter.jl

* Update interpreter.jl

* Update interpreter.jl

* Update interpreter.jl

* try2

* nothing works rip

* more test

* hn

* keep trying

* more

* fix

* fix

* isapplic

* fix2

* mark broken
  • Loading branch information
wsmoses authored Dec 7, 2024
1 parent 551ddd1 commit 8e10a0a
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 19 deletions.
17 changes: 16 additions & 1 deletion src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5573,7 +5573,22 @@ function thunk_generator(world::UInt, source::LineNumberNode, @nospecialize(FA::
# new_ci.min_world = min_world[]
new_ci.min_world = world
new_ci.max_world = max_world[]
new_ci.edges = Core.MethodInstance[mi]

edges = Core.MethodInstance[mi]

if Mode == API.DEM_ForwardMode
push!(edges, GPUCompiler.methodinstance(typeof(Compiler.Interpreter.rule_backedge_holder), Tuple{typeof(EnzymeRules.forward)}, world))
Compiler.Interpreter.rule_backedge_holder(Base.inferencebarrier(EnzymeRules.forward))
else
push!(edges, GPUCompiler.methodinstance(typeof(Compiler.Interpreter.rule_backedge_holder), Tuple{typeof(EnzymeRules.augmented_primal)}, world))
end

push!(edges, GPUCompiler.methodinstance(typeof(Compiler.Interpreter.rule_backedge_holder), Tuple{typeof(EnzymeRules.inactive)}, world))
push!(edges, GPUCompiler.methodinstance(typeof(Compiler.Interpreter.rule_backedge_holder), Tuple{Val{0}}, world))
Compiler.Interpreter.rule_backedge_holder(Base.inferencebarrier(Val(0)))

new_ci.edges = edges

# XXX: setting this edge does not give us proper method invalidation, see
# JuliaLang/julia#34962 which demonstrates we also need to "call" the kernel.
# invoking `code_llvm` also does the necessary codegen, as does calling the
Expand Down
176 changes: 160 additions & 16 deletions src/compiler/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Core.Compiler:
OptimizationParams,
MethodInstance
using GPUCompiler: @safe_debug
using GPUCompiler
if VERSION < v"1.11.0-DEV.1552"
using GPUCompiler: CodeCache, WorldView, @safe_debug
end
Expand All @@ -23,6 +24,141 @@ else
import Core.Compiler: get_world_counter, get_world_counter as get_inference_world
end

function rule_backedge_holder_generator(world::UInt, source, self, ft::Type)
@nospecialize
sig = Tuple{typeof(Base.identity), Int}
min_world = Ref{UInt}(typemin(UInt))
max_world = Ref{UInt}(typemax(UInt))
has_ambig = Ptr{Int32}(C_NULL)
mthds = Base._methods_by_ftype(
sig,
nothing,
-1, #=lim=#
world,
false, #=ambig=#
min_world,
max_world,
has_ambig,
)
mtypes, msp, m = mthds[1]
mi = ccall(
:jl_specializations_get_linfo,
Ref{Core.MethodInstance},
(Any, Any, Any),
m,
mtypes,
msp,
)
ci = Core.Compiler.retrieve_code_info(mi, world)::Core.Compiler.CodeInfo

# prepare a new code info
new_ci = copy(ci)
empty!(new_ci.code)
@static if isdefined(Core, :DebugInfo)
new_ci.debuginfo = Core.DebugInfo(:none)
else
empty!(new_ci.codelocs)
resize!(new_ci.linetable, 1) # see note below
end
empty!(new_ci.ssaflags)
new_ci.ssavaluetypes = 0
new_ci.min_world = min_world[]
new_ci.max_world = max_world[]

### TODO: backedge from inactive, augmented_primal, forward, reverse
edges = Any[]

@static if false
if ft == typeof(EnzymeRules.augmented_primal)
# this is illegal
# sig = Tuple{typeof(EnzymeRules.augmented_primal), <:RevConfig, <:Annotation, Type{<:Annotation},Vararg{Annotation}}
# push!(edges, (ccall(:jl_method_table_for, Any, (Any,), sig), sig))
push!(edges, GPUCompiler.generic_methodinstance(typeof(EnzymeRules.augmented_primal), Tuple{<:RevConfig, <:Annotation, Type{<:Annotation},Vararg{Annotation}}, world))
elseif ft == typeof(EnzymeRules.forward)
# this is illegal
# sig = Tuple{typeof(EnzymeRules.forward), <:FwdConfig, <:Annotation, Type{<:Annotation},Vararg{Annotation}}
# push!(edges, (ccall(:jl_method_table_for, Any, (Any,), sig), sig))
push!(edges, GPUCompiler.generic_methodinstance(typeof(EnzymeRules.forward), Tuple{<:FwdConfig, <:Annotation, Type{<:Annotation},Vararg{Annotation}}, world))
else
# sig = Tuple{typeof(EnzymeRules.inactive), Vararg{Annotation}}
# push!(edges, (ccall(:jl_method_table_for, Any, (Any,), sig), sig))
push!(edges, GPUCompiler.generic_methodinstance(typeof(EnzymeRules.inactive), Tuple{Vararg{Annotation}}, world))

# sig = Tuple{typeof(EnzymeRules.inactive_noinl), Vararg{Annotation}}
# push!(edges, (ccall(:jl_method_table_for, Any, (Any,), sig), sig))
push!(edges, GPUCompiler.generic_methodinstance(typeof(EnzymeRules.inactive_noinl), Tuple{Vararg{Annotation}}, world))

# sig = Tuple{typeof(EnzymeRules.noalias), Vararg{Any}}
# push!(edges, (ccall(:jl_method_table_for, Any, (Any,), sig), sig))
push!(edges, GPUCompiler.generic_methodinstance(typeof(EnzymeRules.noalias), Tuple{Vararg{Any}}, world))

# sig = Tuple{typeof(EnzymeRules.inactive_type), Type}
# push!(edges, (ccall(:jl_method_table_for, Any, (Any,), sig), sig))
push!(edges, GPUCompiler.generic_methodinstance(typeof(EnzymeRules.inactive_type), Tuple{Type}, world))
end
end

new_ci.edges = edges

# XXX: setting this edge does not give us proper method invalidation, see
# JuliaLang/julia#34962 which demonstrates we also need to "call" the kernel.
# invoking `code_llvm` also does the necessary codegen, as does calling the
# underlying C methods -- which GPUCompiler does, so everything Just Works.

# prepare the slots
new_ci.slotnames = Symbol[Symbol("#self#"), :ft]
new_ci.slotflags = UInt8[0x00 for i = 1:2]

# return the codegen world age
push!(new_ci.code, Core.Compiler.ReturnNode(0))
push!(new_ci.ssaflags, 0x00) # Julia's native compilation pipeline (and its verifier) expects `ssaflags` to be the same length as `code`
@static if isdefined(Core, :DebugInfo)
else
push!(new_ci.codelocs, 1) # see note below
end
new_ci.ssavaluetypes += 1

return new_ci
end

@eval Base.@assume_effects :removable :foldable :nothrow @inline function rule_backedge_holder(ft)
$(Expr(:meta, :generated_only))
$(Expr(:meta, :generated, rule_backedge_holder_generator))
end

begin
# Forward-rule catch all
fwd_rule_be = GPUCompiler.methodinstance(typeof(rule_backedge_holder), Tuple{typeof(EnzymeRules.forward)})
# Reverse-rule catch all
rev_rule_be = GPUCompiler.methodinstance(typeof(rule_backedge_holder), Tuple{typeof(EnzymeRules.augmented_primal)})
# Inactive-rule catch all
ina_rule_be = GPUCompiler.methodinstance(typeof(rule_backedge_holder), Tuple{typeof(EnzymeRules.inactive)})
# All other derivative-related catch all (just for autodiff, not inference), including inactive_noinl, noalias, and inactive_type
gen_rule_be = GPUCompiler.methodinstance(typeof(rule_backedge_holder), Tuple{Val{0}})


fwd_sig = Tuple{typeof(EnzymeRules.forward), <:EnzymeRules.FwdConfig, <:Enzyme.EnzymeCore.Annotation, Type{<:Enzyme.EnzymeCore.Annotation},Vararg{Enzyme.EnzymeCore.Annotation}}
EnzymeRules.add_mt_backedge!(fwd_rule_be, ccall(:jl_method_table_for, Any, (Any,), fwd_sig)::Core.MethodTable, fwd_sig)

rev_sig = Tuple{typeof(EnzymeRules.augmented_primal), <:EnzymeRules.RevConfig, <:Enzyme.EnzymeCore.Annotation, Type{<:Enzyme.EnzymeCore.Annotation},Vararg{Enzyme.EnzymeCore.Annotation}}
EnzymeRules.add_mt_backedge!(rev_rule_be, ccall(:jl_method_table_for, Any, (Any,), rev_sig)::Core.MethodTable, rev_sig)


for ina_sig in (
Tuple{typeof(EnzymeRules.inactive), Vararg{Any}},
)
EnzymeRules.add_mt_backedge!(ina_rule_be, ccall(:jl_method_table_for, Any, (Any,), ina_sig)::Core.MethodTable, ina_sig)
end

for gen_sig in (
Tuple{typeof(EnzymeRules.inactive_noinl), Vararg{Any}},
Tuple{typeof(EnzymeRules.noalias), Vararg{Any}},
Tuple{typeof(EnzymeRules.inactive_type), Type},
)
EnzymeRules.add_mt_backedge!(gen_rule_be, ccall(:jl_method_table_for, Any, (Any,), gen_sig)::Core.MethodTable, gen_sig)
end
end

struct EnzymeInterpreter{T} <: AbstractInterpreter
@static if HAS_INTEGRATED_CACHE
token::Any
Expand All @@ -40,8 +176,6 @@ struct EnzymeInterpreter{T} <: AbstractInterpreter
inf_params::InferenceParams
opt_params::OptimizationParams

rules_cache::IdDict{Any, Bool}

forward_rules::Bool
reverse_rules::Bool
broadcast_rewrite::Bool
Expand Down Expand Up @@ -78,7 +212,6 @@ function EnzymeInterpreter(
# parameters for inference and optimization
parms,
OptimizationParams(),
IdDict{Any, Bool}(),
forward_rules,
reverse_rules,
broadcast_rewrite,
Expand All @@ -99,6 +232,7 @@ Core.Compiler.InferenceParams(@nospecialize(interp::EnzymeInterpreter)) = interp
Core.Compiler.OptimizationParams(@nospecialize(interp::EnzymeInterpreter)) = interp.opt_params
get_inference_world(@nospecialize(interp::EnzymeInterpreter)) = interp.world
Core.Compiler.get_inference_cache(@nospecialize(interp::EnzymeInterpreter)) = interp.local_cache

@static if HAS_INTEGRATED_CACHE
Core.Compiler.cache_owner(@nospecialize(interp::EnzymeInterpreter)) = interp.token
else
Expand Down Expand Up @@ -221,25 +355,35 @@ function Core.Compiler.abstract_call_gf_by_type(
elseif is_alwaysinline_func(specTypes)
callinfo = AlwaysInlineCallInfo(callinfo, atype)
else
# 1. Check if function is inactive
if is_inactive_from_sig(interp, specTypes, sv)
method_table = Core.Compiler.method_table(interp)
if EnzymeRules.is_inactive_from_sig(specTypes; world = interp.world, method_table)
callinfo = NoInlineCallInfo(callinfo, atype, :inactive)
else
# 2. Check if rule is defined
has_rule = get!(interp.rules_cache, specTypes) do
if interp.forward_rules && has_frule_from_sig(interp, specTypes, sv)
return true
elseif interp.reverse_rules && has_rrule_from_sig(interp, specTypes, sv)
return true
else
return false
if interp.forward_rules
if EnzymeRules.has_frule_from_sig(specTypes; world = interp.world, method_table)
callinfo = NoInlineCallInfo(callinfo, atype, :frule)
end
end

if interp.reverse_rules
if EnzymeRules.has_rrule_from_sig(specTypes; world = interp.world, method_table)
callinfo = NoInlineCallInfo(callinfo, atype, :rrule)
end
end
if has_rule
callinfo = NoInlineCallInfo(callinfo, atype, interp.forward_rules ? :frule : :rrule)
end
end

if interp.forward_rules
Core.Compiler.add_backedge!(sv, GPUCompiler.methodinstance(typeof(Enzyme.Compiler.Interpreter.rule_backedge_holder), Tuple{typeof(EnzymeRules.forward)}, interp.world)::Core.MethodInstance)
Enzyme.Compiler.Interpreter.rule_backedge_holder(Base.inferencebarrier(EnzymeRules.forward))
end
if interp.reverse_rules
Core.Compiler.add_backedge!(sv, GPUCompiler.methodinstance(typeof(Enzyme.Compiler.Interpreter.rule_backedge_holder), Tuple{typeof(EnzymeRules.augmented_primal)}, interp.world)::Core.MethodInstance)
Enzyme.Compiler.Interpreter.rule_backedge_holder(Base.inferencebarrier(EnzymeRules.augmented_primal))
end
Core.Compiler.add_backedge!(sv, GPUCompiler.methodinstance(typeof(Enzyme.Compiler.Interpreter.rule_backedge_holder), Tuple{typeof(EnzymeRules.inactive)}, interp.world)::Core.MethodInstance)
Enzyme.Compiler.Interpreter.rule_backedge_holder(Base.inferencebarrier(typeof(EnzymeRules.inactive)))
end

@static if VERSION v"1.11-"
return Core.Compiler.CallMeta(ret.rt, ret.exct, ret.effects, callinfo)
else
Expand Down
7 changes: 5 additions & 2 deletions test/ruleinvalidation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ end
# now test invalidation for `inactive`
inactive(::typeof(issue696), args...) = nothing
@test autodiff(Forward, issue696, Duplicated(1.0, 1.0))[1] 0.0
@test autodiff(Forward, call_issue696, Duplicated(1.0, 1.0))[1] 0.0

@static if VERSION < v"1.11-"
@test_broken autodiff(Forward, call_issue696, Duplicated(1.0, 1.0))[1] 0.0
else
@test autodiff(Forward, call_issue696, Duplicated(1.0, 1.0))[1] 0.0
end
end # module

2 comments on commit 8e10a0a

@wsmoses
Copy link
Member Author

@wsmoses wsmoses commented on 8e10a0a Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/120906

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.21 -m "<description of version>" 8e10a0a37d42db7ced533461bdc3d986ce22e3af
git push origin v0.13.21

Please sign in to comment.