Skip to content

Commit e54fc66

Browse files
committed
[TableGen] Allow emitter actions to use const RecordKeeper &
- Refactor TableGen backend options to allow specifying an action function that takes a const reference to `RecordKeeper`. This will enable gradual migration of code to use const references and pointers to `RecordKeeper` and `Record` in the TableGen backend. - Refactor handling of these action command line options. Split `Action` into 2 global variables `ActionConst` for actions that take a const reference and `ActionNonConst` for actions that take a non-const reference. Also move these variables from the header to the CPP file and added a function `ApplyAction` to apply one of these actions. - Change some existing actions to take const reference. - Change global variables in TableGen.cpp to be static instead of being in an anonymous namespace per LLVM coding standards.
1 parent e9e3a18 commit e54fc66

File tree

7 files changed

+78
-43
lines changed

7 files changed

+78
-43
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ class Record {
17571757
ArrayRef<AssertionInfo> getAssertions() const { return Assertions; }
17581758
ArrayRef<DumpInfo> getDumps() const { return Dumps; }
17591759

1760-
ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
1760+
ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
17611761
return SuperClasses;
17621762
}
17631763

llvm/include/llvm/TableGen/TableGenBackend.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#define LLVM_TABLEGEN_TABLEGENBACKEND_H
1515

1616
#include "llvm/ADT/StringRef.h"
17-
#include "llvm/Support/CommandLine.h"
18-
#include "llvm/Support/ManagedStatic.h"
1917
#include "llvm/TableGen/Record.h"
2018

