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: Add successors to assembly format #3124

Merged
merged 1 commit into from
Sep 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
179 changes: 179 additions & 0 deletions tests/irdl/test_declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
opt_prop_def,
opt_region_def,
opt_result_def,
opt_successor_def,
prop_def,
region_def,
result_def,
successor_def,
var_operand_def,
var_region_def,
var_result_def,
var_successor_def,
)
from xdsl.parser import Parser
from xdsl.printer import Printer
Expand Down Expand Up @@ -1329,6 +1332,182 @@ class OptionalOperandsOp(IRDLOperation): # pyright: ignore[reportUnusedClass]
assembly_format = "attr-dict-with-keyword $region1 $region2"


################################################################################
# Successors #
################################################################################


def test_missing_successor():
"""Test that successors should be parsed."""
with pytest.raises(PyRDLOpDefinitionError, match="successor 'successor' not found"):

@irdl_op_definition
class NoSuccessorOp(IRDLOperation): # pyright: ignore[reportUnusedClass]
name = "test.no_successor_op"
successor = successor_def()

assembly_format = "attr-dict-with-keyword"


def test_successors():
"""Test the parsing of successors"""

program = textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
test.two_successors ^0 ^0
}) : () -> ()"""
)

generic_program = textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
"test.two_successors"() [^0, ^0] : () -> ()
}) : () -> ()"""
)

@irdl_op_definition
class TwoSuccessorsOp(IRDLOperation):
name = "test.two_successors"
fst = successor_def()
snd = successor_def()

assembly_format = "$fst $snd attr-dict"

ctx = MLContext()
ctx.load_op(TwoSuccessorsOp)
ctx.load_dialect(Test)

check_roundtrip(program, ctx)
check_equivalence(program, generic_program, ctx)


