Skip to content

Commit 24c0af1

Browse files
committed
Replace FPVariant with APFloat::Semantics
1 parent ac562ed commit 24c0af1

File tree

8 files changed

+166
-155
lines changed

8 files changed

+166
-155
lines changed

llvm/include/llvm/CodeGenTypes/LowLevelType.h

Lines changed: 102 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
#ifndef LLVM_CODEGEN_LOWLEVELTYPE_H
2727
#define LLVM_CODEGEN_LOWLEVELTYPE_H
2828

29+
#include "llvm/ADT/APFloat.h"
2930
#include "llvm/ADT/DenseMapInfo.h"
31+
#include "llvm/ADT/bit.h"
3032
#include "llvm/CodeGenTypes/MachineValueType.h"
3133
#include "llvm/Support/Compiler.h"
3234
#include "llvm/Support/Debug.h"
35+
#include "llvm/Support/ErrorHandling.h"
3336
#include <cassert>
3437

3538
namespace llvm {
@@ -39,16 +42,7 @@ class raw_ostream;
3942

4043
class LLT {
4144
public:
42-
enum class FPVariant {
43-
IEEE_FLOAT,
44-
BF16, // BF16
45-
TENSOR_FLOAT32, // TENSOR_FLOAT32
46-
EXTENDED_FP80, // FP80
47-
PPC128_FLOAT, // PPC128_FLOAT
48-
VARIANT_FLOAT_5, // UNASSIGNED
49-
VARIANT_FLOAT_6, // UNASSIGNED
50-
VARIANT_FLOAT_7, // UNASSIGNED
51-
};
45+
using FpSemantics = APFloat::Semantics;
5246

5347
enum class Kind : uint8_t {
5448
INVALID,
@@ -91,38 +85,39 @@ class LLT {
9185
/// Get a low-level scalar or aggregate "bag of bits".
9286
static constexpr LLT scalar(unsigned SizeInBits) {
9387
return LLT{Kind::ANY_SCALAR, ElementCount::getFixed(0), SizeInBits,
94-
/*AddressSpace=*/0, static_cast<FPVariant>(0)};
88+
/*AddressSpace=*/0, static_cast<FpSemantics>(0)};
9589
}
9690

9791
static constexpr LLT integer(unsigned SizeInBits) {
9892
return LLT{Kind::INTEGER, ElementCount::getFixed(0), SizeInBits,
99-
/*AddressSpace=*/0, static_cast<FPVariant>(0)};
93+
/*AddressSpace=*/0, static_cast<FpSemantics>(0)};
10094
}
10195

102-
static constexpr LLT floatingPoint(unsigned SizeInBits, FPVariant FP) {
103-
return LLT{Kind::FLOAT, ElementCount::getFixed(0), SizeInBits,
104-
/*AddressSpace=*/0, FP};
96+
static LLT floatingPoint(const FpSemantics &Sem) {
97+
return LLT{Kind::FLOAT, ElementCount::getFixed(0),
98+
APFloat::getSizeInBits(APFloatBase::EnumToSemantics(Sem)),
99+
/*AddressSpace=*/0, Sem};
105100
}
106101

107102
/// Get a low-level token; just a scalar with zero bits (or no size).
108103
static constexpr LLT token() {
109104
return LLT{Kind::ANY_SCALAR, ElementCount::getFixed(0),
110105
/*SizeInBits=*/0,
111-
/*AddressSpace=*/0, static_cast<FPVariant>(0)};
106+
/*AddressSpace=*/0, static_cast<FpSemantics>(0)};
112107
}
113108

114109
/// Get a low-level pointer in the given address space.
115110
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits) {
116111
assert(SizeInBits > 0 && "invalid pointer size");
117112
return LLT{Kind::POINTER, ElementCount::getFixed(0), SizeInBits,
118-
AddressSpace, static_cast<FPVariant>(0)};
113+
AddressSpace, static_cast<FpSemantics>(0)};
119114
}
120115

