[PATCH] D71373: [ConstantFolding] Disable constant folding for bitcasts to x86_fp80

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 12:09:27 PST 2019


craig.topper created this revision.
craig.topper added reviewers: scanon, efriedma, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

80-bit floating point  does not have an implicit hidden bit like single and double precision. If this bit is not set we can end up with various weird encodings like pseudo-denormals, pseudo-infinities, and unnormals. The APFloat conversion from APInt turns all of these things into a NAN and loses the original exponent from incoming bit representation. This means if the APFloat gets turned back into an APInt it might not be exactly the original value.

This patch disables the constant folding to avoid hitting this condition. We could maybe be smarter and look at the value we're going to convert and make sure its safe, but that would likely require a new APFloat interface to find that out.

Fixes PR44188. I can put together a test case based on that PR, but wanted to get an opinion on whether this patch was the right thing to do.


https://reviews.llvm.org/D71373

Files:
  llvm/lib/IR/ConstantFold.cpp
  llvm/test/Transforms/InstCombine/X86/2009-03-23-i80-fp80.ll


Index: llvm/test/Transforms/InstCombine/X86/2009-03-23-i80-fp80.ll
===================================================================
--- llvm/test/Transforms/InstCombine/X86/2009-03-23-i80-fp80.ll
+++ llvm/test/Transforms/InstCombine/X86/2009-03-23-i80-fp80.ll
@@ -13,7 +13,7 @@
 
 define x86_fp80 @to() {
 ; CHECK-LABEL: @to(
-; CHECK-NEXT:    ret x86_fp80 0xK40018000000000000000
+; CHECK-NEXT:    ret x86_fp80 bitcast (i80 302259125019767858003968 to x86_fp80)
 ;
   %tmp = bitcast i80 302259125019767858003968 to x86_fp80
   ret x86_fp80 %tmp
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -169,7 +169,12 @@
       return V;
 
     // See note below regarding the PPC_FP128 restriction.
-    if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
+    // Also disable for x86_fp80 to avoid turning unnormals, a pseudo-denormals
+    // or pseudo-infinities into a nan which we can't convert back to the
+    // original value if it gets casted back to integer.
+    // FIXME: Can we preserve this information in APFloat?
+    if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty() &&
+        !DestTy->isX86_FP80Ty())
       return ConstantFP::get(DestTy->getContext(),
                              APFloat(DestTy->getFltSemantics(),
                                      CI->getValue()));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71373.233434.patch
Type: text/x-patch
Size: 1434 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191211/547f4909/attachment.bin>


More information about the llvm-commits mailing list