diff options
Diffstat (limited to 'llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp')
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index be6c8c631001..3d9261eb99ba 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -280,6 +280,7 @@ static cl::opt<unsigned> PGOVerifyBFICutoff( cl::desc("Set the threshold for pgo-verify-bfi -- skip the counts whose " "profile count value is below.")); +namespace llvm { // Command line option to turn on CFG dot dump after profile annotation. // Defined in Analysis/BlockFrequencyInfo.cpp: -pgo-view-counts extern cl::opt<PGOViewCountsType> PGOViewCounts; @@ -287,6 +288,7 @@ extern cl::opt<PGOViewCountsType> PGOViewCounts; // Command line option to specify the name of the function for CFG dump // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name= extern cl::opt<std::string> ViewBlockFreqFuncName; +} // namespace llvm static cl::opt<bool> PGOOldCFGHashing("pgo-instr-old-cfg-hashing", cl::init(false), cl::Hidden, @@ -529,7 +531,7 @@ struct PGOEdge { : SrcBB(Src), DestBB(Dest), Weight(W) {} // Return the information string of an edge. - const std::string infoString() const { + std::string infoString() const { return (Twine(Removed ? "-" : " ") + (InMST ? " " : "*") + (IsCritical ? "c" : " ") + " W=" + Twine(Weight)).str(); } @@ -544,7 +546,7 @@ struct BBInfo { BBInfo(unsigned IX) : Group(this), Index(IX) {} // Return the information string of this object. - const std::string infoString() const { + std::string infoString() const { return (Twine("Index=") + Twine(Index)).str(); } @@ -987,7 +989,7 @@ struct PGOUseEdge : public PGOEdge { } // Return the information string for this object. - const std::string infoString() const { + std::string infoString() const { if (!CountValid) return PGOEdge::infoString(); return (Twine(PGOEdge::infoString()) + " Count=" + Twine(CountValue)) @@ -1018,7 +1020,7 @@ struct UseBBInfo : public BBInfo { } // Return the information string of this object. - const std::string infoString() const { + std::string infoString() const { if (!CountValid) return BBInfo::infoString(); return (Twine(BBInfo::infoString()) + " Count=" + Twine(CountValue)).str(); @@ -1147,7 +1149,7 @@ private: void setEdgeCount(DirectEdges &Edges, uint64_t Value); // Return FuncName string; - const std::string getFuncName() const { return FuncInfo.FuncName; } + std::string getFuncName() const { return FuncInfo.FuncName; } // Set the hot/cold inline hints based on the count values. // FIXME: This function should be removed once the functionality in @@ -1245,6 +1247,28 @@ void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) { llvm_unreachable("Cannot find the unknown count edge"); } +// Emit function metadata indicating PGO profile mismatch. +static void annotateFunctionWithHashMismatch(Function &F, + LLVMContext &ctx) { + const char MetadataName[] = "instr_prof_hash_mismatch"; + SmallVector<Metadata *, 2> Names; + // If this metadata already exists, ignore. + auto *Existing = F.getMetadata(LLVMContext::MD_annotation); + if (Existing) { + MDTuple *Tuple = cast<MDTuple>(Existing); + for (auto &N : Tuple->operands()) { + if (cast<MDString>(N.get())->getString() == MetadataName) + return; + Names.push_back(N.get()); + } + } + + MDBuilder MDB(ctx); + Names.push_back(MDB.createString(MetadataName)); + MDNode *MD = MDTuple::get(ctx, Names); + F.setMetadata(LLVMContext::MD_annotation, MD); +} + // Read the profile from ProfileFileName and assign the value to the // instrumented BB and the edges. This function also updates ProgramMaxCount. // Return true if the profile are successfully read, and false on errors. @@ -1272,6 +1296,8 @@ bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros, (F.hasComdat() || F.getLinkage() == GlobalValue::AvailableExternallyLinkage)); LLVM_DEBUG(dbgs() << "hash mismatch (skip=" << SkipWarning << ")"); + // Emit function metadata indicating PGO profile mismatch. + annotateFunctionWithHashMismatch(F, M->getContext()); } LLVM_DEBUG(dbgs() << " IsCS=" << IsCS << "\n"); @@ -1441,8 +1467,8 @@ void PGOUseFunc::setBranchWeights() { } static bool isIndirectBrTarget(BasicBlock *BB) { - for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { - if (isa<IndirectBrInst>((*PI)->getTerminator())) + for (BasicBlock *Pred : predecessors(BB)) { + if (isa<IndirectBrInst>(Pred->getTerminator())) return true; } return false; @@ -2100,14 +2126,13 @@ template <> struct DOTGraphTraits<PGOUseFunc *> : DefaultDOTGraphTraits { if (!PGOInstrSelect) return Result; - for (auto BI = Node->begin(); BI != Node->end(); ++BI) { - auto *I = &*BI; - if (!isa<SelectInst>(I)) + for (const Instruction &I : *Node) { + if (!isa<SelectInst>(&I)) continue; // Display scaled counts for SELECT instruction: OS << "SELECT : { T = "; uint64_t TC, FC; - bool HasProf = I->extractProfMetadata(TC, FC); + bool HasProf = I.extractProfMetadata(TC, FC); if (!HasProf) OS << "Unknown, F = Unknown }\\l"; else |