Skip to content

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

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

dialect: (riscv) add folding patterns for shift ops#5656
osmanyasar05 wants to merge 6 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 78.26087% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.25%. Comparing base (9a3c91b) to head (3075de6).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
xdsl/dialects/riscv/ops.py 66.66% 0 Missing and 3 partials ⚠️
xdsl/transforms/canonicalization_patterns/riscv.py 80.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5656      +/-   ##
==========================================
- Coverage   86.25%   86.25%   -0.01%     
==========================================
  Files         403      403              
  Lines       56852    56864      +12     
  Branches     6556     6567      +11     
==========================================
+ Hits        49038    49047       +9     
  Misses       6283     6283              
- Partials     1531     1534       +3     

☔ 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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants