Skip to content

Commit 664c9fa

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 13013bd commit 664c9fa

File tree

19 files changed

+134
-76
lines changed

19 files changed

+134
-76
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
2062+
// ArrayRef<const Record *>.
2063+
20602064
/// Get all the concrete records that inherit from the one specified
20612065
/// class. The class must be defined.
2062-
std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
2066+
ArrayRef<const Record *> 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+
ArrayRef<const Record *>
20722079
getAllDerivedDefinitionsIfDefined(StringRef ClassName) const;
2080+
const std::vector<Record *> &
2081+
getAllDerivedDefinitionsIfDefined(StringRef ClassName);
20732082

20742083
void dump() const;
20752084

@@ -2081,9 +2090,24 @@ class RecordKeeper {
20812090
RecordKeeper &operator=(RecordKeeper &&) = delete;
20822091
RecordKeeper &operator=(const RecordKeeper &) = delete;
20832092

2093+
// Helper template functions for backend accessors.
2094+
template <typename VecTy>
2095+
const VecTy &
2096+
getAllDerivedDefinitionsImpl(StringRef ClassName,
2097+
std::map<std::string, VecTy> &Cache) const;
2098+
2099+
template <typename VecTy>
2100+
VecTy getAllDerivedDefinitionsImpl(ArrayRef<StringRef> ClassNames) const;
2101+
2102+
template <typename VecTy>
2103+
const VecTy &getAllDerivedDefinitionsIfDefinedImpl(
2104+
StringRef ClassName, std::map<std::string, VecTy> &Cache) const;
2105+
20842106
std::string InputFilename;
20852107
RecordMap Classes, Defs;
2086-
mutable StringMap<std::vector<Record *>> ClassRecordsMap;
2108+
mutable std::map<std::string, std::vector<const Record *>>
2109+
ClassRecordsMapConst;
2110+
mutable std::map<std::string, std::vector<Record *>> ClassRecordsMap;
20872111
GlobalMap ExtraGlobals;
20882112

20892113
// 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
@@ -3248,46 +3248,83 @@ void RecordKeeper::stopBackendTimer() {
32483248
}
32493249
}
32503250

3251-
std::vector<Record *>
3252-
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
3251+
template <typename VecTy>
3252+
const VecTy &RecordKeeper::getAllDerivedDefinitionsImpl(
3253+
StringRef ClassName, std::map<std::string, VecTy> &Cache) const {
32533254
// We cache the record vectors for single classes. Many backends request
32543255
// the same vectors multiple times.
3255-
auto Pair = ClassRecordsMap.try_emplace(ClassName);
3256+
auto Pair = Cache.try_emplace(ClassName.str());
32563257
if (Pair.second)
3257-
Pair.first->second = getAllDerivedDefinitions(ArrayRef(ClassName));
3258+
Pair.first->second =
3259+
getAllDerivedDefinitionsImpl<VecTy>(ArrayRef(ClassName));
32583260

32593261
return Pair.first->second;
32603262
}
32613263

3262-
std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
3264+
template <typename VecTy>
3265+
VecTy RecordKeeper::getAllDerivedDefinitionsImpl(
32633266
ArrayRef<StringRef> ClassNames) const {
3264-
SmallVector<Record *, 2> ClassRecs;
3265-
std::vector<Record *> Defs;
3267+
SmallVector<const Record *, 2> ClassRecs;
3268+
VecTy Defs;
32663269

32673270
assert(ClassNames.size() > 0 && "At least one class must be passed.");
32683271
for (const auto &ClassName : ClassNames) {
3269-
Record *Class = getClass(ClassName);
3272+
const Record *Class = getClass(ClassName);
32703273
if (!Class)
32713274
PrintFatalError("The class '" + ClassName + "' is not defined\n");
32723275
ClassRecs.push_back(Class);
32733276
}
32743277

32753278
for (const auto &OneDef : getDefs()) {
32763279
if (all_of(ClassRecs, [&OneDef](const Record *Class) {
3277-
return OneDef.second->isSubClassOf(Class);
3278-
}))
3280+
return OneDef.second->isSubClassOf(Class);
3281+
}))
32793282
Defs.push_back(OneDef.second.get());
32803283
}
3281-
32823284
llvm::sort(Defs, LessRecord());
3283-
32843285
return Defs;
32853286
}
32863287

3288+
template <typename VecTy>
3289+
const VecTy &RecordKeeper::getAllDerivedDefinitionsIfDefinedImpl(
3290+
StringRef ClassName, std::map<std::string, VecTy> &Cache) const {
3291+
return getClass(ClassName)
3292+
? getAllDerivedDefinitionsImpl<VecTy>(ClassName, Cache)
3293+
: Cache[""];
3294+
}
3295+
3296+
ArrayRef<const Record *>
3297+
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const {
3298+
return getAllDerivedDefinitionsImpl<std::vector<const Record *>>(
3299+
ClassName, ClassRecordsMapConst);
3300+
}
3301+
3302+
const std::vector<Record *> &
3303+
RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) {
3304+
return getAllDerivedDefinitionsImpl<std::vector<Record *>>(ClassName,
3305+
ClassRecordsMap);
3306+
}
3307+
3308+
std::vector<const Record *>
3309+
RecordKeeper::getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames) const {
3310+
return getAllDerivedDefinitionsImpl<std::vector<const Record *>>(ClassNames);
3311+
}
3312+
32873313
std::vector<Record *>
3314+
RecordKeeper::getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames) {
3315+
return getAllDerivedDefinitionsImpl<std::vector<Record *>>(ClassNames);
3316+
}
3317+
3318+
ArrayRef<const Record *>
32883319
RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) const {
3289-
return getClass(ClassName) ? getAllDerivedDefinitions(ClassName)
3290-
: std::vector<Record *>();
3320+
return getAllDerivedDefinitionsIfDefinedImpl<std::vector<const Record *>>(
3321+
ClassName, ClassRecordsMapConst);
3322+
}
3323+
3324+
const std::vector<Record *> &
3325+
RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) {
3326+
return getAllDerivedDefinitionsIfDefinedImpl<std::vector<Record *>>(
3327+
ClassName, ClassRecordsMap);
32913328
}
32923329

