Skip to content

Commit abedb3b

Browse files
authored
[CodeGenTypes] Speed up getVectorElementType and getVectorMinNumElements (#95282)
Implement MVT::getVectorElementType and MVT::getVectorMinNumElements with table lookup instead of switch. This speeds up "check-llvm-codegen-amdgpu" by about 7% in my Release build.
1 parent 987b59a commit abedb3b

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

llvm/include/llvm/CodeGenTypes/MachineValueType.h

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace llvm {
3838
// are considered extended value types.
3939
INVALID_SIMPLE_VALUE_TYPE = 0,
4040

41-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc) Ty = n,
41+
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, NElem, EltTy) Ty = n,
4242
#define GET_VT_RANGES
4343
#include "llvm/CodeGen/GenVT.inc"
4444
#undef GET_VT_ATTR
@@ -171,9 +171,9 @@ namespace llvm {
171171
/// Return true if this is an overloaded type for TableGen.
172172
bool isOverloaded() const {
173173
switch (SimpleTy) {
174-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc) \
175-
case Ty: \
176-
return Any;
174+
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, NElem, EltTy) \
175+
case Ty: \
176+
return Any;
177177
#include "llvm/CodeGen/GenVT.inc"
178178
#undef GET_VT_ATTR
179179
default:
@@ -252,30 +252,28 @@ namespace llvm {
252252
}
253253

254254
MVT getVectorElementType() const {
255-
switch (SimpleTy) {
256-
default:
257-
llvm_unreachable("Not a vector MVT!");
258-
259-
#define GET_VT_VECATTR(Ty, Sc, nElem, ElTy, ElSz) \
260-
case Ty: \
261-
return ElTy;
255+
assert(SimpleTy >= FIRST_VALUETYPE && SimpleTy <= LAST_VALUETYPE);
256+
static constexpr SimpleValueType EltTyTable[] = {
257+
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, NElem, EltTy) EltTy,
262258
#include "llvm/CodeGen/GenVT.inc"
263-
#undef GET_VT_VECATTR
264-
}
259+
#undef GET_VT_ATTR
260+
};
261+
SimpleValueType VT = EltTyTable[SimpleTy - FIRST_VALUETYPE];
262+
assert(VT != INVALID_SIMPLE_VALUE_TYPE && "Not a vector MVT!");
263+
return VT;
265264
}
266265

267266
/// Given a vector type, return the minimum number of elements it contains.
268267
unsigned getVectorMinNumElements() const {
269-
switch (SimpleTy) {
270-
default:
271-
llvm_unreachable("Not a vector MVT!");
272-
273-
#define GET_VT_VECATTR(Ty, Sc, nElem, ElTy, ElSz) \
274-
case Ty: \
275-
return nElem;
268+
assert(SimpleTy >= FIRST_VALUETYPE && SimpleTy <= LAST_VALUETYPE);
269+
static constexpr uint16_t NElemTable[] = {
270+
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, NElem, EltTy) NElem,
276271
#include "llvm/CodeGen/GenVT.inc"
277-
#undef GET_VT_VECATTR
278-
}
272+
#undef GET_VT_ATTR
273+
};
274+
unsigned NElem = NElemTable[SimpleTy - FIRST_VALUETYPE];
275+
assert(NElem != 0 && "Not a vector MVT!");
276+
return NElem;
279277
}
280278

281279
ElementCount getVectorElementCount() const {
@@ -298,8 +296,8 @@ namespace llvm {
298296
/// base size.
299297
TypeSize getSizeInBits() const {
300298
static constexpr TypeSize SizeTable[] = {
301-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc) \
302-
TypeSize(Sz, Sc || Ty == aarch64svcount /* FIXME: Not in the td. */),
299+
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, NElem, EltTy) \
300+
TypeSize(Sz, Sc || Ty == aarch64svcount /* FIXME: Not in the td. */),
303301
#include "llvm/CodeGen/GenVT.inc"
304302
#undef GET_VT_ATTR
305303
};
@@ -420,19 +418,19 @@ namespace llvm {
420418
}
421419

422420
static MVT getFloatingPointVT(unsigned BitWidth) {
423-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc) \
424-
if (FP == 3 && sz == BitWidth) \
425-
return Ty;
421+
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, NElem, EltTy) \
422+
if (FP == 3 && sz == BitWidth) \
423+
return Ty;
426424
#include "llvm/CodeGen/GenVT.inc"
427425
#undef GET_VT_ATTR
428426

429427
llvm_unreachable("Bad bit width!");
430428
}
431429

432430
static MVT getIntegerVT(unsigned BitWidth) {
433-
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc) \
434-
if (Int == 3 && sz == BitWidth) \
435-
return Ty;
431+
#define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc, NElem, EltTy) \
432+
if (Int == 3 && sz == BitWidth) \
433+
return Ty;
436434
#include "llvm/CodeGen/GenVT.inc"
437435
#undef GET_VT_ATTR
438436

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ StringRef llvm::getName(MVT::SimpleValueType T) {
6363
StringRef llvm::getEnumName(MVT::SimpleValueType T) {
6464
// clang-format off
6565
switch (T) {
66-
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc) \
66+
#define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, NElem, EltTy) \
6767
case MVT::Ty: return "MVT::" # Ty;
6868
#include "llvm/CodeGen/GenVT.inc"
6969
default: llvm_unreachable("ILLEGAL VALUE TYPE!");

llvm/utils/TableGen/VTEmitter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ void VTEmitter::run(raw_ostream &OS) {
7373
bool IsVector = VT->getValueAsBit("isVector");
7474
bool IsScalable = VT->getValueAsBit("isScalable");
7575
bool IsNormalValueType = VT->getValueAsBit("isNormalValueType");
76+
int64_t NElem = IsVector ? VT->getValueAsInt("nElem") : 0;
77+
StringRef EltName = IsVector ? VT->getValueAsDef("ElementType")->getName()
78+
: "INVALID_SIMPLE_VALUE_TYPE";
7679

7780
UpdateVTRange("INTEGER_FIXEDLEN_VECTOR_VALUETYPE", Name,
7881
IsInteger && IsVector && !IsScalable);
@@ -97,7 +100,9 @@ void VTEmitter::run(raw_ostream &OS) {
97100
<< (IsInteger ? Name[0] == 'i' ? 3 : 1 : 0) << ", "
98101
<< (IsFP ? Name[0] == 'f' ? 3 : 1 : 0) << ", "
99102
<< IsVector << ", "
100-
<< IsScalable << ")\n";
103+
<< IsScalable << ", "
104+
<< NElem << ", "
105+
<< EltName << ")\n";
101106
// clang-format on
102107
}
103108
OS << "#endif\n\n";

0 commit comments

Comments
 (0)