Skip to content

Commit

Permalink
[TableGen] Migrate CTags/DFA/Directive Emitters to const RecordKeeper (
Browse files Browse the repository at this point in the history
…#107693)

Migrate CTags/DFA/Directive Emitters to const RecordKeeper.
  • Loading branch information
jurahul authored Sep 8, 2024
1 parent ce3c58e commit 8ae5521
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
15 changes: 8 additions & 7 deletions llvm/include/llvm/TableGen/DirectiveEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace llvm {
// DirectiveBase.td and provides helper methods for accessing it.
class DirectiveLanguage {
public:
explicit DirectiveLanguage(llvm::RecordKeeper &Records) : Records(Records) {
explicit DirectiveLanguage(const llvm::RecordKeeper &Records)
: Records(Records) {
const auto &DirectiveLanguages = getDirectiveLanguages();
Def = DirectiveLanguages[0];
}
Expand Down Expand Up @@ -50,29 +51,29 @@ class DirectiveLanguage {
return Def->getValueAsBit("enableBitmaskEnumInNamespace");
}

std::vector<Record *> getAssociations() const {
ArrayRef<const Record *> getAssociations() const {
return Records.getAllDerivedDefinitions("Association");
}

std::vector<Record *> getCategories() const {
ArrayRef<const Record *> getCategories() const {
return Records.getAllDerivedDefinitions("Category");
}

std::vector<Record *> getDirectives() const {
ArrayRef<const Record *> getDirectives() const {
return Records.getAllDerivedDefinitions("Directive");
}

std::vector<Record *> getClauses() const {
ArrayRef<const Record *> getClauses() const {
return Records.getAllDerivedDefinitions("Clause");
}

bool HasValidityErrors() const;

private:
const llvm::Record *Def;
llvm::RecordKeeper &Records;
const llvm::RecordKeeper &Records;

std::vector<Record *> getDirectiveLanguages() const {
ArrayRef<const Record *> getDirectiveLanguages() const {
return Records.getAllDerivedDefinitions("DirectiveLanguage");
}
};
Expand Down
4 changes: 2 additions & 2 deletions llvm/utils/TableGen/CTagsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class Tag {

class CTagsEmitter {
private:
RecordKeeper &Records;
const RecordKeeper &Records;

public:
CTagsEmitter(RecordKeeper &R) : Records(R) {}
CTagsEmitter(const RecordKeeper &R) : Records(R) {}

void run(raw_ostream &OS);

Expand Down
24 changes: 12 additions & 12 deletions llvm/utils/TableGen/DFAEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Transition {
SmallVector<std::string, 4> Types;

public:
Transition(Record *R, Automaton *Parent);
Transition(const Record *R, Automaton *Parent);
const ActionTuple &getActions() { return Actions; }
SmallVector<std::string, 4> getTypes() { return Types; }

Expand All @@ -191,16 +191,16 @@ class Transition {
};

class Automaton {
RecordKeeper &Records;
Record *R;
const RecordKeeper &Records;
const Record *R;
std::vector<Transition> Transitions;
/// All possible action tuples, uniqued.
UniqueVector<ActionTuple> Actions;
/// The fields within each Transition object to find the action symbols.
std::vector<StringRef> ActionSymbolFields;

public:
Automaton(RecordKeeper &Records, Record *R);
Automaton(const RecordKeeper &Records, const Record *R);
void emit(raw_ostream &OS);

ArrayRef<StringRef> getActionSymbolFields() { return ActionSymbolFields; }
Expand All @@ -210,10 +210,10 @@ class Automaton {
};

class AutomatonEmitter {
RecordKeeper &Records;
const RecordKeeper &Records;

public:
AutomatonEmitter(RecordKeeper &R) : Records(R) {}
AutomatonEmitter(const RecordKeeper &R) : Records(R) {}
void run(raw_ostream &OS);
};

Expand All @@ -232,23 +232,23 @@ class CustomDfaEmitter : public DfaEmitter {
} // namespace

void AutomatonEmitter::run(raw_ostream &OS) {
for (Record *R : Records.getAllDerivedDefinitions("GenericAutomaton")) {
for (const Record *R : Records.getAllDerivedDefinitions("GenericAutomaton")) {
Automaton A(Records, R);
OS << "#ifdef GET_" << R->getName() << "_DECL\n";
A.emit(OS);
OS << "#endif // GET_" << R->getName() << "_DECL\n";
}
}

Automaton::Automaton(RecordKeeper &Records, Record *R)
Automaton::Automaton(const RecordKeeper &Records, const Record *R)
: Records(Records), R(R) {
LLVM_DEBUG(dbgs() << "Emitting automaton for " << R->getName() << "\n");
ActionSymbolFields = R->getValueAsListOfStrings("SymbolFields");
}

void Automaton::emit(raw_ostream &OS) {
StringRef TransitionClass = R->getValueAsString("TransitionClass");
for (Record *T : Records.getAllDerivedDefinitions(TransitionClass)) {
for (const Record *T : Records.getAllDerivedDefinitions(TransitionClass)) {
assert(T->isSubClassOf("Transition"));
Transitions.emplace_back(T, this);
Actions.insert(Transitions.back().getActions());
Expand Down Expand Up @@ -305,7 +305,7 @@ StringRef Automaton::getActionSymbolType(StringRef A) {
return R->getValueAsString(Ty.str());
}

Transition::Transition(Record *R, Automaton *Parent) {
Transition::Transition(const Record *R, Automaton *Parent) {
BitsInit *NewStateInit = R->getValueAsBitsInit("NewState");
NewState = 0;
assert(NewStateInit->getNumBits() <= sizeof(uint64_t) * 8 &&
Expand All @@ -318,8 +318,8 @@ Transition::Transition(Record *R, Automaton *Parent) {
}

for (StringRef A : Parent->getActionSymbolFields()) {
RecordVal *SymbolV = R->getValue(A);
if (auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
const RecordVal *SymbolV = R->getValue(A);
if (const auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
Actions.emplace_back(R->getValueAsDef(A));
Types.emplace_back(Ty->getAsString());
} else if (isa<IntRecTy>(SymbolV->getType())) {
Expand Down
51 changes: 25 additions & 26 deletions llvm/utils/TableGen/DirectiveEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class IfDefScope {

// Generate enum class. Entries are emitted in the order in which they appear
// in the `Records` vector.
static void GenerateEnumClass(const std::vector<Record *> &Records,
raw_ostream &OS, StringRef Enum, StringRef Prefix,
static void GenerateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
StringRef Enum, StringRef Prefix,
const DirectiveLanguage &DirLang,
bool ExportEnums) {
OS << "\n";
Expand Down Expand Up @@ -79,7 +79,7 @@ static void GenerateEnumClass(const std::vector<Record *> &Records,

// Generate enums for values that clauses can take.
// Also generate function declarations for get<Enum>Name(StringRef Str).
static void GenerateEnumClauseVal(const std::vector<Record *> &Records,
static void GenerateEnumClauseVal(ArrayRef<const Record *> Records,
raw_ostream &OS,
const DirectiveLanguage &DirLang,
std::string &EnumHelperFuncs) {
Expand Down Expand Up @@ -144,7 +144,7 @@ static bool HasDuplicateClauses(const std::vector<Record *> &Clauses,
// three allowed list. Also, since required implies allowed, clauses cannot
// appear in both the allowedClauses and requiredClauses lists.
static bool
HasDuplicateClausesInDirectives(const std::vector<Record *> &Directives) {
HasDuplicateClausesInDirectives(ArrayRef<const Record *> Directives) {
bool HasDuplicate = false;
for (const auto &D : Directives) {
Directive Dir{D};
Expand Down Expand Up @@ -184,7 +184,7 @@ bool DirectiveLanguage::HasValidityErrors() const {
// Count the maximum number of leaf constituents per construct.
static size_t GetMaxLeafCount(const DirectiveLanguage &DirLang) {
size_t MaxCount = 0;
for (Record *R : DirLang.getDirectives()) {
for (const Record *R : DirLang.getDirectives()) {
size_t Count = Directive{R}.getLeafConstructs().size();
MaxCount = std::max(MaxCount, Count);
}
Expand All @@ -193,7 +193,7 @@ static size_t GetMaxLeafCount(const DirectiveLanguage &DirLang) {

// Generate the declaration section for the enumeration in the directive
// language
static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
static void EmitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
const auto DirLang = DirectiveLanguage{Records};
if (DirLang.HasValidityErrors())
return;
Expand All @@ -220,7 +220,7 @@ static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
OS << "\nLLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n";

// Emit Directive associations
std::vector<Record *> associations;
std::vector<const Record *> associations;
llvm::copy_if(
DirLang.getAssociations(), std::back_inserter(associations),
// Skip the "special" value
Expand Down Expand Up @@ -283,9 +283,8 @@ static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
}

// Generate function implementation for get<Enum>Name(StringRef Str)
static void GenerateGetName(const std::vector<Record *> &Records,
raw_ostream &OS, StringRef Enum,
const DirectiveLanguage &DirLang,
static void GenerateGetName(ArrayRef<const Record *> Records, raw_ostream &OS,
StringRef Enum, const DirectiveLanguage &DirLang,
StringRef Prefix) {
OS << "\n";
OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
Expand All @@ -308,13 +307,13 @@ static void GenerateGetName(const std::vector<Record *> &Records,
}

// Generate function implementation for get<Enum>Kind(StringRef Str)
static void GenerateGetKind(const std::vector<Record *> &Records,
raw_ostream &OS, StringRef Enum,
const DirectiveLanguage &DirLang, StringRef Prefix,
bool ImplicitAsUnknown) {
static void GenerateGetKind(ArrayRef<const Record *> Records, raw_ostream &OS,
StringRef Enum, const DirectiveLanguage &DirLang,
StringRef Prefix, bool ImplicitAsUnknown) {

auto DefaultIt = llvm::find_if(
Records, [](Record *R) { return R->getValueAsBit("isDefault") == true; });
auto DefaultIt = llvm::find_if(Records, [](const Record *R) {
return R->getValueAsBit("isDefault") == true;
});

if (DefaultIt == Records.end()) {
PrintError("At least one " + Enum + " must be defined as default.");
Expand Down Expand Up @@ -506,8 +505,8 @@ static void EmitLeafTable(const DirectiveLanguage &DirLang, raw_ostream &OS,
// row. To avoid this, an auxiliary ordering table is created, such that
// row for Dir_A = table[auxiliary[Dir_A]].

std::vector<Record *> Directives = DirLang.getDirectives();
DenseMap<Record *, int> DirId; // Record * -> llvm::omp::Directive
ArrayRef<const Record *> Directives = DirLang.getDirectives();
DenseMap<const Record *, int> DirId; // Record * -> llvm::omp::Directive

for (auto [Idx, Rec] : llvm::enumerate(Directives))
DirId.insert(std::make_pair(Rec, Idx));
Expand Down Expand Up @@ -628,7 +627,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
Invalid,
};

std::vector<Record *> associations = DirLang.getAssociations();
ArrayRef<const Record *> associations = DirLang.getAssociations();

auto getAssocValue = [](StringRef name) -> Association {
return StringSwitch<Association>(name)
Expand Down Expand Up @@ -720,7 +719,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
return Result;
};

for (Record *R : DirLang.getDirectives())
for (const Record *R : DirLang.getDirectives())
compAssocImpl(R, compAssocImpl); // Updates AsMap.

OS << '\n';
Expand All @@ -739,7 +738,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
OS << AssociationTypeName << " llvm::" << DirLang.getCppNamespace()
<< "::getDirectiveAssociation(" << DirectiveTypeName << " Dir) {\n";
OS << " switch (Dir) {\n";
for (Record *R : DirLang.getDirectives()) {
for (const Record *R : DirLang.getDirectives()) {
if (auto F = AsMap.find(R); F != AsMap.end()) {
Directive Dir{R};
OS << " case " << getQualifiedName(Dir.getFormattedName()) << ":\n";
Expand All @@ -763,7 +762,7 @@ static void GenerateGetDirectiveCategory(const DirectiveLanguage &DirLang,
<< GetDirectiveType(DirLang) << " Dir) {\n";
OS << " switch (Dir) {\n";

for (Record *R : DirLang.getDirectives()) {
for (const Record *R : DirLang.getDirectives()) {
Directive D{R};
OS << " case " << GetDirectiveName(DirLang, R) << ":\n";
OS << " return " << CategoryNamespace
Expand Down Expand Up @@ -903,7 +902,7 @@ static void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang,
IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS);

OS << "\n";
llvm::interleaveComma(DirLang.getClauses(), OS, [&](Record *C) {
llvm::interleaveComma(DirLang.getClauses(), OS, [&](const Record *C) {
Clause Clause{C};
OS << Clause.getFormattedParserClassName() << "\n";
});
Expand Down Expand Up @@ -1013,7 +1012,7 @@ static void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
<< " Parser clause\");\n";
}

static bool compareClauseName(Record *R1, Record *R2) {
static bool compareClauseName(const Record *R1, const Record *R2) {
Clause C1{R1};
Clause C2{R2};
return (C1.getName() > C2.getName());
Expand All @@ -1022,7 +1021,7 @@ static bool compareClauseName(Record *R1, Record *R2) {
// Generate the parser for the clauses.
static void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
raw_ostream &OS) {
std::vector<Record *> Clauses = DirLang.getClauses();
std::vector<const Record *> Clauses = DirLang.getClauses();
// Sort clauses in reverse alphabetical order so with clauses with same
// beginning, the longer option is tried before.
llvm::sort(Clauses, compareClauseName);
Expand Down Expand Up @@ -1231,7 +1230,7 @@ void EmitDirectivesBasicImpl(const DirectiveLanguage &DirLang,

// Generate the implemenation section for the enumeration in the directive
// language.
static void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
static void EmitDirectivesImpl(const RecordKeeper &Records, raw_ostream &OS) {
const auto DirLang = DirectiveLanguage{Records};
if (DirLang.HasValidityErrors())
return;
Expand Down

0 comments on commit 8ae5521

Please sign in to comment.