Skip to content

Commit 6b6e210

Browse files
authored
[LLVM][TableGen] Change CodeGenMapTable to use const RecordKeeper (#109034)
Change CodeGenMapTable 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 8fc3ac4 commit 6b6e210

File tree

2 files changed

+84
-97
lines changed

2 files changed

+84
-97
lines changed

llvm/utils/TableGen/CodeGenMapTable.cpp

Lines changed: 83 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@
7777

7878
#include "Common/CodeGenInstruction.h"
7979
#include "Common/CodeGenTarget.h"
80+
#include "TableGenBackends.h"
81+
#include "llvm/ADT/StringExtras.h"
8082
#include "llvm/TableGen/Error.h"
8183
#include "llvm/TableGen/Record.h"
82-
using namespace llvm;
83-
typedef std::map<std::string, std::vector<Record *>> InstrRelMapTy;
8484

85-
typedef std::map<std::vector<Init *>, std::vector<Record *>> RowInstrMapTy;
85+
using namespace llvm;
86+
typedef std::map<std::string, std::vector<const Record *>> InstrRelMapTy;
87+
typedef std::map<std::vector<const Init *>, std::vector<const Record *>>
88+
RowInstrMapTy;
8689

8790
namespace {
8891

@@ -92,13 +95,13 @@ class InstrMap {
9295
private:
9396
std::string Name;
9497
std::string FilterClass;
95-
ListInit *RowFields;
96-
ListInit *ColFields;
97-
ListInit *KeyCol;
98-
std::vector<ListInit *> ValueCols;
98+
const ListInit *RowFields;
99+
const ListInit *ColFields;
100+
const ListInit *KeyCol;
101+
std::vector<const ListInit *> ValueCols;
99102

100103
public:
101-
InstrMap(Record *MapRec) {
104+
InstrMap(const Record *MapRec) {
102105
Name = std::string(MapRec->getName());
103106

104107
// FilterClass - It's used to reduce the search space only to the
@@ -133,8 +136,8 @@ class InstrMap {
133136
MapRec->getName() + "' has empty " +
134137
"`ValueCols' field!");
135138

136-
for (Init *I : ColValList->getValues()) {
137-
auto *ColI = cast<ListInit>(I);
139+
for (const Init *I : ColValList->getValues()) {
140+
const auto *ColI = cast<ListInit>(I);
138141

139142
// Make sure that all the sub-lists in 'ValueCols' have same number of
140143
// elements as the fields in 'ColFields'.
@@ -148,26 +151,19 @@ class InstrMap {
148151
}
149152

150153
const std::string &getName() const { return Name; }
151-
152154
const std::string &getFilterClass() const { return FilterClass; }
153-
154-
ListInit *getRowFields() const { return RowFields; }
155-
156-
ListInit *getColFields() const { return ColFields; }
157-
158-
ListInit *getKeyCol() const { return KeyCol; }
159-
160-
const std::vector<ListInit *> &getValueCols() const { return ValueCols; }
155+
const ListInit *getRowFields() const { return RowFields; }
156+
const ListInit *getColFields() const { return ColFields; }
157+
const ListInit *getKeyCol() const { return KeyCol; }
158+
ArrayRef<const ListInit *> getValueCols() const { return ValueCols; }
161159
};
162-
} // end anonymous namespace
163160

164161
//===----------------------------------------------------------------------===//
165162
// class MapTableEmitter : It builds the instruction relation maps using
166163
// the information provided in InstrMapping records. It outputs these
167164
// relationship maps as tables into XXXGenInstrInfo.inc file along with the
168165
// functions to query them.
169166

170-
namespace {
171167
class MapTableEmitter {
172168
private:
173169
// std::string TargetName;
@@ -177,18 +173,19 @@ class MapTableEmitter {
177173

178174
// InstrDefs - list of instructions filtered using FilterClass defined
179175
// in InstrMapDesc.
180-
std::vector<Record *> InstrDefs;
176+
ArrayRef<const Record *> InstrDefs;
181177

182178
// RowInstrMap - maps RowFields values to the instructions. It's keyed by the
183179
// values of the row fields and contains vector of records as values.
184180
RowInstrMapTy RowInstrMap;
185181

186182
// KeyInstrVec - list of key instructions.
187-
std::vector<Record *> KeyInstrVec;
188-
DenseMap<const Record *, std::vector<Record *>> MapTable;
183+
std::vector<const Record *> KeyInstrVec;
184+
DenseMap<const Record *, std::vector<const Record *>> MapTable;
189185

190186
public:
191-
MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec)
187+
MapTableEmitter(const CodeGenTarget &Target, const RecordKeeper &Records,
188+
const Record *IMRec)
192189
: Target(Target), InstrMapDesc(IMRec) {
193190
const std::string &FilterClass = InstrMapDesc.getFilterClass();
194191
InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
@@ -198,11 +195,12 @@ class MapTableEmitter {
198195

199196
// Returns true if an instruction is a key instruction, i.e., its ColFields
200197
// have same values as KeyCol.
201-
bool isKeyColInstr(Record *CurInstr);
198+
bool isKeyColInstr(const Record *CurInstr);
202199

203200
// Find column instruction corresponding to a key instruction based on the
204201
// constraints for that column.
205-
Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
202+
const Record *getInstrForColumn(const Record *KeyInstr,
203+
const ListInit *CurValueCol);
206204

207205
// Find column instructions for each key instruction based
208206
// on ValueCols and store them into MapTable.
@@ -226,17 +224,17 @@ class MapTableEmitter {
226224
//===----------------------------------------------------------------------===//
227225

228226
void MapTableEmitter::buildRowInstrMap() {
229-
for (Record *CurInstr : InstrDefs) {
230-
std::vector<Init *> KeyValue;
231-
ListInit *RowFields = InstrMapDesc.getRowFields();
232-
for (Init *RowField : RowFields->getValues()) {
233-
RecordVal *RecVal = CurInstr->getValue(RowField);
227+
for (const Record *CurInstr : InstrDefs) {
228+
std::vector<const Init *> KeyValue;
229+
const ListInit *RowFields = InstrMapDesc.getRowFields();
230+
for (const Init *RowField : RowFields->getValues()) {
231+
const RecordVal *RecVal = CurInstr->getValue(RowField);
234232
if (RecVal == nullptr)
235233
PrintFatalError(CurInstr->getLoc(),
236234
"No value " + RowField->getAsString() + " found in \"" +
237235
CurInstr->getName() +
238236
"\" instruction description.");
239-
Init *CurInstrVal = RecVal->getValue();
237+
const Init *CurInstrVal = RecVal->getValue();
240238
KeyValue.push_back(CurInstrVal);
241239
}
242240

@@ -254,18 +252,19 @@ void MapTableEmitter::buildRowInstrMap() {
254252
// Return true if an instruction is a KeyCol instruction.
255253
//===----------------------------------------------------------------------===//
256254

257-
bool MapTableEmitter::isKeyColInstr(Record *CurInstr) {
258-
ListInit *ColFields = InstrMapDesc.getColFields();
259-
ListInit *KeyCol = InstrMapDesc.getKeyCol();
255+
bool MapTableEmitter::isKeyColInstr(const Record *CurInstr) {
256+
const ListInit *ColFields = InstrMapDesc.getColFields();
257+
const ListInit *KeyCol = InstrMapDesc.getKeyCol();
260258

261259
// Check if the instruction is a KeyCol instruction.
262260
bool MatchFound = true;
263261
for (unsigned j = 0, endCF = ColFields->size(); (j < endCF) && MatchFound;
264262
j++) {
265-
RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
263+
const RecordVal *ColFieldName =
264+
CurInstr->getValue(ColFields->getElement(j));
266265
std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
267266
std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
268-
MatchFound = (CurInstrVal == KeyColValue);
267+
MatchFound = CurInstrVal == KeyColValue;
269268
}
270269
return MatchFound;
271270
}
@@ -278,15 +277,15 @@ bool MapTableEmitter::isKeyColInstr(Record *CurInstr) {
278277
void MapTableEmitter::buildMapTable() {
279278
// Find column instructions for a given key based on the ColField
280279
// constraints.
281-
const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols();
280+
ArrayRef<const ListInit *> ValueCols = InstrMapDesc.getValueCols();
282281
unsigned NumOfCols = ValueCols.size();
283-
for (Record *CurKeyInstr : KeyInstrVec) {
284-
std::vector<Record *> ColInstrVec(NumOfCols);
282+
for (const Record *CurKeyInstr : KeyInstrVec) {
283+
std::vector<const Record *> ColInstrVec(NumOfCols);
285284

286285
// Find the column instruction based on the constraints for the column.
287286
for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
288-
ListInit *CurValueCol = ValueCols[ColIdx];
289-
Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
287+
const ListInit *CurValueCol = ValueCols[ColIdx];
288+
const Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
290289
ColInstrVec[ColIdx] = ColInstr;
291290
}
292291
MapTable[CurKeyInstr] = ColInstrVec;
@@ -297,43 +296,43 @@ void MapTableEmitter::buildMapTable() {
297296
// Find column instruction based on the constraints for that column.
298297
//===----------------------------------------------------------------------===//
299298

300-
Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
301-
ListInit *CurValueCol) {
302-
ListInit *RowFields = InstrMapDesc.getRowFields();
303-
std::vector<Init *> KeyValue;
299+
const Record *MapTableEmitter::getInstrForColumn(const Record *KeyInstr,
300+
const ListInit *CurValueCol) {
301+
const ListInit *RowFields = InstrMapDesc.getRowFields();
302+
std::vector<const Init *> KeyValue;
304303

305304
// Construct KeyValue using KeyInstr's values for RowFields.
306-
for (Init *RowField : RowFields->getValues()) {
307-
Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
305+
for (const Init *RowField : RowFields->getValues()) {
306+
const Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
308307
KeyValue.push_back(KeyInstrVal);
309308
}
310309

311310
// Get all the instructions that share the same KeyValue as the KeyInstr
312311
// in RowInstrMap. We search through these instructions to find a match
313312
// for the current column, i.e., the instruction which has the same values
314313
// as CurValueCol for all the fields in ColFields.
315-
const std::vector<Record *> &RelatedInstrVec = RowInstrMap[KeyValue];
314+
ArrayRef<const Record *> RelatedInstrVec = RowInstrMap[KeyValue];
316315

317-
ListInit *ColFields = InstrMapDesc.getColFields();
318-
Record *MatchInstr = nullptr;
316+
const ListInit *ColFields = InstrMapDesc.getColFields();
317+
const Record *MatchInstr = nullptr;
319318

320-
for (llvm::Record *CurInstr : RelatedInstrVec) {
319+
for (const Record *CurInstr : RelatedInstrVec) {
321320
bool MatchFound = true;
322321
for (unsigned j = 0, endCF = ColFields->size(); (j < endCF) && MatchFound;
323322
j++) {
324-
Init *ColFieldJ = ColFields->getElement(j);
325-
Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
323+
const Init *ColFieldJ = ColFields->getElement(j);
324+
const Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
326325
std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
327-
Init *ColFieldJVallue = CurValueCol->getElement(j);
328-
MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
326+
const Init *ColFieldJVallue = CurValueCol->getElement(j);
327+
MatchFound = CurInstrVal == ColFieldJVallue->getAsUnquotedString();
329328
}
330329

331330
if (MatchFound) {
332331
if (MatchInstr) {
333332
// Already had a match
334333
// Error if multiple matches are found for a column.
335334
std::string KeyValueStr;
336-
for (Init *Value : KeyValue) {
335+
for (const Init *Value : KeyValue) {
337336
if (!KeyValueStr.empty())
338337
KeyValueStr += ", ";
339338
KeyValueStr += Value->getAsString();
@@ -357,11 +356,10 @@ Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
357356
//===----------------------------------------------------------------------===//
358357

359358
unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
360-
361359
ArrayRef<const CodeGenInstruction *> NumberedInstructions =
362360
Target.getInstructionsByEnumValue();
363361
StringRef Namespace = Target.getInstNamespace();
364-
const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols();
362+
ArrayRef<const ListInit *> ValueCols = InstrMapDesc.getValueCols();
365363
unsigned NumCol = ValueCols.size();
366364
unsigned TotalNumInstr = NumberedInstructions.size();
367365
unsigned TableSize = 0;
@@ -372,7 +370,7 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
372370
OS << "Table[][" << NumCol + 1 << "] = {\n";
373371
for (unsigned i = 0; i < TotalNumInstr; i++) {
374372
const Record *CurInstr = NumberedInstructions[i]->TheDef;
375-
std::vector<Record *> ColInstrs = MapTable[CurInstr];
373+
ArrayRef<const Record *> ColInstrs = MapTable[CurInstr];
376374
std::string OutStr;
377375
unsigned RelExists = 0;
378376
if (!ColInstrs.empty()) {
@@ -434,8 +432,8 @@ void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
434432

435433
void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, unsigned TableSize) {
436434

437-
ListInit *ColFields = InstrMapDesc.getColFields();
438-
const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols();
435+
const ListInit *ColFields = InstrMapDesc.getColFields();
436+
ArrayRef<const ListInit *> ValueCols = InstrMapDesc.getValueCols();
439437

440438
// Emit binary search algorithm to locate instructions in the
441439
// relation table. If found, return opcode value from the appropriate column
@@ -444,7 +442,7 @@ void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, unsigned TableSize) {
444442

445443
if (ValueCols.size() > 1) {
446444
for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
447-
ListInit *ColumnI = ValueCols[i];
445+
const ListInit *ColumnI = ValueCols[i];
448446
OS << " if (";
449447
for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
450448
std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
@@ -476,8 +474,8 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
476474
// since first column is used for the key instructions), then we also need
477475
// to pass another input to indicate the column to be selected.
478476

479-
ListInit *ColFields = InstrMapDesc.getColFields();
480-
const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols();
477+
const ListInit *ColFields = InstrMapDesc.getColFields();
478+
ArrayRef<const ListInit *> ValueCols = InstrMapDesc.getValueCols();
481479
OS << "// " << InstrMapDesc.getName() << "\nLLVM_READONLY\n";
482480
OS << "int " << InstrMapDesc.getName() << "(uint16_t Opcode";
483481
if (ValueCols.size() > 1) {
@@ -499,23 +497,20 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
499497
// Emit enums for the column fields across all the instruction maps.
500498
//===----------------------------------------------------------------------===//
501499

502-
static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
503-
504-
std::vector<Record *> InstrMapVec;
505-
InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
506-
std::map<std::string, std::vector<Init *>> ColFieldValueMap;
500+
static void emitEnums(raw_ostream &OS, const RecordKeeper &Records) {
501+
std::map<std::string, std::vector<const Init *>> ColFieldValueMap;
507502

508503
// Iterate over all InstrMapping records and create a map between column
509504
// fields and their possible values across all records.
510-
for (Record *CurMap : InstrMapVec) {
511-
ListInit *ColFields;
512-
ColFields = CurMap->getValueAsListInit("ColFields");
513-
ListInit *List = CurMap->getValueAsListInit("ValueCols");
514-
std::vector<ListInit *> ValueCols;
505+
for (const Record *CurMap :
506+
Records.getAllDerivedDefinitions("InstrMapping")) {
507+
const ListInit *ColFields = CurMap->getValueAsListInit("ColFields");
508+
const ListInit *List = CurMap->getValueAsListInit("ValueCols");
509+
std::vector<const ListInit *> ValueCols;
515510
unsigned ListSize = List->size();
516511

517512
for (unsigned j = 0; j < ListSize; j++) {
518-
auto *ListJ = cast<ListInit>(List->getElement(j));
513+
const auto *ListJ = cast<ListInit>(List->getElement(j));
519514

520515
if (ListJ->size() != ColFields->size())
521516
PrintFatalError("Record `" + CurMap->getName() +
@@ -533,12 +528,10 @@ static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
533528
}
534529
}
535530

536-
for (auto &Entry : ColFieldValueMap) {
537-
std::vector<Init *> FieldValues = Entry.second;
538-
531+
for (auto &[EnumName, FieldValues] : ColFieldValueMap) {
539532
// Delete duplicate entries from ColFieldValueMap
540533
for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
541-
Init *CurVal = FieldValues[i];
534+
const Init *CurVal = FieldValues[i];
542535
for (unsigned j = i + 1; j < FieldValues.size(); j++) {
543536
if (CurVal == FieldValues[j]) {
544537
FieldValues.erase(FieldValues.begin() + j);
@@ -548,28 +541,24 @@ static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
548541
}
549542

550543
// Emit enumerated values for the column fields.
551-
OS << "enum " << Entry.first << " {\n";
552-
for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
553-
OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
554-
if (i != endFV - 1)
555-
OS << ",\n";
556-
else
557-
OS << "\n};\n\n";
558-
}
544+
OS << "enum " << EnumName << " {\n";
545+
ListSeparator LS(",\n");
546+
for (const Init *Field : FieldValues)
547+
OS << LS << "\t" << EnumName << "_" << Field->getAsUnquotedString();
548+
OS << "\n};\n\n";
559549
}
560550
}
561551

562-
namespace llvm {
563552
//===----------------------------------------------------------------------===//
564553
// Parse 'InstrMapping' records and use the information to form relationship
565554
// between instructions. These relations are emitted as a tables along with the
566555
// functions to query them.
567556
//===----------------------------------------------------------------------===//
568-
void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
557+
void llvm::EmitMapTable(const RecordKeeper &Records, raw_ostream &OS) {
569558
CodeGenTarget Target(Records);
570559
StringRef NameSpace = Target.getInstNamespace();
571-
std::vector<Record *> InstrMapVec;
572-
InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
560+
ArrayRef<const Record *> InstrMapVec =
561+
Records.getAllDerivedDefinitions("InstrMapping");
573562

574563
if (InstrMapVec.empty())
575564
return;
@@ -585,7 +574,7 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
585574
// Iterate over all instruction mapping records and construct relationship
586575
// maps based on the information specified there.
587576
//
588-
for (Record *CurMap : InstrMapVec) {
577+
for (const Record *CurMap : InstrMapVec) {
589578
MapTableEmitter IMap(Target, Records, CurMap);
590579

591580
// Build RowInstrMap to group instructions based on their values for
@@ -604,5 +593,3 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
604593
OS << "} // end namespace llvm\n";
605594
OS << "#endif // GET_INSTRMAP_INFO\n\n";
606595
}
607-
608-
} // namespace llvm

0 commit comments

Comments
 (0)