]> granicus.if.org Git - llvm/commitdiff
[PM] port IR based PGO prof-gen pass to new pass manager
authorXinliang David Li <davidxl@google.com>
Fri, 6 May 2016 05:49:19 +0000 (05:49 +0000)
committerXinliang David Li <davidxl@google.com>
Fri, 6 May 2016 05:49:19 +0000 (05:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268710 91177308-0d34-0410-b5e6-96231b3b80d8

20 files changed:
include/llvm/InitializePasses.h
include/llvm/LinkAllPasses.h
include/llvm/Transforms/Instrumentation.h
include/llvm/Transforms/PGOInstrumentation.h [new file with mode: 0644]
lib/Passes/PassBuilder.cpp
lib/Passes/PassRegistry.def
lib/Transforms/IPO/PassManagerBuilder.cpp
lib/Transforms/Instrumentation/Instrumentation.cpp
lib/Transforms/Instrumentation/PGOInstrumentation.cpp
test/Transforms/PGOProfile/branch1.ll
test/Transforms/PGOProfile/branch2.ll
test/Transforms/PGOProfile/comdat_internal.ll
test/Transforms/PGOProfile/criticaledge.ll
test/Transforms/PGOProfile/do-not-instrument.ll
test/Transforms/PGOProfile/indirect_call_profile.ll
test/Transforms/PGOProfile/landingpad.ll
test/Transforms/PGOProfile/loop1.ll
test/Transforms/PGOProfile/loop2.ll
test/Transforms/PGOProfile/single_bb.ll
test/Transforms/PGOProfile/switch.ll

index 7df5c7f429d98e915a35aa27736d4adcf56a70e7..19c7680a9c61b0f1df7793483ebbc5dfc6b3c056 100644 (file)
@@ -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 &);
index 0a882c745a08cb1bd4c38786daf611ecb9500e38..114adf0d1070301bbbd50897eccedda34e0a851d 100644 (file)
@@ -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();
index d2742593eed9d4dafeed2ec2096935c870592eee..9b03900dd7b2fcd2e0a56b6f238cbb655bce17a2 100644 (file)
@@ -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 (file)
index 0000000..dca5a08
--- /dev/null
@@ -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<PGOInstrumentationGen> {
+public:
+  PGOInstrumentationGen() {}
+
+  PreservedAnalyses run(Module &M, AnalysisManager<Module> &AM);
+};
+
+} // End llvm namespace
+#endif
index a81634cbb96641988cebbb0c951282b789097ee0..df34ba20de28972cf55f4987df2a5990333b99bf 100644 (file)
@@ -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"
index ca8b1033499cb0d216c1825422a8c9b295fd8be6..9a31be3c8169f8b79d7b2d27a98b8a9124f6d2a5 100644 (file)
@@ -46,6 +46,7 @@ MODULE_PASS("instrprof", InstrProfiling())
 MODULE_PASS("invalidate<all>", 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()))
index 7669895c81d5b210a6cc7dc53b89c8e8d8611935..8ce4f032dac432d0270cb5b568e3ca94c37f0ae5 100644 (file)
@@ -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;
index 2d857c6165207abf21ca1deca1f18fe2f69b60ec..5f47cf831d30b8c14cbf7704909e3fdb3ce7404e 100644 (file)
@@ -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);
index 4c4bc5ecfc954f8def5b07d07ec1b4f31de8102b..500d08d46a01a1834c40cc5b3da89be77cdb42e3 100644 (file)
@@ -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 <algorithm>
 #include <string>
 #include <utility>
 #include <vector>
@@ -110,12 +112,13 @@ static cl::opt<unsigned> 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<Module> &AM) {
+
+  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+  auto LookupBPI = [&FAM](Function &F) -> BranchProbabilityInfo & {
+    return FAM.getResult<BranchProbabilityAnalysis>(F);
+  };
+
+  auto LookupBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
+    return FAM.getResult<BlockFrequencyAnalysis>(F);
+  };
+
+  if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI))
+    return PreservedAnalyses::all();
+
+  return PreservedAnalyses::none();
+}
+
 static void setPGOCountOnFunc(PGOUseFunc &Func,
                               IndexedInstrProfReader *PGOReader) {
   if (Func.readCounters(PGOReader)) {
index 397154e9dd574f82ff602de6accdb4e8813a174a..7d78e8a60d4af6a5e6b6359a2398dc417dad2604 100644 (file)
@@ -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"
index f3f0b961ea8a72650969da7327f9c2a7ce24abc1..a31792a1f41c1df195982bdaf549594e2d2b8c22 100644 (file)
@@ -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"
index 014d8aae9f7de2c79393b495a7f396104a76fbd6..8cc41bf50068f23896712fba6e3dea439bd5a264 100644 (file)
@@ -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"
 
index 5f6ffadc00fa93883c0272806165789fc9d55ca6..de6893b48d8ce0ca1419bf105994a5334f5fed90 100644 (file)
@@ -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"
index a42969ccc5b4a4bb0a05462c5e595b981cdfde75..616e94273756cb2927de264d790fb138dfb19a6f 100644 (file)
@@ -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"
index 9e0c174becd122254389a5ff0acde5842e26454f..0d934ba7584b59e6c302c1ed142b9668edfc2f07 100644 (file)
@@ -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"
 
index f9c0498610f40305a31e92af9b3961d58d70caa4..bd6c063580af6256e0598e5a83ca36f322a126c7 100644 (file)
@@ -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"
index d35e727822eceb3fb059b086c7704f6eafb7fe21..4f2c17c713df6e8d7bc51b51345124d7bb528307 100644 (file)
@@ -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"
index 92293df38fefbc14259df5ca3fab655efde096b2..edf546f685eb5ea68cdd822727dee73e10974b4c 100644 (file)
@@ -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"
index 380b4f27954292adf03b7518b6cae954146d9204..874d8e4d22d69299078acd7108bfe48cae19f89f 100644 (file)
@@ -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"
 
index e65f003acbc6e2d1479ec54622bd8f1a73ac18df..447d274fa8043f624869caff48933ebacaf3e474 100644 (file)
@@ -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"