Skip to content

Let back-ends name methods to always inline - #1150

Open
maleadt wants to merge 1 commit into
mainfrom
tb/inline-hook
Open

maleadt wants to merge 1 commit into
mainfrom
tb/inline-hook

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026

Copy link
Copy Markdown
Member

always_inline is all or nothing: it makes every function of the job cheap to inline. A back-end sometimes needs only a few methods it doesn't own inlined into their callers. The case at hand is CUDA.jl's native print/println in kernels (JuliaGPU/CUDA.jl#3665): string literals are turned into Symbols by a Julia IR pass, which only sees the literal once every frame between it and the hostcall has been inlined. With stdout overlaid to a device IO (#1147), println("a", "b") goes through Base's own println(xs...), which isn't @inline and exceeds the inlining threshold with two or more literals (print(xs...) with three), so the kernel fails to compile:

julia> k() = (println("s1", "s2"); nothing)
julia> @cuda k()
ERROR: InvalidIRError: ... static assertion failed (strings printed in device code must be literals ...)

This adds an interface function listing the methods to treat that way:

GPUCompiler.always_inline_methods(job::CompilerJob{MyTarget}) = MY_METHODS

Specializations of the listed Methods are marked cheap to inline, through the same mechanism as always_inline (#890), so call-site guards and constant propagation are kept, and @noinline methods and always-throwing calls are left alone as before. Dispatch doesn't change: other methods of the same function, like a user's print(io, ::MyType), follow Julia's usual policy. The list is stored on the interpreter like julia_ir_passes and falls under the same cache_owner contract (a pure function of the job's target and params), so caching needs no change, and launches don't call it.

Alternatives: overlaying Base's wrappers with @inline copies shadows every Base and user method they match; raising inline_cost_threshold affects every kernel and still has a limit (8 literals need a threshold of 400 instead of 100); a predicate on MethodInstances or a method-table declaration (like binding overlays) would be more machinery for a fixed per-back-end list.

Tested on Julia 1.10 through 1.15-DEV with a new native test: the listed method is inlined, also when it comes from the cache, while a more specific method of the same function, a @noinline method and other jobs are unaffected. With CUDA.jl listing Base's print/println wrappers, kernels compile with 1 to 9 literals through println(xs...) and print(xs...) (before: at most 1 and 2), and the compile times of unrelated kernels stay within 1% (medians over 14-20 fresh sessions).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant