[llvm] [TableGen] Migrate Option Emitters to const RecordKeeper (PR #107696)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 13:45:22 PDT 2024


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/107696

>From 040c3b032c1e9851a3d042c1b15425ea9be71d17 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Sat, 7 Sep 2024 05:29:19 -0700
Subject: [PATCH] [TableGen] Migrate Opt/OptRST Emitters to cosst RecordKeeper

---
 llvm/utils/TableGen/Common/OptEmitter.cpp | 16 ++++++------
 llvm/utils/TableGen/Common/OptEmitter.h   |  4 +--
 llvm/utils/TableGen/ExegesisEmitter.cpp   | 17 +++++++------
 llvm/utils/TableGen/OptParserEmitter.cpp  |  8 +++---
 llvm/utils/TableGen/OptRSTEmitter.cpp     | 30 +++++++++--------------
 5 files changed, 34 insertions(+), 41 deletions(-)

diff --git a/llvm/utils/TableGen/Common/OptEmitter.cpp b/llvm/utils/TableGen/Common/OptEmitter.cpp
index 7fcf3074e0931e..1c91ec5b3dbc44 100644
--- a/llvm/utils/TableGen/Common/OptEmitter.cpp
+++ b/llvm/utils/TableGen/Common/OptEmitter.cpp
@@ -1,4 +1,4 @@
-//===- OptEmitter.cpp - Helper for emitting options.----------- -----------===//
+//===- OptEmitter.cpp - Helper for emitting options -------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -39,21 +39,19 @@ static int StrCmpOptionName(const char *A, const char *B) {
   return (a < b) ? -1 : 1;
 }
 
-int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
-  const Record *A = *Av;
-  const Record *B = *Bv;
-
+// Returns true if A is ordered before B.
+bool CompareOptionRecords(const Record *A, const Record *B) {
   // Sentinel options precede all others and are only ordered by precedence.
   bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
   bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
   if (ASent != BSent)
-    return ASent ? -1 : 1;
+    return ASent;
 
   // Compare options by name, unless they are sentinels.
   if (!ASent)
     if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),
                                    B->getValueAsString("Name").str().c_str()))
-      return Cmp;
+      return Cmp < 0;
 
   if (!ASent) {
     std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
@@ -65,7 +63,7 @@ int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
                                                 BEPre = BPrefixes.end();
          APre != AEPre && BPre != BEPre; ++APre, ++BPre) {
       if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))
-        return Cmp;
+        return Cmp < 0;
     }
   }
 
@@ -78,7 +76,7 @@ int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
     PrintError(B->getLoc(), Twine("Other defined here"));
     PrintFatalError("Equivalent Options found.");
   }
-  return APrec < BPrec ? -1 : 1;
+  return APrec < BPrec;
 }
 
 } // namespace llvm
diff --git a/llvm/utils/TableGen/Common/OptEmitter.h b/llvm/utils/TableGen/Common/OptEmitter.h
index eaef966bbac666..5eecd619873373 100644
--- a/llvm/utils/TableGen/Common/OptEmitter.h
+++ b/llvm/utils/TableGen/Common/OptEmitter.h
@@ -1,4 +1,4 @@
-//===- OptEmitter.h - Helper for emitting options. --------------*- C++ -*-===//
+//===- OptEmitter.h - Helper for emitting options ---------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,7 +11,7 @@
 
 namespace llvm {
 class Record;
-int CompareOptionRecords(Record *const *Av, Record *const *Bv);
+bool CompareOptionRecords(const Record *A, const Record *B);
 } // namespace llvm
 
 #endif // LLVM_UTILS_TABLEGEN_COMMON_OPTEMITTER_H