32933330
void RecordKeeper::dumpAllocationStats(raw_ostream &OS) const {

llvm/utils/TableGen/Basic/CodeGenIntrinsics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ CodeGenIntrinsicContext::CodeGenIntrinsicContext(const RecordKeeper &RC) {
3434
CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
3535
CodeGenIntrinsicContext Ctx(RC);
3636

37-
std::vector<Record *> Defs = RC.getAllDerivedDefinitions("Intrinsic");
37+
ArrayRef<const Record *> Defs = RC.getAllDerivedDefinitions("Intrinsic");
3838
Intrinsics.reserve(Defs.size());
3939

4040
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
}

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,10 @@ class DefGenerator {
690690
bool emitDefs(StringRef selectedDialect);
691691

692692
protected:
693-
DefGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os,
693+
DefGenerator(const std::vector<llvm::Record *> &defs, raw_ostream &os,
694694
StringRef defType, StringRef valueType, bool isAttrGenerator)
695-
: defRecords(std::move(defs)), os(os), defType(defType),
696-
valueType(valueType), isAttrGenerator(isAttrGenerator) {
695+
: defRecords(defs), os(os), defType(defType), valueType(valueType),
696+
isAttrGenerator(isAttrGenerator) {
697697
// Sort by occurrence in file.
698698
llvm::sort(defRecords, [](llvm::Record *lhs, llvm::Record *rhs) {
699699
return lhs->getID() < rhs->getID();
@@ -721,13 +721,13 @@ class DefGenerator {
721721

722722
/// A specialized generator for AttrDefs.
723723
struct AttrDefGenerator : public DefGenerator {
724-
AttrDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
724+
AttrDefGenerator(llvm::RecordKeeper &records, raw_ostream &os)
725725
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os,
726726
"Attr", "Attribute", /*isAttrGenerator=*/true) {}
727727
};
728728
/// A specialized generator for TypeDefs.
729729
struct TypeDefGenerator : public DefGenerator {
730-
TypeDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
730+
TypeDefGenerator(llvm::RecordKeeper &records, raw_ostream &os)
731731
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("TypeDef"), os,
732732
"Type", "Type", /*isAttrGenerator=*/false) {}
733733
};
@@ -1029,7 +1029,7 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
10291029

10301030
/// Find all type constraints for which a C++ function should be generated.
10311031
static std::vector<Constraint>
1032-
getAllTypeConstraints(const llvm::RecordKeeper &records) {
1032+
getAllTypeConstraints(llvm::RecordKeeper &records) {
10331033
std::vector<Constraint> result;
10341034
for (llvm::Record *def :
10351035
records.getAllDerivedDefinitionsIfDefined("TypeConstraint")) {
@@ -1046,7 +1046,7 @@ getAllTypeConstraints(const llvm::RecordKeeper &records) {
10461046
return result;
10471047
}
10481048

1049-
static void emitTypeConstraintDecls(const llvm::RecordKeeper &records,
1049+
static void emitTypeConstraintDecls(llvm::RecordKeeper &records,
10501050
raw_ostream &os) {
10511051
static const char *const typeConstraintDecl = R"(
10521052
bool {0}(::mlir::Type type);
@@ -1056,7 +1056,7 @@ bool {0}(::mlir::Type type);
10561056
os << strfmt(typeConstraintDecl, *constr.getCppFunctionName());
10571057
}
10581058

1059-
static void emitTypeConstraintDefs(const llvm::RecordKeeper &records,
1059+
static void emitTypeConstraintDefs(llvm::RecordKeeper &records,
10601060
raw_ostream &os) {
10611061
static const char *const typeConstraintDef = R"(
10621062
bool {0}(::mlir::Type type) {
@@ -1087,13 +1087,13 @@ static llvm::cl::opt<std::string>
10871087

10881088
static mlir::GenRegistration
10891089
genAttrDefs("gen-attrdef-defs", "Generate AttrDef definitions",
1090-
[](const llvm::RecordKeeper &records, raw_ostream &os) {
1090+
[](llvm::RecordKeeper &records, raw_ostream &os) {
10911091
AttrDefGenerator generator(records, os);
10921092
return generator.emitDefs(attrDialect);
10931093
});
10941094
static mlir::GenRegistration
10951095
genAttrDecls("gen-attrdef-decls", "Generate AttrDef declarations",
1096-
[](const llvm::RecordKeeper &records, raw_ostream &os) {
1096+
[](llvm::RecordKeeper &records, raw_ostream &os) {
10971097
AttrDefGenerator generator(records, os);
10981098
return generator.emitDecls(attrDialect);
10991099
});
@@ -1109,28 +1109,28 @@ static llvm::cl::opt<std::string>
11091109

11101110
static mlir::GenRegistration
11111111
genTypeDefs("gen-typedef-defs", "Generate TypeDef definitions",
1112-
[](const llvm::RecordKeeper &records, raw_ostream &os) {
1112+
[](llvm::RecordKeeper &records, raw_ostream &os) {
11131113
TypeDefGenerator generator(records, os);
11141114
return generator.emitDefs(typeDialect);
11151115
});
11161116
static mlir::GenRegistration
11171117
genTypeDecls("gen-typedef-decls", "Generate TypeDef declarations",
1118-
[](const llvm::RecordKeeper &records, raw_ostream &os) {
1118+
[](llvm::RecordKeeper &records, raw_ostream &os) {
11191119
TypeDefGenerator generator(records, os);
11201120
return generator.emitDecls(typeDialect);
11211121
});
11221122

11231123
static mlir::GenRegistration
11241124
genTypeConstrDefs("gen-type-constraint-defs",
11251125
"Generate type constraint definitions",
1126-
[](const llvm::RecordKeeper &records, raw_ostream &os) {
1126+
[](llvm::RecordKeeper &records, raw_ostream &os) {
11271127
emitTypeConstraintDefs(records, os);
11281128
return false;
11291129
});
11301130
static mlir::GenRegistration
11311131
genTypeConstrDecls("gen-type-constraint-decls",
11321132
"Generate type constraint declarations",
1133-
[](const llvm::RecordKeeper &records, raw_ostream &os) {
1133+
[](llvm::RecordKeeper &records, raw_ostream &os) {
11341134
emitTypeConstraintDecls(records, os);
11351135
return false;
11361136
});

0 commit comments

Comments
 (0)