Skip to content

Commit e34bd1e

Browse files
committed
[APFloat] prevent NaN morphing into Inf on conversion (PR43907)
We shift the significand right on a truncation, but that needs to be made NaN-safe: always set at least 1 bit in the significand. https://llvm.org/PR43907 See D88238 for the likely follow-up (but needs some plumbing fixes before it can proceed). Differential Revision: https://reviews.llvm.org/D87835
1 parent 03f22b0 commit e34bd1e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

llvm/lib/Support/APFloat.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,21 @@ IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics,
22422242
if (!X86SpecialNan && semantics == &semX87DoubleExtended)
22432243
APInt::tcSetBit(significandParts(), semantics->precision - 1);
22442244

2245+
// If we are truncating NaN, it is possible that we shifted out all of the
2246+
// set bits in a signalling NaN payload. But NaN must remain NaN, so some
2247+
// bit in the significand must be set (otherwise it is Inf).
2248+
// This can only happen with sNaN. Set the 1st bit after the quiet bit,
2249+
// so that we still have an sNaN.
2250+
// FIXME: Set quiet and return opInvalidOp (on convert of any sNaN).
2251+
// But this requires fixing LLVM to parse 32-bit hex FP or ignoring
2252+
// conversions while parsing IR.
2253+
if (APInt::tcIsZero(significandParts(), newPartCount)) {
2254+
assert(shift < 0 && "Should not lose NaN payload on extend");
2255+
assert(semantics->precision >= 3 && "Unexpectedly narrow significand");
2256+
assert(*losesInfo && "Missing payload should have set lost info");
2257+
APInt::tcSetBit(significandParts(), semantics->precision - 3);
2258+
}
2259+
22452260
// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
22462261
// does not give you back the same bits. This is dubious, and we
22472262
// don't currently do it. You're really supposed to get

llvm/test/Transforms/InstSimplify/ConstProp/cast.ll

+9-3
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,25 @@ define float @overflow_sitofp() {
3939
ret float %i
4040
}
4141

42-
; https://llvm.org/PR43907
42+
; https://llvm.org/PR43907 - make sure that NaN doesn't morph into Inf.
43+
; SNaN remains SNaN.
4344

4445
define float @nan_f64_trunc() {
4546
; CHECK-LABEL: @nan_f64_trunc(
46-
; CHECK-NEXT: ret float 0x7FF0000000000000
47+
; CHECK-NEXT: ret float 0x7FF4000000000000
4748
;
4849
%f = fptrunc double 0x7FF0000000000001 to float
4950
ret float %f
5051
}
5152

53+
; Verify again with a vector and different destination type.
54+
; SNaN remains SNaN (first two elements).
55+
; QNaN remains QNaN (third element).
56+
; Lower 42 bits of NaN source payload are lost.
57+
5258
define <3 x half> @nan_v3f64_trunc() {
5359
; CHECK-LABEL: @nan_v3f64_trunc(
54-
; CHECK-NEXT: ret <3 x half> <half 0xH7C00, half 0xH7C00, half 0xH7E00>
60+
; CHECK-NEXT: ret <3 x half> <half 0xH7D00, half 0xH7D00, half 0xH7E00>
5561
;
5662
%f = fptrunc <3 x double> <double 0x7FF0020000000000, double 0x7FF003FFFFFFFFFF, double 0x7FF8000000000001> to <3 x half>
5763
ret <3 x half> %f

llvm/unittests/ADT/APFloatTest.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1841,14 +1841,15 @@ TEST(APFloatTest, convert) {
18411841
EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
18421842
EXPECT_FALSE(losesInfo);
18431843

1844-
// FIXME: This is wrong - NaN becomes Inf.
1844+
// The payload is lost in truncation, but we must retain NaN, so we set the bit after the quiet bit.
18451845
APInt payload(52, 1);
18461846
test = APFloat::getSNaN(APFloat::IEEEdouble(), false, &payload);
18471847
status = test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1848-
EXPECT_EQ(0x7f800000, test.bitcastToAPInt());
1848+
EXPECT_EQ(0x7fa00000, test.bitcastToAPInt());
18491849
EXPECT_TRUE(losesInfo);
18501850
EXPECT_EQ(status, APFloat::opOK);
18511851

1852+
// The payload is lost in truncation. QNaN remains QNaN.
18521853
test = APFloat::getQNaN(APFloat::IEEEdouble(), false, &payload);
18531854
status = test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
18541855
EXPECT_EQ(0x7fc00000, test.bitcastToAPInt());

0 commit comments

Comments
 (0)