Skip to content

Commit d862a1a

Browse files
committed
Add ifdefs to avoid compiling float128 when the host does not have fp128
1 parent d959ed9 commit d862a1a

File tree

8 files changed

+41
-40
lines changed

8 files changed

+41
-40
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ class IEEEFloat final : public APFloatBase {
300300
IEEEFloat(const fltSemantics &, integerPart);
301301
IEEEFloat(const fltSemantics &, uninitializedTag);
302302
IEEEFloat(const fltSemantics &, const APInt &);
303+
#ifdef __FLOAT128__
303304
explicit IEEEFloat(float128 ld);
305+
#endif
304306
explicit IEEEFloat(double d);
305307
explicit IEEEFloat(float f);
306308
IEEEFloat(const IEEEFloat &);
@@ -356,7 +358,9 @@ class IEEEFloat final : public APFloatBase {
356358
Expected<opStatus> convertFromString(StringRef, roundingMode);
357359
APInt bitcastToAPInt() const;
358360
double convertToDouble() const;
361+
#ifdef __FLOAT128__
359362
float128 convertToQuad() const;
363+
#endif
360364
float convertToFloat() const;
361365

362366
/// @}
@@ -945,7 +949,9 @@ class APFloat : public APFloatBase {
945949
APFloat(const fltSemantics &Semantics, uninitializedTag)
946950
: U(Semantics, uninitialized) {}
947951
APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {}
952+
#ifdef __FLOAT128__
948953
explicit APFloat(float128 ld) : U(IEEEFloat(ld), IEEEquad()) {}
954+
#endif
949955
explicit APFloat(double d) : U(IEEEFloat(d), IEEEdouble()) {}
950956
explicit APFloat(float f) : U(IEEEFloat(f), IEEEsingle()) {}
951957
APFloat(const APFloat &RHS) = default;
@@ -1227,7 +1233,9 @@ class APFloat : public APFloatBase {
12271233
/// \pre The APFloat must be built using semantics, that can be represented by
12281234
/// the host float type without loss of precision. It can be IEEEquad and
12291235
/// shorter semantics, like IEEEdouble and others.
1236+
#ifdef __FLOAT128__
12301237
float128 convertToQuad() const;
1238+
#endif
12311239

12321240
/// Converts this APFloat to host float value.
12331241
///

llvm/include/llvm/ADT/APInt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,10 +1671,12 @@ class [[nodiscard]] APInt {
16711671
/// any bit width. Exactly 64 bits will be translated.
16721672
double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
16731673

1674+
#ifdef __FLOAT128__
16741675
float128 bitsToQuad() const {
16751676
__uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
16761677
return llvm::bit_cast<float128>(ul);
16771678
}
1679+
#endif
16781680

16791681
/// Converts APInt bits to a float
16801682
///
@@ -1701,13 +1703,15 @@ class [[nodiscard]] APInt {
17011703
return APInt(sizeof(float) * CHAR_BIT, llvm::bit_cast<uint32_t>(V));
17021704
}
17031705

1706+
#ifdef __FLOAT128__
17041707
static APInt longDoubleToBits(float128 V) {
17051708
const uint64_t Words[2] = {
17061709
static_cast<uint64_t>(V),
17071710
static_cast<uint64_t>(llvm::bit_cast<__uint128_t>(V) >> 64),
17081711
};
17091712
return APInt(sizeof(float128) * CHAR_BIT, 2, Words);
17101713
}
1714+
#endif
17111715

17121716
/// @}
17131717
/// \name Mathematics Operations

llvm/include/llvm/IR/Constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ class ConstantFP final : public ConstantData {
290290
/// host double and as the target format.
291291
static Constant *get(Type *Ty, double V);
292292

293+
#ifdef __FLOAT128__
293294
static Constant *get128(Type *Ty, float128 V);
295+
#endif
294296

295297
/// If Ty is a vector type, return a Constant with a splat of the given
296298
/// value. Otherwise return a ConstantFP for the given value.

llvm/include/llvm/Support/float128.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
#ifndef LLVM_FLOAT128
1010
#define LLVM_FLOAT128
1111

12-
#if defined(__clang__)
12+
#if defined(__clang__) && defined(__FLOAT128__)
1313
typedef __float128 float128;
14-
#elif defined(__GNUC__) || defined(__GNUG__)
14+
#elif defined(__FLOAT128__) && (defined(__GNUC__) || defined(__GNUG__))
1515
typedef _Float128 float128;
16-
#else
17-
typedef long double float128;
1816
#endif
1917

2018
#endif // LLVM_FLOAT128

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,8 +1674,7 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
16741674
Name == "fmod" || Name == "fmodf";
16751675
case 'l':
16761676
return Name == "log" || Name == "logf" || Name == "log2" ||
1677-
Name == "log2f" || Name == "log10" || Name == "log10f" ||
1678-
Name == "logl";
1677+
Name == "log2f" || Name == "log10" || Name == "log10f";
16791678
case 'n':
16801679
return Name == "nearbyint" || Name == "nearbyintf";
16811680
case 'p':
@@ -1758,15 +1757,6 @@ inline bool llvm_fenv_testexcept() {
17581757
return false;
17591758
}
17601759

1761-
Constant *ConstantFoldLogf128(const APFloat &V, Type *Ty) {
1762-
#ifdef HAS_LOGF128
1763-
float128 l = logf128(V.convertToQuad());
1764-
return ConstantFP::get128(Ty, l);
1765-
#else
1766-
return nullptr;
1767-
#endif
1768-
}
1769-
17701760
Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
17711761
Type *Ty) {
17721762
llvm_fenv_clearexcept();
@@ -2214,8 +2204,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
22142204
switch (IntrinsicID) {
22152205
default: break;
22162206
case Intrinsic::log:
2217-
if (Ty->isFP128Ty())
2218-
return ConstantFoldLogf128(APF, Ty);
2207+
#if defined(__FLOAT128__) && defined (HAS_LOGF128)
2208+
if (Ty->isFP128Ty()){
2209+
return ConstantFP::get(Ty, logf128(APF.convertToQuad()));
2210+
}
2211+
#endif
22192212
return ConstantFoldFP(log, APF, Ty);
22202213
case Intrinsic::log2:
22212214
// TODO: What about hosts that lack a C99 library?
@@ -2345,11 +2338,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
23452338
if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
23462339
return ConstantFoldFP(log, APF, Ty);
23472340
break;
2348-
case LibFunc_logl:
2349-
if (!APF.isNegative() && !APF.isZero() && TLI->has(Func) &&
2350-
Ty->isFP128Ty())
2351-
return ConstantFoldLogf128(APF, Ty);
2352-
break;
23532341
case LibFunc_log2:
23542342
case LibFunc_log2f:
23552343
case LibFunc_log2_finite:

llvm/lib/IR/Constants.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ Constant *ConstantFP::get(Type *Ty, double V) {
976976
return C;
977977
}
978978

979+
#ifdef __FLOAT128__
979980
Constant *ConstantFP::get128(Type *Ty, float128 V) {
980981
LLVMContext &Context = Ty->getContext();
981982

@@ -991,6 +992,7 @@ Constant *ConstantFP::get128(Type *Ty, float128 V) {
991992

992993
return C;
993994
}
995+
#endif
994996

995997
Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
996998
ConstantFP *C = get(Ty->getContext(), V);

llvm/lib/Support/APFloat.cpp

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

3673+
#ifdef __FLOAT128__
36733674
float128 IEEEFloat::convertToQuad() const {
36743675
assert(semantics == (const llvm::fltSemantics *)&semIEEEquad &&
36753676
"Float semantics are not IEEEquads");
36763677
APInt api = bitcastToAPInt();
36773678
return api.bitsToQuad();
36783679
}
3680+
#endif
36793681

36803682
/// Integer bit is explicit in this format. Intel hardware (387 and later)
36813683
/// does not support these bit patterns:
@@ -3965,9 +3967,11 @@ IEEEFloat::IEEEFloat(double d) {
39653967
initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d));
39663968
}
39673969

3970+
#ifdef __FLOAT128__
39683971
IEEEFloat::IEEEFloat(float128 ld) {
39693972
initFromAPInt(&semIEEEquad, APInt::longDoubleToBits(ld));
39703973
}
3974+
#endif
39713975

39723976
namespace {
39733977
void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
@@ -5276,6 +5280,7 @@ double APFloat::convertToDouble() const {
52765280
return Temp.getIEEE().convertToDouble();
52775281
}
52785282

5283+
#ifdef __FLOAT128__
52795284
float128 APFloat::convertToQuad() const {
52805285
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
52815286
return getIEEE().convertToQuad();
@@ -5288,6 +5293,7 @@ float128 APFloat::convertToQuad() const {
52885293
(void)St;
52895294
return Temp.getIEEE().convertToQuad();
52905295
}
5296+
#endif
52915297

52925298
float APFloat::convertToFloat() const {
52935299
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)

llvm/unittests/Analysis/ConstantLogf128.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ using namespace llvm;
1717

1818
namespace {
1919

20-
class ConstantFoldLogf128Fixture
21-
: public ::testing ::TestWithParam<std::string> {
22-
protected:
23-
std::string FuncName;
24-
};
25-
26-
TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) {
20+
TEST(ConstantFoldLogf128Fixture, ConstantFoldLogf128) {
21+
#ifdef __FLOAT128__
2722
LLVMContext Context;
2823
IRBuilder<> Builder(Context);
2924
Module MainModule("Logf128TestModule", Context);
@@ -40,15 +35,13 @@ TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) {
4035
FunctionType *FP128FP128Prototype =
4136
FunctionType::get(FP128Ty, {FP128Ty}, false);
4237
Constant *Constant2L = ConstantFP::get128(FP128Ty, 2.0L);
43-
44-
std::string FunctionName = GetParam();
45-
Function *Logl = Function::Create(
46-
FP128FP128Prototype, Function::ExternalLinkage, FunctionName, MainModule);
47-
CallInst *LoglCall = Builder.CreateCall(Logl, Constant2L);
38+
Function *Logf128 = Function::Create(
39+
FP128FP128Prototype, Function::ExternalLinkage, "llvm.log.f128", MainModule);
40+
CallInst *Logf128Call = Builder.CreateCall(Logf128, Constant2L);
4841

4942
TargetLibraryInfoImpl TLII(Triple(MainModule.getTargetTriple()));
5043
TargetLibraryInfo TLI(TLII, Logf128TestFunction);
51-
Constant *FoldResult = ConstantFoldCall(LoglCall, Logl, Constant2L, &TLI);
44+
Constant *FoldResult = ConstantFoldCall(Logf128Call, Logf128, Constant2L, &TLI);
5245

5346
#ifndef HAS_LOGF128
5447
ASSERT_TRUE(FoldResult == nullptr);
@@ -64,11 +57,11 @@ TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) {
6457
EXPECT_GT(Size, 0U);
6558

6659
ASSERT_STREQ(LongDoubleHexString,
67-
std::string("0X1.62E42FEFA39E0000000000000000000P-1").c_str());
60+
std::string("0X1.62E42FEFA39EF000000000000000000P-1").c_str());
61+
#endif //HAS_LOGF128
62+
#else // __FLOAT128__
63+
ASSERT_TRUE(true);
6864
#endif
6965
}
7066

71-
INSTANTIATE_TEST_SUITE_P(ConstantFoldLogf128, ConstantFoldLogf128Fixture,
72-
::testing::Values("logl", "llvm.log.f128"));
73-
74-
} // end anonymous namespace
67+
}

0 commit comments

Comments
 (0)