[Float2Int] Make sure the CFP can be represented in the integer type#167699
Merged
[Float2Int] Make sure the CFP can be represented in the integer type#167699
Conversation
Member
|
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesWhen Full diff: https://github.com/llvm/llvm-project/pull/167699.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/Float2Int.cpp b/llvm/lib/Transforms/Scalar/Float2Int.cpp
index 14686ce8c2ab6..37822cf05f144 100644
--- a/llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ b/llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -237,10 +237,14 @@ std::optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
// OK, it's representable. Now get it.
APSInt Int(MaxIntegerBW+1, false);
bool Exact;
- CF->getValueAPF().convertToInteger(Int,
- APFloat::rmNearestTiesToEven,
- &Exact);
- OpRanges.push_back(ConstantRange(Int));
+ APFloat::opStatus Status = CF->getValueAPF().convertToInteger(
+ Int, APFloat::rmNearestTiesToEven, &Exact);
+ // Although the round above is loseless, we still need to check if the
+ // floating-point value can be represented in the integer type.
+ if (Status == APFloat::opOK || Status == APFloat::opInexact)
+ OpRanges.push_back(ConstantRange(Int));
+ else
+ return badRange();
} else {
llvm_unreachable("Should have already marked this as badRange!");
}
diff --git a/llvm/test/Transforms/Float2Int/pr167627.ll b/llvm/test/Transforms/Float2Int/pr167627.ll
new file mode 100644
index 0000000000000..a170c21af7a2a
--- /dev/null
+++ b/llvm/test/Transforms/Float2Int/pr167627.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=float2int < %s | FileCheck %s
+
+; Make sure that we don't demote constant floating-point values when
+; it cannot be represented by target integer type.
+
+define i1 @pr167627() {
+; CHECK-LABEL: define i1 @pr167627() {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[FADD:%.*]] = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
+; CHECK-NEXT: [[CMP:%.*]] = fcmp one float [[FADD]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %fadd = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
+ %cmp = fcmp one float %fadd, 0.000000e+00
+ ret i1 %cmp
+}
|
antoniofrighetto
approved these changes
Nov 13, 2025
Contributor
antoniofrighetto
left a comment
There was a problem hiding this comment.
Does make sense, thanks.
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/141/builds/12965 Here is the relevant piece of the build log for the reference |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
convertToIntegerfails, the integer result is undefined. In this case, we cannot use it in the subsequent steps.Close #167627.