-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[llvm] Add KnownBits implementations for avgFloor and avgCeil #86445
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
[llvm] Add KnownBits implementations for avgFloor and avgCeil #86445
Conversation
@llvm/pr-subscribers-llvm-selectiondag @llvm/pr-subscribers-llvm-support Author: Nhat Nguyen (changkhothuychung) ChangesThis PR is to address the issue #84640 Full diff: https://github.com/llvm/llvm-project/pull/86445.diff 1 Files Affected:
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index d72355dab6f1d3..07c7ad0882387a 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -762,6 +762,46 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
}
+KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
+ // (C1 & C2) + (C1 ^ C2).ashr(1)
+ KnownBits andResult = LHS & RHS;
+ KnownBits xorResult = LHS ^ RHS;
+ xorResult.Zero.ashrInPlace(1);
+ xorResult.One.ashrInPlace(1);
+ return computeForSatAddSub(/*Add*/ true, /*Signed*/ true, andResult,
+ xorResult);
+}
+
+KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
+ // (C1 & C2) + (C1 ^ C2).lshr(1)
+ KnownBits andResult = LHS & RHS;
+ KnownBits xorResult = LHS ^ RHS;
+ xorResult.Zero.lshrInPlace(1);
+ xorResult.One.lshrInPlace(1);
+ return computeForSatAddSub(/*Add*/ true, /*Signed*/ false, andResult,
+ xorResult);
+}
+
+KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
+ // (C1 | C2) - (C1 ^ C2).ashr(1)
+ KnownBits andResult = LHS & RHS;
+ KnownBits xorResult = LHS ^ RHS;
+ xorResult.Zero.ashrInPlace(1);
+ xorResult.One.ashrInPlace(1);
+ return computeForSatAddSub(/*Add*/ false, /*Signed*/ true, andResult,
+ xorResult);
+}
+
+KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
+ // (C1 | C2) - (C1 ^ C2).lshr(1)
+ KnownBits andResult = LHS & RHS;
+ KnownBits xorResult = LHS ^ RHS;
+ xorResult.Zero.lshrInPlace(1);
+ xorResult.One.lshrInPlace(1);
+ return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, andResult,
+ xorResult);
+}
+
KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
bool NoUndefSelfMultiply) {
unsigned BitWidth = LHS.getBitWidth();
|
✅ With the latest revision this PR passed the Python code formatter. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
@RKSimon RkSim llvm-project/llvm/unittests/Support/KnownBitsTest.cpp Lines 834 to 848 in ffe4181
|
@RKSimon Rkso |
Your unit tests should look something like:
The first argument is a function pointer to the knownbits function we are testing.
The third is optional. If your implementation is complete (i.e proves all knowable bits) you can |
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.
#86754 has now landed so please can you update the SelectionDAG::computeKnownBits AVG cases to use KnowBits implementations
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.
Please address the KnownBitsTests failures
llvm/lib/Support/KnownBits.cpp
Outdated
|
||
KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) { | ||
// (C1 | C2) - (C1 ^ C2).ashr(1) | ||
KnownBits andResult = LHS | RHS; |
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.
Change name to OrResult. Variable names should be capitalised throughout.
/// Compute knownbits resulting from APIntOps::avgFloorU | ||
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS); | ||
|
||
/// Compute knownbits resulting from APIntOps::avgCelS |
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.
Typo "Cel" here and below
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.
avgCel -> avgCeil
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 looks good to me now except for the remaining cosmetic issues.
This LGTM, wait one 1 more approval to push. |
llvm/lib/Support/KnownBits.cpp
Outdated
@@ -475,7 +475,7 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS, | |||
Known.Zero.setAllBits(); | |||
Known.One.setAllBits(); | |||
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount; | |||
++ShiftAmt) { | |||
++ShiftAmt) { |
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 looks like spurious reformatting.
@jayfoad f Link to failed CI: https://buildkite.com/llvm-project/github-pull-requests/builds/65024#018f86e0-35f7-4c66-bb68-14cd36220d70 |
Oh, I thought this implementation was supposed to be optimal, but maybe I got that wrong. Patch LGTM anyway. |
Having looked into it, the unsigned functions are optimal. The signed ones are not because LHS/RHS.sext(BitWidth + 1) does not preserve the knowledge that the top two bits are the same. We can look into improving that later. For now, it's fine to commit the patch with optimality checking removed. |
I am assuming I need to put back the flag to disable optimality check ? |
You already do. Its the |
Yes (at least for the signed functions CeilS and FloorS) because you can't commit with failing tests. |
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
I made a push to put it back after I made that comment. |
Thanks everyon! looks like I got 2 approves already, I will merge this PR shortly if there is no other concern. |
@RKSimon RK Should we revert the changes? |
wait until the current build has finished to see if it was an intermittent fail |
This PR is to address the issue #84640