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: store blocks as doubly-linked list on Region [2/2] #2795

Merged
merged 6 commits into from
Jul 1, 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
104 changes: 103 additions & 1 deletion tests/test_linked_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest

from xdsl.dialects.arith import Constant
from xdsl.dialects.builtin import i32
from xdsl.ir import Block
from xdsl.ir import Block, Region


def test_block_insert():
Expand Down Expand Up @@ -73,3 +75,103 @@ def test_block_insert():
assert e.prev_op is d

assert list(block.ops) == [a, b, c, d, e]


def test_region_insert():
a = Block((Constant.from_int_and_width(1, i32),))
b = Block((Constant.from_int_and_width(2, i32),))
c = Block((Constant.from_int_and_width(3, i32),))
d = Block((Constant.from_int_and_width(4, i32),))
e = Block((Constant.from_int_and_width(5, i32),))

region = Region()

assert not region.blocks

assert region.first_block is None
assert region.last_block is None

assert list(region.blocks) == []

region.add_block(a)

assert region.blocks

assert region.first_block is a
assert region.last_block is a

assert list(region.blocks) == [a]

region.add_block(c)

assert region.first_block is a
assert region.last_block is c

assert a.next_block is c
assert c.prev_block is a

assert list(region.blocks) == [a, c]

region.insert_block_after(b, a)

assert region.first_block is a
assert region.last_block is c

assert a.next_block is b
assert b.prev_block is a

assert b.next_block is c
assert c.prev_block is b

assert list(region.blocks) == [a, b, c]

region.add_block(e)

assert region.first_block is a
assert region.last_block is e

assert c.next_block is e
assert e.prev_block is c

assert list(region.blocks) == [a, b, c, e]

region.insert_block_before(d, e)

assert region.first_block is a
assert region.last_block is e

assert c.next_block is d
assert d.prev_block is c

assert d.next_block is e
assert e.prev_block is d

assert list(region.blocks) == [a, b, c, d, e]


def test_region_indexing():
a = Block((Constant.from_int_and_width(1, i32),))
b = Block((Constant.from_int_and_width(2, i32),))
c = Block((Constant.from_int_and_width(3, i32),))
d = Block((Constant.from_int_and_width(4, i32),))
e = Block((Constant.from_int_and_width(5, i32),))

region = Region((a, b, c, d, e))

assert region.blocks[0] == a
assert region.blocks[1] == b
assert region.blocks[2] == c
assert region.blocks[3] == d
assert region.blocks[4] == e

with pytest.raises(IndexError):
region.blocks[5]

assert region.blocks[-1] == e
assert region.blocks[-2] == d
assert region.blocks[-3] == c
assert region.blocks[-4] == b
assert region.blocks[-5] == a

with pytest.raises(IndexError):
region.blocks[-6]
2 changes: 1 addition & 1 deletion xdsl/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ def parse(cls, parser: Parser) -> ModuleOp:
region = parser.parse_region()

# Add a block if the region is empty
if len(region.blocks) == 0:
if not region.blocks:
region.add_block(Block())

return ModuleOp(region, attributes)
Expand Down
7 changes: 1 addition & 6 deletions xdsl/dialects/riscv_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,7 @@ def verify_(self) -> None:
if parent_region is None:
return

# TODO: use next_block to check this instead
blocks = tuple(parent_region.blocks)
this_index = blocks.index(parent_block)
else_index = blocks.index(self.else_block)

if this_index + 1 != else_index:
if parent_block.next_block is not self.else_block:
raise VerifyException(
"riscv_cf branch op else block must be immediately after op"
)
Expand Down
7 changes: 1 addition & 6 deletions xdsl/dialects/x86/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2051,12 +2051,7 @@ def verify_(self) -> None:
if parent_region is None:
return

# TODO: replace check with next_block later
blocks = tuple(parent_region.blocks)
this_index = blocks.index(parent_block)
else_index = blocks.index(self.else_block)

if this_index + 1 != else_index:
if parent_block.next_block is not self.else_block:
raise VerifyException("else block must be immediately after op")

def assembly_line_args(self) -> tuple[AssemblyInstructionArg, ...]:
Expand Down
Loading
Loading