Skip to content

Commit edd8b29

Browse files
authored
[Float2Int] Make sure the CFP can be represented in the integer type (#167699)
When `convertToInteger` fails, the integer result is undefined. In this case, we cannot use it in the subsequent steps. Close #167627.
1 parent 3cfe6aa commit edd8b29

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

llvm/lib/Transforms/Scalar/Float2Int.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,14 @@ std::optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
237237
// OK, it's representable. Now get it.
238238
APSInt Int(MaxIntegerBW+1, false);
239239
bool Exact;
240-
CF->getValueAPF().convertToInteger(Int,
241-
APFloat::rmNearestTiesToEven,
242-
&Exact);
243-
OpRanges.push_back(ConstantRange(Int));
240+
APFloat::opStatus Status = CF->getValueAPF().convertToInteger(
241+
Int, APFloat::rmNearestTiesToEven, &Exact);
242+
// Although the round above is loseless, we still need to check if the
243+
// floating-point value can be represented in the integer type.
244+
if (Status == APFloat::opOK || Status == APFloat::opInexact)
245+
OpRanges.push_back(ConstantRange(Int));
246+
else
247+
return badRange();
244248
} else {
245249
llvm_unreachable("Should have already marked this as badRange!");
246250
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
2+
; RUN: opt -S -passes=float2int < %s | FileCheck %s
3+
4+
; Make sure that we don't demote constant floating-point values when
5+
; it cannot be represented by target integer type.
6+
7+
define i1 @pr167627() {
8+
; CHECK-LABEL: define i1 @pr167627() {
9+
; CHECK-NEXT: [[ENTRY:.*:]]
10+
; CHECK-NEXT: [[FADD:%.*]] = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
11+
; CHECK-NEXT: [[CMP:%.*]] = fcmp one float [[FADD]], 0.000000e+00
12+
; CHECK-NEXT: ret i1 [[CMP]]
13+
;
14+
entry:
15+
%fadd = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
16+
%cmp = fcmp one float %fadd, 0.000000e+00
17+
ret i1 %cmp
18+
}

0 commit comments

Comments
 (0)