[clang] 8150986 - [Flang] Support -fpass-plugin-arg (#248486)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 01:12:01 PST 2026


Author: Alexis Engelke
Date: 2026-12-05T10:11:55+01:00
New Revision: 815098613391e0c40b9286701c3facf5216f603f

URL: https://github.com/llvm/llvm-project/commit/815098613391e0c40b9286701c3facf5216f603f
DIFF: https://github.com/llvm/llvm-project/commit/815098613391e0c40b9286701c3facf5216f603f.diff

LOG: [Flang] Support -fpass-plugin-arg (#248486)

The method to pass arguments to plugins is now going through
llvm::passPluginArguments, permitting pass plugins to take options
specified on the command line without relying on llvm::cl.

This is a sync with/port of Clang's plugin argument passing.

Added: 
    

Modified: 
    clang/include/clang/Options/Options.td
    clang/lib/Driver/ToolChains/Flang.cpp
    flang/include/flang/Frontend/CodeGenOptions.h
    flang/include/flang/Frontend/CompilerInstance.h
    flang/lib/Frontend/CompilerInstance.cpp
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
    flang/test/Driver/frontend-forwarding.f90
    flang/test/Driver/pass-plugin-codegen.f90
    flang/test/Driver/pass-plugin.f90

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 4840853a4edd2d..a55e26703fc81b 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -4582,7 +4582,7 @@ def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">,
   HelpText<"Load pass plugin from a dynamic shared object file (only with new pass manager).">,
   MarshallingInfoStringVector<CodeGenOpts<"PassPlugins">>;
 def fpass_plugin_arg_EQ : Joined<["-"], "fpass-plugin-arg=">,
-  Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
+  Group<f_Group>, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   MetaVarName<"<plugin>,<arg>">, Flags<[NoArgumentUnused]>,
   HelpText<"Pass <arg> to the pass plugin named <plugin>">,
   MarshallingInfoStringVector<CodeGenOpts<"PassPluginArgs">>;

diff  --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 62be118ea8c8c9..3b2a2f63f75688 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -283,6 +283,7 @@ void Flang::addDebugOptions(const llvm::opt::ArgList &Args, const JobAction &JA,
                    options::OPT_W_Joined,
                    options::OPT_fconvert_EQ,
                    options::OPT_fpass_plugin_EQ,
+                   options::OPT_fpass_plugin_arg_EQ,
                    options::OPT_funderscoring,
                    options::OPT_fno_underscoring,
                    options::OPT_funsigned,

diff  --git a/flang/include/flang/Frontend/CodeGenOptions.h b/flang/include/flang/Frontend/CodeGenOptions.h
index 0a661e6d778a69..17adf80b1a029b 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.h
+++ b/flang/include/flang/Frontend/CodeGenOptions.h
@@ -52,6 +52,8 @@ class CodeGenOptions : public CodeGenOptionsBase {
 public:
   /// The paths to the pass plugins that were registered using -fpass-plugin.
   std::vector<std::string> LLVMPassPlugins;
+  /// Arguments to be passed to the pass plugins.
+  std::vector<std::string> LLVMPassPluginArgs;
 
   /// The prefered vector width, if requested by -mprefer-vector-width.
   std::string PreferVectorWidth;

diff  --git a/flang/include/flang/Frontend/CompilerInstance.h b/flang/include/flang/Frontend/CompilerInstance.h
index 363fc1e9dd052e..e5354f62f4994f 100644
--- a/flang/include/flang/Frontend/CompilerInstance.h
+++ b/flang/include/flang/Frontend/CompilerInstance.h
@@ -276,6 +276,8 @@ class CompilerInstance {
     return passPlugins;
   }
 
+  void handlePassPluginArguments();
+
   /// }
   llvm::cl::LibraryOptionsContext &getLibraryOptions() {
     return libraryOptions;

diff  --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp
index bfd2f49a75575a..33d596334a6ed2 100644
--- a/flang/lib/Frontend/CompilerInstance.cpp
+++ b/flang/lib/Frontend/CompilerInstance.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/PassTimingInfo.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Pass.h"
+#include "llvm/Plugins/PassPlugin.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
@@ -31,6 +32,10 @@
 #include "llvm/TargetParser/PPCTargetParser.h"
 #include "llvm/TargetParser/Triple.h"
 
+#define HANDLE_EXTENSION(Ext)                                                  \
+  llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
+#include "llvm/Support/Extension.def"
+
 using namespace Fortran::frontend;
 
 CompilerInstance::CompilerInstance()
@@ -406,3 +411,18 @@ bool CompilerInstance::setUpTargetMachine() {
   }
   return true;
 }
+
+void CompilerInstance::handlePassPluginArguments() {
+  const std::vector<std::string> &args =
+      getInvocation().getCodeGenOpts().LLVMPassPluginArgs;
+  llvm::SmallVector<llvm::PassPluginLibraryInfo, 0> infos;
+  for (const std::unique_ptr<llvm::PassPlugin> &plugin : getPassPlugins())
+    infos.push_back(plugin->getInfo());
+#define HANDLE_EXTENSION(Ext) infos.push_back(get##Ext##PluginInfo());
+#include "llvm/Support/Extension.def"
+  if (llvm::Error err = llvm::passPluginArguments(infos, args)) {
+    unsigned diagID =
+        getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "%0");
+    getDiagnostics().Report(diagID) << toString(std::move(err));
+  }
+}

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index d74c4915229142..8f6bde08057fe9 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -392,6 +392,8 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
 
   for (auto *a : args.filtered(clang::options::OPT_fpass_plugin_EQ))
     opts.LLVMPassPlugins.push_back(a->getValue());
+  for (auto *a : args.filtered(clang::options::OPT_fpass_plugin_arg_EQ))
+    opts.LLVMPassPluginArgs.push_back(a->getValue());
 
   opts.Reciprocals = clang::parseMRecipOption(diags, args);
 

diff  --git a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 0391042c540f33..f93a5ee6b84943 100644
--- a/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -191,6 +191,7 @@ bool executeCompilerInvocation(CompilerInstance *flang) {
           << path << toString(passPlugin.takeError());
     }
   }
+  flang->handlePassPluginArguments();
 
   // Honor -mllvm. This should happen AFTER plugins have been loaded!
   if (!flang->getFrontendOpts().llvmArgs.empty()) {

diff  --git a/flang/test/Driver/frontend-forwarding.f90 b/flang/test/Driver/frontend-forwarding.f90
index 2cc5524d351ff8..862487c4ea636b 100644
--- a/flang/test/Driver/frontend-forwarding.f90
+++ b/flang/test/Driver/frontend-forwarding.f90
@@ -19,6 +19,7 @@
 ! RUN:     -fno-strict-overflow \
 ! RUN:     -fomit-frame-pointer \
 ! RUN:     -fpass-plugin=Bye%pluginext \
+! RUN:     -fpass-plugin-arg=Bye,-wave-goodbye \
 ! RUN:     -fversion-loops-for-stride \
 ! RUN:     -fno-ppc-native-vector-element-order \
 ! RUN:     -fppc-native-vector-element-order \
@@ -50,6 +51,7 @@
 ! CHECK: "-freciprocal-math"
 ! CHECK: "-fconvert=little-endian"
 ! CHECK: "-fpass-plugin=Bye
+! CHECK: "-fpass-plugin-arg=Bye,-wave-goodbye"
 ! CHECK: "-fversion-loops-for-stride"
 ! CHECK: "-fno-ppc-native-vector-element-order"
 ! CHECK: "-fppc-native-vector-element-order"

diff  --git a/flang/test/Driver/pass-plugin-codegen.f90 b/flang/test/Driver/pass-plugin-codegen.f90
index bdc7bd0b4ec4b0..529e18b6ddd1c4 100644
--- a/flang/test/Driver/pass-plugin-codegen.f90
+++ b/flang/test/Driver/pass-plugin-codegen.f90
@@ -14,24 +14,24 @@
 
 ! With -last-words the pass intercepts the code generation and prints
 ! "CodeGen Bye" instead.
-! RUN: %flang_fc1 -S %s -o - %loadbye -mllvm -last-words \
+! RUN: %flang_fc1 -S %s -o - %loadbye -fpass-plugin-arg=Bye,-last-words \
 ! RUN: | FileCheck %s --check-prefix=CHECK-ACTIVE
 
 ! With split DWARF output to a separate file, the pass receives the .dwo stream
 ! and writes to it as well.
-! RUN: %flang_fc1 -S %s -o - %loadbye -mllvm -last-words \
+! RUN: %flang_fc1 -S %s -o - %loadbye -fpass-plugin-arg=Bye,-last-words \
 ! RUN: -debug-info-kind=standalone -split-dwarf-file %t.dwo \
 ! RUN: -split-dwarf-output %t.dwo | FileCheck %s --check-prefix=CHECK-ACTIVE
 ! RUN: FileCheck %s --check-prefix=CHECK-DWO < %t.dwo
 
 ! When emitting LLVM IR, no back-end is executed and therefore no
 ! pre-codegen hook of LLVM pass plugins are executed.
-! RUN: %flang_fc1 -emit-llvm %s -o - %loadbye -mllvm -last-words \
+! RUN: %flang_fc1 -emit-llvm %s -o - %loadbye -fpass-plugin-arg=Bye,-last-words \
 ! RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-LLVM
 
 ! Bye fails and reports an error when an object file should be emitted.
 ! (Note that this is specific for Bye; other plugins can support this.)
-! RUN: not %flang_fc1 -emit-obj %s -o - %loadbye -mllvm -last-words \
+! RUN: not %flang_fc1 -emit-obj %s -o - %loadbye -fpass-plugin-arg=Bye,-last-words \
 ! RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-ERR
 
 ! CHECK-INACTIVE-NOT: Bye

diff  --git a/flang/test/Driver/pass-plugin.f90 b/flang/test/Driver/pass-plugin.f90
index 5669025c2c7877..64e5854146e3ac 100644
--- a/flang/test/Driver/pass-plugin.f90
+++ b/flang/test/Driver/pass-plugin.f90
@@ -7,11 +7,11 @@
 ! Plugins are currently broken on AIX, at least in the CI.
 ! XFAIL: system-aix
 
-! RUN: %flang -S %s %loadbye -mllvm -wave-goodbye -o /dev/null \
-! RUN: 2>&1 | FileCheck %s
+! RUN: %flang -S %s %loadbye -fpass-plugin-arg=Bye,-wave-goodbye \
+! RUN: -o /dev/null 2>&1 | FileCheck %s
 
-! RUN: %flang_fc1 -S %s %loadbye -mllvm -wave-goodbye -o /dev/null \
-! RUN: 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -S %s %loadbye -fpass-plugin-arg=Bye,-wave-goodbye \
+! RUN: -o /dev/null 2>&1 | FileCheck %s
 
 
 ! CHECK: Bye: empty_


        


More information about the cfe-commits mailing list