Skip to content

Commit

Permalink
dialects: (builtin) fix true/false printing (#3555)
Browse files Browse the repository at this point in the history
We currently don't consistently print true and false for i1 values, this
PR fixes that in the cases I could find.
  • Loading branch information
superlopuh authored Dec 2, 2024
1 parent e9b9297 commit 92101f8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
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)

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

0 comments on commit 92101f8

Please sign in to comment.