Skip to content

Commit

Permalink
prefix all SCFG variables with __scfg_
Browse files Browse the repository at this point in the history
As title
  • Loading branch information
esc committed Jun 3, 2024
1 parent 682aff7 commit 40b9daa
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 79 deletions.
37 changes: 20 additions & 17 deletions numba_rvsdg/core/datastructures/ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ def function(a: int) -> None
# the CFG.
target = ast.unparse(node.target)
iter_setup = ast.unparse(node.iter)
iter_assign = f"__iterator_{head_index}__"
last_target_value = f"__iter_last_{head_index}__"
iter_assign = f"__scfg_iterator_{head_index}__"
last_target_value = f"__scfg_iter_last_{head_index}__"

# Emit iterator setup to pre-header.
preheader_code = textwrap.dedent(
Expand All @@ -605,14 +605,14 @@ def function(a: int) -> None

# Emit header instructions. This first makes a backup of the iteration
# target and then checks if the iterator is exhausted and if the loop
# should continue. The '__sentinel__' is an singleton style marker, so
# it need not be versioned.
# should continue. The '_scfg__sentinel__' is an singleton style
# marker, so it need not be versioned.

header_code = textwrap.dedent(
f"""
{last_target_value} = {target}
{target} = next({iter_assign}, "__sentinel__")
{target} != "__sentinel__"
{target} = next({iter_assign}, "__scfg_sentinel__")
{target} != "__scfg_sentinel__"
"""
)
self.codegen(ast.parse(header_code).body)
Expand Down Expand Up @@ -720,12 +720,13 @@ def codegen(self, block: Any) -> list[ast.AST]:
# 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
# 'return', which is implicitly 'return None'. So, if it is
# None, we assign the __return_value__ a ast.Constant(None) and
# whatever the ast.AST node is otherwise.
# None, we assign the __scfg_return_value__ an
# ast.Constant(None) and whatever the ast.AST node is
# otherwise.
val = block.tree[-1].value
return block.tree[:-1] + [
ast.Assign(
[ast.Name("__return_value__")],
[ast.Name("__scfg_return_value__")],
(ast.Constant(None) if val is None else val),
lineno=0,
)
Expand Down Expand Up @@ -756,17 +757,17 @@ def codegen_view() -> list[Any]:
if block.kind in ("head", "tail", "branch"):
rval = codegen_view()
elif block.kind == "loop":
# A loop region gives rise to a Python while __loop_cont__
# A loop region gives rise to a Python while __scfg_loop_cont__
# loop. We recursively visit the body. The exiting latch will
# update __loop_continue__.
# update __scfg_loop_continue__.
rval = [
ast.Assign(
[ast.Name("__loop_cont__")],
[ast.Name("__scfg_loop_cont__")],
ast.Constant(True),
lineno=0,
),
ast.While(
test=ast.Name("__loop_cont__"),
test=ast.Name("__scfg_loop_cont__"),
body=codegen_view(),
orelse=[],
),
Expand All @@ -779,7 +780,9 @@ def codegen_view() -> list[Any]:
# Synthetic assignments just create Python assignments, one for
# each variable..
return [
ast.Assign([ast.Name(t)], ast.Constant(v), lineno=0)
ast.Assign(
[ast.Name(t)], ast.Constant(v), lineno=0
)
for t, v in block.variable_assignment.items()
]
elif type(block) is SyntheticTail:
Expand All @@ -792,15 +795,15 @@ def codegen_view() -> list[Any]:
elif type(block) is SyntheticReturn:
# Synthetic return blocks must re-assigne the return value to a
# special reserved variable.
return [ast.Return(ast.Name("__return_value__"))]
return [ast.Return(ast.Name("__scfg_return_value__"))]
elif type(block) is SyntheticExitingLatch:
# The synthetic exiting latch simply assigns the negated value of
# the exit variable to '__loop_cont__'.
# the exit variable to '__scfg_loop_cont__'.
assert len(block.jump_targets) == 1
assert len(block.backedges) == 1
return [
ast.Assign(
[ast.Name("__loop_cont__")],
[ast.Name("__scfg_loop_cont__")],
ast.UnaryOp(ast.Not(), ast.Name(block.variable)),
lineno=0,
)
Expand Down
4 changes: 2 additions & 2 deletions numba_rvsdg/core/datastructures/scfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ def new_var_name(self, kind: str) -> str:
"""
if kind in self.kinds.keys():
idx = self.kinds[kind]
name = str(kind) + "_var_" + str(idx)
name = "__scfg_" + str(kind) + "_var_" + str(idx) + "__"
self.kinds[kind] = idx + 1
else:
idx = 0
name = str(kind) + "_var_" + str(idx)
name = "__scfg_" + str(kind) + "_var_" + str(idx) + "__"
self.kinds[kind] = idx + 1
return name

Expand Down
80 changes: 40 additions & 40 deletions numba_rvsdg/tests/test_ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,17 +769,17 @@ def function() -> int:
"0": {
"instructions": [
"x = 0",
"__iterator_1__ = iter(range(10))",
"__scfg_iterator_1__ = iter(range(10))",
"i = None",
],
"jump_targets": ["1"],
"name": "0",
},
"1": {
"instructions": [
"__iter_last_1__ = i",
"i = next(__iterator_1__, '__sentinel__')",
"i != '__sentinel__'",
"__scfg_iter_last_1__ = i",
"i = next(__scfg_iterator_1__, '__scfg_sentinel__')",
"i != '__scfg_sentinel__'",
],
"jump_targets": ["2", "3"],
"name": "1",
Expand All @@ -790,7 +790,7 @@ def function() -> int:
"name": "2",
},
"3": {
"instructions": ["i = __iter_last_1__"],
"instructions": ["i = __scfg_iter_last_1__"],
"jump_targets": ["4"],
"name": "3",
},
Expand All @@ -815,32 +815,32 @@ def function() -> int:
"0": {
"instructions": [
"x = 0",
"__iterator_1__ = iter(range(3))",
"__scfg_iterator_1__ = iter(range(3))",
"i = None",
],
"jump_targets": ["1"],
"name": "0",
},
"1": {
"instructions": [
"__iter_last_1__ = i",
"i = next(__iterator_1__, '__sentinel__')",
"i != '__sentinel__'",
"__scfg_iter_last_1__ = i",
"i = next(__scfg_iterator_1__, '__scfg_sentinel__')",
"i != '__scfg_sentinel__'",
],
"jump_targets": ["2", "3"],
"name": "1",
},
"2": {
"instructions": [
"x += i",
"__iterator_5__ = iter(range(3))",
"__scfg_iterator_5__ = iter(range(3))",
"j = None",
],
"jump_targets": ["5"],
"name": "2",
},
"3": {
"instructions": ["i = __iter_last_1__"],
"instructions": ["i = __scfg_iter_last_1__"],
"jump_targets": ["4"],
"name": "3",
},
Expand All @@ -851,9 +851,9 @@ def function() -> int:
},
"5": {
"instructions": [
"__iter_last_5__ = j",
"j = next(__iterator_5__, '__sentinel__')",
"j != '__sentinel__'",
"__scfg_iter_last_5__ = j",
"j = next(__scfg_iterator_5__, '__scfg_sentinel__')",
"j != '__scfg_sentinel__'",
],
"jump_targets": ["6", "7"],
"name": "5",
Expand All @@ -864,7 +864,7 @@ def function() -> int:
"name": "6",
},
"7": {
"instructions": ["j = __iter_last_5__"],
"instructions": ["j = __scfg_iter_last_5__"],
"jump_targets": ["1"],
"name": "7",
},
Expand All @@ -887,17 +887,17 @@ def function(x: int, y: int) -> int:
expected = {
"0": {
"instructions": [
"__iterator_1__ = iter(range(2))",
"__scfg_iterator_1__ = iter(range(2))",
"i = None",
],
"jump_targets": ["1"],
"name": "0",
},
"1": {
"instructions": [
"__iter_last_1__ = i",
"i = next(__iterator_1__, '__sentinel__')",
"i != '__sentinel__'",
"__scfg_iter_last_1__ = i",
"i = next(__scfg_iterator_1__, '__scfg_sentinel__')",
"i != '__scfg_sentinel__'",
],
"jump_targets": ["2", "3"],
"name": "1",
Expand All @@ -908,7 +908,7 @@ def function(x: int, y: int) -> int:
"name": "2",
},
"3": {
"instructions": ["i = __iter_last_1__"],
"instructions": ["i = __scfg_iter_last_1__"],
"jump_targets": ["4"],
"name": "3",
},
Expand Down Expand Up @@ -957,17 +957,17 @@ def function(y: int):
"0": {
"instructions": [
"x = 0",
"__iterator_1__ = iter(range(10))",
"__scfg_iterator_1__ = iter(range(10))",
"i = None",
],
"jump_targets": ["1"],
"name": "0",
},
"1": {
"instructions": [
"__iter_last_1__ = i",
"i = next(__iterator_1__, '__sentinel__')",
"i != '__sentinel__'",
"__scfg_iter_last_1__ = i",
"i = next(__scfg_iterator_1__, '__scfg_sentinel__')",
"i != '__scfg_sentinel__'",
],
"jump_targets": ["2", "3"],
"name": "1",
Expand All @@ -978,7 +978,7 @@ def function(y: int):
"name": "2",
},
"3": {
"instructions": ["i = __iter_last_1__", "y == 0"],
"instructions": ["i = __scfg_iter_last_1__", "y == 0"],
"jump_targets": ["5", "6"],
"name": "3",
},
Expand Down Expand Up @@ -1023,31 +1023,31 @@ def function(y: int) -> int:
"0": {
"instructions": [
"x = 1",
"__iterator_1__ = iter(range(1))",
"__scfg_iterator_1__ = iter(range(1))",
"i = None",
],
"jump_targets": ["1"],
"name": "0",
},
"1": {
"instructions": [
"__iter_last_1__ = i",
"i = next(__iterator_1__, '__sentinel__')",
"i != '__sentinel__'",
"__scfg_iter_last_1__ = i",
"i = next(__scfg_iterator_1__, '__scfg_sentinel__')",
"i != '__scfg_sentinel__'",
],
"jump_targets": ["2", "3"],
"name": "1",
},
"2": {
"instructions": [
"__iterator_5__ = iter(range(1))",
"__scfg_iterator_5__ = iter(range(1))",
"j = None",
],
"jump_targets": ["5"],
"name": "2",
},
"3": {
"instructions": ["i = __iter_last_1__", "x *= 9"],
"instructions": ["i = __scfg_iter_last_1__", "x *= 9"],
"jump_targets": ["4"],
"name": "3",
},
Expand All @@ -1058,9 +1058,9 @@ def function(y: int) -> int:
},
"5": {
"instructions": [
"__iter_last_5__ = j",
"j = next(__iterator_5__, '__sentinel__')",
"j != '__sentinel__'",
"__scfg_iter_last_5__ = j",
"j = next(__scfg_iterator_5__, '__scfg_sentinel__')",
"j != '__scfg_sentinel__'",
],
"jump_targets": ["6", "7"],
"name": "5",
Expand All @@ -1071,7 +1071,7 @@ def function(y: int) -> int:
"name": "6",
},
"7": {
"instructions": ["j = __iter_last_5__", "x *= 5"],
"instructions": ["j = __scfg_iter_last_5__", "x *= 5"],
"jump_targets": ["1"],
"name": "7",
},
Expand Down Expand Up @@ -1122,17 +1122,17 @@ def function(x: int) -> int:
expected = {
"0": {
"instructions": [
"__iterator_1__ = iter(range(2))",
"__scfg_iterator_1__ = iter(range(2))",
"i = None",
],
"jump_targets": ["1"],
"name": "0",
},
"1": {
"instructions": [
"__iter_last_1__ = i",
"i = next(__iterator_1__, '__sentinel__')",
"i != '__sentinel__'",
"__scfg_iter_last_1__ = i",
"i = next(__scfg_iterator_1__, '__scfg_sentinel__')",
"i != '__scfg_sentinel__'",
],
"jump_targets": ["2", "3"],
"name": "1",
Expand Down Expand Up @@ -1188,7 +1188,7 @@ def function(x: int) -> int:
"name": "25",
},
"3": {
"instructions": ["i = __iter_last_1__"],
"instructions": ["i = __scfg_iter_last_1__"],
"jump_targets": ["4"],
"name": "3",
},
Expand Down
Loading

0 comments on commit 40b9daa

Please sign in to comment.