diff --git a/llvm/utils/TableGen/ExegesisEmitter.cpp b/llvm/utils/TableGen/ExegesisEmitter.cpp
index 0de7cb42337481..a5dd2994b37534 100644
--- a/llvm/utils/TableGen/ExegesisEmitter.cpp
+++ b/llvm/utils/TableGen/ExegesisEmitter.cpp
@@ -30,7 +30,7 @@ namespace {
 
 class ExegesisEmitter {
 public:
-  ExegesisEmitter(RecordKeeper &RK);
+  ExegesisEmitter(const RecordKeeper &RK);
 
   void run(raw_ostream &OS) const;
 
@@ -51,7 +51,7 @@ class ExegesisEmitter {
 
   void emitPfmCountersLookupTable(raw_ostream &OS) const;
 
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
   std::string Target;
 
   // Table of counter name -> counter index.
@@ -59,7 +59,7 @@ class ExegesisEmitter {
 };
 
 static std::map<llvm::StringRef, unsigned>
-collectPfmCounters(RecordKeeper &Records) {
+collectPfmCounters(const RecordKeeper &Records) {
   std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
   const auto AddPfmCounterName = [&PfmCounterNameTable](
                                      const Record *PfmCounterDef) {
@@ -67,7 +67,8 @@ collectPfmCounters(RecordKeeper &Records) {
     if (!Counter.empty())
       PfmCounterNameTable.emplace(Counter, 0);
   };
-  for (Record *Def : Records.getAllDerivedDefinitions("ProcPfmCounters")) {
+  for (const Record *Def :
+       Records.getAllDerivedDefinitions("ProcPfmCounters")) {
     // Check that ResourceNames are unique.
     llvm::SmallSet<llvm::StringRef, 16> Seen;
     for (const Record *IssueCounter :
@@ -95,9 +96,9 @@ collectPfmCounters(RecordKeeper &Records) {
   return PfmCounterNameTable;
 }
 
-ExegesisEmitter::ExegesisEmitter(RecordKeeper &RK)
+ExegesisEmitter::ExegesisEmitter(const RecordKeeper &RK)
     : Records(RK), PfmCounterNameTable(collectPfmCounters(RK)) {
-  std::vector<Record *> Targets = Records.getAllDerivedDefinitions("Target");
+  ArrayRef<const Record *> Targets = Records.getAllDerivedDefinitions("Target");
   if (Targets.size() == 0)
     PrintFatalError("No 'Target' subclasses defined!");
   if (Targets.size() != 1)
@@ -223,7 +224,7 @@ void ExegesisEmitter::emitPfmCounters(raw_ostream &OS) const {
 } // namespace
 
 void ExegesisEmitter::emitPfmCountersLookupTable(raw_ostream &OS) const {
-  std::vector<Record *> Bindings =
+  std::vector<const Record *> Bindings =
       Records.getAllDerivedDefinitions("PfmCountersBinding");
   assert(!Bindings.empty() && "there must be at least one binding");
   llvm::sort(Bindings, [](const Record *L, const Record *R) {
@@ -232,7 +233,7 @@ void ExegesisEmitter::emitPfmCountersLookupTable(raw_ostream &OS) const {
 
   OS << "// Sorted (by CpuName) array of pfm counters.\n"
      << "static const CpuAndPfmCounters " << Target << "CpuPfmCounters[] = {\n";
-  for (Record *Binding : Bindings) {
+  for (const Record *Binding : Bindings) {
     // Emit as { "cpu", procinit },
     OS << "  { \""                                                        //
        << Binding->getValueAsString("CpuName") << "\","                   //
diff --git a/llvm/utils/TableGen/OptParserEmitter.cpp b/llvm/utils/TableGen/OptParserEmitter.cpp
index 81195c8c106c23..a41c684f169e9f 100644
--- a/llvm/utils/TableGen/OptParserEmitter.cpp
+++ b/llvm/utils/TableGen/OptParserEmitter.cpp
@@ -250,15 +250,15 @@ static void EmitHelpTextsForVariants(
 /// OptParserEmitter - This tablegen backend takes an input .td file
 /// describing a list of options and emits a data structure for parsing and
 /// working with those options when given an input command line.
-static void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
+static void EmitOptParser(const RecordKeeper &Records, raw_ostream &OS) {
   // Get the option groups and options.
-  const std::vector<Record *> &Groups =
+  ArrayRef<const Record *> Groups =
       Records.getAllDerivedDefinitions("OptionGroup");
-  std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
+  std::vector<const Record *> Opts = Records.getAllDerivedDefinitions("Option");
 
   emitSourceFileHeader("Option Parsing Definitions", OS);
 
-  array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
+  llvm::sort(Opts, CompareOptionRecords);
   // Generate prefix groups.
   typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
   typedef std::map<PrefixKeyT, std::string> PrefixesT;
diff --git a/llvm/utils/TableGen/OptRSTEmitter.cpp b/llvm/utils/TableGen/OptRSTEmitter.cpp
index 75b7cbdf298879..43b0f78c44d909 100644
--- a/llvm/utils/TableGen/OptRSTEmitter.cpp
+++ b/llvm/utils/TableGen/OptRSTEmitter.cpp
@@ -16,30 +16,24 @@ using namespace llvm;
 
 /// OptParserEmitter - This tablegen backend takes an input .td file
 /// describing a list of options and emits a RST man page.
-static void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
-  llvm::StringMap<std::vector<Record *>> OptionsByGroup;
+static void EmitOptRST(const RecordKeeper &Records, raw_ostream &OS) {
+  llvm::StringMap<std::vector<const Record *>> OptionsByGroup;
   std::vector<Record *> OptionsWithoutGroup;
 
   // Get the options.
-  std::vector<Record *> Opts = Records.getAllDerivedDefinitions("Option");
-  array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
+  std::vector<const Record *> Opts = Records.getAllDerivedDefinitions("Option");
+  llvm::sort(Opts, CompareOptionRecords);
 
   // Get the option groups.
-  const std::vector<Record *> &Groups =
-      Records.getAllDerivedDefinitions("OptionGroup");
-  for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
-    const Record &R = *Groups[i];
-    OptionsByGroup.try_emplace(R.getValueAsString("Name"));
-  }
+  for (const Record *R : Records.getAllDerivedDefinitions("OptionGroup"))
+    OptionsByGroup.try_emplace(R->getValueAsString("Name"));
 
   // Map options to their group.
-  for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
-    const Record &R = *Opts[i];
-    if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
-      OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(Opts[i]);
-    } else {
-      OptionsByGroup["options"].push_back(Opts[i]);
-    }
+  for (const Record *R : Opts) {
+    if (const DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group")))
+      OptionsByGroup[DI->getDef()->getValueAsString("Name")].push_back(R);
+    else
+      OptionsByGroup["options"].push_back(R);
   }
 
   // Print options under their group.
@@ -49,7 +43,7 @@ static void EmitOptRST(RecordKeeper &Records, raw_ostream &OS) {
     OS << std::string(GroupName.size(), '-') << '\n';
     OS << '\n';
 
-    for (Record *R : KV.getValue()) {
+    for (const Record *R : KV.getValue()) {
       OS << ".. option:: ";
 
       // Print the prefix.



More information about the llvm-commits mailing list