[flang-commits] [flang] [flang] Parse -mllvm library options into a context. NFC (PR #233021)

Fangrui Song via flang-commits flang-commits at lists.llvm.org
Fri Oct 16 22:49:55 PDT 2026


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

Follow-up to #231407: move flang -fc1 off cl::ParseCommandLineOptions,
which sets the libraries' `XXXOptions::Global`, to
`cl::parseCommandLineOptions(LibraryOptionsContext &, ...)`.

This prepares for removing the T::Global fallback in
`LibraryOptionsContext::get<T>()`.

The context lives in CompilerInstance like clang -cc1.

>From d171ff8aa4f00a9f5f51aa6cd7333da5f4f5ba62 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 16 Oct 2026 22:10:44 -0700
Subject: [PATCH] [flang] Parse -mllvm library options into a context. NFC

Follow-up to #231407: move flang -fc1 off cl::ParseCommandLineOptions,
which sets the libraries' `XXXOptions::Global`, to
`cl::parseCommandLineOptions(LibraryOptionsContext &, ...)`.

This prepares for removing the T::Global fallback in
`LibraryOptionsContext::get<T>()`.

The context lives in CompilerInstance like clang -cc1.
---
 flang/include/flang/Frontend/CompilerInstance.h      | 11 +++++++++++
 flang/lib/Frontend/CompilerInstance.cpp              |  1 +
 flang/lib/Frontend/FrontendActions.cpp               |  1 +
 flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp |  3 ++-
 4 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/flang/include/flang/Frontend/CompilerInstance.h b/flang/include/flang/Frontend/CompilerInstance.h
index 842f37290e109f..e4321fe74b3eee 100644
--- a/flang/include/flang/Frontend/CompilerInstance.h
+++ b/flang/include/flang/Frontend/CompilerInstance.h
@@ -21,6 +21,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Support/StringOstream.h"
 #include "llvm/Plugins/PassPlugin.h"
+#include "llvm/Support/LibraryOptionsContext.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
 
@@ -59,6 +60,9 @@ class CompilerInstance {
 
   std::unique_ptr<Fortran::semantics::SemanticsContext> semaContext;
 
+  /// The library options set by -mllvm.
+  llvm::cl::LibraryOptionsContext libraryOptions;
+
   std::unique_ptr<llvm::TargetMachine> targetMachine;
 
   /// LLVM pass plugins.
@@ -273,6 +277,13 @@ class CompilerInstance {
   }
 
   /// }
+  llvm::cl::LibraryOptionsContext &getLibraryOptions() {
+    return libraryOptions;
+  }
+  const llvm::cl::LibraryOptionsContext &getLibraryOptions() const {
+    return libraryOptions;
+  }
+
   /// @name Target Machine
   /// {
 
diff --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp
index f63b7cb1580825..91eb2fe779bef2 100644
--- a/flang/lib/Frontend/CompilerInstance.cpp
+++ b/flang/lib/Frontend/CompilerInstance.cpp
@@ -384,6 +384,7 @@ bool CompilerInstance::setUpTargetMachine() {
     tOpts.FunctionSplitting = llvm::FunctionSplittingMode::All;
   tOpts.VecLib = convertDriverVectorLibraryToVectorLibrary(CGOpts.getVecLib());
   tOpts.MCOptions.DisableIntegratedAS = CGOpts.DisableIntegratedAS;
+  tOpts.MCOptions.LibraryOptions = &libraryOptions;
   tOpts.FunctionSections = CGOpts.FunctionSections;
   tOpts.DataSections = CGOpts.DataSections;
 
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 21170893e6e898..62dd3fb0548636 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -180,6 +180,7 @@ bool CodeGenAction::beginSourceFileAction() {
     llvmModule.reset(nullptr);
   llvmCtx = std::make_unique<llvm::LLVMContext>();
   CompilerInstance &ci = this->getInstance();
+  llvmCtx->getLibraryOptions().assign(ci.getLibraryOptions());
   mlir::DefaultTimingManager &timingMgr = ci.getTimingManager();
   mlir::TimingScope &timingScopeRoot = ci.getTimingScopeRoot();
 
diff --git a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index aa06cc6ebb036a..0391042c540f33 100644
--- a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -202,7 +202,8 @@ bool executeCompilerInvocation(CompilerInstance *flang) {
       args[i + 1] = flang->getFrontendOpts().llvmArgs[i].c_str();
 
     args[numArgs + 1] = nullptr;
-    llvm::cl::ParseCommandLineOptions(numArgs + 1, args.get());
+    llvm::cl::parseCommandLineOptions(flang->getLibraryOptions(),
+                                      llvm::ArrayRef(args.get(), numArgs + 1));
   }
 
   // Honor -mmlir. This should happen AFTER plugins have been loaded!



More information about the flang-commits mailing list