From 9018fd27927acf28b47059ce267d8e55ad0f34c1 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 3 Dec 2026 20:23:13 +0100 Subject: [PATCH] Let back-ends name methods to always inline The job-wide `always_inline` option marks all eligible specializations cheap to inline. Back-ends that need only a few methods inlined, e.g. a Base wrapper whose constant arguments a Julia IR pass has to see in the caller, can now list them with `always_inline_methods(job)`. Their specializations are marked cheap the same way, so call-site guards and constant propagation are kept. Dispatch doesn't change, and other methods of the same function, including user overloads, follow Julia's usual policy. The list is part of the inference configuration, like `julia_ir_passes`, so it must be a pure function of the job's target and params. --- src/interface.jl | 37 ++++++++++++++++++++++----- src/jlgen.jl | 21 ++++++++++----- test/native.jl | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index ac764f5d..f25d508f 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -115,9 +115,9 @@ Julia 1.11+, the job-level `cache_owner(job)` returns the pre-boxed token stored The default token covers the full `target` and `params` instances plus `always_inline`. That is sufficient because the inference inputs derived from a job — `method_table`, -`method_tables`, `method_table_view`, `inference_params`, `optimization_params` and -`julia_ir_passes` — must be pure functions of those values, so back-ends normally leave this -untouched. +`method_tables`, `method_table_view`, `inference_params`, `optimization_params`, +`julia_ir_passes` and `always_inline_methods` — must be pure functions of those values, so +back-ends normally leave this untouched. When overriding, the returned value must match under `===`/`jl_egal` after package-image deserialization: use immutable containers, and only reference mutable objects (like method @@ -439,6 +439,7 @@ function get_interpreter(@nospecialize(job::CompilerJob)) inf_params=inference_params(job), opt_params=optimization_params(job), always_inline=job.config.always_inline, + always_inline_methods=always_inline_methods(job), julia_ir_passes=julia_ir_passes(job)) end else @@ -449,6 +450,7 @@ function get_interpreter(@nospecialize(job::CompilerJob)) inf_params=inference_params(job), opt_params=optimization_params(job), always_inline=job.config.always_inline, + always_inline_methods=always_inline_methods(job), julia_ir_passes=julia_ir_passes(job)) end end @@ -682,10 +684,10 @@ end # HAS_INTEGRATED_CACHE # the method table to use # -# NOTE: these (like `inference_params`, `optimization_params` and `julia_ir_passes` below) -# may only depend on the job's world and its config's `target`/`params` values -# (+ `always_inline`); [`cache_owner`](@ref) relies on that to partition inference -# results correctly. +# NOTE: these (like `inference_params`, `optimization_params`, `julia_ir_passes` and +# `always_inline_methods` below) may only depend on the job's world and its config's +# `target`/`params` values (+ `always_inline`); [`cache_owner`](@ref) relies on that +# to partition inference results correctly. # deprecate method_table on next-breaking release method_table(@nospecialize(job::CompilerJob)) = GLOBAL_METHOD_TABLE @@ -763,6 +765,27 @@ function julia_ir_passes(@nospecialize(job::CompilerJob)) return passes end +""" + always_inline_methods(job::CompilerJob)::Tuple{Vararg{Method}} + +Methods to inline into their callers in code compiled for `job`, as if `always_inline` were +set for them only. Their specializations are marked cheap to inline, except for those of +methods declared `@noinline`, and those that always throw unless declared `@inline`; Julia +still decides per call, e.g. not where the call is annotated `@noinline`. Which method a call +dispatches to doesn't change, and other methods follow Julia's usual policy. The default is +`()`. Custom interpreters (see `get_interpreter`) have to implement this themselves. + +This is for methods that a back-end needs inlined but doesn't define itself, e.g. a Base +wrapper whose arguments a Julia IR pass (see `julia_ir_passes`) has to see as constants. +List every method that dispatch can select: Base may define, e.g., `println(x)` and +`println(x1, x2)` next to `println(xs...)`. + +Like [`method_tables`](@ref), this may only depend on the job's `target` and `params` (see +[`cache_owner`](@ref)), so return a fixed tuple. +""" +always_inline_methods(@nospecialize(job::CompilerJob)) = () +@public always_inline_methods + # how much debuginfo to emit function llvm_debug_info(@nospecialize(job::CompilerJob)) if job.config.debug_level == 0 diff --git a/src/jlgen.jl b/src/jlgen.jl index 8602a22d..ad4092a3 100644 --- a/src/jlgen.jl +++ b/src/jlgen.jl @@ -298,6 +298,8 @@ end opt_params::CC.OptimizationParams always_inline::Bool + # methods inlined as if `always_inline` were set (see `always_inline_methods`) + always_inline_methods::Tuple{Vararg{Method}} # passes to run over the optimized Julia IR (see `julia_ir_passes`) julia_ir_passes::Tuple @@ -310,11 +312,12 @@ function GPUInterpreter(world::UInt=Base.get_world_counter(); inf_params::CC.InferenceParams, opt_params::CC.OptimizationParams, always_inline::Bool=false, + always_inline_methods::Tuple{Vararg{Method}}=(), julia_ir_passes::Tuple=()) @assert world <= Base.get_world_counter() return GPUInterpreter{typeof(method_table_view)}( world, method_table_view, owner, INFERENCE_CACHE_TYPE(), - inf_params, opt_params, always_inline, julia_ir_passes) + inf_params, opt_params, always_inline, always_inline_methods, julia_ir_passes) end function GPUInterpreter(interp::GPUInterpreter; @@ -325,10 +328,11 @@ function GPUInterpreter(interp::GPUInterpreter; inf_params::CC.InferenceParams=interp.inf_params, opt_params::CC.OptimizationParams=interp.opt_params, always_inline::Bool=interp.always_inline, + always_inline_methods::Tuple{Vararg{Method}}=interp.always_inline_methods, julia_ir_passes::Tuple=interp.julia_ir_passes) return GPUInterpreter{typeof(method_table_view)}( world, method_table_view, owner, inf_cache, - inf_params, opt_params, always_inline, julia_ir_passes) + inf_params, opt_params, always_inline, always_inline_methods, julia_ir_passes) end CC.cache_owner(interp::GPUInterpreter) = interp.owner @@ -341,11 +345,12 @@ function GPUInterpreter(world::UInt=Base.get_world_counter(); inf_params::CC.InferenceParams, opt_params::CC.OptimizationParams, always_inline::Bool=false, + always_inline_methods::Tuple{Vararg{Method}}=(), julia_ir_passes::Tuple=()) @assert world <= Base.get_world_counter() return GPUInterpreter{typeof(method_table_view)}( world, method_table_view, code_cache, Vector{CC.InferenceResult}(), - inf_params, opt_params, always_inline, julia_ir_passes) + inf_params, opt_params, always_inline, always_inline_methods, julia_ir_passes) end function GPUInterpreter(interp::GPUInterpreter; @@ -356,10 +361,11 @@ function GPUInterpreter(interp::GPUInterpreter; inf_params::CC.InferenceParams=interp.inf_params, opt_params::CC.OptimizationParams=interp.opt_params, always_inline::Bool=interp.always_inline, + always_inline_methods::Tuple{Vararg{Method}}=interp.always_inline_methods, julia_ir_passes::Tuple=interp.julia_ir_passes) return GPUInterpreter{typeof(method_table_view)}( world, method_table_view, code_cache, inf_cache, - inf_params, opt_params, always_inline, julia_ir_passes) + inf_params, opt_params, always_inline, always_inline_methods, julia_ir_passes) end CC.code_cache(interp::GPUInterpreter) = WorldView(interp.code_cache, interp.world) @@ -429,12 +435,13 @@ function CC.concrete_eval_eligible(interp::GPUInterpreter, end # Costs saturate at `MAX_INLINE_COST`, so raising the threshold cannot implement -# `always_inline`. Mark eligible sources cheap instead. Unlike #795's source-policy -# override, this preserves call-site guards and constant propagation (#797). +# `always_inline` or `always_inline_methods`. Mark eligible sources cheap instead. Unlike +# #795's source-policy override, this preserves call-site guards and constant propagation +# (#797). function force_inlineable(interp::GPUInterpreter, mi::MethodInstance, declared_inline::Bool, declared_noinline::Bool, @nospecialize(rt)) - interp.always_inline || return false + (interp.always_inline || mi.def in interp.always_inline_methods) || return false declared_noinline && return false isa(mi.def, Method) || return false sig = Base.unwrap_unionall(mi.specTypes) diff --git a/test/native.jl b/test/native.jl index 380c96aa..27e64377 100644 --- a/test/native.jl +++ b/test/native.jl @@ -1517,6 +1517,72 @@ end @test :callee in mod.seen end +@testset "always_inline_methods" begin + mod = @eval module $(gensym()) + using ..GPUCompiler + import ..sink + + # an expensive wrapper, and a more specific method of the same function + wrapper(xs...) = $(foldl((e, _) -> :($sink($e) + $sink(xs[1])), 1:100; init=:(xs[1]))) + wrapper(x::Int) = $(foldl((e, _) -> :($sink($e) + $sink(x)), 1:100; init=:x)) + @noinline never(xs...) = sink(xs[1]) + + # how often the optimizer finished a specialization of the vararg wrapper + const optimized = Ref(0) + function count!(interp, opt, ir) + opt.linfo.def === which(wrapper, Tuple{Vararg{Any}}) && (optimized[] += 1) + return ir + end + + struct Params <: GPUCompiler.AbstractCompilerParams end + GPUCompiler.always_inline_methods(::CompilerJob{<:Any,Params}) = + (which(wrapper, Tuple{Vararg{Any}}), which(never, Tuple{Vararg{Any}})) + GPUCompiler.julia_ir_passes(job::CompilerJob{<:Any,Params}) = + (@invoke(GPUCompiler.julia_ir_passes(job::CompilerJob))..., count!) + + struct DefaultParams <: GPUCompiler.AbstractCompilerParams end + + caller(x) = (wrapper(x, x); never(x, x); nothing) + other_caller(x) = (wrapper(x, x); nothing) + specific_caller(x) = (wrapper(x); nothing) + end + vararg_wrapper = which(mod.wrapper, Tuple{Vararg{Any}}) + specific_wrapper = which(mod.wrapper, Tuple{Int}) + never = which(mod.never, Tuple{Vararg{Any}}) + + # the methods invoked by the optimized Julia IR of `f(::Int)` + function invoked(f, params) + source = methodinstance(typeof(f), Tuple{Int}, Base.get_world_counter()) + config = CompilerConfig(NativeCompilerTarget(), params; kernel=false) + ci, _ = only(GPUCompiler.code_typed(CompilerJob(source, config))) + methods = Set{Method}() + for stmt in ci.code + Meta.isexpr(stmt, :invoke) || continue + callee = stmt.args[1] + callee isa Core.CodeInstance && (callee = callee.def) + push!(methods, callee.def) + end + return methods + end + + # the listed methods are inlined, unless declared `@noinline` + callees = invoked(mod.caller, mod.Params()) + @test vararg_wrapper ∉ callees + @test never ∈ callees + + # also from the cache, into a caller compiled later + optimized = mod.optimized[] + @test optimized > 0 + @test vararg_wrapper ∉ invoked(mod.other_caller, mod.Params()) + @test mod.optimized[] == optimized + + # other methods of the same function are not + @test specific_wrapper ∈ invoked(mod.specific_caller, mod.Params()) + + # and jobs that don't list the wrapper don't inline it + @test vararg_wrapper ∈ invoked(mod.other_caller, mod.DefaultParams()) +end + @testset "throw arguments" begin mod = @eval module $(gensym()) # an exception object that Julia can prove removable