Skip to content

Commit 43f044b

Browse files
authored
[LLVM][TableGen] Change CodeGenTarget to use const RecordKeeper (#108752)
Change CodeGenTarget 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 8783bd5 commit 43f044b

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

llvm/utils/TableGen/AsmWriterEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
636636
Record *AsmWriter = Target.getAsmWriter();
637637
StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
638638
const auto &Registers = Target.getRegBank().getRegisters();
639-
const std::vector<Record *> &AltNameIndices = Target.getRegAltNameIndices();
639+
ArrayRef<const Record *> AltNameIndices = Target.getRegAltNameIndices();
640640
bool hasAltNames = AltNameIndices.size() > 1;
641641
StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
642642

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ std::string llvm::getQualifiedName(const Record *R) {
8585

8686
/// getTarget - Return the current instance of the Target class.
8787
///
88-
CodeGenTarget::CodeGenTarget(RecordKeeper &records)
88+
CodeGenTarget::CodeGenTarget(const RecordKeeper &records)
8989
: Records(records), CGH(records), Intrinsics(records) {
90-
std::vector<Record *> Targets = Records.getAllDerivedDefinitions("Target");
90+
ArrayRef<const Record *> Targets = Records.getAllDerivedDefinitions("Target");
9191
if (Targets.size() == 0)
9292
PrintFatalError("No 'Target' subclasses defined!");
9393
if (Targets.size() != 1)
@@ -223,11 +223,6 @@ std::optional<CodeGenRegisterClass *> CodeGenTarget::getSuperRegForSubReg(
223223
return Candidates[0];
224224
}
225225

226-
void CodeGenTarget::ReadRegAltNameIndices() const {
227-
RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
228-
llvm::sort(RegAltNameIndices, LessRecord());
229-
}
230-
231226
/// getRegisterByName - If there is a register with the specific AsmName,
232227
/// return it.
233228
const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
@@ -271,12 +266,13 @@ CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
271266
}
272267

273268
void CodeGenTarget::ReadInstructions() const {
274-
std::vector<Record *> Insts = Records.getAllDerivedDefinitions("Instruction");
269+
ArrayRef<const Record *> Insts =
270+
Records.getAllDerivedDefinitions("Instruction");
275271
if (Insts.size() <= 2)
276272
PrintFatalError("No 'Instruction' subclasses defined!");
277273

278274
// Parse the instructions defined in the .td file.
279-
for (Record *R : Insts) {
275+
for (const Record *R : Insts) {
280276
Instructions[R] = std::make_unique<CodeGenInstruction>(R);
281277
if (Instructions[R]->isVariableLengthEncoding())
282278
HasVariableLengthEncodings = true;
@@ -286,7 +282,7 @@ void CodeGenTarget::ReadInstructions() const {
286282
static const CodeGenInstruction *GetInstByName(
287283
const char *Name,
288284
const DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &Insts,
289-
RecordKeeper &Records) {
285+
const RecordKeeper &Records) {
290286
const Record *Rec = Records.getDef(Name);
291287

292288
const auto I = Insts.find(Rec);
@@ -358,9 +354,8 @@ void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
358354
if (!isLittleEndianEncoding())
359355
return;
360356

361-
std::vector<Record *> Insts =
362-
Records.getAllDerivedDefinitions("InstructionEncoding");
363-
for (Record *R : Insts) {
357+
for (const Record *R :
358+
Records.getAllDerivedDefinitions("InstructionEncoding")) {
364359
if (R->getValueAsString("Namespace") == "TargetOpcode" ||
365360
R->getValueAsBit("isPseudo"))
366361
continue;
@@ -383,11 +378,15 @@ void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
383378
NewBits[middle] = BI->getBit(middle);
384379
}
385380

386-
BitsInit *NewBI = BitsInit::get(Records, NewBits);
381+
RecordKeeper &MutableRC = const_cast<RecordKeeper &>(Records);
382+
BitsInit *NewBI = BitsInit::get(MutableRC, NewBits);
387383

388-
// Update the bits in reversed order so that emitInstrOpBits will get the
389-
// correct endianness.
390-
R->getValue("Inst")->setValue(NewBI);
384+
// Update the bits in reversed order so that emitters will get the correct
385+
// endianness.
386+
// FIXME: Eliminate mutation of TG records by creating a helper function
387+
// to reverse bits and maintain a cache instead of mutating records.
388+
Record *MutableR = const_cast<Record *>(R);
389+
MutableR->getValue("Inst")->setValue(NewBI);
391390
}
392391
}
393392

llvm/utils/TableGen/Common/CodeGenTarget.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,18 @@ std::string getQualifiedName(const Record *R);
5656
/// CodeGenTarget - This class corresponds to the Target class in the .td files.
5757
///
5858
class CodeGenTarget {
59-
RecordKeeper &Records;
60-
Record *TargetRec;
59+
const RecordKeeper &Records;
60+
const Record *TargetRec;
6161

6262
mutable DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>>
6363
Instructions;
6464
mutable std::unique_ptr<CodeGenRegBank> RegBank;
65-
mutable std::vector<Record *> RegAltNameIndices;
65+
mutable ArrayRef<const Record *> RegAltNameIndices;
6666
mutable SmallVector<ValueTypeByHwMode, 8> LegalValueTypes;
6767
CodeGenHwModes CGH;
68-
std::vector<Record *> MacroFusions;
68+
ArrayRef<const Record *> MacroFusions;
6969
mutable bool HasVariableLengthEncodings = false;
7070

71-
void ReadRegAltNameIndices() const;
7271
void ReadInstructions() const;
7372
void ReadLegalValueTypes() const;
7473

@@ -81,10 +80,10 @@ class CodeGenTarget {
8180
mutable unsigned NumPseudoInstructions = 0;
8281

8382
public:
84-
CodeGenTarget(RecordKeeper &Records);
83+
CodeGenTarget(const RecordKeeper &Records);
8584
~CodeGenTarget();
8685

87-
Record *getTargetRecord() const { return TargetRec; }
86+
const Record *getTargetRecord() const { return TargetRec; }
8887
StringRef getName() const;
8988

9089
/// getInstNamespace - Return the target-specific instruction namespace.
@@ -135,9 +134,9 @@ class CodeGenTarget {
135134
/// return it.
136135
const CodeGenRegister *getRegisterByName(StringRef Name) const;
137136

138-
const std::vector<Record *> &getRegAltNameIndices() const {
137+
ArrayRef<const Record *> getRegAltNameIndices() const {
139138
if (RegAltNameIndices.empty())
140-
ReadRegAltNameIndices();
139+
RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
141140
return RegAltNameIndices;
142141
}
143142

@@ -159,7 +158,7 @@ class CodeGenTarget {
159158

160159
bool hasMacroFusion() const { return !MacroFusions.empty(); }
161160

162-
const std::vector<Record *> getMacroFusions() const { return MacroFusions; }
161+
ArrayRef<const Record *> getMacroFusions() const { return MacroFusions; }
163162

164163
private:
165164
DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &

llvm/utils/TableGen/RegisterInfoEmitter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS, CodeGenTarget &Target,
152152
OS << "} // end namespace " << Namespace << "\n\n";
153153
}
154154

155-
const std::vector<Record *> &RegAltNameIndices =
156-
Target.getRegAltNameIndices();
155+
ArrayRef<const Record *> RegAltNameIndices = Target.getRegAltNameIndices();
157156
// If the only definition is the default NoRegAltName, we don't need to
158157
// emit anything.
159158
if (RegAltNameIndices.size() > 1) {

0 commit comments

Comments
 (0)