Skip to content
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

[Float2Int] Fix pessimization in the MinBW calculation. #86051

Merged
merged 2 commits into from
Mar 21, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Mar 21, 2024

The MinBW was being calculated using the significant bits of the upper and lower bounds. The upper bound is 1 past the last value in the range so I don't think it should be included. Instead get the min and max signed values from the range and use the significant bits of those.

I'm still not sure if the +1 is needed after the std::max.

I haven't investigated if its possible to add a test for this. We only look at whether the MinBW is > 32 right now. So the ranges need to be carefully crafted to see a difference.

CC: @AtariDreams

The MinBW was being calculated using the significant bits of the
upper and lower bounds. The upper bound is 1 past the last value in
the range so I don't think it should be included. Instead get the
min and max signed values from the range and use the significant
bits of those.

I'm still not sure if the +1 is needed after the std::max.

I haven't investigated if its possible to add a test for this. We
only look at whether the MinBW is > 32 right now. So the ranges
need to be carefully crafted to see a difference.
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 21, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Craig Topper (topperc)

Changes

The MinBW was being calculated using the significant bits of the upper and lower bounds. The upper bound is 1 past the last value in the range so I don't think it should be included. Instead get the min and max signed values from the range and use the significant bits of those.

I'm still not sure if the +1 is needed after the std::max.

I haven't investigated if its possible to add a test for this. We only look at whether the MinBW is > 32 right now. So the ranges need to be carefully crafted to see a difference.

CC: @AtariDreams


Full diff: https://github.com/llvm/llvm-project/pull/86051.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/Float2Int.cpp (+2-2)
diff --git a/llvm/lib/Transforms/Scalar/Float2Int.cpp b/llvm/lib/Transforms/Scalar/Float2Int.cpp
index ccca8bcc1a56ac..47042d96b46571 100644
--- a/llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ b/llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -359,8 +359,8 @@ bool Float2IntPass::validateAndTransform() {
 
     // The number of bits required is the maximum of the upper and
     // lower limits, plus one so it can be signed.
-    unsigned MinBW = std::max(R.getLower().getSignificantBits(),
-                              R.getUpper().getSignificantBits()) +
+    unsigned MinBW = std::max(R.getSignedMin().getSignificantBits(),
+                              R.getSignedMax().getSignificantBits()) +
                      1;
     LLVM_DEBUG(dbgs() << "F2I: MinBitwidth=" << MinBW << ", R: " << R << "\n");
 

unsigned MinBW = std::max(R.getLower().getSignificantBits(),
R.getUpper().getSignificantBits()) +
unsigned MinBW = std::max(R.getSignedMin().getSignificantBits(),
R.getSignedMax().getSignificantBits()) +
Copy link
Contributor

Choose a reason for hiding this comment

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

Use R.getMinSignedBits() instead?

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM -- but should probably only land after your ConstantRange fix. I think they might be partially canceling each other out right now...

@topperc topperc merged commit 796efa8 into llvm:main Mar 21, 2024
3 of 4 checks passed
@topperc topperc deleted the pr/minbw branch March 21, 2024 18:09
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
The MinBW was being calculated using the significant bits of the upper
and lower bounds. The upper bound is 1 past the last value in the range
so I don't think it should be included. Instead use ConstantRange::getMinSignedBits.

I'm still not sure if the +1 is needed after the getMinSignedBits call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants