Skip to content

Commit 3ae71d1

Browse files
authored
[LLVM][TableGen] Change CodeGenSchedule to use const RecordKeeper (#108617)
Change CodeGenSchedule 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 8837898 commit 3ae71d1

File tree

8 files changed

+203
-204
lines changed

8 files changed

+203
-204
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,9 @@ class Record {
19121912
/// vector of records, throwing an exception if the field does not exist or
19131913
/// if the value is not the right type.
19141914
std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1915+
// Temporary function to help staged migration to const Record pointers.
1916+
std::vector<const Record *>
1917+
getValueAsListOfConstDefs(StringRef FieldName) const;
19151918

19161919
/// This method looks up the specified field and returns its value as a
19171920
/// vector of integers, throwing an exception if the field does not exist or

llvm/lib/TableGen/Record.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,6 +3027,21 @@ Record::getValueAsListOfDefs(StringRef FieldName) const {
30273027
return Defs;
30283028
}
30293029

3030+
std::vector<const Record *>
3031+
Record::getValueAsListOfConstDefs(StringRef FieldName) const {
3032+
ListInit *List = getValueAsListInit(FieldName);
3033+
std::vector<const Record *> Defs;
3034+
for (const Init *I : List->getValues()) {
3035+
if (const DefInit *DI = dyn_cast<DefInit>(I))
3036+
Defs.push_back(DI->getDef());
3037+
else
3038+
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
3039+
FieldName +
3040+
"' list is not entirely DefInit!");
3041+
}
3042+
return Defs;
3043+
}
3044+
30303045
int64_t Record::getValueAsInt(StringRef FieldName) const {
30313046
const RecordVal *R = getValue(FieldName);
30323047
if (!R || !R->getValue())

llvm/utils/TableGen/Common/CodeGenSchedule.cpp

Lines changed: 93 additions & 117 deletions
Large diffs are not rendered by default.

llvm/utils/TableGen/Common/CodeGenSchedule.h

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ using IdxIter = IdxVec::const_iterator;
5151
struct CodeGenSchedRW {
5252
unsigned Index;
5353
std::string Name;
54-
Record *TheDef;
54+
const Record *TheDef;
5555
bool IsRead;
5656
bool IsAlias;
5757
bool HasVariants;
5858
bool IsVariadic;
5959
bool IsSequence;
6060
IdxVec Sequence;
61-
RecVec Aliases;
61+
ConstRecVec Aliases;
6262

6363
CodeGenSchedRW()
6464
: Index(0), TheDef(nullptr), IsRead(false), IsAlias(false),
6565
HasVariants(false), IsVariadic(false), IsSequence(false) {}
66-
CodeGenSchedRW(unsigned Idx, Record *Def)
66+
CodeGenSchedRW(unsigned Idx, const Record *Def)
6767
: Index(Idx), TheDef(Def), IsAlias(false), IsVariadic(false) {
6868
Name = std::string(Def->getName());
6969
IsRead = Def->isSubClassOf("SchedRead");
@@ -102,7 +102,7 @@ struct CodeGenSchedRW {
102102
struct CodeGenSchedTransition {
103103
unsigned ToClassIdx;
104104
unsigned ProcIndex;
105-
RecVec PredTerm;
105+
ConstRecVec PredTerm;
106106
};
107107

108108
/// Scheduling class.
@@ -145,7 +145,7 @@ struct CodeGenSchedClass {
145145
// Instruction no longer mapped to this class by InstrClassMap. These
146146
// Instructions should be ignored by this class because they have been split
147147
// off to join another inferred class.
148-
RecVec InstRWs;
148+
ConstRecVec InstRWs;
149149
// InstRWs processor indices. Filled in inferFromInstRWs
150150
DenseSet<unsigned> InstRWProcIndices;
151151

@@ -189,14 +189,14 @@ struct CodeGenRegisterCost {
189189
/// stalls due to register pressure.
190190
struct CodeGenRegisterFile {
191191
std::string Name;
192-
Record *RegisterFileDef;
192+
const Record *RegisterFileDef;
193193
unsigned MaxMovesEliminatedPerCycle;
194194
bool AllowZeroMoveEliminationOnly;
195195

196196
unsigned NumPhysRegs;
197197
std::vector<CodeGenRegisterCost> Costs;
198198

199-
CodeGenRegisterFile(StringRef name, Record *def,
199+
CodeGenRegisterFile(StringRef name, const Record *def,
200200
unsigned MaxMoveElimPerCy = 0,
201201
bool AllowZeroMoveElimOnly = false)
202202
: Name(name), RegisterFileDef(def),
@@ -223,8 +223,8 @@ struct CodeGenRegisterFile {
223223
struct CodeGenProcModel {
224224
unsigned Index;
225225
std::string ModelName;
226-
Record *ModelDef;
227-
Record *ItinsDef;
226+
const Record *ModelDef;
227+
const Record *ItinsDef;
228228

229229
// Derived members...
230230

@@ -235,30 +235,31 @@ struct CodeGenProcModel {
235235

236236
// Map itinerary classes to per-operand resources.
237237
// This list is empty if no ItinRW refers to this Processor.
238-
RecVec ItinRWDefs;
238+
ConstRecVec ItinRWDefs;
239239

240240
// List of unsupported feature.
241241
// This list is empty if the Processor has no UnsupportedFeatures.
242242
RecVec UnsupportedFeaturesDefs;
243243

244244
// All read/write resources associated with this processor.
245-
RecVec WriteResDefs;
246-
RecVec ReadAdvanceDefs;
245+
ConstRecVec WriteResDefs;
246+
ConstRecVec ReadAdvanceDefs;
247247

248248
// Per-operand machine model resources associated with this processor.
249-
RecVec ProcResourceDefs;
249+
ConstRecVec ProcResourceDefs;
250250

251251
// List of Register Files.
252252
std::vector<CodeGenRegisterFile> RegisterFiles;
253253

254254
// Optional Retire Control Unit definition.
255-
Record *RetireControlUnit;
255+
const Record *RetireControlUnit;
256256

257257
// Load/Store queue descriptors.
258-
Record *LoadQueue;
259-
Record *StoreQueue;
258+
const Record *LoadQueue;
259+
const Record *StoreQueue;
260260

261-
CodeGenProcModel(unsigned Idx, std::string Name, Record *MDef, Record *IDef)
261+
CodeGenProcModel(unsigned Idx, std::string Name, const Record *MDef,
262+
const Record *IDef)
262263
: Index(Idx), ModelName(std::move(Name)), ModelDef(MDef), ItinsDef(IDef),
263264
RetireControlUnit(nullptr), LoadQueue(nullptr), StoreQueue(nullptr) {}
264265

@@ -275,12 +276,12 @@ struct CodeGenProcModel {
275276
!RegisterFiles.empty();
276277
}
277278

278-
unsigned getProcResourceIdx(Record *PRDef) const;
279+
unsigned getProcResourceIdx(const Record *PRDef) const;
279280

280281
bool isUnsupported(const CodeGenInstruction &Inst) const;
281282

282283
// Return true if the given write record is referenced by a ReadAdvance.
283-
bool hasReadOfWrite(Record *WriteDef) const;
284+
bool hasReadOfWrite(const Record *WriteDef) const;
284285

285286
#ifndef NDEBUG
286287
void dump() const;
@@ -421,7 +422,7 @@ using ProcModelMapTy = DenseMap<const Record *, unsigned>;
421422

422423
/// Top level container for machine model data.
423424
class CodeGenSchedModels {
424-
RecordKeeper &Records;
425+
const RecordKeeper &Records;
425426
const CodeGenTarget &Target;
426427

427428
// Map dag expressions to Instruction lists.
@@ -443,8 +444,8 @@ class CodeGenSchedModels {
443444
// Any inferred SchedClass has an index greater than NumInstrSchedClassses.
444445
unsigned NumInstrSchedClasses;
445446

446-
RecVec ProcResourceDefs;
447-
RecVec ProcResGroups;
447+
ConstRecVec ProcResourceDefs;
448+
ConstRecVec ProcResGroups;
448449

449450
// Map each instruction to its unique SchedClass index considering the
450451
// combination of it's itinerary class, SchedRW list, and InstRW records.
@@ -455,7 +456,7 @@ class CodeGenSchedModels {
455456
std::vector<unsigned> getAllProcIndices() const;
456457

457458
public:
458-
CodeGenSchedModels(RecordKeeper &RK, const CodeGenTarget &TGT);
459+
CodeGenSchedModels(const RecordKeeper &RK, const CodeGenTarget &TGT);
459460

460461
// iterator access to the scheduling classes.
461462
using class_iterator = std::vector<CodeGenSchedClass>::iterator;
@@ -477,9 +478,9 @@ class CodeGenSchedModels {
477478
return make_range(classes_begin(), classes_begin() + NumInstrSchedClasses);
478479
}
479480

480-
Record *getModelOrItinDef(Record *ProcDef) const {
481-
Record *ModelDef = ProcDef->getValueAsDef("SchedModel");
482-
Record *ItinsDef = ProcDef->getValueAsDef("ProcItin");
481+
const Record *getModelOrItinDef(const Record *ProcDef) const {
482+
const Record *ModelDef = ProcDef->getValueAsDef("SchedModel");
483+
const Record *ItinsDef = ProcDef->getValueAsDef("ProcItin");
483484
if (!ItinsDef->getValueAsListOfDefs("IID").empty()) {
484485
assert(ModelDef->getValueAsBit("NoModel") &&
485486
"Itineraries must be defined within SchedMachineModel");
@@ -489,18 +490,18 @@ class CodeGenSchedModels {
489490
}
490491

491492
const CodeGenProcModel &getModelForProc(Record *ProcDef) const {
492-
Record *ModelDef = getModelOrItinDef(ProcDef);
493+
const Record *ModelDef = getModelOrItinDef(ProcDef);
493494
ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
494495
assert(I != ProcModelMap.end() && "missing machine model");
495496
return ProcModels[I->second];
496497
}
497498

498-
CodeGenProcModel &getProcModel(Record *ModelDef) {
499+
CodeGenProcModel &getProcModel(const Record *ModelDef) {
499500
ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
500501
assert(I != ProcModelMap.end() && "missing machine model");
501502
return ProcModels[I->second];
502503
}
503-
const CodeGenProcModel &getProcModel(Record *ModelDef) const {
504+
const CodeGenProcModel &getProcModel(const Record *ModelDef) const {
504505
return const_cast<CodeGenSchedModels *>(this)->getProcModel(ModelDef);
505506
}
506507

@@ -575,8 +576,9 @@ class CodeGenSchedModels {
575576

576577
unsigned findOrInsertRW(ArrayRef<unsigned> Seq, bool IsRead);
577578

578-
Record *findProcResUnits(Record *ProcResKind, const CodeGenProcModel &PM,
579-
ArrayRef<SMLoc> Loc) const;
579+
const Record *findProcResUnits(const Record *ProcResKind,
580+
const CodeGenProcModel &PM,
581+
ArrayRef<SMLoc> Loc) const;
580582

581583
ArrayRef<STIPredicateFunction> getSTIPredicates() const {
582584
return STIPredicates;
@@ -586,7 +588,7 @@ class CodeGenSchedModels {
586588
void collectProcModels();
587589

588590
// Initialize a new processor model if it is unique.
589-
void addProcModel(Record *ProcDef);
591+
void addProcModel(const Record *ProcDef);
590592

591593
void collectSchedRW();
592594

@@ -605,7 +607,7 @@ class CodeGenSchedModels {
605607
ArrayRef<unsigned> OperWrites,
606608
ArrayRef<unsigned> OperReads);
607609
std::string createSchedClassName(const ConstRecVec &InstDefs);
608-
void createInstRWClass(Record *InstRWDef);
610+
void createInstRWClass(const Record *InstRWDef);
609611

610612
void collectProcItins();
611613

@@ -643,12 +645,12 @@ class CodeGenSchedModels {
643645
void collectRWResources(ArrayRef<unsigned> Writes, ArrayRef<unsigned> Reads,
644646
ArrayRef<unsigned> ProcIndices);
645647

646-
void addProcResource(Record *ProcResourceKind, CodeGenProcModel &PM,
648+
void addProcResource(const Record *ProcResourceKind, CodeGenProcModel &PM,
647649
ArrayRef<SMLoc> Loc);
648650

649-
void addWriteRes(Record *ProcWriteResDef, unsigned PIdx);
651+
void addWriteRes(const Record *ProcWriteResDef, unsigned PIdx);
650652

651-
void addReadAdvance(Record *ProcReadAdvanceDef, unsigned PIdx);
653+
void addReadAdvance(const Record *ProcReadAdvanceDef, unsigned PIdx);
652654
};
653655

654656
} // namespace llvm

llvm/utils/TableGen/Common/Utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct LessRecordFieldNameAndID {
2727

2828
/// Sort an array of Records on the "Name" field, and check for records with
2929
/// duplicate "Name" field. If duplicates are found, report a fatal error.
30-
void llvm::sortAndReportDuplicates(MutableArrayRef<Record *> Records,
30+
void llvm::sortAndReportDuplicates(MutableArrayRef<const Record *> Records,
3131
StringRef ObjectName) {
3232
llvm::sort(Records, LessRecordFieldNameAndID());
3333

llvm/utils/TableGen/Common/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Record;
1717

1818
/// Sort an array of Records on the "Name" field, and check for records with
1919
/// duplicate "Name" field. If duplicates are found, report a fatal error.
20-
void sortAndReportDuplicates(MutableArrayRef<Record *> Records,
20+
void sortAndReportDuplicates(MutableArrayRef<const Record *> Records,
2121
StringRef ObjectName);
2222

2323
} // namespace llvm

llvm/utils/TableGen/DFAPacketizerEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ int DFAPacketizerEmitter::collectAllFuncUnits(
100100
LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
101101
LLVM_DEBUG(dbgs() << " (" << ProcModels.size() << " itineraries)\n");
102102

103-
std::set<Record *> ProcItinList;
103+
std::set<const Record *> ProcItinList;
104104
for (const CodeGenProcModel *Model : ProcModels)
105105
ProcItinList.insert(Model->ItinsDef);
106106

107107
int totalFUs = 0;
108108
// Parse functional units for all the itineraries.
109-
for (Record *Proc : ProcItinList) {
109+
for (const Record *Proc : ProcItinList) {
110110
std::vector<Record *> FUs = Proc->getValueAsListOfDefs("FU");
111111

112112
LLVM_DEBUG(dbgs() << " FU:"

0 commit comments

Comments
 (0)