Skip to content

Commit 6f52a98

Browse files
authored
Allow constant propagation of mutables with const fields (#58371)
This make the ScopedValue code slightly better by folding away the has_default and default lookups Still needs a test
1 parent 157b8bc commit 6f52a98

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Compiler/src/abstractlattice.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ that should be forwarded along with constant propagation.
227227
end
228228
@nospecializeinfer function is_const_prop_profitable_arg(𝕃::ConstsLattice, @nospecialize t)
229229
if isa(t, Const)
230-
# don't consider mutable values useful constants
230+
# don't consider mutable values useful constants unless they have const fields
231231
val = t.val
232-
return isa(val, Symbol) || isa(val, Type) || isa(val, Method) || isa(val, CodeInstance) || !ismutable(val)
232+
return isa(val, Symbol) || isa(val, Type) || isa(val, Method) || isa(val, CodeInstance) ||
233+
!ismutable(val) || (typeof(val).name.constfields != C_NULL)
233234
end
234235
isa(t, PartialTypeVar) && return false # this isn't forwardable
235236
return is_const_prop_profitable_arg(widenlattice(𝕃), t)

Compiler/test/irpasses.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,3 +2111,13 @@ let src = code_typed1(()) do
21112111
end
21122112
@test count(iscall((src, isdefined)), src.code) == 0
21132113
end
2114+
# We should successfully fold the default values of a ScopedValue
2115+
const svalconstprop = ScopedValue(1)
2116+
foosvalconstprop() = svalconstprop[]
2117+
2118+
let src = code_typed1(foosvalconstprop, ())
2119+
function is_constfield_load(expr)
2120+
iscall((src, getfield))(expr) && expr.args[3] in (:(:has_default), :(:default))
2121+
end
2122+
@test count(is_constfield_load, src.code) == 0
2123+
end

0 commit comments

Comments
 (0)