Skip to content

Commit

Permalink
Reland logf128 constant folding (#103217)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MDevereau authored Aug 14, 2024
1 parent 8eadf21 commit 3cab7c5
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 36 deletions.
2 changes: 0 additions & 2 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,6 @@ set(LLVM_USE_STATIC_ZSTD FALSE CACHE BOOL "Use static version of zstd. Can be TR

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

set(LLVM_HAS_LOGF128 "OFF" CACHE STRING "Use logf128 to constant fold fp128 logarithm calls. Can be ON, OFF, or FORCE_ON")

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

set(LLVM_Z3_INSTALL_DIR "" CACHE STRING "Install directory of the Z3 solver.")
Expand Down
17 changes: 6 additions & 11 deletions llvm/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,6 @@ else()
set(HAVE_LIBEDIT 0)
endif()

if(LLVM_HAS_LOGF128)
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)

if(LLVM_HAS_LOGF128 STREQUAL FORCE_ON AND NOT HAS_LOGF128)
message(FATAL_ERROR "Failed to configure logf128")
endif()

set(LLVM_HAS_LOGF128 "${HAS_LOGF128}")
endif()

# function checks
check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM)
find_package(Backtrace)
Expand All @@ -271,6 +260,12 @@ if(C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
endif()

check_cxx_symbol_exists(logf128 cmath HAS_LOGF128)
if(HAS_LOGF128)
set(LLVM_HAS_LOGF128 On)
add_compile_definitions(HAS_LOGF128)
endif()

# Determine whether we can register EH tables.
check_symbol_exists(__register_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_REGISTER_FRAME)
check_symbol_exists(__deregister_frame "${CMAKE_CURRENT_LIST_DIR}/unwind.h" HAVE_DEREGISTER_FRAME)
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class IEEEFloat final : public APFloatBase {
Expected<opStatus> convertFromString(StringRef, roundingMode);
APInt bitcastToAPInt() const;
double convertToDouble() const;
#ifdef HAS_IEE754_FLOAT128
#if defined(HAS_IEE754_FLOAT128)
float128 convertToQuad() const;
#endif
float convertToFloat() const;
Expand Down Expand Up @@ -1279,7 +1279,7 @@ class APFloat : public APFloatBase {
/// \pre The APFloat must be built using semantics, that can be represented by
/// the host float type without loss of precision. It can be IEEEquad and
/// shorter semantics, like IEEEdouble and others.
#ifdef HAS_IEE754_FLOAT128
#if defined(HAS_IEE754_FLOAT128)
float128 convertToQuad() const;
#endif

Expand Down
16 changes: 8 additions & 8 deletions llvm/include/llvm/Support/float128.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
#ifndef LLVM_FLOAT128
#define LLVM_FLOAT128

#ifndef _GLIBCXX_MATH_H
#include <cmath>
#endif

namespace llvm {

#if defined(__clang__) && defined(__FLOAT128__) && \
defined(__SIZEOF_INT128__) && !defined(__LONG_DOUBLE_IBM128__)
#define HAS_IEE754_FLOAT128
typedef __float128 float128;
#elif defined(__FLOAT128__) && defined(__SIZEOF_INT128__) && \
!defined(__LONG_DOUBLE_IBM128__) && \
(defined(__GNUC__) || defined(__GNUG__))
#ifdef HAS_LOGF128
#if !defined(__LONG_DOUBLE_IBM128__) && (__SIZEOF_INT128__ == 16)
typedef decltype(logf128(0.)) float128;
#define HAS_IEE754_FLOAT128
typedef _Float128 float128;
#endif
#endif // HAS_LOGF128

} // namespace llvm
#endif // LLVM_FLOAT128
6 changes: 0 additions & 6 deletions llvm/lib/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,3 @@ add_llvm_component_library(LLVMAnalysis
Support
TargetParser
)

include(CheckCXXSymbolExists)
check_cxx_symbol_exists(logf128 math.h HAS_LOGF128)
if(HAS_LOGF128)
target_compile_definitions(LLVMAnalysis PRIVATE HAS_LOGF128)
endif()
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
return GetConstantFoldFPValue(Result, Ty);
}

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

#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
#if defined(HAS_IEE754_FLOAT128)
if (Ty->isFP128Ty()) {
if (IntrinsicID == Intrinsic::log) {
float128 Result = logf128(Op->getValueAPF().convertToQuad());
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Support/APFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3749,7 +3749,7 @@ double IEEEFloat::convertToDouble() const {
return api.bitsToDouble();
}

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

#ifdef HAS_IEE754_FLOAT128
#if defined(HAS_IEE754_FLOAT128)
float128 APFloat::convertToQuad() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
return getIEEE().convertToQuad();
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ define fp128 @log_e_smallest_number_larger_than_one(){

define fp128 @log_e_negative_2(){
; CHECK-LABEL: define fp128 @log_e_negative_2() {
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF800000000000
; CHECK-NEXT: ret fp128 0xL0000000000000000{{[7|F]}}FFF800000000000
;
%A = call fp128 @llvm.log.f128(fp128 noundef 0xL0000000000000000C000000000000000)
ret fp128 %A
Expand Down Expand Up @@ -104,7 +104,7 @@ define fp128 @log_e_infinity(){

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

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

0 comments on commit 3cab7c5

Please sign in to comment.