Skip to content

Commit 3ef64f7

Browse files
committed
Revert "Enable logf128 constant folding for hosts with 128bit long double (#104929)"
ConstantFolding behaves differently depending on host's `HAS_IEE754_FLOAT128`. LLVM should not change the behavior depending on host configurations. This reverts commit 14c7e4a. (llvmorg-20-init-3262-g14c7e4a18449 and llvmorg-20-init-3498-g001e423ac626)
1 parent a6f87ab commit 3ef64f7

File tree

8 files changed

+75
-42
lines changed

8 files changed

+75
-42
lines changed

llvm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ 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+
563565
set(LLVM_ENABLE_HTTPLIB "OFF" CACHE STRING "Use cpp-httplib HTTP server library if available. Can be ON, OFF, or FORCE_ON")
564566

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

llvm/cmake/config-ix.cmake

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ 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+
249260
# function checks
250261
check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
251262
find_package(Backtrace)
@@ -259,13 +270,6 @@ if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
259270
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
260271
endif()
261272

262-
check_cxx_symbol_exists(logf128 cmath HAS_LOGF128)
263-
check_symbol_exists(__powerpc__ "" __PPC64LE)
264-
if(HAS_LOGF128 AND NOT __PPC64LE)
265-
set(LLVM_HAS_LOGF128 On)
266-
add_compile_definitions(HAS_LOGF128)
267-
endif()
268-
269273
# Determine whether we can register EH tables.
270274
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
271275
check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)

llvm/include/llvm/ADT/APFloat.h

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

2425
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
@@ -377,6 +378,9 @@ class IEEEFloat final : public APFloatBase {
377378
Expected<opStatus> convertFromString(StringRef, roundingMode);
378379
APInt bitcastToAPInt() const;
379380
double convertToDouble() const;
381+
#ifdef HAS_IEE754_FLOAT128
382+
float128 convertToQuad() const;
383+
#endif
380384
float convertToFloat() const;
381385

382386
/// @}
@@ -1270,9 +1274,14 @@ class APFloat : public APFloatBase {
12701274
/// shorter semantics, like IEEEsingle and others.
12711275
double convertToDouble() const;
12721276

1273-
/// Return true if this APFloat has quadruple precision floating point
1274-
/// semantics
1275-
bool isValidIEEEQuad() const;
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
12761285

12771286
/// Converts this APFloat to host float value.
12781287
///

llvm/include/llvm/ADT/APInt.h

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

1818
#include "llvm/Support/Compiler.h"
1919
#include "llvm/Support/MathExtras.h"
20+
#include "llvm/Support/float128.h"
2021
#include <cassert>
2122
#include <climits>
2223
#include <cstring>
@@ -1676,6 +1677,13 @@ class [[nodiscard]] APInt {
16761677
/// any bit width. Exactly 64 bits will be translated.
16771678
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
16781679

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+
16791687
/// Converts APInt bits to a float
16801688
///
16811689
/// The conversion does not do a translation from integer to float, it just

llvm/include/llvm/Support/float128.h

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

12-
#include <cmath>
13-
1412
namespace llvm {
1513

16-
#ifdef HAS_LOGF128
17-
#if !defined(__LONG_DOUBLE_IBM128__) && (__SIZEOF_INT128__ == 16)
18-
typedef decltype(logf128(0.)) float128;
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__))
1921
#define HAS_IEE754_FLOAT128
22+
typedef _Float128 float128;
2023
#endif
21-
#endif // HAS_LOGF128
2224

2325
} // namespace llvm
2426
#endif // LLVM_FLOAT128

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,9 @@ 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: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include "llvm/Support/ErrorHandling.h"
5555
#include "llvm/Support/KnownBits.h"
5656
#include "llvm/Support/MathExtras.h"
57-
#include "llvm/Support/float128.h"
5857
#include <cassert>
5958
#include <cerrno>
6059
#include <cfenv>
@@ -1742,7 +1741,7 @@ Constant *GetConstantFoldFPValue(double V, Type *Ty) {
17421741
llvm_unreachable("Can only constant fold half/float/double");
17431742
}
17441743

1745-
#if defined(HAS_IEE754_FLOAT128)
1744+
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
17461745
Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) {
17471746
if (Ty->isFP128Ty())
17481747
return ConstantFP::get(Ty, V);
@@ -1782,25 +1781,11 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
17821781
return GetConstantFoldFPValue(Result, Ty);
17831782
}
17841783

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)
1784+
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
17961785
Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
17971786
Type *Ty) {
17981787
llvm_fenv_clearexcept();
1799-
if (!V.isValidIEEEQuad())
1800-
return nullptr;
1801-
1802-
float128 Result = NativeFP(ConvertToQuad(V));
1803-
1788+
float128 Result = NativeFP(V.convertToQuad());
18041789
if (llvm_fenv_testexcept()) {
18051790
llvm_fenv_clearexcept();
18061791
return nullptr;
@@ -2129,16 +2114,13 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
21292114
if (IntrinsicID == Intrinsic::canonicalize)
21302115
return constantFoldCanonicalize(Ty, Call, U);
21312116

2132-
#if defined(HAS_IEE754_FLOAT128)
2117+
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
21332118
if (Ty->isFP128Ty()) {
21342119
if (IntrinsicID == Intrinsic::log) {
2135-
APFloat Value = Op->getValueAPF();
2136-
if (!Value.isValidIEEEQuad())
2137-
return nullptr;
2138-
2139-
float128 Result = logf128(ConvertToQuad(Value));
2120+
float128 Result = logf128(Op->getValueAPF().convertToQuad());
21402121
return GetConstantFoldFPValue128(Result, Ty);
21412122
}
2123+
21422124
LibFunc Fp128Func = NotLibFunc;
21432125
if (TLI && TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
21442126
Fp128Func == LibFunc_logl)

llvm/lib/Support/APFloat.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,6 +3749,15 @@ 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+
37523761
/// Integer bit is explicit in this format. Intel hardware (387 and later)
37533762
/// does not support these bit patterns:
37543763
/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
@@ -5397,9 +5406,20 @@ double APFloat::convertToDouble() const {
53975406
return Temp.getIEEE().convertToDouble();
53985407
}
53995408

5400-
bool APFloat::isValidIEEEQuad() const {
5401-
return (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad);
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();
54025421
}
5422+
#endif
54035423

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

0 commit comments

Comments
 (0)