dialect: (riscv) add folding patterns for shift ops#5656
dialect: (riscv) add folding patterns for shift ops#5656osmanyasar05 wants to merge 11 commits intoxdslproject:mainfrom
Conversation
|
I think there are multiple ways that we can add this function. We can either make py_operation a static method and pass both operands as arguments, or make it an instance method and only pass the register operand, not the immediate. I followed #5497 here. What do you think @superlopuh ? |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5656 +/- ##
==========================================
- Coverage 86.23% 86.23% -0.01%
==========================================
Files 403 403
Lines 57216 57209 -7
Branches 6647 6646 -1
==========================================
- Hits 49342 49335 -7
Misses 6319 6319
Partials 1555 1555 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
added new test cases to ensure correctness of srai and slli with signed numbers. they work as intended. |
superlopuh
left a comment
There was a problem hiding this comment.
This is looking great! Can you please add tests for the new ops, and probably also new tests for the ops that already had the pattern to show the difference in behaviour between the shifts that respect the sign bits and not?
I have added these tests to canonicalize.mlir. I believe these should cover what you requested? Logical and arithmetic shifting of -1. |
| %shift_left_immediate = riscv.slli %c2, 4 : (!riscv.reg) -> !riscv.reg<a0> | ||
| "test.op"(%shift_left_immediate) : (!riscv.reg<a0>) -> () | ||
|
|
||
| %shift_right_immediate = riscv.srli %shift_left_immediate, 3 : (!riscv.reg<a0>) -> !riscv.reg<a0> | ||
| "test.op"(%shift_right_immediate) : (!riscv.reg<a0>) -> () | ||
|
|
||
| // Check shifts with signed numbers | ||
| %srli_neg = riscv.srli %c_neg1, 1 : (!riscv.reg) -> !riscv.reg<a0> | ||
| "test.op"(%srli_neg) : (!riscv.reg<a0>) -> () | ||
|
|
||
| %srai_neg = riscv.srai %c_neg1, 1 : (!riscv.reg) -> !riscv.reg<a0> | ||
| "test.op"(%srai_neg) : (!riscv.reg<a0>) -> () | ||
|
|
||
| %slli_neg = riscv.slli %c_neg1, 1 : (!riscv.reg) -> !riscv.reg<a0> | ||
| "test.op"(%slli_neg) : (!riscv.reg<a0>) -> () | ||
|
|
There was a problem hiding this comment.
let's normalize the tests here? I mean it would be good to have tests for all the shift ops with the same positive and negative inputs to show what the results should be
superlopuh
left a comment
There was a problem hiding this comment.
One more idea, can you please use py_operation in the interpreter implementation? For now, we would need to construct the IntegerAttr from the int passed in, but I'd like to migrate the interpreter to use attributes instead of arbitrary Python objects so it shouldn't have overhead in the future
This PR adds constant folding patterns to shift operations Slli and Srli.