Skip to content

Commit 21b08aa

Browse files
Merge pull request #161 from arusuki/main
cfg: fix dead block detection by accounting for fall-through edges
2 parents b3daaaa + 550d6c7 commit 21b08aa

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

doc/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
ChangeLog
22
=========
33

4+
2025-04-14: Version 0.16.2
5+
--------------------------
6+
7+
Bugfixes:
8+
9+
- fix ControlFlowGraph dead block detection by accounting for fall-through
10+
edges. PR #161
11+
412
2025-01-21: Version 0.16.1
513
--------------------------
614

src/bytecode/cfg.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,12 +731,18 @@ def get_dead_blocks(self) -> List[BasicBlock]:
731731
if id(block) in seen_block_ids:
732732
continue
733733
seen_block_ids.add(id(block))
734+
fall_through = True
734735
for i in block:
735-
if isinstance(i, Instr) and isinstance(i.arg, BasicBlock):
736-
stack.append(i.arg)
736+
if isinstance(i, Instr):
737+
if isinstance(i.arg, BasicBlock):
738+
stack.append(i.arg)
739+
if i.is_final():
740+
fall_through = False
737741
elif isinstance(i, TryBegin):
738742
assert isinstance(i.target, BasicBlock)
739743
stack.append(i.target)
744+
if fall_through and block.next_block:
745+
stack.append(block.next_block)
740746

741747
return [b for b in self if id(b) not in seen_block_ids]
742748

tests/test_cfg.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,20 @@ def test_get_block_index(self):
712712
other_block = BasicBlock()
713713
self.assertRaises(ValueError, blocks.get_block_index, other_block)
714714

715+
def test_get_dead_blocks(self):
716+
def condition():
717+
pass
718+
719+
def test():
720+
if condition():
721+
print("1")
722+
else:
723+
print("2")
724+
725+
bytecode = Bytecode.from_code(test.__code__)
726+
cfg = ControlFlowGraph.from_bytecode(bytecode)
727+
assert len(cfg.get_dead_blocks()) == 0
728+
715729

716730
class CFGStacksizeComputationTests(TestCase):
717731
def check_stack_size(self, func):

0 commit comments

Comments
 (0)