]> granicus.if.org Git - clang/commitdiff
[NewPM] Add support for new-PM plugins to clang
authorPhilip Pfaffe <philip.pfaffe@gmail.com>
Sat, 2 Feb 2019 23:19:32 +0000 (23:19 +0000)
committerPhilip Pfaffe <philip.pfaffe@gmail.com>
Sat, 2 Feb 2019 23:19:32 +0000 (23:19 +0000)
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

include/clang/Basic/CodeGenOptions.h
include/clang/Driver/Options.td
lib/CodeGen/BackendUtil.cpp
lib/Driver/ToolChains/Clang.cpp
lib/Frontend/CompilerInvocation.cpp

index a18bcd9c87b823f8aea3d709ee800cdd946eeff2..5c8abe359ebfe3c75fe2c1a255fb1cf16b9c79de 100644 (file)
@@ -287,6 +287,9 @@ public:
 
   std::vector<std::string> DefaultFunctionAttrs;
 
+  /// List of dynamic shared object files to be loaded as pass plugins.
+  std::vector<std::string> PassPlugins;
+
 public:
   // Define accessors/mutators for code generation options of enumeration type.
 #define CODEGENOPT(Name, Bits, Default)
index 2e450a5f2393e21c6b20b3e57294608f163151c3..e8d5873572a6348250d9979b285cda4eeab897f0 100644 (file)
@@ -1612,6 +1612,9 @@ def frwpi : Flag<["-"], "frwpi">, Group<f_Group>;
 def fno_rwpi : Flag<["-"], "fno-rwpi">, Group<f_Group>;
 def fplugin_EQ : Joined<["-"], "fplugin=">, Group<f_Group>, Flags<[DriverOption]>, MetaVarName<"<dsopath>">,
   HelpText<"Load the named plugin (dynamic shared object)">;
+def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">,
+  Group<f_Group>, Flags<[CC1Option]>, MetaVarName<"<dsopath>">,
+  HelpText<"Load pass plugin from a dynamic shared object file (only with new pass manager).">;
 def fpreserve_as_comments : Flag<["-"], "fpreserve-as-comments">, Group<f_Group>;
 def fno_preserve_as_comments : Flag<["-"], "fno-preserve-as-comments">, Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Do not preserve comments in inline assembly">;
index 76f5f915e61e19d1301e1c004dc3222fdd852992..cc704c77986b9b0a92c2fdddec20ef4876beea46 100644 (file)
@@ -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);
index c02446bcd5e515af4cf6f3c91c31823cacc0d9d3..f125bce01ddedfda6619cf34b8e859e4fc91f88e 100644 (file)
@@ -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())
index 05cce3c909412e1241bf0766afd24c48b5f4857b..b991a89e41b3da46e8c869a0968240224302988f 100644 (file)
@@ -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;
 }