Skip to content

Commit

Permalink
feature: make prune_noops a pass and allow pass statement
Browse files Browse the repository at this point in the history
As title
  • Loading branch information
esc committed Apr 24, 2024
1 parent a497881 commit dc64724
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
20 changes: 17 additions & 3 deletions numba_rvsdg/core/datastructures/ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ def seal_inside_loop(
"""
if self.is_continue():
self.set_jump_targets(head_index)
self.instructions.pop()
elif self.is_break():
self.set_jump_targets(exit_index)
self.instructions.pop()
elif self.is_return():
pass
else:
Expand All @@ -89,7 +87,7 @@ def to_dict(self) -> dict[str, Any]:
def __repr__(self) -> str:
return (
f"WritableASTBlock({self.name}, "
"{self.instructions}, {self.jump_targets})"
f"{self.instructions}, {self.jump_targets})"
)


Expand Down Expand Up @@ -134,6 +132,20 @@ def prune_unreachable(self) -> set[WritableASTBlock]:
self.unreachable = unreachable
return unreachable

def prune_noops(self) -> set[type[ast.AST]]:
"""Prune no-op instructions from the CFG."""
noops = set()
exclude = (ast.Pass, ast.Continue, ast.Break)
for block in self.values():
block.instructions = [
i for i in block.instructions if not isinstance(i, exclude)
]
noops.update(
[i for i in block.instructions if isinstance(i, exclude)]
)
self.noops = noops
return noops # type: ignore

def prune_empty(self) -> set[WritableASTBlock]:
"""Prune empty blocks from the CFG."""
empty = set()
Expand Down Expand Up @@ -230,6 +242,7 @@ def transform(self) -> None:
# Prune if requested.
if self.prune:
_ = self.blocks.prune_unreachable()
_ = self.blocks.prune_noops()
_ = self.blocks.prune_empty()

def codegen(self, tree: list[type[ast.AST]] | list[ast.stmt]) -> None:
Expand Down Expand Up @@ -257,6 +270,7 @@ def handle_ast_node(self, node: type[ast.AST] | ast.stmt) -> None:
ast.Return,
ast.Break,
ast.Continue,
ast.Pass,
),
):
self.current_block.instructions.append(node)
Expand Down
13 changes: 13 additions & 0 deletions numba_rvsdg/tests/test_ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def function() -> None:
}
self.compare(function, expected)

def test_solo_pass(self):
def function() -> None:
pass

expected = {
"0": {
"instructions": ["return"],
"jump_targets": [],
"name": "0",
}
}
self.compare(function, expected)

def test_assign_return(self):
def function() -> int:
x = 1
Expand Down

0 comments on commit dc64724

Please sign in to comment.