diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1214,6 +1214,9 @@ PB.registerLoopAnalyses(LAM); PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + if (TM) + TM->registerPassBuilderCallbacks(PB, CodeGenOpts.DebugPassManager); + ModulePassManager MPM(CodeGenOpts.DebugPassManager); if (!CodeGenOpts.DisableLLVMPasses) { diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -34,6 +34,7 @@ class MCSubtargetInfo; class MCSymbol; class raw_pwrite_stream; +class PassBuilder; class PassManagerBuilder; struct PerFunctionMIParsingState; class SMDiagnostic; @@ -294,6 +295,11 @@ /// 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 diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.h b/llvm/lib/Target/Hexagon/HexagonTargetMachine.h --- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.h +++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.h @@ -37,6 +37,8 @@ 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/lib/Target/Hexagon/HexagonTargetMachine.cpp b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp --- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp +++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp @@ -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::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)); diff --git a/llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll b/llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll @@ -0,0 +1,27 @@ +; RUN: opt -mtriple=hexagon -disable-verify -debug-pass-manager \ +; RUN: -disable-output -passes='default' -S %s 2>&1 \ +; RUN: | FileCheck %s --check-prefix=NPM +; RUN: opt -mtriple=hexagon -disable-verify -debug-pass-manager \ +; RUN: -disable-output -passes='default' -S %s 2>&1 \ +; RUN: | FileCheck %s --check-prefix=NPM +; RUN: opt -mtriple=hexagon -disable-verify -debug-pass-manager \ +; RUN: -disable-output -passes='default' -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 +} diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -375,6 +375,9 @@ 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());