Skip to content

Commit

Permalink
Merged main:3ef64f7ab5b8651eab500cd944984379fce5f639 into amd-gfx:3b4…
Browse files Browse the repository at this point in the history
…93b7eeddf

Local branch amd-gfx 3b493b7 Merged main:b9a02765504f8b83701ffffc097531638c4fc22e into amd-gfx:3906fefcb801
Remote branch main 3ef64f7 Revert "Enable logf128 constant folding for hosts with 128bit long double (llvm#104929)"
  • Loading branch information
SC llvm team authored and SC llvm team committed Aug 25, 2024
2 parents 3b493b7 + 3ef64f7 commit 4bb4bb5
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 47 deletions.
2 changes: 2 additions & 0 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ 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
18 changes: 11 additions & 7 deletions llvm/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ 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 @@ -259,13 +270,6 @@ 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)
check_symbol_exists(__powerpc__ "" __PPC64LE)
if(HAS_LOGF128 AND NOT __PPC64LE)
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
15 changes: 12 additions & 3 deletions llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/float128.h"
#include <memory>

#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
Expand Down Expand Up @@ -377,6 +378,9 @@ class IEEEFloat final : public APFloatBase {
Expected<opStatus> convertFromString(StringRef, roundingMode);
APInt bitcastToAPInt() const;
double convertToDouble() const;
#ifdef HAS_IEE754_FLOAT128
float128 convertToQuad() const;
#endif
float convertToFloat() const;

/// @}
Expand Down Expand Up @@ -1270,9 +1274,14 @@ class APFloat : public APFloatBase {
/// shorter semantics, like IEEEsingle and others.
double convertToDouble() const;

/// Return true if this APFloat has quadruple precision floating point
/// semantics
bool isValidIEEEQuad() const;
/// Converts this APFloat to host float value.
///
/// \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
float128 convertToQuad() const;
#endif

/// Converts this APFloat to host float value.
///
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/ADT/APInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/float128.h"
#include <cassert>
#include <climits>
#include <cstring>
Expand Down Expand Up @@ -1676,6 +1677,13 @@ class [[nodiscard]] APInt {
/// any bit width. Exactly 64 bits will be translated.
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }

#ifdef HAS_IEE754_FLOAT128
float128 bitsToQuad() const {
__uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
return llvm::bit_cast<float128>(ul);
}
#endif

/// Converts APInt bits to a float
///
/// The conversion does not do a translation from integer to float, it just
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Config/llvm-config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/* Indicate that this is LLVM compiled from the amd-gfx branch. */
#define LLVM_HAVE_BRANCH_AMD_GFX
#define LLVM_MAIN_REVISION 509547
#define LLVM_MAIN_REVISION 509549

/* Define if LLVM_ENABLE_DUMP is enabled */
#cmakedefine LLVM_ENABLE_DUMP
Expand Down
14 changes: 8 additions & 6 deletions llvm/include/llvm/Support/float128.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
#ifndef LLVM_FLOAT128
#define LLVM_FLOAT128

#include <cmath>

namespace llvm {

#ifdef HAS_LOGF128
#if !defined(__LONG_DOUBLE_IBM128__) && (__SIZEOF_INT128__ == 16)
typedef decltype(logf128(0.)) float128;
#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__))
#define HAS_IEE754_FLOAT128
typedef _Float128 float128;
#endif
#endif // HAS_LOGF128

} // namespace llvm
#endif // LLVM_FLOAT128
6 changes: 6 additions & 0 deletions llvm/lib/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,9 @@ 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()
30 changes: 6 additions & 24 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/float128.h"
#include <cassert>
#include <cerrno>
#include <cfenv>
Expand Down Expand Up @@ -1742,7 +1741,7 @@ Constant *GetConstantFoldFPValue(double V, Type *Ty) {
llvm_unreachable("Can only constant fold half/float/double");
}

#if defined(HAS_IEE754_FLOAT128)
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) {
if (Ty->isFP128Ty())
return ConstantFP::get(Ty, V);
Expand Down Expand Up @@ -1782,25 +1781,11 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
return GetConstantFoldFPValue(Result, Ty);
}

#if defined(HAS_IEE754_FLOAT128)
float128 ConvertToQuad(const APFloat &Apf) {
APInt Api = Apf.bitcastToAPInt();
__uint128_t Uint128 =
((__uint128_t)Api.extractBitsAsZExtValue(64, 64) << 64) +
Api.extractBitsAsZExtValue(64, 0);
return llvm::bit_cast<float128>(Uint128);
}
#endif

#if defined(HAS_IEE754_FLOAT128)
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
Type *Ty) {
llvm_fenv_clearexcept();
if (!V.isValidIEEEQuad())
return nullptr;

float128 Result = NativeFP(ConvertToQuad(V));

float128 Result = NativeFP(V.convertToQuad());
if (llvm_fenv_testexcept()) {
llvm_fenv_clearexcept();
return nullptr;
Expand Down Expand Up @@ -2129,16 +2114,13 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
if (IntrinsicID == Intrinsic::canonicalize)
return constantFoldCanonicalize(Ty, Call, U);

#if defined(HAS_IEE754_FLOAT128)
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
if (Ty->isFP128Ty()) {
if (IntrinsicID == Intrinsic::log) {
APFloat Value = Op->getValueAPF();
if (!Value.isValidIEEEQuad())
return nullptr;

float128 Result = logf128(ConvertToQuad(Value));
float128 Result = logf128(Op->getValueAPF().convertToQuad());
return GetConstantFoldFPValue128(Result, Ty);
}

LibFunc Fp128Func = NotLibFunc;
if (TLI && TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
Fp128Func == LibFunc_logl)
Expand Down
24 changes: 22 additions & 2 deletions llvm/lib/Support/APFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3749,6 +3749,15 @@ double IEEEFloat::convertToDouble() const {
return api.bitsToDouble();
}

#ifdef HAS_IEE754_FLOAT128
float128 IEEEFloat::convertToQuad() const {
assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
"Float semantics are not IEEEquads");
APInt api = bitcastToAPInt();
return api.bitsToQuad();
}
#endif

/// Integer bit is explicit in this format. Intel hardware (387 and later)
/// does not support these bit patterns:
/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
Expand Down Expand Up @@ -5397,9 +5406,20 @@ double APFloat::convertToDouble() const {
return Temp.getIEEE().convertToDouble();
}

bool APFloat::isValidIEEEQuad() const {
return (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad);
#ifdef HAS_IEE754_FLOAT128
float128 APFloat::convertToQuad() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
return getIEEE().convertToQuad();
assert(getSemantics().isRepresentableBy(semIEEEquad) &&
"Float semantics is not representable by IEEEquad");
APFloat Temp = *this;
bool LosesInfo;
opStatus St = Temp.convert(semIEEEquad, rmNearestTiesToEven, &LosesInfo);
assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision");
(void)St;
return Temp.getIEEE().convertToQuad();
}
#endif

float APFloat::convertToFloat() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)
Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/Target/Mips/MipsConstantIslandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1630,8 +1630,6 @@ MipsConstantIslands::fixupConditionalBr(ImmBranch &Br) {
}

void MipsConstantIslands::prescanForConstants() {
unsigned J = 0;
(void)J;
for (MachineBasicBlock &B : *MF) {
for (MachineBasicBlock::instr_iterator I = B.instr_begin(),
EB = B.instr_end();
Expand All @@ -1640,8 +1638,7 @@ void MipsConstantIslands::prescanForConstants() {
case Mips::LwConstant32: {
PrescannedForConstants = true;
LLVM_DEBUG(dbgs() << "constant island constant " << *I << "\n");
J = I->getNumOperands();
LLVM_DEBUG(dbgs() << "num operands " << J << "\n");
LLVM_DEBUG(dbgs() << "num operands " << I->getNumOperands() << "\n");
MachineOperand& Literal = I->getOperand(1);
if (Literal.isImm()) {
int64_t V = Literal.getImm();
Expand Down

0 comments on commit 4bb4bb5

Please sign in to comment.