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 custom attr and prop name support to assembly format #2123

Merged
merged 1 commit into from
Feb 10, 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
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
Loading