diff --git a/tests/test_declarative_assembly_format.py b/tests/test_declarative_assembly_format.py index 8f28661425..bf0cdbc83d 100644 --- a/tests/test_declarative_assembly_format.py +++ b/tests/test_declarative_assembly_format.py @@ -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" @@ -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 # ################################################################################ diff --git a/xdsl/irdl/declarative_assembly_format_parser.py b/xdsl/irdl/declarative_assembly_format_parser.py index a4ff5872a3..e6cf6163a1 100644 --- a/xdsl/irdl/declarative_assembly_format_parser.py +++ b/xdsl/irdl/declarative_assembly_format_parser.py @@ -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: