[clang] [lld] [llvm] [Support] Pass tool options to parseCommandLineOptions (PR #240009)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 14:20:55 PST 2026


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/240009

>From 44112a1fedb1904cee68d25f9941c82adcd14438 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 7 Nov 2026 10:00:16 -0800
Subject: [PATCH] [Support] Pass tool options to parseCommandLineOptions

Follow-up to #239920: `RegisterMCToolOptions` adds `MCToolOptions` to
the process-wide registry. Once lld registers them, every later parse in
the process accepts `-crel`, including clang's `-mllvm`, which then
ignores it.

Add a `parseCommandLineOptions` overload that takes the libraries a tool
opts in to. Replace `RegisterMCToolOptions` with `mc::getToolOptions`
and `codegen::getToolOptions`, which tools pass where they parse,
including lld's `-mllvm`, gold's `-plugin-opt`, and libLTO's debug
options. `RegisterCodeGenFlags` no longer registers the MC options. When
CodeGen's options move to TableGen, their parser joins
`codegen::getToolOptions` without changing these call sites.

clang-linker-wrapper forwards `-mllvm` to the device and host link steps
and does not pass the MC options. Its wrapper module uses the module's
own context instead of the user's `-mllvm`, as discussed on #233513.

`LibraryOptionsContext` records the library that parsed each argument,
so `assign`, which runs on each ThinLTO backend thread, replays without
looking up the registry.

LLM-aided
---
 .../ClangLinkerWrapper.cpp                    | 22 ++---
 .../ClangNVLinkWrapper.cpp                    |  3 +-
 lld/COFF/Driver.cpp                           |  3 +-
 lld/Common/TargetOptionsCommandFlags.cpp      |  4 +
 lld/ELF/Driver.cpp                            |  4 +-
 lld/MachO/Driver.cpp                          |  4 +-
 .../lld/Common/TargetOptionsCommandFlags.h    |  6 ++
 lld/wasm/Driver.cpp                           |  3 +-
 llvm/docs/CommandLine.md                      |  1 +
 llvm/docs/ReleaseNotes.md                     |  3 +
 llvm/include/llvm/CodeGen/CommandFlags.h      |  9 ++
 llvm/include/llvm/FuzzMutate/FuzzerCLI.h      |  3 +
 .../llvm/MC/MCTargetOptionsCommandFlags.h     | 10 +--
 llvm/include/llvm/Support/CommandLine.h       | 13 +++
 .../llvm/Support/LibraryOptionsContext.h      |  5 +-
 llvm/lib/CodeGen/CommandFlags.cpp             |  4 +-
 llvm/lib/FuzzMutate/FuzzerCLI.cpp             |  3 +-
 llvm/lib/LTO/LTOCodeGenerator.cpp             |  3 +-
 llvm/lib/MC/MCTargetOptionsCommandFlags.cpp   |  6 +-
 llvm/lib/Support/CommandLine.cpp              | 89 +++++++++++++------
 llvm/test/tools/llvm-mc/help.test             |  4 +
 llvm/tools/gold/gold-plugin.cpp               |  3 +-
 llvm/tools/llc/lib/llcdriver.cpp              |  3 +-
 llvm/tools/lli/lli.cpp                        |  3 +-
 .../llvm-isel-fuzzer/llvm-isel-fuzzer.cpp     |  2 +-
 llvm/tools/llvm-lto/llvm-lto.cpp              |  4 +-
 llvm/tools/llvm-lto2/llvm-lto2.cpp            |  3 +-
 llvm/tools/llvm-mc/llvm-mc.cpp                |  5 +-
 llvm/tools/llvm-mca/llvm-mca.cpp              |  5 +-
 .../tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp |  2 +-
 llvm/tools/llvm-reduce/llvm-reduce.cpp        |  3 +-
 llvm/tools/lto/lto.cpp                        |  3 +-
 llvm/tools/opt/optdriver.cpp                  |  3 +-
 llvm/unittests/Option/LibraryOptionsTest.cpp  | 31 +++++++
 34 files changed, 199 insertions(+), 73 deletions(-)

diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 1024d5e6e1a6c8..39843c6c4d0ee3 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -681,16 +681,15 @@ Expected<StringRef> writeOffloadFile(const OffloadFile &File) {
 
 // Compile the module to an object file using the appropriate target machine for
 // the host triple.
-Expected<StringRef>
-compileModule(const cl::LibraryOptionsContext &LibraryOptions, Module &M,
-              OffloadKind Kind) {
+Expected<StringRef> compileModule(Module &M, OffloadKind Kind) {
   llvm::TimeTraceScope TimeScope("Compile module");
   std::string Msg;
   const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
   if (!T)
     return createStringError(Msg);
 
-  auto Options = codegen::getTargetOptions(LibraryOptions, M.getTargetTriple());
+  auto Options = codegen::getTargetOptions(M.getContext().getLibraryOptions(),
+                                           M.getTargetTriple());
   StringRef CPU = "";
   StringRef Features = "";
   std::unique_ptr<TargetMachine> TM(
@@ -782,8 +781,7 @@ wrapDeviceImagesVerbose(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
 /// Creates the object file containing the device image and runtime
 /// registration code from the device images stored in \p Images.
 Expected<StringRef>
-wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
-                 ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
+wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
                  const ArgList &Args, OffloadKind Kind) {
   llvm::TimeTraceScope TimeScope("Wrap bundled images");
 
@@ -849,7 +847,7 @@ wrapDeviceImages(const cl::LibraryOptionsContext &LibraryOptions,
     WriteBitcodeToFile(M, OS);
   }
 
-  auto FileOrErr = compileModule(LibraryOptions, M, Kind);
+  auto FileOrErr = compileModule(M, Kind);
   if (!FileOrErr)
     return FileOrErr.takeError();
   return *FileOrErr;
@@ -1096,8 +1094,7 @@ Error handleOverrideImages(
 /// be registered by the runtime. If NeedsWrapping is false, writes bundled
 /// output directly without wrapping or host linking.
 Expected<SmallVector<StringRef>>
-linkAndWrapDeviceFiles(const cl::LibraryOptionsContext &LibraryOptions,
-                       ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
+linkAndWrapDeviceFiles(ArrayRef<SmallVector<OffloadFile>> LinkerInputFiles,
                        const InputArgList &Args, char **Argv, int Argc,
                        bool NeedsWrapping) {
   llvm::TimeTraceScope TimeScope("Handle all device input");
@@ -1229,8 +1226,7 @@ linkAndWrapDeviceFiles(const cl::LibraryOptionsContext &LibraryOptions,
       continue;
     }
 
-    auto OutputOrErr = wrapDeviceImages(LibraryOptions, *BundledImagesOrErr,
-                                        Args, WrapperKind);
+    auto OutputOrErr = wrapDeviceImages(*BundledImagesOrErr, Args, WrapperKind);
     if (!OutputOrErr)
       return OutputOrErr.takeError();
     WrappedOutput.push_back(*OutputOrErr);
@@ -1614,8 +1610,8 @@ int main(int Argc, char **Argv) {
 
     // Link and process the device images. The function may emit a direct fat
     // binary if --emit-fatbin-only is specified.
-    auto FilesOrErr = linkAndWrapDeviceFiles(LibraryOptions, *DeviceInputFiles,
-                                             Args, Argv, Argc, !EmitFatbinOnly);
+    auto FilesOrErr = linkAndWrapDeviceFiles(*DeviceInputFiles, Args, Argv,
+                                             Argc, !EmitFatbinOnly);
     if (!FilesOrErr)
       reportError(FilesOrErr.takeError());
 
diff --git a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
index 9b134b004953ca..b047a76907d661 100644
--- a/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
+++ b/clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
@@ -823,7 +823,8 @@ int main(int argc, char **argv) {
   for (const opt::Arg *Arg : Args.filtered(OPT_plugin_opt))
     NewArgv.push_back(Arg->getValue());
   cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(LibraryOptions, NewArgv);
+  cl::parseCommandLineOptions(LibraryOptions, codegen::getToolOptions(),
+                              NewArgv);
 
   // Get the input files to pass to 'nvlink'.
   auto FilesOrErr = getInput(LibraryOptions, Args);
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 8580526c0ab4c1..06a938aebdebba 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -20,6 +20,7 @@
 #include "lld/Common/Args.h"
 #include "lld/Common/CommonLinkerContext.h"
 #include "lld/Common/Filesystem.h"
+#include "lld/Common/TargetOptionsCommandFlags.h"
 #include "lld/Common/Timer.h"
 #include "lld/Common/Version.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -1656,7 +1657,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   {
     llvm::TimeTraceScope timeScope2("Parse cl::opt");
     cl::ResetAllOptionOccurrences();
-    cl::parseCommandLineOptions(config->libraryOptions, v);
+    cl::parseCommandLineOptions(config->libraryOptions, getToolOptions(), v);
   }
 
   // Handle /errorlimit early, because error() depends on it.
diff --git a/lld/Common/TargetOptionsCommandFlags.cpp b/lld/Common/TargetOptionsCommandFlags.cpp
index 180e8c98e1037b..d0baeb47fa32f8 100644
--- a/lld/Common/TargetOptionsCommandFlags.cpp
+++ b/lld/Common/TargetOptionsCommandFlags.cpp
@@ -17,6 +17,10 @@ lld::getTargetOptions(const llvm::cl::LibraryOptionsContext &libraryOptions) {
   return llvm::codegen::getTargetOptions(libraryOptions, llvm::Triple());
 }
 
+llvm::ArrayRef<llvm::cl::LibraryOptions *> lld::getToolOptions() {
+  return llvm::codegen::getToolOptions();
+}
+
 std::optional<llvm::Reloc::Model> lld::getRelocModelFromCMModel() {
   return llvm::codegen::getExplicitRelocModel();
 }
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 08c2fb3b4985fb..10c67fdd8e9f51 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -42,6 +42,7 @@
 #include "lld/Common/ErrorHandler.h"
 #include "lld/Common/Memory.h"
 #include "lld/Common/Strings.h"
+#include "lld/Common/TargetOptionsCommandFlags.h"
 #include "lld/Common/Version.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetVector.h"
@@ -1360,7 +1361,8 @@ static void parseClangOption(Ctx &ctx, StringRef opt, const Twine &msg) {
   const char *argv[] = {ctx.arg.progName.data(), opt.data()};
   // Called after initLLVM() to ensure target-specific RegisterLibraryOptions
   // have been called.
-  if (cl::parseCommandLineOptions(ctx.arg.libraryOptions, argv, "", &os))
+  if (cl::parseCommandLineOptions(ctx.arg.libraryOptions, getToolOptions(),
+                                  argv, "", &os))
     return;
   ErrAlways(ctx) << msg << ": " << StringRef(err).trim();
 }
diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 525c86d4fc141a..7b88d549fb691a 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -30,6 +30,7 @@
 #include "lld/Common/LLVM.h"
 #include "lld/Common/Memory.h"
 #include "lld/Common/Reproduce.h"
+#include "lld/Common/TargetOptionsCommandFlags.h"
 #include "lld/Common/Version.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
@@ -1126,7 +1127,8 @@ static void parseClangOption(StringRef opt, const Twine &msg) {
   raw_string_ostream os(err);
 
   const char *argv[] = {"lld", opt.data()};
-  if (cl::parseCommandLineOptions(config->libraryOptions, argv, "", &os))
+  if (cl::parseCommandLineOptions(config->libraryOptions, getToolOptions(),
+                                  argv, "", &os))
     return;
   error(msg + ": " + StringRef(err).trim());
 }
diff --git a/lld/include/lld/Common/TargetOptionsCommandFlags.h b/lld/include/lld/Common/TargetOptionsCommandFlags.h
index 92334bb3ba4ff3..fbbc702542d8a4 100644
--- a/lld/include/lld/Common/TargetOptionsCommandFlags.h
+++ b/lld/include/lld/Common/TargetOptionsCommandFlags.h
@@ -13,13 +13,19 @@
 #ifndef LLD_COMMON_TARGETOPTIONSCOMMANDFLAGS_H
 #define LLD_COMMON_TARGETOPTIONSCOMMANDFLAGS_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Target/TargetOptions.h"
 #include <optional>
 
+namespace llvm::cl {
+class LibraryOptions;
+}
+
 namespace lld {
 llvm::TargetOptions
 getTargetOptions(const llvm::cl::LibraryOptionsContext &libraryOptions);
+llvm::ArrayRef<llvm::cl::LibraryOptions *> getToolOptions();
 std::optional<llvm::Reloc::Model> getRelocModelFromCMModel();
 std::optional<llvm::CodeModel::Model> getCodeModelFromCMModel();
 std::string getCPUStr();
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index e5e6a414519b0d..dedbc9dbd99cb8 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -20,6 +20,7 @@
 #include "lld/Common/Memory.h"
 #include "lld/Common/Reproduce.h"
 #include "lld/Common/Strings.h"
+#include "lld/Common/TargetOptionsCommandFlags.h"
 #include "lld/Common/Version.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Config/llvm-config.h"
@@ -1321,7 +1322,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   for (auto *arg : args.filtered(OPT_mllvm))
     v.push_back(arg->getValue());
   cl::ResetAllOptionOccurrences();
-  cl::parseCommandLineOptions(ctx.arg.libraryOptions, v);
+  cl::parseCommandLineOptions(ctx.arg.libraryOptions, getToolOptions(), v);
 
   readConfigs(args);
   setConfigs();
diff --git a/llvm/docs/CommandLine.md b/llvm/docs/CommandLine.md
index 00e293a12be1d0..8c7a601e6d7a29 100644
--- a/llvm/docs/CommandLine.md
+++ b/llvm/docs/CommandLine.md
@@ -1599,6 +1599,7 @@ Only `-help-hidden` lists the options, like `cl::Hidden`.
 A default is the member's C++ initializer, so `"\"-\""` initializes `foo_path` to `"-"`.
 An `OptionalValueField` or `OptionalEnumField` takes no default; its `std::optional` member stays `std::nullopt` unless the option is given, replacing `getNumOccurrences()`.
 The header declares the struct after including what the member defaults need, and one source file defines it and registers it with `cl::`.
+Options that only some tools accept, such as those that `mc::getMCTargetOptions` reads, are not registered: a function returns a function-local `opt::LibraryOptionsParser`, and each such tool passes it to the `cl::parseCommandLineOptions` overload that takes tool options.
 
 The library then lists `FooOptionsTableGen` under `DEPENDS` and `Option` under `LINK_COMPONENTS`.
 A library that otherwise needs only `llvm-min-tblgen` sets `LLVM_TABLEGEN_PROJECT` to `LLVM_HEADERS` before its `llvm_tablegen()` call, so that its sources need not wait for `llvm-tblgen`.
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 738ca4f4ace4b9..bb5daf4ef0988e 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -174,6 +174,9 @@ Makes programs 10x faster by doing Special New Thing.
 
 ### Changes to LLVM infrastructure
 
+* Removed `mc::RegisterMCTargetOptionsFlags`.
+  A tool that created it now passes `mc::getToolOptions()`, or `codegen::getToolOptions()` with `codegen::RegisterCodeGenFlags`, to `cl::parseCommandLineOptions`.
+
 * Removed `TargetOptions::FloatABIType`. The soft float ABI should be
   controlled by setting the `"float-abi"` module flag.
 
diff --git a/llvm/include/llvm/CodeGen/CommandFlags.h b/llvm/include/llvm/CodeGen/CommandFlags.h
index 2c343fe95226d0..4509c95e17b425 100644
--- a/llvm/include/llvm/CodeGen/CommandFlags.h
+++ b/llvm/include/llvm/CodeGen/CommandFlags.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_CODEGEN_COMMANDFLAGS_H
 #define LLVM_CODEGEN_COMMANDFLAGS_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/FloatingPointMode.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/Compiler.h"
@@ -31,6 +32,10 @@ class Function;
 class Triple;
 class TargetMachine;
 
+namespace cl {
+class LibraryOptions;
+}
+
 namespace codegen {
 
 LLVM_ABI std::string getMArch();
@@ -143,6 +148,10 @@ struct RegisterCodeGenFlags {
   LLVM_ABI RegisterCodeGenFlags();
 };
 
+/// The options that a tool reading getTargetOptions passes to
+/// cl::parseCommandLineOptions.
+LLVM_ABI ArrayRef<cl::LibraryOptions *> getToolOptions();
+
 /// Tools that support subtarget tuning should create this object with static
 /// storage to register the -mtune command line option.
 struct RegisterMTuneFlag {
diff --git a/llvm/include/llvm/FuzzMutate/FuzzerCLI.h b/llvm/include/llvm/FuzzMutate/FuzzerCLI.h
index 865542a7efe564..06b73d81e8f46e 100644
--- a/llvm/include/llvm/FuzzMutate/FuzzerCLI.h
+++ b/llvm/include/llvm/FuzzMutate/FuzzerCLI.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_FUZZMUTATE_FUZZERCLI_H
 #define LLVM_FUZZMUTATE_FUZZERCLI_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DataTypes.h"
 #include <stddef.h>
@@ -22,6 +23,7 @@ namespace llvm {
 
 class StringRef;
 namespace cl {
+class LibraryOptions;
 class LibraryOptionsContext;
 }
 
@@ -29,6 +31,7 @@ class LibraryOptionsContext;
 ///
 /// This handles all arguments after -ignore_remaining_args=1 as cl::opts.
 LLVM_ABI void parseFuzzerCLOpts(cl::LibraryOptionsContext &LibraryOptions,
+                                ArrayRef<cl::LibraryOptions *> ToolOptions,
                                 int ArgC, char *ArgV[]);
 
 /// Handle backend options that are encoded in the executable name.
diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
index 87eb39817bff37..b9cb73cb10b19b 100644
--- a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
+++ b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
 #define LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/Support/Compiler.h"
 
 namespace llvm {
@@ -22,6 +23,7 @@ class MCTargetOptions;
 class StringRef;
 
 namespace cl {
+class LibraryOptions;
 class LibraryOptionsContext;
 }
 
@@ -29,11 +31,9 @@ namespace mc {
 
 LLVM_ABI StringRef getABIName(const cl::LibraryOptionsContext &C);
 
-/// Create this object with static storage to register mc-related command
-/// line options.
-struct RegisterMCToolOptions {
-  LLVM_ABI RegisterMCToolOptions();
-};
+/// The options that a tool reading getMCTargetOptions passes to
+/// cl::parseCommandLineOptions.
+LLVM_ABI ArrayRef<cl::LibraryOptions *> getToolOptions();
 
 LLVM_ABI MCTargetOptions getMCTargetOptions(const cl::LibraryOptionsContext &C);
 
diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index f4cf54884846b7..ad5ce9161c35d3 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -50,6 +50,7 @@ class Error;
 /// It is intentionally a short name to make qualified usage concise.
 namespace cl {
 
+class LibraryOptions;
 class LibraryOptionsContext;
 
 //===----------------------------------------------------------------------===//
@@ -81,6 +82,18 @@ LLVM_ABI bool parseCommandLineOptions(LibraryOptionsContext &Context,
                                       vfs::FileSystem *VFS = nullptr,
                                       const char *EnvVar = nullptr);
 
+// Like the above, but also recognizes the options of ToolOptions, libraries
+// that a tool opts in to instead of registering them with addLibraryOptions.
+// They must outlive the contexts that parse them, and the parser refers to them
+// until a later parse passes other ToolOptions or ResetCommandLineParser.
+LLVM_ABI bool parseCommandLineOptions(LibraryOptionsContext &Context,
+                                      ArrayRef<LibraryOptions *> ToolOptions,
+                                      ArrayRef<const char *> Args,
+                                      StringRef Overview = "",
+                                      raw_ostream *Errs = nullptr,
+                                      vfs::FileSystem *VFS = nullptr,
+                                      const char *EnvVar = nullptr);
+
 // The library options that LLVMParseCommandLineOptions sets, which contexts
 // created through the C API start with.
 LLVM_ABI LibraryOptionsContext &getCAPILibraryOptions();
diff --git a/llvm/include/llvm/Support/LibraryOptionsContext.h b/llvm/include/llvm/Support/LibraryOptionsContext.h
index ab6507ebdae5c0..f83ebd97ab95f6 100644
--- a/llvm/include/llvm/Support/LibraryOptionsContext.h
+++ b/llvm/include/llvm/Support/LibraryOptionsContext.h
@@ -28,7 +28,6 @@ class LibraryOptions;
 class LLVM_ABI LibraryOptionsContext {
 public:
   // Replaces the library options with Other's by replaying its arguments.
-  // Indexing libraries registered since the last parse is not thread-safe.
   void assign(const LibraryOptionsContext &Other);
 
   // Parses the option of L that Args[0] spells, keeping the other values.
@@ -38,6 +37,9 @@ class LLVM_ABI LibraryOptionsContext {
   // The arguments assigned so far.
   ArrayRef<const char *> args() const { return SavedArgs; }
 
+  // The library that parsed each argument in args().
+  ArrayRef<LibraryOptions *> argLibraries() const { return SavedLibraries; }
+
   // T's values, or T::Default if none of its options is parsed.
   template <typename T> const T &get() const {
     auto It = Values.find(&T::Default);
@@ -53,6 +55,7 @@ class LLVM_ABI LibraryOptionsContext {
 private:
   DenseMap<const void *, void *> Values;
   SmallVector<const char *, 0> SavedArgs;
+  SmallVector<LibraryOptions *, 0> SavedLibraries;
   BumpPtrAllocator Alloc;
 };
 
diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
index e6d059acda0455..5537e3f539020b 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -467,8 +467,10 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
                "values are put into the RO data section."),
       cl::init(false));
   CGBINDOPT(XCOFFReadOnlyPointers);
+}
 
-  mc::RegisterMCToolOptions();
+ArrayRef<cl::LibraryOptions *> codegen::getToolOptions() {
+  return mc::getToolOptions();
 }
 
 codegen::RegisterMTuneFlag::RegisterMTuneFlag() {
diff --git a/llvm/lib/FuzzMutate/FuzzerCLI.cpp b/llvm/lib/FuzzMutate/FuzzerCLI.cpp
index d183209c34720d..739d662c35943b 100644
--- a/llvm/lib/FuzzMutate/FuzzerCLI.cpp
+++ b/llvm/lib/FuzzMutate/FuzzerCLI.cpp
@@ -16,6 +16,7 @@
 using namespace llvm;
 
 void llvm::parseFuzzerCLOpts(cl::LibraryOptionsContext &LibraryOptions,
+                             ArrayRef<cl::LibraryOptions *> ToolOptions,
                              int ArgC, char *ArgV[]) {
   std::vector<const char *> CLArgs;
   CLArgs.push_back(ArgV[0]);
@@ -27,7 +28,7 @@ void llvm::parseFuzzerCLOpts(cl::LibraryOptionsContext &LibraryOptions,
   while (I < ArgC)
     CLArgs.push_back(ArgV[I++]);
 
-  cl::parseCommandLineOptions(LibraryOptions, CLArgs);
+  cl::parseCommandLineOptions(LibraryOptions, ToolOptions, CLArgs);
 }
 
 void llvm::handleExecNameEncodedBEOpts(
diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 82a089a454dd21..b5bb4fbe22d659 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -683,7 +683,8 @@ void LTOCodeGenerator::parseCodeGenDebugOptions() {
     std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
     for (std::string &Arg : CodegenOptions)
       CodegenArgv.push_back(Arg.c_str());
-    cl::parseCommandLineOptions(Context.getLibraryOptions(), CodegenArgv);
+    cl::parseCommandLineOptions(Context.getLibraryOptions(),
+                                codegen::getToolOptions(), CodegenArgv);
   }
 }
 
diff --git a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
index e8c4a7ef8e7442..39fc4980047cb4 100644
--- a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
+++ b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
@@ -23,8 +23,10 @@ using namespace llvm;
 #define OPTIONS_STRUCT_DEFS
 #include "MCToolOptions.inc"
 
-llvm::mc::RegisterMCToolOptions::RegisterMCToolOptions() {
-  static opt::RegisterLibraryOptions<MCToolOptions> Registration;
+ArrayRef<cl::LibraryOptions *> llvm::mc::getToolOptions() {
+  static opt::LibraryOptionsParser Parser(std::in_place_type<MCToolOptions>);
+  static cl::LibraryOptions *const Libraries[] = {&Parser};
+  return Libraries;
 }
 
 StringRef llvm::mc::getABIName(const cl::LibraryOptionsContext &C) {
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 3ada0587b9407f..bbdfb2ec183891 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -179,6 +179,9 @@ class CommandLineParser {
   SmallVector<LibraryOptions *, 0> Libraries;
   DenseMap<StringRef, LibraryOptions *> LibraryIndex;
   size_t NumIndexedLibraries = 0;
+  // The libraries that the last parse recognized in addition to Libraries,
+  // also in LibraryIndex.
+  SmallVector<LibraryOptions *, 0> ToolOptions;
   // Storage that library options refer to: copies of the arguments passed to
   // LibraryOptions::parse and what it allocates. Response file expansions do
   // not outlive parsing.
@@ -191,7 +194,8 @@ class CommandLineParser {
   bool ParseCommandLineOptions(int argc, const char *const *argv,
                                StringRef Overview, raw_ostream *Errs = nullptr,
                                vfs::FileSystem *VFS = nullptr,
-                               LibraryOptionsContext *Context = nullptr);
+                               LibraryOptionsContext *Context = nullptr,
+                               ArrayRef<LibraryOptions *> Tools = {});
 
   void forEachSubCommand(Option &Opt, function_ref<void(SubCommand &)> Action) {
     if (Opt.Subs.empty()) {
@@ -334,10 +338,9 @@ class CommandLineParser {
 
   void printOptionValues();
 
-  void indexLibraryOptions() {
+  void indexLibraries(ArrayRef<LibraryOptions *> Libs) {
     bool HadErrors = false;
-    for (; NumIndexedLibraries != Libraries.size(); ++NumIndexedLibraries) {
-      LibraryOptions *L = Libraries[NumIndexedLibraries];
+    for (LibraryOptions *L : Libs) {
       L->forEachOption([&](StringRef Spelling, StringRef, StringRef) {
         StringRef Name = Spelling.rtrim('=');
         auto [It, Inserted] = LibraryIndex.try_emplace(Name, L);
@@ -356,6 +359,22 @@ class CommandLineParser {
       report_fatal_error("inconsistency in registered CommandLine options");
   }
 
+  void indexLibraryOptions() {
+    indexLibraries(ArrayRef(Libraries).drop_front(NumIndexedLibraries));
+    NumIndexedLibraries = Libraries.size();
+  }
+
+  // Rebuilds the index when the tool options differ from the last parse's.
+  void setToolOptions(ArrayRef<LibraryOptions *> Libs) {
+    if (ArrayRef(ToolOptions) == Libs)
+      return;
+    ToolOptions.assign(Libs);
+    LibraryIndex.clear();
+    NumIndexedLibraries = 0;
+    indexLibraryOptions();
+    indexLibraries(ToolOptions);
+  }
+
   // A library loaded while parsing (e.g. a pass plugin) may define the name.
   LibraryOptions *lookupLibraryOption(StringRef Name) {
     indexLibraryOptions();
@@ -417,6 +436,7 @@ class CommandLineParser {
     Libraries.clear();
     LibraryIndex.clear();
     NumIndexedLibraries = 0;
+    ToolOptions.clear();
 
     SubCommand::getTopLevel().reset();
     SubCommand::getAll().reset();
@@ -1446,11 +1466,10 @@ Error ExpansionContext::readConfigFile(StringRef CfgFile,
 }
 
 static void initCommonOptions();
-static bool parseCommandLineOptionsImpl(LibraryOptionsContext *Context,
-                                        ArrayRef<const char *> Args,
-                                        StringRef Overview, raw_ostream *Errs,
-                                        vfs::FileSystem *VFS,
-                                        const char *EnvVar) {
+static bool parseCommandLineOptionsImpl(
+    LibraryOptionsContext *Context, ArrayRef<const char *> Args,
+    StringRef Overview, raw_ostream *Errs, vfs::FileSystem *VFS,
+    const char *EnvVar, ArrayRef<LibraryOptions *> ToolOptions = {}) {
   initCommonOptions();
   SmallVector<const char *, 20> NewArgv;
   BumpPtrAllocator A;
@@ -1469,8 +1488,8 @@ static bool parseCommandLineOptionsImpl(LibraryOptionsContext *Context,
   int NewArgc = static_cast<int>(NewArgv.size());
 
   // Parse all options.
-  return globalParser().ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview,
-                                                Errs, VFS, Context);
+  return globalParser().ParseCommandLineOptions(
+      NewArgc, &NewArgv[0], Overview, Errs, VFS, Context, ToolOptions);
 }
 
 bool cl::ParseCommandLineOptions(int argc, const char *const *argv,
@@ -1488,6 +1507,15 @@ bool cl::parseCommandLineOptions(LibraryOptionsContext &Context,
                                      EnvVar);
 }
 
+bool cl::parseCommandLineOptions(LibraryOptionsContext &Context,
+                                 ArrayRef<LibraryOptions *> ToolOptions,
+                                 ArrayRef<const char *> Args,
+                                 StringRef Overview, raw_ostream *Errs,
+                                 vfs::FileSystem *VFS, const char *EnvVar) {
+  return parseCommandLineOptionsImpl(&Context, Args, Overview, Errs, VFS,
+                                     EnvVar, ToolOptions);
+}
+
 /// Reset all options at least once, so that we can parse different options.
 void CommandLineParser::ResetAllOptionOccurrences() {
   // Reset all option values to look like they have never been seen before.
@@ -1507,15 +1535,17 @@ void CommandLineParser::ResetAllOptionOccurrences() {
     if (SC->ConsumeAfterOpt)
       SC->ConsumeAfterOpt->reset();
   }
-  for (LibraryOptions *L : Libraries)
+  for (LibraryOptions *L :
+       concat<LibraryOptions *const>(Libraries, ToolOptions))
     L->reset();
   LibraryArgAlloc.Reset();
 }
 
 bool CommandLineParser::ParseCommandLineOptions(
     int argc, const char *const *argv, StringRef Overview, raw_ostream *Errs,
-    vfs::FileSystem *VFS, LibraryOptionsContext *Context) {
-  assert(hasOptions() && "No options specified!");
+    vfs::FileSystem *VFS, LibraryOptionsContext *Context,
+    ArrayRef<LibraryOptions *> Tools) {
+  assert((hasOptions() || !Tools.empty()) && "No options specified!");
 
   ProgramOverview = Overview;
   bool IgnoreErrors = Errs;
@@ -1544,6 +1574,7 @@ bool CommandLineParser::ParseCommandLineOptions(
   // Copy the program name into ProgName, making sure not to overflow it.
   ProgramName = std::string(sys::path::filename(StringRef(argv[0])));
   indexLibraryOptions();
+  setToolOptions(Tools);
 
   // Check out the positional arguments to collect information about them.
   unsigned NumPositionalRequired = 0;
@@ -2494,7 +2525,8 @@ class HelpPrinter {
 
     SmallVector<std::pair<std::string, StringRef>, 0> LibraryOpts;
     if (ShowHidden)
-      for (LibraryOptions *L : globalParser().Libraries)
+      for (LibraryOptions *L : concat<LibraryOptions *const>(
+               globalParser().Libraries, globalParser().ToolOptions))
         L->forEachOption(
             [&](StringRef Spelling, StringRef MetaVar, StringRef Help) {
               if (!Help.empty())
@@ -2943,20 +2975,18 @@ void cl::addLibraryOptions(LibraryOptions &L) {
   globalParser().Libraries.push_back(&L);
 }
 
-// Parses the library options that a LibraryOptionsContext recorded in Args
-// into Context, or into their global() if Context is null.
+// Parses the arguments that a LibraryOptionsContext recorded, each with the
+// library that parsed it, into Context, or into their global() if Context is
+// null.
 static void replayLibraryArgs(ArrayRef<const char *> Args,
+                              ArrayRef<LibraryOptions *> Libraries,
                               LibraryOptionsContext *Context) {
   for (size_t I = 0, E = Args.size(); I != E;) {
-    StringRef Name = Args[I];
-    Name.consume_front("-");
-    Name.consume_front("-");
-    LibraryOptions *L =
-        globalParser().lookupLibraryOption(Name.split('=').first);
+    LibraryOptions &L = *Libraries[I];
     unsigned N;
     ArrayRef<const char *> Rest = Args.drop_front(I);
-    cantFail(Context ? Context->parse(*L, Rest, N)
-                     : parseLibraryOption(*L, L->global(), Rest, N,
+    cantFail(Context ? Context->parse(L, Rest, N)
+                     : parseLibraryOption(L, L.global(), Rest, N,
                                           globalParser().LibraryArgAlloc));
     I += N;
   }
@@ -2966,7 +2996,8 @@ void LibraryOptionsContext::assign(const LibraryOptionsContext &Other) {
   assert(this != &Other);
   Values.clear();
   SavedArgs.clear();
-  replayLibraryArgs(Other.SavedArgs, this);
+  SavedLibraries.clear();
+  replayLibraryArgs(Other.SavedArgs, Other.SavedLibraries, this);
 }
 
 Error LibraryOptionsContext::parse(LibraryOptions &L,
@@ -2975,7 +3006,10 @@ Error LibraryOptionsContext::parse(LibraryOptions &L,
   auto [It, Inserted] = Values.try_emplace(L.getDefault());
   if (Inserted)
     It->second = L.copyDefault(Alloc);
-  return parseLibraryOption(L, It->second, Args, Consumed, Alloc, &SavedArgs);
+  Error E =
+      parseLibraryOption(L, It->second, Args, Consumed, Alloc, &SavedArgs);
+  SavedLibraries.resize(SavedArgs.size(), &L);
+  return E;
 }
 
 void cl::ResetAllOptionOccurrences() {
@@ -2996,7 +3030,8 @@ static bool llvmParseCommandLineOptionsImpl(int argc, const char *const *argv,
 
   // Code without a context, such as a target machine created through the C
   // API, reads the global values.
-  replayLibraryArgs(C.args().drop_front(Begin), nullptr);
+  replayLibraryArgs(C.args().drop_front(Begin),
+                    C.argLibraries().drop_front(Begin), nullptr);
 
   return Res;
 }
diff --git a/llvm/test/tools/llvm-mc/help.test b/llvm/test/tools/llvm-mc/help.test
index 62cbdea111590b..3ce58769dd16c9 100644
--- a/llvm/test/tools/llvm-mc/help.test
+++ b/llvm/test/tools/llvm-mc/help.test
@@ -1,7 +1,11 @@
 # RUN: llvm-mc --help | FileCheck %s --check-prefix HELP --implicit-check-not='{{[Oo]}}ptions:'
+# RUN: llvm-mc --help-hidden | FileCheck %s --check-prefix HIDDEN
 
 # HELP: OVERVIEW: llvm machine code playground
 # HELP: USAGE
 # HELP: Color Options
 # HELP: Generic Options
 # HELP: MC Options
+
+# HIDDEN: Library options:
+# HIDDEN:   --crel
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index 3ceeadd0704152..4e5300be67eb32 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -1155,7 +1155,8 @@ static ld_plugin_status allSymbolsReadHook() {
 
   cl::LibraryOptionsContext LibraryOptions;
   if (!options::extra.empty())
-    cl::parseCommandLineOptions(LibraryOptions, options::extra);
+    cl::parseCommandLineOptions(LibraryOptions, codegen::getToolOptions(),
+                                options::extra);
 
   // Initialize time trace profiler
   if (!options::time_trace_file.empty())
diff --git a/llvm/tools/llc/lib/llcdriver.cpp b/llvm/tools/llc/lib/llcdriver.cpp
index d5f5c73593617d..e809319a9f0e42 100644
--- a/llvm/tools/llc/lib/llcdriver.cpp
+++ b/llvm/tools/llc/lib/llcdriver.cpp
@@ -397,7 +397,8 @@ extern "C" int llcMain(int argc, char **argv) {
   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
 
   LLVMContext Context;
-  cl::parseCommandLineOptions(Context.getLibraryOptions(), ArrayRef(argv, argc),
+  cl::parseCommandLineOptions(Context.getLibraryOptions(),
+                              codegen::getToolOptions(), ArrayRef(argv, argc),
                               "llvm system compiler\n");
 
   SmallVector<PassPlugin, 1> PluginList;
diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index f5b00bf76030d7..263946d1df2492 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -435,7 +435,8 @@ int main(int argc, char **argv, char * const *envp) {
   InitializeNativeTargetAsmParser();
 
   cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(LibraryOptions, ArrayRef(argv, argc),
+  cl::parseCommandLineOptions(LibraryOptions, codegen::getToolOptions(),
+                              ArrayRef(argv, argc),
                               "llvm interpreter & dynamic compiler\n");
 
   if (HostJITTriple) {
diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
index 613500ba0cfa77..e324592f71cd7f 100644
--- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
+++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
@@ -123,7 +123,7 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
   // Created after the targets register their library options.
   static cl::LibraryOptionsContext LibraryOptions;
   handleExecNameEncodedBEOpts(LibraryOptions, ExecName);
-  parseFuzzerCLOpts(LibraryOptions, *argc, *argv);
+  parseFuzzerCLOpts(LibraryOptions, codegen::getToolOptions(), *argc, *argv);
 
   if (TargetTriple.empty()) {
     errs() << ExecName << ": -mtriple must be specified\n";
diff --git a/llvm/tools/llvm-lto/llvm-lto.cpp b/llvm/tools/llvm-lto/llvm-lto.cpp
index 2ae1f0b4acb82c..05f2cebd70588d 100644
--- a/llvm/tools/llvm-lto/llvm-lto.cpp
+++ b/llvm/tools/llvm-lto/llvm-lto.cpp
@@ -1010,8 +1010,8 @@ int main(int argc, char **argv) {
   InitializeAllAsmParsers();
 
   cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(LibraryOptions, ArrayRef(argv, argc),
-                              "llvm LTO linker\n");
+  cl::parseCommandLineOptions(LibraryOptions, codegen::getToolOptions(),
+                              ArrayRef(argv, argc), "llvm LTO linker\n");
 
   if (OptLevel < '0' || OptLevel > '3')
     error("optimization level must be between 0 and 3");
diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp
index 506262b7c0d53d..a7835f8f306330 100644
--- a/llvm/tools/llvm-lto2/llvm-lto2.cpp
+++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp
@@ -294,7 +294,8 @@ static int usage() {
 
 static int run(int argc, char **argv) {
   cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(LibraryOptions, ArrayRef(argv, argc),
+  cl::parseCommandLineOptions(LibraryOptions, codegen::getToolOptions(),
+                              ArrayRef(argv, argc),
                               "Resolution-based LTO test harness");
 
   if (TimeTrace)
diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
index e6241ee283df4e..0d1d1c04a4dc2e 100644
--- a/llvm/tools/llvm-mc/llvm-mc.cpp
+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
@@ -49,8 +49,6 @@
 
 using namespace llvm;
 
-static mc::RegisterMCToolOptions MOF;
-
 static cl::OptionCategory MCCategory("MC Options");
 
 static cl::opt<std::string> InputFilename(cl::Positional,
@@ -397,7 +395,8 @@ int main(int argc, char **argv) {
 
   cl::HideUnrelatedOptions({&MCCategory, &getColorCategory()});
   cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(LibraryOptions, ArrayRef(argv, argc),
+  cl::parseCommandLineOptions(LibraryOptions, mc::getToolOptions(),
+                              ArrayRef(argv, argc),
                               "llvm machine code playground\n");
 
   if (TimeTrace)
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index f79d28f9fa4dce..2c2dc8e70e8179 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -66,8 +66,6 @@
 
 using namespace llvm;
 
-static mc::RegisterMCToolOptions MOF;
-
 static cl::OptionCategory ToolOptions("Tool Options");
 static cl::OptionCategory ViewOptions("View Options");
 
@@ -381,7 +379,8 @@ int main(int argc, char **argv) {
 
   // Parse flags and initialize target options.
   cl::LibraryOptionsContext LibraryOptions;
-  cl::parseCommandLineOptions(LibraryOptions, ArrayRef(argv, argc),
+  cl::parseCommandLineOptions(LibraryOptions, mc::getToolOptions(),
+                              ArrayRef(argv, argc),
                               "llvm machine code performance analyzer.\n");
 
   Triple TheTriple(TripleNameOpt.empty()
diff --git a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
index d9397795efd6f6..387280e8a799e3 100644
--- a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
+++ b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
@@ -193,7 +193,7 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
   //
 
   handleExecNameEncodedOptimizerOpts(LibraryOptions, ExecName);
-  parseFuzzerCLOpts(LibraryOptions, *argc, *argv);
+  parseFuzzerCLOpts(LibraryOptions, codegen::getToolOptions(), *argc, *argv);
 
   // Create TargetMachine
   //
diff --git a/llvm/tools/llvm-reduce/llvm-reduce.cpp b/llvm/tools/llvm-reduce/llvm-reduce.cpp
index c43da8f2f7175d..b33c7ab900a5bd 100644
--- a/llvm/tools/llvm-reduce/llvm-reduce.cpp
+++ b/llvm/tools/llvm-reduce/llvm-reduce.cpp
@@ -144,7 +144,8 @@ int main(int Argc, char **Argv) {
   LLVMContext Context;
   cl::HideUnrelatedOptions({&LLVMReduceOptions, &getColorCategory()});
   cl::parseCommandLineOptions(
-      Context.getLibraryOptions(), ArrayRef(Argv, Argc),
+      Context.getLibraryOptions(), codegen::getToolOptions(),
+      ArrayRef(Argv, Argc),
       "LLVM automatic testcase reducer.\n"
       "See https://llvm.org/docs/CommandGuide/llvm-reduce.html for more "
       "information.\n");
diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
index 74c90d62541b6c..2f7aaae7104a45 100644
--- a/llvm/tools/lto/lto.cpp
+++ b/llvm/tools/lto/lto.cpp
@@ -518,7 +518,8 @@ static void parseDebugOptions(ArrayRef<const char *> Options) {
   lto_initialize();
   std::vector<const char *> CodegenArgv(1, "libLTO");
   append_range(CodegenArgv, Options);
-  cl::parseCommandLineOptions(LTOContext->getLibraryOptions(), CodegenArgv);
+  cl::parseCommandLineOptions(LTOContext->getLibraryOptions(),
+                              codegen::getToolOptions(), CodegenArgv);
 }
 
 void lto_set_debug_options(const char *const *options, int number) {
diff --git a/llvm/tools/opt/optdriver.cpp b/llvm/tools/opt/optdriver.cpp
index aa46d98e2ef724..1473f557735cc4 100644
--- a/llvm/tools/opt/optdriver.cpp
+++ b/llvm/tools/opt/optdriver.cpp
@@ -455,7 +455,8 @@ optMain(int argc, char **argv,
 
   LLVMContext Context;
   cl::parseCommandLineOptions(
-      Context.getLibraryOptions(), ArrayRef(argv, argc),
+      Context.getLibraryOptions(), codegen::getToolOptions(),
+      ArrayRef(argv, argc),
       "llvm .bc -> .bc modular optimizer and analysis printer\n");
 
   SmallVector<PassPluginLibraryInfo, 0> Extensions;
diff --git a/llvm/unittests/Option/LibraryOptionsTest.cpp b/llvm/unittests/Option/LibraryOptionsTest.cpp
index 78ee5fde611797..56aafac9cf6460 100644
--- a/llvm/unittests/Option/LibraryOptionsTest.cpp
+++ b/llvm/unittests/Option/LibraryOptionsTest.cpp
@@ -245,4 +245,35 @@ TEST(LibraryOptionsTest, Values) {
   cl::ResetCommandLineParser();
 }
 
+// A parse recognizes an unregistered library that its caller passes, as a tool
+// opts in to options.
+TEST(LibraryOptionsTest, ToolOptions) {
+  cl::ResetCommandLineParser();
+  opt::LibraryOptionsParser L{std::in_place_type<TestLibraryOptions>};
+  cl::LibraryOptionsContext V;
+  const char *Args[] = {"prog", "-lib-count=5"};
+  EXPECT_TRUE(cl::parseCommandLineOptions(V, &L, Args, "", &nulls()));
+  EXPECT_EQ(TestLibraryOptions::Global.count, 5u);
+  L.reset();
+
+  // Passing another library replaces the earlier one's names.
+  opt::LibraryOptionsParser L2{std::in_place_type<TestLibraryOptions>};
+  EXPECT_TRUE(cl::parseCommandLineOptions(V, &L2, Args, "", &nulls()));
+  EXPECT_EQ(TestLibraryOptions::Global.count, 5u);
+  L2.reset();
+
+  // assign() replays the arguments with the library that parsed them.
+  unsigned N;
+  EXPECT_EQ(toString(V.parse(L, {"-lib-count", "7"}, N)), "");
+  cl::LibraryOptionsContext Copy;
+  Copy.assign(V);
+  EXPECT_EQ(Copy.get<TestLibraryOptions>().count, 7u);
+
+  // A parse without the library does not recognize its options. Unrelated
+  // keeps the parser from having no options at all.
+  cl::opt<bool> Unrelated("tool-options-unrelated");
+  EXPECT_FALSE(cl::parseCommandLineOptions(V, Args, "", &nulls()));
+  cl::ResetCommandLineParser();
+}
+
 } // namespace



More information about the cfe-commits mailing list