diff --git a/src/jlgen.jl b/src/jlgen.jl index 8602a22d..0ed70f5f 100644 --- a/src/jlgen.jl +++ b/src/jlgen.jl @@ -473,6 +473,159 @@ else end +## overlay dependencies +# +# Julia only records that a call depends on the absence of a method when the methods it +# found do not fully cover the call. A call that misses in the overlay tables and resolves +# to a method in a later table or in Base's table thus has no dependency on the overlay +# tables, and adding an overlay later does not invalidate it (JuliaGPU/GPUCompiler.jl#207). +# Record that dependency for every method lookup when inference finishes a function. +# +# All such dependencies are recorded by `record_overlay_lookup!`. They are a stopgap, as +# Julia's developers haven't settled how code that is inferred with overlay tables should +# depend on them. Either Julia records and validates the lookups in overlay tables itself +# (JuliaLang/julia#64726, #64727), in which case the edges below are the ones it would +# record, and this code can go. Or interpreters like ours are to stay away from Julia's +# backedges, and verify the forward edges of their own code (the position taken in +# JuliaLang/julia#63959), in which case that function is the place to record what we would +# verify. + +# the overlay tables a method table view consults before Base's, from most to least +# specific, or `nothing` for views that may do something else +overlay_tables(mtv::CC.MethodTableView) = nothing +overlay_tables(mtv::CC.InternalMethodTable) = () +@static if isdefined(CC, :CachedMethodTable) + overlay_tables(mtv::CC.CachedMethodTable) = overlay_tables(mtv.table) +end +overlay_tables(mtv::CC.OverlayMethodTable) = (mtv.mt,) +function overlay_tables(mtv::StackedMethodTable) + tables = overlay_tables(mtv.parent) + return tables === nothing ? nothing : (mtv.mt, tables...) +end +overlay_tables(mtv::MethodTableStack) = (mtv.tables...,) + +@static if isdefined(CC, :ForwardToBackedgeIterator) + +# Make code with forward edges `edges` depend on no method being added for `sig` to the +# overlay tables `tables`. +# +# On 1.12+, such a missing-method edge is a `(sig, Core.methodtable)` pair in the forward +# edges. These backedges are kept globally and checked when inserting into any method table, +# overlay tables included, so a single edge covers all of them. +function record_overlay_lookup!(edges::Vector{Any}, @nospecialize(sig), tables::Tuple) + for j in 2:length(edges) + edges[j] === Core.methodtable && edges[j-1] == sig && return + end + push!(edges, sig, Core.methodtable) + return +end + +function add_overlay_dependencies!(edges::Vector{Any}, tables::Tuple) + sigs = Any[] + i = 1 + while i <= length(edges) + item = edges[i] + if item isa Int # a lookup: count, signature, then the matches + push!(sigs, edges[i+1]) + i += 2 + abs(item) + elseif item isa CodeInstance + push!(sigs, CC.get_ci_mi(item).specTypes) + i += 1 + elseif item isa MethodInstance + push!(sigs, item.specTypes) + i += 1 + elseif item isa Type # an `invoke` signature, or a missing-method edge + edges[i+1] === Core.methodtable || push!(sigs, item) + i += 2 + else # bindings, methods + i += 1 + end + end + for sig in sigs + record_overlay_lookup!(edges, sig, tables) + end + return +end + +function CC.finish!(interp::GPUInterpreter, caller::CC.InferenceState, + validation_world::UInt, time_before::UInt64) + tables = overlay_tables(interp.method_table_view) + if tables !== nothing && !isempty(tables) + # the optimizer shares and extends the frame's edges, so add to the final ones + opt = caller.result.src + edges = opt isa CC.OptimizationState ? opt.inlining.edges : caller.edges + add_overlay_dependencies!(edges, tables) + end + return @invoke CC.finish!(interp::CC.AbstractInterpreter, caller::CC.InferenceState, + validation_world::UInt, time_before::UInt64) +end + +else + +# Make code with edges `edges` depend on no method being added for `sig` to the overlay +# tables `tables`. +# +# Before 1.12, such a missing-method edge is a `(mt, sig)` pair, and its backedge is kept in +# that table, so we need one for every overlay table. +function record_overlay_lookup!(edges::Vector{Any}, @nospecialize(sig), tables::Tuple) + for mt in tables + exists = false + for j in 1:length(edges)-1 + if edges[j] === mt && edges[j+1] == sig + exists = true + break + end + end + exists || push!(edges, mt, sig) + end + return +end + +function add_overlay_dependencies!(edges::Vector{Any}, tables::Tuple) + sigs = Any[] + i = 1 + while i <= length(edges) + item = edges[i] + if item isa MethodInstance + push!(sigs, item.specTypes) + i += 1 + elseif item isa Core.MethodTable # a missing-method edge + push!(sigs, edges[i+1]) + i += 2 + else # an `invoke` signature and its target + push!(sigs, item) + i += 2 + end + end + for sig in sigs + record_overlay_lookup!(edges, sig, tables) + end + return +end + +@static if hasmethod(CC.finish!, Tuple{CC.AbstractInterpreter, CC.InferenceState}) + # 1.11: backedges are stored when finishing the frame, after optimization + function CC.finish!(interp::GPUInterpreter, caller::CC.InferenceState) + tables = overlay_tables(interp.method_table_view) + tables === nothing || isempty(tables) || + add_overlay_dependencies!(caller.stmt_edges[1], tables) + return @invoke CC.finish!(interp::CC.AbstractInterpreter, caller::CC.InferenceState) + end +else + # 1.10: backedges are stored after optimization without a hook in between, so add them + # to the frame's edges as soon as they are collected + function CC.finish(me::CC.InferenceState, interp::GPUInterpreter) + ret = @invoke CC.finish(me::CC.InferenceState, interp::CC.AbstractInterpreter) + tables = overlay_tables(interp.method_table_view) + tables === nothing || isempty(tables) || + add_overlay_dependencies!(me.stmt_edges[1]::Vector{Any}, tables) + return ret + end +end + +end + + ## Julia IR passes # run Julia IR passes (see `julia_ir_passes`) on the IR of a function, as optimized by Julia. diff --git a/test/native.jl b/test/native.jl index 380c96aa..0999e17f 100644 --- a/test/native.jl +++ b/test/native.jl @@ -3218,9 +3218,9 @@ end ############################################################################################ @testset "overrides" begin - # NOTE: method overrides do not support redefinitions, so we use different kernels - mod = @eval module $(gensym()) + Base.Experimental.@MethodTable(method_table) + kernel() = child() @inline child() = 0 end @@ -3228,25 +3228,76 @@ end @test @filecheck begin @check_label "@julia_kernel" @check "ret i64 0" - Native.code_llvm(mod.kernel, Tuple{}) + Native.code_llvm(mod.kernel, Tuple{}; mod.method_table) end - mod = @eval module $(gensym()) - using ..GPUCompiler + # adding an overlay invalidates code that called the method it overrides + @eval mod Base.Experimental.@overlay method_table child() = 1 + @test @filecheck begin + @check_label "@julia_kernel" + @check "ret i64 1" + Native.code_llvm(mod.kernel, Tuple{}; mod.method_table) + end + # ... as does redefining it + @eval mod Base.Experimental.@overlay method_table child() = 2 + @test @filecheck begin + @check_label "@julia_kernel" + @check "ret i64 2" + Native.code_llvm(mod.kernel, Tuple{}; mod.method_table) + end +end + +@testset "overrides invalidation" begin + # a back-end that caches results with the code + mod = @eval module $(gensym()) Base.Experimental.@MethodTable(method_table) + Base.Experimental.@MethodTable(other_method_table) + + mutable struct Results + asm::Union{Nothing,String} + Results() = new(nothing) + end kernel() = child() @inline child() = 0 - - Base.Experimental.@overlay method_table child() = 1 + invoke_kernel() = invoke(invoked, Tuple{Any}, 1) + @inline invoked(x) = 0 + applicable_kernel() = applicable(probe, 1) ? 1 : 0 + function probe end end - - @test @filecheck begin - @check_label "@julia_kernel" - @check "ret i64 1" - Native.code_llvm(mod.kernel, Tuple{}; mod.method_table) + function cached_llvm(f; kwargs...) + job, _ = Native.create_job(f, (); kwargs...) + res = GPUCompiler.cached_results(mod.Results, job) + if res === nothing || res.asm === nothing + asm = JuliaContext() do ctx + ir, meta = GPUCompiler.compile(:llvm, job) + string(ir) + end + res = @something res GPUCompiler.cached_results(mod.Results, job) + res.asm = asm + end + return res.asm end + tables = (mod.method_table, mod.other_method_table) + + @test occursin("ret i64 0", cached_llvm(mod.kernel; method_table=tables)) + @eval mod Base.Experimental.@overlay other_method_table child() = 1 + @test occursin("ret i64 1", cached_llvm(mod.kernel; method_table=tables)) + + # an overlay in a more specific table overrides the one in a less specific table + @eval mod Base.Experimental.@overlay method_table child() = 2 + @test occursin("ret i64 2", cached_llvm(mod.kernel; method_table=tables)) + + # `invoke` depends on the lookup of the signature it is given + @test occursin("ret i64 0", cached_llvm(mod.invoke_kernel; method_table=tables)) + @eval mod Base.Experimental.@overlay method_table invoked(x::Any) = 1 + @test occursin("ret i64 1", cached_llvm(mod.invoke_kernel; method_table=tables)) + + # so do reflection queries + @test occursin("ret i64 0", cached_llvm(mod.applicable_kernel; method_table=tables)) + @eval mod Base.Experimental.@overlay other_method_table probe(x::Int) = nothing + @test occursin("ret i64 1", cached_llvm(mod.applicable_kernel; method_table=tables)) end @testset "shared method table" begin