Skip to content

Commit ef71226

Browse files
authored
[LLVM][TableGen] Change WebAsm Emitter to use const RecordKeeper (#109051)
Change WebAssemblyDisassemblerEmitter to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
1 parent 2bb3621 commit ef71226

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ class BitsInit final : public TypedInit, public FoldingSetNode,
603603

604604
Init *convertInitializerTo(RecTy *Ty) const override;
605605
Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
606+
std::optional<int64_t> convertInitializerToInt() const;
606607

607608
bool isComplete() const override {
608609
for (unsigned i = 0; i != getNumBits(); ++i)

llvm/lib/TableGen/Record.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,18 +497,24 @@ Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
497497
}
498498

499499
if (isa<IntRecTy>(Ty)) {
500-
int64_t Result = 0;
501-
for (unsigned i = 0, e = getNumBits(); i != e; ++i)
502-
if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
503-
Result |= static_cast<int64_t>(Bit->getValue()) << i;
504-
else
505-
return nullptr;
506-
return IntInit::get(getRecordKeeper(), Result);
500+
std::optional<int64_t> Result = convertInitializerToInt();
501+
if (Result)
502+
return IntInit::get(getRecordKeeper(), *Result);
507503
}
508504

509505
return nullptr;
510506
}
511507

508+
std::optional<int64_t> BitsInit::convertInitializerToInt() const {
509+
int64_t Result = 0;
510+
for (unsigned i = 0, e = getNumBits(); i != e; ++i)
511+
if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
512+
Result |= static_cast<int64_t>(Bit->getValue()) << i;
513+
else
514+
return std::nullopt;
515+
return Result;
516+
}
517+
512518
Init *
513519
BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
514520
SmallVector<Init *, 16> NewBits(Bits.size());

llvm/utils/TableGen/WebAssemblyDisassemblerEmitter.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,23 @@
1919
#include "llvm/Support/raw_ostream.h"
2020
#include "llvm/TableGen/Record.h"
2121

22-
namespace llvm {
23-
2422
static constexpr int WebAssemblyInstructionTableSize = 256;
2523

26-
void emitWebAssemblyDisassemblerTables(
24+
void llvm::emitWebAssemblyDisassemblerTables(
2725
raw_ostream &OS,
28-
const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
26+
ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
2927
// First lets organize all opcodes by (prefix) byte. Prefix 0 is the
3028
// starting table.
3129
std::map<unsigned,
3230
std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
3331
OpcodeTable;
3432
for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
35-
auto &CGI = *NumberedInstructions[I];
36-
auto &Def = *CGI.TheDef;
33+
const CodeGenInstruction &CGI = *NumberedInstructions[I];
34+
const Record &Def = *CGI.TheDef;
3735
if (!Def.getValue("Inst"))
3836
continue;
39-
auto &Inst = *Def.getValueAsBitsInit("Inst");
40-
RecordKeeper &RK = Inst.getRecordKeeper();
41-
unsigned Opc = static_cast<unsigned>(
42-
cast<IntInit>(Inst.convertInitializerTo(IntRecTy::get(RK)))
43-
->getValue());
37+
const BitsInit &Inst = *Def.getValueAsBitsInit("Inst");
38+
unsigned Opc = static_cast<unsigned>(*Inst.convertInitializerToInt());
4439
if (Opc == 0xFFFFFFFF)
4540
continue; // No opcode defined.
4641
assert(Opc <= 0xFFFFFF);
@@ -97,14 +92,14 @@ void emitWebAssemblyDisassemblerTables(
9792
OS << "};\n\n";
9893
std::vector<std::string> OperandTable, CurOperandList;
9994
// Output one table per prefix.
100-
for (auto &PrefixPair : OpcodeTable) {
101-
if (PrefixPair.second.empty())
95+
for (const auto &[Prefix, Table] : OpcodeTable) {
96+
if (Table.empty())
10297
continue;
103-
OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
98+
OS << "WebAssemblyInstruction InstructionTable" << Prefix;
10499
OS << "[] = {\n";
105100
for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
106-
auto InstIt = PrefixPair.second.find(I);
107-
if (InstIt != PrefixPair.second.end()) {
101+
auto InstIt = Table.find(I);
102+
if (InstIt != Table.end()) {
108103
// Regular instruction.
109104
assert(InstIt->second.second);
110105
auto &CGI = *InstIt->second.second;
@@ -144,7 +139,7 @@ void emitWebAssemblyDisassemblerTables(
144139
} else {
145140
auto PrefixIt = OpcodeTable.find(I);
146141
// If we have a non-empty table for it that's not 0, this is a prefix.
147-
if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
142+
if (PrefixIt != OpcodeTable.end() && I && !Prefix) {
148143
OS << " { 0, ET_Prefix, 0, 0";
149144
} else {
150145
OS << " { 0, ET_Unused, 0, 0";
@@ -163,15 +158,11 @@ void emitWebAssemblyDisassemblerTables(
163158
// Create a table of all extension tables:
164159
OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
165160
OS << "PrefixTable[] = {\n";
166-
for (auto &PrefixPair : OpcodeTable) {
167-
if (PrefixPair.second.empty() || !PrefixPair.first)
161+
for (const auto &[Prefix, Table] : OpcodeTable) {
162+
if (Table.empty() || !Prefix)
168163
continue;
169-
OS << " { " << PrefixPair.first << ", InstructionTable"
170-
<< PrefixPair.first;
171-
OS << " },\n";
164+
OS << " { " << Prefix << ", InstructionTable" << Prefix << " },\n";
172165
}
173166
OS << " { 0, nullptr }\n};\n\n";
174167
OS << "} // end namespace llvm\n";
175168
}
176-
177-
} // namespace llvm

llvm/utils/TableGen/WebAssemblyDisassemblerEmitter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class CodeGenInstruction;
2222
class raw_ostream;
2323

2424
void emitWebAssemblyDisassemblerTables(
25-
raw_ostream &OS,
26-
const ArrayRef<const CodeGenInstruction *> &NumberedInstructions);
25+
raw_ostream &OS, ArrayRef<const CodeGenInstruction *> NumberedInstructions);
2726

2827
} // namespace llvm
2928

0 commit comments

Comments
 (0)