Skip to content

Commit

Permalink
core: make TypedAttribute printing generic (#3490)
Browse files Browse the repository at this point in the history
This currently doesn't really do anything because `IntegerAttr` is the
only `TypedAttribute`. But after future PRs this will unify the way in
which `TypedAttribute` is printed.
  • Loading branch information
alexarice authored Nov 20, 2024
1 parent 178e3aa commit 0c871d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions xdsl/ir/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ class TypedAttribute(ParametrizedAttribute, Generic[AttributeCovT], ABC):
An attribute with a type.
"""

@staticmethod
def get_type_index() -> int: ...
@classmethod
def get_type_index(cls) -> int: ...

@staticmethod
def parse_with_type(
Expand Down
7 changes: 6 additions & 1 deletion xdsl/irdl/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ def irdl_param_attr_definition(cls: _PAttrTT) -> _PAttrTT:
if issubclass(cls, TypedAttribute):
parameter_names: tuple[str] = tuple(zip(*attr_def.parameters))[0]
type_index = parameter_names.index("type")
new_fields["get_type_index"] = lambda: type_index

@classmethod
def get_type_index(cls: Any) -> int:
return type_index

new_fields["get_type_index"] = get_type_index

return runtime_final(
dataclass(frozen=True, init=False)(
Expand Down
38 changes: 20 additions & 18 deletions xdsl/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
SpacedOpaqueSyntaxAttribute,
SSAValue,
TypeAttribute,
TypedAttribute,
)
from xdsl.traits import IsolatedFromAbove, IsTerminator
from xdsl.utils.bitwise_casts import (
Expand Down Expand Up @@ -525,6 +526,25 @@ def print_attribute(self, attribute: Attribute) -> None:
self.print_string("f128")
return

if isinstance(attribute, IntegerAttr):
# boolean shorthands
if (
isinstance(
(ty := attribute.parameters[attribute.get_type_index()]),
IntegerType,
)
and ty.width.data == 1
):
self.print_string("true" if attribute.value.data else "false")
return
# Otherwise we fall through to TypedAttribute case

if isinstance(attribute, TypedAttribute):
attribute.print_without_type(self)
self.print_string(" : ")
self.print_attribute(attribute.parameters[attribute.get_type_index()])
return

if isinstance(attribute, StringAttr):
self.print_string_literal(attribute.data)
return
Expand All @@ -541,24 +561,6 @@ def print_attribute(self, attribute: Attribute) -> None:
self.print_identifier_or_string_literal(ref.data)
return

if isinstance(attribute, IntegerAttr):
attribute = cast(AnyIntegerAttr, attribute)

# boolean shorthands
if (
isinstance((attr_type := attribute.type), IntegerType)
and attr_type.width.data == 1
):
self.print_string("false" if attribute.value.data == 0 else "true")
return

width = attribute.value
attr_type = attribute.type
assert isinstance(width, IntAttr)
self.print_string(f"{width.data} : ")
self.print_attribute(attr_type)
return

if isinstance(attribute, FloatAttr):
attr = cast(AnyFloatAttr, attribute)
self.print_float(attr)
Expand Down

0 comments on commit 0c871d9

Please sign in to comment.