From: Philip Pfaffe Date: Sat, 2 Feb 2019 23:19:32 +0000 (+0000) Subject: [NewPM] Add support for new-PM plugins to clang X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cda98073a0c79faff5ab069a7b49fff27894ea28;p=clang [NewPM] Add support for new-PM plugins to clang Summary: This adds support for new-PM plugin loading to clang. The option `-fpass-plugin=` may be used to specify a dynamic shared object file that adheres to the PassPlugin API. Tested: created simple plugin that registers an EP callback; with optimization level > 0, the pass is run as expected. Committed on behalf of Marco Elver Differential Revision: https://reviews.llvm.org/D56935 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352972 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/CodeGenOptions.h b/include/clang/Basic/CodeGenOptions.h index a18bcd9c87..5c8abe359e 100644 --- a/include/clang/Basic/CodeGenOptions.h +++ b/include/clang/Basic/CodeGenOptions.h @@ -287,6 +287,9 @@ public: std::vector DefaultFunctionAttrs; + /// List of dynamic shared object files to be loaded as pass plugins. + std::vector PassPlugins; + public: // Define accessors/mutators for code generation options of enumeration type. #define CODEGENOPT(Name, Bits, Default) diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 2e450a5f23..e8d5873572 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -1612,6 +1612,9 @@ def frwpi : Flag<["-"], "frwpi">, Group; def fno_rwpi : Flag<["-"], "fno-rwpi">, Group; def fplugin_EQ : Joined<["-"], "fplugin=">, Group, Flags<[DriverOption]>, MetaVarName<"">, HelpText<"Load the named plugin (dynamic shared object)">; +def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">, + Group, Flags<[CC1Option]>, MetaVarName<"">, + HelpText<"Load pass plugin from a dynamic shared object file (only with new pass manager).">; def fpreserve_as_comments : Flag<["-"], "fpreserve-as-comments">, Group; def fno_preserve_as_comments : Flag<["-"], "fno-preserve-as-comments">, Group, Flags<[CC1Option]>, HelpText<"Do not preserve comments in inline assembly">; diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 76f5f915e6..cc704c7798 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -36,6 +36,7 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/SubtargetFeature.h" #include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" #include "llvm/Support/BuryPointer.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/MemoryBuffer.h" @@ -961,6 +962,17 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager( PassBuilder PB(TM.get(), PGOOpt); + // Attempt to load pass plugins and register their callbacks with PB. + for (auto &PluginFN : CodeGenOpts.PassPlugins) { + auto PassPlugin = PassPlugin::Load(PluginFN); + if (PassPlugin) { + PassPlugin->registerPassBuilderCallbacks(PB); + } else { + Diags.Report(diag::err_fe_unable_to_load_plugin) + << PluginFN << toString(PassPlugin.takeError()); + } + } + LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager); FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager); CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager); diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp index c02446bcd5..f125bce01d 100644 --- a/lib/Driver/ToolChains/Clang.cpp +++ b/lib/Driver/ToolChains/Clang.cpp @@ -5077,6 +5077,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, A->claim(); } + // Forward -fpass-plugin=name.so to -cc1. + for (const Arg *A : Args.filtered(options::OPT_fpass_plugin_EQ)) { + CmdArgs.push_back( + Args.MakeArgString(Twine("-fpass-plugin=") + A->getValue())); + A->claim(); + } + // Setup statistics file output. SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); if (!StatsFile.empty()) diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 05cce3c909..b991a89e41 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -1322,6 +1322,8 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.DefaultFunctionAttrs = Args.getAllArgValues(OPT_default_function_attr); + Opts.PassPlugins = Args.getAllArgValues(OPT_fpass_plugin_EQ); + return Success; }