From: Xinliang David Li Date: Fri, 6 May 2016 05:49:19 +0000 (+0000) Subject: [PM] port IR based PGO prof-gen pass to new pass manager X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ea2fb7d1dd29bb7c569c1f6b1f0317b24e58fc47;p=llvm [PM] port IR based PGO prof-gen pass to new pass manager git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268710 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index 7df5c7f429d..19c7680a9c6 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -123,7 +123,7 @@ void initializeEdgeBundlesPass(PassRegistry&); void initializeExpandPostRAPass(PassRegistry&); void initializeAAResultsWrapperPassPass(PassRegistry &); void initializeGCOVProfilerPass(PassRegistry&); -void initializePGOInstrumentationGenPass(PassRegistry&); +void initializePGOInstrumentationGenLegacyPassPass(PassRegistry&); void initializePGOInstrumentationUsePass(PassRegistry&); void initializePGOIndirectCallPromotionPass(PassRegistry&); void initializeInstrProfilingLegacyPassPass(PassRegistry &); diff --git a/include/llvm/LinkAllPasses.h b/include/llvm/LinkAllPasses.h index 0a882c745a0..114adf0d107 100644 --- a/include/llvm/LinkAllPasses.h +++ b/include/llvm/LinkAllPasses.h @@ -89,7 +89,7 @@ namespace { (void) llvm::createDomOnlyViewerPass(); (void) llvm::createDomViewerPass(); (void) llvm::createGCOVProfilerPass(); - (void) llvm::createPGOInstrumentationGenPass(); + (void) llvm::createPGOInstrumentationGenLegacyPass(); (void) llvm::createPGOInstrumentationUsePass(); (void) llvm::createPGOIndirectCallPromotionPass(); (void) llvm::createInstrProfilingLegacyPass(); diff --git a/include/llvm/Transforms/Instrumentation.h b/include/llvm/Transforms/Instrumentation.h index d2742593eed..9b03900dd7b 100644 --- a/include/llvm/Transforms/Instrumentation.h +++ b/include/llvm/Transforms/Instrumentation.h @@ -80,7 +80,7 @@ ModulePass *createGCOVProfilerPass(const GCOVOptions &Options = GCOVOptions::getDefault()); // PGO Instrumention -ModulePass *createPGOInstrumentationGenPass(); +ModulePass *createPGOInstrumentationGenLegacyPass(); ModulePass * createPGOInstrumentationUsePass(StringRef Filename = StringRef("")); ModulePass *createPGOIndirectCallPromotionPass(bool InLTO = false); diff --git a/include/llvm/Transforms/PGOInstrumentation.h b/include/llvm/Transforms/PGOInstrumentation.h new file mode 100644 index 00000000000..dca5a089414 --- /dev/null +++ b/include/llvm/Transforms/PGOInstrumentation.h @@ -0,0 +1,33 @@ +//===- Transforms/PGOInstrumentation.h - PGO gen/use passes ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// \file +/// This file provides the interface for IR based instrumentation passes ( +/// (profile-gen, and profile-use). +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_PGOINSTRUMENTATION_H +#define LLVM_TRANSFORMS_PGOINSTRUMENTATION_H + +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/PassManager.h" +#include "llvm/ProfileData/InstrProf.h" +#include "llvm/Transforms/Instrumentation.h" + +namespace llvm { + +/// The instrumentation (profile-instr-gen) pass for IR based PGO. +class PGOInstrumentationGen : public PassInfoMixin { +public: + PGOInstrumentationGen() {} + + PreservedAnalyses run(Module &M, AnalysisManager &AM); +}; + +} // End llvm namespace +#endif diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index a81634cbb96..df34ba20de2 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -60,6 +60,7 @@ #include "llvm/Transforms/IPO/StripDeadPrototypes.h" #include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/InstrProfiling.h" +#include "llvm/Transforms/PGOInstrumentation.h" #include "llvm/Transforms/Scalar/ADCE.h" #include "llvm/Transforms/Scalar/DCE.h" #include "llvm/Transforms/Scalar/EarlyCSE.h" diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index ca8b1033499..9a31be3c816 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -46,6 +46,7 @@ MODULE_PASS("instrprof", InstrProfiling()) MODULE_PASS("invalidate", InvalidateAllAnalysesPass()) MODULE_PASS("ipsccp", IPSCCPPass()) MODULE_PASS("no-op-module", NoOpModulePass()) +MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen()) MODULE_PASS("print", PrintModulePass(dbgs())) MODULE_PASS("print-callgraph", CallGraphPrinterPass(dbgs())) MODULE_PASS("print-lcg", LazyCallGraphPrinterPass(dbgs())) diff --git a/lib/Transforms/IPO/PassManagerBuilder.cpp b/lib/Transforms/IPO/PassManagerBuilder.cpp index 7669895c81d..8ce4f032dac 100644 --- a/lib/Transforms/IPO/PassManagerBuilder.cpp +++ b/lib/Transforms/IPO/PassManagerBuilder.cpp @@ -212,7 +212,7 @@ void PassManagerBuilder::populateFunctionPassManager( // Do PGO instrumentation generation or use pass as the option specified. void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) { if (!PGOInstrGen.empty()) { - MPM.add(createPGOInstrumentationGenPass()); + MPM.add(createPGOInstrumentationGenLegacyPass()); // Add the profile lowering pass. InstrProfOptions Options; Options.InstrProfileOutput = PGOInstrGen; diff --git a/lib/Transforms/Instrumentation/Instrumentation.cpp b/lib/Transforms/Instrumentation/Instrumentation.cpp index 2d857c61652..5f47cf831d3 100644 --- a/lib/Transforms/Instrumentation/Instrumentation.cpp +++ b/lib/Transforms/Instrumentation/Instrumentation.cpp @@ -60,7 +60,7 @@ void llvm::initializeInstrumentation(PassRegistry &Registry) { initializeAddressSanitizerModulePass(Registry); initializeBoundsCheckingPass(Registry); initializeGCOVProfilerPass(Registry); - initializePGOInstrumentationGenPass(Registry); + initializePGOInstrumentationGenLegacyPassPass(Registry); initializePGOInstrumentationUsePass(Registry); initializePGOIndirectCallPromotionPass(Registry); initializeInstrProfilingLegacyPassPass(Registry); diff --git a/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index 4c4bc5ecfc9..500d08d46a0 100644 --- a/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -48,6 +48,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/PGOInstrumentation.h" #include "CFGMST.h" #include "IndirectCallSiteVisitor.h" #include "llvm/ADT/STLExtras.h" @@ -71,6 +72,7 @@ #include "llvm/Support/JamCRC.h" #include "llvm/Transforms/Instrumentation.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" +#include #include #include #include @@ -110,12 +112,13 @@ static cl::opt MaxNumAnnotations( "call callsite")); namespace { -class PGOInstrumentationGen : public ModulePass { +class PGOInstrumentationGenLegacyPass : public ModulePass { public: static char ID; - PGOInstrumentationGen() : ModulePass(ID) { - initializePGOInstrumentationGenPass(*PassRegistry::getPassRegistry()); + PGOInstrumentationGenLegacyPass() : ModulePass(ID), PGOInstrGen() { + initializePGOInstrumentationGenLegacyPassPass( + *PassRegistry::getPassRegistry()); } const char *getPassName() const override { @@ -123,6 +126,7 @@ public: } private: + PGOInstrumentationGen PGOInstrGen; bool runOnModule(Module &M) override; void getAnalysisUsage(AnalysisUsage &AU) const override { @@ -157,16 +161,16 @@ private: }; } // end anonymous namespace -char PGOInstrumentationGen::ID = 0; -INITIALIZE_PASS_BEGIN(PGOInstrumentationGen, "pgo-instr-gen", +char PGOInstrumentationGenLegacyPass::ID = 0; +INITIALIZE_PASS_BEGIN(PGOInstrumentationGenLegacyPass, "pgo-instr-gen", "PGO instrumentation.", false, false) INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) -INITIALIZE_PASS_END(PGOInstrumentationGen, "pgo-instr-gen", +INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen", "PGO instrumentation.", false, false) -ModulePass *llvm::createPGOInstrumentationGenPass() { - return new PGOInstrumentationGen(); +ModulePass *llvm::createPGOInstrumentationGenLegacyPass() { + return new PGOInstrumentationGenLegacyPass(); } char PGOInstrumentationUse::ID = 0; @@ -788,7 +792,7 @@ static bool InstrumentAllFunctions( return true; } -bool PGOInstrumentationGen::runOnModule(Module &M) { +bool PGOInstrumentationGenLegacyPass::runOnModule(Module &M) { if (skipModule(M)) return false; @@ -801,6 +805,24 @@ bool PGOInstrumentationGen::runOnModule(Module &M) { return InstrumentAllFunctions(M, LookupBPI, LookupBFI); } +PreservedAnalyses PGOInstrumentationGen::run(Module &M, + AnalysisManager &AM) { + + auto &FAM = AM.getResult(M).getManager(); + auto LookupBPI = [&FAM](Function &F) -> BranchProbabilityInfo & { + return FAM.getResult(F); + }; + + auto LookupBFI = [&FAM](Function &F) -> BlockFrequencyInfo & { + return FAM.getResult(F); + }; + + if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI)) + return PreservedAnalyses::all(); + + return PreservedAnalyses::none(); +} + static void setPGOCountOnFunc(PGOUseFunc &Func, IndexedInstrProfReader *PGOReader) { if (Func.readCounters(PGOReader)) { diff --git a/test/Transforms/PGOProfile/branch1.ll b/test/Transforms/PGOProfile/branch1.ll index 397154e9dd5..7d78e8a60d4 100644 --- a/test/Transforms/PGOProfile/branch1.ll +++ b/test/Transforms/PGOProfile/branch1.ll @@ -1,5 +1,9 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN --check-prefix=GEN-COMDAT ; RUN: opt < %s -mtriple=x86_64-apple-darwin -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN --check-prefix=GEN-DARWIN-LINKONCE + +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN --check-prefix=GEN-COMDAT +; RUN: opt < %s -mtriple=x86_64-apple-darwin -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN --check-prefix=GEN-DARWIN-LINKONCE + ; RUN: llvm-profdata merge %S/Inputs/branch1.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/PGOProfile/branch2.ll b/test/Transforms/PGOProfile/branch2.ll index f3f0b961ea8..a31792a1f41 100644 --- a/test/Transforms/PGOProfile/branch2.ll +++ b/test/Transforms/PGOProfile/branch2.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN ; RUN: llvm-profdata merge %S/Inputs/branch2.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/PGOProfile/comdat_internal.ll b/test/Transforms/PGOProfile/comdat_internal.ll index 014d8aae9f7..8cc41bf5006 100644 --- a/test/Transforms/PGOProfile/comdat_internal.ll +++ b/test/Transforms/PGOProfile/comdat_internal.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -instrprof -S | FileCheck %s +; RUN: opt < %s -passes=pgo-instr-gen,instrprof -S | FileCheck %s target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/criticaledge.ll b/test/Transforms/PGOProfile/criticaledge.ll index 5f6ffadc00f..de6893b48d8 100644 --- a/test/Transforms/PGOProfile/criticaledge.ll +++ b/test/Transforms/PGOProfile/criticaledge.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN ; RUN: llvm-profdata merge %S/Inputs/criticaledge.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/PGOProfile/do-not-instrument.ll b/test/Transforms/PGOProfile/do-not-instrument.ll index a42969ccc5b..616e9427375 100644 --- a/test/Transforms/PGOProfile/do-not-instrument.ll +++ b/test/Transforms/PGOProfile/do-not-instrument.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.12.0" diff --git a/test/Transforms/PGOProfile/indirect_call_profile.ll b/test/Transforms/PGOProfile/indirect_call_profile.ll index 9e0c174becd..0d934ba7584 100644 --- a/test/Transforms/PGOProfile/indirect_call_profile.ll +++ b/test/Transforms/PGOProfile/indirect_call_profile.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/landingpad.ll b/test/Transforms/PGOProfile/landingpad.ll index f9c0498610f..bd6c063580a 100644 --- a/test/Transforms/PGOProfile/landingpad.ll +++ b/test/Transforms/PGOProfile/landingpad.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN ; RUN: llvm-profdata merge %S/Inputs/landingpad.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/PGOProfile/loop1.ll b/test/Transforms/PGOProfile/loop1.ll index d35e727822e..4f2c17c713d 100644 --- a/test/Transforms/PGOProfile/loop1.ll +++ b/test/Transforms/PGOProfile/loop1.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN ; RUN: llvm-profdata merge %S/Inputs/loop1.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/PGOProfile/loop2.ll b/test/Transforms/PGOProfile/loop2.ll index 92293df38fe..edf546f685e 100644 --- a/test/Transforms/PGOProfile/loop2.ll +++ b/test/Transforms/PGOProfile/loop2.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN ; RUN: llvm-profdata merge %S/Inputs/loop2.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/PGOProfile/single_bb.ll b/test/Transforms/PGOProfile/single_bb.ll index 380b4f27954..874d8e4d22d 100644 --- a/test/Transforms/PGOProfile/single_bb.ll +++ b/test/Transforms/PGOProfile/single_bb.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/switch.ll b/test/Transforms/PGOProfile/switch.ll index e65f003acbc..447d274fa80 100644 --- a/test/Transforms/PGOProfile/switch.ll +++ b/test/Transforms/PGOProfile/switch.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt < %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN ; RUN: llvm-profdata merge %S/Inputs/switch.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"