Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2695,7 +2695,7 @@ MetaVarName<"-">, HelpText<"Pass to plugin ">; def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">, - Group, Flags<[CC1Option]>, MetaVarName<"">, + Group, Flags<[CC1Option,FlangOption,FC1Option]>, MetaVarName<"">, HelpText<"Load pass plugin from a dynamic shared object file (only with new pass manager).">, MarshallingInfoStringVector>; defm preserve_as_comments : BoolFOption<"preserve-as-comments", Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -55,7 +55,7 @@ Args.AddAllArgs(CmdArgs, {options::OPT_module_dir, options::OPT_fdebug_module_writer, options::OPT_fintrinsic_modules_path, options::OPT_pedantic, - options::OPT_std_EQ, options::OPT_W_Joined}); + options::OPT_std_EQ, options::OPT_W_Joined,options::OPT_fpass_plugin_EQ}); } void Flang::AddPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const { Index: flang/docs/FlangDriver.md =================================================================== --- flang/docs/FlangDriver.md +++ flang/docs/FlangDriver.md @@ -507,3 +507,23 @@ to re-analyze expressions and modify scope or symbols. You can check [Semantics.md](Semantics.md) for more details on how `ParseTree` is edited e.g. during the semantic checks. + +# LLVM Pass Plugins + +Pass plugins are an extension to the frontend drive that make it possible to run +out-of-tree LLVM passes on the LLVM-IR. The passes are run after lowering to +LLVM-IR. The exact position of the pass in the pipeline will depend on how it +has been registered with the `llvm::PassBuilder`. See the documentation for +`llvm::PassBuilder` for details. + +The framework to enable pass plugins in Flang uses the exact same machinery as +that used by clang and thus has the same capabilities and limitations. + +In order to use a pass plugin, the pass(es) must be compiled into a dynamic +shared object which is then loaded using the `-fpass-plugin` option. + +``` +flang-new -fpass-plugin=/path/to/plugin.so +``` + +This option is available in both the compiler driver and the frontend driver. Index: flang/docs/ReleaseNotes.md =================================================================== --- flang/docs/ReleaseNotes.md +++ flang/docs/ReleaseNotes.md @@ -24,6 +24,10 @@ ## Major New Features +* Flang now supports loading LLVM pass plugins with the `-fpass-plugin` option + which is also available in clang. The option mimics the behavior of the + corresponding option in clang and has the same capabilities and limitations. + ## Bug Fixes ## Non-comprehensive list of changes in this release Index: flang/include/flang/Frontend/CodeGenOptions.h =================================================================== --- flang/include/flang/Frontend/CodeGenOptions.h +++ flang/include/flang/Frontend/CodeGenOptions.h @@ -45,6 +45,9 @@ /// to the LLVM backend. class CodeGenOptions : public CodeGenOptionsBase { +public: + std::vector LLVMPassPlugins; + public: // Define accessors/mutators for code generation options of enumeration type. #define CODEGENOPT(Name, Bits, Default) Index: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ flang/lib/Frontend/CompilerInvocation.cpp @@ -125,6 +125,9 @@ clang::driver::options::OPT_fno_debug_pass_manager, false)) opts.DebugPassManager = 1; + for (auto *a : args.filtered(clang::driver::options::OPT_fpass_plugin_EQ)) + opts.LLVMPassPlugins.push_back(a->getValue()); + // -mrelocation-model option. if (const llvm::opt::Arg *A = args.getLastArg(clang::driver::options::OPT_mrelocation_model)) { Index: flang/lib/Frontend/FrontendActions.cpp =================================================================== --- flang/lib/Frontend/FrontendActions.cpp +++ flang/lib/Frontend/FrontendActions.cpp @@ -46,6 +46,7 @@ #include "llvm/IRReader/IRReader.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" #include "llvm/Passes/StandardInstrumentations.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/SourceMgr.h" @@ -660,6 +661,7 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) { auto opts = getInstance().getInvocation().getCodeGenOpts(); + auto &diags = getInstance().getDiagnostics(); llvm::OptimizationLevel level = mapToLevel(opts); // Create the analysis managers. @@ -676,6 +678,17 @@ si.registerCallbacks(pic, &fam); llvm::PassBuilder pb(tm.get(), pto, pgoOpt, &pic); + // Attempt to load pass plugins and register their callbacks with PB. + for (auto &pluginFile : opts.LLVMPassPlugins) { + auto passPlugin = llvm::PassPlugin::Load(pluginFile); + if (passPlugin) { + passPlugin->registerPassBuilderCallbacks(pb); + } else { + diags.Report(clang::diag::err_fe_unable_to_load_plugin) + << pluginFile << passPlugin.takeError(); + } + } + // Register all the basic analyses with the managers. pb.registerModuleAnalyses(mam); pb.registerCGSCCAnalyses(cgam); Index: flang/test/Driver/driver-help-hidden.f90 =================================================================== --- flang/test/Driver/driver-help-hidden.f90 +++ flang/test/Driver/driver-help-hidden.f90 @@ -44,6 +44,7 @@ ! CHECK-NEXT: -fno-integrated-as Disable the integrated assembler ! CHECK-NEXT: -fopenacc Enable OpenACC ! CHECK-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code. +! CHECK-NEXT: -fpass-plugin= Load pass plugin from a dynamic shared object file (only with new pass manager). ! CHECK-NEXT: -fsyntax-only Run the preprocessor, parser and semantic analysis stages ! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV. ! CHECK-NEXT: -help Display available options @@ -71,4 +72,3 @@ ! Frontend driver -help-hidden is not supported ! ERROR-FLANG-FC1: error: unknown argument: '{{.*}}' - Index: flang/test/Driver/driver-help.f90 =================================================================== --- flang/test/Driver/driver-help.f90 +++ flang/test/Driver/driver-help.f90 @@ -42,6 +42,7 @@ ! HELP-NEXT: -fno-integrated-as Disable the integrated assembler ! HELP-NEXT: -fopenacc Enable OpenACC ! HELP-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code. +! HELP-NEXT: -fpass-plugin= Load pass plugin from a dynamic shared object file (only with new pass manager). ! HELP-NEXT: -fsyntax-only Run the preprocessor, parser and semantic analysis stages ! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV. ! HELP-NEXT: -help Display available options @@ -120,6 +121,7 @@ ! HELP-FC1-NEXT: -fno-reformat Dump the cooked character stream in -E mode ! HELP-FC1-NEXT: -fopenacc Enable OpenACC ! HELP-FC1-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code. +! HELP-FC1-NEXT: -fpass-plugin= Load pass plugin from a dynamic shared object file (only with new pass manager). ! HELP-FC1-NEXT: -fsyntax-only Run the preprocessor, parser and semantic analysis stages ! HELP-FC1-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV. ! HELP-FC1-NEXT: -help Display available options Index: flang/test/Driver/frontend-forwarding.f90 =================================================================== --- flang/test/Driver/frontend-forwarding.f90 +++ flang/test/Driver/frontend-forwarding.f90 @@ -1,12 +1,15 @@ ! Test that flang-new forwards Flang frontend ! options to flang-new -fc1 as expected. +! REQUIRES: plugins, shell + ! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \ ! RUN: -finput-charset=utf-8 \ ! RUN: -fdefault-double-8 \ ! RUN: -fdefault-integer-8 \ ! RUN: -fdefault-real-8 \ ! RUN: -flarge-sizes \ +! RUN: -fpass-plugin=Bye.so \ ! RUN: -mllvm -print-before-all\ ! RUN: -P \ ! RUN: | FileCheck %s @@ -17,4 +20,5 @@ ! CHECK: "-fdefault-integer-8" ! CHECK: "-fdefault-real-8" ! CHECK: "-flarge-sizes" +! CHECK: "-fpass-plugin=Bye.so" ! CHECK: "-mllvm" "-print-before-all" Index: flang/test/Driver/pass-plugin-not-found.f90 =================================================================== --- /dev/null +++ flang/test/Driver/pass-plugin-not-found.f90 @@ -0,0 +1,8 @@ +! Check the correct error diagnostic is reported when a pass plugin shared object isn't found + +! REQUIRES: plugins, shell + +! RUN: not %flang -fpass-plugin=X.Y %s 2>&1 | FileCheck %s --check-prefix=ERROR +! RUN: not %flang_fc1 -emit-llvm -o /dev/null -fpass-plugin=X.Y %s 2>&1 | FileCheck %s --check-prefix=ERROR + +! ERROR: error: unable to load plugin 'X.Y': 'Could not load library 'X.Y': X.Y: cannot open shared object file: No such file or directory' Index: flang/test/Driver/pass-plugin.f90 =================================================================== --- /dev/null +++ flang/test/Driver/pass-plugin.f90 @@ -0,0 +1,13 @@ +! Verify that the plugin passed to -fpass-plugin is loaded and run + +! UNSUPPORTED: system-windows + +! REQUIRES: plugins, shell + +! RUN: %flang -S %s -fpass-plugin=%llvmshlibdir/Bye.so -Xflang -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-PASS-PLUGIN +! RUN: %flang_fc1 -S %s -fpass-plugin=%llvmshlibdir/Bye.so -fdebug-pass-manager -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-PASS-PLUGIN + +! CHECK-PASS-PLUGIN: Running pass: {anonymous}::Bye on empty_ + +subroutine empty +end subroutine empty