Skip to content

Commit 5f25b89

Browse files
authored
[TableGen] Migrate Option Emitters to const RecordKeeper (#107696)
Migrate Opt/OptRST Emitters to 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 5904448 commit 5f25b89

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
lines changed

llvm/utils/TableGen/Common/OptEmitter.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- OptEmitter.cpp - Helper for emitting options.----------- -----------===//
1+
//===- OptEmitter.cpp - Helper for emitting options -------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -39,21 +39,19 @@ static int StrCmpOptionName(const char *A, const char *B) {
3939
return (a < b) ? -1 : 1;
4040
}
4141

42-
int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
43-
const Record *A = *Av;
44-
const Record *B = *Bv;
45-
42+
// Returns true if A is ordered before B.
43+
bool CompareOptionRecords(const Record *A, const Record *B) {
4644
// Sentinel options precede all others and are only ordered by precedence.
4745
bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
4846
bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
4947
if (ASent != BSent)
50-
return ASent ? -1 : 1;
48+
return ASent;
5149

5250
// Compare options by name, unless they are sentinels.
5351
if (!ASent)
5452
if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),
5553
B->getValueAsString("Name").str().c_str()))
56-
return Cmp;
54+
return Cmp < 0;
5755

5856
if (!ASent) {
5957
std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
@@ -65,7 +63,7 @@ int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
6563
BEPre = BPrefixes.end();
6664
APre != AEPre && BPre != BEPre; ++APre, ++BPre) {
6765
if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))
68-
return Cmp;
66+
return Cmp < 0;
6967
}
7068
}
7169

@@ -78,7 +76,7 @@ int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
7876
PrintError(B->getLoc(), Twine("Other defined here"));
7977
PrintFatalError("Equivalent Options found.");
8078
}
81-
return APrec < BPrec ? -1 : 1;
79+
return APrec < BPrec;
8280
}
8381

8482
} // namespace llvm

llvm/utils/TableGen/Common/OptEmitter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- OptEmitter.h - Helper for emitting options. --------------*- C++ -*-===//
1+
//===- OptEmitter.h - Helper for emitting options ---------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -11,7 +11,7 @@
1111

1212
namespace llvm {
1313
class Record;
14-
int CompareOptionRecords(Record *const *Av, Record *const *Bv);
14+
bool CompareOptionRecords(const Record *A, const Record *B);
1515
} // namespace llvm
1616

1717
#endif // LLVM_UTILS_TABLEGEN_COMMON_OPTEMITTER_H

