Skip to content

Commit 7c53a7a

Browse files
authored
[clang][TableGen] Change OpenCL emitter to use const RecordKeeper (#108213)
Change OpenCL builtins emitter 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 0767027 commit 7c53a7a

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct BuiltinTableEntries {
8787
//
8888
class BuiltinNameEmitter {
8989
public:
90-
BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS)
90+
BuiltinNameEmitter(const RecordKeeper &Records, raw_ostream &OS)
9191
: Records(Records), OS(OS) {}
9292

9393
// Entrypoint to generate the functions and structures for checking
@@ -100,7 +100,7 @@ class BuiltinNameEmitter {
100100

101101
// Contains OpenCL builtin functions and related information, stored as
102102
// Record instances. They are coming from the associated TableGen file.
103-
RecordKeeper &Records;
103+
const RecordKeeper &Records;
104104

105105
// The output file.
106106
raw_ostream &OS;
@@ -113,7 +113,7 @@ class BuiltinNameEmitter {
113113
// \param Output (out) String containing the enums to emit in the output file.
114114
// \param List (out) List containing the extracted Types, except the Types in
115115
// TypesSeen.
116-
void ExtractEnumTypes(std::vector<Record *> &Types,
116+
void ExtractEnumTypes(ArrayRef<const Record *> Types,
117117
StringMap<bool> &TypesSeen, std::string &Output,
118118
std::vector<const Record *> &List);
119119

@@ -237,7 +237,7 @@ class BuiltinNameEmitter {
237237
/// Base class for emitting a file (e.g. header or test) from OpenCLBuiltins.td
238238
class OpenCLBuiltinFileEmitterBase {
239239
public:
240-
OpenCLBuiltinFileEmitterBase(RecordKeeper &Records, raw_ostream &OS)
240+
OpenCLBuiltinFileEmitterBase(const RecordKeeper &Records, raw_ostream &OS)
241241
: Records(Records), OS(OS) {}
242242
virtual ~OpenCLBuiltinFileEmitterBase() = default;
243243

@@ -305,7 +305,7 @@ class OpenCLBuiltinFileEmitterBase {
305305

306306
// Contains OpenCL builtin functions and related information, stored as
307307
// Record instances. They are coming from the associated TableGen file.
308-
RecordKeeper &Records;
308+
const RecordKeeper &Records;
309309

310310
// The output file.
311311
raw_ostream &OS;
@@ -316,7 +316,7 @@ class OpenCLBuiltinFileEmitterBase {
316316
// builtin function described in the .td input.
317317
class OpenCLBuiltinTestEmitter : public OpenCLBuiltinFileEmitterBase {
318318
public:
319-
OpenCLBuiltinTestEmitter(RecordKeeper &Records, raw_ostream &OS)
319+
OpenCLBuiltinTestEmitter(const RecordKeeper &Records, raw_ostream &OS)
320320
: OpenCLBuiltinFileEmitterBase(Records, OS) {}
321321

322322
// Entrypoint to generate the functions for testing all OpenCL builtin
@@ -329,7 +329,7 @@ class OpenCLBuiltinTestEmitter : public OpenCLBuiltinFileEmitterBase {
329329
// prototype for each builtin function described in the .td input.
330330
class OpenCLBuiltinHeaderEmitter : public OpenCLBuiltinFileEmitterBase {
331331
public:
332-
OpenCLBuiltinHeaderEmitter(RecordKeeper &Records, raw_ostream &OS)
332+
OpenCLBuiltinHeaderEmitter(const RecordKeeper &Records, raw_ostream &OS)
333333
: OpenCLBuiltinFileEmitterBase(Records, OS) {}
334334

335335
// Entrypoint to generate the header.
@@ -362,7 +362,7 @@ void BuiltinNameEmitter::Emit() {
362362
EmitQualTypeFinder();
363363
}
364364

365-
void BuiltinNameEmitter::ExtractEnumTypes(std::vector<Record *> &Types,
365+
void BuiltinNameEmitter::ExtractEnumTypes(ArrayRef<const Record *> Types,
366366
StringMap<bool> &TypesSeen,
367367
std::string &Output,
368368
std::vector<const Record *> &List) {
@@ -392,11 +392,11 @@ void BuiltinNameEmitter::EmitDeclarations() {
392392
// Extract generic types and non-generic types separately, to keep
393393
// gentypes at the end of the enum which simplifies the special handling
394394
// for gentypes in SemaLookup.
395-
std::vector<Record *> GenTypes =
395+
ArrayRef<const Record *> GenTypes =
396396
Records.getAllDerivedDefinitions("GenericType");
397397
ExtractEnumTypes(GenTypes, TypesSeen, GenTypeEnums, GenTypeList);
398398

399-
std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type");
399+
ArrayRef<const Record *> Types = Records.getAllDerivedDefinitions("Type");
400400
ExtractEnumTypes(Types, TypesSeen, TypeEnums, TypeList);
401401

402402
OS << TypeEnums;
@@ -499,15 +499,16 @@ static void VerifySignature(const std::vector<Record *> &Signature,
499499

500500
void BuiltinNameEmitter::GetOverloads() {
501501
// Populate the TypeMap.
502-
std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type");
502+
ArrayRef<const Record *> Types = Records.getAllDerivedDefinitions("Type");
503503
unsigned I = 0;
504504
for (const auto &T : Types) {
505505
TypeMap.insert(std::make_pair(T, I++));
506506
}
507507

508508
// Populate the SignaturesList and the FctOverloadMap.
509509
unsigned CumulativeSignIndex = 0;
510-
std::vector<Record *> Builtins = Records.getAllDerivedDefinitions("Builtin");
510+
ArrayRef<const Record *> Builtins =
511+
Records.getAllDerivedDefinitions("Builtin");
511512
for (const auto *B : Builtins) {
512513
StringRef BName = B->getValueAsString("Name");
513514
FctOverloadMap.try_emplace(BName);
@@ -535,7 +536,7 @@ void BuiltinNameEmitter::GetOverloads() {
535536
void BuiltinNameEmitter::EmitExtensionTable() {
536537
OS << "static const char *FunctionExtensionTable[] = {\n";
537538
unsigned Index = 0;
538-
std::vector<Record *> FuncExtensions =
539+
ArrayRef<const Record *> FuncExtensions =
539540
Records.getAllDerivedDefinitions("FunctionExtension");
540541

541542
for (const auto &FE : FuncExtensions) {
@@ -804,11 +805,11 @@ static void OCL2Qual(Sema &S, const OpenCLTypeStruct &Ty,
804805
OS << "\n switch (Ty.ID) {\n";
805806

806807
// Switch cases for image types (Image2d, Image3d, ...)
807-
std::vector<Record *> ImageTypes =
808+
ArrayRef<const Record *> ImageTypes =
808809
Records.getAllDerivedDefinitions("ImageType");
809810

810811
// Map an image type name to its 3 access-qualified types (RO, WO, RW).
811-
StringMap<SmallVector<Record *, 3>> ImageTypesMap;
812+
StringMap<SmallVector<const Record *, 3>> ImageTypesMap;
812813
for (auto *IT : ImageTypes)
813814
ImageTypesMap[IT->getValueAsString("Name")].push_back(IT);
814815

@@ -890,7 +891,7 @@ static void OCL2Qual(Sema &S, const OpenCLTypeStruct &Ty,
890891
// Switch cases for non generic, non image types (int, int4, float, ...).
891892
// Only insert the plain scalar type; vector information and type qualifiers
892893
// are added in step 2.
893-
std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type");
894+
ArrayRef<const Record *> Types = Records.getAllDerivedDefinitions("Type");
894895
StringMap<bool> TypesSeen;
895896

896897
for (const auto *T : Types) {
@@ -1211,7 +1212,8 @@ void OpenCLBuiltinTestEmitter::emit() {
12111212
unsigned TestID = 0;
12121213

12131214
// Iterate over all builtins.
1214-
std::vector<Record *> Builtins = Records.getAllDerivedDefinitions("Builtin");
1215+
ArrayRef<const Record *> Builtins =
1216+
Records.getAllDerivedDefinitions("Builtin");
12151217
for (const auto *B : Builtins) {
12161218
StringRef Name = B->getValueAsString("Name");
12171219

@@ -1274,7 +1276,8 @@ void OpenCLBuiltinHeaderEmitter::emit() {
12741276
)";
12751277

12761278
// Iterate over all builtins; sort to follow order of definition in .td file.
1277-
std::vector<Record *> Builtins = Records.getAllDerivedDefinitions("Builtin");
1279+
std::vector<const Record *> Builtins =
1280+
Records.getAllDerivedDefinitions("Builtin");
12781281
llvm::sort(Builtins, LessRecord());
12791282

12801283
for (const auto *B : Builtins) {
@@ -1319,18 +1322,19 @@ void OpenCLBuiltinHeaderEmitter::emit() {
13191322
"#pragma OPENCL EXTENSION all : disable\n";
13201323
}
13211324

1322-
void clang::EmitClangOpenCLBuiltins(RecordKeeper &Records, raw_ostream &OS) {
1325+
void clang::EmitClangOpenCLBuiltins(const RecordKeeper &Records,
1326+
raw_ostream &OS) {
13231327
BuiltinNameEmitter NameChecker(Records, OS);
13241328
NameChecker.Emit();
13251329
}
13261330

1327-
void clang::EmitClangOpenCLBuiltinHeader(RecordKeeper &Records,
1331+
void clang::EmitClangOpenCLBuiltinHeader(const RecordKeeper &Records,
13281332
raw_ostream &OS) {
13291333
OpenCLBuiltinHeaderEmitter HeaderFileGenerator(Records, OS);
13301334
HeaderFileGenerator.emit();
13311335
}
13321336

1333-
void clang::EmitClangOpenCLBuiltinTests(RecordKeeper &Records,
1337+
void clang::EmitClangOpenCLBuiltinTests(const RecordKeeper &Records,
13341338
raw_ostream &OS) {
13351339
OpenCLBuiltinTestEmitter TestFileGenerator(Records, OS);
13361340
TestFileGenerator.emit();

clang/utils/TableGen/TableGenBackends.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ void EmitClangAttrDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
145145
void EmitClangDiagDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
146146
void EmitClangOptDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
147147

148-
void EmitClangOpenCLBuiltins(llvm::RecordKeeper &Records,
148+
void EmitClangOpenCLBuiltins(const llvm::RecordKeeper &Records,
149149
llvm::raw_ostream &OS);
150-
void EmitClangOpenCLBuiltinHeader(llvm::RecordKeeper &Records,
150+
void EmitClangOpenCLBuiltinHeader(const llvm::RecordKeeper &Records,
151151
llvm::raw_ostream &OS);
152-
void EmitClangOpenCLBuiltinTests(llvm::RecordKeeper &Records,
152+
void EmitClangOpenCLBuiltinTests(const llvm::RecordKeeper &Records,
153153
llvm::raw_ostream &OS);
154154

155155
void EmitClangDataCollectors(const llvm::RecordKeeper &Records,

0 commit comments

Comments
 (0)