[NPM] Add target specific hook to add passes for New Pass Manager
authorArthur Eubanks <aeubanks@google.com>
Wed, 30 Sep 2020 20:23:21 +0000 (13:23 -0700)
committerArthur Eubanks <aeubanks@google.com>
Wed, 30 Sep 2020 20:29:43 +0000 (13:29 -0700)
The patch adds a new TargetMachine member "registerPassBuilderCallbacks" for targets to add passes to the pass pipeline using the New Pass Manager (similar to adjustPassManager for the Legacy Pass Manager).

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D88138

clang/lib/CodeGen/BackendUtil.cpp
llvm/include/llvm/Target/TargetMachine.h
llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
llvm/lib/Target/Hexagon/HexagonTargetMachine.h
llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll [new file with mode: 0644]
llvm/tools/opt/NewPMDriver.cpp

index d77590c..dbd67a6 100644 (file)
@@ -1214,6 +1214,9 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
   PB.registerLoopAnalyses(LAM);
   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
 
+  if (TM)
+    TM->registerPassBuilderCallbacks(PB, CodeGenOpts.DebugPassManager);
+
   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
 
   if (!CodeGenOpts.DisableLLVMPasses) {
index 60d4fb5..4a91528 100644 (file)
@@ -34,6 +34,7 @@ class MCRegisterInfo;
 class MCSubtargetInfo;
 class MCSymbol;
 class raw_pwrite_stream;
+class PassBuilder;
 class PassManagerBuilder;
 struct PerFunctionMIParsingState;
 class SMDiagnostic;
@@ -294,6 +295,11 @@ public:
   /// PassManagerBuilder::addExtension.
   virtual void adjustPassManager(PassManagerBuilder &) {}
 
+  /// Allow the target to modify the pass pipeline with New Pass Manager
+  /// (similar to adjustPassManager for Legacy Pass manager).
+  virtual void registerPassBuilderCallbacks(PassBuilder &,
+                                            bool DebugPassManager) {}
+
   /// Add passes to the specified pass manager to get the specified file
   /// emitted.  Typically this will involve several steps of code generation.
   /// This method should return true if emission of this file type is not
index cb3b6fb..0f15c46 100644 (file)
@@ -22,6 +22,7 @@
 #include "llvm/CodeGen/TargetPassConfig.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Passes/PassBuilder.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
@@ -273,6 +274,18 @@ void HexagonTargetMachine::adjustPassManager(PassManagerBuilder &PMB) {
       });
 }
 
+void HexagonTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB,
+                                                        bool DebugPassManager) {
+  PB.registerOptimizerLastEPCallback(
+      [=](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
+        LoopPassManager LPM(DebugPassManager);
+        FunctionPassManager FPM(DebugPassManager);
+        LPM.addPass(HexagonVectorLoopCarriedReusePass());
+        FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
+        MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
+      });
+}
+
 TargetTransformInfo
 HexagonTargetMachine::getTargetTransformInfo(const Function &F) {
   return TargetTransformInfo(HexagonTTIImpl(this, F));
index 7ee4474..fa17412 100644 (file)
@@ -37,6 +37,8 @@ public:
   static unsigned getModuleMatchQuality(const Module &M);
 
   void adjustPassManager(PassManagerBuilder &PMB) override;
+  void registerPassBuilderCallbacks(PassBuilder &PB,
+                                    bool DebugPassManager) override;
   TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
   TargetTransformInfo getTargetTransformInfo(const Function &F) override;
 
diff --git a/llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll b/llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll
new file mode 100644 (file)
index 0000000..18bca19
--- /dev/null
@@ -0,0 +1,27 @@
+; RUN: opt -mtriple=hexagon -disable-verify -debug-pass-manager \
+; RUN:     -disable-output -passes='default<O1>' -S %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=NPM
+; RUN: opt -mtriple=hexagon -disable-verify -debug-pass-manager \
+; RUN:     -disable-output -passes='default<O2>' -S %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=NPM
+; RUN: opt -mtriple=hexagon -disable-verify -debug-pass-manager \
+; RUN:     -disable-output -passes='default<O3>' -S %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=NPM
+
+; Test TargetMachine::registerPassBuilderCallbacks
+; NPM: Running pass: HexagonVectorLoopCarriedReusePass
+
+declare void @bar() local_unnamed_addr
+
+define void @foo(i32 %n) local_unnamed_addr {
+entry:
+  br label %loop
+loop:
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
+  %iv.next = add i32 %iv, 1
+  tail call void @bar()
+  %cmp = icmp eq i32 %iv, %n
+  br i1 %cmp, label %exit, label %loop
+exit:
+  ret void
+}
index c6c4191..f01d33e 100644 (file)
@@ -375,6 +375,9 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
   PB.registerLoopAnalyses(LAM);
   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
 
+  if (TM)
+    TM->registerPassBuilderCallbacks(PB, DebugPM);
+
   ModulePassManager MPM(DebugPM);
   if (VK > VK_NoVerifier)
     MPM.addPass(VerifierPass());