Skip to content

Commit 985600d

Browse files
authored
[TableGen] Migrate CodeGenHWModes to use const RecordKeeper (#107851)
Migrate CodeGenHWModes to use const RecordKeeper and const Record pointers. 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 b3d2d50 commit 985600d

File tree

6 files changed

+37
-36
lines changed

6 files changed

+37
-36
lines changed

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CodeEmitterGen {
5858
int getVariableBit(const std::string &VarName, BitsInit *BI, int bit);
5959
std::pair<std::string, std::string>
6060
getInstructionCases(Record *R, CodeGenTarget &Target);
61-
void addInstructionCasesForEncoding(Record *R, Record *EncodingDef,
61+
void addInstructionCasesForEncoding(Record *R, const Record *EncodingDef,
6262
CodeGenTarget &Target, std::string &Case,
6363
std::string &BitOffsetCase);
6464
bool addCodeToMergeInOperand(Record *R, BitsInit *BI,
@@ -342,8 +342,8 @@ CodeEmitterGen::getInstructionCases(Record *R, CodeGenTarget &Target) {
342342
}
343343

344344
void CodeEmitterGen::addInstructionCasesForEncoding(
345-
Record *R, Record *EncodingDef, CodeGenTarget &Target, std::string &Case,
346-
std::string &BitOffsetCase) {
345+
Record *R, const Record *EncodingDef, CodeGenTarget &Target,
346+
std::string &Case, std::string &BitOffsetCase) {
347347
BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst");
348348

349349
// Loop over all of the fields in the instruction, determining which are the
@@ -413,7 +413,7 @@ void CodeEmitterGen::emitInstructionBaseValues(
413413
continue;
414414
}
415415

416-
Record *EncodingDef = R;
416+
const Record *EncodingDef = R;
417417
if (const RecordVal *RV = R->getValue("EncodingInfos")) {
418418
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
419419
EncodingInfoByHwMode EBM(DI->getDef(), HWM);

llvm/utils/TableGen/Common/CodeGenHwModes.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ using namespace llvm;
1818

1919
StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";
2020

21-
HwMode::HwMode(Record *R) {
21+
HwMode::HwMode(const Record *R) {
2222
Name = R->getName();
2323
Features = std::string(R->getValueAsString("Features"));
2424

25-
std::vector<Record *> PredicateRecs = R->getValueAsListOfDefs("Predicates");
2625
SmallString<128> PredicateCheck;
2726
raw_svector_ostream OS(PredicateCheck);
2827
ListSeparator LS(" && ");
29-
for (Record *Pred : PredicateRecs) {
28+
for (const Record *Pred : R->getValueAsListOfDefs("Predicates")) {
3029
StringRef CondString = Pred->getValueAsString("CondString");
3130
if (CondString.empty())
3231
continue;
@@ -39,7 +38,7 @@ HwMode::HwMode(Record *R) {
3938
LLVM_DUMP_METHOD
4039
void HwMode::dump() const { dbgs() << Name << ": " << Features << '\n'; }
4140

42-
HwModeSelect::HwModeSelect(Record *R, CodeGenHwModes &CGH) {
41+
HwModeSelect::HwModeSelect(const Record *R, CodeGenHwModes &CGH) {
4342
std::vector<Record *> Modes = R->getValueAsListOfDefs("Modes");
4443
std::vector<Record *> Objects = R->getValueAsListOfDefs("Objects");
4544
if (Modes.size() != Objects.size()) {
@@ -64,8 +63,8 @@ void HwModeSelect::dump() const {
6463
dbgs() << " }\n";
6564
}
6665

67-
CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {
68-
for (Record *R : Records.getAllDerivedDefinitions("HwMode")) {
66+
CodeGenHwModes::CodeGenHwModes(const RecordKeeper &RK) : Records(RK) {
67+
for (const Record *R : Records.getAllDerivedDefinitions("HwMode")) {
6968
// The default mode needs a definition in the .td sources for TableGen
7069
// to accept references to it. We need to ignore the definition here.
7170
if (R->getName() == DefaultModeName)
@@ -76,22 +75,22 @@ CodeGenHwModes::CodeGenHwModes(RecordKeeper &RK) : Records(RK) {
7675

7776
assert(Modes.size() <= 32 && "number of HwModes exceeds maximum of 32");
7877

79-
for (Record *R : Records.getAllDerivedDefinitions("HwModeSelect")) {
78+
for (const Record *R : Records.getAllDerivedDefinitions("HwModeSelect")) {
8079
auto P = ModeSelects.emplace(std::pair(R, HwModeSelect(R, *this)));
8180
assert(P.second);
8281
(void)P;
8382
}
8483
}
8584

86-
unsigned CodeGenHwModes::getHwModeId(Record *R) const {
85+
unsigned CodeGenHwModes::getHwModeId(const Record *R) const {
8786
if (R->getName() == DefaultModeName)
8887
return DefaultMode;
8988
auto F = ModeIds.find(R);
9089
assert(F != ModeIds.end() && "Unknown mode name");
9190
return F->second;
9291
}
9392

94-
const HwModeSelect &CodeGenHwModes::getHwModeSelect(Record *R) const {
93+
const HwModeSelect &CodeGenHwModes::getHwModeSelect(const Record *R) const {
9594
auto F = ModeSelects.find(R);
9695
assert(F != ModeSelects.end() && "Record is not a \"mode select\"");
9796
return F->second;

llvm/utils/TableGen/Common/CodeGenHwModes.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ class RecordKeeper;
2828
struct CodeGenHwModes;
2929

3030
struct HwMode {
31-
HwMode(Record *R);
31+
HwMode(const Record *R);
3232
StringRef Name;
3333
std::string Features;
3434
std::string Predicates;
3535
void dump() const;
3636
};
3737

3838
struct HwModeSelect {
39-
HwModeSelect(Record *R, CodeGenHwModes &CGH);
40-
typedef std::pair<unsigned, Record *> PairType;
39+
HwModeSelect(const Record *R, CodeGenHwModes &CGH);
40+
typedef std::pair<unsigned, const Record *> PairType;
4141
std::vector<PairType> Items;
4242
void dump() const;
4343
};
@@ -46,8 +46,8 @@ struct CodeGenHwModes {
4646
enum : unsigned { DefaultMode = 0 };
4747
static StringRef DefaultModeName;
4848

49-
CodeGenHwModes(RecordKeeper &R);
50-
unsigned getHwModeId(Record *R) const;
49+
CodeGenHwModes(const RecordKeeper &R);
50+
unsigned getHwModeId(const Record *R) const;
5151
const HwMode &getMode(unsigned Id) const {
5252
assert(Id != 0 && "Mode id of 0 is reserved for the default mode");
5353
return Modes[Id - 1];
@@ -57,18 +57,18 @@ struct CodeGenHwModes {
5757
return DefaultModeName;
5858
return getMode(Id).Name;
5959
}
60-
const HwModeSelect &getHwModeSelect(Record *R) const;
61-
const std::map<Record *, HwModeSelect> &getHwModeSelects() const {
60+
const HwModeSelect &getHwModeSelect(const Record *R) const;
61+
const std::map<const Record *, HwModeSelect> &getHwModeSelects() const {
6262
return ModeSelects;
6363
}
6464
unsigned getNumModeIds() const { return Modes.size() + 1; }
6565
void dump() const;
6666

6767
private:
68-
RecordKeeper &Records;
69-
DenseMap<Record *, unsigned> ModeIds; // HwMode Record -> HwModeId
68+
const RecordKeeper &Records;
69+
DenseMap<const Record *, unsigned> ModeIds; // HwMode Record -> HwModeId
7070
std::vector<HwMode> Modes;
71-
std::map<Record *, HwModeSelect> ModeSelects;
71+
std::map<const Record *, HwModeSelect> ModeSelects;
7272
};
7373
} // namespace llvm
7474

llvm/utils/TableGen/Common/InfoByHwMode.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec,
115115
return ValueTypeByHwMode(Rec, llvm::getValueType(Rec));
116116
}
117117

118-
RegSizeInfo::RegSizeInfo(Record *R) {
118+
RegSizeInfo::RegSizeInfo(const Record *R) {
119119
RegSize = R->getValueAsInt("RegSize");
120120
SpillSize = R->getValueAsInt("SpillSize");
121121
SpillAlignment = R->getValueAsInt("SpillAlignment");
@@ -136,7 +136,8 @@ void RegSizeInfo::writeToStream(raw_ostream &OS) const {
136136
<< ']';
137137
}
138138

139-
RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH) {
139+
RegSizeInfoByHwMode::RegSizeInfoByHwMode(const Record *R,
140+
const CodeGenHwModes &CGH) {
140141
const HwModeSelect &MS = CGH.getHwModeSelect(R);
141142
for (const HwModeSelect::PairType &P : MS.Items) {
142143
auto I = Map.insert({P.first, RegSizeInfo(P.second)});
@@ -183,12 +184,13 @@ void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {
183184
OS << '}';
184185
}
185186

186-
SubRegRange::SubRegRange(Record *R) {
187+
SubRegRange::SubRegRange(const Record *R) {
187188
Size = R->getValueAsInt("Size");
188189
Offset = R->getValueAsInt("Offset");
189190
}
190191

191-
SubRegRangeByHwMode::SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH) {
192+
SubRegRangeByHwMode::SubRegRangeByHwMode(const Record *R,
193+
const CodeGenHwModes &CGH) {
192194
const HwModeSelect &MS = CGH.getHwModeSelect(R);
193195
for (const HwModeSelect::PairType &P : MS.Items) {
194196
auto I = Map.insert({P.first, SubRegRange(P.second)});
@@ -197,7 +199,7 @@ SubRegRangeByHwMode::SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH) {
197199
}
198200
}
199201

200-
EncodingInfoByHwMode::EncodingInfoByHwMode(Record *R,
202+
EncodingInfoByHwMode::EncodingInfoByHwMode(const Record *R,
201203
const CodeGenHwModes &CGH) {
202204
const HwModeSelect &MS = CGH.getHwModeSelect(R);
203205
for (const HwModeSelect::PairType &P : MS.Items) {

llvm/utils/TableGen/Common/InfoByHwMode.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct RegSizeInfo {
183183
unsigned SpillSize;
184184
unsigned SpillAlignment;
185185

186-
RegSizeInfo(Record *R);
186+
RegSizeInfo(const Record *R);
187187
RegSizeInfo() = default;
188188
bool operator<(const RegSizeInfo &I) const;
189189
bool operator==(const RegSizeInfo &I) const {
@@ -197,7 +197,7 @@ struct RegSizeInfo {
197197
};
198198

199199
struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> {
200-
RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
200+
RegSizeInfoByHwMode(const Record *R, const CodeGenHwModes &CGH);
201201
RegSizeInfoByHwMode() = default;
202202
bool operator<(const RegSizeInfoByHwMode &VI) const;
203203
bool operator==(const RegSizeInfoByHwMode &VI) const;
@@ -222,12 +222,12 @@ struct SubRegRange {
222222
uint16_t Size;
223223
uint16_t Offset;
224224

225-
SubRegRange(Record *R);
225+
SubRegRange(const Record *R);
226226
SubRegRange(uint16_t Size, uint16_t Offset) : Size(Size), Offset(Offset) {}
227227
};
228228

229229
struct SubRegRangeByHwMode : public InfoByHwMode<SubRegRange> {
230-
SubRegRangeByHwMode(Record *R, const CodeGenHwModes &CGH);
230+
SubRegRangeByHwMode(const Record *R, const CodeGenHwModes &CGH);
231231
SubRegRangeByHwMode(SubRegRange Range) { Map.insert({DefaultMode, Range}); }
232232
SubRegRangeByHwMode() = default;
233233

@@ -236,8 +236,8 @@ struct SubRegRangeByHwMode : public InfoByHwMode<SubRegRange> {
236236
}
237237
};
238238

239-
struct EncodingInfoByHwMode : public InfoByHwMode<Record *> {
240-
EncodingInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
239+
struct EncodingInfoByHwMode : public InfoByHwMode<const Record *> {
240+
EncodingInfoByHwMode(const Record *R, const CodeGenHwModes &CGH);
241241
EncodingInfoByHwMode() = default;
242242
};
243243

llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
241241
for (auto &KV : EBM) {
242242
AltEncodingTy Mode = KV.first;
243243
Modes.insert({Mode, "_" + HWM.getMode(Mode).Name.str()});
244-
Record *EncodingDef = KV.second;
245-
RecordVal *RV = EncodingDef->getValue("Inst");
244+
const Record *EncodingDef = KV.second;
245+
const RecordVal *RV = EncodingDef->getValue("Inst");
246246
DagInit *DI = cast<DagInit>(RV->getValue());
247247
VarLenInsts[R].insert({Mode, VarLenInst(DI, RV)});
248248
}

0 commit comments

Comments
 (0)