Skip to content

Commit 56b1714

Browse files
committed
lattice overhaul step 3: clean up implementations
1 parent 2b24dd0 commit 56b1714

File tree

12 files changed

+107
-97
lines changed

12 files changed

+107
-97
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# constants #
55
#############
66

7-
const ⊤, ⊥ = LatticeElement(Any), LatticeElement(Bottom)
87
const _REF_NAME = Ref.body.name
98

109
#########
@@ -321,7 +320,7 @@ end
321320
function from_interconditional(typ::LatticeElement, sv::InferenceState, (; fargs, argtypes)::ArgInfo, @nospecialize(maybecondinfo))
322321
fargs === nothing && return widenconditional(typ)
323322
slot = 0
324-
vtype = elsetype = Any
323+
vtype = elsetype =
325324
condval = maybe_extract_const_bool(typ)
326325
for i in 1:length(fargs)
327326
# find the first argument which supports refinement,
@@ -339,36 +338,36 @@ function from_interconditional(typ::LatticeElement, sv::InferenceState, (; fargs
339338
else
340339
# otherwise compute it on the fly
341340
cnd = conditional_argtype(typ, maybecondinfo, argtypes, i)
342-
new_vtype = cnd.vtype
343-
new_elsetype = cnd.elsetype
341+
new_vtype = LatticeElement(cnd.vtype)
342+
new_elsetype = LatticeElement(cnd.elsetype)
344343
end
345344
if condval === false
346-
vtype = Bottom
345+
vtype =
347346
elseif new_vtype vtype
348347
vtype = new_vtype
349348
else
350-
vtype = tmeet(vtype, widenconst(new_vtype))
349+
vtype = vtype widenconst(new_vtype)
351350
end
352351
if condval === true
353352
elsetype = Bottom
354353
elseif new_elsetype elsetype
355354
elsetype = new_elsetype
356355
else
357-
elsetype = tmeet(elsetype, widenconst(new_elsetype))
356+
elsetype = elsetype widenconst(new_elsetype)
358357
end
359358
if (slot > 0 || condval !== false) && vtype old
360359
slot = id
361360
elseif (slot > 0 || condval !== true) && elsetype old
362361
slot = id
363362
else # reset: no new useful information for this slot
364-
vtype = elsetype = Any
363+
vtype = elsetype =
365364
if slot > 0
366365
slot = 0
367366
end
368367
end
369368
end
370369
end
371-
if vtype === Bottom && elsetype === Bottom
370+
if vtype === && elsetype ===
372371
return# accidentally proved this call to be dead / throw !
373372
elseif slot > 0
374373
return Conditional(slot, vtype, elsetype) # record a Conditional improvement to this slot
@@ -383,7 +382,7 @@ function conditional_argtype(rt::LatticeElement, @nospecialize(sig), argtypes::V
383382
return cnd
384383
end
385384
end
386-
vtype = elsetype = tmeet(argtypes[i], fieldtype(sig, i))
385+
vtype = elsetype = argtypes[i] fieldtype(sig, i)
387386
condval = maybe_extract_const_bool(rt)
388387
condval === true && (elsetype = Bottom)
389388
condval === false && (vtype = Bottom)
@@ -1201,10 +1200,10 @@ function abstract_call_builtin(
12011200
a = ssa_def_slot(fargs[3], sv)
12021201
b = ssa_def_slot(fargs[4], sv)
12031202
if isa(a, SlotNumber) && cnd.slot_id == slot_id(a)
1204-
tx = (cnd.vtype tx ? cnd.vtype : tmeet(tx, widenconst(cnd.vtype)))
1203+
tx = (cnd.vtype tx ? cnd.vtype : (tx widenconst(cnd.vtype)))
12051204
end
12061205
if isa(b, SlotNumber) && cnd.slot_id == slot_id(b)
1207-
ty = (cnd.elsetype ty ? cnd.elsetype : tmeet(ty, widenconst(cnd.elsetype)))
1206+
ty = (cnd.elsetype ty ? cnd.elsetype : (ty widenconst(cnd.elsetype)))
12081207
end
12091208
return tmerge(tx, ty)
12101209
end
@@ -1452,7 +1451,7 @@ function abstract_call_known(
14521451
aty = argtypes[2]
14531452
if isConditional(aty)
14541453
cnd = conditional(aty)
1455-
call = abstract_call_gf_by_type(interp, f, ArgInfo(fargs, LatticeElement[Const(f), NativeType(Bool)]), Tuple{typeof(f), Bool}, sv, max_methods) # make sure we've inferred `!(::Bool)`
1454+
call = abstract_call_gf_by_type(interp, f, ArgInfo(fargs, LatticeElement[Const(f), LBool]), Tuple{typeof(f), Bool}, sv, max_methods) # make sure we've inferred `!(::Bool)`
14561455
return CallMeta(Conditional(cnd.slot_id, cnd.elsetype, cnd.vtype), call.info)
14571456
end
14581457
elseif la == 3 && istopfunction(f, :!==)
@@ -1610,7 +1609,7 @@ function abstract_eval_value_expr(interp::AbstractInterpreter, e::Expr, vtypes::
16101609
n = e.args[1]::Int
16111610
return 1 <= n <= length(sv.sptypes) ? sv.sptypes[n] :
16121611
elseif e.head === :boundscheck
1613-
return NativeType(Bool)
1612+
return LBool
16141613
else
16151614
return
16161615
end
@@ -1686,7 +1685,7 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
16861685
for i = 2:length(e.args)
16871686
at = widenconditional(abstract_eval_value(interp, e.args[i], vtypes, sv))
16881687
ft = fieldtype(ty, i-1)
1689-
at = tmeet(at, ft)
1688+
at = at ft
16901689
if at ===
16911690
t =
16921691
@goto t_computed
@@ -1733,7 +1732,7 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
17331732
argtypes = collect_argtypes(interp, ea, vtypes, sv)
17341733
if argtypes !== nothing
17351734
t = _opaque_closure_tfunc(argtypes[1], argtypes[2], argtypes[3],
1736-
argtypes[4], argtypes[5], anymap(unwraptype, argtypes[6:end]), sv.linfo)
1735+
argtypes[4], argtypes[5], argtypes[6:end], sv.linfo)
17371736
if isPartialOpaque(t)
17381737
# Infer this now so that the specialization is available to
17391738
# optimization.
@@ -1770,7 +1769,7 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
17701769
error("type inference data-flow error: tried to double infer a function")
17711770
elseif ehead === :isdefined
17721771
sym = e.args[1]
1773-
t = NativeType(Bool)
1772+
t = LBool
17741773
if isa(sym, SlotNumber)
17751774
vtyp = vtypes[slot_id(sym)]
17761775
if vtyp.typ ===
@@ -2103,7 +2102,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
21032102

21042103
if t === nothing
21052104
# mark other reached expressions as `Any` to indicate they don't throw
2106-
ssavaluetypes[pc] = NativeType(Any)
2105+
ssavaluetypes[pc] =
21072106
end
21082107

21092108
pc´ > n && break # can't proceed with the fast-path fall-through
@@ -2139,7 +2138,7 @@ function conditional_changes(changes::VarTable, typ::LatticeElement, slot_id::In
21392138
# since we probably formed these types with `typesubstract`, the comparison is likely simple
21402139
typ′, oldtyp′ = ignorelimited(typ), ignorelimited(oldtyp)
21412140
if typ′ oldtyp′
2142-
# typ is better unlimited, but we may still need to compute the tmeet with the limit "causes" since we ignored those in the comparison
2141+
# typ is better unlimited, but we may still need to compute the meet with the limit "causes" since we ignored those in the comparison
21432142
if isLimitedAccuracy(oldtyp)
21442143
typ = LimitedAccuracy(typ′, causes(oldtyp))
21452144
end

base/compiler/inferenceresult.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ function matching_cache_argtypes(
3737
if slotid !== nothing
3838
# using union-split signature, we may be able to narrow down `Conditional`
3939
sigt = widenconst(slotid > nargs ? argtypes[slotid] : cache_argtypes[slotid])
40-
vtype = tmeet(cnd.vtype, sigt)
41-
elsetype = tmeet(cnd.elsetype, sigt)
40+
vtype = LatticeElement(cnd.vtype) sigt
41+
elsetype = LatticeElement(cnd.elsetype) sigt
4242
if vtype === Bottom && elsetype === Bottom
4343
# we accidentally proved this method match is impossible
4444
# TODO bail out here immediately rather than just propagating Bottom ?
@@ -67,7 +67,7 @@ function matching_cache_argtypes(
6767
else
6868
last = nargs
6969
end
70-
isva_given_argtypes[nargs] = LatticeElement(tuple_tfunc(given_argtypes[last:end]))
70+
isva_given_argtypes[nargs] = tuple_tfunc(given_argtypes[last:end])
7171
# invalidate `Conditional` imposed on varargs
7272
if condargs !== nothing
7373
for (slotid, i) in condargs
@@ -219,7 +219,7 @@ function cache_lookup(linfo::MethodInstance, given_argtypes::Argtypes, cache::Ve
219219
end
220220
end
221221
if method.isva && cache_match
222-
cache_match = is_argtype_match(LatticeElement(tuple_tfunc(anymap(unwraptype, given_argtypes[(nargs + 1):end]))),
222+
cache_match = is_argtype_match(tuple_tfunc(given_argtypes[(nargs + 1):end]),
223223
cache_argtypes[end],
224224
cache_overridden_by_const[end])
225225
end

base/compiler/optimize.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function argextype(
354354
if x.head === :static_parameter
355355
return sptypes[x.args[1]::Int]
356356
elseif x.head === :boundscheck
357-
return NativeType(Bool)
357+
return LBool
358358
elseif x.head === :copyast
359359
return argextype(x.args[1], src, sptypes, slottypes)
360360
end
@@ -547,7 +547,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
547547
# insert a side-effect instruction before the current instruction in the same basic block
548548
insert!(code, idx, Expr(:code_coverage_effect))
549549
insert!(codelocs, idx, codeloc)
550-
insert!(ssavaluetypes, idx, NativeType(Nothing))
550+
insert!(ssavaluetypes, idx, LNothing)
551551
insert!(stmtinfo, idx, nothing)
552552
insert!(ssaflags, idx, IR_FLAG_NULL)
553553
changemap[oldidx] += 1

base/compiler/ssair/inlining.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
330330
push!(linetable, newentry)
331331
end
332332
if coverage && spec.ir.stmts[1][:line] + linetable_offset != topline
333-
insert_node_here!(compact, NewInstruction(Expr(:code_coverage_effect), Nothing, topline))
333+
insert_node_here!(compact, NewInstruction(Expr(:code_coverage_effect), LNothing, topline))
334334
end
335335
if def.isva
336336
nargs_def = Int(def.nargs::Int32)
@@ -400,7 +400,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
400400
inline_compact.result[idx′][:type] =
401401
argextype(val, isa(val, Expr) ? compact : inline_compact)
402402
insert_node_here!(inline_compact, NewInstruction(GotoNode(post_bb_id),
403-
Any, compact.result[idx′][:line]),
403+
, compact.result[idx′][:line]),
404404
true)
405405
push!(pn.values, SSAValue(idx′))
406406
else
@@ -446,11 +446,11 @@ function fix_va_argexprs!(compact::IncrementalCompact,
446446
push!(newargexprs, argexprs[i])
447447
end
448448
tuple_call = Expr(:call, TOP_TUPLE)
449-
tuple_typs = Any[]
449+
tuple_typs = LatticeElement[]
450450
for i in nargs_def:length(argexprs)
451451
arg = argexprs[i]
452452
push!(tuple_call.args, arg)
453-
push!(tuple_typs, unwraptype(argextype(arg, compact)))
453+
push!(tuple_typs, argextype(arg, compact))
454454
end
455455
tuple_typ = tuple_tfunc(tuple_typs)
456456
push!(newargexprs, insert_node_here!(compact, NewInstruction(tuple_call, tuple_typ, line_idx)))
@@ -483,15 +483,15 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
483483
a <: m && continue
484484
# Generate isa check
485485
isa_expr = Expr(:call, isa, argexprs[i], m)
486-
ssa = insert_node_here!(compact, NewInstruction(isa_expr, Bool, line))
486+
ssa = insert_node_here!(compact, NewInstruction(isa_expr, LBool, line))
487487
if cond === true
488488
cond = ssa
489489
else
490490
and_expr = Expr(:call, and_int, cond, ssa)
491-
cond = insert_node_here!(compact, NewInstruction(and_expr, Bool, line))
491+
cond = insert_node_here!(compact, NewInstruction(and_expr, LBool, line))
492492
end
493493
end
494-
insert_node_here!(compact, NewInstruction(GotoIfNot(cond, next_cond_bb), Union{}, line))
494+
insert_node_here!(compact, NewInstruction(GotoIfNot(cond, next_cond_bb), , line))
495495
bb = next_cond_bb - 1
496496
finish_current_bb!(compact, 0)
497497
argexprs′ = argexprs
@@ -503,7 +503,7 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
503503
a, m = aparams[i], mparams[i]
504504
if !(a <: m)
505505
argexprs′[i] = insert_node_here!(compact,
506-
NewInstruction(PiNode(argex, m), m, line))
506+
NewInstruction(PiNode(argex, m), NativeType(m), line))
507507
end
508508
end
509509
end
@@ -520,25 +520,25 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
520520
push!(pn.edges, bb)
521521
push!(pn.values, val)
522522
insert_node_here!(compact,
523-
NewInstruction(GotoNode(join_bb), Union{}, line))
523+
NewInstruction(GotoNode(join_bb), , line))
524524
else
525525
insert_node_here!(compact,
526-
NewInstruction(ReturnNode(), Union{}, line))
526+
NewInstruction(ReturnNode(), , line))
527527
end
528528
finish_current_bb!(compact, 0)
529529
end
530530
bb += 1
531531
# We're now in the fall through block, decide what to do
532532
if fully_covered
533533
e = Expr(:call, GlobalRef(Core, :throw), FATAL_TYPE_BOUND_ERROR)
534-
insert_node_here!(compact, NewInstruction(e, Union{}, line))
535-
insert_node_here!(compact, NewInstruction(ReturnNode(), Union{}, line))
534+
insert_node_here!(compact, NewInstruction(e, , line))
535+
insert_node_here!(compact, NewInstruction(ReturnNode(), , line))
536536
finish_current_bb!(compact, 0)
537537
else
538538
ssa = insert_node_here!(compact, NewInstruction(stmt, typ, line))
539539
push!(pn.edges, bb)
540540
push!(pn.values, ssa)
541-
insert_node_here!(compact, NewInstruction(GotoNode(join_bb), Union{}, line))
541+
insert_node_here!(compact, NewInstruction(GotoNode(join_bb), , line))
542542
finish_current_bb!(compact, 0)
543543
end
544544

@@ -702,15 +702,15 @@ function rewrite_apply_exprargs!(
702702
new_sig, istate, todo)
703703
end
704704
if i != length(thisarginfo.each)
705-
valT = getfield_tfunc(unwraptype(call.rt), Const(1))
705+
valT = LatticeElement(getfield_tfunc(unwraptype(call.rt), Const(1)))
706706
val_extracted = insert_node!(ir, idx, NewInstruction(
707707
Expr(:call, GlobalRef(Core, :getfield), state1, 1),
708708
valT))
709709
push!(new_argexprs, val_extracted)
710-
push!(new_argtypes, LatticeElement(valT))
710+
push!(new_argtypes, valT)
711711
state_extracted = insert_node!(ir, idx, NewInstruction(
712712
Expr(:call, GlobalRef(Core, :getfield), state1, 2),
713-
getfield_tfunc(unwraptype(call.rt), Const(2))))
713+
LatticeElement(getfield_tfunc(unwraptype(call.rt), Const(2)))))
714714
state = Core.svec(state_extracted)
715715
end
716716
end
@@ -907,7 +907,7 @@ function inline_splatnew!(ir::IRCode, idx::Int, stmt::Expr, rt::LatticeElement)
907907
n = constant(tnf)::Int
908908
new_argexprs = Any[eargs[1]]
909909
for j = 1:n
910-
atype = getfield_tfunc(unwraptype(tt), Const(j))
910+
atype = LatticeElement(getfield_tfunc(unwraptype(tt), Const(j)))
911911
new_call = Expr(:call, Core.getfield, tup, j)
912912
new_argexpr = insert_node!(ir, idx, NewInstruction(new_call, atype))
913913
push!(new_argexprs, new_argexpr)
@@ -1045,8 +1045,8 @@ function narrow_opaque_closure!(ir::IRCode, stmt::Expr, @nospecialize(info), sta
10451045
ub, exact = instanceof_tfunc(unwraptype(ubt))
10461046
exact || return
10471047
# Narrow opaque closure type
1048-
newT = widenconst(tmeet(tmerge(lb, info.unspec.rt), ub))
1049-
if newT != ub
1048+
newT = widenconst(tmerge(lb, info.unspec.rt) ub)
1049+
if newT !== ub
10501050
# N.B.: Narrowing the ub requires a backdge on the mi whose type
10511051
# information we're using, since a change in that function may
10521052
# invalidate ub result.
@@ -1436,7 +1436,7 @@ function late_inline_special_case!(
14361436
return SomeCase(quoted(constant(type)))
14371437
end
14381438
cmp_call = Expr(:call, GlobalRef(Core, :(===)), stmt.args[2], stmt.args[3])
1439-
cmp_call_ssa = insert_node!(ir, idx, effect_free(NewInstruction(cmp_call, Bool)))
1439+
cmp_call_ssa = insert_node!(ir, idx, effect_free(NewInstruction(cmp_call, LBool)))
14401440
not_call = Expr(:call, GlobalRef(Core.Intrinsics, :not_int), cmp_call_ssa)
14411441
return SomeCase(not_call)
14421442
elseif isinlining && length(argtypes) == 3 && istopfunction(f, :(>:))

base/compiler/ssair/ir.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,17 @@ struct NewInstruction
166166
# Don't bother redoing so on insertion.
167167
effect_free_computed::Bool
168168

169-
function NewInstruction(@nospecialize(stmt), @nospecialize(type), @nospecialize(info),
169+
function NewInstruction(@nospecialize(stmt), type::LatticeElement, @nospecialize(info),
170170
line::Union{Int32, Nothing}, flag::UInt8, effect_free_computed::Bool)
171171
if isa(type, Type)
172172
type = NativeType(type)
173173
end
174174
return new(stmt, type, info, line, flag, effect_free_computed)
175175
end
176176
end
177-
NewInstruction(@nospecialize(stmt), @nospecialize(type)) =
177+
NewInstruction(@nospecialize(stmt), type::LatticeElement) =
178178
NewInstruction(stmt, type, nothing)
179-
NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{Nothing, Int32}) =
179+
NewInstruction(@nospecialize(stmt), type::LatticeElement, line::Union{Nothing, Int32}) =
180180
NewInstruction(stmt, type, nothing, line, IR_FLAG_NULL, false)
181181

182182
effect_free(inst::NewInstruction) =
@@ -1156,7 +1156,7 @@ function finish_current_bb!(compact::IncrementalCompact, active_bb, old_result_i
11561156
if unreachable
11571157
node[:inst], node[:type], node[:line] = ReturnNode(), ⊥, 0
11581158
else
1159-
node[:inst], node[:type], node[:line] = nothing, NativeType(Nothing), 0
1159+
node[:inst], node[:type], node[:line] = nothing, LNothing, 0
11601160
end
11611161
compact.result_idx = old_result_idx + 1
11621162
elseif compact.cfg_transforms_enabled && compact.result_idx - 1 == first(bb.stmts)

0 commit comments

Comments
 (0)