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

via flang-commits flang-commits at lists.llvm.org
Fri Oct 16 23:19:17 PDT 2026


Author: Fangrui Song
Date: 2026-10-17T06:19:11Z
New Revision: 4937515a87d776d925ffb02efc855e6112fd9da2

URL: https://github.com/llvm/llvm-project/commit/4937515a87d776d925ffb02efc855e6112fd9da2
DIFF: https://github.com/llvm/llvm-project/commit/4937515a87d776d925ffb02efc855e6112fd9da2.diff

LOG: [flang] Parse -mllvm library options into a context. NFC (#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.

Added: 
    

Modified: 
    flang/include/flang/Frontend/CompilerInstance.h
    flang/lib/Frontend/CompilerInstance.cpp
    flang/lib/Frontend/FrontendActions.cpp
    flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Removed: 
    


################################################################################
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