Skip to content

Commit

Permalink
core: add custom attr and prop name support to assembly format (#2123)
Browse files Browse the repository at this point in the history
We can either use the python or the irdl name in the custom assembly
format, it make sense to me to stick to the IRDL name of things to be
portable. Not sure if there's an aspect that I'm missing here.
  • Loading branch information
superlopuh authored Feb 10, 2024
1 parent 1a491ea commit 4d9849f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
50 changes: 50 additions & 0 deletions tests/test_declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ def test_attr_variable_shadowed():
parser.parse_operation()


@pytest.mark.parametrize(
"program, generic_program",
[
("test.one_attr i32", '"test.one_attr"() {"irdl" = i32} : () -> ()'),
(
'test.one_attr i32 {"attr2" = i64}',
'"test.one_attr"() {"irdl" = i32, "attr2" = i64} : () -> ()',
),
],
)
def test_attr_name(program: str, generic_program: str):
@irdl_op_definition
class OpWithRenamedAttr(IRDLOperation):
name = "test.one_attr"

python = attr_def(Attribute, attr_name="irdl")
assembly_format = "$irdl attr-dict"

ctx = MLContext()
ctx.load_op(OpWithRenamedAttr)

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


def test_missing_property_error():
class OpWithMissingProp(IRDLOperation):
name = "test.missing_prop"
Expand Down Expand Up @@ -332,6 +357,31 @@ class OpWithProp(IRDLOperation):
check_roundtrip(program, ctx)


@pytest.mark.parametrize(
"program, generic_program",
[
("test.one_prop i32", '"test.one_prop"() <{"irdl" = i32}> : () -> ()'),
(
'test.one_prop i32 {"attr2" = i64}',
'"test.one_prop"() <{"irdl" = i32}> {"attr2" = i64} : () -> ()',
),
],
)
def test_prop_name(program: str, generic_program: str):
@irdl_op_definition
class OpWithRenamedProp(IRDLOperation):
name = "test.one_prop"

python = prop_def(Attribute, prop_name="irdl")
assembly_format = "$irdl attr-dict"

ctx = MLContext()
ctx.load_op(OpWithRenamedProp)

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


################################################################################
# Punctuations, keywords, and whitespaces #
################################################################################
Expand Down
10 changes: 8 additions & 2 deletions xdsl/irdl/declarative_assembly_format_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,15 @@ def parse_optional_variable(
else:
return ResultVariable(variable_name, idx)

attr_or_prop_by_name = {
attr_name: attr_or_prop
for attr_name, attr_or_prop in self.op_def.accessor_names.values()
}

# Check if the variable is an attribute
if variable_name in self.op_def.accessor_names:
(attr_name, attr_or_prop) = self.op_def.accessor_names[variable_name]
if variable_name in attr_or_prop_by_name:
attr_name = variable_name
attr_or_prop = attr_or_prop_by_name[attr_name]
if self.context == ParsingContext.TopLevel:
if attr_or_prop == "property":
if attr_name in self.seen_properties:
Expand Down

0 comments on commit 4d9849f

Please sign in to comment.