Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made jump targets and back edges an attribute of SCFG #58

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 1 addition & 57 deletions numba_rvsdg/core/datastructures/basic_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,6 @@ class BasicBlock:
name: str
"""The corresponding name for this block. """

_jump_targets: Tuple[str] = tuple()
"""Jump targets (branch destinations) for this block"""

backedges: Tuple[str] = tuple()
"""Backedges for this block."""

@property
def is_exiting(self) -> bool:
return not self.jump_targets

@property
def fallthrough(self) -> bool:
return len(self._jump_targets) == 1

@property
def jump_targets(self) -> Tuple[str]:
acc = []
for j in self._jump_targets:
if j not in self.backedges:
acc.append(j)
return tuple(acc)

def replace_backedge(self, target: str) -> "BasicBlock":
if target in self.jump_targets:
assert not self.backedges
return replace(self, backedges=(target,))
return self

def replace_jump_targets(self, jump_targets: Tuple) -> "BasicBlock":
return replace(self, _jump_targets=jump_targets)


@dataclass(frozen=True)
class PythonBytecodeBlock(BasicBlock):
Expand Down Expand Up @@ -102,31 +71,6 @@ class SyntheticBranch(SyntheticBlock):
variable: str = None
branch_value_table: dict = None

def replace_jump_targets(self, jump_targets: Tuple) -> "BasicBlock":
fallthrough = len(jump_targets) == 1
old_branch_value_table = self.branch_value_table
new_branch_value_table = {}
for target in self.jump_targets:
if target not in jump_targets:
# ASSUMPTION: only one jump_target is being updated
diff = set(jump_targets).difference(self.jump_targets)
assert len(diff) == 1
new_target = next(iter(diff))
for k, v in old_branch_value_table.items():
if v == target:
new_branch_value_table[k] = new_target
else:
# copy all old values
for k, v in old_branch_value_table.items():
if v == target:
new_branch_value_table[k] = v

return replace(
self,
_jump_targets=jump_targets,
branch_value_table=new_branch_value_table,
)


@dataclass(frozen=True)
class SyntheticHead(SyntheticBranch):
Expand Down Expand Up @@ -156,5 +100,5 @@ class RegionBlock(BasicBlock):
"""

def get_full_graph(self):
graph = ChainMap(self.subregion.graph, self.headers)
graph = ChainMap(self.subregion.blocks, self.headers)
return graph
2 changes: 1 addition & 1 deletion numba_rvsdg/core/datastructures/byte_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def restructure(self):


def _iter_subregions(scfg: "SCFG"):
for node in scfg.graph.values():
for node in scfg.blocks.values():
if isinstance(node, RegionBlock):
yield node
yield from _iter_subregions(node.subregion)
12 changes: 5 additions & 7 deletions numba_rvsdg/core/datastructures/flow_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,17 @@ def build_basicblocks(self: "FlowInfo", end_offset=None) -> "SCFG":

for begin, end in zip(offsets, [*offsets[1:], end_offset]):
name = names[begin]
targets: Tuple[str, ...]
targets: list[str]
term_offset = _prev_inst_offset(end)
if term_offset not in self.jump_insts:
# implicit jump
targets = (names[end],)
targets = [names[end]]
else:
targets = tuple(names[o] for o in self.jump_insts[term_offset])
targets = [names[o] for o in self.jump_insts[term_offset]]
block = PythonBytecodeBlock(
name=name,
begin=begin,
end=end,
_jump_targets=targets,
backedges=(),
end=end
)
scfg.add_block(block)
scfg.add_block(block, targets, [])
return scfg
Loading