From 40ae2e56f0545ab1677ed5d53796cea3c947363d Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Fri, 9 May 2025 18:18:31 -0300 Subject: [PATCH 1/2] Allow contant propagation of mutables with const fields --- Compiler/src/abstractlattice.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Compiler/src/abstractlattice.jl b/Compiler/src/abstractlattice.jl index f1514c6c9eed6..4d0accedfc765 100644 --- a/Compiler/src/abstractlattice.jl +++ b/Compiler/src/abstractlattice.jl @@ -227,9 +227,10 @@ that should be forwarded along with constant propagation. end @nospecializeinfer function is_const_prop_profitable_arg(𝕃::ConstsLattice, @nospecialize t) if isa(t, Const) - # don't consider mutable values useful constants + # don't consider mutable values useful constants unless they have const fields val = t.val - return isa(val, Symbol) || isa(val, Type) || isa(val, Method) || isa(val, CodeInstance) || !ismutable(val) + return isa(val, Symbol) || isa(val, Type) || isa(val, Method) || isa(val, CodeInstance) || + !ismutable(val) || (typeof(val).name.constfields != C_NULL) end isa(t, PartialTypeVar) && return false # this isn't forwardable return is_const_prop_profitable_arg(widenlattice(𝕃), t) From ceba08eb83a454731e6f92376be5d6383aa123ad Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Tue, 13 May 2025 10:48:02 -0300 Subject: [PATCH 2/2] Add test --- Compiler/test/irpasses.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Compiler/test/irpasses.jl b/Compiler/test/irpasses.jl index d777a2c387b3d..758efaab9ab6b 100644 --- a/Compiler/test/irpasses.jl +++ b/Compiler/test/irpasses.jl @@ -2111,3 +2111,13 @@ let src = code_typed1(()) do end @test count(iscall((src, isdefined)), src.code) == 0 end +# We should successfully fold the default values of a ScopedValue +const svalconstprop = ScopedValue(1) +foosvalconstprop() = svalconstprop[] + +let src = code_typed1(foosvalconstprop, ()) + function is_constfield_load(expr) + iscall((src, getfield))(expr) && expr.args[3] in (:(:has_default), :(:default)) + end + @test count(is_constfield_load, src.code) == 0 +end