@pytest.mark.parametrize(
"program, generic_program",
[
(
'"test.op"() ({\n "test.op"() [^0] : () -> ()\n^0:\n test.var_successor \n}) : () -> ()',
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
"test.var_successor"() : () -> ()
}) : () -> ()"""
),
),
(
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
test.var_successor ^0
}) : () -> ()"""
),
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
"test.var_successor"() [^0] : () -> ()
}) : () -> ()"""
),
),
(
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
test.var_successor ^0 ^0
}) : () -> ()"""
),
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
"test.var_successor"() [^0, ^0] : () -> ()
}) : () -> ()"""
),
),
],
)
def test_variadic_successor(program: str, generic_program: str):
"""Test the parsing of successors"""

@irdl_op_definition
class VarSuccessorOp(IRDLOperation):
name = "test.var_successor"
succ = var_successor_def()

assembly_format = "$succ attr-dict"

ctx = MLContext()
ctx.load_op(VarSuccessorOp)
ctx.load_dialect(Test)

check_roundtrip(program, ctx)
check_equivalence(program, generic_program, ctx)


@pytest.mark.parametrize(
"program, generic_program",
[
(
'"test.op"() ({\n "test.op"() [^0] : () -> ()\n^0:\n test.opt_successor \n}) : () -> ()',
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
"test.opt_successor"() : () -> ()
}) : () -> ()"""
),
),
(
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
test.opt_successor ^0
}) : () -> ()"""
),
textwrap.dedent(
"""\
"test.op"() ({
"test.op"() [^0] : () -> ()
^0:
"test.opt_successor"() [^0] : () -> ()
}) : () -> ()"""
),
),
],
)
def test_optional_successor(program: str, generic_program: str):
"""Test the parsing of successors"""

@irdl_op_definition
class OptSuccessorOp(IRDLOperation):
name = "test.opt_successor"
succ = opt_successor_def()

assembly_format = "$succ attr-dict"

ctx = MLContext()
ctx.load_op(OptSuccessorOp)
ctx.load_dialect(Test)

check_roundtrip(program, ctx)
check_equivalence(program, generic_program, ctx)


################################################################################
# Inference #
################################################################################
Expand Down
82 changes: 78 additions & 4 deletions xdsl/irdl/declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
IRDLOperationInvT,
OpDef,
OptionalDef,
Successor,
VariadicDef,
VarIRConstruct,
)
Expand All @@ -50,19 +51,17 @@ class ParsingState:
operand_types: list[Attribute | None | list[Attribute | None]]
result_types: list[Attribute | None | list[Attribute | None]]
regions: list[Region | None | list[Region]]
successors: list[Successor | None | list[Successor]]
attributes: dict[str, Attribute]
properties: dict[str, Attribute]
constraint_context: ConstraintContext

def __init__(self, op_def: OpDef):
if op_def.successors:
raise NotImplementedError(
"Operation definitions with successors are not yet supported"
)
self.operands = [None] * len(op_def.operands)
self.operand_types = [None] * len(op_def.operands)
self.result_types = [None] * len(op_def.results)
self.regions = [None] * len(op_def.regions)
self.successors = [None] * len(op_def.successors)
self.attributes = {}
self.properties = {}
self.constraint_context = ConstraintContext()
Expand Down Expand Up @@ -167,6 +166,7 @@ def parse(
attributes=state.attributes,
properties=properties,
regions=state.regions,
successors=state.successors,
)

def assign_constraint_variables(
Expand Down Expand Up @@ -805,6 +805,80 @@ def print(self, printer: Printer, state: PrintingState, op: IRDLOperation) -> No
state.should_emit_space = True


class SuccessorVariable(VariableDirective, OptionallyParsableDirective):
"""
A successor variable, with the following format:
successor-directive ::= dollar-ident
The directive will request a space to be printed after.
"""

def parse_optional(self, parser: Parser, state: ParsingState) -> bool:
successor = parser.parse_optional_successor()

state.successors[self.index] = successor

return successor is not None

def print(self, printer: Printer, state: PrintingState, op: IRDLOperation) -> None:
if state.should_emit_space or not state.last_was_punctuation:
printer.print(" ")
printer.print_block_name(getattr(op, self.name))
state.last_was_punctuation = False
state.should_emit_space = True


class VariadicSuccessorVariable(VariadicVariable, OptionallyParsableDirective):
"""
A variadic successor variable, with the following format:
successor-directive ::= dollar-ident
The directive will request a space to be printed after.
"""

def parse_optional(self, parser: Parser, state: ParsingState) -> bool:
successors: list[Successor] = []
current_successor = parser.parse_optional_successor()
while current_successor is not None:
successors.append(current_successor)
current_successor = parser.parse_optional_successor()

state.successors[self.index] = successors

return bool(successors)

def print(self, printer: Printer, state: PrintingState, op: IRDLOperation) -> None:
if state.should_emit_space or not state.last_was_punctuation:
printer.print(" ")
successor = getattr(op, self.name)
if successor:
printer.print_list(successor, printer.print_block_name, delimiter=" ")
state.last_was_punctuation = False
state.should_emit_space = True


class OptionalSuccessorVariable(OptionalVariable, OptionallyParsableDirective):
"""
An optional successor variable, with the following format:
successor-directive ::= dollar-ident
The directive will request a space to be printed after.
"""

def parse_optional(self, parser: Parser, state: ParsingState) -> bool:
successor = parser.parse_optional_successor()
if successor is None:
successor = list[Successor]()
state.successors[self.index] = successor
return bool(successor)

def print(self, printer: Printer, state: PrintingState, op: IRDLOperation) -> None:
if state.should_emit_space or not state.last_was_punctuation:
printer.print(" ")
successor = getattr(op, self.name)
if successor:
printer.print_block_name(successor)
state.last_was_punctuation = False
state.should_emit_space = True


@dataclass(frozen=True)
class AttributeVariable(FormatDirective):
"""
Expand Down
Loading
Loading