Skip to content

Commit 0ff8f82

Browse files
aviateskKristofferC
authored and
KristofferC
committed
irinterp: set IR_FLAG_REFINED for narrowed PhiNodes (#56391)
`adce_pass!` can transform a `Union`-type `PhiNode` into a narrower `PhiNode`, but in such cases, the `IR_FLAG_REFINED` flag isn’t set on that `PhiNode` statement. By setting this flag, irinterp can perform statement reprocessing using the narrowed `PhiNode`, enabling type stability in cases like #56387. - fixes #56387 (cherry picked from commit 671e1d8)
1 parent d031595 commit 0ff8f82

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

base/compiler/ssair/passes.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,18 +2075,19 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
20752075
unionphi = unionphis[i]
20762076
phi = unionphi[1]
20772077
t = unionphi[2]
2078+
inst = compact.result[phi]
20782079
if t === Union{}
2079-
stmt = compact[SSAValue(phi)][:stmt]::PhiNode
2080+
stmt = inst[:stmt]::PhiNode
20802081
kill_phi!(compact, phi_uses, 1:length(stmt.values), SSAValue(phi), stmt, true)
20812082
made_changes = true
20822083
continue
20832084
elseif t === Any
20842085
continue
2085-
elseif (𝕃ₒ, compact.result[phi][:type], t)
2086-
continue
20872086
end
2087+
= strictpartialorder(𝕃ₒ)
2088+
t inst[:type] || continue
20882089
to_drop = Int[]
2089-
stmt = compact[SSAValue(phi)][:stmt]
2090+
stmt = inst[:stmt]
20902091
stmt === nothing && continue
20912092
stmt = stmt::PhiNode
20922093
for i = 1:length(stmt.values)
@@ -2098,7 +2099,8 @@ function adce_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
20982099
push!(to_drop, i)
20992100
end
21002101
end
2101-
compact.result[phi][:type] = t
2102+
inst[:type] = t
2103+
add_flag!(inst, IR_FLAG_REFINED) # t ⊏ inst[:type]
21022104
kill_phi!(compact, phi_uses, to_drop, SSAValue(phi), stmt, false)
21032105
made_changes = true
21042106
end

test/compiler/inference.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5693,3 +5693,15 @@ end === Union{}
56935693
@test Base.infer_return_type() do
56945694
TypeVar(:Issue56248, Any, 1)
56955695
end === Union{}
5696+
5697+
function issue56387(nt::NamedTuple, field::Symbol=:a)
5698+
NT = typeof(nt)
5699+
names = fieldnames(NT)
5700+
types = fieldtypes(NT)
5701+
index = findfirst(==(field), names)
5702+
if index === nothing
5703+
throw(ArgumentError("Field $field not found"))
5704+
end
5705+
types[index]
5706+
end
5707+
@test Base.infer_return_type(issue56387, (typeof((;a=1)),)) == Type{Int}

0 commit comments

Comments
 (0)