[PATCH] D141074: Avoid converting 64-bit integers to floating point using x87 on Windows

icedrocket via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 5 10:28:22 PST 2023


icedrocket created this revision.
Herald added subscribers: pengfei, hiraditya.
Herald added a project: All.
icedrocket requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The code below currently outputs an abnormal value only on Windows 32-bit. The reason is that x87 precision control is only 56-bit by default on Windows. This revision avoids incorrect conversions by using library calls instead.

  #include <stdio.h>
  #include <stdint.h>
  
  int main() {
      int64_t n = 0b0000000000111111111111111111111111011111111111111111111111111111;
      printf("%lld, %.0f, %.0f", n, (float)n, (float)(uint64_t)n);
  
      return 0;
  }


https://reviews.llvm.org/D141074

Files:
  llvm/lib/Target/X86/X86ISelLowering.cpp


Index: llvm/lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- llvm/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -21368,6 +21368,10 @@
   if (VT == MVT::f128 || !Subtarget.hasX87())
     return SDValue();
 
+  // Windows default precision control is only 56-bit.
+  if (Subtarget.isOSWindows())
+    return SDValue();
+
   SDValue ValueToStore = Src;
   if (SrcVT == MVT::i64 && Subtarget.hasSSE2() && !Subtarget.is64Bit())
     // Bitcasting to f64 here allows us to do a single 64-bit store from
@@ -21859,6 +21863,9 @@
       (DstVT == MVT::f32 || DstVT == MVT::f64))
     return SDValue();
 
+  if (!Subtarget.hasX87())
+    return SDValue();
+
   // Make a 64-bit buffer, and use it to build an FILD.
   SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64, 8);
   int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
@@ -21880,6 +21887,11 @@
   }
 
   assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
+
+  // Windows default precision control is only 56-bit.
+  if (Subtarget.isOSWindows())
+    return SDValue();
+
   SDValue ValueToStore = Src;
   if (isScalarFPTypeInSSEReg(Op.getValueType()) && !Subtarget.is64Bit()) {
     // Bitcasting to f64 here allows us to do a single 64-bit store from


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141074.486621.patch
Type: text/x-patch
Size: 1315 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230105/89694956/attachment.bin>


More information about the llvm-commits mailing list