[llvm] r218567 - Fix llvm::huge_valf multiple initializations with Visual C++.

Yaron Keren yaron.keren at gmail.com
Sat Sep 27 07:41:30 PDT 2014


Author: yrnkrn
Date: Sat Sep 27 09:41:29 2014
New Revision: 218567

URL: http://llvm.org/viewvc/llvm-project?rev=218567&view=rev
Log:
Fix llvm::huge_valf multiple initializations with Visual C++.

llvm::huge_valf is defined in a header file, so it is initialized
multiple times in every compiled unit upon program startup.

With non-VC compilers huge_valf is set to a HUGE_VALF which the
compiler can probably optimize out.

With VC numeric_limits<float>::infinity() does not return a number
but a runtime structure member which therotically may change 
between calls so the compiler does not optimize out the 
initialization and it happens many times. It can be easily seen by 
placing a breakpoint on the initialization line.

This patch moves llvm::huge_valf initialization to a source file
instead of the header.


Added:
    llvm/trunk/lib/Support/MathExtras.cpp
Modified:
    llvm/trunk/include/llvm/Support/MathExtras.h
    llvm/trunk/lib/Support/CMakeLists.txt

Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=218567&r1=218566&r2=218567&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Sat Sep 27 09:41:29 2014
@@ -22,7 +22,6 @@
 
 #ifdef _MSC_VER
 #include <intrin.h>
-#include <limits>
 #endif
 
 namespace llvm {
@@ -639,13 +638,7 @@ inline int64_t SignExtend64(uint64_t X,
   return int64_t(X << (64 - B)) >> (64 - B);
 }
 
-#if defined(_MSC_VER)
-  // Visual Studio defines the HUGE_VAL class of macros using purposeful
-  // constant arithmetic overflow, which it then warns on when encountered.
-  const float huge_valf = std::numeric_limits<float>::infinity();
-#else
-  const float huge_valf = HUGE_VALF;
-#endif
+extern const float huge_valf;
 } // End llvm namespace
 
 #endif

Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=218567&r1=218566&r2=218567&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Sat Sep 27 09:41:29 2014
@@ -36,6 +36,7 @@ add_llvm_library(LLVMSupport
   Locale.cpp
   LockFileManager.cpp
   ManagedStatic.cpp
+  MathExtras.cpp
   MemoryBuffer.cpp
   MemoryObject.cpp
   MD5.cpp

Added: llvm/trunk/lib/Support/MathExtras.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MathExtras.cpp?rev=218567&view=auto
==============================================================================
--- llvm/trunk/lib/Support/MathExtras.cpp (added)
+++ llvm/trunk/lib/Support/MathExtras.cpp Sat Sep 27 09:41:29 2014
@@ -0,0 +1,32 @@
+//===-- MathExtras.cpp - Implement the MathExtras header --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the MathExtras.h header
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/MathExtras.h"
+
+#ifdef _MSC_VER
+#include <limits>
+#else
+#include <math.h>
+#endif
+
+namespace llvm {
+
+#if defined(_MSC_VER)
+  // Visual Studio defines the HUGE_VAL class of macros using purposeful
+  // constant arithmetic overflow, which it then warns on when encountered.
+  const float huge_valf = std::numeric_limits<float>::infinity();
+#else
+  const float huge_valf = HUGE_VALF;
+#endif
+
+}





More information about the llvm-commits mailing list