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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions tests/dialects/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ def test_IntegerAttr_normalize():
IntegerAttr(256, 8)


def test_IntegerAttr_to_bool():
assert not IntegerAttr.from_bool(False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert not IntegerAttr.from_bool(False)
assert not BoolAttr.from_bool(False)

Super nit and probably doesn't really matter

assert IntegerAttr.from_bool(True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert IntegerAttr.from_bool(True)
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 @@ -294,6 +294,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)

Comment on lines +297 to +300
Copy link
Member

Choose a reason for hiding this comment

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

would be good to add unit tests for this for good measure


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

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


AnyIntegerAttr: TypeAlias = IntegerAttr[IntegerType | IndexType]
AnyIntegerAttrConstr: BaseAttr[AnyIntegerAttr] = BaseAttr(IntegerAttr)
Expand Down
2 changes: 1 addition & 1 deletion xdsl/parser/attribute_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ def _parse_typed_integer(
"""
Parse an (possible negative) integer. The integer can
either be decimal or hexadecimal.
Optionally allow parsing of 'true' or 'false' into 1 and 0.
Optionally allow parsing of 'true' or 'false' into -1 and 0.
"""

pos = self.pos
Expand Down
8 changes: 4 additions & 4 deletions xdsl/parser/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def parse_optional_integer(
"""
Parse an (possible negative) integer. The integer can either be
decimal or hexadecimal.
Optionally allow parsing of 'true' or 'false' into 1 and 0.
Optionally allow parsing of 'true' or 'false' into -1 and 0.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not agree with the implementation below

Copy link
Member

Choose a reason for hiding this comment

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

It does though

Copy link
Member

Choose a reason for hiding this comment

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

Ah actually not here

Copy link
Member

Choose a reason for hiding this comment

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

You're right, all of the functions with the changed documentation here return the 1/0 values, it's only when creating the attribute that the value is different. This should be reflected in documentation for the attribute, not the parser.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I did not put a comment on the doc change for the attribute

"""
# Parse true and false if needed
if allow_boolean:
Expand Down Expand Up @@ -78,7 +78,7 @@ def parse_integer(
"""
Parse an (possible negative) integer. The integer can
either be decimal or hexadecimal.
Optionally allow parsing of 'true' or 'false' into 1 and 0.
Optionally allow parsing of 'true' or 'false' into -1 and 0.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

"""

return self.expect(
Expand Down Expand Up @@ -121,7 +121,7 @@ def parse_optional_number(
) -> int | float | None:
"""
Parse a (possibly negative) integer or float literal, if present.
Can optionally parse 'true' or 'false' into 1 and 0.
Can optionally parse 'true' or 'false' into -1 and 0.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

"""

is_negative = self._parse_optional_token(MLIRTokenKind.MINUS) is not None
Expand Down Expand Up @@ -149,7 +149,7 @@ def parse_number(
) -> int | float:
"""
Parse a (possibly negative) integer or float literal.
Can optionally parse 'true' or 'false' into 1 and 0.
Can optionally parse 'true' or 'false' into -1 and 0.
Copy link
Collaborator

Choose a reason for hiding this comment

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

and here

"""
return self.expect(
lambda: self.parse_optional_number(allow_boolean=allow_boolean),
Expand Down
Loading