121116
/// Get a low-level vector of some number of elements and element width.
122117
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits) {
123118
assert(!EC.isScalar() && "invalid number of vector elements");
124119
return LLT{Kind::VECTOR_ANY, EC, ScalarSizeInBits,
125-
/*AddressSpace=*/0, static_cast<FPVariant>(0)};
120+
/*AddressSpace=*/0, static_cast<FpSemantics>(0)};
126121
}
127122

128123
/// Get a low-level vector of some number of elements and element type.
@@ -133,41 +128,62 @@ class LLT {
133128
Kind Info = toVector(ScalarTy.Info);
134129
return LLT{Info, EC, ScalarTy.getSizeInBits().getFixedValue(),
135130
ScalarTy.isPointer() ? ScalarTy.getAddressSpace() : 0,
136-
ScalarTy.isFloat() ? ScalarTy.getFPVariant()
137-
: static_cast<FPVariant>(0)};
131+
ScalarTy.isFloat() ? ScalarTy.getFpSemantics()
132+
: static_cast<FpSemantics>(0)};
133+
}
134+
135+
static constexpr LLT floatIEEE(unsigned SizeInBits) {
136+
switch (SizeInBits) {
137+
default:
138+
llvm_unreachable("Wrong SizeInBits for IEEE Floating point!");
139+
case 16:
140+
return float16();
141+
case 32:
142+
return float32();
143+
case 64:
144+
return float64();
145+
case 128:
146+
return float128();
147+
}
138148
}
139149

140150
// Get a bfloat16 value.
141-
static constexpr LLT bfloat16() { return floatingPoint(16, FPVariant::BF16); }
142-
151+
static constexpr LLT bfloat16() {
152+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 16, 0,
153+
FpSemantics::S_BFloat};
154+
}
143155
/// Get a 16-bit IEEE half value.
144156
static constexpr LLT float16() {
145-
return floatingPoint(16, FPVariant::IEEE_FLOAT);
157+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 16, 0,
158+
FpSemantics::S_IEEEhalf};
146159
}
147-
148160
/// Get a 32-bit IEEE float value.
149161
static constexpr LLT float32() {
150-
return floatingPoint(32, FPVariant::IEEE_FLOAT);
162+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 32, 0,
163+
FpSemantics::S_IEEEsingle};
151164
}
152-
153165
/// Get a 64-bit IEEE double value.
154166
static constexpr LLT float64() {
155-
return floatingPoint(64, FPVariant::IEEE_FLOAT);
167+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 64, 0,
168+
FpSemantics::S_IEEEdouble};
156169
}
157170

158171
/// Get a 80-bit X86 floating point value.
159172
static constexpr LLT x86fp80() {
160-
return floatingPoint(80, FPVariant::EXTENDED_FP80);
173+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 80, 0,
174+
FpSemantics::S_x87DoubleExtended};
161175
}
162176

163177
/// Get a 128-bit IEEE quad value.
164178
static constexpr LLT float128() {
165-
return floatingPoint(128, FPVariant::IEEE_FLOAT);
179+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 128, 0,
180+
FpSemantics::S_IEEEquad};
166181
}
167182

168183
/// Get a 128-bit PowerPC double double value.
169184
static constexpr LLT ppcf128() {
170-
return floatingPoint(128, FPVariant::PPC128_FLOAT);
185+
return LLT{Kind::FLOAT, ElementCount::getFixed(0), 128, 0,
186+
FpSemantics::S_PPCDoubleDouble};
171187
}
172188

173189
/// Get a low-level fixed-width vector of some number of elements and element
@@ -209,70 +225,70 @@ class LLT {
209225
}
210226

