Skip to content

Commit 478b4e1

Browse files
z30050559zhengwentao (C)
authored andcommitted
[TableGen] Introduce a less aggressive suppression for HwMode DecoderTable
1. Remove 'AllModes' and 'DefaultMode' suffixes for DecoderTables under default HwMode. 2. Introduce a less aggressive suppression for HwMode DecoderTable, only reduce necessary tables duplications. This allows encodings under different HwModes to retain the original DecoderNamespace. 3. Change 'suppress-per-hwmode-duplicates' command option from bool type to enum type, allowing users to choose what level of suppression to use.
1 parent 631248d commit 478b4e1

File tree

3 files changed

+132
-47
lines changed

3 files changed

+132
-47
lines changed

llvm/test/TableGen/HwModeEncodeDecode2.td

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: llvm-tblgen -gen-disassembler -I %p/../../include %s | \
22
// RUN: FileCheck %s --check-prefix=DECODER
3-
// RUN: llvm-tblgen -gen-disassembler --suppress-per-hwmode-duplicates -I \
3+
// RUN: llvm-tblgen -gen-disassembler --suppress-per-hwmode-duplicates=O2 -I \
44
// RUN: %p/../../include %s | FileCheck %s --check-prefix=DECODER-SUPPRESS
55

66
// Test duplicate table suppression for per-HwMode decoders.
@@ -105,11 +105,10 @@ let OutOperandList = (outs) in {
105105
// DECODER-DAG: Opcode: fooTypeEncA:baz
106106
// DECODER-DAG: Opcode: bar
107107

108-
109-
// DECODER-SUPPRESS-LABEL: DecoderTableAlt_AllModes32[] =
110-
// DECODER-SUPPRESS-DAG: Opcode: unrelated
111-
// DECODER-SUPPRESS-LABEL: DecoderTable_AllModes32[] =
108+
// DECODER-SUPPRESS-LABEL: DecoderTable32[] =
112109
// DECODER-SUPPRESS-DAG: Opcode: bar
110+
// DECODER-SUPPRESS-LABEL: DecoderTableAlt32[] =
111+
// DECODER-SUPPRESS-DAG: Opcode: unrelated
113112
// DECODER-SUPPRESS-LABEL: DecoderTable_ModeA32[] =
114113
// DECODER-SUPPRESS-DAG: Opcode: fooTypeEncA:foo
115114
// DECODER-SUPPRESS-NOT: Opcode: bar

llvm/test/TableGen/HwModeEncodeDecode3.td

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// RUN: FileCheck %s --check-prefix=ENCODER
33
// RUN: llvm-tblgen -gen-disassembler -I %p/../../include %s | \
44
// RUN: FileCheck %s --check-prefix=DECODER
5-
// RUN: llvm-tblgen -gen-disassembler --suppress-per-hwmode-duplicates -I \
6-
// RUN: %p/../../include %s | FileCheck %s --check-prefix=DECODER-SUPPRESS
5+
// RUN: llvm-tblgen -gen-disassembler --suppress-per-hwmode-duplicates=O1 -I \
6+
// RUN: %p/../../include %s | FileCheck %s --check-prefix=DECODER-SUPPRESS-O1
7+
// RUN: llvm-tblgen -gen-disassembler --suppress-per-hwmode-duplicates=O2 -I \
8+
// RUN: %p/../../include %s | FileCheck %s --check-prefix=DECODER-SUPPRESS-O2
79

810
include "llvm/Target/Target.td"
911

@@ -99,16 +101,20 @@ def unrelated: Instruction {
99101
}
100102

101103

102-
// DECODER-LABEL: DecoderTableAlt_DefaultMode32[] =
104+
// Under default settings, using 'HwMode' to dictate instruction encodings results in
105+
// significant duplication of DecoderTables. The three tables ‘DecoderTableAlt32’,
106+
// ‘DecoderTableAlt_ModeA32’, and ‘DecoderTableAlt_ModeB32’ are exact duplicates and
107+
// could effectively be merged into one.
108+
// DECODER-LABEL: DecoderTable32[] =
109+
// DECODER-DAG: Opcode: bar
110+
// DECODER-LABEL: DecoderTable64[] =
111+
// DECODER-DAG: Opcode: fooTypeEncDefault:foo
112+
// DECODER-LABEL: DecoderTableAlt32[] =
103113
// DECODER-DAG: Opcode: unrelated
104114
// DECODER-LABEL: DecoderTableAlt_ModeA32[] =
105115
// DECODER-DAG: Opcode: unrelated
106116
// DECODER-LABEL: DecoderTableAlt_ModeB32[] =
107117
// DECODER-DAG: Opcode: unrelated
108-
// DECODER-LABEL: DecoderTable_DefaultMode32[] =
109-
// DECODER-DAG: Opcode: bar
110-
// DECODER-LABEL: DecoderTable_DefaultMode64[] =
111-
// DECODER-DAG: Opcode: fooTypeEncDefault:foo
112118
// DECODER-LABEL: DecoderTable_ModeA32[] =
113119
// DECODER-DAG: Opcode: fooTypeEncA:foo
114120
// DECODER-DAG: Opcode: bar
@@ -117,21 +123,42 @@ def unrelated: Instruction {
117123
// DECODER-DAG: Opcode: fooTypeEncA:baz
118124
// DECODER-DAG: Opcode: bar
119125

120-
121-
// DECODER-SUPPRESS-LABEL: DecoderTableAlt_AllModes32[] =
122-
// DECODER-SUPPRESS-DAG: Opcode: unrelated
123-
// DECODER-SUPPRESS-LABEL: DecoderTable_AllModes32[] =
124-
// DECODER-SUPPRESS-DAG: Opcode: bar
125-
// DECODER-SUPPRESS-LABEL: DecoderTable_DefaultMode64[] =
126-
// DECODER-SUPPRESS-NOT: Opcode: bar
127-
// DECODER-SUPPRESS-DAG: Opcode: fooTypeEncDefault:foo
128-
// DECODER-SUPPRESS-LABEL: DecoderTable_ModeA32[] =
129-
// DECODER-SUPPRESS-DAG: Opcode: fooTypeEncA:foo
130-
// DECODER-SUPPRESS-NOT: Opcode: bar
131-
// DECODER-SUPPRESS-LABEL: DecoderTable_ModeB32[] =
132-
// DECODER-SUPPRESS-DAG: Opcode: fooTypeEncB:foo
133-
// DECODER-SUPPRESS-DAG: Opcode: fooTypeEncA:baz
134-
// DECODER-SUPPRESS-NOT: Opcode: bar
126+
// Under the 'O1' optimization level, unnecessary duplicate tables will be eliminated,
127+
// reducing the three ‘Alt’ tables down to just one.
128+
// DECODER-SUPPRESS-O1-LABEL: DecoderTable32[] =
129+
// DECODER-SUPPRESS-O1-DAG: Opcode: bar
130+
// DECODER-SUPPRESS-O1-LABEL: DecoderTable64[] =
131+
// DECODER-SUPPRESS-O1-DAG: Opcode: fooTypeEncDefault:foo
132+
// DECODER-SUPPRESS-O1-LABEL: DecoderTableAlt32[] =
133+
// DECODER-SUPPRESS-O1-DAG: Opcode: unrelated
134+
// DECODER-SUPPRESS-O1-LABEL: DecoderTable_ModeA32[] =
135+
// DECODER-SUPPRESS-O1-DAG: Opcode: fooTypeEncA:foo
136+
// DECODER-SUPPRESS-O1-DAG: Opcode: bar
137+
// DECODER-SUPPRESS-O1-LABEL: DecoderTable_ModeB32[] =
138+
// DECODER-SUPPRESS-O1-DAG: Opcode: fooTypeEncB:foo
139+
// DECODER-SUPPRESS-O1-DAG: Opcode: fooTypeEncA:baz
140+
// DECODER-SUPPRESS-O1-DAG: Opcode: bar
141+
142+
// Under the 'O2' optimization condition, instructions possessing the 'EncodingByHwMode'
143+
// attribute will be extracted from their original DecoderNamespace and placed into their
144+
// respective HwMode tables. Meanwhile, other instructions that do not have the 'EncodingByHwMode'
145+
// attribute but are within the same DecoderNamespace will be stored in the 'Default' table. This
146+
// approach will significantly reduce instruction redundancy, but it necessitates users to thoroughly
147+
// consider the interplay between HwMode and DecoderNamespace for their instructions.
148+
// DECODER-SUPPRESS-O2-LABEL: DecoderTable32[] =
149+
// DECODER-SUPPRESS-O2-DAG: Opcode: bar
150+
// DECODER-SUPPRESS-O2-LABEL: DecoderTable64[] =
151+
// DECODER-SUPPRESS-O2-NOT: Opcode: bar
152+
// DECODER-SUPPRESS-O2-DAG: Opcode: fooTypeEncDefault:foo
153+
// DECODER-SUPPRESS-O2-LABEL: DecoderTableAlt32[] =
154+
// DECODER-SUPPRESS-O2-DAG: Opcode: unrelated
155+
// DECODER-SUPPRESS-O2-LABEL: DecoderTable_ModeA32[] =
156+
// DECODER-SUPPRESS-O2-DAG: Opcode: fooTypeEncA:foo
157+
// DECODER-SUPPRESS-O2-NOT: Opcode: bar
158+
// DECODER-SUPPRESS-O2-LABEL: DecoderTable_ModeB32[] =
159+
// DECODER-SUPPRESS-O2-DAG: Opcode: fooTypeEncB:foo
160+
// DECODER-SUPPRESS-O2-DAG: Opcode: fooTypeEncA:baz
161+
// DECODER-SUPPRESS-O2-NOT: Opcode: bar
135162

136163
// ENCODER-LABEL: static const uint64_t InstBits_DefaultMode[] = {
137164
// ENCODER: UINT64_C(2), // bar

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,23 @@ using namespace llvm;
5454

5555
extern cl::OptionCategory DisassemblerEmitterCat;
5656

57-
cl::opt<bool> DecoderEmitterSuppressDuplicates(
57+
enum SuppressLevel {
58+
SUPPRESSION_DISABLE,
59+
SUPPRESSION_LEVEL1,
60+
SUPPRESSION_LEVEL2
61+
};
62+
63+
cl::opt<SuppressLevel> DecoderEmitterSuppressDuplicates(
5864
"suppress-per-hwmode-duplicates",
5965
cl::desc("Suppress duplication of instrs into per-HwMode decoder tables"),
60-
cl::init(false), cl::cat(DisassemblerEmitterCat));
66+
cl::values(clEnumValN(SUPPRESSION_DISABLE, "O0",
67+
"Do not prevent DecoderTable duplications caused by HwModes"),
68+
clEnumValN(SUPPRESSION_LEVEL1, "O1",
69+
"Remove duplicate DecoderTable entries generated due to HwModes"),
70+
clEnumValN(SUPPRESSION_LEVEL2, "O2",
71+
"Extract HwModes-specific instructions into new DecoderTables, "
72+
"significantly reducing Table Duplications")),
73+
cl::init(SUPPRESSION_DISABLE), cl::cat(DisassemblerEmitterCat));
6174

6275
namespace {
6376

@@ -128,6 +141,8 @@ struct EncodingIDAndOpcode {
128141
};
129142

130143
using EncodingIDsVec = std::vector<EncodingIDAndOpcode>;
144+
using NamespacesHwModesMap =
145+
std::map<std::string, std::map<StringRef, unsigned>>;
131146

132147
raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) {
133148
if (Value.EncodingDef != Value.Inst->TheDef)
@@ -2417,21 +2432,63 @@ static bool Check(DecodeStatus &Out, DecodeStatus In) {
24172432

24182433
// Collect all HwModes referenced by the target for encoding purposes,
24192434
// returning a vector of corresponding names.
2420-
static void
2421-
collectHwModesReferencedForEncodings(const CodeGenHwModes &HWM,
2422-
std::vector<StringRef> &Names) {
2435+
static void collectHwModesReferencedForEncodings(
2436+
const CodeGenHwModes &HWM, std::vector<StringRef> &Names,
2437+
NamespacesHwModesMap &NamespacesWithHwModes) {
24232438
SmallBitVector BV(HWM.getNumModeIds());
24242439
for (const auto &MS : HWM.getHwModeSelects()) {
24252440
for (const HwModeSelect::PairType &P : MS.second.Items) {
2426-
if (P.second->isSubClassOf("InstructionEncoding"))
2441+
if (P.second->isSubClassOf("InstructionEncoding")) {
2442+
std::string DecoderNamespace =
2443+
std::string(P.second->getValueAsString("DecoderNamespace"));
2444+
if (P.first == DefaultMode) {
2445+
NamespacesWithHwModes[DecoderNamespace][""] = 1;
2446+
} else {
2447+
NamespacesWithHwModes[DecoderNamespace][HWM.getMode(P.first).Name] =
2448+
1;
2449+
}
24272450
BV.set(P.first);
2451+
}
24282452
}
24292453
}
24302454
transform(BV.set_bits(), std::back_inserter(Names), [&HWM](const int &M) {
2455+
if (M == DefaultMode)
2456+
return StringRef("");
24312457
return HWM.getModeName(M, /*IncludeDefault=*/true);
24322458
});
24332459
}
24342460

2461+
static void handleHwModesUnrelatedEncodings(
2462+
const CodeGenInstruction *Instr, const std::vector<StringRef> &HwModeNames,
2463+
NamespacesHwModesMap &NamespacesWithHwModes, std::vector<EncodingAndInst> &GlobalEncodings) {
2464+
const Record *InstDef = Instr->TheDef;
2465+
2466+
switch (DecoderEmitterSuppressDuplicates) {
2467+
case SUPPRESSION_DISABLE: {
2468+
for (StringRef HwModeName : HwModeNames)
2469+
GlobalEncodings.emplace_back(InstDef, Instr, HwModeName);
2470+
break;
2471+
}
2472+
case SUPPRESSION_LEVEL1: {
2473+
std::string DecoderNamespace = std::string(
2474+
InstDef->getValueAsString("DecoderNamespace"));
2475+
for (StringRef HwModeName : HwModeNames) {
2476+
if (NamespacesWithHwModes.count(DecoderNamespace) > 0 &&
2477+
NamespacesWithHwModes[DecoderNamespace].count(HwModeName) > 0) {
2478+
GlobalEncodings.emplace_back(InstDef, Instr, HwModeName);
2479+
} else {
2480+
GlobalEncodings.emplace_back(InstDef, Instr, "");
2481+
break;
2482+
}
2483+
}
2484+
break;
2485+
}
2486+
case SUPPRESSION_LEVEL2:
2487+
GlobalEncodings.emplace_back(InstDef, Instr, "");
2488+
break;
2489+
}
2490+
}
2491+
24352492
// Emits disassembler code for instruction decoding.
24362493
void DecoderEmitter::run(raw_ostream &o) {
24372494
formatted_raw_ostream OS(o);
@@ -2457,10 +2514,12 @@ namespace llvm {
24572514
// Parameterize the decoders based on namespace and instruction width.
24582515

24592516
// First, collect all encoding-related HwModes referenced by the target.
2517+
// And establish a mapping table between DecoderNamespace and HwMode.
24602518
// If HwModeNames is empty, add the empty string so we always have one HwMode.
24612519
const CodeGenHwModes &HWM = Target.getHwModes();
24622520
std::vector<StringRef> HwModeNames;
2463-
collectHwModesReferencedForEncodings(HWM, HwModeNames);
2521+
NamespacesHwModesMap NamespacesWithHwModes;
2522+
collectHwModesReferencedForEncodings(HWM, HwModeNames, NamespacesWithHwModes);
24642523
if (HwModeNames.empty())
24652524
HwModeNames.push_back("");
24662525

@@ -2471,22 +2530,22 @@ namespace llvm {
24712530
if (const RecordVal *RV = InstDef->getValue("EncodingInfos")) {
24722531
if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
24732532
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
2474-
for (auto &KV : EBM)
2475-
NumberedEncodings.emplace_back(
2476-
KV.second, NumberedInstruction,
2477-
HWM.getModeName(KV.first, /*IncludeDefault=*/true));
2533+
for (auto &[ModeId, Encoding] : EBM) {
2534+
// DecoderTables with DefaultMode should not have any suffix.
2535+
if (ModeId == DefaultMode) {
2536+
NumberedEncodings.emplace_back(Encoding, NumberedInstruction, "");
2537+
} else {
2538+
NumberedEncodings.emplace_back(Encoding, NumberedInstruction,
2539+
HWM.getMode(ModeId).Name);
2540+
}
2541+
}
24782542
continue;
24792543
}
24802544
}
2481-
// This instruction is encoded the same on all HwModes. Emit it for all
2482-
// HwModes by default, otherwise leave it in a single common table.
2483-
if (DecoderEmitterSuppressDuplicates) {
2484-
NumberedEncodings.emplace_back(InstDef, NumberedInstruction, "AllModes");
2485-
} else {
2486-
for (StringRef HwModeName : HwModeNames)
2487-
NumberedEncodings.emplace_back(InstDef, NumberedInstruction,
2488-
HwModeName);
2489-
}
2545+
// This instruction is encoded the same on all HwModes.
2546+
// According to user needs, provide varying degrees of suppression.
2547+
handleHwModesUnrelatedEncodings(NumberedInstruction, HwModeNames,
2548+
NamespacesWithHwModes, NumberedEncodings);
24902549
}
24912550
for (const auto &NumberedAlias :
24922551
RK.getAllDerivedDefinitions("AdditionalEncoding"))

0 commit comments

Comments
 (0)