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

dialects: (builtin) fix true/false printing #3555

Merged
merged 3 commits into from
Dec 2, 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
2 changes: 1 addition & 1 deletion tests/filecheck/parser-printer/builtin_attrs.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
value1 = dense<[true, false]> : tensor<2xi1>,
sym_name = "dense_bool_attr"} : () -> ()

// CHECK: "value1" = dense<[1, 0]> : tensor<2xi1>
// CHECK: "value1" = dense<[true, false]> : tensor<2xi1>

"func.func"() ({}) {function_type = () -> (),
value1 = opaque<"test", "contents">,
Expand Down
8 changes: 4 additions & 4 deletions tests/irdl/test_declarative_assembly_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,7 @@ class DefaultOp(IRDLOperation):
),
(
"test.default prop true opt_prop false",
"test.default prop 1 opt_prop 0",
"test.default prop true opt_prop false",
'"test.default"() <{"prop" = true, "opt_prop" = false}> {"attr" = false} : () -> ()',
),
(
Expand All @@ -2807,7 +2807,7 @@ class DefaultOp(IRDLOperation):
),
(
"test.default attr true opt_attr false",
"test.default attr 1 opt_attr 0",
"test.default attr true opt_attr false",
'"test.default"() <{"prop" = false}> {"attr" = true, "opt_attr" = false} : () -> ()',
),
(
Expand Down Expand Up @@ -2859,12 +2859,12 @@ class RenamedPropOp(IRDLOperation):
),
(
"test.renamed prop1 false prop2 false",
"test.renamed prop2 0",
"test.renamed prop2 false",
'"test.renamed"() <{"test_prop1" = false, "test_prop2" = false}> : () -> ()',
),
(
"test.renamed prop1 true prop2 true",
"test.renamed prop1 1 prop2 1",
"test.renamed prop1 true prop2 true",
'"test.renamed"() <{"test_prop1" = true, "test_prop2" = true}> : () -> ()',
),
],
Expand Down
19 changes: 17 additions & 2 deletions xdsl/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ def verify_value(self, value: int):
def bitwidth(self) -> int:
return self.width.data

def print_value_without_type(self, value: int, printer: Printer):
"""
Prints the value, printing `true` or `false` if self.width == 1.
"""
if self.width.data == 1:
printer.print_string("true" if value else "false", indent=0)
else:
printer.print_string(f"{value}")


i64 = IntegerType(64)
i32 = IntegerType(32)
Expand Down Expand Up @@ -440,6 +449,12 @@ class LocationAttr(ParametrizedAttribute):
class IndexType(ParametrizedAttribute):
name = "index"

def print_value_without_type(self, value: int, printer: Printer):
"""
Prints the value.
"""
printer.print_string(f"{value}")


IndexTypeConstr = BaseAttr(IndexType)

Expand Down Expand Up @@ -513,7 +528,7 @@ def parse_with_type(
return IntegerAttr(parser.parse_integer(allow_boolean=(type == i1)), type)

def print_without_type(self, printer: Printer):
return printer.print(self.value.data)
self.type.print_value_without_type(self.value.data, printer)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The polymorphism feels unnecessary to me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Collaborator

@alexarice alexarice Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this could just be

if isinstance(self.type, IntegerAttr) and self.type.bitwidth == 1:
  # print bools 
  ...
else:
  printer.print_string(str(self.value.data))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered it and then felt like doing Python runtime polymorphism is cleaner, if only because we have types like DenseArrayBase, and DenseIntOrFPAttr that will soon have a type and then lots of data, and it seems a little simpler to have the functionality available on the type directly.


def get_type(self) -> Attribute:
return self.type
Expand Down Expand Up @@ -1896,7 +1911,7 @@ def parse_with_type(parser: AttrParser, type: Attribute) -> TypedAttribute:
@staticmethod
def _print_one_elem(val: Attribute, printer: Printer):
if isinstance(val, IntegerAttr):
printer.print_string(f"{val.value.data}")
val.print_without_type(printer)
elif isinstance(val, FloatAttr):
printer.print_float(cast(AnyFloatAttr, val))
else:
Expand Down
2 changes: 1 addition & 1 deletion xdsl/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def print_attribute(self, attribute: Attribute) -> None:
if attribute.elt_type == i1:
self.print_list(
data,
lambda x: self.print_string("true" if x == 1 else "false"),
lambda x: self.print_string("true" if x else "false"),
)
else:
self.print_list(data, lambda x: self.print_string(f"{x}"))
Expand Down
Loading