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 documentation for parsing of bools, and add conversion to Python bool #3689

Merged
merged 8 commits into from
Jan 30, 2025
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)

watermelonwolverine marked this conversation as resolved.
Show resolved Hide resolved

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