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

core: replace BlockReverseOps with a Reversible conformance #2792

Merged
merged 1 commit into from
Jun 27, 2024
Merged
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
8 changes: 4 additions & 4 deletions xdsl/backend/riscv/register_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def allocate_for_loop(self, loop: riscv_scf.ForOp) -> None:
assert isinstance(iter_arg.type, IntRegisterType | FloatRegisterType)
self.available_registers.reserve_register(iter_arg.type)

for op in loop.body.block.ops_reverse:
for op in reversed(loop.body.block.ops):
self.process_operation(op)

# Unreserve the loop carried variables for allocation outside of the body
Expand Down Expand Up @@ -260,7 +260,7 @@ def allocate_frep_loop(self, loop: riscv_snitch.FRepOperation) -> None:
assert isinstance(iter_arg.type, IntRegisterType | FloatRegisterType)
self.available_registers.reserve_register(iter_arg.type)

for op in loop.body.block.ops_reverse:
for op in reversed(loop.body.block.ops):
self.process_operation(op)

# Unreserve the loop carried variables for allocation outside of the body
Expand Down Expand Up @@ -299,7 +299,7 @@ def allocate_func(self, func: riscv_func.FuncOp) -> None:

self.live_ins_per_block = live_ins_per_block(block)
assert not self.live_ins_per_block[block]
for op in block.ops_reverse:
for op in reversed(block.ops):
self.process_operation(op)


Expand All @@ -308,7 +308,7 @@ def _live_ins_per_block(
) -> OrderedSet[SSAValue]:
res = OrderedSet[SSAValue]([])

for op in block.ops_reverse:
for op in reversed(block.ops):
# Remove values defined in the block
# We are traversing backwards, so cannot use the value removed here again
res.difference_update(op.results)
Expand Down
37 changes: 8 additions & 29 deletions xdsl/ir/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re
from abc import ABC, abstractmethod
from collections.abc import Hashable, Iterable, Iterator, Mapping, Sequence
from collections.abc import Hashable, Iterable, Iterator, Mapping, Reversible, Sequence
from dataclasses import dataclass, field
from io import StringIO
from itertools import chain
Expand Down Expand Up @@ -1075,7 +1075,7 @@ def __format__(self, __format_spec: str) -> str:


@dataclass
class _BlockOpsIterator:
class _BlockOpsIterator(Iterator[Operation]):
"""
Single-pass iterable of the operations in a block. Follows the next_op for
each operation.
Expand All @@ -1095,7 +1095,7 @@ def __next__(self):


@dataclass
class _BlockOpsReverseIterator:
class _BlockOpsReverseIterator(Iterator[Operation]):
"""
Single-pass iterable of the operations in a block. Follows the prev_op for
each operation.
Expand All @@ -1115,7 +1115,7 @@ def __next__(self):


@dataclass
class BlockOps:
class BlockOps(Reversible[Operation], Iterable[Operation]):
"""
Multi-pass iterable of the operations in a block. Follows the next_op for
each operation.
Expand All @@ -1136,6 +1136,9 @@ def __bool__(self) -> bool:
"""Returns `True` if there are operations in this block."""
return not self.block.is_empty

def __reversed__(self):
return _BlockOpsReverseIterator(self.block.last_op)

@property
def first(self) -> Operation | None:
"""
Expand All @@ -1151,25 +1154,6 @@ def last(self) -> Operation | None:
return self.block.last_op


@dataclass
class BlockReverseOps:
"""
Multi-pass iterable of the operations in a block. Follows the prev_op for
each operation.
"""

block: Block

def __iter__(self):
return _BlockOpsReverseIterator(self.block.last_op)

def __len__(self):
result = 0
for _ in self:
result += 1
return result


@dataclass(init=False)
class Block(IRNode):
"""A sequence of operations"""
Expand Down Expand Up @@ -1208,11 +1192,6 @@ def ops(self) -> BlockOps:
"""Returns a multi-pass Iterable of this block's operations."""
return BlockOps(self)

@property
def ops_reverse(self) -> BlockReverseOps:
"""Returns a multi-pass Iterable of this block's operations."""
return BlockReverseOps(self)

def parent_op(self) -> Operation | None:
return self.parent.parent if self.parent else None

Expand Down Expand Up @@ -1480,7 +1459,7 @@ def walk(
operation. If reverse is set, then the region, block, and operation lists are
iterated in reverse order.
"""
for op in self.ops_reverse if reverse else self.ops:
for op in reversed(self.ops) if reverse else self.ops:
yield from op.walk(reverse=reverse, region_first=region_first)

def verify(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def match_and_rewrite(self, op: ApplyOp, rewriter: PatternRewriter, /):
new_return_op.detach()
new_return_op.erase()

for operation in new_apply_block.ops_reverse:
for operation in reversed(new_apply_block.ops):
op_index = new_apply_block.get_operation_index(operation)
if op_index not in operation_indices:
operation.detach()
Expand Down
Loading