Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -1217,6 +1217,9 @@ PB.registerLoopAnalyses(LAM); PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + if (TM) + TM->registerPassBuilderCallbacks(PB); + ModulePassManager MPM(CodeGenOpts.DebugPassManager); if (!CodeGenOpts.DisableLLVMPasses) { Index: llvm/include/llvm/Target/TargetMachine.h =================================================================== --- llvm/include/llvm/Target/TargetMachine.h +++ 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; @@ -293,6 +294,10 @@ /// 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 &) {} + /// 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: llvm/lib/Target/Hexagon/HexagonTargetMachine.h =================================================================== --- llvm/lib/Target/Hexagon/HexagonTargetMachine.h +++ llvm/lib/Target/Hexagon/HexagonTargetMachine.h @@ -37,6 +37,7 @@ static unsigned getModuleMatchQuality(const Module &M); void adjustPassManager(PassManagerBuilder &PMB) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; TargetPassConfig *createPassConfig(PassManagerBase &PM) override; TargetTransformInfo getTargetTransformInfo(const Function &F) override; Index: llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp =================================================================== --- llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp +++ 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,17 @@ }); } +void HexagonTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { + PB.registerOptimizerLastEPCallback( + [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) { + LoopPassManager LPM(false); + FunctionPassManager FPM(false); + 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: llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/Hexagon/registerpassbuildercallbacks.ll @@ -0,0 +1,31 @@ +; RUN: opt -passes='default' -mtriple=hexagon -disable-output -disable-verify -debug-pass-manager %s 2>&1 | FileCheck -check-prefix=NPM-O1 %s +; REQUIRES: asserts +; NPM-O1: Running pass: HexagonVectorLoopCarriedReusePass on Loop at depth 1 containing: %for.body
+ +; Function Attrs: noinline nounwind optnone +define dso_local void @foo() { +entry: + %i = alloca i32, align 4 + store i32 0, i32* %i, align 4 + br label %for.cond + +for.cond: ; preds = %for.inc, %entry + %0 = load i32, i32* %i, align 4 + %cmp = icmp slt i32 %0, 1000 + br i1 %cmp, label %for.body, label %for.end + +for.body: ; preds = %for.cond + call void bitcast (void (...)* @bar to void ()*)() + br label %for.inc + +for.inc: ; preds = %for.body + %1 = load i32, i32* %i, align 4 + %inc = add nsw i32 %1, 1 + store i32 %inc, i32* %i, align 4 + br label %for.cond + +for.end: ; preds = %for.cond + ret void +} + +declare dso_local void @bar(...) Index: llvm/tools/opt/NewPMDriver.cpp =================================================================== --- llvm/tools/opt/NewPMDriver.cpp +++ llvm/tools/opt/NewPMDriver.cpp @@ -375,6 +375,9 @@ PB.registerLoopAnalyses(LAM); PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + if (TM) + TM->registerPassBuilderCallbacks(PB); + ModulePassManager MPM(DebugPM); if (VK > VK_NoVerifier) MPM.addPass(VerifierPass());