Skip to content

Commit 6b41bc2

Browse files
committed
WIP
1 parent 4f1b68e commit 6b41bc2

File tree

9 files changed

+393
-144
lines changed

9 files changed

+393
-144
lines changed

base/compiler/ssair/driver.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,20 @@ end
159159

160160
function run_passes(ci::CodeInfo, nargs::Int, linetable::Vector{LineInfoNode}, sv::OptimizationState)
161161
ir = just_construct_ssa(ci, copy(ci.code), nargs, linetable)
162+
#@Base.show ("after_construct", ir)
162163
# TODO: Domsorting can produce an updated domtree - no need to recompute here
163164
@timeit "compact 1" ir = compact!(ir)
164165
#@timeit "verify 1" verify_ir(ir)
165166
@timeit "Inlining" ir = ssa_inlining_pass!(ir, linetable, sv)
166167
#@timeit "verify 2" verify_ir(ir)
167168
@timeit "domtree 2" domtree = construct_domtree(ir.cfg)
168169
ir = compact!(ir)
170+
#@Base.show ("before_sroa", ir)
169171
@timeit "SROA" ir = getfield_elim_pass!(ir, domtree)
172+
#@Base.show ir.new_nodes
173+
#@Base.show ("after_sroa", ir)
170174
ir = adce_pass!(ir)
175+
#@Base.show ("after_adce", ir)
171176
@timeit "type lift" ir = type_lift_pass!(ir)
172177
@timeit "compact 3" ir = compact!(ir)
173178
#@Base.show ir

base/compiler/ssair/ir.jl

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,16 @@ function resort_pending!(compact)
500500
sort!(compact.pending_perm, DEFAULT_STABLE, Order.By(x->compact.pending_nodes[x].pos))
501501
end
502502

