Skip to content

Commit

Permalink
don't prune first block and emit pass for empty blocks
Browse files Browse the repository at this point in the history
As title
  • Loading branch information
esc committed Sep 25, 2024
1 parent 113fe23 commit 2f7924a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
5 changes: 4 additions & 1 deletion numba_rvsdg/core/datastructures/ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def prune_empty(self) -> set[WritableASTBlock]:
"""Prune empty blocks from the CFG."""
empty = set()
for name, block in list(self.items()):
if not block.instructions:
if not block.instructions and name != "0":
empty.add(self.pop(name))
# Empty blocks can only have a single jump target.
it = block.jump_targets[0]
Expand Down Expand Up @@ -724,6 +724,9 @@ def codegen(self, block: Any) -> MutableSequence[ast.AST]:
)
if_node = ast.If(test, body, orelse)
return block.tree[:-1] + [if_node]
elif block.fallthrough and len(block.tree) == 0:
# Am empty block, do nothing.
return [ast.Pass()]
elif block.fallthrough and type(block.tree[-1]) is ast.Return:
# The value of the ast.Return could be either None or an
# ast.AST type. In the case of None, this refers to a plain
Expand Down
23 changes: 6 additions & 17 deletions numba_rvsdg/tests/test_ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,39 +621,28 @@ def function() -> int:
def test_if_in_while_without_else(self):
def function(i) -> int:
while i < 10:
if i > 2:
s = 123
i += 1
return s
return i

expected = {
"0": {"instructions": [], "jump_targets": ["1"], "name": "0"},
"1": {
"instructions": ["i < 10"],
"jump_targets": ["2", "3"],
"name": "1",
},
"2": {
"instructions": ["i > 2"],
"jump_targets": ["5", "7"],
"instructions": ["i += 1"],
"jump_targets": ["1"],
"name": "2",
},
"3": {
"instructions": ["return s"],
"instructions": ["return i"],
"jump_targets": [],
"name": "3",
},
"5": {
"instructions": ["s = 123"],
"jump_targets": ["7"],
"name": "5",
},
"7": {
"instructions": ["i += 1"],
"jump_targets": ["1"],
"name": "7",
},
}
self.compare(function, expected, empty={"0", "4", "6"})
self.compare(function, expected, empty={"4"}, arguments=[(0,)])

def test_while_in_if(self):
def function(y: int) -> int:
Expand Down

0 comments on commit 2f7924a

Please sign in to comment.