Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference: propagate struct initialization info on setfield! #57222

Merged
merged 8 commits into from
Feb 8, 2025
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
20 changes: 16 additions & 4 deletions Compiler/src/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2665,12 +2665,24 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
end
pushfirst!(argtypes, ft)
refinements = nothing
if sv isa InferenceState && f === typeassert
# perform very limited back-propagation of invariants after this type assertion
if rt !== Bottom && isa(fargs, Vector{Any})
if sv isa InferenceState
if f === typeassert
# perform very limited back-propagation of invariants after this type assertion
if rt !== Bottom && isa(fargs, Vector{Any})
farg2 = ssa_def_slot(fargs[2], sv)
if farg2 isa SlotNumber
refinements = SlotRefinement(farg2, rt)
end
end
elseif f === setfield! && length(argtypes) == 4 && isa(argtypes[3], Const)
# from there on we know that the struct field will never be undefined,
# so we try to encode that information with a `PartialStruct`
farg2 = ssa_def_slot(fargs[2], sv)
if farg2 isa SlotNumber
refinements = SlotRefinement(farg2, rt)
refined = form_partially_defined_struct(argtypes[2], argtypes[3])
if refined !== nothing
refinements = SlotRefinement(farg2, refined)
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions Compiler/test/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2042,3 +2042,14 @@ let src = code_typed1(()) do
end
@test count(iscall((src, setfield!)), src.code) == 1
end

# optimize `isdefined` away in the presence of a dominating `setfield!`
let src = code_typed1(()) do
a = Ref{Any}()
setfield!(a, :x, 2)
invokelatest(identity, a)
isdefined(a, :x) && return 1.0
a[]
end
@test count(iscall((src, isdefined)), src.code) == 0
end