https://github.com/JuliaLang/julia
Tip revision: 40bef5436e18e0cf5d3a4ae88fa082a23d086c1f authored by Jeff Bezanson on 24 January 2023, 17:16:15 UTC
WIP: "silent" type parameters
WIP: "silent" type parameters
Tip revision: 40bef54
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)
"""
struct OptimizerLattice{๐<:AbstractLattice} <: AbstractLattice
The lattice used by the optimizer.
Extends a base lattice `๐` and adjoins `MaybeUndef`.
"""
struct OptimizerLattice{๐<:AbstractLattice} <: AbstractLattice
parent::๐
end
OptimizerLattice() = OptimizerLattice(SimpleInferenceLattice.instance)
widenlattice(๐::OptimizerLattice) = ๐.parent
is_valid_lattice_norec(::OptimizerLattice, @nospecialize(elem)) = isa(elem, MaybeUndef)
"""
tmeet(๐::AbstractLattice, a, b::Type)
Compute the lattice meet of lattice elements `a` and `b` over the lattice `๐`.
If `๐` is `JLTypeLattice`, this is equivalent to type intersection.
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) || 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
โ(::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 `โ`.
"""
โ(๐::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.
"""
โค(๐::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.
"""
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.
"""
has_nontrivial_extended_info(๐::AbstractLattice, @nospecialize t) =
has_nontrivial_extended_info(widenlattice(๐), t)
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
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
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.
"""
is_const_prop_profitable_arg(๐::AbstractLattice, @nospecialize t) =
is_const_prop_profitable_arg(widenlattice(๐), t)
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
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
is_const_prop_profitable_arg(::JLTypeLattice, @nospecialize t) = false
is_forwardable_argtype(๐::AbstractLattice, @nospecialize(x)) =
is_forwardable_argtype(widenlattice(๐), x)
function is_forwardable_argtype(๐::ConditionalsLattice, @nospecialize x)
isa(x, Conditional) && return true
return is_forwardable_argtype(widenlattice(๐), x)
end
function is_forwardable_argtype(๐::PartialsLattice, @nospecialize x)
isa(x, PartialStruct) && return true
isa(x, PartialOpaque) && return true
return is_forwardable_argtype(widenlattice(๐), x)
end
function is_forwardable_argtype(๐::ConstsLattice, @nospecialize x)
isa(x, Const) && return true
return is_forwardable_argtype(widenlattice(๐), x)
end
function is_forwardable_argtype(::JLTypeLattice, @nospecialize x)
return false
end
"""
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 cachable 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
is_valid_lattice(๐::AbstractLattice, @nospecialize(elem)) =
is_valid_lattice_norec(๐, elem) && is_valid_lattice(widenlattice(๐), elem)
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
# Curried versions
โ(lattice::AbstractLattice) = (@nospecialize(a), @nospecialize(b)) -> โ(lattice, a, b)
โ(lattice::AbstractLattice) = (@nospecialize(a), @nospecialize(b)) -> โ(lattice, a, b)
โค(lattice::AbstractLattice) = (@nospecialize(a), @nospecialize(b)) -> โค(lattice, a, b)
# Fallbacks for external packages using these methods
const fallback_lattice = InferenceLattice(BaseInferenceLattice.instance)
const fallback_ipo_lattice = InferenceLattice(IPOResultLattice.instance)
โ(@nospecialize(a), @nospecialize(b)) = โ(fallback_lattice, a, b)
tmeet(@nospecialize(a), @nospecialize(b)) = tmeet(fallback_lattice, a, b)
tmerge(@nospecialize(a), @nospecialize(b)) = tmerge(fallback_lattice, a, b)
โ(@nospecialize(a), @nospecialize(b)) = โ(fallback_lattice, a, b)
โค(@nospecialize(a), @nospecialize(b)) = โค(fallback_lattice, a, b)
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