Skip to content

Commit

Permalink
experiment @noinfer on Core.Compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Apr 12, 2023
1 parent e5e33d4 commit 6a84531
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
6 changes: 3 additions & 3 deletions base/compiler/abstractlattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,23 @@ If `𝕃` is `JLTypeLattice`, this is equivalent to subtyping.
"""
function end

(::JLTypeLattice, @nospecialize(a::Type), @nospecialize(b::Type)) = a <: b
@noinfer (::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)
@noinfer (𝕃::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)
@noinfer (𝕃::AbstractLattice, @nospecialize(a), @nospecialize(b)) = !(𝕃, b, a)

"""
is_lattice_equal(𝕃::AbstractLattice, a, b) -> Bool
Expand Down
12 changes: 6 additions & 6 deletions base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ ignorelimited(typ::LimitedAccuracy) = typ.typ
# lattice order
# =============

function (lattice::InferenceLattice, @nospecialize(a), @nospecialize(b))
@noinfer function (lattice::InferenceLattice, @nospecialize(a), @nospecialize(b))
r = (widenlattice(lattice), ignorelimited(a), ignorelimited(b))
r || return false
isa(b, LimitedAccuracy) || return true
Expand All @@ -420,7 +420,7 @@ function ⊑(lattice::InferenceLattice, @nospecialize(a), @nospecialize(b))
return b.causes a.causes
end

function (lattice::OptimizerLattice, @nospecialize(a), @nospecialize(b))
@noinfer function (lattice::OptimizerLattice, @nospecialize(a), @nospecialize(b))
if isa(a, MaybeUndef)
isa(b, MaybeUndef) || return false
a, b = a.typ, b.typ
Expand All @@ -430,7 +430,7 @@ function ⊑(lattice::OptimizerLattice, @nospecialize(a), @nospecialize(b))
return (widenlattice(lattice), a, b)
end