llvm/utils/TableGen/ExegesisEmitter.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace {
3030

3131
class ExegesisEmitter {
3232
public:
33-
ExegesisEmitter(RecordKeeper &RK);
33+
ExegesisEmitter(const RecordKeeper &RK);
3434

3535
void run(raw_ostream &OS) const;
3636

@@ -51,23 +51,24 @@ class ExegesisEmitter {
5151

5252
void emitPfmCountersLookupTable(raw_ostream &OS) const;
5353

54-
RecordKeeper &Records;
54+
const RecordKeeper &Records;
5555
std::string Target;
5656

5757
// Table of counter name -> counter index.
5858
const std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
5959
};
6060

6161
static std::map<llvm::StringRef, unsigned>
62-
collectPfmCounters(RecordKeeper &Records) {
62+
collectPfmCounters(const RecordKeeper &Records) {
6363
std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
6464
const auto AddPfmCounterName = [&PfmCounterNameTable](
6565
const Record *PfmCounterDef) {
6666
const llvm::StringRef Counter = PfmCounterDef->getValueAsString("Counter");
6767
if (!Counter.empty())
6868
PfmCounterNameTable.emplace(Counter, 0);
6969
};
70-
for (Record *Def : Records.getAllDerivedDefinitions("ProcPfmCounters")) {
70+
for (const Record *Def :
71+
Records.getAllDerivedDefinitions("ProcPfmCounters")) {
7172
// Check that ResourceNames are unique.
7273
llvm::SmallSet<llvm::StringRef, 16> Seen;
7374
for (const Record *IssueCounter :
@@ -95,9 +96,9 @@ collectPfmCounters(RecordKeeper &Records) {
9596
return PfmCounterNameTable;
9697
}
9798

98-
ExegesisEmitter::ExegesisEmitter(RecordKeeper &RK)
99+
ExegesisEmitter::ExegesisEmitter(const RecordKeeper &RK)
99100
: Records(RK), PfmCounterNameTable(collectPfmCounters(RK)) {
100-
std::vector<Record *> Targets = Records.getAllDerivedDefinitions("Target");
101+
ArrayRef<const Record *> Targets = Records.getAllDerivedDefinitions("Target");
101102
if (Targets.size() == 0)
102103
PrintFatalError("No 'Target' subclasses defined!");
103104
if (Targets.size() != 1)
@@ -223,7 +224,7 @@ void ExegesisEmitter::emitPfmCounters(raw_ostream &OS) const {
223224
} // namespace
224225

225226
void ExegesisEmitter::emitPfmCountersLookupTable(raw_ostream &OS) const {
226-
std::vector<Record *> Bindings =
227+
std::vector<const Record *> Bindings =
227228
Records.getAllDerivedDefinitions("PfmCountersBinding");
228229
assert(!Bindings.empty() && "there must be at least one binding");
229230
llvm::sort(Bindings, [](const Record *L, const Record *R) {
@@ -232,7 +233,7 @@ void ExegesisEmitter::emitPfmCountersLookupTable(raw_ostream &OS) const {
232233

233234
OS << "// Sorted (by CpuName) array of pfm counters.\n"
234235
<< "static const CpuAndPfmCounters " << Target << "CpuPfmCounters[] = {\n";
235-
for (Record *Binding : Bindings) {
236+
for (const Record *Binding : Bindings) {
236237
// Emit as { "cpu", procinit },
237238
OS << " { \"" //
238239
<< Binding->getValueAsString("CpuName") << "\"," //

llvm/utils/TableGen/OptParserEmitter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ static void EmitHelpTextsForVariants(
250250
/// OptParserEmitter - This tablegen backend takes an input .td file
251251
/// describing a list of options and emits a data structure for parsing and
252252
/// working with those options when given an input command line.
253-
static void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
253+
static void EmitOptParser(const RecordKeeper &Records, raw_ostream &OS) {
254254
// Get the option groups and options.
255-
const std::vector<Record *> &Groups =
255+
ArrayRef<const Record *> Groups =
256256
Records.getAllDerivedDefinitions("OptionGroup");
257-
std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
257+
std::vector<const Record *> Opts = Records.getAllDerivedDefinitions("Option");
258258

259259
emitSourceFileHeader("Option Parsing Definitions", OS);
260260

261-
array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
261+
llvm::sort(Opts, CompareOptionRecords);
262262
// Generate prefix groups.
263263
typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
264264
typedef std::map<PrefixKeyT, std::string> PrefixesT;

llvm/utils/TableGen/OptRSTEmitter.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,24 @@ using namespace llvm;
1616

1717
/// OptParserEmitter - This tablegen backend takes an input .td file
1818
/// describing a list of options and emits a RST man page.
19-
static void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
20-
llvm::StringMap<std::vector<Record *>> OptionsByGroup;
19+
static void EmitOptRST(const RecordKeeper &Records, raw_ostream &OS) {
20+
llvm::StringMap<std::vector<const Record *>> OptionsByGroup;
2121
std::vector<Record *> OptionsWithoutGroup;
2222

2323
// Get the options.
24-
std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
25-
array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
24+
std::vector<const Record *> Opts = Records.getAllDerivedDefinitions("Option");
25+
llvm::sort(Opts, CompareOptionRecords);
2626

2727
// Get the option groups.
28-
const std::vector<Record *> &Groups =
29-
Records.getAllDerivedDefinitions("OptionGroup");
30-
for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
31-
const Record &R = *Groups[i];
32-
OptionsByGroup.try_emplace(R.getValueAsString("Name"));
33-
}
28+
for (const Record *R : Records.getAllDerivedDefinitions("OptionGroup"))
29+
OptionsByGroup.try_emplace(R->getValueAsString("Name"));
3430

3531
// Map options to their group.
36-
for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
37-
const Record &R = *Opts[i];
38-
if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
39-
OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);
40-
} else {
41-
OptionsByGroup["options"].push_back(Opts[i]);
42-
}
32+
for (const Record *R : Opts) {
33+
if (const DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group")))
34+
OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(R);
35+
else
36+
OptionsByGroup["options"].push_back(R);
4337
}
4438

4539
// Print options under their group.
@@ -49,7 +43,7 @@ static void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
4943
OS << std::string(GroupName.size(), '-') << '\n';
5044
OS << '\n';
5145

52-
for (Record *R : KV.getValue()) {
46+
for (const Record *R : KV.getValue()) {
5347
OS << ".. option:: ";
5448

5549
// Print the prefix.

0 commit comments

Comments
 (0)