Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Compiler/src/abstractlattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +232 to +233
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about mutable singletons? Something like:

mutable struct Singleton end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC those get folded via the type system already. I couldn't make a case where it didnt do it (I only saw this case here with scoped values)

end
isa(t, PartialTypeVar) && return false # this isn't forwardable
return is_const_prop_profitable_arg(widenlattice(𝕃), t)
Expand Down
10 changes: 10 additions & 0 deletions Compiler/test/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading