Skip to content

Commit

Permalink
inference: cleanup some lattice operations (#49271)
Browse files Browse the repository at this point in the history
The smerge function was bypassing some of the limits of tmerge, which
was undesirable, though the impact is apparently negligible.

Co-authored-by: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com>
Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 7, 2023
1 parent f2cbca6 commit 8baf10e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2991,7 +2991,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
if !isempty(frame.limitations)
rt = LimitedAccuracy(rt, copy(frame.limitations))
end
if tchanged(𝕃ₚ, rt, bestguess)
if !(𝕃ₚ, rt, bestguess)
# new (wider) return type for frame
bestguess = tmerge(𝕃ₚ, bestguess, rt)
# TODO: if bestguess isa InterConditional && !interesting(bestguess); bestguess = widenconditional(bestguess); end
Expand Down
9 changes: 1 addition & 8 deletions base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -736,22 +736,15 @@ widenconst(::LimitedAccuracy) = error("unhandled LimitedAccuracy")
# state management #
####################

issubstate(lattice::AbstractLattice, a::VarState, b::VarState) =
(lattice, a.typ, b.typ) && a.undef <= b.undef

function smerge(lattice::AbstractLattice, sa::Union{NotFound,VarState}, sb::Union{NotFound,VarState})
sa === sb && return sa
sa === NOT_FOUND && return sb
sb === NOT_FOUND && return sa
issubstate(lattice, sa, sb) && return sb
issubstate(lattice, sb, sa) && return sa
return VarState(tmerge(lattice, sa.typ, sb.typ), sa.undef | sb.undef)
end

@inline tchanged(lattice::AbstractLattice, @nospecialize(n), @nospecialize(o)) =
o === NOT_FOUND || (n !== NOT_FOUND && !(lattice, n, o))
@inline schanged(lattice::AbstractLattice, @nospecialize(n), @nospecialize(o)) =
(n !== o) && (o === NOT_FOUND || (n !== NOT_FOUND && !issubstate(lattice, n::VarState, o::VarState)))
(n !== o) && (o === NOT_FOUND || (n !== NOT_FOUND && !(n.undef <= o.undef && (lattice, n.typ, o.typ))))

# remove any lattice elements that wrap the reassigned slot object from the vartable
function invalidate_slotwrapper(vt::VarState, changeid::Int, ignore_conditional::Bool)
Expand Down
9 changes: 8 additions & 1 deletion base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ end
function issimplertype(𝕃::AbstractLattice, @nospecialize(typea), @nospecialize(typeb))
typea isa MaybeUndef && (typea = typea.typ) # n.b. does not appear in inference
typeb isa MaybeUndef && (typeb = typeb.typ) # n.b. does not appear in inference
@assert !isa(typea, LimitedAccuracy) && !isa(typeb, LimitedAccuracy) "LimitedAccuracy not supported by simplertype lattice" # n.b. the caller was supposed to handle these
typea === typeb && return true
if typea isa PartialStruct
aty = widenconst(typea)
Expand All @@ -327,7 +328,7 @@ function issimplertype(𝕃::AbstractLattice, @nospecialize(typea), @nospecializ
end
elseif typea isa Type
return issimpleenoughtype(typea)
# elseif typea isa Const # fall-through good
# elseif typea isa Const # fall-through to true is good
elseif typea isa Conditional # follow issubconditional query
typeb isa Const && return true
typeb isa Conditional || return false
Expand All @@ -352,6 +353,12 @@ function issimplertype(𝕃::AbstractLattice, @nospecialize(typea), @nospecializ
issimplertype(𝕃, typea.fldtyp, typeb.fldtyp) || return false
elseif typea isa PartialOpaque
# TODO
aty = widenconst(typea)
bty = widenconst(typeb)
if typea.source === typeb.source && typea.parent === typeb.parent && aty == bty && typea.env == typeb.env
return false
end
return false
end
return true
end
Expand Down

0 comments on commit 8baf10e

Please sign in to comment.