Skip to content

Commit 5ee1d2e

Browse files
committed
[TableGen] Add const variants of accessors for backend
Split RecordKeeper `getAllDerivedDefinitions` family of functions into two variants: (a) non-const ones that return vectors of `Record *` and (b) const ones, that return vectors of `const Record *`. This will help gradual migration of backend to use `const RecordKeeper` and by implication change code to work with const pointers.
1 parent ddaf2e2 commit 5ee1d2e

File tree

19 files changed

+141
-86
lines changed

19 files changed

+141
-86
lines changed

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) {
189189

190190
typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
191191

192-
static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
192+
static ParsedAttrMap getParsedAttrList(RecordKeeper &Records,
193193
ParsedAttrMap *Dupes = nullptr,
194194
bool SemaOnly = true) {
195195
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
@@ -4344,7 +4344,7 @@ static void GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
43444344
// written into OS and the checks for merging declaration attributes are
43454345
// written into MergeOS.
43464346
static void GenerateMutualExclusionsChecks(const Record &Attr,
4347-
const RecordKeeper &Records,
4347+
RecordKeeper &Records,
43484348
raw_ostream &OS,
43494349
raw_ostream &MergeDeclOS,
43504350
raw_ostream &MergeStmtOS) {

clang/utils/TableGen/ClangSyntaxEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ using llvm::formatv;
4141
// stable and useful way, where abstract Node subclasses correspond to ranges.
4242
class Hierarchy {
4343
public:
44-
Hierarchy(const llvm::RecordKeeper &Records) {
44+
Hierarchy(llvm::RecordKeeper &Records) {
4545
for (llvm::Record *T : Records.getAllDerivedDefinitions("NodeType"))
4646
add(T);
4747
for (llvm::Record *Derived : Records.getAllDerivedDefinitions("NodeType"))

llvm/include/llvm/TableGen/DirectiveEmitter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace llvm {
1515
// DirectiveBase.td and provides helper methods for accessing it.
1616
class DirectiveLanguage {
1717
public:
18-
explicit DirectiveLanguage(const llvm::RecordKeeper &Records)
19-
: Records(Records) {
18+
explicit DirectiveLanguage(llvm::RecordKeeper &Records) : Records(Records) {
2019
const auto &DirectiveLanguages = getDirectiveLanguages();
2120
Def = DirectiveLanguages[0];
2221
}
@@ -71,7 +70,7 @@ class DirectiveLanguage {
7170

7271
private:
7372
const llvm::Record *Def;
74-
const llvm::RecordKeeper &Records;
73+
llvm::RecordKeeper &Records;
7574

7675
std::vector<Record *> getDirectiveLanguages() const {
7776
return Records.getAllDerivedDefinitions("DirectiveLanguage");

llvm/include/llvm/TableGen/Record.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class RecordRecTy final : public RecTy, public FoldingSetNode,
264264

265265
std::string getAsString() const override;
266266

267-
bool isSubClassOf(Record *Class) const;
267+
bool isSubClassOf(const Record *Class) const;
268268
bool typeIsConvertibleTo(const RecTy *RHS) const override;
269269

270270
bool typeIsA(const RecTy *RHS) const override;
@@ -2057,19 +2057,28 @@ class RecordKeeper {
20572057
//===--------------------------------------------------------------------===//
20582058
// High-level helper methods, useful for tablegen backends.
20592059

2060+
// Non-const methods return std::vector<Record *> by value or reference.
2061+
// Const methods return std::vector<const Record *> by value or reference.
2062+
20602063
/// Get all the concrete records that inherit from the one specified
20612064
/// class. The class must be defined.
2062-
std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
2065+
const std::vector<const Record *> &
2066+
getAllDerivedDefinitions(StringRef ClassName) const;
2067+
const std::vector<Record *> &getAllDerivedDefinitions(StringRef ClassName);
20632068

20642069
/// Get all the concrete records that inherit from all the specified
20652070
/// classes. The classes must be defined.
2066-
std::vector<Record *> getAllDerivedDefinitions(
2067-
ArrayRef<StringRef> ClassNames) const;
2071+
std::vector<const Record *>
2072+
getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames) const;
2073+
std::vector<Record *>
2074+
getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames);
20682075

20692076
/// Get all the concrete records that inherit from specified class, if the
20702077
/// class is defined. Returns an empty vector if the class is not defined.
2071-
std::vector<Record *>
2078+
const std::vector<const Record *> &
20722079
getAllDerivedDefinitionsIfDefined(StringRef ClassName) const;
2080+
const std::vector<Record *> &
2081+
getAllDerivedDefinitionsIfDefined(StringRef ClassName);
20732082

20742083
void dump() const;
20752084

@@ -2079,9 +2088,24 @@ class RecordKeeper {
20792088
RecordKeeper &operator=(RecordKeeper &&) = delete;
20802089
RecordKeeper &operator=(const RecordKeeper &) = delete;
20812090

2091+
// Helper template functions for backend accessors.
2092+
template <typename VecTy>
2093+
const VecTy &
2094+
getAllDerivedDefinitionsImpl(StringRef ClassName,
2095+
std::map<std::string, VecTy> &Cache) const;
2096+
2097+
template <typename VecTy>
2098+
VecTy getAllDerivedDefinitionsImpl(ArrayRef<StringRef> ClassNames) const;
2099+
2100+
template <typename VecTy>
2101+
const VecTy &getAllDerivedDefinitionsIfDefinedImpl(
2102+
StringRef ClassName, std::map<std::string, VecTy> &Cache) const;
2103+
20822104
std::string InputFilename;
20832105
RecordMap Classes, Defs;
2084-
mutable StringMap<std::vector<Record *>> ClassRecordsMap;
2106+
mutable std::map<std::string, std::vector<const Record *>>
2107+
ClassRecordsMapConst;
2108+
mutable std::map<std::string, std::vector<Record *>> ClassRecordsMap;
20852109
GlobalMap ExtraGlobals;
20862110

20872111
// These members are for the phase timing feature. We need a timer group,

llvm/lib/TableGen/Record.cpp

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,10 @@ std::string RecordRecTy::getAsString() const {
266266
return Str;
267267
}
268268

269-
bool RecordRecTy::isSubClassOf(Record *Class) const {
270-
return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
271-
return MySuperClass == Class ||
272-
MySuperClass->isSubClassOf(Class);
273-
});
269+
bool RecordRecTy::isSubClassOf(const Record *Class) const {
270+
return llvm::any_of(getClasses(), [Class](const Record *MySuperClass) {
271+
return MySuperClass == Class || MySuperClass->isSubClassOf(Class);
272+
});
274273
}
275274

276275
bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
@@ -3219,46 +3218,83 @@ void RecordKeeper::stopBackendTimer() {
32193218
}
32203219
}
32213220

3222-
std::vector<Record *>
3223-
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
3221+
template <typename VecTy>
3222+
const VecTy &RecordKeeper::getAllDerivedDefinitionsImpl(
3223+
StringRef ClassName, std::map<std::string, VecTy> &Cache) const {
32243224
// We cache the record vectors for single classes. Many backends request
32253225
// the same vectors multiple times.
3226-
auto Pair = ClassRecordsMap.try_emplace(ClassName);
3226+
auto Pair = Cache.try_emplace(ClassName.str());
32273227
if (Pair.second)
3228-
Pair.first->second = getAllDerivedDefinitions(ArrayRef(ClassName));
3228+
Pair.first->second =
3229+
getAllDerivedDefinitionsImpl<VecTy>(ArrayRef(ClassName));
32293230

32303231
return Pair.first->second;
32313232
}
32323233

3233-
std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
3234+
template <typename VecTy>
3235+
VecTy RecordKeeper::getAllDerivedDefinitionsImpl(
32343236
ArrayRef<StringRef> ClassNames) const {
3235-
SmallVector<Record *, 2> ClassRecs;
3236-
std::vector<Record *> Defs;
3237+
SmallVector<const Record *, 2> ClassRecs;
3238+
VecTy Defs;
32373239

32383240
assert(ClassNames.size() > 0 && "At least one class must be passed.");
32393241
for (const auto &ClassName : ClassNames) {
3240-
Record *Class = getClass(ClassName);
3242+
const Record *Class = getClass(ClassName);
32413243
if (!Class)
32423244
PrintFatalError("The class '" + ClassName + "' is not defined\n");
32433245
ClassRecs.push_back(Class);
32443246
}
32453247

32463248
for (const auto &OneDef : getDefs()) {
32473249
if (all_of(ClassRecs, [&OneDef](const Record *Class) {
3248-
return OneDef.second->isSubClassOf(Class);
3249-
}))
3250+
return OneDef.second->isSubClassOf(Class);
3251+
}))
32503252
Defs.push_back(OneDef.second.get());
32513253
}
3252-
32533254
llvm::sort(Defs, LessRecord());
3254-
32553255
return Defs;
32563256
}
32573257

3258+
template <typename VecTy>
3259+
const VecTy &RecordKeeper::getAllDerivedDefinitionsIfDefinedImpl(
3260+
StringRef ClassName, std::map<std::string, VecTy> &Cache) const {
3261+
return getClass(ClassName)
3262+
? getAllDerivedDefinitionsImpl<VecTy>(ClassName, Cache)
3263+
: Cache[""];
3264+
}
3265+
3266+
const std::vector<const Record *> &
3267+
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
3268+
return getAllDerivedDefinitionsImpl<std::vector<const Record *>>(
3269+
ClassName, ClassRecordsMapConst);
3270+
}
3271+
3272+
const std::vector<Record *> &
3273+
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) {
3274+
return getAllDerivedDefinitionsImpl<std::vector<Record *>>(ClassName,
3275+
ClassRecordsMap);
3276+
}
3277+
3278+
std::vector<const Record *>
3279+
RecordKeeper::getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames) const {
3280+
return getAllDerivedDefinitionsImpl<std::vector<const Record *>>(ClassNames);
3281+
}
3282+
32583283
std::vector<Record *>
3284+
RecordKeeper::getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames) {
3285+
return getAllDerivedDefinitionsImpl<std::vector<Record *>>(ClassNames);
3286+
}
3287+
3288+
const std::vector<const Record *> &
32593289
RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) const {
3260-
return getClass(ClassName) ? getAllDerivedDefinitions(ClassName)
3261-
: std::vector<Record *>();
3290+
return getAllDerivedDefinitionsIfDefinedImpl<std::vector<const Record *>>(
3291+
ClassName, ClassRecordsMapConst);
3292+
}
3293+
3294+
const std::vector<Record *> &
3295+
RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) {
3296+
return getAllDerivedDefinitionsIfDefinedImpl<std::vector<Record *>>(
3297+
ClassName, ClassRecordsMap);
32623298
}
32633299

