Skip to content

Commit 7c41145

Browse files
committed
Enable logf128 constant folding for hosts with 128bit long double
Hosts with a long double size of 128 bits can benefit highly from constant fp128 folding with the function logf128 This patch relands llvm#104929. With commits 001e423 and 83a5c7c fixing buildbot failures. This patch also adds the option to manually disable the behaviour with the cmake option -DLLVM_DISABLE_LOGF128=On
1 parent 8ae877a commit 7c41145

File tree

16 files changed

+53
-82
lines changed

16 files changed

+53
-82
lines changed

llvm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ 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")
563+
set(LLVM_DISABLE_LOGF128 "OFF" CACHE BOOL "Disable the use of logf128 to constant fold fp128 logarithm calls. Can be ON, OFF, or FORCE_ON")
564564

565565
set(LLVM_ENABLE_HTTPLIB "OFF" CACHE STRING "Use cpp-httplib HTTP server library if available. Can be ON, OFF, or FORCE_ON")
566566

llvm/cmake/config-ix.cmake

Lines changed: 6 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,12 @@ 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(__powerpc__ "" __PPC)
264+
if(HAS_LOGF128 AND NOT __PPC AND NOT LLVM_DISABLE_LOGF128)
265+
add_compile_definitions(HAS_LOGF128)
266+
endif()
267+
273268
# Determine whether we can register EH tables.
274269
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
275270
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/Config/llvm-config.h.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
/* Define if plugins enabled */
199199
#cmakedefine LLVM_ENABLE_PLUGINS
200200

201-
/* Define if logf128 is available */
202-
#cmakedefine LLVM_HAS_LOGF128
201+
/* Define if logf128 is disabled */
202+
#cmakedefine LLVM_DISABLE_LOGF128
203203

204204
#endif

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,3 @@ add_llvm_component_library(LLVMAnalysis
162162
Support
163163
TargetParser
164164
)
165-
166-
if(LLVM_HAS_LOGF128)
167-
target_compile_definitions(LLVMAnalysis PRIVATE HAS_LOGF128)
168-
endif()

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 24 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,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,10 +2129,14 @@ 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
}
21232142

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)

llvm/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ llvm_canonicalize_cmake_booleans(
2626
LLVM_TOOL_LLVM_DRIVER_BUILD
2727
LLVM_INCLUDE_SPIRV_TOOLS_TESTS
2828
LLVM_APPEND_VC_REV
29-
LLVM_HAS_LOGF128
29+
LLVM_DISABLE_LOGF128
3030
)
3131

3232
configure_lit_site_cfg(

llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
22
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
33

4-
; REQUIRES: has_logf128
4+
; UNSUPPORTED: disable_logf128
55
declare fp128 @llvm.log.f128(fp128)
66
declare fp128 @logl(fp128)
77

llvm/test/lit.cfg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,5 +620,5 @@ def have_ld64_plugin_support():
620620
if "system-aix" in config.available_features:
621621
config.environment["OBJECT_MODE"] = "any"
622622

623-
if config.has_logf128:
624-
config.available_features.add("has_logf128")
623+
if config.disable_logf128:
624+
config.available_features.add("disable_logf128")

llvm/test/lit.site.cfg.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
6363
config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@
6464
config.have_vc_rev = @LLVM_APPEND_VC_REV@
6565
config.force_vc_rev = "@LLVM_FORCE_VC_REVISION@"
66-
config.has_logf128 = @LLVM_HAS_LOGF128@
66+
config.disable_logf128 = @LLVM_DISABLE_LOGF128@
6767

6868
import lit.llvm
6969
lit.llvm.initialize(lit_config, config)

llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ write_cmake_config("llvm-config") {
316316
"LLVM_ENABLE_HTTPLIB=",
317317
"LLVM_FORCE_USE_OLD_TOOLCHAIN=",
318318
"LLVM_HAS_ATOMICS=1",
319-
"LLVM_HAS_LOGF128=",
319+
"LLVM_DISABLE_LOGF128=",
320320
"LLVM_HAVE_TFLITE=",
321321
"LLVM_HOST_TRIPLE=$llvm_current_triple",
322322
"LLVM_NATIVE_ARCH=$native_target",

llvm/utils/gn/secondary/llvm/test/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ write_lit_config("lit_site_cfg") {
6464
"LLVM_ENABLE_FFI=0",
6565
"LLVM_ENABLE_HTTPLIB=0",
6666
"LLVM_FORCE_VC_REVISION=",
67-
"LLVM_HAS_LOGF128=0",
67+
"LLVM_DISABLE_LOGF128=0",
6868
"LLVM_HAVE_OPT_VIEWER_MODULES=0",
6969
"LLVM_HOST_TRIPLE=$llvm_current_triple",
7070
"LLVM_INCLUDE_DXIL_TESTS=0",

utils/bazel/llvm_configs/llvm-config.h.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
/* Define if plugins enabled */
199199
#cmakedefine LLVM_ENABLE_PLUGINS
200200

201-
/* Define if logf128 is available */
202-
#cmakedefine LLVM_HAS_LOGF128
201+
/* Define if logf128 is disabled */
202+
#cmakedefine LLVM_DISABLE_LOGF128
203203

204204
#endif

0 commit comments

Comments
 (0)