Skip to content

Commit 3cab7c5

Browse files
authored
Reland logf128 constant folding (#103217)
This is a reland of #96287. This change makes tests in logf128.ll ignore the sign of NaNs for negative value tests and moves an #include <cmath> to be blocked behind #ifndef _GLIBCXX_MATH_H.
1 parent 8eadf21 commit 3cab7c5

File tree

8 files changed

+23
-36
lines changed

8 files changed

+23
-36
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: 6 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,12 @@ 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+
if(HAS_LOGF128)
265+
set(LLVM_HAS_LOGF128 On)
266+
add_compile_definitions(HAS_LOGF128)
267+
endif()
268+
274269
# Determine whether we can register EH tables.
275270
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
276271
check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)

llvm/include/llvm/ADT/APFloat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class IEEEFloat final : public APFloatBase {
378378
Expected<opStatus> convertFromString(StringRef, roundingMode);
379379
APInt bitcastToAPInt() const;
380380
double convertToDouble() const;
381-
#ifdef HAS_IEE754_FLOAT128
381+
#if defined(HAS_IEE754_FLOAT128)
382382
float128 convertToQuad() const;
383383
#endif
384384
float convertToFloat() const;
@@ -1279,7 +1279,7 @@ class APFloat : public APFloatBase {
12791279
/// \pre The APFloat must be built using semantics, that can be represented by
12801280
/// the host float type without loss of precision. It can be IEEEquad and
12811281
/// shorter semantics, like IEEEdouble and others.
1282-
#ifdef HAS_IEE754_FLOAT128
1282+
#if defined(HAS_IEE754_FLOAT128)
12831283
float128 convertToQuad() const;
12841284
#endif
12851285

llvm/include/llvm/Support/float128.h

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

12+
#ifndef _GLIBCXX_MATH_H
13+
#include <cmath>
14+
#endif
15+
1216
namespace llvm {
1317

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__))
18+
#ifdef HAS_LOGF128
19+
#if !defined(__LONG_DOUBLE_IBM128__) && (__SIZEOF_INT128__ == 16)
20+
typedef decltype(logf128(0.)) float128;
2121
#define HAS_IEE754_FLOAT128
22-
typedef _Float128 float128;
2322
#endif
23+
#endif // HAS_LOGF128
2424

2525
} // namespace llvm
2626
#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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
17811781
return GetConstantFoldFPValue(Result, Ty);
17821782
}
17831783

1784-
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1784+
#if defined(HAS_IEE754_FLOAT128)
17851785
Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
17861786
Type *Ty) {
17871787
llvm_fenv_clearexcept();
@@ -2114,7 +2114,7 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
21142114
if (IntrinsicID == Intrinsic::canonicalize)
21152115
return constantFoldCanonicalize(Ty, Call, U);
21162116

2117-
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2117+
#if defined(HAS_IEE754_FLOAT128)
21182118
if (Ty->isFP128Ty()) {
21192119
if (IntrinsicID == Intrinsic::log) {
21202120
float128 Result = logf128(Op->getValueAPF().convertToQuad());

llvm/lib/Support/APFloat.cpp

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

3752-
#ifdef HAS_IEE754_FLOAT128
3752+
#if defined(HAS_IEE754_FLOAT128)
37533753
float128 IEEEFloat::convertToQuad() const {
37543754
assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
37553755
"Float semantics are not IEEEquads");
@@ -5406,7 +5406,7 @@ double APFloat::convertToDouble() const {
54065406
return Temp.getIEEE().convertToDouble();
54075407
}
54085408

5409-
#ifdef HAS_IEE754_FLOAT128
5409+
#if defined(HAS_IEE754_FLOAT128)
54105410
float128 APFloat::convertToQuad() const {
54115411
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
54125412
return getIEEE().convertToQuad();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ define fp128 @log_e_smallest_number_larger_than_one(){
7272

7373
define fp128 @log_e_negative_2(){
7474
; CHECK-LABEL: define fp128 @log_e_negative_2() {
75-
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF800000000000
75+
; CHECK-NEXT: ret fp128 0xL0000000000000000{{[7|F]}}FFF800000000000
7676
;
7777
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000C000000000000000)
7878
ret fp128 %A
@@ -104,7 +104,7 @@ define fp128 @log_e_infinity(){
104104

105105
define fp128 @log_e_negative_infinity(){
106106
; CHECK-LABEL: define fp128 @log_e_negative_infinity() {
107-
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF800000000000
107+
; CHECK-NEXT: ret fp128 0xL0000000000000000{{[7|F]}}FFF800000000000
108108
;
109109
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000FFFF000000000000)
110110
ret fp128 %A
@@ -120,7 +120,7 @@ define fp128 @log_e_nan(){
120120

121121
define <2 x fp128> @log_e_negative_2_vector(){
122122
; CHECK-LABEL: define <2 x fp128> @log_e_negative_2_vector() {
123-
; CHECK-NEXT: ret <2 x fp128> <fp128 0xL00000000000000007FFF800000000000, fp128 0xL00000000000000007FFF800000000000>
123+
; CHECK-NEXT: ret <2 x fp128> <fp128 0xL0000000000000000{{[7|F]}}FFF800000000000, fp128 0xL0000000000000000{{[7|F]}}FFF800000000000>
124124
;
125125
%A = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
126126
ret <2 x fp128> %A

0 commit comments

Comments
 (0)