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: fix lattice for unusual InterConditional return and Const Bool representations #57080

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion Compiler/src/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3785,14 +3785,23 @@ function update_bestguess!(interp::AbstractInterpreter, frame::InferenceState,
slottypes = frame.slottypes
rt = widenreturn(rt, BestguessInfo(interp, bestguess, nargs, slottypes, currstate))
# narrow representation of bestguess slightly to prepare for tmerge with rt
if rt isa InterConditional && bestguess isa Const
if rt isa InterConditional && bestguess isa Const && bestguess.val isa Bool
slot_id = rt.slot
old_id_type = widenconditional(slottypes[slot_id])
if bestguess.val === true && rt.elsetype !== Bottom
bestguess = InterConditional(slot_id, old_id_type, Bottom)
elseif bestguess.val === false && rt.thentype !== Bottom
bestguess = InterConditional(slot_id, Bottom, old_id_type)
end
# or narrow representation of rt slightly to prepare for tmerge with bestguess
elseif bestguess isa InterConditional && rt isa Const && rt.val isa Bool
slot_id = bestguess.slot
old_id_type = widenconditional(slottypes[slot_id])
if rt.val === true && bestguess.elsetype !== Bottom
rt = InterConditional(slot_id, old_id_type, Bottom)
elseif rt.val === false && bestguess.thentype !== Bottom
rt = InterConditional(slot_id, Bottom, old_id_type)
end
end
# copy limitations to return value
if !isempty(frame.pclimitations)
Expand Down
7 changes: 7 additions & 0 deletions Compiler/src/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ end
end
a = Bool
elseif isa(b, ConditionalT)
if isa(a, Const) && isa(a.val, Bool)
if (a.val === true && b.thentype === Any && b.elsetype === Bottom) ||
(a.val === false && b.elsetype === Any && b.thentype === Bottom)
# this Conditional contains distinctly no lattice information, and is simply an alternative representation of the Const Bool used for internal tracking purposes
return true
end
end
return false
end
return ⊑(widenlattice(lattice), a, b)
Expand Down
20 changes: 20 additions & 0 deletions Compiler/test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,26 @@ vacond(cnd, va...) = cnd ? va : 0
vacond(isa(x, Tuple{Int,Int}), x, x)
end |> only == Union{Int,Tuple{Any,Any}}

let A = Core.Const(true)
B = Core.InterConditional(2, Tuple, Union{})
C = Core.InterConditional(2, Any, Union{})
L = ipo_lattice(Compiler.NativeInterpreter())
@test !⊑(L, A, B)
@test ⊑(L, B, A)
@test tmerge(L, A, B) == C
@test ⊑(L, A, C)
end
function tail_is_ntuple((@nospecialize t::Tuple))
if unknown
t isa Tuple
else
tail_is_ntuple(t)
end
end
tail_is_ntuple_val((@nospecialize t::Tuple)) = Val(tail_is_ntuple(t))
@test Base.return_types(tail_is_ntuple, (Tuple,)) |> only === Bool
@test Base.return_types(tail_is_ntuple_val, (Tuple,)) |> only === Val{true}

# https://github.com/JuliaLang/julia/issues/47435
is_closed_ex(e::InvalidStateException) = true
is_closed_ex(e) = false
Expand Down
Loading