Skip to content

Commit 125bac4

Browse files
authored
inference: cleans up abstract interpretation code (#55308)
- propagate the lattice that was not propagated - remove unused `condargs` allocation
1 parent 6bc2c55 commit 125bac4

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

base/compiler/abstractinterpretation.jl

+21-24
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
1717
arginfo::ArgInfo, si::StmtInfo, @nospecialize(atype),
1818
sv::AbsIntState, max_methods::Int)
1919
𝕃ₚ, 𝕃ᵢ = ipo_lattice(interp), typeinf_lattice(interp)
20-
= (𝕃ₚ)
20+
, ₚ, = partialorder(𝕃ₚ), join(𝕃ₚ), join(𝕃ᵢ)
2121
if !should_infer_this_call(interp, sv)
2222
add_remark!(interp, sv, "Skipped call in throw block")
2323
# At this point we are guaranteed to end up throwing on this path,
@@ -37,7 +37,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
3737
(; valid_worlds, applicable, info) = matches
3838
update_valid_age!(sv, valid_worlds)
3939
napplicable = length(applicable)
40-
rettype = excttype = Bottom
40+
rettype = exctype = Bottom
4141
edges = MethodInstance[]
4242
conditionals = nothing # keeps refinement information of call argument types when the return type is boolean
4343
seen = 0 # number of signatures actually inferred
@@ -96,8 +96,8 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
9696
const_results[i] = const_result
9797
end
9898
edge === nothing || push!(edges, edge)
99-
this_rt = tmerge(this_rt, rt)
100-
this_exct = tmerge(this_exct, exct)
99+
this_rt = this_rt ₚ rt
100+
this_exct = this_exct exct
101101
if bail_out_call(interp, this_rt, sv)
102102
break
103103
end
@@ -156,17 +156,17 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
156156
end
157157
@assert !(this_conditional isa Conditional || this_rt isa MustAlias) "invalid lattice element returned from inter-procedural context"
158158
seen += 1
159-
rettype = tmerge(𝕃ₚ, rettype, this_rt)
160-
excttype = tmerge(𝕃ₚ, excttype, this_exct)
159+
rettype = rettype this_rt
160+
exctype = exctype this_exct
161161
if has_conditional(𝕃ₚ, sv) && this_conditional !== Bottom && is_lattice_bool(𝕃ₚ, rettype) && fargs !== nothing
162162
if conditionals === nothing
163163
conditionals = Any[Bottom for _ in 1:length(argtypes)],
164164
Any[Bottom for _ in 1:length(argtypes)]
165165
end
166166
for i = 1:length(argtypes)
167167
cnd = conditional_argtype(𝕃ᵢ, this_conditional, sig, argtypes, i)
168-
conditionals[1][i] = tmerge(𝕃ᵢ, conditionals[1][i], cnd.thentype)
169-
conditionals[2][i] = tmerge(𝕃ᵢ, conditionals[2][i], cnd.elsetype)
168+
conditionals[1][i] = conditionals[1][i] cnd.thentype
169+
conditionals[2][i] = conditionals[2][i] cnd.elsetype
170170
end
171171
end
172172
if bail_out_call(interp, InferenceLoopState(sig, rettype, all_effects), sv)
@@ -182,14 +182,14 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
182182

183183
if seen napplicable
184184
# there is unanalyzed candidate, widen type and effects to the top
185-
rettype = excttype = Any
185+
rettype = exctype = Any
186186
all_effects = Effects()
187187
else
188188
if (matches isa MethodMatches ? (!matches.fullmatch || any_ambig(matches)) :
189189
(!all(matches.fullmatches) || any_ambig(matches)))
190190
# Account for the fact that we may encounter a MethodError with a non-covered or ambiguous signature.
191191
all_effects = Effects(all_effects; nothrow=false)
192-
excttype = tmerge(𝕃ₚ, excttype, MethodError)
192+
exctype = exctype MethodError
193193
end
194194
if sv isa InferenceState && fargs !== nothing
195195
slotrefinements = collect_slot_refinements(𝕃ᵢ, applicable, argtypes, fargs, sv)
@@ -240,7 +240,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
240240
end
241241
end
242242

243-
return CallMeta(rettype, excttype, all_effects, info, slotrefinements)
243+
return CallMeta(rettype, exctype, all_effects, info, slotrefinements)
244244
end
245245