503-
function insert_node!(compact::IncrementalCompact, before, @nospecialize(typ), @nospecialize(val), reverse_affinity::Bool=false)
503+
function insert_node!(compact::IncrementalCompact, before, @nospecialize(typ), @nospecialize(val), attach_after::Bool=false)
504504
if isa(before, SSAValue)
505505
if before.id < compact.result_idx
506506
count_added_node!(compact, val)
507507
line = compact.result_lines[before.id]
508-
push!(compact.new_new_nodes, NewNode(before.id, reverse_affinity, typ, val, line))
508+
push!(compact.new_new_nodes, NewNode(before.id, attach_after, typ, val, line))
509509
return NewSSAValue(length(compact.new_new_nodes))
510510
else
511511
line = compact.ir.lines[before.id]
512-
push!(compact.pending_nodes, NewNode(before.id, reverse_affinity, typ, val, line))
512+
push!(compact.pending_nodes, NewNode(before.id, attach_after, typ, val, line))
513513
push!(compact.pending_perm, length(compact.pending_nodes))
514514
resort_pending!(compact)
515515
os = OldSSAValue(length(compact.ir.stmts) + length(compact.ir.new_nodes) + length(compact.pending_nodes))
@@ -520,12 +520,12 @@ function insert_node!(compact::IncrementalCompact, before, @nospecialize(typ), @
520520
elseif isa(before, OldSSAValue)
521521
pos = before.id
522522
if pos > length(compact.ir.stmts)
523-
@assert reverse_affinity
523+
#@assert attach_after
524524
entry = compact.pending_nodes[pos - length(compact.ir.stmts) - length(compact.ir.new_nodes)]
525-
pos, reverse_affinity = entry.pos, entry.reverse_affinity
525+
pos, attach_after = entry.pos, entry.attach_after
526526
end
527-
line = 0 #compact.ir.lines[before.id]
528-
push!(compact.pending_nodes, NewNode(pos, reverse_affinity, typ, val, line))
527+
line = compact.ir.lines[pos]
528+
push!(compact.pending_nodes, NewNode(pos, attach_after, typ, val, line))
529529
push!(compact.pending_perm, length(compact.pending_nodes))
530530
resort_pending!(compact)
531531
os = OldSSAValue(length(compact.ir.stmts) + length(compact.ir.new_nodes) + length(compact.pending_nodes))
@@ -534,25 +534,33 @@ function insert_node!(compact::IncrementalCompact, before, @nospecialize(typ), @
534534
return os
535535
elseif isa(before, NewSSAValue)
536536
before_entry = compact.new_new_nodes[before.id]
537-
push!(compact.new_new_nodes, NewNode(before_entry.pos, reverse_affinity, typ, val, before_entry.line))
537+
push!(compact.new_new_nodes, NewNode(before_entry.pos, attach_after, typ, val, before_entry.line))
538538
return NewSSAValue(length(compact.new_new_nodes))
539539
else
540540
error("Unsupported")
541541
end
542542
end
543543

544-
function insert_node_here!(compact::IncrementalCompact, @nospecialize(val), @nospecialize(typ), ltable_idx::Int)
544+
function insert_node_here!(compact::IncrementalCompact, @nospecialize(val), @nospecialize(typ), ltable_idx::Int, reverse_affinity=false)
545545
if compact.result_idx > length(compact.result)
546546
@assert compact.result_idx == length(compact.result) + 1
547547
resize!(compact, compact.result_idx)
548548
end
549+
refinish = false
550+
if compact.result_idx == first(compact.result_bbs[compact.active_result_bb].stmts) && reverse_affinity
551+
compact.active_result_bb -= 1
552+
refinish = true
553+
end
549554
compact.result[compact.result_idx] = val
550555
compact.result_types[compact.result_idx] = typ
551556
compact.result_lines[compact.result_idx] = ltable_idx
552557
compact.result_flags[compact.result_idx] = 0x00
553-
count_added_node!(compact, val)
558+
if count_added_node!(compact, val)
559+
push!(compact.late_fixup, compact.result_idx)
560+
end
554561
ret = SSAValue(compact.result_idx)
555562
compact.result_idx += 1
563+
refinish && finish_current_bb!(compact)
556564
ret
557565
end
558566

@@ -624,27 +632,73 @@ end
624632

625633
function process_phinode_values(old_values::Vector{Any}, late_fixup::Vector{Int},
626634
processed_idx::Int, result_idx::Int,
627-
ssa_rename::Vector{Any}, used_ssas::Vector{Int})
635+
ssa_rename::Vector{Any}, used_ssas::Vector{Int},
636+
do_rename_ssa::Bool)
628637
values = Vector{Any}(undef, length(old_values))
629638
for i = 1:length(old_values)
630639
isassigned(old_values, i) || continue
631640
val = old_values[i]
632641
if isa(val, SSAValue)
642+
if do_rename_ssa
643+
if val.id > processed_idx
644+
push!(late_fixup, result_idx)
645+
val = OldSSAValue(val.id)
646+
else
647+
val = renumber_ssa2(val, ssa_rename, used_ssas, do_rename_ssa)
648+
end
649+
else
650+
used_ssas[val.id] += 1
651+
end
652+
elseif isa(val, OldSSAValue)
633653
if val.id > processed_idx
634654
push!(late_fixup, result_idx)
635-
val = OldSSAValue(val.id)
636655
else
637-
val = renumber_ssa!(val, ssa_rename, true, used_ssas)
656+
# Always renumber these. do_rename_ssa applies only to actual SSAValues
657+
val = renumber_ssa2(SSAValue(val.id), ssa_rename, used_ssas, true)
638658
end
659+
elseif isa(val, NewSSAValue)
660+
push!(late_fixup, result_idx)
639661
end
640662
values[i] = val
641663
end
642664
return values
643665
end
644666

667+
function renumber_ssa2(val::SSAValue, ssanums::Vector{Any}, used_ssa::Vector{Int}, do_rename_ssa::Bool)
668+
id = val.id
669+
if id > length(ssanums)
670+
return val
671+
end
672+
if do_rename_ssa
673+
val = ssanums[id]
674+
end
675+
if isa(val, SSAValue) && used_ssa !== nothing
676+
used_ssa[val.id] += 1
677+
end
678+
return val
679+
end
680+
681+
function renumber_ssa2!(@nospecialize(stmt), ssanums::Vector{Any}, used_ssa::Vector{Int}, late_fixup::Vector{Int}, result_idx::Int, do_rename_ssa::Bool)
682+
urs = userefs(stmt)
683+
for op in urs
684+
val = op[]
685+
if isa(val, OldSSAValue) || isa(val, NewSSAValue)
686+
push!(late_fixup, result_idx)
687+
end
688+
if isa(val, SSAValue)
689+
val = renumber_ssa2(val, ssanums, used_ssa, do_rename_ssa)
690+
end
691+
if isa(val, OldSSAValue) || isa(val, NewSSAValue)
692+
push!(late_fixup, result_idx)
693+
end
694+
op[] = val
695+
end
696+
return urs[]
697+
end
698+
645699
function process_node!(result::Vector{Any}, result_idx::Int, ssa_rename::Vector{Any},
646700
late_fixup::Vector{Int}, used_ssas::Vector{Int}, @nospecialize(stmt),
647-
idx::Int, processed_idx::Int)
701+
idx::Int, processed_idx::Int, do_rename_ssa::Bool)
648702
ssa_rename[idx] = SSAValue(result_idx)
649703
if stmt === nothing
650704
ssa_rename[idx] = stmt
@@ -654,27 +708,30 @@ function process_node!(result::Vector{Any}, result_idx::Int, ssa_rename::Vector{
654708
result[result_idx] = stmt
655709
result_idx += 1
656710
elseif isa(stmt, Expr) || isa(stmt, PiNode) || isa(stmt, GotoIfNot) || isa(stmt, ReturnNode) || isa(stmt, UpsilonNode)
657-
result[result_idx] = renumber_ssa!(stmt, ssa_rename, true, used_ssas)
711+
result[result_idx] = renumber_ssa2!(stmt, ssa_rename, used_ssas, late_fixup, result_idx, do_rename_ssa)
658712
result_idx += 1
659713
elseif isa(stmt, PhiNode)
660-
result[result_idx] = PhiNode(stmt.edges, process_phinode_values(stmt.values, late_fixup, processed_idx, result_idx, ssa_rename, used_ssas))
714+
result[result_idx] = PhiNode(stmt.edges, process_phinode_values(stmt.values, late_fixup, processed_idx, result_idx, ssa_rename, used_ssas, do_rename_ssa))
661715
result_idx += 1
662716
elseif isa(stmt, PhiCNode)
663-
result[result_idx] = PhiCNode(process_phinode_values(stmt.values, late_fixup, processed_idx, result_idx, ssa_rename, used_ssas))
717+
result[result_idx] = PhiCNode(process_phinode_values(stmt.values, late_fixup, processed_idx, result_idx, ssa_rename, used_ssas, do_rename_ssa))
664718
result_idx += 1
665719
elseif isa(stmt, SSAValue)
666720
# identity assign, replace uses of this ssa value with its result
667-
stmt = ssa_rename[stmt.id]
721+
if do_rename_ssa
722+
stmt = ssa_rename[stmt.id]
723+
end
668724
ssa_rename[idx] = stmt
669725
else
670726
# Constant assign, replace uses of this ssa value with its result
671727
ssa_rename[idx] = stmt
672728
end
673729
return result_idx
674730
end
675-
function process_node!(compact::IncrementalCompact, result_idx::Int, @nospecialize(stmt), idx::Int, processed_idx::Int)
731+
function process_node!(compact::IncrementalCompact, result_idx::Int, @nospecialize(stmt), idx::Int, processed_idx::Int, do_rename_ssa::Bool)
676732
return process_node!(compact.result, result_idx, compact.ssa_rename,
677-
compact.late_fixup, compact.used_ssas, stmt, idx, processed_idx)
733+
compact.late_fixup, compact.used_ssas, stmt, idx, processed_idx,
734+
do_rename_ssa)
678735
end
679736

680737
function resize!(compact::IncrementalCompact, nnewnodes)
@@ -717,12 +774,12 @@ function attach_after_stmt_after(compact::IncrementalCompact, idx::Int)
717774
entry.pos == idx && entry.attach_after
718775
end
719776

720-
function process_newnode!(compact, new_idx, new_node_entry, idx, active_bb)
777+
function process_newnode!(compact, new_idx, new_node_entry, idx, active_bb, do_rename_ssa)
721778
old_result_idx = compact.result_idx
722779
bb = compact.ir.cfg.blocks[active_bb]
723780
compact.result_types[old_result_idx] = new_node_entry.typ
724781
compact.result_lines[old_result_idx] = new_node_entry.line
725-
result_idx = process_node!(compact, old_result_idx, new_node_entry.node, new_idx, idx)
782+
result_idx = process_node!(compact, old_result_idx, new_node_entry.node, new_idx, idx, do_rename_ssa)
726783
compact.result_idx = result_idx
727784
# If this instruction has reverse affinity and we were at the end of a basic block,
728785
# finish it now.
@@ -750,21 +807,21 @@ function iterate(compact::IncrementalCompact, (idx, active_bb)::Tuple{Int, Int}=
750807
compact.new_nodes_idx += 1
751808
new_node_entry = compact.ir.new_nodes[new_idx]
752809
new_idx += length(compact.ir.stmts)
753-
return process_newnode!(compact, new_idx, new_node_entry, idx, active_bb)
810+
return process_newnode!(compact, new_idx, new_node_entry, idx, active_bb, true)
754811
elseif !isempty(compact.pending_perm) &&
755812
(entry = compact.pending_nodes[compact.pending_perm[1]];
756813
entry.attach_after ? entry.pos == idx - 1 : entry.pos == idx)
757814
new_idx = popfirst!(compact.pending_perm)
758815
new_node_entry = compact.pending_nodes[new_idx]
759816
new_idx += length(compact.ir.stmts) + length(compact.ir.new_nodes)
760-
return process_newnode!(compact, new_idx, new_node_entry, idx, active_bb)
817+
return process_newnode!(compact, new_idx, new_node_entry, idx, active_bb, false)
761818
end
762819
# This will get overwritten in future iterations if
763820
# result_idx is not, incremented, but that's ok and expected
764821
compact.result_types[old_result_idx] = compact.ir.types[idx]
765822
compact.result_lines[old_result_idx] = compact.ir.lines[idx]
766823
compact.result_flags[old_result_idx] = compact.ir.flags[idx]
767-
result_idx = process_node!(compact, old_result_idx, compact.ir.stmts[idx], idx, idx)
824+
result_idx = process_node!(compact, old_result_idx, compact.ir.stmts[idx], idx, idx, true)
768825
stmt_if_any = old_result_idx == result_idx ? nothing : compact.result[old_result_idx]
769826
compact.result_idx = result_idx
770827
if idx == last(bb.stmts) && !attach_after_stmt_after(compact, idx)
@@ -849,7 +906,11 @@ function just_fixup!(compact)
849906
for idx in 1:length(compact.new_new_nodes)
850907
node = compact.new_new_nodes[idx]
851908
new_stmt = fixup_node(compact, node.node)
852-
(node.node !== new_stmt) && (compact.new_new_nodes[idx] = NewNode(node, node=new_stmt))
909+
if node.node !== new_stmt
910+
compact.new_new_nodes[idx] = NewNode(
911+
node.pos, node.attach_after, node.typ,
912+
new_stmt, node.line)
913+
end
853914
end
854915
end
855916

0 commit comments

Comments
 (0)