Skip to content

Commit

Permalink
core: (assembly-format) omit default attributes/properties from attr-…
Browse files Browse the repository at this point in the history
…dict directive (#3783)

The `attr-dict` directive currently prints attributes/properties which
are set to the default value, which is not the correct behaviour.
  • Loading branch information
alexarice authored Jan 24, 2025
1 parent 906fa5c commit 2d39e73
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
62 changes: 62 additions & 0 deletions tests/irdl/test_declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2965,6 +2965,68 @@ class DefaultConstantOp(IRDLOperation):
check_equivalence(program, generic, ctx)


@pytest.mark.parametrize(
"program, generic",
[
(
"test.default_attr_dict",
'"test.default_attr_dict"() <{prop = false}> {attr = false} : () -> ()',
),
(
"test.default_attr_dict {attr = true, prop = true}",
'"test.default_attr_dict"() <{prop = true}> {attr = true} : () -> ()',
),
],
)
def test_default_property_in_attr_dict(program: str, generic: str):
@irdl_op_definition
class DefaultAttrDictOp(IRDLOperation):
name = "test.default_attr_dict"

prop = prop_def(BoolAttr, default_value=BoolAttr.from_bool(False))

attr = attr_def(BoolAttr, default_value=BoolAttr.from_bool(False))

irdl_options = [ParsePropInAttrDict()]

assembly_format = "attr-dict"

ctx = MLContext()
ctx.load_op(DefaultAttrDictOp)

check_roundtrip(program, ctx)
check_equivalence(program, generic, ctx)


@pytest.mark.parametrize(
"program, generic",
[
(
"test.default_attr_dict",
'"test.default_attr_dict"() {attr = false} : () -> ()',
),
(
"test.default_attr_dict {attr = true}",
'"test.default_attr_dict"() {attr = true} : () -> ()',
),
],
)
def test_default_attr_in_attr_dict(program: str, generic: str):
@irdl_op_definition
class DefaultAttrDictOp(IRDLOperation):
name = "test.default_attr_dict"

attr = attr_def(BoolAttr, default_value=BoolAttr.from_bool(False))

assembly_format = "attr-dict"

ctx = MLContext()
ctx.load_op(DefaultAttrDictOp)

check_roundtrip(program, ctx)
check_equivalence(program, generic, ctx)


################################################################################
# Extractors #
################################################################################
Expand Down
32 changes: 22 additions & 10 deletions xdsl/irdl/declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,27 +438,39 @@ def parse(self, parser: Parser, state: ParsingState) -> None:

def print(self, printer: Printer, state: PrintingState, op: IRDLOperation) -> None:
if self.print_properties:
if (
not (set(op.attributes.keys()) | set(op.properties.keys()))
- self.reserved_attr_names
):
return
if any(name in op.attributes for name in op.properties):
raise ValueError(
"Cannot print attributes and properties with the same name "
"in a signle dictionary"
"in a single dictionary"
)
op_def = op.get_irdl_definition()
dictionary = op.attributes | op.properties
reserved_or_default = self.reserved_attr_names.union(
name
for name, d in (op_def.properties | op_def.attributes).items()
if d.default_value is not None
and dictionary.get(name) == d.default_value
)
if reserved_or_default.issuperset(dictionary.keys()):
return
printer.print_op_attributes(
op.attributes | op.properties,
reserved_attr_names=self.reserved_attr_names,
dictionary,
reserved_attr_names=reserved_or_default,
print_keyword=self.with_keyword,
)
else:
if not set(op.attributes.keys()) - self.reserved_attr_names:
op_def = op.get_irdl_definition()
reserved_or_default = self.reserved_attr_names.union(
name
for name, d in op_def.attributes.items()
if d.default_value is not None
and op.attributes.get(name) == d.default_value
)
if reserved_or_default.issuperset(op.attributes.keys()):
return
printer.print_op_attributes(
op.attributes,
reserved_attr_names=self.reserved_attr_names,
reserved_attr_names=reserved_or_default,
print_keyword=self.with_keyword,
)

Expand Down

0 comments on commit 2d39e73

Please sign in to comment.