Skip to content

Commit 4829608

Browse files
committed
Make MustAlias propagate ssadef to Conditional
Another change will probably be needed to make sure that `MustAlias` itself is invalidated by `.defssa`, but this is enough to make sure that any Conditional derived from MustAlias works correctly.
1 parent abf839b commit 4829608

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ function from_interprocedural!(interp::AbstractInterpreter, @nospecialize(rt), s
392392
arginfo::ArgInfo, @nospecialize(maybecondinfo), vtypes::Union{VarTable,Nothing})
393393
rt = collect_limitations!(rt, sv)
394394
if isa(rt, InterMustAlias)
395-
rt = from_intermustalias(typeinf_lattice(interp), rt, arginfo, sv)
395+
rt = from_intermustalias(typeinf_lattice(interp), rt, arginfo, vtypes, sv)
396396
elseif is_lattice_bool(ipo_lattice(interp), rt)
397397
if maybecondinfo === nothing
398398
rt = widenconditional(rt)
@@ -412,15 +412,17 @@ function collect_limitations!(@nospecialize(typ), sv::InferenceState)
412412
return typ
413413
end
414414

415-
function from_intermustalias(𝕃ᵢ::AbstractLattice, rt::InterMustAlias, arginfo::ArgInfo, sv::AbsIntState)
415+
function from_intermustalias(𝕃ᵢ::AbstractLattice, rt::InterMustAlias, arginfo::ArgInfo, vtypes::Union{VarTable,Nothing}, sv::AbsIntState)
416416
fargs = arginfo.fargs
417417
if fargs !== nothing && 1 rt.slot length(fargs)
418418
arg = ssa_def_slot(fargs[rt.slot], sv)
419419
if isa(arg, SlotNumber)
420420
argtyp = widenslotwrapper(arginfo.argtypes[rt.slot])
421421
= partialorder(𝕃ᵢ)
422422
if rt.vartyp argtyp
423-
return MustAlias(arg, rt.vartyp, rt.fldidx, rt.fldtyp)
423+
@assert vtypes !== nothing
424+
vtyp = vtypes[slot_id(arg)]
425+
return MustAlias(arg, vtyp.ssadef, rt.vartyp, rt.fldidx, rt.fldtyp)
424426
else
425427
# TODO optimize this case?
426428
end
@@ -1916,7 +1918,9 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, (; fargs
19161918
fldidx = maybe_const_fldidx(vartyp, a3.val)
19171919
if fldidx !== nothing
19181920
# wrap this aliasable field into `MustAlias` for possible constraint propagations
1919-
return MustAlias(var, vartyp, fldidx, rt)
1921+
@assert vtypes !== nothing
1922+
vtyp = vtypes[slot_id(var)]
1923+
return MustAlias(var, vtyp.ssadef, vartyp, fldidx, rt)
19201924
end
19211925
end
19221926
end
@@ -3068,7 +3072,7 @@ end
30683072

30693073
@nospecializeinfer function widenreturn(𝕃ᵢ::MustAliasesLattice, @nospecialize(rt), info::BestguessInfo)
30703074
if isa(rt, MustAlias)
3071-
if 1 rt.slot info.nargs
3075+
if 1 rt.slot info.nargs && rt.ssadef == 0
30723076
rt = InterMustAlias(rt)
30733077
else
30743078
rt = widenmustalias(rt)
@@ -3286,7 +3290,6 @@ end
32863290
function init_vartable!(vartable::VarTable, frame::InferenceState)
32873291
nargtypes = length(frame.result.argtypes)
32883292
for i = 1:length(vartable)
3289-
# TODO: This should probably be `0`, not typemin(Int)
32903293
vartable[i] = VarState(Bottom, #= ssadef =# typemin(Int), i > nargtypes)
32913294
end
32923295
return vartable
@@ -3612,8 +3615,7 @@ function apply_refinement!(𝕃ᵢ::AbstractLattice, slot::SlotNumber, @nospecia
36123615
end
36133616

36143617
function conditional_valid(condt::Conditional, currstate::VarTable)
3615-
# TODO: Remove typemin condition...
3616-
# @assert condt.ssadef != typemin(Int)
3618+
@assert condt.ssadef != typemin(Int)
36173619
return currstate[condt.slot].ssadef == condt.ssadef
36183620
end
36193621

base/compiler/inferencestate.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,10 @@ mutable struct InferenceState
315315
nargtypes = length(argtypes)
316316
for i = 1:nslots
317317
argtyp = (i > nargtypes) ? Bottom : argtypes[i]
318-
# 0 = function entry (think carefully)
319318
if argtyp === Bool && has_conditional(typeinf_lattice(interp))
320319
argtyp = Conditional(i, #= ssadef =# 0, Const(true), Const(false))
321320
end
322321
slottypes[i] = argtyp
323-
# 0 = function entry (think carefully)
324322
bb_vartable1[i] = VarState(argtyp, #= ssadef =# 0, i > nargtypes)
325323
end
326324
src.ssavaluetypes = ssavaluetypes = Any[ NOT_FOUND for i = 1:nssavalues ]

base/compiler/typelattice.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,20 @@ N.B. currently this lattice element is only used in abstractinterpret, not in op
136136
"""
137137
struct MustAlias
138138
slot::Int
139+
ssadef::Int
139140
vartyp::Any
140141
fldidx::Int
141142
fldtyp::Any
142-
function MustAlias(slot::Int, @nospecialize(vartyp), fldidx::Int, @nospecialize(fldtyp))
143+
function MustAlias(slot::Int, ssadef::Int, @nospecialize(vartyp), fldidx::Int, @nospecialize(fldtyp))
143144
assert_nested_slotwrapper(vartyp)
144145
assert_nested_slotwrapper(fldtyp)
145146
# @assert !isalreadyconst(vartyp) "vartyp is already const"
146147
# @assert !isalreadyconst(fldtyp) "fldtyp is already const"
147-
return new(slot, vartyp, fldidx, fldtyp)
148+
return new(slot, ssadef, vartyp, fldidx, fldtyp)
148149
end
149150
end
150-
MustAlias(var::SlotNumber, @nospecialize(vartyp), fldidx::Int, @nospecialize(fldtyp)) =
151-
MustAlias(slot_id(var), vartyp, fldidx, fldtyp)
151+
MustAlias(var::SlotNumber, ssadef::Int, @nospecialize(vartyp), fldidx::Int, @nospecialize(fldtyp)) =
152+
MustAlias(slot_id(var), ssadef, vartyp, fldidx, fldtyp)
152153

153154
"""
154155
alias::InterMustAlias
@@ -172,8 +173,10 @@ InterMustAlias(var::SlotNumber, @nospecialize(vartyp), fldidx::Int, @nospecializ
172173
InterMustAlias(slot_id(var), vartyp, fldidx, fldtyp)
173174

174175
const AnyMustAlias = Union{MustAlias,InterMustAlias}
175-
MustAlias(alias::InterMustAlias) = MustAlias(alias.slot, alias.vartyp, alias.fldidx, alias.fldtyp)
176-
InterMustAlias(alias::MustAlias) = InterMustAlias(alias.slot, alias.vartyp, alias.fldidx, alias.fldtyp)
176+
function InterMustAlias(alias::MustAlias)
177+
@assert alias.ssadef == 0
178+
InterMustAlias(alias.slot, alias.vartyp, alias.fldidx, alias.fldtyp)
179+
end
177180

178181
struct PartialTypeVar
179182
tv::TypeVar
@@ -392,7 +395,7 @@ end
392395
end
393396

394397
@nospecializeinfer function form_mustalias_conditional(alias::MustAlias, @nospecialize(thentype), @nospecialize(elsetype))
395-
(; slot, vartyp, fldidx) = alias
398+
(; slot, ssadef, vartyp, fldidx) = alias
396399
if isa(vartyp, PartialStruct)
397400
fields = vartyp.fields
398401
thenfields = thentype === Bottom ? nothing : copy(fields)
@@ -403,7 +406,7 @@ end
403406
elsefields === nothing || (elsefields[i] = elsetype)
404407
end
405408
end
406-
return Conditional(slot, typemin(Int), # TODO
409+
return Conditional(slot, ssadef,
407410
thenfields === nothing ? Bottom : PartialStruct(vartyp.typ, thenfields),
408411
elsefields === nothing ? Bottom : PartialStruct(vartyp.typ, elsefields))
409412
else
@@ -420,7 +423,7 @@ end
420423
elsefields === nothing || push!(elsefields, t)
421424
end
422425
end
423-
return Conditional(slot, typemin(Int),
426+
return Conditional(slot, ssadef,
424427
thenfields === nothing ? Bottom : PartialStruct(vartyp_widened, thenfields),
425428
elsefields === nothing ? Bottom : PartialStruct(vartyp_widened, elsefields))
426429
end

test/compiler/inference.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,20 +2289,20 @@ let 𝕃ᵢ = InferenceLattice(MustAliasesLattice(BaseInferenceLattice.instance)
22892289
isa_tfunc(@nospecialize xs...) = Core.Compiler.isa_tfunc(𝕃ᵢ, xs...)
22902290
ifelse_tfunc(@nospecialize xs...) = Core.Compiler.ifelse_tfunc(𝕃ᵢ, xs...)
22912291

2292-
@test (MustAlias(2, AliasableField{Any}, 1, Int) Int)
2293-
@test !(Int MustAlias(2, AliasableField{Any}, 1, Int))
2294-
@test (Int MustAlias(2, AliasableField{Any}, 1, Any))
2295-
@test (Const(42) MustAlias(2, AliasableField{Any}, 1, Int))
2296-
@test !(MustAlias(2, AliasableField{Any}, 1, Any) Int)
2297-
@test tmerge(MustAlias(2, AliasableField{Any}, 1, Any), Const(nothing)) === Any
2298-
@test tmerge(MustAlias(2, AliasableField{Any}, 1, Int), Const(nothing)) === Union{Int,Nothing}
2299-
@test tmerge(Const(nothing), MustAlias(2, AliasableField{Any}, 1, Any)) === Any
2300-
@test tmerge(Const(nothing), MustAlias(2, AliasableField{Any}, 1, Int)) === Union{Int,Nothing}
2301-
@test isa_tfunc(MustAlias(2, AliasableField{Any}, 1, Bool), Const(Bool)) === Const(true)
2302-
@test isa_tfunc(MustAlias(2, AliasableField{Any}, 1, Bool), Type{Bool}) === Const(true)
2303-
@test isa_tfunc(MustAlias(2, AliasableField{Any}, 1, Int), Type{Bool}) === Const(false)
2304-
@test ifelse_tfunc(MustAlias(2, AliasableField{Any}, 1, Bool), Int, Int) === Int
2305-
@test ifelse_tfunc(MustAlias(2, AliasableField{Any}, 1, Int), Int, Int) === Union{}
2292+
@test (MustAlias(2, 0, AliasableField{Any}, 1, Int) Int)
2293+
@test !(Int MustAlias(2, 0, AliasableField{Any}, 1, Int))
2294+
@test (Int MustAlias(2, 0, AliasableField{Any}, 1, Any))
2295+
@test (Const(42) MustAlias(2, 0, AliasableField{Any}, 1, Int))
2296+
@test !(MustAlias(2, 0, AliasableField{Any}, 1, Any) Int)
2297+
@test tmerge(MustAlias(2, 0, AliasableField{Any}, 1, Any), Const(nothing)) === Any
2298+
@test tmerge(MustAlias(2, 0, AliasableField{Any}, 1, Int), Const(nothing)) === Union{Int,Nothing}
2299+
@test tmerge(Const(nothing), MustAlias(2, 0, AliasableField{Any}, 1, Any)) === Any
2300+
@test tmerge(Const(nothing), MustAlias(2, 0, AliasableField{Any}, 1, Int)) === Union{Int,Nothing}
2301+
@test isa_tfunc(MustAlias(2, 0, AliasableField{Any}, 1, Bool), Const(Bool)) === Const(true)
2302+
@test isa_tfunc(MustAlias(2, 0, AliasableField{Any}, 1, Bool), Type{Bool}) === Const(true)
2303+
@test isa_tfunc(MustAlias(2, 0, AliasableField{Any}, 1, Int), Type{Bool}) === Const(false)
2304+
@test ifelse_tfunc(MustAlias(2, 0, AliasableField{Any}, 1, Bool), Int, Int) === Int
2305+
@test ifelse_tfunc(MustAlias(2, 0, AliasableField{Any}, 1, Int), Int, Int) === Union{}
23062306
end
23072307

23082308
maybeget_mustalias_tmerge(x::AliasableField) = x.f

0 commit comments

Comments
 (0)