Skip to content

Commit

Permalink
core: fix (and test) operands/results directives with no variadics (#…
Browse files Browse the repository at this point in the history
…3551)

Managed to miss the test for when there are no variadic/optional
operands or results. Turns out this functionality was broken.
  • Loading branch information
alexarice authored Dec 2, 2024
1 parent 6328693 commit 3d23c8f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
48 changes: 43 additions & 5 deletions tests/irdl/test_declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ class TwoOperandsOp(IRDLOperation):
"test.operands_directive %0, %1, %2 : i32, i32, i32",
],
)
def test_operands_directive(program: str):
"""Test the operands directive"""
def test_operands_directive_with_variadic(program: str):
"""Test the operands directive with a variadic operand"""

@irdl_op_definition
class OperandsDirectiveOp(IRDLOperation):
Expand All @@ -1041,7 +1041,7 @@ class OperandsDirectiveOp(IRDLOperation):
],
)
def test_operands_directive_with_optional(program: str):
"""Test the operands directive"""
"""Test the operands directive with an optional operand"""

@irdl_op_definition
class OperandsDirectiveOp(IRDLOperation):
Expand All @@ -1059,6 +1059,25 @@ class OperandsDirectiveOp(IRDLOperation):
check_roundtrip(program, ctx)


def test_operands_directive_with_no_variadic():
"""Test the operands directive with no variadic operands"""

@irdl_op_definition
class OperandsDirectiveOp(IRDLOperation):
name = "test.operands_directive"

op1 = operand_def()
op2 = operand_def()

assembly_format = "operands `:` type(operands) attr-dict"

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

check_roundtrip("test.operands_directive %0, %1 : i32, i32", ctx)


def test_operands_directive_fails_with_two_var():
"""Test operands directive cannot be used with two variadic operands"""

Expand Down Expand Up @@ -1393,8 +1412,8 @@ class OptionalResultOp(IRDLOperation):
"%0, %1, %2 = test.results_directive : i32, i32, i32",
],
)
def test_results_directive(program: str):
"""Test the results directive"""
def test_results_directive_with_variadic(program: str):
"""Test the results directive with a variadic result"""

@irdl_op_definition
class ResultsDirectiveOp(IRDLOperation):
Expand Down Expand Up @@ -1438,6 +1457,25 @@ class ResultsDirectiveOp(IRDLOperation):
check_roundtrip(program, ctx)


def test_results_directive_with_no_variadic():
"""Test the results directive with no variadic results"""

@irdl_op_definition
class ResultsDirectiveOp(IRDLOperation):
name = "test.results_directive"

res1 = result_def()
res2 = result_def()

assembly_format = "attr-dict `:` type(results)"

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

check_roundtrip("%0, %1 = test.results_directive : i32, i32", ctx)


def test_results_directive_fails_with_two_var():
"""Test results directive cannot be used with two variadic results"""

Expand Down
2 changes: 1 addition & 1 deletion xdsl/irdl/declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def _set_using_variadic_index(
if self.variadic_index is None:
if len(set_to) != len(field):
return f"Expected {len(field)} {field_name} but found {len(set_to)}"
field = [o for o in set_to] # Copy needed as list is not covariant
field[:] = set_to
return

is_optional, var_position = self.variadic_index
Expand Down

0 comments on commit 3d23c8f

Please sign in to comment.