[clang] [clang-repl] Rework -mllvm handling (PR #242861)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 15 12:40:14 PST 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/242861

Parse -mllvm for the host and device CompilerInstances before their
IncrementalActions run, returning a bad option as an llvm::Error from
Interpreter::create rather than exiting the process (#197133). Point the
Orc and Wasm TargetMachines, which ignore the parsed options, at the
LLVMContext's library options. Drop the Wasm executor's re-parse with
cl::ParseCommandLineOptions, which rejects library options; lld no
longer resets LLVM options (#242717).

Parse clang-repl's own command line after target initialization with
cl::parseCommandLineOptions, forwarding library options as -mllvm, so
`clang-repl -print-after-isel` works again.

Stop calling llvm_shutdown() after each unittest suite; it destroyed
the option registry that later suites need.

LLM-aided

>From e0939de1e53b78b8e2aade6707887923147579b3 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 15 Nov 2026 11:50:43 -0800
Subject: [PATCH] [clang-repl] Rework -mllvm handling

Parse -mllvm for the host and device CompilerInstances before their
IncrementalActions run, returning a bad option as an llvm::Error from
Interpreter::create rather than exiting the process (#197133). Point the
Orc and Wasm TargetMachines, which ignore the parsed options, at the
LLVMContext's library options. Drop the Wasm executor's re-parse with
cl::ParseCommandLineOptions, which rejects library options; lld no
longer resets LLVM options (#242717).

Parse clang-repl's own command line after target initialization with
cl::parseCommandLineOptions, forwarding library options as -mllvm, so
`clang-repl -print-after-isel` works again.

Stop calling llvm_shutdown() after each unittest suite; it destroyed
the option registry that later suites need.

LLM-aided
---
 .../include/clang/Frontend/CompilerInstance.h |  8 +++---
 .../clang/Interpreter/IncrementalExecutor.h   |  3 ---
 clang/lib/Frontend/CompilerInstance.cpp       | 23 +++++++---------
 clang/lib/Interpreter/Interpreter.cpp         | 13 +++++-----
 .../OrcIncrementalExecutorBuilder.cpp         |  9 +++++++
 clang/lib/Interpreter/Wasm.cpp                | 26 ++++---------------
 clang/lib/Interpreter/Wasm.h                  |  5 +---
 clang/test/Interpreter/mllvm.cpp              | 11 ++++++++
 clang/tools/clang-repl/ClangRepl.cpp          | 18 +++++++++----
 .../unittests/Interpreter/InterpreterTest.cpp | 11 ++++++++
 .../Interpreter/InterpreterTestFixture.h      | 13 ----------
 11 files changed, 70 insertions(+), 70 deletions(-)
 create mode 100644 clang/test/Interpreter/mllvm.cpp

diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index bceb85f6e923bf..c001229eea7fd8 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -269,10 +269,10 @@ class CompilerInstance : public ModuleLoader {
   /// Load the list of plugins requested in the \c FrontendOptions.
   void LoadRequestedPlugins();
 
-  /// Parse and apply LLVM command line arguments from FrontendOptions.
-  /// This processes the LLVMArgs option that comes from -mllvm flags.
-  /// This should be called after plugins are loaded and before ExecuteAction.
-  void parseLLVMArgs();
+  /// Parse the -mllvm options in FrontendOptions::LLVMArgs, setting library
+  /// options in getLibraryOptions(). Call after loading plugins. Return false
+  /// after reporting errors to \p Errs, or exit if \p Errs is null.
+  bool parseLLVMArgs(llvm::raw_ostream *Errs = nullptr);
 
   const llvm::cl::LibraryOptionsContext &getLibraryOptions() const {
     return LibraryOptions;
diff --git a/clang/include/clang/Interpreter/IncrementalExecutor.h b/clang/include/clang/Interpreter/IncrementalExecutor.h
index 6615355b798c1a..4e243852123441 100644
--- a/clang/include/clang/Interpreter/IncrementalExecutor.h
+++ b/clang/include/clang/Interpreter/IncrementalExecutor.h
@@ -17,7 +17,6 @@
 
 #include <memory>
 #include <string>
-#include <vector>
 
 namespace llvm {
 class Triple;
@@ -46,8 +45,6 @@ class IncrementalExecutorBuilder {
 
   /// An optional external IncrementalExecutor.
   std::unique_ptr<IncrementalExecutor> IE;
-  /// Frontend -mllvm arguments for backends that need to restore LLVM options.
-  std::vector<std::string> LLVMArgs;
 
   virtual ~IncrementalExecutorBuilder();
 
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 7e9d43c418a0f7..1f6e5629d7b728 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1135,19 +1135,16 @@ void CompilerInstance::LoadRequestedPlugins() {
   }
 }
 
-void CompilerInstance::parseLLVMArgs() {
-  if (!getFrontendOpts().LLVMArgs.empty()) {
-    unsigned NumArgs = getFrontendOpts().LLVMArgs.size();
-    auto Args = std::make_unique<const char *[]>(NumArgs + 2);
-    Args[0] = "clang (LLVM option parsing)";
-    for (unsigned i = 0; i != NumArgs; ++i)
-      Args[i + 1] = getFrontendOpts().LLVMArgs[i].c_str();
-    Args[NumArgs + 1] = nullptr;
-    llvm::cl::parseCommandLineOptions(LibraryOptions,
-                                      ArrayRef(Args.get(), NumArgs + 1),
-                                      /*Overview=*/"", /*Errs=*/nullptr,
-                                      /*VFS=*/&getVirtualFileSystem());
-  }
+bool CompilerInstance::parseLLVMArgs(raw_ostream *Errs) {
+  const std::vector<std::string> &LLVMArgs = getFrontendOpts().LLVMArgs;
+  if (LLVMArgs.empty())
+    return true;
+  SmallVector<const char *> Args = {"clang (LLVM option parsing)"};
+  for (const std::string &Arg : LLVMArgs)
+    Args.push_back(Arg.c_str());
+  return llvm::cl::parseCommandLineOptions(LibraryOptions, Args,
+                                           /*Overview=*/"", Errs,
+                                           &getVirtualFileSystem());
 }
 
 /// Determine the appropriate source input kind based on language
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index d6cce74b7275f7..dcb7824d270a1a 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -228,6 +228,12 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
 
 static llvm::Error ExecuteIncrementalAction(CompilerInstance &CI,
                                             IncrementalAction &Act) {
+  // Parse -mllvm before the action gives CI's library options to the
+  // LLVMContext.
+  std::string Msg;
+  llvm::raw_string_ostream OS(Msg);
+  if (!CI.parseLLVMArgs(&OS))
+    return llvm::createStringError(llvm::StringRef(Msg).rtrim());
   if (!CI.ExecuteAction(Act) || CI.getDiagnostics().hasErrorOccurred()) {
     return llvm::createStringError(llvm::errc::not_supported,
                                    "Failed to execute incremental action");
@@ -443,9 +449,6 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
   auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
   TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
 
-  // Honor -mllvm options
-  CI->parseLLVMArgs();
-
   Act = TSCtx->withContextDo([&](llvm::LLVMContext *Ctx) {
     return std::make_unique<IncrementalAction>(*CI, *Ctx, ErrOut, *this,
                                                std::move(Consumer));
@@ -725,10 +728,6 @@ llvm::Error Interpreter::CreateExecutor() {
   if (!IncrExecutorBuilder)
     IncrExecutorBuilder = IncrementalExecutorBuilder::createDefault();
 
-  // Propagate mllvm args so the wasm executor can restore them after each
-  // lldMain invocation (which resets all cl options for test isolation).
-  IncrExecutorBuilder->LLVMArgs = CI->getFrontendOpts().LLVMArgs;
-
   auto ExecutorOrErr = IncrExecutorBuilder->create(*TSCtx, CI->getTarget());
   if (ExecutorOrErr)
     IncrExecutor = std::move(*ExecutorOrErr);
diff --git a/clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp b/clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp
index 89db2d71197a0a..e84f9fd1fcc543 100644
--- a/clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp
+++ b/clang/lib/Interpreter/OrcIncrementalExecutorBuilder.cpp
@@ -459,6 +459,15 @@ OrcIncrementalExecutorBuilder::createExecutor(llvm::orc::ThreadSafeContext &TSC,
           });
   }
 
+  // Point the JIT's TargetMachine at the LLVMContext's -mllvm library options.
+  // A builder without a JTMB, which LLJIT detects later, keeps the defaults.
+  std::optional<llvm::orc::JITTargetMachineBuilder> &JTMB =
+      JITBuilder->getJITTargetMachineBuilder();
+  if (JTMB && !JTMB->getOptions().MCOptions.LibraryOptions)
+    TSC.withContextDo([&](llvm::LLVMContext *Ctx) {
+      JTMB->getOptions().MCOptions.LibraryOptions = &Ctx->getLibraryOptions();
+    });
+
   llvm::Error Err = llvm::Error::success();
   std::unique_ptr<IncrementalExecutor> Executor =
       std::make_unique<OrcIncrementalExecutor>(TSC, *JITBuilder, Err);
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 95e3b2f1ad96cd..e6a7c11505d658 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -12,10 +12,10 @@
 
 #include "Wasm.h"
 
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/TargetParser/Host.h"
+#include <llvm/IR/LLVMContext.h>
 #include <llvm/IR/LegacyPassManager.h>
 #include <llvm/IR/Module.h>
 #include <llvm/MC/TargetRegistry.h>
@@ -87,15 +87,13 @@ WasmIncrementalExecutorBuilder::createExecutor(
     llvm::orc::ThreadSafeContext &TSC, const clang::TargetInfo &TI) {
   llvm::Error Err = llvm::Error::success();
   std::unique_ptr<IncrementalExecutor> Executor =
-      std::make_unique<WasmIncrementalExecutor>(Err, LLVMArgs);
+      std::make_unique<WasmIncrementalExecutor>(Err);
   if (Err)
     return std::move(Err);
   return std::move(Executor);
 }
 
-WasmIncrementalExecutor::WasmIncrementalExecutor(
-    llvm::Error &Err, std::vector<std::string> LLVMArgs)
-    : StoredLLVMArgs(std::move(LLVMArgs)) {
+WasmIncrementalExecutor::WasmIncrementalExecutor(llvm::Error &Err) {
   llvm::ErrorAsOutParameter EAO(&Err);
 
   if (Err)
@@ -122,6 +120,8 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   }
 
   llvm::TargetOptions TO = llvm::TargetOptions();
+  TO.MCOptions.LibraryOptions =
+      &PTU.TheModule->getContext().getLibraryOptions();
   llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
       PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
 
@@ -176,22 +176,6 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
   if (!Result.canRunAgain)
     lld::exitLld(Result.retCode);
 
-  // lld::wasm::linkerMain calls cl::ResetAllOptionOccurrences() which wipes
-  // all global LLVM cl options, including mllvm flags set by the frontend
-  // (e.g. -wasm-enable-eh, -wasm-enable-sjlj). Re-apply them so the next
-  // Parse() call's WebAssemblyTargetMachine creation finds the correct state.
-  //
-  // FIXME: Remove this once library command-line options no longer rely on
-  // process-global cl::opt state. See:
-  // https://discourse.llvm.org/t/rfc-declare-library-command-line-options-in-tablegen-one-struct-per-library/91877
-  if (!StoredLLVMArgs.empty()) {
-    std::vector<const char *> ArgPtrs;
-    ArgPtrs.push_back("clang-repl (restoring LLVM options)");
-    for (const std::string &Arg : StoredLLVMArgs)
-      ArgPtrs.push_back(Arg.c_str());
-    llvm::cl::ParseCommandLineOptions(ArgPtrs.size(), ArgPtrs.data());
-  }
-
   if (Result.retCode)
     return llvm::make_error<llvm::StringError>(
         "Failed to link incremental module", llvm::inconvertibleErrorCode());
diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h
index 4eb6c8d05f37db..3134e839afc337 100644
--- a/clang/lib/Interpreter/Wasm.h
+++ b/clang/lib/Interpreter/Wasm.h
@@ -19,8 +19,6 @@
 
 #include "clang/Interpreter/IncrementalExecutor.h"
 #include "llvm/ADT/SmallString.h"
-#include <string>
-#include <vector>
 
 namespace clang {
 
@@ -39,7 +37,7 @@ class WasmIncrementalExecutorBuilder : public IncrementalExecutorBuilder {
 
 class WasmIncrementalExecutor : public IncrementalExecutor {
 public:
-  WasmIncrementalExecutor(llvm::Error &Err, std::vector<std::string> LLVMArgs);
+  WasmIncrementalExecutor(llvm::Error &Err);
   ~WasmIncrementalExecutor() override;
 
   llvm::Error addModule(PartialTranslationUnit &PTU) override;
@@ -53,7 +51,6 @@ class WasmIncrementalExecutor : public IncrementalExecutor {
 
 private:
   llvm::SmallString<256> TempDir;
-  std::vector<std::string> StoredLLVMArgs;
 };
 
 } // namespace clang
diff --git a/clang/test/Interpreter/mllvm.cpp b/clang/test/Interpreter/mllvm.cpp
new file mode 100644
index 00000000000000..8e97a2beab3b54
--- /dev/null
+++ b/clang/test/Interpreter/mllvm.cpp
@@ -0,0 +1,11 @@
+// LLVM options reach the JIT's TargetMachine for every input.
+// RUN: cat %s | clang-repl -Xcc -mllvm -Xcc -print-after-isel 2>&1 | FileCheck %s
+// RUN: cat %s | clang-repl -print-after-isel 2>&1 | FileCheck %s
+
+extern "C" int f1() { return 1; }
+int v1 = f1();
+// CHECK: # Machine code for function f1:
+
+extern "C" int f2() { return 2; }
+int v2 = f2();
+// CHECK: # Machine code for function f2:
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index df0786796cb8ca..99d408af4f5a8d 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -25,6 +25,7 @@
 #include "llvm/LineEditor/LineEditor.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/LibraryOptionsContext.h"
 #include "llvm/Support/ManagedStatic.h" // llvm_shutdown
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Signals.h"
@@ -260,13 +261,8 @@ int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
 
   ExitOnErr.setBanner("clang-repl: ");
-  llvm::cl::ParseCommandLineOptions(argc, argv);
-
   llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
 
-  std::vector<const char *> ClangArgv(ClangArgs.size());
-  std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
-                 [](const std::string &s) -> const char * { return s.data(); });
   // Initialize all targets (required for device offloading)
   llvm::InitializeAllTargetInfos();
   llvm::InitializeAllTargets();
@@ -274,6 +270,18 @@ int main(int argc, const char **argv) {
   llvm::InitializeAllAsmPrinters();
   llvm::InitializeAllAsmParsers();
 
+  // Parse after the target initialization, which registers target library
+  // options. Pass library options to the compiler as -mllvm.
+  llvm::cl::LibraryOptionsContext LibOpts;
+  llvm::cl::parseCommandLineOptions(LibOpts, llvm::ArrayRef(argv, argc));
+  std::vector<const char *> ClangArgv;
+  for (const std::string &Arg : ClangArgs)
+    ClangArgv.push_back(Arg.c_str());
+  for (const char *Arg : LibOpts.args()) {
+    ClangArgv.push_back("-mllvm");
+    ClangArgv.push_back(Arg);
+  }
+
   auto IEB = clang::IncrementalExecutorBuilder::createDefault();
   if (OptHostSupportsJit) {
     llvm::outs() << (IEB->supportsJIT() ? "true\n" : "false\n");
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 41c1facc8f2f08..9a20e0024919e3 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -688,4 +688,15 @@ TEST_F(InterpreterTest, EmscriptenExceptionHandling) {
       Interp->ParseAndExecute("try { throw 1; } catch (...) { 0; }"));
 }
 
+TEST_F(InterpreterTest, BadLLVMArgs) {
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs({"-mllvm", "-clang-repl-unknown-option"});
+  auto Interp = Interpreter::create(cantFail(CB.CreateCpp()));
+  ASSERT_FALSE(!!Interp);
+  EXPECT_THAT(
+      llvm::toString(Interp.takeError()),
+      testing::HasSubstr(
+          "Unknown command line argument '-clang-repl-unknown-option'"));
+}
+
 } // end anonymous namespace
diff --git a/clang/unittests/Interpreter/InterpreterTestFixture.h b/clang/unittests/Interpreter/InterpreterTestFixture.h
index c4a30dd06220c4..627b728b2f0daa 100644
--- a/clang/unittests/Interpreter/InterpreterTestFixture.h
+++ b/clang/unittests/Interpreter/InterpreterTestFixture.h
@@ -11,7 +11,6 @@
 
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/Support/Error.h"
-#include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/TargetSelect.h"
 
 #include "gtest/gtest.h"
@@ -54,18 +53,6 @@ class InterpreterTestBase : public ::testing::Test {
     llvm::InitializeNativeTarget();
     llvm::InitializeNativeTargetAsmPrinter();
   }
-
-  static void TearDownTestSuite() {
-    // llvm_shutdown() cleans up JIT (LLJIT/ORC) state between test suites.
-    // On Emscripten, WasmIncrementalExecutor is used instead of LLJIT, so
-    // there is no JIT state to tear down. Calling llvm_shutdown() here would
-    // destroy the ManagedStatic<CommandLineParser> global, permanently
-    // deregistering all cl::opt options (wasm-enable-eh, etc.) since their
-    // constructors already ran and will not fire again after the reset.
-#ifndef __EMSCRIPTEN__
-    llvm::llvm_shutdown();
-#endif
-  }
 };
 
 } // namespace clang



More information about the cfe-commits mailing list