Skip to content

Commit 6bd959c

Browse files
committed
Enable logf128 constant folding for hosts with 128bit long double
This is a reland of (llvm#96287). This patch attempts to reduce clang's compile time by removing #includes of float128.h and inlining convertToQuad functions instead.
1 parent 99a10f1 commit 6bd959c

File tree

8 files changed

+43
-80
lines changed

8 files changed

+43
-80
lines changed

llvm/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,6 @@ set(LLVM_USE_STATIC_ZSTD FALSE CACHE BOOL "Use static version of zstd. Can be TR
560560

561561
set(LLVM_ENABLE_CURL "OFF" CACHE STRING "Use libcurl for the HTTP client if available. Can be ON, OFF, or FORCE_ON")
562562

563-
set(LLVM_HAS_LOGF128 "OFF" CACHE STRING "Use logf128 to constant fold fp128 logarithm calls. Can be ON, OFF, or FORCE_ON")
564-
565563
set(LLVM_ENABLE_HTTPLIB "OFF" CACHE STRING "Use cpp-httplib HTTP server library if available. Can be ON, OFF, or FORCE_ON")
566564

567565
set(LLVM_Z3_INSTALL_DIR "" CACHE STRING "Install directory of the Z3 solver.")

llvm/cmake/config-ix.cmake

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,6 @@ else()
247247
set(HAVE_LIBEDIT 0)
248248
endif()
249249

250-
if(LLVM_HAS_LOGF128)
251-
include(CheckCXXSymbolExists)
252-
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
253-
254-
if(LLVM_HAS_LOGF128 STREQUAL FORCE_ON AND NOT HAS_LOGF128)
255-
message(FATAL_ERROR "Failed to configure logf128")
256-
endif()
257-
258-
set(LLVM_HAS_LOGF128 "${HAS_LOGF128}")
259-
endif()
260-
261250
# function checks
262251
check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
263252
find_package(Backtrace)
@@ -271,6 +260,13 @@ if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
271260
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
272261
endif()
273262

263+
check_cxx_symbol_exists(logf128 cmath HAS_LOGF128)
264+
check_symbol_exists(__powerpc64le__ "" __PPC64LE)
265+
if(HAS_LOGF128 AND NOT __PPC64LE)
266+
set(LLVM_HAS_LOGF128 On)
267+
add_compile_definitions(HAS_LOGF128)
268+
endif()
269+
274270
# Determine whether we can register EH tables.
275271
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
276272
check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)

llvm/include/llvm/ADT/APFloat.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/FloatingPointMode.h"
2121
#include "llvm/Support/ErrorHandling.h"
22-
#include "llvm/Support/float128.h"
2322
#include <memory>
2423

2524
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
@@ -378,9 +377,6 @@ class IEEEFloat final : public APFloatBase {
378377
Expected<opStatus> convertFromString(StringRef, roundingMode);
379378
APInt bitcastToAPInt() const;
380379
double convertToDouble() const;
381-
#ifdef HAS_IEE754_FLOAT128
382-
float128 convertToQuad() const;
383-
#endif
384380
float convertToFloat() const;
385381

386382
/// @}
@@ -1274,14 +1270,9 @@ class APFloat : public APFloatBase {
12741270
/// shorter semantics, like IEEEsingle and others.
12751271
double convertToDouble() const;
12761272

1277-
/// Converts this APFloat to host float value.
1278-
///
1279-
/// \pre The APFloat must be built using semantics, that can be represented by
1280-
/// the host float type without loss of precision. It can be IEEEquad and
1281-
/// shorter semantics, like IEEEdouble and others.
1282-
#ifdef HAS_IEE754_FLOAT128
1283-
float128 convertToQuad() const;
1284-
#endif
1273+
/// Return true if this APFloat has quadruple precision floating point
1274+
/// semantics
1275+
bool isValidIEEEQuad() const;
12851276

12861277
/// Converts this APFloat to host float value.
12871278
///

llvm/include/llvm/ADT/APInt.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include "llvm/Support/Compiler.h"
1919
#include "llvm/Support/MathExtras.h"
20-
#include "llvm/Support/float128.h"
2120
#include <cassert>
2221
#include <climits>
2322
#include <cstring>
@@ -1679,13 +1678,6 @@ class [[nodiscard]] APInt {
16791678
/// any bit width. Exactly 64 bits will be translated.
16801679
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
16811680

1682-
#ifdef HAS_IEE754_FLOAT128
1683-
float128 bitsToQuad() const {
1684-
__uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
1685-
return llvm::bit_cast<float128>(ul);
1686-
}
1687-
#endif
1688-
16891681
/// Converts APInt bits to a float
16901682
///
16911683
/// The conversion does not do a translation from integer to float, it just
@@ -1883,6 +1875,12 @@ class [[nodiscard]] APInt {
18831875
/// Returns whether this instance allocated memory.
18841876
bool needsCleanup() const { return !isSingleWord(); }
18851877

1878+
/// Get the word corresponding to a bit position
1879+
/// \returns the corresponding word for the specified bit position.
1880+
uint64_t getWord(unsigned bitPosition) const {
1881+
return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
1882+
}
1883+
18861884
private:
18871885
/// This union is used to store the integer value. When the
18881886
/// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
@@ -1948,12 +1946,6 @@ class [[nodiscard]] APInt {
19481946
return *this;
19491947
}
19501948

1951-
/// Get the word corresponding to a bit position
1952-
/// \returns the corresponding word for the specified bit position.
1953-
uint64_t getWord(unsigned bitPosition) const {
1954-
return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
1955-
}
1956-
19571949
/// Utility method to change the bit width of this APInt to new bit width,
19581950
/// allocating and/or deallocating as necessary. There is no guarantee on the
19591951
/// value of any bits upon return. Caller should populate the bits after.

llvm/include/llvm/Support/float128.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
#ifndef LLVM_FLOAT128
1010
#define LLVM_FLOAT128
1111

12+
#include <cmath>
13+
1214
namespace llvm {
1315

14-
#if defined(__clang__) && defined(__FLOAT128__) && \
15-
defined(__SIZEOF_INT128__) && !defined(__LONG_DOUBLE_IBM128__)
16-
#define HAS_IEE754_FLOAT128
17-
typedef __float128 float128;
18-
#elif defined(__FLOAT128__) && defined(__SIZEOF_INT128__) && \
19-
!defined(__LONG_DOUBLE_IBM128__) && \
20-
(defined(__GNUC__) || defined(__GNUG__))
16+
#ifdef HAS_LOGF128
17+
#if !defined(__LONG_DOUBLE_IBM128__) && (__SIZEOF_INT128__ == 16)
18+
typedef decltype(logf128(0.)) float128;
2119
#define HAS_IEE754_FLOAT128
22-
typedef _Float128 float128;
2320
#endif
21+
#endif // HAS_LOGF128
2422

2523
} // namespace llvm
2624
#endif // LLVM_FLOAT128

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,3 @@ add_llvm_component_library(LLVMAnalysis
162162
Support
163163
TargetParser
164164
)
165-
166-
include(CheckCXXSymbolExists)
167-
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
168-
if(HAS_LOGF128)
169-
target_compile_definitions(LLVMAnalysis PRIVATE HAS_LOGF128)
170-
endif()

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "llvm/Support/ErrorHandling.h"
5555
#include "llvm/Support/KnownBits.h"
5656
#include "llvm/Support/MathExtras.h"
57+
#include "llvm/Support/float128.h"
5758
#include <cassert>
5859
#include <cerrno>
5960
#include <cfenv>
@@ -1741,7 +1742,7 @@ Constant *GetConstantFoldFPValue(double V, Type *Ty) {
17411742
llvm_unreachable("Can only constant fold half/float/double");
17421743
}
17431744

1744-
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1745+
#if defined(HAS_IEE754_FLOAT128)
17451746
Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) {
17461747
if (Ty->isFP128Ty())
17471748
return ConstantFP::get(Ty, V);
@@ -1781,11 +1782,18 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
17811782
return GetConstantFoldFPValue(Result, Ty);
17821783
}
17831784

1784-
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1785+
#if defined(HAS_IEE754_FLOAT128)
17851786
Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
17861787
Type *Ty) {
17871788
llvm_fenv_clearexcept();
1788-
float128 Result = NativeFP(V.convertToQuad());
1789+
1790+
if (!V.isValidIEEEQuad())
1791+
return nullptr;
1792+
1793+
APInt Api = V.bitcastToAPInt();
1794+
__uint128_t Int128 = ((__uint128_t)Api.getWord(64) << 64) + Api.getWord(0);
1795+
float128 Result = NativeFP(llvm::bit_cast<float128>(Int128));
1796+
17891797
if (llvm_fenv_testexcept()) {
17901798
llvm_fenv_clearexcept();
17911799
return nullptr;
@@ -2114,10 +2122,16 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
21142122
if (IntrinsicID == Intrinsic::canonicalize)
21152123
return constantFoldCanonicalize(Ty, Call, U);
21162124

2117-
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2125+
#if defined(HAS_IEE754_FLOAT128)
21182126
if (Ty->isFP128Ty()) {
21192127
if (IntrinsicID == Intrinsic::log) {
2120-
float128 Result = logf128(Op->getValueAPF().convertToQuad());
2128+
APFloat Value = Op->getValueAPF();
2129+
if (!Value.isValidIEEEQuad())
2130+
return nullptr;
2131+
APInt api = Value.bitcastToAPInt();
2132+
__uint128_t Int128 =
2133+
((__uint128_t)api.getWord(64) << 64) + api.getWord(0);
2134+
float128 Result = logf128(llvm::bit_cast<float128>(Int128));
21212135
return GetConstantFoldFPValue128(Result, Ty);
21222136
}
21232137

llvm/lib/Support/APFloat.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,15 +3749,6 @@ double IEEEFloat::convertToDouble() const {
37493749
return api.bitsToDouble();
37503750
}
37513751

3752-
#ifdef HAS_IEE754_FLOAT128
3753-
float128 IEEEFloat::convertToQuad() const {
3754-
assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
3755-
"Float semantics are not IEEEquads");
3756-
APInt api = bitcastToAPInt();
3757-
return api.bitsToQuad();
3758-
}
3759-
#endif
3760-
37613752
/// Integer bit is explicit in this format. Intel hardware (387 and later)
37623753
/// does not support these bit patterns:
37633754
/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
@@ -5406,20 +5397,9 @@ double APFloat::convertToDouble() const {
54065397
return Temp.getIEEE().convertToDouble();
54075398
}
54085399

5409-
#ifdef HAS_IEE754_FLOAT128
5410-
float128 APFloat::convertToQuad() const {
5411-
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
5412-
return getIEEE().convertToQuad();
5413-
assert(getSemantics().isRepresentableBy(semIEEEquad) &&
5414-
"Float semantics is not representable by IEEEquad");
5415-
APFloat Temp = *this;
5416-
bool LosesInfo;
5417-
opStatus St = Temp.convert(semIEEEquad, rmNearestTiesToEven, &LosesInfo);
5418-
assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision");
5419-
(void)St;
5420-
return Temp.getIEEE().convertToQuad();
5400+
bool APFloat::isValidIEEEQuad() const {
5401+
return (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad);
54215402
}
5422-
#endif
54235403

54245404
float APFloat::convertToFloat() const {
54255405
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)

0 commit comments

Comments
 (0)