Skip to content

Commit 89b8057

Browse files
authored
Merge pull request #32145 from yhls/yhls/fixrenaming
fix bug in block renaming for dead code elimination
2 parents 20ef262 + 3829e8f commit 89b8057

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

base/compiler/ssair/ir.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,24 +499,28 @@ mutable struct IncrementalCompact
499499
cur_bb = 1
500500
for i = 1:length(bb_rename)
501501
if i != 1 && length(blocks[i].preds) == 0
502-
bb_rename[i] = 0
502+
bb_rename[i] = -1
503503
else
504504
bb_rename[i] = cur_bb
505505
cur_bb += 1
506506
end
507507
end
508508
for i = 1:length(bb_rename)
509-
bb_rename[i] == 0 && continue
509+
bb_rename[i] == -1 && continue
510510
preds, succs = blocks[i].preds, blocks[i].succs
511511
# Rename preds
512-
for j = 1:length(preds); preds[j] = bb_rename[preds[j]]; end
512+
for j = 1:length(preds)
513+
if preds[j] != 0
514+
preds[j] = bb_rename[preds[j]]
515+
end
516+
end
513517
# Dead blocks get removed from the predecessor list
514-
filter!(x->x !== 0, preds)
518+
filter!(x->x !== -1, preds)
515519
# Rename succs
516520
for j = 1:length(succs); succs[j] = bb_rename[succs[j]]; end
517521
end
518522
let blocks=blocks
519-
result_bbs = BasicBlock[blocks[i] for i = 1:length(blocks) if bb_rename[i] != 0]
523+
result_bbs = BasicBlock[blocks[i] for i = 1:length(blocks) if bb_rename[i] != -1]
520524
end
521525
else
522526
bb_rename = Vector{Int}()

test/compiler/ssair.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ let
9797
end
9898
end
9999

100+
# PR #32145
101+
# Make sure IncrementalCompact can handle blocks with predecessors of index 0
102+
# while removing blocks with no predecessors.
103+
let cfg = CFG(BasicBlock[
104+
make_bb([] , [2, 4]),
105+
make_bb([1] , [4, 5]),
106+
make_bb([] , [4] ), # should be removed
107+
make_bb([0, 1, 2] , [5] ), # 0 predecessor should be preserved
108+
make_bb([2, 3] , [] ),
109+
], Int[])
110+
code = Compiler.IRCode(
111+
[], [], Int32[], UInt8[], cfg, LineInfoNode[], [], [], [])
112+
compact = Compiler.IncrementalCompact(code, true)
113+
@test length(compact.result_bbs) == 4 && 0 in compact.result_bbs[3].preds
114+
end
115+
100116
# Issue #32579 - Optimizer bug involving type constraints
101117
function f32579(x::Int, b::Bool)
102118
if b

0 commit comments

Comments
 (0)