Skip to content

Commit d0afe40

Browse files
jurahultmsri
authored andcommitted
[LLVM][TableGen] Change CallingConvEmitter to use const RecordKeeper (llvm#108955)
Change CallingConvEmitter 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 6a7a8bc commit d0afe40

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@ class RecordKeeper {
20362036
}
20372037

20382038
/// Start timing a phase. Automatically stops any previous phase timer.
2039-
void startTimer(StringRef Name);
2039+
void startTimer(StringRef Name) const;
20402040

20412041
/// Stop timing a phase.
20422042
void stopTimer();
@@ -2110,12 +2110,13 @@ class RecordKeeper {
21102110
mutable std::map<std::string, std::vector<Record *>> ClassRecordsMap;
21112111
GlobalMap ExtraGlobals;
21122112

2113+
// TODO: Move timing related code out of RecordKeeper.
21132114
// These members are for the phase timing feature. We need a timer group,
21142115
// the last timer started, and a flag to say whether the last timer
21152116
// is the special "backend overall timer."
2116-
TimerGroup *TimingGroup = nullptr;
2117-
Timer *LastTimer = nullptr;
2118-
bool BackendTimer = false;
2117+
mutable TimerGroup *TimingGroup = nullptr;
2118+
mutable Timer *LastTimer = nullptr;
2119+
mutable bool BackendTimer = false;
21192120

21202121
/// The internal uniquer implementation of the RecordKeeper.
21212122
std::unique_ptr<detail::RecordKeeperImpl> Impl;

llvm/lib/TableGen/Record.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3225,7 +3225,7 @@ Init *RecordKeeper::getNewAnonymousName() {
32253225
// These functions implement the phase timing facility. Starting a timer
32263226
// when one is already running stops the running one.
32273227

3228-
void RecordKeeper::startTimer(StringRef Name) {
3228+
void RecordKeeper::startTimer(StringRef Name) const {
32293229
if (TimingGroup) {
32303230
if (LastTimer && LastTimer->isRunning()) {
32313231
LastTimer->stopTimer();

llvm/utils/TableGen/CallingConvEmitter.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace llvm;
2222

2323
namespace {
2424
class CallingConvEmitter {
25-
RecordKeeper &Records;
25+
const RecordKeeper &Records;
2626
unsigned Counter = 0u;
2727
std::string CurrentAction;
2828
bool SwiftAction = false;
@@ -32,27 +32,28 @@ class CallingConvEmitter {
3232
std::map<std::string, std::set<std::string>> DelegateToMap;
3333

3434
public:
35-
explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {}
35+
explicit CallingConvEmitter(const RecordKeeper &R) : Records(R) {}
3636

3737
void run(raw_ostream &o);
3838

3939
private:
40-
void EmitCallingConv(Record *CC, raw_ostream &O);
41-
void EmitAction(Record *Action, unsigned Indent, raw_ostream &O);
40+
void EmitCallingConv(const Record *CC, raw_ostream &O);
41+
void EmitAction(const Record *Action, unsigned Indent, raw_ostream &O);
4242
void EmitArgRegisterLists(raw_ostream &O);
4343
};
4444
} // End anonymous namespace
4545

4646
void CallingConvEmitter::run(raw_ostream &O) {
4747
emitSourceFileHeader("Calling Convention Implementation Fragment", O);
4848

49-
std::vector<Record *> CCs = Records.getAllDerivedDefinitions("CallingConv");
49+
ArrayRef<const Record *> CCs =
50+
Records.getAllDerivedDefinitions("CallingConv");
5051

5152
// Emit prototypes for all of the non-custom CC's so that they can forward ref
5253
// each other.
5354
Records.startTimer("Emit prototypes");
5455
O << "#ifndef GET_CC_REGISTER_LISTS\n\n";
55-
for (Record *CC : CCs) {
56+
for (const Record *CC : CCs) {
5657
if (!CC->getValueAsBit("Custom")) {
5758
unsigned Pad = CC->getName().size();
5859
if (CC->getValueAsBit("Entry")) {
@@ -71,7 +72,7 @@ void CallingConvEmitter::run(raw_ostream &O) {
7172

7273
// Emit each non-custom calling convention description in full.
7374
Records.startTimer("Emit full descriptions");
74-
for (Record *CC : CCs) {
75+
for (const Record *CC : CCs) {
7576
if (!CC->getValueAsBit("Custom")) {
7677
EmitCallingConv(CC, O);
7778
}
@@ -82,8 +83,8 @@ void CallingConvEmitter::run(raw_ostream &O) {
8283
O << "\n#endif // CC_REGISTER_LIST\n";
8384
}
8485

85-
void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
86-
ListInit *CCActions = CC->getValueAsListInit("Actions");
86+
void CallingConvEmitter::EmitCallingConv(const Record *CC, raw_ostream &O) {
87+
const ListInit *CCActions = CC->getValueAsListInit("Actions");
8788
Counter = 0;
8889

8990
CurrentAction = CC->getName().str();
@@ -106,7 +107,7 @@ void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
106107
<< std::string(Pad, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n";
107108
// Emit all of the actions, in order.
108109
for (unsigned i = 0, e = CCActions->size(); i != e; ++i) {
109-
Record *Action = CCActions->getElementAsRecord(i);
110+
const Record *Action = CCActions->getElementAsRecord(i);
110111
SwiftAction =
111112
llvm::any_of(Action->getSuperClasses(),
112113
[](const std::pair<Record *, SMRange> &Class) {
@@ -122,7 +123,7 @@ void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) {
122123
O << "}\n";
123124
}
124125

125-
void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
126+
void CallingConvEmitter::EmitAction(const Record *Action, unsigned Indent,
126127
raw_ostream &O) {
127128
std::string IndentStr = std::string(Indent, ' ');
128129

@@ -150,14 +151,14 @@ void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
150151
O << IndentStr << "}\n";
151152
} else {
152153
if (Action->isSubClassOf("CCDelegateTo")) {
153-
Record *CC = Action->getValueAsDef("CC");
154+
const Record *CC = Action->getValueAsDef("CC");
154155
O << IndentStr << "if (!" << CC->getName()
155156
<< "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
156157
<< IndentStr << " return false;\n";
157158
DelegateToMap[CurrentAction].insert(CC->getName().str());
158159
} else if (Action->isSubClassOf("CCAssignToReg") ||
159160
Action->isSubClassOf("CCAssignToRegAndStack")) {
160-
ListInit *RegList = Action->getValueAsListInit("RegList");
161+
const ListInit *RegList = Action->getValueAsListInit("RegList");
161162
if (RegList->size() == 1) {
162163
std::string Name = getQualifiedName(RegList->getElementAsRecord(0));
163164
O << IndentStr << "if (MCRegister Reg = State.AllocateReg(" << Name
@@ -210,8 +211,9 @@ void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
210211
O << IndentStr << " return false;\n";
211212
O << IndentStr << "}\n";
212213
} else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
213-
ListInit *RegList = Action->getValueAsListInit("RegList");
214-
ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
214+
const ListInit *RegList = Action->getValueAsListInit("RegList");
215+
const ListInit *ShadowRegList =
216+
Action->getValueAsListInit("ShadowRegList");
215217
if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
216218
PrintFatalError(Action->getLoc(),
217219
"Invalid length of list of shadowed registers");
@@ -278,7 +280,8 @@ void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
278280
} else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
279281
int Size = Action->getValueAsInt("Size");
280282
int Align = Action->getValueAsInt("Align");
281-
ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
283+
const ListInit *ShadowRegList =
284+
Action->getValueAsListInit("ShadowRegList");
282285

283286
unsigned ShadowRegListNumber = ++Counter;
284287

@@ -297,7 +300,7 @@ void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
297300
<< Counter << ", LocVT, LocInfo));\n";
298301
O << IndentStr << "return false;\n";
299302
} else if (Action->isSubClassOf("CCPromoteToType")) {
300-
Record *DestTy = Action->getValueAsDef("DestTy");
303+
const Record *DestTy = Action->getValueAsDef("DestTy");
301304
MVT::SimpleValueType DestVT = getValueType(DestTy);
302305
O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
303306
if (MVT(DestVT).isFloatingPoint()) {
@@ -311,7 +314,7 @@ void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
311314
<< IndentStr << " LocInfo = CCValAssign::AExt;\n";
312315
}
313316
} else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
314-
Record *DestTy = Action->getValueAsDef("DestTy");
317+
const Record *DestTy = Action->getValueAsDef("DestTy");
315318
MVT::SimpleValueType DestVT = getValueType(DestTy);
316319
O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n";
317320
if (MVT(DestVT).isFloatingPoint()) {
@@ -327,17 +330,17 @@ void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent,
327330
<< IndentStr << " LocInfo = CCValAssign::AExtUpper;\n";
328331
}
329332
} else if (Action->isSubClassOf("CCBitConvertToType")) {
330-
Record *DestTy = Action->getValueAsDef("DestTy");
333+
const Record *DestTy = Action->getValueAsDef("DestTy");
331334
O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy))
332335
<< ";\n";
333336
O << IndentStr << "LocInfo = CCValAssign::BCvt;\n";
334337
} else if (Action->isSubClassOf("CCTruncToType")) {
335-
Record *DestTy = Action->getValueAsDef("DestTy");
338+
const Record *DestTy = Action->getValueAsDef("DestTy");
336339
O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy))
337340
<< ";\n";
338341
O << IndentStr << "LocInfo = CCValAssign::Trunc;\n";
339342
} else if (Action->isSubClassOf("CCPassIndirect")) {
340-
Record *DestTy = Action->getValueAsDef("DestTy");
343+
const Record *DestTy = Action->getValueAsDef("DestTy");
341344
O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy))
342345
<< ";\n";
343346
O << IndentStr << "LocInfo = CCValAssign::Indirect;\n";

0 commit comments

Comments
 (0)