Skip to content

Commit

Permalink
fix bug in block renaming for dead code elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
yhls committed Jul 27, 2019
1 parent 20ef262 commit 3829e8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 9 additions & 5 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,24 +499,28 @@ mutable struct IncrementalCompact
cur_bb = 1
for i = 1:length(bb_rename)
if i != 1 && length(blocks[i].preds) == 0
bb_rename[i] = 0
bb_rename[i] = -1
else
bb_rename[i] = cur_bb
cur_bb += 1
end
end
for i = 1:length(bb_rename)
bb_rename[i] == 0 && continue
bb_rename[i] == -1 && continue
preds, succs = blocks[i].preds, blocks[i].succs
# Rename preds
for j = 1:length(preds); preds[j] = bb_rename[preds[j]]; end
for j = 1:length(preds)
if preds[j] != 0
preds[j] = bb_rename[preds[j]]
end
end
# Dead blocks get removed from the predecessor list
filter!(x->x !== 0, preds)
filter!(x->x !== -1, preds)
# Rename succs
for j = 1:length(succs); succs[j] = bb_rename[succs[j]]; end
end
let blocks=blocks
result_bbs = BasicBlock[blocks[i] for i = 1:length(blocks) if bb_rename[i] != 0]
result_bbs = BasicBlock[blocks[i] for i = 1:length(blocks) if bb_rename[i] != -1]
end
else
bb_rename = Vector{Int}()
Expand Down
16 changes: 16 additions & 0 deletions test/compiler/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ let
end
end

# PR #32145
# Make sure IncrementalCompact can handle blocks with predecessors of index 0
# while removing blocks with no predecessors.
let cfg = CFG(BasicBlock[
make_bb([] , [2, 4]),
make_bb([1] , [4, 5]),
make_bb([] , [4] ), # should be removed
make_bb([0, 1, 2] , [5] ), # 0 predecessor should be preserved
make_bb([2, 3] , [] ),
], Int[])
code = Compiler.IRCode(
[], [], Int32[], UInt8[], cfg, LineInfoNode[], [], [], [])
compact = Compiler.IncrementalCompact(code, true)
@test length(compact.result_bbs) == 4 && 0 in compact.result_bbs[3].preds
end

# Issue #32579 - Optimizer bug involving type constraints
function f32579(x::Int, b::Bool)
if b
Expand Down

0 comments on commit 3829e8f

Please sign in to comment.