Skip to content

Commit

Permalink
dialects: (builtin) Fix documentation for parsing of bools, and add c…
Browse files Browse the repository at this point in the history
…onversion to Python bool (#3689)
  • Loading branch information
watermelonwolverine authored Jan 30, 2025
1 parent 6c0a6e9 commit b99334b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
11 changes: 11 additions & 0 deletions tests/dialects/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
AnyVectorType,
ArrayAttr,
BFloat16Type,
BoolAttr,
BytesAttr,
ComplexType,
DenseArrayBase,
Expand Down Expand Up @@ -183,6 +184,16 @@ def test_IntegerAttr_normalize():
IntegerAttr(256, 8)


def test_IntAttr___bool__():
assert not IntAttr(0)
assert IntAttr(1)


def test_BoolAttr___bool__():
assert not BoolAttr.from_bool(False)
assert BoolAttr.from_bool(True)


def test_IntegerType_packing():
# i1
nums_i1 = (0, 1, 0, 1)
Expand Down
4 changes: 2 additions & 2 deletions xdsl/backend/csl/print_csl.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,9 @@ def print_block(self, body: Block):
f" .{q_type}_queue = @get_{q_type}_queue({queue_id.value.data}),"
)
self.print(f" .fabric_color = {fabric_color},")
if wavelet_index_offset:
if wavelet_index_offset is not None:
self.print(f" .wavelet_index_offset = {wavelet_index_offset},")
if control:
if control is not None:
self.print(f" .control = {control},")
self.print("}});")
case csl.SetDsdBaseAddrOp(
Expand Down
8 changes: 8 additions & 0 deletions xdsl/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ def print_parameter(self, printer: Printer) -> None:
with printer.in_angle_brackets():
printer.print_string(f"{self.data}")

def __bool__(self) -> bool:
"""Returns True if value is non-zero."""
return bool(self.data)


class Signedness(Enum):
"Signedness semantics for integer"
Expand Down Expand Up @@ -713,6 +717,10 @@ def constr(
),
)

def __bool__(self) -> bool:
"""Returns True if value is non-zero."""
return bool(self.value)

@staticmethod
def iter_unpack(
type: _IntegerAttrTypeInvT, buffer: ReadableBuffer, /
Expand Down

0 comments on commit b99334b

Please sign in to comment.