Skip to content

dialect: (riscv) add folding patterns for shift ops#5656

Open
osmanyasar05 wants to merge 11 commits intoxdslproject:mainfrom
osmanyasar05:rv/py_func
Open

dialect: (riscv) add folding patterns for shift ops#5656
osmanyasar05 wants to merge 11 commits intoxdslproject:mainfrom
osmanyasar05:rv/py_func

Conversation

@osmanyasar05
Copy link
Contributor

This PR adds constant folding patterns to shift operations Slli and Srli.

@osmanyasar05
Copy link
Contributor Author

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
Copy link

codecov bot commented Feb 12, 2026

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.23%. Comparing base (22285d6) to head (ee82909).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
xdsl/transforms/canonicalization_patterns/riscv.py 87.50% 0 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@osmanyasar05
Copy link
Contributor Author

osmanyasar05 commented Feb 12, 2026

added new test cases to ensure correctness of srai and slli with signed numbers. they work as intended.

(srai -1, 1) = -1
(srli -1,1) = 2147483647

Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

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?

@osmanyasar05
Copy link
Contributor Author

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?

 %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>) -> ()

I have added these tests to canonicalize.mlir. I believe these should cover what you requested? Logical and arithmetic shifting of -1.

Comment on lines 102 to +117
%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>) -> ()

Copy link
Member

Choose a reason for hiding this comment

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

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 superlopuh added the dialects Changes on the dialects label Feb 18, 2026
Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dialects Changes on the dialects

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants