[llvm] [CGData] Declare command line options in TableGen, one struct per library (PR #226087)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 02:20:15 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lto

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

Implement the first step of
https://discourse.llvm.org/t/rfc-declare-library-command-line-options-in-tablegen-one-struct-per-library/91877
and migrate LLVMCGData's 13 cl::opts.

A .td with an `OptionsStruct` def declares a library's options with
`BoolField` (`-x`, `-no-x`, `-x=<bool>`) and `ValueField` (`-x=v`,
`-x v`). `-gen-opt-parser-defs` generates a struct with one member per
option, a `Global` instance, the option table, and `apply(const Arg &)`.

`cl::ParseCommandLineOptions` keeps owning argv: a static
`opt::RegisterLibraryOptions<T>` registers the struct as a
`cl::LibraryOptions`, and an argument naming none of cl::'s options is
dispatched to the library that declares it. `-help-hidden` lists library
options, `ResetAllOptionOccurrences` restores their defaults, and a name
shared with a cl::opt or another library is a fatal error.

CGDataOptions is private to lib/CGData:

* LTO calls `cgdata::thinLTOTwoRounds()` instead of declaring
  `extern cl::opt<bool> CodeGenDataThinLTOTwoRounds`.
* llvm-cgdata passes `--indexed-codegen-data-lazy-loading` to
  `CodeGenDataReader::create` instead of assigning the cl::opt exported
  from CodeGenDataReader.h.

Aided by Opus 5.5

---

Patch is 62.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/226087.diff


28 Files Affected:

- (modified) llvm/docs/CommandLine.md (+33) 
- (modified) llvm/include/llvm/CGData/CodeGenData.h (+4) 
- (modified) llvm/include/llvm/CGData/CodeGenDataReader.h (+8-8) 
- (added) llvm/include/llvm/Option/LibraryOptions.h (+96) 
- (modified) llvm/include/llvm/Option/OptParser.td (+36) 
- (modified) llvm/include/llvm/Support/CommandLine.h (+28) 
- (added) llvm/lib/CGData/CGDataOptions.cpp (+15) 
- (added) llvm/lib/CGData/CGDataOptions.h (+17) 
- (added) llvm/lib/CGData/CGDataOptions.td (+51) 
- (modified) llvm/lib/CGData/CMakeLists.txt (+7) 
- (modified) llvm/lib/CGData/CodeGenData.cpp (+11-20) 
- (modified) llvm/lib/CGData/CodeGenDataReader.cpp (+10-19) 
- (modified) llvm/lib/CGData/StableFunctionMap.cpp (+10-47) 
- (modified) llvm/lib/LTO/LTO.cpp (+1-2) 
- (modified) llvm/lib/Option/CMakeLists.txt (+1) 
- (added) llvm/lib/Option/LibraryOptions.cpp (+52) 
- (modified) llvm/lib/Support/CommandLine.cpp (+80-2) 
- (modified) llvm/tools/llvm-cgdata/llvm-cgdata.cpp (+4-4) 
- (modified) llvm/unittests/Option/CMakeLists.txt (+4) 
- (added) llvm/unittests/Option/LibraryOptionsTest.cpp (+111) 
- (added) llvm/unittests/Option/LibraryOpts.td (+16) 
- (modified) llvm/unittests/Support/CommandLineTest.cpp (+127) 
- (modified) llvm/utils/TableGen/OptionParserEmitter.cpp (+123-2) 
- (modified) llvm/utils/gn/secondary/llvm/lib/CGData/BUILD.gn (+10) 
- (modified) llvm/utils/gn/secondary/llvm/lib/Option/BUILD.gn (+1) 
- (modified) llvm/utils/gn/secondary/llvm/unittests/Option/BUILD.gn (+7) 
- (modified) utils/bazel/llvm-project-overlay/llvm/BUILD.bazel (+15-1) 
- (modified) utils/bazel/llvm-project-overlay/llvm/unittests/BUILD.bazel (+11) 


``````````diff
diff --git a/llvm/docs/CommandLine.md b/llvm/docs/CommandLine.md
index 3dd499e2a2f6c..d8614e394109d 100644
--- a/llvm/docs/CommandLine.md
+++ b/llvm/docs/CommandLine.md
@@ -1680,3 +1680,36 @@ TODO: complete this section
 :::{todo}
 TODO: fill in this section
 :::
+
+## Declaring a Library's Options in TableGen
+
+A library can declare its options in a `.td` file instead of as `cl::opt`
+globals. `llvm-tblgen -gen-opt-parser-defs` generates a struct with a member
+per option, the table that parses them, and the hooks through which
+`cl::ParseCommandLineOptions` parses them and `-help-hidden` lists them.
+
+```text
+include "llvm/Option/OptParser.td"
+
+def FooOptions : OptionsStruct;
+// The spellings of FooMode, a C++ enumeration declared elsewhere.
+def FooMode : OptionEnum<"FooMode", [EnumMember<"Fast", "fast">,
+                                     EnumMember<"Small", "small">]>;
+
+defm Enable : BoolField<"foo-enable", "1", "Enable foo">;
+defm Threshold : ValueField<"foo-threshold", "unsigned", "8", "The threshold">;
+defm Mode : EnumField<"foo-mode", FooMode, "FooMode::Fast", "Foo's mode">;
+```
+
+The `defm` name is the member name. A `BoolField` is set by `-x`, `-no-x`, or
+`-x=true|false|1|0`; a `ValueField` of an integer type, `double`,
+or `std::string` by `-x=value` or `-x value`. Both accept `--` for `-`.
+
+The header declares the struct after including what the member defaults need,
+and one source file defines it and registers it with `cl::`.
+
+The library then lists `XXOptionsTableGen` under `DEPENDS` and `Option`
+under `LINK_COMPONENTS`. Code reads `XXOptions::Global.CodeGenDataGenerate`,
+the instance the command line sets. Keep the header in `lib/`, as private as the
+`static cl::opt` it replaces; another library that needs a value calls a
+function or takes a parameter.
diff --git a/llvm/include/llvm/CGData/CodeGenData.h b/llvm/include/llvm/CGData/CodeGenData.h
index e44497a408245..cd1d849fdc155 100644
--- a/llvm/include/llvm/CGData/CodeGenData.h
+++ b/llvm/include/llvm/CGData/CodeGenData.h
@@ -242,6 +242,10 @@ struct StreamCacheData {
   }
 };
 
+/// Returns whether ThinLTO runs two rounds of code generation
+/// (-codegen-data-thinlto-two-rounds).
+LLVM_ABI bool thinLTOTwoRounds();
+
 /// Save \p TheModule before the first codegen round.
 /// \p Task represents the partition number in the parallel code generation
 /// process. \p AddStream is the callback used to add the serialized module to
diff --git a/llvm/include/llvm/CGData/CodeGenDataReader.h b/llvm/include/llvm/CGData/CodeGenDataReader.h
index a656147a48610..94f7af2de1701 100644
--- a/llvm/include/llvm/CGData/CodeGenDataReader.h
+++ b/llvm/include/llvm/CGData/CodeGenDataReader.h
@@ -16,7 +16,6 @@
 #include "llvm/CGData/CodeGenData.h"
 #include "llvm/CGData/OutlinedHashTreeRecord.h"
 #include "llvm/CGData/StableFunctionMapRecord.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/LineIterator.h"
 #include "llvm/Support/VirtualFileSystem.h"
@@ -50,14 +49,15 @@ class CodeGenDataReader {
   }
 
   /// Factory method to create an appropriately typed reader for the given
-  /// codegen data file path and file system.
+  /// codegen data file path and file system. \p LazyLoading defers
+  /// deserializing the stable function map of indexed codegen data.
   LLVM_ABI static Expected<std::unique_ptr<CodeGenDataReader>>
-  create(const Twine &Path, vfs::FileSystem &FS);
+  create(const Twine &Path, vfs::FileSystem &FS, bool LazyLoading = false);
 
   /// Factory method to create an appropriately typed reader for the given
   /// memory buffer.
   LLVM_ABI static Expected<std::unique_ptr<CodeGenDataReader>>
-  create(std::unique_ptr<MemoryBuffer> Buffer);
+  create(std::unique_ptr<MemoryBuffer> Buffer, bool LazyLoading = false);
 
   /// Extract the cgdata embedded in sections from the given object file and
   /// merge them into the GlobalOutlineRecord. This is a static helper that
@@ -100,17 +100,17 @@ class CodeGenDataReader {
   Error success() { return error(cgdata_error::success); }
 };
 
-LLVM_ABI extern cl::opt<bool> IndexedCodeGenDataLazyLoading;
-
 class LLVM_ABI IndexedCodeGenDataReader : public CodeGenDataReader {
   /// The codegen data file contents.
   std::unique_ptr<MemoryBuffer> DataBuffer;
   /// The header
   IndexedCGData::Header Header;
+  bool LazyLoading;
 
 public:
-  IndexedCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer)
-      : DataBuffer(std::move(DataBuffer)) {}
+  IndexedCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer,
+                           bool LazyLoading = false)
+      : DataBuffer(std::move(DataBuffer)), LazyLoading(LazyLoading) {}
   IndexedCodeGenDataReader(const IndexedCodeGenDataReader &) = delete;
   IndexedCodeGenDataReader &
   operator=(const IndexedCodeGenDataReader &) = delete;
diff --git a/llvm/include/llvm/Option/LibraryOptions.h b/llvm/include/llvm/Option/LibraryOptions.h
new file mode 100644
index 0000000000000..2ddcb34b3323b
--- /dev/null
+++ b/llvm/include/llvm/Option/LibraryOptions.h
@@ -0,0 +1,96 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Support for the options struct that -gen-opt-parser-defs generates from an
+// OptionsStruct def. See "Declaring a Library's Options in TableGen" in
+// llvm/docs/CommandLine.md.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_OPTION_LIBRARYOPTIONS_H
+#define LLVM_OPTION_LIBRARYOPTIONS_H
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/OptTable.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Compiler.h"
+#include <string>
+#include <type_traits>
+
+namespace llvm {
+namespace opt {
+
+// Each accepts the spellings cl::opt accepts for the type.
+inline bool parseArgValue(StringRef S, bool &V) {
+  if (S == "true" || S == "1")
+    V = true;
+  else if (S == "false" || S == "0")
+    V = false;
+  else
+    return false;
+  return true;
+}
+
+inline bool parseArgValue(StringRef S, std::string &V) {
+  V = S.str();
+  return true;
+}
+
+template <typename T>
+std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>, bool>
+parseArgValue(StringRef S, T &V) {
+  if constexpr (std::is_floating_point_v<T>)
+    return to_float(S, V);
+  else
+    return to_integer(S, V);
+}
+
+/// An OptTable with a public constructor, shared by every options struct.
+class LLVM_ABI LibraryOptTable : public OptTable {
+public:
+  explicit LibraryOptTable(const Tables &T) : OptTable(T) {}
+  ~LibraryOptTable() override;
+};
+
+/// Connects an options struct to cl::ParseCommandLineOptions.
+class LLVM_ABI LibraryOptionsParser final : public cl::LibraryOptions {
+public:
+  using ApplyFn = bool (*)(const Arg &);
+  using TableFn = const OptTable &(*)();
+  LibraryOptionsParser(TableFn Table, ApplyFn Apply, void (*Reset)())
+      : Table(Table), Apply(Apply), Reset(Reset) {}
+
+  void forEachOption(
+      function_ref<void(StringRef, StringRef, StringRef)> Fn) const override;
+  Error parse(ArrayRef<const char *> Args, unsigned &Consumed) override;
+  void reset() override { Reset(); }
+
+private:
+  TableFn Table;
+  ApplyFn Apply;
+  void (*Reset)();
+};
+
+/// Registers T::Global with cl::ParseCommandLineOptions. The library owning T
+/// defines one static instance in the file that includes the struct's
+/// definitions.
+template <typename T> class RegisterLibraryOptions {
+  LibraryOptionsParser Parser{T::optTable,
+                              [](const Arg &A) { return T::Global.apply(A); },
+                              [] { T::Global = T(); }};
+
+public:
+  RegisterLibraryOptions() { cl::addLibraryOptions(Parser); }
+};
+
+} // namespace opt
+} // namespace llvm
+
+#endif // LLVM_OPTION_LIBRARYOPTIONS_H
diff --git a/llvm/include/llvm/Option/OptParser.td b/llvm/include/llvm/Option/OptParser.td
index 7adb2cf6fa5de..d5fe98430b5a6 100644
--- a/llvm/include/llvm/Option/OptParser.td
+++ b/llvm/include/llvm/Option/OptParser.td
@@ -136,6 +136,13 @@ class Option<list<string> prefixes, string name, OptionKind kind,
   code Denormalizer = "";
   list<code> NormalizedValues = ?;
   list<SubCommand> SubCommands = subcommands;
+  // For an option of an OptionsStruct: the member it sets, the member's type
+  // and default value, and the value a Flag assigns. Other kinds parse their
+  // argument.
+  string FieldName = ?;
+  string FieldType = ?;
+  code FieldDefault = ?;
+  code FieldValue = ?;
 }
 
 // Helpers for defining options.
@@ -271,6 +278,35 @@ class Denormalizer<code denormalizer> { code Denormalizer = denormalizer; }
 class NormalizedValuesScope<code scope> { code NormalizedValuesScope = scope; }
 class NormalizedValues<list<code> definitions> { list<code> NormalizedValues = definitions; }
 
+// Declaring a library's options as the members of a struct. A .td with an
+// OptionsStruct def generates a struct named after the def, with one member per
+// BoolField or ValueField. Every option is hidden from -help.
+
+class OptionsStruct;
+
+class Field<string name, string type, code default> {
+  string FieldName = name;
+  string FieldType = type;
+  code FieldDefault = default;
+}
+class FieldValue<code value> { code FieldValue = value; }
+
+// A bool member set by -<name>, -no-<name>, or -<name>=<true|false|1|0>.
+multiclass BoolField<string name, code default, string help> {
+  def NAME : Flag<["-", "--"], name>, Field<NAME, "bool", default>,
+    FieldValue<"true">, HelpText<help>, Flags<[HelpHidden]>;
+  def no_#NAME : Flag<["-", "--"], "no-"#name>, Field<NAME, "bool", default>,
+    FieldValue<"false">;
+  def NAME#_EQ : Joined<["-", "--"], name#"=">, Field<NAME, "bool", default>;
+}
+
+// A member of the C++ type <type> set by -<name>=<value> or -<name> <value>.
+multiclass ValueField<string name, string type, code default, string help> {
+  def NAME#_EQ : Joined<["-", "--"], name#"=">, Field<NAME, type, default>,
+    HelpText<help>, Flags<[HelpHidden]>;
+  def NAME : Separate<["-", "--"], name>, Alias<!cast<Option>(NAME#"_EQ")>;
+}
+
 // Predefined options.
 
 // FIXME: Have generator validate that these appear in correct position (and
diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index 15fbb8c56a0b0..c8917a75a291d 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -44,6 +44,7 @@ namespace llvm {
 
 class StringSaver;
 class ElementCount;
+class Error;
 
 /// This namespace contains all of the command line option processing machinery.
 /// It is intentionally a short name to make qualified usage concise.
@@ -2316,6 +2317,33 @@ LLVM_ABI void ResetCommandLineParser();
 /// Parses `Arg` into the option handler `Handler`.
 LLVM_ABI bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i);
 
+/// The options of a library that declares them in TableGen rather than as
+/// cl::opt (see llvm/Option/LibraryOptions.h). ParseCommandLineOptions hands
+/// every argument naming one of them to parse().
+class LLVM_ABI LibraryOptions {
+public:
+  /// Calls \p Fn with the spelling of each option without its prefix (e.g.
+  /// "x" or "x="), its metavariable, and its help text.
+  virtual void forEachOption(
+      function_ref<void(StringRef Spelling, StringRef MetaVar, StringRef Help)>
+          Fn) const = 0;
+
+  /// Parses the option spelled by Args[0], which may take Args[1] as its
+  /// value, and sets \p Consumed to the number of arguments it spans.
+  virtual Error parse(ArrayRef<const char *> Args, unsigned &Consumed) = 0;
+
+  /// Restores the default values.
+  virtual void reset() = 0;
+
+protected:
+  // Registrations are static and never destroyed through this class.
+  ~LibraryOptions() = default;
+};
+
+/// Makes ParseCommandLineOptions recognize \p L's options. A name that is also
+/// a cl::opt or belongs to another library is a fatal error.
+LLVM_ABI void addLibraryOptions(LibraryOptions &L);
+
 } // end namespace cl
 
 } // end namespace llvm
diff --git a/llvm/lib/CGData/CGDataOptions.cpp b/llvm/lib/CGData/CGDataOptions.cpp
new file mode 100644
index 0000000000000..5154719dcb3f0
--- /dev/null
+++ b/llvm/lib/CGData/CGDataOptions.cpp
@@ -0,0 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CGDataOptions.h"
+#include "llvm/Option/LibraryOptions.h"
+
+#define OPTIONS_STRUCT_DEFS
+#include "CGDataOptions.inc"
+
+static llvm::opt::RegisterLibraryOptions<llvm::CGDataOptions> Registration;
diff --git a/llvm/lib/CGData/CGDataOptions.h b/llvm/lib/CGData/CGDataOptions.h
new file mode 100644
index 0000000000000..b2430ad000f3a
--- /dev/null
+++ b/llvm/lib/CGData/CGDataOptions.h
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_CGDATA_CGDATAOPTIONS_H
+#define LLVM_LIB_CGDATA_CGDATAOPTIONS_H
+
+#include <limits>
+
+#define OPTIONS_STRUCT_DECL
+#include "CGDataOptions.inc"
+
+#endif // LLVM_LIB_CGDATA_CGDATAOPTIONS_H
diff --git a/llvm/lib/CGData/CGDataOptions.td b/llvm/lib/CGData/CGDataOptions.td
new file mode 100644
index 0000000000000..b46293d43edb1
--- /dev/null
+++ b/llvm/lib/CGData/CGDataOptions.td
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+include "llvm/Option/OptParser.td"
+
+def CGDataOptions : OptionsStruct;
+
+defm CodeGenDataGenerate : BoolField<"codegen-data-generate", "false",
+  "Emit CodeGen Data into custom sections">;
+defm CodeGenDataThinLTOTwoRounds : BoolField<"codegen-data-thinlto-two-rounds",
+  "false", "Enable two-round ThinLTO code generation. The first round emits "
+  "codegen data, while the second round uses the emitted codegen data for "
+  "further optimizations.">;
+defm CodeGenDataUsePath : ValueField<"codegen-data-use-path", "std::string",
+  "", "File path to where .cgdata file is read">;
+defm GlobalMergingCallOverhead : ValueField<"global-merging-call-overhead",
+  "double", "1.0", "The overhead cost associated with each function call when "
+  "merging functions.">;
+defm GlobalMergingExtraThreshold : ValueField<"global-merging-extra-threshold",
+  "double", "0.0", "An additional cost threshold that must be exceeded for "
+  "merging to be considered beneficial.">;
+defm GlobalMergingInstOverhead : ValueField<"global-merging-inst-overhead",
+  "double", "1.2", "The overhead cost associated with each instruction when "
+  "lowering to machine instruction.">;
+defm GlobalMergingMaxParams : ValueField<"global-merging-max-params",
+  "unsigned", "std::numeric_limits<unsigned>::max()",
+  "The maximum number of parameters allowed when merging functions.">;
+defm GlobalMergingMinInstrs : ValueField<"global-merging-min-instrs",
+  "unsigned", "1",
+  "The minimum instruction count required when merging functions.">;
+defm GlobalMergingMinMerges : ValueField<"global-merging-min-merges",
+  "unsigned", "2", "Minimum number of similar functions with the same hash "
+  "required for merging.">;
+defm GlobalMergingParamOverhead : ValueField<"global-merging-param-overhead",
+  "double", "2.0", "The overhead cost associated with each parameter when "
+  "merging functions.">;
+defm GlobalMergingSkipNoParams : BoolField<"global-merging-skip-no-params",
+  "true", "Skip merging functions with no parameters.">;
+defm IndexedCodeGenDataLazyLoading : BoolField<
+  "indexed-codegen-data-lazy-loading", "false", "Lazily load indexed "
+  "CodeGenData. Enable to save memory and time for final consumption of the "
+  "indexed CodeGenData in production.">;
+defm IndexedCodeGenDataReadFunctionMapNames : BoolField<
+  "indexed-codegen-data-read-function-map-names", "true", "Read function map "
+  "names in indexed CodeGenData. Can be disabled to save memory and time for "
+  "final consumption of the indexed CodeGenData in production.">;
diff --git a/llvm/lib/CGData/CMakeLists.txt b/llvm/lib/CGData/CMakeLists.txt
index 003173139f36c..6c31d76331f6f 100644
--- a/llvm/lib/CGData/CMakeLists.txt
+++ b/llvm/lib/CGData/CMakeLists.txt
@@ -1,4 +1,9 @@
+set(LLVM_TARGET_DEFINITIONS CGDataOptions.td)
+tablegen(LLVM CGDataOptions.inc -gen-opt-parser-defs)
+add_public_tablegen_target(CGDataOptionsTableGen)
+
 add_llvm_component_library(LLVMCGData
+  CGDataOptions.cpp
   CodeGenData.cpp
   CodeGenDataReader.cpp
   CodeGenDataWriter.cpp
@@ -11,6 +16,7 @@ add_llvm_component_library(LLVMCGData
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/CGData
 
   DEPENDS
+  CGDataOptionsTableGen
   intrinsics_gen
 
   LINK_COMPONENTS
@@ -19,4 +25,5 @@ add_llvm_component_library(LLVMCGData
   Core
   Support
   Object
+  Option
   )
diff --git a/llvm/lib/CGData/CodeGenData.cpp b/llvm/lib/CGData/CodeGenData.cpp
index 7900dc7653c03..0b253d1504e50 100644
--- a/llvm/lib/CGData/CodeGenData.cpp
+++ b/llvm/lib/CGData/CodeGenData.cpp
@@ -11,13 +11,13 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "CGDataOptions.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/CGData/CodeGenDataReader.h"
 #include "llvm/CGData/OutlinedHashTreeRecord.h"
 #include "llvm/CGData/StableFunctionMapRecord.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Caching.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/WithColor.h"
 
 #define DEBUG_TYPE "cg-data"
@@ -25,21 +25,6 @@
 using namespace llvm;
 using namespace cgdata;
 
-static cl::opt<bool>
-    CodeGenDataGenerate("codegen-data-generate", cl::init(false), cl::Hidden,
-                        cl::desc("Emit CodeGen Data into custom sections"));
-static cl::opt<std::string>
-    CodeGenDataUsePath("codegen-data-use-path", cl::init(""), cl::Hidden,
-                       cl::desc("File path to where .cgdata file is read"));
-
-namespace llvm {
-cl::opt<bool> CodeGenDataThinLTOTwoRounds(
-    "codegen-data-thinlto-two-rounds", cl::init(false), cl::Hidden,
-    cl::desc("Enable two-round ThinLTO code generation. The first round "
-             "emits codegen data, while the second round uses the emitted "
-             "codegen data for further optimizations."));
-} // end namespace llvm
-
 static std::string getCGDataErrString(cgdata_error Err,
                                       const std::string &ErrMsg = "") {
   std::string Msg;
@@ -123,6 +108,10 @@ const char *CodeGenDataSectNamePrefix[] = {
 
 } // namespace
 
+bool llvm::cgdata::thinLTOTwoRounds() {
+  return CGDataOptions::Global.CodeGenDataThinLTOTwoRounds;
+}
+
 namespace llvm {
 
 std::string getCodeGenDataSectionName(CGDataSectKind CGSK,
@@ -148,17 +137,19 @@ CodeGenData &CodeGenData::getInstance() {
   std::call_once(CodeGenData::OnceFlag, []() {
     Instance = std::unique_ptr<CodeGenData>(new CodeGenData());
 
-    if (CodeGenDataGenerate || CodeGenDataThinLTOTwoRounds)
+    const CGDataOptions &Opts = CGDataOptions::Global;
+    if (Opts.CodeGenDataGenerate || Opts.CodeGenDataThinLTOTwoRounds)
       Instance->EmitCGData = true;
-    else if (!CodeGenDataUsePath.empty()) {
+    else if (!Opts.CodeGenDataUsePath.empty()) {
       // Initialize the global CGData if the input file name is given.
       // We do not error-out when failing to parse the input file.
       // Inst...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/226087


More information about the llvm-commits mailing list