From eb6eb153d9dc22bf006f260d3975babe075da5f2 Mon Sep 17 00:00:00 2001 From: Diego Novillo Date: Tue, 25 Aug 2015 15:25:11 +0000 Subject: [PATCH] Convert SampleProfile pass into a Module pass. Eventually, we will need sample profiles to be incorporated into the inliner's cost models. To do this, we need the sample profile pass to be a module pass. This patch makes no functional changes beyond the mechanical adjustments needed to run SampleProfile as a module pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245940 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/IPO.h | 7 ++++ include/llvm/Transforms/Scalar.h | 7 ---- lib/Transforms/IPO/CMakeLists.txt | 1 + lib/Transforms/IPO/IPO.cpp | 1 + .../{Scalar => IPO}/SampleProfile.cpp | 41 +++++++++++++------ lib/Transforms/Scalar/CMakeLists.txt | 1 - lib/Transforms/Scalar/Scalar.cpp | 1 - 7 files changed, 38 insertions(+), 21 deletions(-) rename lib/Transforms/{Scalar => IPO}/SampleProfile.cpp (96%) diff --git a/include/llvm/Transforms/IPO.h b/include/llvm/Transforms/IPO.h index 2ea47301bb4..96ddc6eceed 100644 --- a/include/llvm/Transforms/IPO.h +++ b/include/llvm/Transforms/IPO.h @@ -16,6 +16,7 @@ #define LLVM_TRANSFORMS_IPO_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" namespace llvm { @@ -209,6 +210,12 @@ ModulePass *createBarrierNoopPass(); /// to bitsets. ModulePass *createLowerBitSetsPass(); +//===----------------------------------------------------------------------===// +// SampleProfilePass - Loads sample profile data from disk and generates +// IR metadata to reflect the profile. +ModulePass *createSampleProfileLoaderPass(); +ModulePass *createSampleProfileLoaderPass(StringRef Name); + } // End llvm namespace #endif diff --git a/include/llvm/Transforms/Scalar.h b/include/llvm/Transforms/Scalar.h index 1d6d10f42a7..0a37d838a93 100644 --- a/include/llvm/Transforms/Scalar.h +++ b/include/llvm/Transforms/Scalar.h @@ -406,13 +406,6 @@ FunctionPass *createLowerExpectIntrinsicPass(); // FunctionPass *createPartiallyInlineLibCallsPass(); -//===----------------------------------------------------------------------===// -// -// SampleProfilePass - Loads sample profile data from disk and generates -// IR metadata to reflect the profile. -FunctionPass *createSampleProfileLoaderPass(); -FunctionPass *createSampleProfileLoaderPass(StringRef Name); - //===----------------------------------------------------------------------===// // // ScalarizerPass - Converts vector operations into scalar operations diff --git a/lib/Transforms/IPO/CMakeLists.txt b/lib/Transforms/IPO/CMakeLists.txt index 336dac45e13..8c777548137 100644 --- a/lib/Transforms/IPO/CMakeLists.txt +++ b/lib/Transforms/IPO/CMakeLists.txt @@ -20,6 +20,7 @@ add_llvm_library(LLVMipo PartialInlining.cpp PassManagerBuilder.cpp PruneEH.cpp + SampleProfile.cpp StripDeadPrototypes.cpp StripSymbols.cpp diff --git a/lib/Transforms/IPO/IPO.cpp b/lib/Transforms/IPO/IPO.cpp index 50f56b0f2af..6efa1193dc0 100644 --- a/lib/Transforms/IPO/IPO.cpp +++ b/lib/Transforms/IPO/IPO.cpp @@ -47,6 +47,7 @@ void llvm::initializeIPO(PassRegistry &Registry) { initializeStripNonDebugSymbolsPass(Registry); initializeBarrierNoopPass(Registry); initializeEliminateAvailableExternallyPass(Registry); + initializeSampleProfileLoaderPass(Registry); } void LLVMInitializeIPO(LLVMPassRegistryRef R) { diff --git a/lib/Transforms/Scalar/SampleProfile.cpp b/lib/Transforms/IPO/SampleProfile.cpp similarity index 96% rename from lib/Transforms/Scalar/SampleProfile.cpp rename to lib/Transforms/IPO/SampleProfile.cpp index c8dfa54a4aa..f6602fda430 100644 --- a/lib/Transforms/Scalar/SampleProfile.cpp +++ b/lib/Transforms/IPO/SampleProfile.cpp @@ -22,7 +22,6 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Transforms/Scalar.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallSet.h" @@ -45,6 +44,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/IPO.h" #include using namespace llvm; @@ -74,14 +74,15 @@ typedef DenseMap> BlockEdgeMap; /// This pass reads profile data from the file specified by /// -sample-profile-file and annotates every affected function with the /// profile information found in that file. -class SampleProfileLoader : public FunctionPass { +class SampleProfileLoader : public ModulePass { public: // Class identification, replacement for typeinfo static char ID; SampleProfileLoader(StringRef Name = SampleProfileFile) - : FunctionPass(ID), DT(nullptr), PDT(nullptr), LI(nullptr), Ctx(nullptr), - Reader(), Samples(nullptr), Filename(Name), ProfileIsValid(false) { + : ModulePass(ID), DT(nullptr), PDT(nullptr), LI(nullptr), + Ctx(nullptr), Reader(), Samples(nullptr), Filename(Name), + ProfileIsValid(false) { initializeSampleProfileLoaderPass(*PassRegistry::getPassRegistry()); } @@ -91,16 +92,23 @@ public: const char *getPassName() const override { return "Sample profile pass"; } - bool runOnFunction(Function &F) override; + bool runOnModule(Module &M) override; void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesCFG(); + AU.addRequired(); + AU.addPreserved(); + AU.addRequired(); + AU.addPreserved(); + AU.addRequired(); + AU.addPreserved(); } protected: + bool runOnFunction(Function &F); unsigned getFunctionLoc(Function &F); bool emitAnnotations(Function &F); unsigned getInstWeight(Instruction &I); @@ -743,10 +751,11 @@ INITIALIZE_PASS_END(SampleProfileLoader, "sample-profile", "Sample Profile loader", false, false) bool SampleProfileLoader::doInitialization(Module &M) { - auto ReaderOrErr = SampleProfileReader::create(Filename, M.getContext()); + auto& Ctx = M.getContext(); + auto ReaderOrErr = SampleProfileReader::create(Filename, Ctx); if (std::error_code EC = ReaderOrErr.getError()) { std::string Msg = "Could not open profile: " + EC.message(); - M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg)); + Ctx.diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg)); return false; } Reader = std::move(ReaderOrErr.get()); @@ -754,21 +763,29 @@ bool SampleProfileLoader::doInitialization(Module &M) { return true; } -FunctionPass *llvm::createSampleProfileLoaderPass() { +ModulePass *llvm::createSampleProfileLoaderPass() { return new SampleProfileLoader(SampleProfileFile); } -FunctionPass *llvm::createSampleProfileLoaderPass(StringRef Name) { +ModulePass *llvm::createSampleProfileLoaderPass(StringRef Name) { return new SampleProfileLoader(Name); } +bool SampleProfileLoader::runOnModule(Module &M) { + bool retval = false; + for (auto &F : M) + if (!F.isDeclaration()) + retval |= runOnFunction(F); + return retval; +} + bool SampleProfileLoader::runOnFunction(Function &F) { if (!ProfileIsValid) return false; - DT = &getAnalysis().getDomTree(); - PDT = &getAnalysis(); - LI = &getAnalysis().getLoopInfo(); + DT = &getAnalysis(F).getDomTree(); + PDT = &getAnalysis(F); + LI = &getAnalysis(F).getLoopInfo(); Ctx = &F.getParent()->getContext(); Samples = Reader->getSamplesFor(F); if (!Samples->empty()) diff --git a/lib/Transforms/Scalar/CMakeLists.txt b/lib/Transforms/Scalar/CMakeLists.txt index 7ee279a5660..1bbbb0317de 100644 --- a/lib/Transforms/Scalar/CMakeLists.txt +++ b/lib/Transforms/Scalar/CMakeLists.txt @@ -38,7 +38,6 @@ add_llvm_library(LLVMScalarOpts RewriteStatepointsForGC.cpp SCCP.cpp SROA.cpp - SampleProfile.cpp Scalar.cpp ScalarReplAggregates.cpp Scalarizer.cpp diff --git a/lib/Transforms/Scalar/Scalar.cpp b/lib/Transforms/Scalar/Scalar.cpp index a0180f13c1d..54b78e36965 100644 --- a/lib/Transforms/Scalar/Scalar.cpp +++ b/lib/Transforms/Scalar/Scalar.cpp @@ -33,7 +33,6 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) { initializeADCEPass(Registry); initializeBDCEPass(Registry); initializeAlignmentFromAssumptionsPass(Registry); - initializeSampleProfileLoaderPass(Registry); initializeConstantHoistingPass(Registry); initializeConstantPropagationPass(Registry); initializeCorrelatedValuePropagationPass(Registry); -- 2.34.1