From 925afc6dd0901f5f23a004140cb4a43b014a72c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belmant?= Date: Thu, 30 Jan 2025 01:28:07 -0500 Subject: [PATCH 1/3] Update defuses based on DCE results --- Compiler/src/ssair/passes.jl | 4 ++++ Compiler/test/irpasses.jl | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Compiler/src/ssair/passes.jl b/Compiler/src/ssair/passes.jl index ff333b9b0a129..c9b3d5515caa3 100644 --- a/Compiler/src/ssair/passes.jl +++ b/Compiler/src/ssair/passes.jl @@ -1511,6 +1511,10 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing) used_ssas[x.id] -= 1 end ir = complete(compact) + # remove any use that has been optimized away by the DCE + for (intermediaries, defuse) in values(defuses) + filter!(x -> ir[SSAValue(x.idx)][:stmt] !== nothing, defuse.uses) + end sroa_mutables!(ir, defuses, used_ssas, lazydomtree, inlining) return ir else diff --git a/Compiler/test/irpasses.jl b/Compiler/test/irpasses.jl index 27b6d75f86c93..5aaa7d492fbc5 100644 --- a/Compiler/test/irpasses.jl +++ b/Compiler/test/irpasses.jl @@ -2030,3 +2030,19 @@ let code = Any[ ir = Compiler.domsort_ssa!(ir, domtree) Compiler.verify_ir(ir) end + +# https://github.com/JuliaLang/julia/issues/57141 +# don't eliminate `setfield!` when the field is to be used +let src = code_typed1(()) do + f = function () + ref = Ref{Any}() + ref[] = 0 + @assert isdefined(ref, :x) + inner() = ref[] + 1 + (inner(), ref[]) + end + first(f()) + end + ex = src.code[2] + @test isexpr(ex, :call) && argextype(ex.args[1], src) === Const(setfield!) +end From 2dcdb5405355691b8b336d9935cc01404c4ff122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belmant?= Date: Mon, 3 Feb 2025 09:30:49 +0100 Subject: [PATCH 2/3] Update Compiler/test/irpasses.jl Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> --- Compiler/test/irpasses.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compiler/test/irpasses.jl b/Compiler/test/irpasses.jl index 5aaa7d492fbc5..d6f4388003d76 100644 --- a/Compiler/test/irpasses.jl +++ b/Compiler/test/irpasses.jl @@ -2044,5 +2044,5 @@ let src = code_typed1(()) do first(f()) end ex = src.code[2] - @test isexpr(ex, :call) && argextype(ex.args[1], src) === Const(setfield!) + @test count(iscall((src, setfield!)), src.code) == 1 end From f19dad96720fddfd8d64a22acc40a50c9aa8de41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belmant?= Date: Mon, 3 Feb 2025 03:32:12 -0500 Subject: [PATCH 3/3] Shorten test --- Compiler/test/irpasses.jl | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Compiler/test/irpasses.jl b/Compiler/test/irpasses.jl index d6f4388003d76..0d0b0e4daf83e 100644 --- a/Compiler/test/irpasses.jl +++ b/Compiler/test/irpasses.jl @@ -2034,15 +2034,11 @@ end # https://github.com/JuliaLang/julia/issues/57141 # don't eliminate `setfield!` when the field is to be used let src = code_typed1(()) do - f = function () - ref = Ref{Any}() - ref[] = 0 - @assert isdefined(ref, :x) - inner() = ref[] + 1 - (inner(), ref[]) - end - first(f()) + ref = Ref{Any}() + ref[] = 0 + @assert isdefined(ref, :x) + inner() = ref[] + 1 + (inner(), ref[]) end - ex = src.code[2] @test count(iscall((src, setfield!)), src.code) == 1 end