246246
struct FailedMethodMatch
@@ -367,7 +367,7 @@ function from_interprocedural!(interp::AbstractInterpreter, @nospecialize(rt), s
367367
arginfo::ArgInfo, @nospecialize(maybecondinfo))
368368
rt = collect_limitations!(rt, sv)
369369
if isa(rt, InterMustAlias)
370-
rt = from_intermustalias(rt, arginfo, sv)
370+
rt = from_intermustalias(typeinf_lattice(interp), rt, arginfo, sv)
371371
elseif is_lattice_bool(ipo_lattice(interp), rt)
372372
if maybecondinfo === nothing
373373
rt = widenconditional(rt)
@@ -387,12 +387,13 @@ function collect_limitations!(@nospecialize(typ), sv::InferenceState)
387387
return typ
388388
end
389389

390-
function from_intermustalias(rt::InterMustAlias, arginfo::ArgInfo, sv::AbsIntState)
390+
function from_intermustalias(𝕃ᵢ::AbstractLattice, rt::InterMustAlias, arginfo::ArgInfo, sv::AbsIntState)
391391
fargs = arginfo.fargs
392392
if fargs !== nothing && 1 rt.slot length(fargs)
393393
arg = ssa_def_slot(fargs[rt.slot], sv)
394394
if isa(arg, SlotNumber)
395395
argtyp = widenslotwrapper(arginfo.argtypes[rt.slot])
396+
= partialorder(𝕃ᵢ)
396397
if rt.vartyp argtyp
397398
return MustAlias(arg, rt.vartyp, rt.fldidx, rt.fldtyp)
398399
else
@@ -412,6 +413,7 @@ function from_interconditional(𝕃ᵢ::AbstractLattice, @nospecialize(rt), sv::
412413
alias = nothing
413414
thentype = elsetype = Any
414415
condval = maybe_extract_const_bool(rt)
416+
, , = partialorder(𝕃ᵢ), strictneqpartialorder(𝕃ᵢ), meet(𝕃ᵢ)
415417
for i in 1:length(fargs)
416418
# find the first argument which supports refinement,
417419
# and intersect all equivalent arguments with it
@@ -447,24 +449,24 @@ function from_interconditional(𝕃ᵢ::AbstractLattice, @nospecialize(rt), sv::
447449
end
448450
if condval === false
449451
thentype = Bottom
450-
elseif (𝕃ᵢ, new_thentype, thentype)
452+
elseif new_thentype thentype
451453
thentype = new_thentype
452454
else
453-
thentype = tmeet(𝕃ᵢ, thentype, widenconst(new_thentype))
455+
thentype = thentype widenconst(new_thentype)
454456
end
455457
if condval === true
456458
elsetype = Bottom
457-
elseif (𝕃ᵢ, new_elsetype, elsetype)
459+
elseif new_elsetype elsetype
458460
elsetype = new_elsetype
459461
else
460-
elsetype = tmeet(𝕃ᵢ, elsetype, widenconst(new_elsetype))
462+
elsetype = elsetype widenconst(new_elsetype)
461463
end
462-
if (slot > 0 || condval !== false) && (𝕃ᵢ, thentype, old)
464+
if (slot > 0 || condval !== false) && thentype old
463465
slot = id
464466
if !(arg isa SlotNumber) && argtyp isa MustAlias
465467
alias = argtyp
466468
end
467-
elseif (slot > 0 || condval !== true) && (𝕃ᵢ, elsetype, old)
469+
elseif (slot > 0 || condval !== true) && elsetype old
468470
slot = id
469471
if !(arg isa SlotNumber) && argtyp isa MustAlias
470472
alias = argtyp
@@ -1368,7 +1370,6 @@ function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance,
13681370
given_argtypes = Vector{Any}(undef, length(argtypes))
13691371
def = mi.def::Method
13701372
nargs = Int(def.nargs)
1371-
local condargs = nothing
13721373
for i in 1:length(argtypes)
13731374
argtype = argtypes[i]
13741375
# forward `Conditional` if it conveys a constraint on any other argument
@@ -1385,10 +1386,6 @@ function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance,
13851386
# TODO bail out here immediately rather than just propagating Bottom ?
13861387
given_argtypes[i] = Bottom
13871388
else
1388-
if condargs === nothing
1389-
condargs = Tuple{Int,Int}[]
1390-
end
1391-
push!(condargs, (slotid, i))
13921389
given_argtypes[i] = Conditional(slotid, thentype, elsetype)
13931390
end
13941391
continue

0 commit comments

Comments
 (0)