Skip to content

Commit ea1e62d

Browse files
authored
[CodeGenTypes] Remove explicit VT numbers from ValueTypes.td (#169670)
Remove explicit VT numbers from ValueTypes.td so that patches that add a new VT do not have to renumber the entire file. In TableGen VTs are now identified by ValueType.LLVMName instead of ValueType.Value. This is important for target-defined types (typically based on PtrValueType) which are not mentioned in ValueTypes.td itself.
1 parent fca41f4 commit ea1e62d

File tree

7 files changed

+344
-339
lines changed

7 files changed

+344
-339
lines changed

llvm/include/llvm/CodeGen/ValueTypes.td

Lines changed: 306 additions & 313 deletions
Large diffs are not rendered by default.

llvm/include/llvm/CodeGenTypes/MachineValueType.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ namespace llvm {
4040
// are considered extended value types.
4141
INVALID_SIMPLE_VALUE_TYPE = 0,
4242

43-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
44-
Ty = n,
43+
#define GET_VT_ATTR(Ty, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) Ty,
4544
#define GET_VT_RANGES
4645
#include "llvm/CodeGen/GenVT.inc"
4746
#undef GET_VT_ATTR
@@ -187,7 +186,7 @@ namespace llvm {
187186
/// Return true if this is an overloaded type for TableGen.
188187
bool isOverloaded() const {
189188
switch (SimpleTy) {
190-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
189+
#define GET_VT_ATTR(Ty, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
191190
case Ty: \
192191
return Any;
193192
#include "llvm/CodeGen/GenVT.inc"
@@ -270,7 +269,7 @@ namespace llvm {
270269
MVT getVectorElementType() const {
271270
assert(SimpleTy >= FIRST_VALUETYPE && SimpleTy <= LAST_VALUETYPE);
272271
static constexpr SimpleValueType EltTyTable[] = {
273-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
272+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
274273
EltTy,
275274
#include "llvm/CodeGen/GenVT.inc"
276275
#undef GET_VT_ATTR
@@ -284,7 +283,7 @@ namespace llvm {
284283
unsigned getVectorMinNumElements() const {
285284
assert(SimpleTy >= FIRST_VALUETYPE && SimpleTy <= LAST_VALUETYPE);
286285
static constexpr uint16_t NElemTable[] = {
287-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
286+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
288287
NElem,
289288
#include "llvm/CodeGen/GenVT.inc"
290289
#undef GET_VT_ATTR
@@ -314,7 +313,7 @@ namespace llvm {
314313
/// base size.
315314
TypeSize getSizeInBits() const {
316315
static constexpr TypeSize SizeTable[] = {
317-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
316+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
318317
TypeSize(Sz, Sc || Tup || Ty == aarch64svcount /* FIXME: Not in the td. \
319318
*/),
320319
#include "llvm/CodeGen/GenVT.inc"
@@ -437,7 +436,7 @@ namespace llvm {
437436
}
438437

439438
static MVT getFloatingPointVT(unsigned BitWidth) {
440-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
439+
#define GET_VT_ATTR(Ty, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
441440
if (FP == 3 && sz == BitWidth) \
442441
return Ty;
443442
#include "llvm/CodeGen/GenVT.inc"
@@ -447,7 +446,7 @@ namespace llvm {
447446
}
448447

449448
static MVT getIntegerVT(unsigned BitWidth) {
450-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
449+
#define GET_VT_ATTR(Ty, sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
451450
if (Int == 3 && sz == BitWidth) \
452451
return Ty;
453452
#include "llvm/CodeGen/GenVT.inc"
@@ -477,7 +476,7 @@ namespace llvm {
477476
}
478477

479478
static MVT getRISCVVectorTupleVT(unsigned Sz, unsigned NFields) {
480-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, Tup, NF, nElem, EltTy) \
479+
#define GET_VT_ATTR(Ty, sz, Any, Int, FP, Vec, Sc, Tup, NF, nElem, EltTy) \
481480
if (Tup && sz == Sz && NF == NFields) \
482481
return Ty;
483482
#include "llvm/CodeGen/GenVT.inc"
@@ -491,8 +490,7 @@ namespace llvm {
491490
assert(isRISCVVectorTuple() && SimpleTy >= FIRST_VALUETYPE &&
492491
SimpleTy <= LAST_VALUETYPE);
493492
static constexpr uint8_t NFTable[] = {
494-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
495-
NF,
493+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) NF,
496494
#include "llvm/CodeGen/GenVT.inc"
497495
#undef GET_VT_ATTR
498496
};

llvm/include/llvm/Target/Target.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class HwModeSelect<list<HwMode> Ms, int ObjectsLength> {
6161
// objects could be used. This is specifically applicable to selection
6262
// patterns.
6363
class ValueTypeByHwMode<list<HwMode> Ms, list<ValueType> Ts>
64-
: HwModeSelect<Ms, !size(Ts)>, ValueType<0, 0> {
64+
: HwModeSelect<Ms, !size(Ts)>, ValueType<0, "INVALID_SIMPLE_VALUE_TYPE"> {
6565
// The length of this list must be the same as the length of Ms.
6666
list<ValueType> Objects = Ts;
6767
}
@@ -73,7 +73,7 @@ class ValueTypeByHwMode<list<HwMode> Ms, list<ValueType> Ts>
7373
// patterns.
7474
class PtrValueTypeByHwMode<ValueTypeByHwMode scalar, int addrspace>
7575
: HwModeSelect<scalar.Modes, !size(scalar.Objects)>,
76-
PtrValueType<ValueType<0, 0>, addrspace> {
76+
PtrValueType<ValueType<0, "INVALID_SIMPLE_VALUE_TYPE">, addrspace> {
7777
// The length of this list must be the same as the length of Ms.
7878
list<ValueType> Objects = scalar.Objects;
7979
}

llvm/lib/Target/SPIRV/SPIRVRegisterInfo.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let Namespace = "SPIRV" in {
1515
def p64 : PtrValueType <i64, 0>;
1616

1717
class VTPtrVec<int nelem, PtrValueType ptr>
18-
: VTVec<nelem, ValueType<ptr.Size, ptr.Value>, ptr.Value> {
18+
: VTVec<nelem, ValueType<ptr.Size, ptr.LLVMName>, ptr.LLVMName> {
1919
int isPointer = true;
2020
}
2121

llvm/utils/TableGen/Basic/VTEmitter.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ void VTEmitter::run(raw_ostream &OS) {
9191
emitSourceFileHeader("ValueTypes Source Fragment", OS, Records);
9292

9393
std::vector<const Record *> VTsByNumber{512};
94-
for (auto *VT : Records.getAllDerivedDefinitions("ValueType")) {
95-
auto Number = VT->getValueAsInt("Value");
96-
assert(0 <= Number && Number < (int)VTsByNumber.size() &&
97-
"ValueType should be uint16_t");
98-
assert(!VTsByNumber[Number] && "Duplicate ValueType");
94+
unsigned Number = 0;
95+
std::vector<const Record *> Defs(
96+
Records.getAllDerivedDefinitions("ValueType"));
97+
// Emit VTs in the order they were declared so that VTRanges stay contiguous.
98+
llvm::sort(Defs, LessRecordByID());
99+
for (auto *VT : Defs) {
100+
++Number;
101+
assert(Number < VTsByNumber.size() && "ValueType should be uint16_t");
99102
VTsByNumber[Number] = VT;
100103
}
101104

@@ -120,13 +123,12 @@ void VTEmitter::run(raw_ostream &OS) {
120123
}
121124
};
122125

123-
OS << "#ifdef GET_VT_ATTR // (Ty, n, sz, Any, Int, FP, Vec, Sc, Tup, NF, "
126+
OS << "#ifdef GET_VT_ATTR // (Ty, sz, Any, Int, FP, Vec, Sc, Tup, NF, "
124127
"NElem, EltTy)\n";
125128
for (const auto *VT : VTsByNumber) {
126129
if (!VT)
127130
continue;
128131
auto Name = VT->getValueAsString("LLVMName");
129-
auto Value = VT->getValueAsInt("Value");
130132
bool IsInteger = VT->getValueAsBit("isInteger");
131133
bool IsFP = VT->getValueAsBit("isFP");
132134
bool IsVector = VT->getValueAsBit("isVector");
@@ -158,7 +160,6 @@ void VTEmitter::run(raw_ostream &OS) {
158160
// clang-format off
159161
OS << " GET_VT_ATTR("
160162
<< Name << ", "
161-
<< Value << ", "
162163
<< VT->getValueAsInt("Size") << ", "
163164
<< VT->getValueAsBit("isOverloaded") << ", "
164165
<< (IsInteger ? Name[0] == 'i' ? 3 : 1 : 0) << ", "

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "CodeGenRegisters.h"
1919
#include "CodeGenSchedule.h"
2020
#include "llvm/ADT/STLExtras.h"
21+
#include "llvm/ADT/StringSwitch.h"
2122
#include "llvm/ADT/Twine.h"
2223
#include "llvm/Support/CommandLine.h"
2324
#include "llvm/Support/ErrorHandling.h"
@@ -42,15 +43,21 @@ static cl::opt<unsigned>
4243
/// Returns the MVT that the specified TableGen
4344
/// record corresponds to.
4445
MVT llvm::getValueType(const Record *Rec) {
45-
return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
46+
return StringSwitch<MVT>(Rec->getValueAsString("LLVMName"))
47+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
48+
.Case(#Ty, MVT::Ty)
49+
#include "llvm/CodeGen/GenVT.inc"
50+
#undef GET_VT_ATTR
51+
.Case("INVALID_SIMPLE_VALUE_TYPE", MVT::INVALID_SIMPLE_VALUE_TYPE);
4652
}
4753

4854
StringRef llvm::getEnumName(MVT T) {
4955
// clang-format off
5056
switch (T.SimpleTy) {
51-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
57+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
5258
case MVT::Ty: return "MVT::" # Ty;
5359
#include "llvm/CodeGen/GenVT.inc"
60+
#undef GET_VT_ATTR
5461
default: llvm_unreachable("ILLEGAL VALUE TYPE!");
5562
}
5663
// clang-format on

mlir/tools/mlir-tblgen/LLVMIRIntrinsicGen.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "mlir/TableGen/GenInfo.h"
1515

1616
#include "llvm/ADT/SmallBitVector.h"
17+
#include "llvm/ADT/StringSwitch.h"
1718
#include "llvm/CodeGenTypes/MachineValueType.h"
1819
#include "llvm/Support/CommandLine.h"
1920
#include "llvm/Support/PrettyStackTrace.h"
@@ -60,8 +61,13 @@ using IndicesTy = llvm::SmallBitVector;
6061

6162
/// Return a CodeGen value type entry from a type record.
6263
static llvm::MVT::SimpleValueType getValueType(const Record *rec) {
63-
return (llvm::MVT::SimpleValueType)rec->getValueAsDef("VT")->getValueAsInt(
64-
"Value");
64+
return StringSwitch<llvm::MVT::SimpleValueType>(
65+
rec->getValueAsDef("VT")->getValueAsString("LLVMName"))
66+
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
67+
.Case(#Ty, llvm::MVT::Ty)
68+
#include "llvm/CodeGen/GenVT.inc"
69+
#undef GET_VT_ATTR
70+
.Case("INVALID_SIMPLE_VALUE_TYPE", llvm::MVT::INVALID_SIMPLE_VALUE_TYPE);
6571
}
6672

6773
/// Return the indices of the definitions in a list of definitions that

0 commit comments

Comments
 (0)