Skip to content

Commit c999912

Browse files
committed
lattice overhaul step 3: clean up implementations
1 parent 17ff56f commit c999912

File tree

15 files changed

+1000
-929
lines changed

15 files changed

+1000
-929
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 71 additions & 73 deletions
Large diffs are not rendered by default.

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 = cnd.vtype sigt
41+
elsetype = 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function stmt_effect_free(@nospecialize(stmt), @nospecialize(rt), src::Union{IRC
223223
rt ===&& return false
224224
return _builtin_nothrow(f, LatticeElement[argextype(args[i], src) for i = 2:length(args)], rt)
225225
elseif head === :new
226-
typ = unwraptype(argextype(args[1], src))
226+
typ = argextype(args[1], src)
227227
# `Expr(:new)` of unknown type could raise arbitrary TypeError.
228228
typ, isexact = instanceof_tfunc(typ)
229229
isexact || return false
@@ -240,7 +240,7 @@ function stmt_effect_free(@nospecialize(stmt), @nospecialize(rt), src::Union{IRC
240240
return foreigncall_effect_free(stmt, rt, src)
241241
elseif head === :new_opaque_closure
242242
length(args) < 5 && return false
243-
typ = unwraptype(argextype(args[1], src))
243+
typ = argextype(args[1], src)
244244
typ, isexact = instanceof_tfunc(typ)
245245
isexact || return false
246246
typ Tuple || return false
@@ -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: 25 additions & 25 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
@@ -443,11 +443,11 @@ function fix_va_argexprs!(compact::IncrementalCompact,
443443
argexprs::Vector{Any}, nargs_def::Int, line_idx::Int32)
444444
newargexprs = argexprs[1:(nargs_def-1)]
445445
tuple_call = Expr(:call, TOP_TUPLE)
446-
tuple_typs = Any[]
446+
tuple_typs = LatticeElement[]
447447
for i in nargs_def:length(argexprs)
448448
arg = argexprs[i]
449449
push!(tuple_call.args, arg)
450-
push!(tuple_typs, unwraptype(argextype(arg, compact)))
450+
push!(tuple_typs, argextype(arg, compact))
451451
end
452452
tuple_typ = tuple_tfunc(tuple_typs)
453453
push!(newargexprs, insert_node_here!(compact, NewInstruction(tuple_call, tuple_typ, line_idx)))
@@ -480,15 +480,15 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
480480
a <: m && continue
481481
# Generate isa check
482482
isa_expr = Expr(:call, isa, argexprs[i], m)
483-
ssa = insert_node_here!(compact, NewInstruction(isa_expr, Bool, line))
483+
ssa = insert_node_here!(compact, NewInstruction(isa_expr, LBool, line))
484484
if cond === true
485485
cond = ssa
486486
else
487487
and_expr = Expr(:call, and_int, cond, ssa)
488-
cond = insert_node_here!(compact, NewInstruction(and_expr, Bool, line))
488+
cond = insert_node_here!(compact, NewInstruction(and_expr, LBool, line))
489489
end
490490
end
491-
insert_node_here!(compact, NewInstruction(GotoIfNot(cond, next_cond_bb), Union{}, line))
491+
insert_node_here!(compact, NewInstruction(GotoIfNot(cond, next_cond_bb), , line))
492492
bb = next_cond_bb - 1
493493
finish_current_bb!(compact, 0)
494494
argexprs′ = argexprs
@@ -500,7 +500,7 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
500500
a, m = aparams[i], mparams[i]
501501
if !(a <: m)
502502
argexprs′[i] = insert_node_here!(compact,
503-
NewInstruction(PiNode(argex, m), m, line))
503+
NewInstruction(PiNode(argex, m), NativeType(m), line))
504504
end
505505
end
506506
end
@@ -517,25 +517,25 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
517517
push!(pn.edges, bb)
518518
push!(pn.values, val)
519519
insert_node_here!(compact,
520-
NewInstruction(GotoNode(join_bb), Union{}, line))
520+
NewInstruction(GotoNode(join_bb), , line))
521521
else
522522
insert_node_here!(compact,
523-
NewInstruction(ReturnNode(), Union{}, line))
523+
NewInstruction(ReturnNode(), , line))
524524
end
525525
finish_current_bb!(compact, 0)
526526
end
527527
bb += 1
528528
# We're now in the fall through block, decide what to do
529529
if fully_covered
530530
e = Expr(:call, GlobalRef(Core, :throw), FATAL_TYPE_BOUND_ERROR)
531-
insert_node_here!(compact, NewInstruction(e, Union{}, line))
532-
insert_node_here!(compact, NewInstruction(ReturnNode(), Union{}, line))
531+
insert_node_here!(compact, NewInstruction(e, , line))
532+
insert_node_here!(compact, NewInstruction(ReturnNode(), , line))
533533
finish_current_bb!(compact, 0)
534534
else
535535
ssa = insert_node_here!(compact, NewInstruction(stmt, typ, line))
536536
push!(pn.edges, bb)
537537
push!(pn.values, ssa)
538-
insert_node_here!(compact, NewInstruction(GotoNode(join_bb), Union{}, line))
538+
insert_node_here!(compact, NewInstruction(GotoNode(join_bb), , line))
539539
finish_current_bb!(compact, 0)
540540
end
541541

@@ -642,7 +642,7 @@ function rewrite_apply_exprargs!(
642642
if thisarginfo === nothing
643643
if isPartialStruct(def_type)
644644
# def_type.typ <: Tuple is assumed
645-
def_argtypes = LatticeElement[LatticeElement(t) for t in partialfields(def_type)]
645+
def_argtypes = partialfields(def_type)
646646
else
647647
def_argtypes = LatticeElement[]
648648
if isConst(def_type) # && isa(constant(def_type), Union{Tuple, SimpleVector}) is implied
@@ -699,15 +699,15 @@ function rewrite_apply_exprargs!(
699699
new_sig, istate, todo)
700700
end
701701
if i != length(thisarginfo.each)
702-
valT = getfield_tfunc(unwraptype(call.rt), Const(1))
702+
valT = getfield_tfunc(call.rt, Const(1))
703703
val_extracted = insert_node!(ir, idx, NewInstruction(
704704
Expr(:call, GlobalRef(Core, :getfield), state1, 1),
705705
valT))
706706
push!(new_argexprs, val_extracted)
707-
push!(new_argtypes, LatticeElement(valT))
707+
push!(new_argtypes, valT)
708708
state_extracted = insert_node!(ir, idx, NewInstruction(
709709
Expr(:call, GlobalRef(Core, :getfield), state1, 2),
710-
getfield_tfunc(unwraptype(call.rt), Const(2))))
710+
getfield_tfunc(call.rt, Const(2))))
711711
state = Core.svec(state_extracted)
712712
end
713713
end
@@ -892,19 +892,19 @@ function is_valid_type_for_apply_rewrite(typ::LatticeElement, params::Optimizati
892892
end
893893

894894
function inline_splatnew!(ir::IRCode, idx::Int, stmt::Expr, rt::LatticeElement)
895-
nf = nfields_tfunc(unwraptype(rt))
895+
nf = nfields_tfunc(rt)
896896
if isConst(nf)
897897
eargs = stmt.args
898898
tup = eargs[2]
899899
tt = argextype(tup, ir)
900-
tnf = nfields_tfunc(unwraptype(tt))
900+
tnf = nfields_tfunc(tt)
901901
# TODO: hoisting this constant(tnf) === constant(nf) check into codegen
902902
# would enable us to almost always do this transform
903903
if isConst(tnf) && constant(tnf) === constant(nf)
904904
n = constant(tnf)::Int
905905
new_argexprs = Any[eargs[1]]
906906
for j = 1:n
907-
atype = getfield_tfunc(unwraptype(tt), Const(j))
907+
atype = getfield_tfunc(tt, Const(j))
908908
new_call = Expr(:call, Core.getfield, tup, j)
909909
new_argexpr = insert_node!(ir, idx, NewInstruction(new_call, atype))
910910
push!(new_argexprs, new_argexpr)
@@ -1036,14 +1036,14 @@ end
10361036
function narrow_opaque_closure!(ir::IRCode, stmt::Expr, @nospecialize(info), state::InliningState)
10371037
if isa(info, OpaqueClosureCreateInfo)
10381038
lbt = argextype(stmt.args[3], ir)
1039-
lb, exact = instanceof_tfunc(unwraptype(lbt))
1039+
lb, exact = instanceof_tfunc(lbt)
10401040
exact || return
10411041
ubt = argextype(stmt.args[4], ir)
1042-
ub, exact = instanceof_tfunc(unwraptype(ubt))
1042+
ub, exact = instanceof_tfunc(ubt)
10431043
exact || return
10441044
# Narrow opaque closure type
1045-
newT = widenconst(tmeet(tmerge(lb, info.unspec.rt), ub))
1046-
if newT != ub
1045+
newT = widenconst(tmerge(lb, info.unspec.rt) ub)
1046+
if newT !== ub
10471047
# N.B.: Narrowing the ub requires a backdge on the mi whose type
10481048
# information we're using, since a change in that function may
10491049
# invalidate ub result.
@@ -1433,7 +1433,7 @@ function late_inline_special_case!(
14331433
return SomeCase(quoted(constant(type)))
14341434
end
14351435
cmp_call = Expr(:call, GlobalRef(Core, :(===)), stmt.args[2], stmt.args[3])
1436-
cmp_call_ssa = insert_node!(ir, idx, effect_free(NewInstruction(cmp_call, Bool)))
1436+
cmp_call_ssa = insert_node!(ir, idx, effect_free(NewInstruction(cmp_call, LBool)))
14371437
not_call = Expr(:call, GlobalRef(Core.Intrinsics, :not_int), cmp_call_ssa)
14381438
return SomeCase(not_call)
14391439
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)

base/compiler/ssair/passes.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ const LiftedLeaves = IdDict{Any, Union{Nothing,LiftedValue}}
340340
# try to compute lifted values that can replace `getfield(x, field)` call
341341
# where `x` is an immutable struct that are defined at any of `leaves`
342342
function lift_leaves(compact::IncrementalCompact,
343-
@nospecialize(result_t), field::Int, leaves::Vector{Any})
343+
result_t::LatticeElement, field::Int, leaves::Vector{Any})
344344
# For every leaf, the lifted value
345345
lifted_leaves = LiftedLeaves()
346346
maybe_undef = false
@@ -513,7 +513,7 @@ function lift_comparison!(compact::IncrementalCompact,
513513
# Let's check if we evaluate the comparison for each one of the leaves
514514
lifted_leaves = nothing
515515
for leaf in leaves
516-
r = egal_tfunc(unwraptype(argextype(leaf, compact)), cmp)
516+
r = egal_tfunc(argextype(leaf, compact), cmp)
517517
if isConst(r)
518518
if lifted_leaves === nothing
519519
lifted_leaves = LiftedLeaves()
@@ -525,7 +525,7 @@ function lift_comparison!(compact::IncrementalCompact,
525525
end
526526

527527
lifted_val = perform_lifting!(compact,
528-
visited_phinodes, cmp, lifting_cache, Bool,
528+
visited_phinodes, cmp, lifting_cache, LBool,
529529
lifted_leaves::LiftedLeaves, val)::LiftedValue
530530

531531
compact[idx] = lifted_val.x
@@ -546,7 +546,7 @@ end
546546
function perform_lifting!(compact::IncrementalCompact,
547547
visited_phinodes::Vector{AnySSAValue}, @nospecialize(cache_key),
548548
lifting_cache::IdDict{Pair{AnySSAValue, Any}, AnySSAValue},
549-
@nospecialize(result_t), lifted_leaves::LiftedLeaves, @nospecialize(stmt_val))
549+
result_t::LatticeElement, lifted_leaves::LiftedLeaves, @nospecialize(stmt_val))
550550
reverse_mapping = IdDict{AnySSAValue, Int}(ssa => id for (id, ssa) in enumerate(visited_phinodes))
551551

552552
# Insert PhiNodes
@@ -792,7 +792,7 @@ function sroa_pass!(ir::IRCode)
792792
if any_undef
793793
if val === nothing
794794
insert_node!(compact, SSAValue(idx),
795-
non_effect_free(NewInstruction(Expr(:throw_undef_if_not, Symbol("##getfield##"), false), Nothing)))
795+
non_effect_free(NewInstruction(Expr(:throw_undef_if_not, Symbol("##getfield##"), false), LNothing)))
796796
else
797797
# val must be defined
798798
end
@@ -914,7 +914,7 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse
914914
phinodes = IdDict{Int, SSAValue}()
915915
for b in phiblocks
916916
phinodes[b] = insert_node!(ir, first(ir.cfg.blocks[b].stmts),
917-
NewInstruction(PhiNode(), ftyp))
917+
NewInstruction(PhiNode(), NativeType(ftyp)))
918918
end
919919
# Now go through all uses and rewrite them
920920
for stmt in du.uses
@@ -1156,15 +1156,15 @@ function type_lift_pass!(ir::IRCode)
11561156
new_phi = if length(values) == 0
11571157
false
11581158
else
1159-
insert_node!(ir, item, NewInstruction(PhiNode(edges, values), Bool))
1159+
insert_node!(ir, item, NewInstruction(PhiNode(edges, values), LBool))
11601160
end
11611161
else
11621162
def = def::PhiCNode
11631163
values = Vector{Any}(undef, length(def.values))
11641164
new_phi = if length(values) == 0
11651165
false
11661166
else
1167-
insert_node!(ir, item, NewInstruction(PhiCNode(values), Bool))
1167+
insert_node!(ir, item, NewInstruction(PhiCNode(values), LBool))
11681168
end
11691169
end
11701170
processed[item] = new_phi
@@ -1215,7 +1215,7 @@ function type_lift_pass!(ir::IRCode)
12151215
if isa(def, PhiNode)
12161216
values[i] = val
12171217
else
1218-
values[i] = insert_node!(ir, up_id, NewInstruction(UpsilonNode(val), Bool))
1218+
values[i] = insert_node!(ir, up_id, NewInstruction(UpsilonNode(val), LBool))
12191219
end
12201220
end
12211221
if which !== SSAValue(0)
@@ -1224,7 +1224,7 @@ function type_lift_pass!(ir::IRCode)
12241224
phi.values[use] = new_phi
12251225
else
12261226
phi = phi::PhiCNode
1227-
phi.values[use] = insert_node!(ir, w_up_id, NewInstruction(UpsilonNode(new_phi), Bool))
1227+
phi.values[use] = insert_node!(ir, w_up_id, NewInstruction(UpsilonNode(new_phi), LBool))
12281228
end
12291229
end
12301230
end

0 commit comments

Comments
 (0)