[llvm] [TableGen] Migrate CTags/DFA/Directive Emitters to use const RecordKe… (PR #107693)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 7 05:12:37 PDT 2024


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/107693

…eper

>From 222a0e4ab50c5f25fce0e42a4d7701d44cb72d22 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Sat, 7 Sep 2024 05:10:23 -0700
Subject: [PATCH] [TableGen] Migrate CTags/DFA/Directive Emitters to use const
 RecordKeeper

---
 llvm/include/llvm/TableGen/DirectiveEmitter.h | 15 +++---
 llvm/utils/TableGen/CTagsEmitter.cpp          |  4 +-
 llvm/utils/TableGen/DFAEmitter.cpp            | 24 ++++-----
 llvm/utils/TableGen/DirectiveEmitter.cpp      | 51 +++++++++----------
 4 files changed, 47 insertions(+), 47 deletions(-)

diff --git a/llvm/include/llvm/TableGen/DirectiveEmitter.h b/llvm/include/llvm/TableGen/DirectiveEmitter.h
index ca21c8fc101450..65453a0b35c6b7 100644
--- a/llvm/include/llvm/TableGen/DirectiveEmitter.h
+++ b/llvm/include/llvm/TableGen/DirectiveEmitter.h
@@ -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];
   }
@@ -50,19 +51,19 @@ 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");
   }
 
@@ -70,9 +71,9 @@ class DirectiveLanguage {
 
 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");
   }
 };
diff --git a/llvm/utils/TableGen/CTagsEmitter.cpp b/llvm/utils/TableGen/CTagsEmitter.cpp
index e21dc36a6e959a..3718486ff7ad4e 100644
--- a/llvm/utils/TableGen/CTagsEmitter.cpp
+++ b/llvm/utils/TableGen/CTagsEmitter.cpp
@@ -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);
 
diff --git a/llvm/utils/TableGen/DFAEmitter.cpp b/llvm/utils/TableGen/DFAEmitter.cpp
index 4b9affb6e850db..18620b2a073f19 100644
--- a/llvm/utils/TableGen/DFAEmitter.cpp
+++ b/llvm/utils/TableGen/DFAEmitter.cpp
@@ -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; }
 
@@ -191,8 +191,8 @@ 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;
@@ -200,7 +200,7 @@ class Automaton {
   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; }
@@ -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);
 };
 
@@ -232,7 +232,7 @@ 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);
@@ -240,7 +240,7 @@ void AutomatonEmitter::run(raw_ostream &OS) {
   }
 }
 
-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");
@@ -248,7 +248,7 @@ Automaton::Automaton(RecordKeeper &Records, Record *R)
 
 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());
@@ -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 &&
@@ -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())) {
diff --git a/llvm/utils/TableGen/DirectiveEmitter.cpp b/llvm/utils/TableGen/DirectiveEmitter.cpp
index 581bc09d5e0538..18ace70be85062 100644
--- a/llvm/utils/TableGen/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/DirectiveEmitter.cpp
@@ -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";
@@ -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) {
@@ -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};
@@ -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);
   }
@@ -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;
@@ -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
@@ -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"
@@ -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.");
@@ -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));
@@ -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)
@@ -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';
@@ -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";
@@ -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
@@ -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";
   });
@@ -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());
@@ -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);
@@ -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;



More information about the llvm-commits mailing list