Skip to content

Commit a4c6ebe

Browse files
authored
[MVT][TableGen] Extend Machine Value Type to uint16_t (#99657)
RFC: https://discourse.llvm.org/t/rfc-extend-machine-value-type-from-uint8-t-to-uint16-t/80274 compile-time-tracker: https://llvm-compile-time-tracker.com/compare.php?from=4b9fab591916eec9fd1942f37afe3b137b564089&to=177d28247efe5a4d59a8d8150b4daf01e4f57d74&stat=wall-time Currently 208 out of 256 MVTs are used, it will be run out soon, so ultimately we need to extend the original `MVT::SimpleValueType` from `uint8_t` to `uint16_t` to accomodate more types. The `MatcherTable` uses `unsigned char` for encoding the matcher code, so the extended MVTs are no longer fit into the table, thus we need to use VBR to encode them as we do on others that are wider than 8 bits. The statistics below shows the difference of "Total Array size" of the matcher table that appears in every files: ``` Table Before After Change(%) WebAssemblyGenDAGISel.inc 23576 23775 0.844 NVPTXGenDAGISel.inc 173498 173498 0 RISCVGenDAGISel.inc 2179121 2369929 8.756 AVRGenDAGISel.inc 2754 2754 0 PPCGenDAGISel.inc 163315 163617 0.185 MipsGenDAGISel.inc 47280 47447 0.353 SystemZGenDAGISel.inc 56243 56461 0.388 AArch64GenDAGISel.inc 467893 487830 4.261 MSP430GenDAGISel.inc 8069 8069 0 LoongArchGenDAGISel.inc 78928 79131 0.257 XCoreGenDAGISel.inc 3432 3432 0 BPFGenDAGISel.inc 3733 3733 0 VEGenDAGISel.inc 65174 66456 1.967 LanaiGenDAGISel.inc 2067 2067 0 X86GenDAGISel.inc 628787 636987 1.304 ARMGenDAGISel.inc 170968 171036 0.040 HexagonGenDAGISel.inc 155764 155764 0 SparcGenDAGISel.inc 5762 5798 0.625 AMDGPUGenDAGISel.inc 504356 504463 0.021 R600GenDAGISel.inc 29785 29785 0 ``` The statistics below shows the runtime peak memory usage by compiling a simple C program: `/bin/time -v clang -target $TARGET -O3 -c test.c` ``` int test(int a) { return a * 3; } ``` ``` Target Before(kbytes) After(kbytes) Change(%) wasm64 110172 110088 -0.076 nvptx64 109784 109980 0.179 riscv64 114020 113656 -0.319 avr 110352 110068 -0.257 ppc64 112612 112476 -0.120 mips64 113588 113668 0.070 systemz 110860 110760 -0.090 aarch64 113704 113432 -0.239 msp430 110284 110200 -0.076 loongarch64 111052 110756 -0.267 xcore 108340 108020 -0.295 bpf 110620 110708 0.080 ve 110960 110920 -0.036 lanai 110180 109960 -0.200 x86_64 113640 113304 -0.296 arm64 113540 113172 -0.324 hexagon 114620 114684 0.056 sparc 110412 110136 -0.250 amdgcn 118164 117144 -0.863 r600 111200 110508 -0.622 ```
1 parent 9718f3d commit a4c6ebe

File tree

7 files changed

+108
-65
lines changed

7 files changed

+108
-65
lines changed

llvm/include/llvm/CodeGen/ValueTypes.td

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,34 +289,34 @@ def aarch64svcount
289289
def spirvbuiltin : ValueType<0, 200>; // SPIR-V's builtin type
290290

291291
let isNormalValueType = false in {
292-
def token : ValueType<0, 248>; // TokenTy
293-
def MetadataVT : ValueType<0, 249> { // Metadata
292+
def token : ValueType<0, 16376>; // TokenTy
293+
def MetadataVT : ValueType<0, 16377> { // Metadata
294294
let LLVMName = "Metadata";
295295
}
296296

297297
// Pseudo valuetype mapped to the current pointer size to any address space.
298298
// Should only be used in TableGen.
299-
def iPTRAny : VTAny<250>;
299+
def iPTRAny : VTAny<16378>;
300300

301301
// Pseudo valuetype to represent "vector of any size"
302302
// Should only be used in TableGen.
303-
def vAny : VTAny<251>;
303+
def vAny : VTAny<16379>;
304304

305305
// Pseudo valuetype to represent "float of any format"
306306
// Should only be used in TableGen.
307-
def fAny : VTAny<252>;
307+
def fAny : VTAny<16380>;
308308

309309
// Pseudo valuetype to represent "integer of any bit width"
310310
// Should only be used in TableGen.
311-
def iAny : VTAny<253>;
311+
def iAny : VTAny<16381>;
312312

313313
// Pseudo valuetype mapped to the current pointer size.
314314
// Should only be used in TableGen.
315-
def iPTR : ValueType<0, 254>;
315+
def iPTR : ValueType<0, 16382>;
316316

317317
// Pseudo valuetype to represent "any type of any size".
318318
// Should only be used in TableGen.
319-
def Any : VTAny<255>;
319+
def Any : VTAny<16383>;
320320

321321
} // isNormalValueType = false
322322

llvm/include/llvm/CodeGenTypes/MachineValueType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace llvm {
3333
/// type can be represented by an MVT.
3434
class MVT {
3535
public:
36-
enum SimpleValueType : uint8_t {
36+
enum SimpleValueType : uint16_t {
3737
// Simple value types that aren't explicitly part of this enumeration
3838
// are considered extended value types.
3939
INVALID_SIMPLE_VALUE_TYPE = 0,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,17 @@ GetVBR(uint64_t Val, const unsigned char *MatcherTable, unsigned &Idx) {
25962596
return Val;
25972597
}
25982598

2599+
/// getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value,
2600+
/// use GetVBR to decode it.
2601+
LLVM_ATTRIBUTE_ALWAYS_INLINE static MVT::SimpleValueType
2602+
getSimpleVT(const unsigned char *MatcherTable, unsigned &MatcherIndex) {
2603+
unsigned SimpleVT = MatcherTable[MatcherIndex++];
2604+
if (SimpleVT & 128)
2605+
SimpleVT = GetVBR(SimpleVT, MatcherTable, MatcherIndex);
2606+
2607+
return static_cast<MVT::SimpleValueType>(SimpleVT);
2608+
}
2609+
25992610
void SelectionDAGISel::Select_JUMP_TABLE_DEBUG_INFO(SDNode *N) {
26002611
SDLoc dl(N);
26012612
CurDAG->SelectNodeTo(N, TargetOpcode::JUMP_TABLE_DEBUG_INFO, MVT::Glue,
@@ -2875,8 +2886,7 @@ CheckChild2CondCode(const unsigned char *MatcherTable, unsigned &MatcherIndex,
28752886
LLVM_ATTRIBUTE_ALWAYS_INLINE static bool
28762887
CheckValueType(const unsigned char *MatcherTable, unsigned &MatcherIndex,
28772888
SDValue N, const TargetLowering *TLI, const DataLayout &DL) {
2878-
MVT::SimpleValueType VT =
2879-
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
2889+
MVT::SimpleValueType VT = getSimpleVT(MatcherTable, MatcherIndex);
28802890
if (cast<VTSDNode>(N)->getVT() == VT)
28812891
return true;
28822892

@@ -3006,17 +3016,16 @@ static unsigned IsPredicateKnownToFail(const unsigned char *Table,
30063016
VT = MVT::i64;
30073017
break;
30083018
default:
3009-
VT = static_cast<MVT::SimpleValueType>(Table[Index++]);
3019+
VT = getSimpleVT(Table, Index);
30103020
break;
30113021
}
30123022
Result = !::CheckType(VT, N, SDISel.TLI, SDISel.CurDAG->getDataLayout());
30133023
return Index;
30143024
}
30153025
case SelectionDAGISel::OPC_CheckTypeRes: {
30163026
unsigned Res = Table[Index++];
3017-
Result = !::CheckType(static_cast<MVT::SimpleValueType>(Table[Index++]),
3018-
N.getValue(Res), SDISel.TLI,
3019-
SDISel.CurDAG->getDataLayout());
3027+
Result = !::CheckType(getSimpleVT(Table, Index), N.getValue(Res),
3028+
SDISel.TLI, SDISel.CurDAG->getDataLayout());
30203029
return Index;
30213030
}
30223031
case SelectionDAGISel::OPC_CheckChild0Type:
@@ -3054,7 +3063,7 @@ static unsigned IsPredicateKnownToFail(const unsigned char *Table,
30543063
VT = MVT::i64;
30553064
ChildNo = Opcode - SelectionDAGISel::OPC_CheckChild0TypeI64;
30563065
} else {
3057-
VT = static_cast<MVT::SimpleValueType>(Table[Index++]);
3066+
VT = getSimpleVT(Table, Index);
30583067
ChildNo = Opcode - SelectionDAGISel::OPC_CheckChild0Type;
30593068
}
30603069
Result = !::CheckChildType(VT, N, SDISel.TLI,
@@ -3558,7 +3567,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
35583567
VT = MVT::i64;
35593568
break;
35603569
default:
3561-
VT = static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3570+
VT = getSimpleVT(MatcherTable, MatcherIndex);
35623571
break;
35633572
}
35643573
if (!::CheckType(VT, N, TLI, CurDAG->getDataLayout()))
@@ -3567,9 +3576,8 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
35673576

35683577
case OPC_CheckTypeRes: {
35693578
unsigned Res = MatcherTable[MatcherIndex++];
3570-
if (!::CheckType(
3571-
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]),
3572-
N.getValue(Res), TLI, CurDAG->getDataLayout()))
3579+
if (!::CheckType(getSimpleVT(MatcherTable, MatcherIndex), N.getValue(Res),
3580+
TLI, CurDAG->getDataLayout()))
35733581
break;
35743582
continue;
35753583
}
@@ -3616,8 +3624,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
36163624
CaseSize = GetVBR(CaseSize, MatcherTable, MatcherIndex);
36173625
if (CaseSize == 0) break;
36183626

3619-
MVT CaseVT =
3620-
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3627+
MVT CaseVT = getSimpleVT(MatcherTable, MatcherIndex);
36213628
if (CaseVT == MVT::iPTR)
36223629
CaseVT = TLI->getPointerTy(CurDAG->getDataLayout());
36233630

@@ -3673,7 +3680,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
36733680
VT = MVT::i64;
36743681
ChildNo = Opcode - SelectionDAGISel::OPC_CheckChild0TypeI64;
36753682
} else {
3676-
VT = static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3683+
VT = getSimpleVT(MatcherTable, MatcherIndex);
36773684
ChildNo = Opcode - SelectionDAGISel::OPC_CheckChild0Type;
36783685
}
36793686
if (!::CheckChildType(VT, N, TLI, CurDAG->getDataLayout(), ChildNo))
@@ -3767,7 +3774,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
37673774
VT = MVT::i64;
37683775
break;
37693776
default:
3770-
VT = static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3777+
VT = getSimpleVT(MatcherTable, MatcherIndex);
37713778
break;
37723779
}
37733780
int64_t Val = MatcherTable[MatcherIndex++];
@@ -3791,7 +3798,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
37913798
VT = MVT::i64;
37923799
break;
37933800
default:
3794-
VT = static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3801+
VT = getSimpleVT(MatcherTable, MatcherIndex);
37953802
break;
37963803
}
37973804
unsigned RegNo = MatcherTable[MatcherIndex++];
@@ -3803,8 +3810,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
38033810
// For targets w/ more than 256 register names, the register enum
38043811
// values are stored in two bytes in the matcher table (just like
38053812
// opcodes).
3806-
MVT::SimpleValueType VT =
3807-
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
3813+
MVT::SimpleValueType VT = getSimpleVT(MatcherTable, MatcherIndex);
38083814
unsigned RegNo = MatcherTable[MatcherIndex++];
38093815
RegNo |= MatcherTable[MatcherIndex++] << 8;
38103816
RecordedNodes.push_back(std::pair<SDValue, SDNode*>(
@@ -4042,8 +4048,7 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
40424048
NumVTs = MatcherTable[MatcherIndex++];
40434049
SmallVector<EVT, 4> VTs;
40444050
for (unsigned i = 0; i != NumVTs; ++i) {
4045-
MVT::SimpleValueType VT =
4046-
static_cast<MVT::SimpleValueType>(MatcherTable[MatcherIndex++]);
4051+
MVT::SimpleValueType VT = getSimpleVT(MatcherTable, MatcherIndex);
40474052
if (VT == MVT::iPTR)
40484053
VT = TLI->getPointerTy(CurDAG->getDataLayout()).SimpleTy;
40494054
VTs.push_back(VT);

llvm/test/TableGen/dag-isel-regclass-emit-enum.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def GPRAbove127 : RegisterClass<"TestTarget", [i32], 32,
2727
// CHECK-NEXT: OPC_CheckChild1Integer, 0,
2828
// CHECK-NEXT: OPC_EmitInteger32, 0|128,2/*256*/,
2929
// CHECK-NEXT: OPC_MorphNodeTo1None, TARGET_VAL(TargetOpcode::COPY_TO_REGCLASS),
30-
// CHECK-NEXT: MVT::i32, 2/*#Ops*/, 1, 0,
30+
// CHECK-NEXT: /*MVT::i32*/7, 2/*#Ops*/, 1, 0,
3131
def : Pat<(i32 (add i32:$src, (i32 0))),
3232
(COPY_TO_REGCLASS GPRAbove127, GPR0:$src)>;
3333

3434
// CHECK: OPC_CheckChild1Integer, 2,
3535
// CHECK-NEXT: OPC_EmitStringInteger32, TestNamespace::GPR127RegClassID,
3636
// CHECK-NEXT: OPC_MorphNodeTo1None, TARGET_VAL(TargetOpcode::COPY_TO_REGCLASS),
37-
// CHECK-NEXT: MVT::i32, 2/*#Ops*/, 1, 0,
37+
// CHECK-NEXT: /*MVT::i32*/7, 2/*#Ops*/, 1, 0,
3838
def : Pat<(i32 (add i32:$src, (i32 1))),
3939
(COPY_TO_REGCLASS GPR127, GPR0:$src)>;

llvm/utils/TableGen/Common/CodeGenDAGPatterns.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ class CodeGenDAGPatterns;
4848
using TreePatternNodePtr = IntrusiveRefCntPtr<TreePatternNode>;
4949

5050
/// This represents a set of MVTs. Since the underlying type for the MVT
51-
/// is uint8_t, there are at most 256 values. To reduce the number of memory
51+
/// is uint16_t, there are at most 65536 values. To reduce the number of memory
5252
/// allocations and deallocations, represent the set as a sequence of bits.
5353
/// To reduce the allocations even further, make MachineValueTypeSet own
5454
/// the storage and use std::array as the bit container.
5555
struct MachineValueTypeSet {
5656
static_assert(std::is_same<std::underlying_type_t<MVT::SimpleValueType>,
57-
uint8_t>::value,
58-
"Change uint8_t here to the SimpleValueType's type");
59-
static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max() + 1;
57+
uint16_t>::value,
58+
"Change uint16_t here to the SimpleValueType's type");
59+
static unsigned constexpr Capacity = std::numeric_limits<uint16_t>::max() + 1;
6060
using WordType = uint64_t;
6161
static unsigned constexpr WordWidth = CHAR_BIT * sizeof(WordType);
6262
static unsigned constexpr NumWords = Capacity / WordWidth;

0 commit comments

Comments
 (0)