32643300
Init *MapResolver::resolve(Init *VarName) {

llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ using namespace llvm;
2626
//===----------------------------------------------------------------------===//
2727

2828
CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
29-
std::vector<Record *> IntrProperties =
30-
RC.getAllDerivedDefinitions("IntrinsicProperty");
31-
3229
std::vector<const Record *> DefaultProperties;
33-
for (const Record *Rec : IntrProperties)
30+
for (const Record *Rec : RC.getAllDerivedDefinitions("IntrinsicProperty"))
3431
if (Rec->getValueAsBit("IsDefault"))
3532
DefaultProperties.push_back(Rec);
3633

37-
std::vector<Record *> Defs = RC.getAllDerivedDefinitions("Intrinsic");
34+
const std::vector<const Record *> &Defs =
35+
RC.getAllDerivedDefinitions("Intrinsic");
3836
Intrinsics.reserve(Defs.size());
3937

4038
for (const Record *Def : Defs)

llvm/utils/TableGen/Common/SubtargetFeatureInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
2121
#endif
2222

2323
std::vector<std::pair<Record *, SubtargetFeatureInfo>>
24-
SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {
24+
SubtargetFeatureInfo::getAll(RecordKeeper &Records) {
2525
std::vector<std::pair<Record *, SubtargetFeatureInfo>> SubtargetFeatures;
2626
std::vector<Record *> AllPredicates =
2727
Records.getAllDerivedDefinitions("Predicate");

llvm/utils/TableGen/Common/SubtargetFeatureInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct SubtargetFeatureInfo {
4949

5050
void dump() const;
5151
static std::vector<std::pair<Record *, SubtargetFeatureInfo>>
52-
getAll(const RecordKeeper &Records);
52+
getAll(RecordKeeper &Records);
5353

5454
/// Emit the subtarget feature flag definitions.
5555
///

llvm/utils/TableGen/ExegesisEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ExegesisEmitter {
5959
};
6060

6161
static std::map<llvm::StringRef, unsigned>
62-
collectPfmCounters(const RecordKeeper &Records) {
62+
collectPfmCounters(RecordKeeper &Records) {
6363
std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
6464
const auto AddPfmCounterName = [&PfmCounterNameTable](
6565
const Record *PfmCounterDef) {

llvm/utils/TableGen/GlobalISelEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
335335
private:
336336
std::string ClassName;
337337

338-
const RecordKeeper &RK;
338+
RecordKeeper &RK;
339339
const CodeGenDAGPatterns CGP;
340340
const CodeGenTarget &Target;
341341
CodeGenRegBank &CGRegs;

llvm/utils/TableGen/SubtargetEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ void SubtargetEmitter::EmitSchedModel(raw_ostream &OS) {
15451545
EmitProcessorModels(OS);
15461546
}
15471547

1548-
static void emitPredicateProlog(const RecordKeeper &Records, raw_ostream &OS) {
1548+
static void emitPredicateProlog(RecordKeeper &Records, raw_ostream &OS) {
15491549
std::string Buffer;
15501550
raw_string_ostream Stream(Buffer);
15511551

llvm/utils/TableGen/TableGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void PrintEnums(RecordKeeper &Records, raw_ostream &OS) {
5252
static void PrintSets(const RecordKeeper &Records, raw_ostream &OS) {
5353
SetTheory Sets;
5454
Sets.addFieldExpander("Set", "Elements");
55-
for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
55+
for (const Record *Rec : Records.getAllDerivedDefinitions("Set")) {
5656
OS << Rec->getName() << " = [";
5757
const std::vector<Record *> *Elts = Sets.expand(Rec);
5858
assert(Elts && "Couldn't expand Set instance");

mlir/include/mlir/TableGen/GenInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class RecordKeeper;
2121
namespace mlir {
2222

2323
/// Generator function to invoke.
24-
using GenFunction = std::function<bool(const llvm::RecordKeeper &recordKeeper,
25-
raw_ostream &os)>;
24+
using GenFunction =
25+
std::function<bool(llvm::RecordKeeper &recordKeeper, raw_ostream &os)>;
2626

2727
/// Structure to group information about a generator (argument to invoke via
2828
/// mlir-tblgen, description, and generator function).
@@ -34,7 +34,7 @@ class GenInfo {
3434
: arg(arg), description(description), generator(std::move(generator)) {}
3535

3636
/// Invokes the generator and returns whether the generator failed.
37-
bool invoke(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) const {
37+
bool invoke(llvm::RecordKeeper &recordKeeper, raw_ostream &os) const {
3838
assert(generator && "Cannot call generator with null generator");
3939
return generator(recordKeeper, os);
4040
}

0 commit comments

Comments
 (0)