diff --git a/include/llvm/Transforms/PGOInstrumentation.h b/include/llvm/Transforms/PGOInstrumentation.h index 42f1e175b5b..9ab82ea0cb6 100644 --- a/include/llvm/Transforms/PGOInstrumentation.h +++ b/include/llvm/Transforms/PGOInstrumentation.h @@ -14,9 +14,7 @@ #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 { @@ -27,5 +25,15 @@ public: PreservedAnalyses run(Module &M, AnalysisManager &AM); }; +/// The profile annotation (profile-instr-use) pass for IR based PGO. +class PGOInstrumentationUse : public PassInfoMixin { +public: + PreservedAnalyses run(Module &M, AnalysisManager &AM); + PGOInstrumentationUse(std::string Filename = ""); + +private: + std::string ProfileFileName; +}; + } // End llvm namespace #endif diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index d79a39cf952..a9c896029f6 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -48,6 +48,7 @@ MODULE_PASS("invalidate", InvalidateAllAnalysesPass()) MODULE_PASS("ipsccp", IPSCCPPass()) MODULE_PASS("no-op-module", NoOpModulePass()) MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen()) +MODULE_PASS("pgo-instr-use", PGOInstrumentationUse()) MODULE_PASS("print", PrintModulePass(dbgs())) MODULE_PASS("print-callgraph", CallGraphPrinterPass(dbgs())) MODULE_PASS("print-lcg", LazyCallGraphPrinterPass(dbgs())) diff --git a/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index ef496840fab..e5e798b3fd0 100644 --- a/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -151,13 +151,9 @@ public: } private: - bool annotateAllFunctions( - Module &M, function_ref LookupBPI, - function_ref LookupBFI); std::string ProfileFileName; - std::unique_ptr PGOReader; - bool runOnModule(Module &M) override; + bool runOnModule(Module &M) override; void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired(); } @@ -835,8 +831,9 @@ static void setPGOCountOnFunc(PGOUseFunc &Func, } } -bool PGOInstrumentationUseLegacyPass::annotateAllFunctions( - Module &M, function_ref LookupBPI, +static bool annotateAllFunctions( + Module &M, StringRef ProfileFileName, + function_ref LookupBPI, function_ref LookupBFI) { DEBUG(dbgs() << "Read in profile counters: "); auto &Ctx = M.getContext(); @@ -848,10 +845,11 @@ bool PGOInstrumentationUseLegacyPass::annotateAllFunctions( return false; } - PGOReader = std::move(ReaderOrErr.get()); + std::unique_ptr PGOReader = + std::move(ReaderOrErr.get()); if (!PGOReader) { Ctx.diagnose(DiagnosticInfoPGOProfile(ProfileFileName.data(), - "Cannot get PGOReader")); + StringRef("Cannot get PGOReader"))); return false; } // TODO: might need to change the warning once the clang option is finalized. @@ -891,6 +889,30 @@ bool PGOInstrumentationUseLegacyPass::annotateAllFunctions( return true; } +PGOInstrumentationUse::PGOInstrumentationUse(std::string Filename) + : ProfileFileName(Filename) { + if (!PGOTestProfileFile.empty()) + ProfileFileName = PGOTestProfileFile; +} + +PreservedAnalyses PGOInstrumentationUse::run(Module &M, + AnalysisManager &AM) { + + auto &FAM = AM.getResult(M).getManager(); + auto LookupBPI = [&FAM](Function &F) { + return &FAM.getResult(F); + }; + + auto LookupBFI = [&FAM](Function &F) { + return &FAM.getResult(F); + }; + + if (!annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI)) + return PreservedAnalyses::all(); + + return PreservedAnalyses::none(); +} + bool PGOInstrumentationUseLegacyPass::runOnModule(Module &M) { if (skipModule(M)) return false; @@ -902,5 +924,5 @@ bool PGOInstrumentationUseLegacyPass::runOnModule(Module &M) { return &this->getAnalysis(F).getBFI(); }; - return annotateAllFunctions(M, LookupBPI, LookupBFI); + return annotateAllFunctions(M, ProfileFileName, LookupBPI, LookupBFI); } diff --git a/test/Transforms/PGOProfile/branch1.ll b/test/Transforms/PGOProfile/branch1.ll index 7d78e8a60d4..317fae2d88d 100644 --- a/test/Transforms/PGOProfile/branch1.ll +++ b/test/Transforms/PGOProfile/branch1.ll @@ -1,11 +1,16 @@ ; 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 +; New PM ; 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 + +; New PM +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu" ; GEN-DARWIN-LINKONCE: target triple = "x86_64-apple-darwin" diff --git a/test/Transforms/PGOProfile/branch2.ll b/test/Transforms/PGOProfile/branch2.ll index a31792a1f41..f8df54b94d4 100644 --- a/test/Transforms/PGOProfile/branch2.ll +++ b/test/Transforms/PGOProfile/branch2.ll @@ -2,6 +2,7 @@ ; 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 +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/criticaledge.ll b/test/Transforms/PGOProfile/criticaledge.ll index de6893b48d8..4b2ea6becfe 100644 --- a/test/Transforms/PGOProfile/criticaledge.ll +++ b/test/Transforms/PGOProfile/criticaledge.ll @@ -2,6 +2,7 @@ ; 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 +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/diag_FE_profile.ll b/test/Transforms/PGOProfile/diag_FE_profile.ll index 30abe6d3519..cd33954284f 100644 --- a/test/Transforms/PGOProfile/diag_FE_profile.ll +++ b/test/Transforms/PGOProfile/diag_FE_profile.ll @@ -1,5 +1,6 @@ ; RUN: llvm-profdata merge %S/Inputs/diag_FE.proftext -o %t.profdata ; RUN: not opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s +; RUN: not opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s ; CHECK: Not an IR level instrumentation profile diff --git a/test/Transforms/PGOProfile/diag_mismatch.ll b/test/Transforms/PGOProfile/diag_mismatch.ll index a2d0b20620f..e2b7f8cdcc5 100644 --- a/test/Transforms/PGOProfile/diag_mismatch.ll +++ b/test/Transforms/PGOProfile/diag_mismatch.ll @@ -1,5 +1,6 @@ ; RUN: llvm-profdata merge %S/Inputs/diag.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s +; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s ; CHECK: Function control flow change detected (hash mismatch) foo diff --git a/test/Transforms/PGOProfile/diag_no_funcprofdata.ll b/test/Transforms/PGOProfile/diag_no_funcprofdata.ll index 2e5ec0444b4..d49751a62b9 100644 --- a/test/Transforms/PGOProfile/diag_no_funcprofdata.ll +++ b/test/Transforms/PGOProfile/diag_no_funcprofdata.ll @@ -1,5 +1,6 @@ ; RUN: llvm-profdata merge %S/Inputs/diag.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s +; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s ; CHECK: No profile data available for function bar diff --git a/test/Transforms/PGOProfile/diag_no_profile.ll b/test/Transforms/PGOProfile/diag_no_profile.ll index ce7b59b8f69..222d9bd0986 100644 --- a/test/Transforms/PGOProfile/diag_no_profile.ll +++ b/test/Transforms/PGOProfile/diag_no_profile.ll @@ -1,4 +1,5 @@ ; RUN: not opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 +; RUN: not opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S 2>&1 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/indirect_call_annotation.ll b/test/Transforms/PGOProfile/indirect_call_annotation.ll index e2479d72bf6..6f72a998784 100644 --- a/test/Transforms/PGOProfile/indirect_call_annotation.ll +++ b/test/Transforms/PGOProfile/indirect_call_annotation.ll @@ -1,5 +1,6 @@ ; RUN: llvm-profdata merge %S/Inputs/indirect_call.proftext -o %t.profdata ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=VP-ANNOTATION +; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=VP-ANNOTATION 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 bd6c063580a..9452cd41b00 100644 --- a/test/Transforms/PGOProfile/landingpad.ll +++ b/test/Transforms/PGOProfile/landingpad.ll @@ -2,6 +2,7 @@ ; 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 +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/loop1.ll b/test/Transforms/PGOProfile/loop1.ll index 4f2c17c713d..5d3be183694 100644 --- a/test/Transforms/PGOProfile/loop1.ll +++ b/test/Transforms/PGOProfile/loop1.ll @@ -2,6 +2,7 @@ ; 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 +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/loop2.ll b/test/Transforms/PGOProfile/loop2.ll index edf546f685e..1fad53a90dc 100644 --- a/test/Transforms/PGOProfile/loop2.ll +++ b/test/Transforms/PGOProfile/loop2.ll @@ -2,6 +2,7 @@ ; 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 +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/PGOProfile/switch.ll b/test/Transforms/PGOProfile/switch.ll index 447d274fa80..e590e217013 100644 --- a/test/Transforms/PGOProfile/switch.ll +++ b/test/Transforms/PGOProfile/switch.ll @@ -2,6 +2,7 @@ ; 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 +; RUN: opt < %s -passes=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" target triple = "x86_64-unknown-linux-gnu"