Skip to content

Commit ac32657

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 e5c7cde commit ac32657

File tree

19 files changed

+136
-80
lines changed

19 files changed

+136
-80
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: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,46 +3219,83 @@ void RecordKeeper::stopBackendTimer() {
32193219
}
32203220
}
32213221

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

32303232
return Pair.first->second;
32313233
}
32323234

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

32383241
assert(ClassNames.size() > 0 && "At least one class must be passed.");
32393242
for (const auto &ClassName : ClassNames) {
3240-
Record *Class = getClass(ClassName);
3243+
const Record *Class = getClass(ClassName);
32413244
if (!Class)
32423245
PrintFatalError("The class '" + ClassName + "' is not defined\n");
32433246
ClassRecs.push_back(Class);
32443247
}
32453248

32463249
for (const auto &OneDef : getDefs()) {
32473250
if (all_of(ClassRecs, [&OneDef](const Record *Class) {
3248-
return OneDef.second->isSubClassOf(Class);
3249-
}))
3251+
return OneDef.second->isSubClassOf(Class);
3252+
}))
32503253
Defs.push_back(OneDef.second.get());
32513254
}
3252-
32533255
llvm::sort(Defs, LessRecord());
3254-
32553256
return Defs;
32563257
}
32573258

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

32643301
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)