Skip to content

Commit

Permalink
lattice: Restore tmerge_fast_path for optimizer lattice (JuliaLang#…
Browse files Browse the repository at this point in the history
…50291)

This function is slightly misnamed, because, while it does perform
some fast pathing, the rest of the tmerge functions assume it has
been run. When I removed the OptimizerLattice in JuliaLang#50257, we
accidentally lost the call to `tmerge_fast_path` in the optimizer_lattice
path, regressing tmerge quality in the optimizer (which admittedly
we don't use a lot, but I did just add another use for in JuliaLang#50287).
The proper fix is probably to split the tmerge into a helper that
always calls the fast path and then delegates to the current
implementation for everything else, but for now, just try moving
the fast path to the start of the PartialsLattice, which should
restore correctness and have reasonable performance, since
there's only a handful of pointer checks between the entry to the
inference tmerge and the partials lattice.
  • Loading branch information
Keno committed Jun 26, 2023
1 parent 879f6d4 commit ed338d0
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ end
return tmerge_limited(lattice, typea, typeb)
end

r = tmerge_fast_path(widenlattice(lattice), typea, typeb)
r !== nothing && return r
return tmerge(widenlattice(lattice), typea, typeb)
end

Expand Down Expand Up @@ -491,8 +489,13 @@ end
end
return Bool
end
typea = widenconditional(typea)
typeb = widenconditional(typeb)
if isa(typea, Conditional)
typeb === Union{} && return typea
typea = widenconditional(typea)
elseif isa(typeb, Conditional)
typea === Union{} && return typeb
typeb = widenconditional(typeb)
end
return tmerge(widenlattice(lattice), typea, typeb)
end

Expand Down Expand Up @@ -526,14 +529,25 @@ end
end
return Bool
end
typea = widenconditional(typea)
typeb = widenconditional(typeb)
if isa(typea, InterConditional)
typeb === Union{} && return typea
typea = widenconditional(typea)
elseif isa(typeb, InterConditional)
typea === Union{} && return typeb
typeb = widenconditional(typeb)
end
return tmerge(widenlattice(lattice), typea, typeb)
end

@nospecializeinfer function tmerge(𝕃::AnyMustAliasesLattice, @nospecialize(typea), @nospecialize(typeb))
typea = widenmustalias(typea)
typeb = widenmustalias(typeb)
if is_valid_lattice_norec(𝕃, typea)
typeb === Union{} && return typea
typea = widenmustalias(typea)
end
if is_valid_lattice_norec(𝕃, typeb)
typea === Union{} && return typeb
typeb = widenmustalias(typeb)
end
return tmerge(widenlattice(𝕃), typea, typeb)
end

Expand Down Expand Up @@ -598,6 +612,9 @@ end
end

@nospecializeinfer function tmerge(lattice::PartialsLattice, @nospecialize(typea), @nospecialize(typeb))
r = tmerge_fast_path(lattice, typea, typeb)
r !== nothing && return r

# type-lattice for Const and PartialStruct wrappers
aps = isa(typea, PartialStruct)
bps = isa(typeb, PartialStruct)
Expand Down

0 comments on commit ed338d0

Please sign in to comment.