2119
namespace llvm {
@@ -24,20 +22,13 @@ class RecordKeeper;
2422
class raw_ostream;
2523

2624
namespace TableGen::Emitter {
27-
using FnT = void (*)(RecordKeeper &Records, raw_ostream &OS);
28-
29-
struct OptCreatorT {
30-
static void *call();
31-
};
32-
33-
extern ManagedStatic<cl::opt<FnT>, OptCreatorT> Action;
25+
// Support for const and non-const forms of action functions.
26+
using FnNonConstT = void (*)(RecordKeeper &Records, raw_ostream &OS);
27+
using FnConstT = void (*)(const RecordKeeper &Records, raw_ostream &OS);
3428

3529
struct Opt {
36-
Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault = false) {
37-
if (ByDefault)
38-
Action->setInitialValue(CB);
39-
Action->getParser().addLiteralOption(Name, CB, Desc);
40-
}
30+
Opt(StringRef Name, FnNonConstT CB, StringRef Desc, bool ByDefault = false);
31+
Opt(StringRef Name, FnConstT CB, StringRef Desc, bool ByDefault = false);
4132
};
4233

4334
template <class EmitterC> class OptClass : Opt {
@@ -47,6 +38,10 @@ template <class EmitterC> class OptClass : Opt {
4738
OptClass(StringRef Name, StringRef Desc) : Opt(Name, run, Desc) {}
4839
};
4940

41+
/// Apply action specified on the command line. Returns false is an action
42+
/// was applied.
43+
bool ApplyAction(RecordKeeper &Records, raw_ostream &OS);
44+
5045
} // namespace TableGen::Emitter
5146

5247
/// emitSourceFileHeader - Output an LLVM style file header to the specified

llvm/lib/TableGen/Main.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,10 @@ int llvm::TableGenMain(const char *argv0,
131131
std::string OutString;
132132
raw_string_ostream Out(OutString);
133133
unsigned status = 0;
134-
TableGen::Emitter::FnT ActionFn = TableGen::Emitter::Action->getValue();
135-
if (ActionFn)
136-
ActionFn(Records, Out);
137-
else if (MainFn)
138-
status = MainFn(Out, Records);
139-
else
140-
return 1;
134+
// ApplyAction will return true if it did not apply any action. In that case,
135+
// attempt to apply the MainFn.
136+
if (TableGen::Emitter::ApplyAction(Records, Out))
137+
status = MainFn ? MainFn(Out, Records) : 1;
141138
Records.stopBackendTimer();
142139
if (status)
143140
return 1;

llvm/lib/TableGen/TableGenBackend.cpp

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,67 @@
1212

1313
#include "llvm/TableGen/TableGenBackend.h"
1414
#include "llvm/ADT/Twine.h"
15+
#include "llvm/Support/CommandLine.h"
16+
#include "llvm/Support/ManagedStatic.h"
1517
#include "llvm/Support/Path.h"
1618
#include "llvm/Support/raw_ostream.h"
1719
#include <algorithm>
1820
#include <cassert>
1921
#include <cstddef>
22+
#include <variant>
2023

2124
using namespace llvm;
25+
using namespace TableGen;
26+
using namespace Emitter;
2227

2328
const size_t MAX_LINE_LEN = 80U;
2429

25-
namespace llvm::TableGen::Emitter {
26-
ManagedStatic<cl::opt<FnT>, OptCreatorT> Action;
27-
void *OptCreatorT::call() {
28-
return new cl::opt<FnT>(cl::desc("Action to perform:"));
30+
namespace {
31+
template <typename FnT> struct OptCreatorT {
32+
static void *call() {
33+
return new cl::opt<FnT>(cl::desc("Action to perform:"));
34+
}
35+
};
36+
} // namespace
37+
38+
// `ActionNonConst` and `ActionConst` will be initialized if the corresponding
39+
// option is seen on the command line. `DefaultAction` will be initialized to
40+
// the action function if an option is specified as default.
41+
static ManagedStatic<cl::opt<FnNonConstT>, OptCreatorT<FnNonConstT>>
42+
ActionNonConst;
43+
static ManagedStatic<cl::opt<FnConstT>, OptCreatorT<FnConstT>> ActionConst;
44+
static std::variant<FnNonConstT, FnConstT> DefaultAction;
45+
46+
Opt::Opt(StringRef Name, FnNonConstT CB, StringRef Desc, bool ByDefault) {
47+
if (ByDefault)
48+
DefaultAction = CB;
49+
ActionNonConst->getParser().addLiteralOption(Name, CB, Desc);
50+
}
51+
52+
Opt::Opt(StringRef Name, FnConstT CB, StringRef Desc, bool ByDefault) {
53+
if (ByDefault)
54+
DefaultAction = CB;
55+
ActionConst->getParser().addLiteralOption(Name, CB, Desc);
56+
}
57+
58+
/// Apply action specified on the command line. Returns false is an action
59+
/// was applied.
60+
bool llvm::TableGen::Emitter::ApplyAction(RecordKeeper &Records,
61+
raw_ostream &OS) {
62+
// Prioritize command line option if one if specified. If none was specified
63+
// use the default action if one was specified.
64+
if (auto Fn = ActionNonConst->getValue())
65+
Fn(Records, OS);
66+
else if (auto Fn = ActionConst->getValue())
67+
Fn(Records, OS);
68+
else if (auto *Fn = std::get_if<FnNonConstT>(&DefaultAction); Fn && *Fn)
69+
(*Fn)(Records, OS);
70+
else if (auto *Fn = std::get_if<FnConstT>(&DefaultAction); Fn && *Fn)
71+
(*Fn)(Records, OS);
72+
else
73+
return true;
74+
return false;
2975
}
30-
} // namespace llvm::TableGen::Emitter
3176

3277
static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
3378
StringRef Suffix) {

llvm/utils/TableGen/DisassemblerEmitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "WebAssemblyDisassemblerEmitter.h"
1212
#include "X86DisassemblerTables.h"
1313
#include "X86RecognizableInstr.h"
14+
#include "llvm/Support/CommandLine.h"
1415
#include "llvm/TableGen/Error.h"
1516
#include "llvm/TableGen/Record.h"
1617
#include "llvm/TableGen/TableGenBackend.h"

llvm/utils/TableGen/IntrinsicEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,14 +681,14 @@ void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
681681
OS << "#endif\n\n";
682682
}
683683

684-
static void EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS) {
684+
static void EmitIntrinsicEnums(const RecordKeeper &RK, raw_ostream &OS) {
685685
IntrinsicEmitter(RK).run(OS, /*Enums=*/true);
686686
}
687687

688688
static TableGen::Emitter::Opt X("gen-intrinsic-enums", EmitIntrinsicEnums,
689689
"Generate intrinsic enums");
690690

691-
static void EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS) {
691+
static void EmitIntrinsicImpl(const RecordKeeper &RK, raw_ostream &OS) {
692692
IntrinsicEmitter(RK).run(OS, /*Enums=*/false);
693693
}
694694

llvm/utils/TableGen/TableGen.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,47 @@ cl::opt<bool> EmitLongStrLiterals(
3333
cl::Hidden, cl::init(true));
3434
} // end namespace llvm
3535

36-
namespace {
36+
static cl::OptionCategory PrintEnumsCat("Options for -print-enums");
37+
static cl::opt<std::string> Class("class",
38+
cl::desc("Print Enum list for this class"),
39+
cl::value_desc("class name"),
40+
cl::cat(PrintEnumsCat));
3741

38-
cl::OptionCategory PrintEnumsCat("Options for -print-enums");
39-
cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"),
40-
cl::value_desc("class name"),
41-
cl::cat(PrintEnumsCat));
42-
43-
void PrintRecords(RecordKeeper &Records, raw_ostream &OS) {
42+
static void PrintRecords(const RecordKeeper &Records, raw_ostream &OS) {
4443
OS << Records; // No argument, dump all contents
4544
}
4645

47-
void PrintEnums(RecordKeeper &Records, raw_ostream &OS) {
46+
static void PrintEnums(const RecordKeeper &Records, raw_ostream &OS) {
4847
for (Record *Rec : Records.getAllDerivedDefinitions(Class))
4948
OS << Rec->getName() << ", ";
5049
OS << "\n";
5150
}
5251

53-
void PrintSets(RecordKeeper &Records, raw_ostream &OS) {
52+
static void PrintSets(const RecordKeeper &Records, raw_ostream &OS) {
5453
SetTheory Sets;
5554
Sets.addFieldExpander("Set", "Elements");
5655
for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
5756
OS << Rec->getName() << " = [";
5857
const std::vector<Record *> *Elts = Sets.expand(Rec);
5958
assert(Elts && "Couldn't expand Set instance");
60-
for (Record *Elt : *Elts)
59+
for (const Record *Elt : *Elts)
6160
OS << ' ' << Elt->getName();
6261
OS << " ]\n";
6362
}
6463
}
6564

66-
TableGen::Emitter::Opt X[] = {
65+
static llvm::TableGen::Emitter::Opt X[] = {
6766
{"print-records", PrintRecords, "Print all records to stdout (default)",
6867
true},
6968
{"print-detailed-records", EmitDetailedRecords,
7069
"Print full details of all records to stdout"},
71-
{"null-backend", [](RecordKeeper &Records, raw_ostream &OS) {},
70+
{"null-backend", [](const RecordKeeper &Records, raw_ostream &OS) {},
7271
"Do nothing after parsing (useful for timing)"},
7372
{"dump-json", EmitJSON, "Dump all records as machine-readable JSON"},
7473
{"print-enums", PrintEnums, "Print enum values for a class"},
7574
{"print-sets", PrintSets, "Print expanded sets for testing DAG exprs"},
7675
};
7776

78-
} // namespace
79-
8077
int main(int argc, char **argv) {
8178
InitLLVM X(argc, argv);
8279
cl::ParseCommandLineOptions(argc, argv);

0 commit comments

Comments
 (0)