-
Notifications
You must be signed in to change notification settings - Fork 396
[Comb] Canonicalize power-of-two unsigned div/mod. #9177
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
Conversation
| pm.addPass(createCSEPass()); | ||
| pm.addPass(createSimpleCanonicalizerPass()); |
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.
This is for canonicalizing divu/modu before running CombToSynth.
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.
LGTM!
Is this also valid if the LHS contains any X? I remember the SV spec mentioning that if the operands contain any X, the result is all X. If you divide 'h1X0 / 16, you'd get 'h1X with the canonicalization, but since the LHS contains any X, the spec calls for 'hXX as a result. Do we view 'h1X as a refinement of 'hXX?
I also remember a discussion at a CIRCT ODM a while ago about making Comb just assume 2-valued bits, and then implement X semantics with dedicated ops around Comb. I liked that approach, because canonicalizations like the one in this PR can just not care about X.
| if (auto rhsConstantOp = adaptor.getRhs().getDefiningOp<hw::ConstantOp>()) | ||
| if (rhsConstantOp.getValue().isPowerOf2()) { | ||
| // Extract upper bits. | ||
| size_t extractAmount = rhsConstantOp.getValue().ceilLogBase2(); | ||
| size_t width = op.getType().getIntOrFloatBitWidth(); | ||
| Value upperBits = rewriter.createOrFold<comb::ExtractOp>( | ||
| op.getLoc(), adaptor.getLhs(), extractAmount, | ||
| width - extractAmount); | ||
| Value constZero = hw::ConstantOp::create(rewriter, op.getLoc(), | ||
| APInt::getZero(extractAmount)); | ||
| replaceOpWithNewOpAndCopyNamehint<comb::ConcatOp>( | ||
| rewriter, op, op.getType(), ArrayRef<Value>{constZero, upperBits}); | ||
| return success(); | ||
| } |
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 to have this in the canonicalizer! 🥳
|
Good point, I added an condition to check bin flag. Not sure how removal of bin flag ended up :) |
This commit adds canonicalization patterns for unsigned division and
modulo operations when the divisor is a power of two constant. These
operations can be efficiently lowered to bit extraction and
concatenation operations.
This is done in CombToSynth already but would be good to perform as part of canonicalization.