From ed338d000a874241032b3d553440d1ac75c3dd21 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Mon, 26 Jun 2023 03:27:33 -0400 Subject: [PATCH] lattice: Restore `tmerge_fast_path` for optimizer lattice (#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 #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 #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. --- base/compiler/typelimits.jl | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/base/compiler/typelimits.jl b/base/compiler/typelimits.jl index 81a176dff1c9d..b648144ea3bd1 100644 --- a/base/compiler/typelimits.jl +++ b/base/compiler/typelimits.jl @@ -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 @@ -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 @@ -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 @@ -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)