Skip to content

Commit dccf331

Browse files
authored
IncrementalCompact: fix display of just-compacted new nodes (#46950)
1 parent 1855583 commit dccf331

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

base/compiler/ssair/show.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -675,14 +675,20 @@ function show_ir_stmt(io::IO, code::Union{IRCode, CodeInfo, IncrementalCompact},
675675
return bb_idx
676676
end
677677

678-
function _new_nodes_iter(stmts, new_nodes, new_nodes_info)
678+
function _new_nodes_iter(stmts, new_nodes, new_nodes_info, new_nodes_idx)
679679
new_nodes_perm = filter(i -> isassigned(new_nodes.inst, i), 1:length(new_nodes))
680680
sort!(new_nodes_perm, by = x -> (x = new_nodes_info[x]; (x.pos, x.attach_after)))
681681
perm_idx = Ref(1)
682682

683-
return function (idx::Int)
683+
return function get_new_node(idx::Int)
684684
perm_idx[] <= length(new_nodes_perm) || return nothing
685685
node_idx = new_nodes_perm[perm_idx[]]
686+
if node_idx < new_nodes_idx
687+
# skip new nodes that have already been processed by incremental compact
688+
# (but don't just return nothing because there may be multiple at this pos)
689+
perm_idx[] += 1
690+
return get_new_node(idx)
691+
end
686692
if new_nodes_info[node_idx].pos != idx
687693
return nothing
688694
end
@@ -695,18 +701,18 @@ function _new_nodes_iter(stmts, new_nodes, new_nodes_info)
695701
end
696702
end
697703

698-
function new_nodes_iter(ir::IRCode)
704+
function new_nodes_iter(ir::IRCode, new_nodes_idx=1)
699705
stmts = ir.stmts
700706
new_nodes = ir.new_nodes.stmts
701707
new_nodes_info = ir.new_nodes.info
702-
return _new_nodes_iter(stmts, new_nodes, new_nodes_info)
708+
return _new_nodes_iter(stmts, new_nodes, new_nodes_info, new_nodes_idx)
703709
end
704710

705711
function new_nodes_iter(compact::IncrementalCompact)
706712
stmts = compact.result
707713
new_nodes = compact.new_new_nodes.stmts
708714
new_nodes_info = compact.new_new_nodes.info
709-
return _new_nodes_iter(stmts, new_nodes, new_nodes_info)
715+
return _new_nodes_iter(stmts, new_nodes, new_nodes_info, 1)
710716
end
711717

712718
# print only line numbers on the left, some of the method names and nesting depth on the right
@@ -864,7 +870,7 @@ function show_ir(io::IO, compact::IncrementalCompact, config::IRShowConfig=defau
864870
# config.line_info_preprinter(io, "", compact.idx)
865871
printstyled(io, ""^(width-indent-1), '\n', color=:red)
866872

867-
pop_new_node! = new_nodes_iter(compact.ir)
873+
pop_new_node! = new_nodes_iter(compact.ir, compact.new_nodes_idx)
868874
maxssaid = length(compact.ir.stmts) + Core.Compiler.length(compact.ir.new_nodes)
869875
let io = IOContext(io, :maxssaid=>maxssaid)
870876
show_ir_stmts(io, compact.ir, compact.idx:length(stmts), config, used_uncompacted, cfg, bb_idx; pop_new_node!)

test/show.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,3 +2448,40 @@ end
24482448
end
24492449
@test contains(str, "%1 = \e[31m%7")
24502450
end
2451+
2452+
@testset "issue #46947: IncrementalCompact double display of just-compacted nodes" begin
2453+
# get some IR
2454+
foo(i) = i == 1 ? 1 : 2
2455+
ir = only(Base.code_ircode(foo, (Int,)))[1]
2456+
2457+
instructions = length(ir.stmts)
2458+
lines_shown(obj) = length(findall('\n', sprint(io->show(io, obj))))
2459+
@test lines_shown(ir) == instructions
2460+
2461+
# insert a couple of instructions
2462+
let inst = Core.Compiler.NewInstruction(Expr(:identity, 1), Nothing)
2463+
Core.Compiler.insert_node!(ir, 2, inst)
2464+
end
2465+
let inst = Core.Compiler.NewInstruction(Expr(:identity, 2), Nothing)
2466+
Core.Compiler.insert_node!(ir, 2, inst)
2467+
end
2468+
let inst = Core.Compiler.NewInstruction(Expr(:identity, 3), Nothing)
2469+
Core.Compiler.insert_node!(ir, 4, inst)
2470+
end
2471+
instructions += 3
2472+
@test lines_shown(ir) == instructions
2473+
2474+
# compact the IR, ensuring we always show the same number of lines
2475+
# (the instructions + a separator line)
2476+
compact = Core.Compiler.IncrementalCompact(ir)
2477+
@test lines_shown(compact) == instructions + 1
2478+
state = Core.Compiler.iterate(compact)
2479+
while state !== nothing
2480+
@test lines_shown(compact) == instructions + 1
2481+
state = Core.Compiler.iterate(compact, state[2])
2482+
end
2483+
@test lines_shown(compact) == instructions + 1
2484+
2485+
ir = Core.Compiler.complete(compact)
2486+
@test lines_shown(compact) == instructions + 1
2487+
end

0 commit comments

Comments
 (0)