Skip to content

Commit 62df07e

Browse files
committed
Constant Fold logf128 calls
This is a second attempt to land llvm#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 efce8a0 commit 62df07e

File tree

13 files changed

+235
-0
lines changed

13 files changed

+235
-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(
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3+
4+
; REQUIRES: has_logf128
5+
declare fp128 @llvm.log.f128(fp128)
6+
7+
define fp128 @log_e_64(){
8+
; CHECK-LABEL: define fp128 @log_e_64() {
9+
; CHECK-NEXT: ret fp128 0xL300000000000000040010A2B23F3BAB7
10+
;
11+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000004005000000000000)
12+
ret fp128 %A
13+
}
14+
15+
define fp128 @log_e_smallest_positive_subnormal_number(){
16+
; CHECK-LABEL: define fp128 @log_e_smallest_positive_subnormal_number() {
17+
; CHECK-NEXT: ret fp128 0xL3000000000000000C00C654628220780
18+
;
19+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000010000000000000000)
20+
ret fp128 %A
21+
}
22+
23+
define fp128 @log_e_largest_subnormal_number(){
24+
; CHECK-LABEL: define fp128 @log_e_largest_subnormal_number() {
25+
; CHECK-NEXT: ret fp128 0xLD000000000000000C00C62D918CE2421
26+
;
27+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xLFFFFFFFFFFFFFFFF0000FFFFFFFFFFFF)
28+
ret fp128 %A
29+
}
30+
31+
define fp128 @log_e_smallest_positive_normal_number(){
32+
;
33+
; CHECK-LABEL: define fp128 @log_e_smallest_positive_normal_number() {
34+
; CHECK-NEXT: ret fp128 0xLD000000000000000C00C62D918CE2421
35+
;
36+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000000001000000000000)
37+
ret fp128 %A
38+
}
39+
40+
define fp128 @log_e_largest_normal_number(){
41+
; CHECK-LABEL: define fp128 @log_e_largest_normal_number() {
42+
; CHECK-NEXT: ret fp128 0xLF000000000000000400C62E42FEFA39E
43+
;
44+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xLFFFFFFFFFFFFFFFF7FFEFFFFFFFFFFFF)
45+
ret fp128 %A
46+
}
47+
48+
define fp128 @log_e_largest_number_less_than_one(){
49+
; CHECK-LABEL: define fp128 @log_e_largest_number_less_than_one() {
50+
; CHECK-NEXT: ret fp128 0xL0000000000000000BF8E000000000000
51+
;
52+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xLFFFFFFFFFFFFFFFF3FFEFFFFFFFFFFFF)
53+
ret fp128 %A
54+
}
55+
56+
define fp128 @log_e_1(){
57+
; CHECK-LABEL: define fp128 @log_e_1() {
58+
; CHECK-NEXT: ret fp128 0xL00000000000000000000000000000000
59+
;
60+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000003FFF000000000000)
61+
ret fp128 %A
62+
}
63+
64+
define fp128 @log_e_smallest_number_larger_than_one(){
65+
; CHECK-LABEL: define fp128 @log_e_smallest_number_larger_than_one() {
66+
; CHECK-NEXT: ret fp128 0xL00000000000000003F8F000000000000
67+
;
68+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000013FFF000000000000)
69+
ret fp128 %A
70+
}
71+
72+
define fp128 @log_e_negative_2(){
73+
; CHECK-LABEL: define fp128 @log_e_negative_2() {
74+
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF800000000000
75+
;
76+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000C000000000000000)
77+
ret fp128 %A
78+
}
79+
80+
define fp128 @log_e_0(){
81+
; CHECK-LABEL: define fp128 @log_e_0() {
82+
; CHECK-NEXT: ret fp128 0xL0000000000000000FFFF000000000000
83+
;
84+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000000000000000000000)
85+
ret fp128 %A
86+
}
87+
88+
define fp128 @log_e_negative_0(){
89+
; CHECK-LABEL: define fp128 @log_e_negative_0() {
90+
; CHECK-NEXT: ret fp128 0xL0000000000000000FFFF000000000000
91+
;
92+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000008000000000000000)
93+
ret fp128 %A
94+
}
95+
96+
define fp128 @log_e_infinity(){
97+
; CHECK-LABEL: define fp128 @log_e_infinity() {
98+
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF000000000000
99+
;
100+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000007FFF000000000000)
101+
ret fp128 %A
102+
}
103+
104+
define fp128 @log_e_negative_infinity(){
105+
; CHECK-LABEL: define fp128 @log_e_negative_infinity() {
106+
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF800000000000
107+
;
108+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000FFFF000000000000)
109+
ret fp128 %A
110+
}
111+
112+
define fp128 @log_e_nan(){
113+
; CHECK-LABEL: define fp128 @log_e_nan() {
114+
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF800000000001
115+
;
116+
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL00000000000000007FFF000000000001)
117+
ret fp128 %A
118+
}
119+
120+
define <2 x fp128> @log_e_negative_2_vector(){
121+
; CHECK-LABEL: define <2 x fp128> @log_e_negative_2_vector() {
122+
; CHECK-NEXT: ret <2 x fp128> <fp128 0xL00000000000000007FFF800000000000, fp128 0xL00000000000000007FFF800000000000>
123+
;
124+
%A = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
125+
ret <2 x fp128> %A
126+
}

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)

0 commit comments

Comments
 (0)