211227
explicit constexpr LLT(Kind Info, ElementCount EC, uint64_t SizeInBits,
212-
unsigned AddressSpace, FPVariant FP)
228+
unsigned AddressSpace, FpSemantics Sem)
213229
: LLT() {
214-
init(Info, EC, SizeInBits, AddressSpace, FP);
230+
init(Info, EC, SizeInBits, AddressSpace, Sem);
215231
}
216232

217233
LLVM_ABI explicit LLT(MVT VT, bool AllowExtendedLLT = false);
218234
explicit constexpr LLT() : RawData(0), Info(static_cast<Kind>(0)) {}
219235

236+
constexpr bool isToken() const {
237+
return Info == Kind::ANY_SCALAR && RawData == 0;
238+
}
220239
constexpr bool isValid() const { return isToken() || RawData != 0; }
240+
constexpr bool isAnyScalar() const { return Info == Kind::ANY_SCALAR; }
241+
constexpr bool isInteger() const { return Info == Kind::INTEGER; }
242+
constexpr bool isFloat() const { return Info == Kind::FLOAT; }
243+
constexpr bool isPointer() const { return Info == Kind::POINTER; }
244+
constexpr bool isVectorAny() const { return Info == Kind::VECTOR_ANY; }
245+
constexpr bool isIntegerVector() const {
246+
return Info == Kind::VECTOR_INTEGER;
247+
}
248+
constexpr bool isFloatVector() const { return Info == Kind::VECTOR_FLOAT; }
249+
constexpr bool isPointerVector() const {
250+
return Info == Kind::VECTOR_POINTER;
251+
}
252+
constexpr bool isPointerOrPointerVector() const {
253+
return isPointer() || isPointerVector();
254+
}
255+
221256
constexpr bool isScalar() const {
222257
return Info == Kind::ANY_SCALAR || Info == Kind::INTEGER ||
223258
Info == Kind::FLOAT;
224259
}
225260
constexpr bool isScalar(unsigned Size) const {
226261
return isScalar() && getScalarSizeInBits() == Size;
227262
}
228-
constexpr bool isFloat() const { return Info == Kind::FLOAT; }
229-
constexpr bool isFloat(unsigned Size) const {
230-
return isFloat() && getScalarSizeInBits() == Size;
263+
constexpr bool isVector() const {
264+
return Info == Kind::VECTOR_ANY || Info == Kind::VECTOR_INTEGER ||
265+
Info == Kind::VECTOR_FLOAT || Info == Kind::VECTOR_POINTER;
231266
}
232-
constexpr bool isVariantFloat() const {
233-
return isFloat() && getFPVariant() != FPVariant::IEEE_FLOAT;
267+
268+
constexpr bool isInteger(unsigned Size) const {
269+
return isInteger() && getScalarSizeInBits() == Size;
234270
}
235-
constexpr bool isVariantFloat(FPVariant Variant) const {
236-
return isFloat() && getFPVariant() == Variant;
271+
272+
constexpr bool isFloat(unsigned Size) const {
273+
return isFloat() && getScalarSizeInBits() == Size;
237274
}
238-
constexpr bool isVariantFloat(unsigned Size, FPVariant Variant) const {
239-
return isVariantFloat(Variant) && getScalarSizeInBits() == Size;
275+
constexpr bool isFloatWithSem(FpSemantics Sem) const {
276+
return isFloat() && getFpSemantics() == Sem;
240277
}
241-
constexpr bool isFloatVector() const { return Info == Kind::VECTOR_FLOAT; }
242-
constexpr bool isIEEEFloat(unsigned Size) const {
243-
return isVariantFloat(Size, FPVariant::IEEE_FLOAT);
278+
constexpr bool isFloatIEEE() const {
279+
return isFloatWithSem(APFloatBase::S_IEEEhalf) ||
280+
isFloatWithSem(APFloatBase::S_IEEEsingle) ||
281+
isFloatWithSem(APFloatBase::S_IEEEdouble) ||
282+
isFloatWithSem(APFloatBase::S_IEEEquad);
244283
}
245-
constexpr bool isBFloat(unsigned Size) const {
246-
return isVariantFloat(Size, FPVariant::BF16);
284+
constexpr bool isBFloat16() const {
285+
return isFloatWithSem(FpSemantics::S_BFloat);
247286
}
248287
constexpr bool isX86FP80() const {
249-
return isVariantFloat(80, FPVariant::EXTENDED_FP80);
288+
return isFloatWithSem(FpSemantics::S_x87DoubleExtended);
250289
}
251290
constexpr bool isPPCF128() const {
252-
return isVariantFloat(128, FPVariant::PPC128_FLOAT);
253-
}
254-
constexpr bool isToken() const {
255-
return Info == Kind::ANY_SCALAR && RawData == 0;
256-
}
257-
constexpr bool isAnyScalar() const { return Info == Kind::ANY_SCALAR; }
258-
constexpr bool isVectorAny() const { return Info == Kind::VECTOR_ANY; }
259-
constexpr bool isInteger() const { return Info == Kind::INTEGER; }
260-
constexpr bool isInteger(unsigned Size) const {
261-
return isInteger() && getScalarSizeInBits() == Size;
262-
}
263-
constexpr bool isIntegerVector() const {
264-
return Info == Kind::VECTOR_INTEGER;
265-
}
266-
constexpr bool isVector() const {
267-
return Info == Kind::VECTOR_ANY || Info == Kind::VECTOR_INTEGER ||
268-
Info == Kind::VECTOR_FLOAT || Info == Kind::VECTOR_POINTER;
269-
}
270-
constexpr bool isPointer() const { return Info == Kind::POINTER; }
271-
constexpr bool isPointerVector() const {
272-
return Info == Kind::VECTOR_POINTER;
273-
}
274-
constexpr bool isPointerOrPointerVector() const {
275-
return isPointer() || isPointerVector();
291+
return isFloatWithSem(FpSemantics::S_PPCDoubleDouble);
276292
}
277293

278294
/// Returns the number of elements in a vector LLT. Must only be called on
@@ -333,11 +349,10 @@ class LLT {
333349
return isVector() ? getElementType() : *this;
334350
}
335351

336-
constexpr FPVariant getFPVariant() const {
352+
constexpr FpSemantics getFpSemantics() const {
337353
assert((isFloat() || isFloatVector()) &&
338354
"cannot get FP info for non float type");
339-
340-
return FPVariant(getFieldValue(FPFieldInfo));
355+
return FpSemantics(getFieldValue(FpSemanticFieldInfo));
341356
}
342357

343358
constexpr Kind getKind() const { return Info; }
@@ -427,7 +442,7 @@ class LLT {
427442
return pointer(getAddressSpace(), getScalarSizeInBits());
428443

429444
if (isFloatVector())
430-
return floatingPoint(getScalarSizeInBits(), getFPVariant());
445+
return floatingPoint(getFpSemantics());
431446

432447
if (isIntegerVector())
433448
return integer(getScalarSizeInBits());
@@ -453,7 +468,8 @@ class LLT {
453468

454469
constexpr bool operator==(const LLT &RHS) const {
455470
if (isAnyScalar() || RHS.isAnyScalar())
456-
return isScalar() == RHS.isScalar() && RawData == RHS.RawData;
471+
return isScalar() == RHS.isScalar() &&
472+
getScalarSizeInBits() == RHS.getScalarSizeInBits();
457473

458474
if (isVector() && RHS.isVector())
459475
return getElementType() == RHS.getElementType() &&
@@ -488,15 +504,15 @@ class LLT {
488504
.... ........ ........ ........ .... (2)
489505
**** ******** **** (3)
490506
~~~~ ~~~~~~~~ ~~~~~~~~ ~~~~ (4)
491-
### (5)
507+
#### #### (5)
492508
^^^^ ^^^^^^^^ ^^^^ (6)
493509
@ (7)
494510
495511
(1) Kind: [63:60]
496512
(2) ScalarSize: [59:28]
497513
(3) PointerSize: [59:44]
498514
(4) PointerAddressSpace: [43:20]
499-
(5) FPVariant: [22:20]
515+
(5) FpSemantics: [27:20]
500516
(6) VectorElements: [19:4]
501517
(7) VectorScalable: [0:0]
502518
@@ -510,7 +526,7 @@ class LLT {
510526
/// * Non-pointer scalar (isPointer == 0 && isVector == 0):
511527
/// Info: [63:60];
512528
/// SizeOfElement: [59:28];
513-
/// FPVariant: [22:20];
529+
/// FpSemantics: [27:20];
514530
///
515531
/// * Pointer (isPointer == 1 && isVector == 0):
516532
/// Info: [63:60];
@@ -520,7 +536,7 @@ class LLT {
520536
/// * Vector-of-non-pointer (isPointer == 0 && isVector == 1):
521537
/// Info: [63:60]
522538
/// SizeOfElement: [59:28];
523-
/// FPVariant: [22:20];
539+
/// FpSemantics: [27:20];
524540
/// VectorElements: [19:4];
525541
/// Scalable: [0:0];
526542
///
@@ -533,9 +549,10 @@ class LLT {
533549

534550
/// BitFieldInfo: {Size, Offset}
535551
typedef int BitFieldInfo[2];
552+
static_assert(bit_width_constexpr((uint32_t)APFloat::S_MaxSemantics) <= 8);
536553
static constexpr BitFieldInfo VectorScalableFieldInfo{1, 0};
537554
static constexpr BitFieldInfo VectorElementsFieldInfo{16, 4};
538-
static constexpr BitFieldInfo FPFieldInfo{3, 20};
555+
static constexpr BitFieldInfo FpSemanticFieldInfo{8, 20};
539556
static constexpr BitFieldInfo PointerAddressSpaceFieldInfo{24, 20};
540557
static constexpr BitFieldInfo ScalarSizeFieldInfo{32, 28};
541558
static constexpr BitFieldInfo PointerSizeFieldInfo{16, 44};
@@ -562,7 +579,7 @@ class LLT {
562579
}
563580

564581
constexpr void init(Kind Info, ElementCount EC, uint64_t SizeInBits,
565-
unsigned AddressSpace, FPVariant FP) {
582+
unsigned AddressSpace, FpSemantics Sem) {
566583
assert(SizeInBits <= std::numeric_limits<unsigned>::max() &&
567584
"Not enough bits in LLT to represent size");
568585
this->Info = Info;
@@ -571,7 +588,7 @@ class LLT {
571588
maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
572589
} else {
573590
RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo) |
574-
maskAndShift((uint64_t)FP, FPFieldInfo);
591+
maskAndShift((uint64_t)Sem, FpSemanticFieldInfo);
575592
}
576593

577594
if (Info == Kind::VECTOR_ANY || Info == Kind::VECTOR_INTEGER ||
@@ -587,7 +604,7 @@ class LLT {
587604
}
588605
};
589606

590-
inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
607+
inline raw_ostream &operator<<(raw_ostream &OS, const LLT &Ty) {
591608
Ty.print(OS);
592609
return OS;
593610
}
@@ -607,9 +624,7 @@ template <> struct DenseMapInfo<LLT> {
607624
uint64_t Val = Ty.getUniqueRAWLLTData();
608625
return DenseMapInfo<uint64_t>::getHashValue(Val);
609626
}
610-
static bool isEqual(const LLT &LHS, const LLT &RHS) {
611-
return LHS == RHS;
612-
}
627+
static bool isEqual(const LLT &LHS, const LLT &RHS) { return LHS == RHS; }
613628
};
614629

615630
} // namespace llvm

0 commit comments

Comments
 (0)