Skip to content

Commit 2bf6b91

Browse files
MDevereaucjdb
authored andcommitted
Enable logf128 constant folding for hosts with 128bit long double (llvm#104929)
This is a reland of (llvm#96287). This patch attempts to reduce the reverted patch's clang compile time by removing #includes of float128.h and inlining convertToQuad functions instead.
1 parent 82f512a commit 2bf6b91

File tree

8 files changed

+42
-75
lines changed

8 files changed

+42
-75
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
@@ -246,17 +246,6 @@ else()
246246
set(HAVE_LIBEDIT 0)
247247
endif()
248248

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

262+
check_cxx_symbol_exists(logf128 cmath HAS_LOGF128)
263+
check_symbol_exists(__powerpc64le__ "" __PPC64LE)
264+
if(HAS_LOGF128 AND NOT __PPC64LE)
265+
set(LLVM_HAS_LOGF128 On)
266+
add_compile_definitions(HAS_LOGF128)
267+
endif()
268+
273269
# Determine whether we can register EH tables.
274270
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
275271
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: 0 additions & 8 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>
@@ -1677,13 +1676,6 @@ class [[nodiscard]] APInt {
16771676
/// any bit width. Exactly 64 bits will be translated.
16781677
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
16791678

1680-
#ifdef HAS_IEE754_FLOAT128
1681-
float128 bitsToQuad() const {
1682-
__uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
1683-
return llvm::bit_cast<float128>(ul);
1684-
}
1685-
#endif
1686-
16871679
/// Converts APInt bits to a float
16881680
///
16891681
/// The conversion does not do a translation from integer to float, it just

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: 24 additions & 6 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,25 @@ 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)
1786+
float128 ConvertToQuad(const APFloat &Apf) {
1787+
APInt Api = Apf.bitcastToAPInt();
1788+
__uint128_t Uint128 =
1789+
((__uint128_t)Api.extractBitsAsZExtValue(64, 64) << 64) +
1790+
Api.extractBitsAsZExtValue(64, 0);
1791+
return llvm::bit_cast<float128>(Uint128);
1792+
}
1793+
#endif
1794+
1795+
#if defined(HAS_IEE754_FLOAT128)
17851796
Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
17861797
Type *Ty) {
17871798
llvm_fenv_clearexcept();
1788-
float128 Result = NativeFP(V.convertToQuad());
1799+
if (!V.isValidIEEEQuad())
1800+
return nullptr;
1801+
1802+
float128 Result = NativeFP(ConvertToQuad(V));
1803+
17891804
if (llvm_fenv_testexcept()) {
17901805
llvm_fenv_clearexcept();
17911806
return nullptr;
@@ -2114,13 +2129,16 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
21142129
if (IntrinsicID == Intrinsic::canonicalize)
21152130
return constantFoldCanonicalize(Ty, Call, U);
21162131

2117-
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2132+
#if defined(HAS_IEE754_FLOAT128)
21182133
if (Ty->isFP128Ty()) {
21192134
if (IntrinsicID == Intrinsic::log) {
2120-
float128 Result = logf128(Op->getValueAPF().convertToQuad());
2135+
APFloat Value = Op->getValueAPF();
2136+
if (!Value.isValidIEEEQuad())
2137+
return nullptr;
2138+
2139+
float128 Result = logf128(ConvertToQuad(Value));
21212140
return GetConstantFoldFPValue128(Result, Ty);
21222141
}
2123-
21242142
LibFunc Fp128Func = NotLibFunc;
21252143
if (TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
21262144
Fp128Func == LibFunc_logl)

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)