-
Notifications
You must be signed in to change notification settings - Fork 85
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) normalize signless values on init #3554
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
xdsl/dialects/builtin.py
Outdated
if any(signed_ub <= attr.data for attr in attr_list): | ||
attr_list = tuple( | ||
IntAttr(data_type.normalize_value(attr.data)) for attr in attr_list | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any harm in just doing this every time? Otherwise I would think an additional check for too negative numbers is also required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an extra filecheck for this case is maybe also needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by doing it every time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omit this check: if any(signed_ub <= attr.data for attr in attr_list):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, isn't normalize_value
doing the same checks anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally dislike the idea of constructors being written in a way that doesn't allow the user to pass in correct data that is not copied/transformed in some way. I'll rewrite this part to conditionally normalize but return the original IntAttr if there's nothing to change.
@@ -649,7 +649,7 @@ csl.func @builtins() { | |||
// CHECK-NEXT: return; | |||
// CHECK-NEXT: } | |||
// CHECK-NEXT: comptime { | |||
// CHECK-NEXT: @bind_control_task(control_task, @get_control_task_id(42)); | |||
// CHECK-NEXT: @bind_control_task(control_task, @get_control_task_id(-22)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@n-io, will this still work? Should the values here be always printed as positive integers by the csl printer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure about this change? It should prob not print negative values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I'll attempt to fix the CSL printer then, as the signless before this change may already be in the negative range.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3554 +/- ##
=======================================
Coverage 90.40% 90.40%
=======================================
Files 469 469
Lines 58907 58941 +34
Branches 5605 5611 +6
=======================================
+ Hits 53252 53287 +35
Misses 4213 4213
+ Partials 1442 1441 -1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this likely subtly changes a number of existing passes, which compare integers by grabbing the data out of an IntegerAttr
. I've caught one in the cf
canonicalization patterns but I'm not what can be done to catch this in general.
tests/dialects/test_builtin.py
Outdated
@@ -58,6 +60,51 @@ def test_IntegerType_size(): | |||
assert IntegerType(64).size == 8 | |||
|
|||
|
|||
def test_IntegerType_normalize(): | |||
s8 = IntegerType(8, Signedness.SIGNED) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extreme nit but I believe si
is usually used in mlir as a prefix for signed integers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh this is great, I couldn't remember it, and it's not in the IntegerAttr doc, of course
tests/dialects/test_builtin.py
Outdated
|
||
assert s8.normalize_value(-1) == -1 | ||
assert s8.normalize_value(1) == 1 | ||
assert s8.normalize_value(255) == 255 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand the semantics here, do we not change values that fall outside the range?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, and your comment means that I should have documented this better: the idea is that we want to first preserve the behaviour for invalid integers, to raise a VerifyException with the invalid value. If the valid value is ambiguous due to signlessness, only then normalize to the signed range. I'll add this somewhere.
- add more explanation to doc comment
xdsl/dialects/builtin.py
Outdated
return value | ||
|
||
signed_ub = signed_upper_bound(self.bitwidth) | ||
unsigned_ub = signed_ub << 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned_ub = signed_ub << 1 | |
unsigned_ub = unsigned_upper_bound(self.bitwidth) |
Why not use this? There will be complications with i0
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, this was a micro-optimisation, but I guess not really very important
rewriter.replace_matched_op(cf.BranchOp(op.then_block, *op.then_arguments)) | ||
elif cond == 0: | ||
else: | ||
rewriter.replace_matched_op(cf.BranchOp(op.else_block, *op.else_arguments)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't work out how to attach the comment in the correct place but line 374 of this file will now be incorrect
xdsl/dialects/builtin.py
Outdated
if any(signed_ub <= attr.data for attr in attr_list): | ||
attr_list = tuple( | ||
IntAttr(data_type.normalize_value(attr.data)) for attr in attr_list | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, isn't normalize_value
doing the same checks anyway?
9261fe6
to
6609edb
Compare
I'm converting to draft as this seems like quite large a change, I'll split this up further by the seemingly unrelated things that are failing. |
035fc8a
to
061e87c
Compare
9de5b32
to
f2d97a7
Compare
f2d97a7
to
bbda01f
Compare
Follows MLIR behaviour.
Follows MLIR behaviour.