https://github.com/JuliaLang/julia
Tip revision: 616e45539db4c49ab019b7ccd318800f240743f6 authored by Kristoffer Carlsson on 26 August 2024, 15:46:55 UTC
release-1.11: set VERSION to 1.11.0-rc3 (#55585)
release-1.11: set VERSION to 1.11.0-rc3 (#55585)
Tip revision: 616e455
abstractlattice.jl
# TODO add more documentations
function widenlattice end
function is_valid_lattice_norec end
"""
struct JLTypeLattice <: AbstractLattice
A singleton type representing the lattice of Julia types, without any inference extensions.
"""
struct JLTypeLattice <: AbstractLattice; end
widenlattice(::JLTypeLattice) = error("Type lattice is the least-precise lattice available")
is_valid_lattice_norec(::JLTypeLattice, @nospecialize(elem)) = isa(elem, Type)
"""
struct ConstsLattice <: AbstractLattice
A lattice extending `JLTypeLattice` and adjoining `Const` and `PartialTypeVar`.
"""
struct ConstsLattice <: AbstractLattice; end
widenlattice(::ConstsLattice) = JLTypeLattice()
is_valid_lattice_norec(::ConstsLattice, @nospecialize(elem)) = isa(elem, Const) || isa(elem, PartialTypeVar)
"""
struct PartialsLattice{๐<:AbstractLattice} <: AbstractLattice
A lattice extending a base lattice `๐` and adjoining `PartialStruct` and `PartialOpaque`.
"""
struct PartialsLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
widenlattice(๐::PartialsLattice) = ๐.parent
is_valid_lattice_norec(::PartialsLattice, @nospecialize(elem)) = isa(elem, PartialStruct) || isa(elem, PartialOpaque)
"""
struct ConditionalsLattice{๐<:AbstractLattice} <: AbstractLattice
A lattice extending a base lattice `๐` and adjoining `Conditional`.
"""
struct ConditionalsLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
widenlattice(๐::ConditionalsLattice) = ๐.parent
is_valid_lattice_norec(::ConditionalsLattice, @nospecialize(elem)) = isa(elem, Conditional)
"""
struct InterConditionalsLattice{๐<:AbstractLattice} <: AbstractLattice
A lattice extending a base lattice `๐` and adjoining `InterConditional`.
"""
struct InterConditionalsLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
widenlattice(๐::InterConditionalsLattice) = ๐.parent
is_valid_lattice_norec(::InterConditionalsLattice, @nospecialize(elem)) = isa(elem, InterConditional)
"""
struct MustAliasesLattice{๐<:AbstractLattice}
A lattice extending lattice `๐` and adjoining `MustAlias`.
"""
struct MustAliasesLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
widenlattice(๐::MustAliasesLattice) = ๐.parent
is_valid_lattice_norec(::MustAliasesLattice, @nospecialize(elem)) = isa(elem, MustAlias)
"""
struct InterMustAliasesLattice{๐<:AbstractLattice}
A lattice extending lattice `๐` and adjoining `InterMustAlias`.
"""
struct InterMustAliasesLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
widenlattice(๐::InterMustAliasesLattice) = ๐.parent
is_valid_lattice_norec(::InterMustAliasesLattice, @nospecialize(elem)) = isa(elem, InterMustAlias)
const AnyConditionalsLattice{๐<:AbstractLattice} = Union{ConditionalsLattice{๐}, InterConditionalsLattice{๐}}
const AnyMustAliasesLattice{๐<:AbstractLattice} = Union{MustAliasesLattice{๐}, InterMustAliasesLattice{๐}}
const SimpleInferenceLattice = typeof(PartialsLattice(ConstsLattice()))
const BaseInferenceLattice = typeof(ConditionalsLattice(SimpleInferenceLattice.instance))
const IPOResultLattice = typeof(InterConditionalsLattice(SimpleInferenceLattice.instance))
"""
struct InferenceLattice{๐<:AbstractLattice} <: AbstractLattice
The full lattice used for abstract interpretation during inference.
Extends a base lattice `๐` and adjoins `LimitedAccuracy`.
"""
struct InferenceLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
widenlattice(๐::InferenceLattice) = ๐.parent
is_valid_lattice_norec(::InferenceLattice, @nospecialize(elem)) = isa(elem, LimitedAccuracy)
"""
tmeet(๐::AbstractLattice, a, b::Type)
Compute the lattice meet of lattice elements `a` and `b` over the lattice `๐`,
dropping any results that will not be inhabited at runtime.
If `๐` is `JLTypeLattice`, this is equivalent to type intersection plus the
elimination of results that have no concrete subtypes.
Note that currently `b` is restricted to being a type
(interpreted as a lattice element in the `JLTypeLattice` sub-lattice of `๐`).
"""
function tmeet end
function tmeet(::JLTypeLattice, @nospecialize(a::Type), @nospecialize(b::Type))
ti = typeintersect(a, b)
valid_as_lattice(ti, true) || return Bottom
return ti
end
"""
tmerge(๐::AbstractLattice, a, b)
Compute a lattice join of elements `a` and `b` over the lattice `๐`.
Note that the computed element need not be the least upper bound of `a` and
`b`, but rather, we impose additional limitations on the complexity of the
joined element, ideally without losing too much precision in common cases and
remaining mostly associative and commutative.
"""
function tmerge end
"""
tmerge_field(๐::AbstractLattice, a, b) -> nothing or lattice element
Compute a lattice join of elements `a` and `b` over the lattice `๐`,
where `a` and `b` are fields of `PartialStruct` or `Const`.
This is an opt-in interface to allow external lattice implementation to provide its own
field-merge strategy. If it returns `nothing`, `tmerge(::PartialsLattice, ...)`
will use the default aggressive type merge implementation that does not use `tmerge`
recursively to reach convergence.
"""
function tmerge_field end
function tmerge_field(๐::AbstractLattice, @nospecialize(a), @nospecialize(b))
return tmerge_field(widenlattice(๐), a, b)
end
tmerge_field(::JLTypeLattice, @nospecialize(a), @nospecialize(b)) = nothing
"""
โ(๐::AbstractLattice, a, b)
Compute the lattice ordering (i.e. less-than-or-equal) relationship between
lattice elements `a` and `b` over the lattice `๐`.
If `๐` is `JLTypeLattice`, this is equivalent to subtyping.
"""
function โ end
@nospecializeinfer โ(::JLTypeLattice, @nospecialize(a::Type), @nospecialize(b::Type)) = a <: b
"""
โ(๐::AbstractLattice, a, b) -> Bool
The strict partial order over the type inference lattice.
This is defined as the irreflexive kernel of `โ`.
"""
@nospecializeinfer โ(๐::AbstractLattice, @nospecialize(a), @nospecialize(b)) = โ(๐, a, b) && !โ(๐, b, a)
"""
โค(๐::AbstractLattice, a, b) -> Bool
This order could be used as a slightly more efficient version of the strict order `โ`,
where we can safely assume `a โ b` holds.
"""
@nospecializeinfer โค(๐::AbstractLattice, @nospecialize(a), @nospecialize(b)) = !โ(๐, b, a)
"""
is_lattice_equal(๐::AbstractLattice, a, b) -> Bool
Check if two lattice elements are partial order equivalent.
This is basically `a โ b && b โ a` in the lattice of `๐`
but (optionally) with extra performance optimizations.
"""
@nospecializeinfer function is_lattice_equal(๐::AbstractLattice, @nospecialize(a), @nospecialize(b))
a === b && return true
return โ(๐, a, b) && โ(๐, b, a)
end
"""
has_nontrivial_extended_info(๐::AbstractLattice, t) -> Bool
Determines whether the given lattice element `t` of `๐` has non-trivial extended lattice
information that would not be available from the type itself.
"""
@nospecializeinfer has_nontrivial_extended_info(๐::AbstractLattice, @nospecialize t) =
has_nontrivial_extended_info(widenlattice(๐), t)
@nospecializeinfer function has_nontrivial_extended_info(๐::PartialsLattice, @nospecialize t)
isa(t, PartialStruct) && return true
isa(t, PartialOpaque) && return true
return has_nontrivial_extended_info(widenlattice(๐), t)
end
@nospecializeinfer function has_nontrivial_extended_info(๐::ConstsLattice, @nospecialize t)
isa(t, PartialTypeVar) && return true
if isa(t, Const)
val = t.val
return !issingletontype(typeof(val)) && !(isa(val, Type) && hasuniquerep(val))
end
return has_nontrivial_extended_info(widenlattice(๐), t)
end
@nospecializeinfer has_nontrivial_extended_info(::JLTypeLattice, @nospecialize(t)) = false
"""
is_const_prop_profitable_arg(๐::AbstractLattice, t) -> Bool
Determines whether the given lattice element `t` of `๐` has new extended lattice information
that should be forwarded along with constant propagation.
"""
@nospecializeinfer is_const_prop_profitable_arg(๐::AbstractLattice, @nospecialize t) =
is_const_prop_profitable_arg(widenlattice(๐), t)
@nospecializeinfer function is_const_prop_profitable_arg(๐::PartialsLattice, @nospecialize t)
if isa(t, PartialStruct)
return true # might be a bit aggressive, may want to enable some check like follows:
# for i = 1:length(t.fields)
# fld = t.fields[i]
# isconstType(fld) && return true
# is_const_prop_profitable_arg(fld) && return true
# fld โ fieldtype(t.typ, i) && return true
# end
# return false
end
isa(t, PartialOpaque) && return true
return is_const_prop_profitable_arg(widenlattice(๐), t)
end
@nospecializeinfer function is_const_prop_profitable_arg(๐::ConstsLattice, @nospecialize t)
if isa(t, Const)
# don't consider mutable values useful constants
val = t.val
return isa(val, Symbol) || isa(val, Type) || !ismutable(val)
end
isa(t, PartialTypeVar) && return false # this isn't forwardable
return is_const_prop_profitable_arg(widenlattice(๐), t)
end
@nospecializeinfer is_const_prop_profitable_arg(::JLTypeLattice, @nospecialize t) = false
@nospecializeinfer is_forwardable_argtype(๐::AbstractLattice, @nospecialize(x)) =
is_forwardable_argtype(widenlattice(๐), x)
@nospecializeinfer function is_forwardable_argtype(๐::ConditionalsLattice, @nospecialize x)
isa(x, Conditional) && return true
return is_forwardable_argtype(widenlattice(๐), x)
end
@nospecializeinfer function is_forwardable_argtype(๐::PartialsLattice, @nospecialize x)
isa(x, PartialStruct) && return true
isa(x, PartialOpaque) && return true
return is_forwardable_argtype(widenlattice(๐), x)
end
@nospecializeinfer function is_forwardable_argtype(๐::ConstsLattice, @nospecialize x)
isa(x, Const) && return true
return is_forwardable_argtype(widenlattice(๐), x)
end
@nospecializeinfer is_forwardable_argtype(::JLTypeLattice, @nospecialize x) = false
"""
widenreturn(๐แตข::AbstractLattice, @nospecialize(rt), info::BestguessInfo) -> new_bestguess
widenreturn_noslotwrapper(๐แตข::AbstractLattice, @nospecialize(rt), info::BestguessInfo) -> new_bestguess
Appropriately converts inferred type of a return value `rt` to such a type
that we know we can store in the cache and is valid and good inter-procedurally,
E.g. if `rt isa Conditional` then `rt` should be converted to `InterConditional`
or the other cacheable lattice element.
External lattice `๐แตข::ExternalLattice` may overload:
- `widenreturn(๐แตข::ExternalLattice, @nospecialize(rt), info::BestguessInfo)`
- `widenreturn_noslotwrapper(๐แตข::ExternalLattice, @nospecialize(rt), info::BestguessInfo)`
"""
function widenreturn end, function widenreturn_noslotwrapper end
@nospecializeinfer is_valid_lattice(๐::AbstractLattice, @nospecialize(elem)) =
is_valid_lattice_norec(๐, elem) && is_valid_lattice(widenlattice(๐), elem)
@nospecializeinfer is_valid_lattice(๐::JLTypeLattice, @nospecialize(elem)) = is_valid_lattice_norec(๐, elem)
has_conditional(๐::AbstractLattice) = has_conditional(widenlattice(๐))
has_conditional(::AnyConditionalsLattice) = true
has_conditional(::JLTypeLattice) = false
has_mustalias(๐::AbstractLattice) = has_mustalias(widenlattice(๐))
has_mustalias(::AnyMustAliasesLattice) = true
has_mustalias(::JLTypeLattice) = false
has_extended_unionsplit(๐::AbstractLattice) = has_extended_unionsplit(widenlattice(๐))
has_extended_unionsplit(::AnyMustAliasesLattice) = true
has_extended_unionsplit(::JLTypeLattice) = false
# Curried versions
โ(๐::AbstractLattice) = (@nospecialize(a), @nospecialize(b)) -> โ(๐, a, b)
โ(๐::AbstractLattice) = (@nospecialize(a), @nospecialize(b)) -> โ(๐, a, b)
โค(๐::AbstractLattice) = (@nospecialize(a), @nospecialize(b)) -> โค(๐, a, b)
partialorder(๐::AbstractLattice) = โ(๐)
strictpartialorder(๐::AbstractLattice) = โ(๐)
strictneqpartialorder(๐::AbstractLattice) = โค(๐)
# Fallbacks for external packages using these methods
const fallback_lattice = InferenceLattice(BaseInferenceLattice.instance)
const fallback_ipo_lattice = InferenceLattice(IPOResultLattice.instance)
@nospecializeinfer @nospecialize(a) โ @nospecialize(b) = โ(fallback_lattice, a, b)
@nospecializeinfer @nospecialize(a) โ @nospecialize(b) = โ(fallback_lattice, a, b)
@nospecializeinfer @nospecialize(a) โค @nospecialize(b) = โค(fallback_lattice, a, b)
@nospecializeinfer tmeet(@nospecialize(a), @nospecialize(b)) = tmeet(fallback_lattice, a, b)
@nospecializeinfer tmerge(@nospecialize(a), @nospecialize(b)) = tmerge(fallback_lattice, a, b)
@nospecializeinfer is_lattice_equal(@nospecialize(a), @nospecialize(b)) = is_lattice_equal(fallback_lattice, a, b)
# Widenlattice with argument
widenlattice(::JLTypeLattice, @nospecialize(t)) = widenconst(t)
function widenlattice(๐::AbstractLattice, @nospecialize(t))
is_valid_lattice_norec(๐, t) && return t
widenlattice(widenlattice(๐), t)
end