function (lattice::AnyConditionalsLattice, @nospecialize(a), @nospecialize(b))
@noinfer function (lattice::AnyConditionalsLattice, @nospecialize(a), @nospecialize(b))
# Fast paths for common cases
b === Any && return true
a === Any && return false
Expand All @@ -450,7 +450,7 @@ function ⊑(lattice::AnyConditionalsLattice, @nospecialize(a), @nospecialize(b)
return (widenlattice(lattice), a, b)
end

function (𝕃::AnyMustAliasesLattice, @nospecialize(a), @nospecialize(b))
@noinfer function (𝕃::AnyMustAliasesLattice, @nospecialize(a), @nospecialize(b))
MustAliasT = isa(𝕃, MustAliasesLattice) ? MustAlias : InterMustAlias
if isa(a, MustAliasT)
if isa(b, MustAliasT)
Expand All @@ -463,7 +463,7 @@ function ⊑(𝕃::AnyMustAliasesLattice, @nospecialize(a), @nospecialize(b))
return (widenlattice(𝕃), a, b)
end

function (lattice::PartialsLattice, @nospecialize(a), @nospecialize(b))
@noinfer function (lattice::PartialsLattice, @nospecialize(a), @nospecialize(b))
if isa(a, PartialStruct)
if isa(b, PartialStruct)
if !(length(a.fields) == length(b.fields) && a.typ <: b.typ)
Expand Down Expand Up @@ -526,7 +526,7 @@ function ⊑(lattice::PartialsLattice, @nospecialize(a), @nospecialize(b))
return (widenlattice(lattice), a, b)
end

function (lattice::ConstsLattice, @nospecialize(a), @nospecialize(b))
@noinfer function (lattice::ConstsLattice, @nospecialize(a), @nospecialize(b))
if isa(a, Const)
if isa(b, Const)
return a.val === b.val
Expand Down
23 changes: 11 additions & 12 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ end

# A simplified type_more_complex query over the extended lattice
# (assumes typeb ⊑ typea)
function issimplertype(𝕃::AbstractLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer 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
Expand Down Expand Up @@ -413,7 +413,7 @@ function merge_causes(causesa::IdSet{InferenceState}, causesb::IdSet{InferenceSt
end
end

@noinline function tmerge_limited(lattice::InferenceLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer @noinline function tmerge_limited(lattice::InferenceLattice, @nospecialize(typea), @nospecialize(typeb))
typea === Union{} && return typeb
typeb === Union{} && return typea

Expand Down Expand Up @@ -464,7 +464,7 @@ end
return LimitedAccuracy(tmerge(widenlattice(lattice), typea, typeb), causes)
end

function tmerge(lattice::InferenceLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge(lattice::InferenceLattice, @nospecialize(typea), @nospecialize(typeb))
if isa(typea, LimitedAccuracy) || isa(typeb, LimitedAccuracy)
return tmerge_limited(lattice, typea, typeb)
end
Expand All @@ -474,7 +474,7 @@ function tmerge(lattice::InferenceLattice, @nospecialize(typea), @nospecialize(t
return tmerge(widenlattice(lattice), typea, typeb)
end

function tmerge(lattice::ConditionalsLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge(lattice::ConditionalsLattice, @nospecialize(typea), @nospecialize(typeb))
# type-lattice for Conditional wrapper (NOTE never be merged with InterConditional)
if isa(typea, Conditional) && isa(typeb, Const)
if typeb.val === true
Expand Down Expand Up @@ -509,7 +509,7 @@ function tmerge(lattice::ConditionalsLattice, @nospecialize(typea), @nospecializ
return tmerge(widenlattice(lattice), typea, typeb)
end

function tmerge(lattice::InterConditionalsLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge(lattice::InterConditionalsLattice, @nospecialize(typea), @nospecialize(typeb))
# type-lattice for InterConditional wrapper (NOTE never be merged with Conditional)
if isa(typea, InterConditional) && isa(typeb, Const)
if typeb.val === true
Expand Down Expand Up @@ -544,15 +544,15 @@ function tmerge(lattice::InterConditionalsLattice, @nospecialize(typea), @nospec
return tmerge(widenlattice(lattice), typea, typeb)
end

function tmerge(𝕃::AnyMustAliasesLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge(𝕃::AnyMustAliasesLattice, @nospecialize(typea), @nospecialize(typeb))
typea = widenmustalias(typea)
typeb = widenmustalias(typeb)
return tmerge(widenlattice(𝕃), typea, typeb)
end

# N.B. This can also be called with both typea::Const and typeb::Const to
# to recover PartialStruct from `Const`s with overlapping fields.
function tmerge_partial_struct(lattice::PartialsLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge_partial_struct(lattice::PartialsLattice, @nospecialize(typea), @nospecialize(typeb))
aty = widenconst(typea)
bty = widenconst(typeb)
if aty === bty
Expand Down Expand Up @@ -610,7 +610,7 @@ function tmerge_partial_struct(lattice::PartialsLattice, @nospecialize(typea), @
return nothing
end

function tmerge(lattice::PartialsLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge(lattice::PartialsLattice, @nospecialize(typea), @nospecialize(typeb))
# type-lattice for Const and PartialStruct wrappers
aps = isa(typea, PartialStruct)
bps = isa(typeb, PartialStruct)
Expand Down Expand Up @@ -653,8 +653,7 @@ function tmerge(lattice::PartialsLattice, @nospecialize(typea), @nospecialize(ty
return tmerge(wl, typea, typeb)
end


function tmerge(lattice::ConstsLattice, @nospecialize(typea), @nospecialize(typeb))
@noinfer function tmerge(lattice::ConstsLattice, @nospecialize(typea), @nospecialize(typeb))
acp = isa(typea, Const) || isa(typea, PartialTypeVar)
bcp = isa(typeb, Const) || isa(typeb, PartialTypeVar)
if acp && bcp
Expand All @@ -666,7 +665,7 @@ function tmerge(lattice::ConstsLattice, @nospecialize(typea), @nospecialize(type
return tmerge(wl, typea, typeb)
end

function tmerge(::JLTypeLattice, @nospecialize(typea::Type), @nospecialize(typeb::Type))
@noinfer function tmerge(::JLTypeLattice, @nospecialize(typea::Type), @nospecialize(typeb::Type))
# it's always ok to form a Union of two concrete types
act = isconcretetype(typea)
bct = isconcretetype(typeb)
Expand All @@ -682,7 +681,7 @@ function tmerge(::JLTypeLattice, @nospecialize(typea::Type), @nospecialize(typeb
return tmerge_types_slow(typea, typeb)
end

@noinline function tmerge_types_slow(@nospecialize(typea::Type), @nospecialize(typeb::Type))
@noinfer @noinline function tmerge_types_slow(@nospecialize(typea::Type), @nospecialize(typeb::Type))
# collect the list of types from past tmerge calls returning Union
# and then reduce over that list
types = Any[]
Expand Down

0 comments on commit 6a84531

Please sign in to comment.