From 56ef06de3a1635f66747e56cf62ef6395a2bface Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Wed, 2 Aug 2023 15:07:09 -0400 Subject: [PATCH] optimizer: move inlineable constants into statement position during `compact!`-ion In code like below ```julia Base.@assume_effects :nothrow function erase_before_inlining(x, y) z = sin(y) if x return "julia" end return z end let y::Float64 length(erase_before_inlining(true, y)) end ``` the constant prop' can figure out the constant return type of `erase_before_inlining(true, y)` while it is profitable not to inline expand it since otherwise we left some `!:nothrow` callees there (xref: #47305). In order to workaround this problem, this commit makes `compact!`move inlineable constants into statement positions so that the such "inlineable, but safe as a whole" calls to be erased during compaction. This should give us general compile-time performance improvement too as we no longer need to expand the IR for those calls. --- base/compiler/ssair/ir.jl | 7 ++++++- test/compiler/inline.jl | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/base/compiler/ssair/ir.jl b/base/compiler/ssair/ir.jl index ef29165a94c19..b6f9cd3915042 100644 --- a/base/compiler/ssair/ir.jl +++ b/base/compiler/ssair/ir.jl @@ -1341,7 +1341,12 @@ function process_node!(compact::IncrementalCompact, result_idx::Int, inst::Instr return result_idx end end - ssa_rename[idx] = SSAValue(result_idx) + typ = inst[:type] + if isa(typ, Const) && is_inlineable_constant(typ.val) + ssa_rename[idx] = quoted(typ.val) + else + ssa_rename[idx] = SSAValue(result_idx) + end result[result_idx][:inst] = stmt result_idx += 1 elseif isa(stmt, PiNode) diff --git a/test/compiler/inline.jl b/test/compiler/inline.jl index 3bc7ab5ccbc82..5d858dc88c755 100644 --- a/test/compiler/inline.jl +++ b/test/compiler/inline.jl @@ -2065,3 +2065,16 @@ end # https://github.com/JuliaLang/julia/issues/50612 f50612(x) = UInt32(x) @test all(!isinvoke(:UInt32),get_code(f50612,Tuple{Char})) + +# move inlineable constant values into statement position during `compact!`-ion +# so that we don't inline DCE-eligibile calls +Base.@assume_effects :nothrow function erase_before_inlining(x, y) + z = sin(y) + if x + return "julia" + end + return z +end +@test fully_eliminated((Float64,); retval=5) do y + length(erase_before_inlining(true, y)) +end