Skip to content

[TableGen] Change TableGenMain to use const RecordKeeper #110578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 30, 2024

Change TableGenMain's MainFn argument to be a function that accepts a const reference to 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

Change TableGenMain's `MainFn` argument to be a function that
accepts a const reference to RecordKeeper.
@jurahul jurahul marked this pull request as ready for review October 1, 2024 01:51
@llvmbot llvmbot added clang Clang issues not falling into any other category mlir:core MLIR Core Infrastructure tablegen libc mlir labels Oct 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2024

@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-libc
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-mlir-core

Author: Rahul Joshi (jurahul)

Changes

Change TableGenMain's MainFn argument to be a function that accepts a const reference to RecordKeeper.


Full diff: https://github.com/llvm/llvm-project/pull/110578.diff

5 Files Affected:

  • (modified) clang/utils/TableGen/TableGen.cpp (+1-1)
  • (modified) libc/utils/HdrGen/Main.cpp (+12-17)
  • (modified) llvm/include/llvm/TableGen/Main.h (+1-1)
  • (modified) llvm/lib/TableGen/TableGenBackendSkeleton.cpp (+4-4)
  • (modified) mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp (+1-1)
diff --git a/clang/utils/TableGen/TableGen.cpp b/clang/utils/TableGen/TableGen.cpp
index 84afd4c0afb269..39c178bc4f9baf 100644
--- a/clang/utils/TableGen/TableGen.cpp
+++ b/clang/utils/TableGen/TableGen.cpp
@@ -317,7 +317,7 @@ ClangComponent("clang-component",
                cl::desc("Only use warnings from specified component"),
                cl::value_desc("component"), cl::Hidden);
 
-bool ClangTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
+bool ClangTableGenMain(raw_ostream &OS, const RecordKeeper &Records) {
   switch (Action) {
   case PrintRecords:
     OS << Records;           // No argument, dump all contents
diff --git a/libc/utils/HdrGen/Main.cpp b/libc/utils/HdrGen/Main.cpp
index d3418f206b10e9..f795e96e45c57a 100644
--- a/libc/utils/HdrGen/Main.cpp
+++ b/libc/utils/HdrGen/Main.cpp
@@ -15,42 +15,39 @@
 #include <string>
 #include <unordered_map>
 
-namespace {
-
-llvm::cl::opt<std::string>
+static llvm::cl::opt<std::string>
     HeaderDefFile("def", llvm::cl::desc("Path to the .h.def file."),
                   llvm::cl::value_desc("<filename>"), llvm::cl::Required);
-llvm::cl::opt<std::string> StandardHeader(
+static llvm::cl::opt<std::string> StandardHeader(
     "header",
     llvm::cl::desc("The standard header file which is to be generated."),
     llvm::cl::value_desc("<header file>"));
-llvm::cl::list<std::string> EntrypointNamesOption(
+static llvm::cl::list<std::string> EntrypointNamesOption(
     "e", llvm::cl::value_desc("<list of entrypoints>"),
     llvm::cl::desc(
         "Each --e is one entrypoint (generated from entrypoints.txt)"),
     llvm::cl::OneOrMore);
-llvm::cl::list<std::string> ReplacementValues(
+static llvm::cl::list<std::string> ReplacementValues(
     "args", llvm::cl::desc("Command separated <argument name>=<value> pairs."),
     llvm::cl::value_desc("<name=value>[,name=value]"));
-llvm::cl::opt<bool> ExportDecls(
+static llvm::cl::opt<bool> ExportDecls(
     "export-decls",
     llvm::cl::desc("Output a new header containing only the entrypoints."));
 
-void ParseArgValuePairs(std::unordered_map<std::string, std::string> &Map) {
+static void
+ParseArgValuePairs(std::unordered_map<std::string, std::string> &Map) {
   for (std::string &R : ReplacementValues) {
     auto Pair = llvm::StringRef(R).split('=');
     Map[std::string(Pair.first)] = std::string(Pair.second);
   }
 }
 
-} // anonymous namespace
-
-namespace llvm_libc {
-
-bool HeaderGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
+static bool HeaderGeneratorMain(llvm::raw_ostream &OS,
+                                const llvm::RecordKeeper &Records) {
   std::unordered_map<std::string, std::string> ArgMap;
   ParseArgValuePairs(ArgMap);
-  Generator G(HeaderDefFile, EntrypointNamesOption, StandardHeader, ArgMap);
+  llvm_libc::Generator G(HeaderDefFile, EntrypointNamesOption, StandardHeader,
+                         ArgMap);
   if (ExportDecls)
     G.generateDecls(OS, Records);
   else
@@ -59,9 +56,7 @@ bool HeaderGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
   return false;
 }
 
-} // namespace llvm_libc
-
 int main(int argc, char *argv[]) {
   llvm::cl::ParseCommandLineOptions(argc, argv);
-  return TableGenMain(argv[0], &llvm_libc::HeaderGeneratorMain);
+  return TableGenMain(argv[0], &HeaderGeneratorMain);
 }
diff --git a/llvm/include/llvm/TableGen/Main.h b/llvm/include/llvm/TableGen/Main.h
index 4639ec756e9b17..e8c60e28699022 100644
--- a/llvm/include/llvm/TableGen/Main.h
+++ b/llvm/include/llvm/TableGen/Main.h
@@ -22,7 +22,7 @@ class RecordKeeper;
 
 /// Perform the action using Records, and write output to OS.
 /// Returns true on error, false otherwise.
-using TableGenMainFn = bool (raw_ostream &OS, RecordKeeper &Records);
+using TableGenMainFn = bool(raw_ostream &OS, const RecordKeeper &Records);
 
 int TableGenMain(const char *argv0,
                  std::function<TableGenMainFn> MainFn = nullptr);
diff --git a/llvm/lib/TableGen/TableGenBackendSkeleton.cpp b/llvm/lib/TableGen/TableGenBackendSkeleton.cpp
index 8e65b7a5b300fd..a7c4a8925799a2 100644
--- a/llvm/lib/TableGen/TableGenBackendSkeleton.cpp
+++ b/llvm/lib/TableGen/TableGenBackendSkeleton.cpp
@@ -1,4 +1,4 @@
-//===- SkeletonEmitter.cpp - Skeleton TableGen backend          -*- C++ -*-===//
+//===- TableGenBackendSkeleton.cpp - Skeleton TableGen backend --*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -29,10 +29,10 @@ namespace {
 
 class SkeletonEmitter {
 private:
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
 
 public:
-  SkeletonEmitter(RecordKeeper &RK) : Records(RK) {}
+  SkeletonEmitter(const RecordKeeper &RK) : Records(RK) {}
 
   void run(raw_ostream &OS);
 }; // emitter class
@@ -55,7 +55,7 @@ static TableGen::Emitter::OptClass<SkeletonEmitter>
 //===----------------------------------------------------------------------===//
 // Option B: Register "EmitSkeleton" directly
 // The emitter entry may be private scope.
-static void EmitSkeleton(RecordKeeper &RK, raw_ostream &OS) {
+static void EmitSkeleton(const RecordKeeper &RK, raw_ostream &OS) {
   // Instantiate the emitter class and invoke run().
   SkeletonEmitter(RK).run(OS);
 }
diff --git a/mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp b/mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp
index 1911b6e3aa3927..7119324dd125d5 100644
--- a/mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp
+++ b/mlir/lib/Tools/mlir-tblgen/MlirTblgenMain.cpp
@@ -126,7 +126,7 @@ static const mlir::GenInfo *generator;
 
 // TableGenMain requires a function pointer so this function is passed in which
 // simply wraps the call to the generator.
-static bool mlirTableGenMain(raw_ostream &os, RecordKeeper &records) {
+static bool mlirTableGenMain(raw_ostream &os, const RecordKeeper &records) {
   if (actionOnDeprecatedValue != DeprecatedAction::None)
     warnOfDeprecatedUses(records);
 

@jurahul jurahul requested a review from arsenm October 1, 2024 01:52
@jurahul jurahul merged commit a86e966 into llvm:main Oct 1, 2024
16 checks passed
@jurahul jurahul deleted the const_record_tablegen_main branch October 1, 2024 13:51
Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
Change TableGenMain's `MainFn` argument to be a function that accepts a
const reference to 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category libc mlir:core MLIR Core Infrastructure mlir tablegen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants