Skip to content

Commit 088aa81

Browse files
committed
Constant Fold logf128 calls
This is a second attempt to land #84501 which failed on several targets. This patch adds the HAS_IEE754_FLOAT128 define which makes the check for typedef'ing float128 more precise by checking whether __uint128_t is available and checking if the host does not use __ibm128 which is prevalent on power pc targets and replaces IEEE754 float128s.
1 parent df241b1 commit 088aa81

File tree

13 files changed

+115
-0
lines changed

13 files changed

+115
-0
lines changed

llvm/CMakeLists.txt

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

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

565+
set(LLVM_HAS_LOGF128 "OFF" CACHE STRING "Use logf128 to constant fold fp128 logarithm calls. Can be ON, OFF, or FORCE_ON")
566+
565567
set(LLVM_ENABLE_HTTPLIB "OFF" CACHE STRING "Use cpp-httplib HTTP server library if available. Can be ON, OFF, or FORCE_ON")
566568

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

llvm/cmake/config-ix.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ else()
257257
set(LLVM_ENABLE_TERMINFO 0)
258258
endif()
259259

260+
if(LLVM_HAS_LOGF128)
261+
include(CheckCXXSymbolExists)
262+
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
263+
264+
if(LLVM_HAS_LOGF128 STREQUAL FORCE_ON AND NOT HAS_LOGF128)
265+
message(FATAL_ERROR "Failed to configure logf128")
266+
endif()
267+
268+
set(LLVM_HAS_LOGF128 "${HAS_LOGF128}")
269+
endif()
270+
260271
# function checks
261272
check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
262273
find_package(Backtrace)

llvm/include/llvm/ADT/APFloat.h

Lines changed: 13 additions & 0 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) \
@@ -354,6 +355,9 @@ class IEEEFloat final : public APFloatBase {
354355
Expected<opStatus> convertFromString(StringRef, roundingMode);
355356
APInt bitcastToAPInt() const;
356357
double convertToDouble() const;
358+
#ifdef HAS_IEE754_FLOAT128
359+
float128 convertToQuad() const;
360+
#endif
357361
float convertToFloat() const;
358362

359363
/// @}
@@ -1218,6 +1222,15 @@ class APFloat : public APFloatBase {
12181222
/// shorter semantics, like IEEEsingle and others.
12191223
double convertToDouble() const;
12201224

1225+
/// Converts this APFloat to host float value.
1226+
///
1227+
/// \pre The APFloat must be built using semantics, that can be represented by
1228+
/// the host float type without loss of precision. It can be IEEEquad and
1229+
/// shorter semantics, like IEEEdouble and others.
1230+
#ifdef HAS_IEE754_FLOAT128
1231+
float128 convertToQuad() const;
1232+
#endif
1233+
12211234
/// Converts this APFloat to host float value.
12221235
///
12231236
/// \pre The APFloat must be built using semantics, that can be represented by

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>
@@ -1670,6 +1671,13 @@ class [[nodiscard]] APInt {
16701671
/// any bit width. Exactly 64 bits will be translated.
16711672
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
16721673

1674+
#ifdef HAS_IEE754_FLOAT128
1675+
float128 bitsToQuad() const {
1676+
__uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
1677+
return llvm::bit_cast<float128>(ul);
1678+
}
1679+
#endif
1680+
16731681
/// Converts APInt bits to a float
16741682
///
16751683
/// The conversion does not do a translation from integer to float, it just

llvm/include/llvm/Config/llvm-config.h.cmake

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

201+
/* Define if logf128 is available */
202+
#cmakedefine LLVM_HAS_LOGF128
203+
201204
#endif

llvm/include/llvm/Support/float128.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- llvm/Support/float128.h - Compiler abstraction support --*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_FLOAT128
10+
#define LLVM_FLOAT128
11+
12+
namespace llvm {
13+
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__))
21+
#define HAS_IEE754_FLOAT128
22+
typedef _Float128 float128;
23+
#endif
24+
25+
} // namespace llvm
26+
#endif // LLVM_FLOAT128

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,9 @@ add_llvm_component_library(LLVMAnalysis
159159
Support
160160
TargetParser
161161
)
162+
163+
include(CheckCXXSymbolExists)
164+
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
165+
if(HAS_LOGF128)
166+
target_compile_definitions(LLVMAnalysis PRIVATE HAS_LOGF128)
167+
endif()

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,17 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
20892089
if (IntrinsicID == Intrinsic::canonicalize)
20902090
return constantFoldCanonicalize(Ty, Call, U);
20912091

2092+
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2093+
if (Ty->isFP128Ty()) {
2094+
switch (IntrinsicID) {
2095+
default:
2096+
return nullptr;
2097+
case Intrinsic::log:
2098+
return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
2099+
}
2100+
}
2101+
#endif
2102+
20922103
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
20932104
return nullptr;
20942105

llvm/lib/Support/APFloat.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,6 +3670,15 @@ double IEEEFloat::convertToDouble() const {
36703670
return api.bitsToDouble();
36713671
}
36723672

3673+
#ifdef HAS_IEE754_FLOAT128
3674+
float128 IEEEFloat::convertToQuad() const {
3675+
assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
3676+
"Float semantics are not IEEEquads");
3677+
APInt api = bitcastToAPInt();
3678+
return api.bitsToQuad();
3679+
}
3680+
#endif
3681+
36733682
/// Integer bit is explicit in this format. Intel hardware (387 and later)
36743683
/// does not support these bit patterns:
36753684
/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
@@ -5265,6 +5274,21 @@ double APFloat::convertToDouble() const {
52655274
return Temp.getIEEE().convertToDouble();
52665275
}
52675276

5277+
#ifdef HAS_IEE754_FLOAT128
5278+
float128 APFloat::convertToQuad() const {
5279+
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
5280+
return getIEEE().convertToQuad();
5281+
assert(getSemantics().isRepresentableBy(semIEEEquad) &&
5282+
"Float semantics is not representable by IEEEquad");
5283+
APFloat Temp = *this;
5284+
bool LosesInfo;
5285+
opStatus St = Temp.convert(semIEEEquad, rmNearestTiesToEven, &LosesInfo);
5286+
assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision");
5287+
(void)St;
5288+
return Temp.getIEEE().convertToQuad();
5289+
}
5290+
#endif
5291+
52685292
float APFloat::convertToFloat() const {
52695293
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)
52705294
return getIEEE().convertToFloat();

llvm/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +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
2930
)
3031

3132
configure_lit_site_cfg(

llvm/test/lit.cfg.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,6 @@ def have_ld64_plugin_support():
617617
# "OBJECT_MODE" to 'any' by default on AIX OS.
618618
if "system-aix" in config.available_features:
619619
config.environment["OBJECT_MODE"] = "any"
620+
621+
if config.has_logf128:
622+
config.available_features.add("has_logf128")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +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@
6667

6768
import lit.llvm
6869
lit.llvm.initialize(lit_config, config)

llvm/unittests/Analysis/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,11 @@ if(NOT WIN32)
8080
export_executable_symbols_for_plugins(AnalysisTests)
8181
endif()
8282

83+
include(CheckCXXSymbolExists)
84+
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
85+
if(HAS_LOGF128)
86+
target_compile_definitions(AnalysisTests PRIVATE HAS_LOGF128)
87+
endif()
88+
8389
add_subdirectory(InlineAdvisorPlugin)
8490
add_subdirectory(InlineOrderPlugin)

0 commit comments

Comments
 (0)