From 4f343a414e23dbea18b9338ef8db02cf8ef436e0 Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Thu, 13 Nov 2025 18:23:56 +0100 Subject: [PATCH 1/2] avoid overflow when `inline_cost_threshold == typemax(Int)` GPUCompiler will set `inline_cost_threshold` to `typemax(Int)` as a way to force inlining, make sure this doesn't cause integer overflow. should fix JuliaGPU/AMDGPU.jl#846 --- Compiler/src/typeinfer.jl | 26 ++++++++++++++------------ Compiler/test/inline.jl | 10 ++++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Compiler/src/typeinfer.jl b/Compiler/src/typeinfer.jl index 2ae0c627b1b4d..d645d7f444b0a 100644 --- a/Compiler/src/typeinfer.jl +++ b/Compiler/src/typeinfer.jl @@ -440,18 +440,20 @@ function inline_cost_model(interp::AbstractInterpreter, result::InferenceResult, # compute the cost (size) of inlining this code params = OptimizationParams(interp) cost_threshold = default = params.inline_cost_threshold - if ⊑(optimizer_lattice(interp), rt, Tuple) && !isconcretetype(widenconst(rt)) - cost_threshold += params.inline_tupleret_bonus - end - # if the method is declared as `@inline`, increase the cost threshold 20x - if declared_inline - cost_threshold += 19*default - end - # a few functions get special treatment - if def.module === _topmod(def.module) - name = def.name - if name === :iterate || name === :unsafe_convert || name === :cconvert - cost_threshold += 4*default + if cost_threshold != typemax(Int) + if ⊑(optimizer_lattice(interp), rt, Tuple) && !isconcretetype(widenconst(rt)) + cost_threshold += params.inline_tupleret_bonus + end + # if the method is declared as `@inline`, increase the cost threshold 20x + if declared_inline + cost_threshold += 19*default + end + # a few functions get special treatment + if def.module === _topmod(def.module) + name = def.name + if name === :iterate || name === :unsafe_convert || name === :cconvert + cost_threshold += 4*default + end end end return inline_cost_model(ir, params, cost_threshold) diff --git a/Compiler/test/inline.jl b/Compiler/test/inline.jl index db60d2a924a5b..a7eb81b0465d9 100644 --- a/Compiler/test/inline.jl +++ b/Compiler/test/inline.jl @@ -2345,4 +2345,14 @@ let src = code_typed1(Base.setindex, (@NamedTuple{next::UInt32,prev::UInt32}, In @test count(iscall((src, Base.merge_fallback)), src.code) == 0 end +let + interp = Compiler.NativeInterpreter(; opt_params=Compiler.OptimizationParams(; inline_cost_threshold=typemax(Int))) + res = Base.code_ircode((Tuple{Tuple{Val{4}}, Tuple{Float32}},); interp) do t + t[1] + end + @test !isempty(res) + ir, rt = only(res) + @test rt == Tuple{Val{4}} +end + end # module inline_tests From 1e40211f4901618f08941796aa4ff71c8f17d66f Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Thu, 13 Nov 2025 21:52:13 +0100 Subject: [PATCH 2/2] fix test --- Compiler/test/inline.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Compiler/test/inline.jl b/Compiler/test/inline.jl index a7eb81b0465d9..afc5b12119bb6 100644 --- a/Compiler/test/inline.jl +++ b/Compiler/test/inline.jl @@ -2345,11 +2345,10 @@ let src = code_typed1(Base.setindex, (@NamedTuple{next::UInt32,prev::UInt32}, In @test count(iscall((src, Base.merge_fallback)), src.code) == 0 end +f60121(t) = t[1] let interp = Compiler.NativeInterpreter(; opt_params=Compiler.OptimizationParams(; inline_cost_threshold=typemax(Int))) - res = Base.code_ircode((Tuple{Tuple{Val{4}}, Tuple{Float32}},); interp) do t - t[1] - end + res = Base.code_ircode(f60121, (Tuple{Tuple{Val{4}}, Tuple{Float32}},); interp) @test !isempty(res) ir, rt = only(res) @test rt == Tuple{Val{4}}