Index: include/llvm/Analysis/ProfileSummaryInfo.h =================================================================== --- include/llvm/Analysis/ProfileSummaryInfo.h +++ include/llvm/Analysis/ProfileSummaryInfo.h @@ -49,12 +49,14 @@ void computeThresholds(); // Count thresholds to answer isHotCount and isColdCount queries. Optional HotCountThreshold, ColdCountThreshold; - bool extractProfTotalWeight(const Instruction *TI, uint64_t &TotalCount); public: ProfileSummaryInfo(Module &M) : M(M) {} ProfileSummaryInfo(ProfileSummaryInfo &&Arg) : M(Arg.M), Summary(std::move(Arg.Summary)) {} + /// Returns the callsite count for \p CS. + static Optional getCallSiteCount(const CallSite &CS, + BlockFrequencyInfo *BFI); /// \brief Returns true if \p F has hot function entry. bool isFunctionEntryHot(const Function *F); /// \brief Returns true if \p F has cold function entry. Index: lib/Analysis/ProfileSummaryInfo.cpp =================================================================== --- lib/Analysis/ProfileSummaryInfo.cpp +++ lib/Analysis/ProfileSummaryInfo.cpp @@ -68,6 +68,21 @@ return true; } +Optional ProfileSummaryInfo::getCallSiteCount( + const CallSite &CS, BlockFrequencyInfo *BFI) { + Instruction *CallInst = CS.getInstruction(); + if (!CallInst) + return None; + // Check if there is a profile metadata on the instruction. If it is present, + // determine hotness solely based on that. + uint64_t TotalCount; + if (CallInst->extractProfTotalWeight(TotalCount)) + return TotalCount; + if (BFI) + return BFI->getBlockProfileCount(CallInst->getParent()); + return None; +} + /// Returns true if the function's entry is hot. If it returns false, it /// either means it is not hot or it is unknown whether it is hot or not (for /// example, no profile data is available). @@ -124,18 +139,7 @@ bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) { auto Count = BFI->getBlockProfileCount(B); - if (Count && isHotCount(*Count)) - return true; - // Use extractProfTotalWeight to get BB count. - // For Sample PGO, BFI may not provide accurate BB count due to errors - // magnified during sample count propagation. This serves as a backup plan - // to ensure all hot BB will not be missed. - // The query currently has false positives as branch instruction cloning does - // not update/scale branch weights. Unlike false negatives, this will not cause - // performance problem. - uint64_t TotalCount; - auto *TI = B->getTerminator(); - return extractProfTotalWeight(TI, TotalCount) && isHotCount(TotalCount); + return Count && isHotCount(*Count); } bool ProfileSummaryInfo::isColdBB(const BasicBlock *B, @@ -144,44 +148,16 @@ return Count && isColdCount(*Count); } -bool ProfileSummaryInfo::extractProfTotalWeight(const Instruction *I, - uint64_t &TotalCount) { - if (!computeSummary()) - return false; - // Use profile weight on metadata only for sample profiling where block counts - // could differ from the count of an instruction within the block. - if (Summary.get()->getKind() != ProfileSummary::PSK_Sample) - return false; - - return (isa(I) || - (isa(I) && !isa(I))) && - I->extractProfTotalWeight(TotalCount); -} - bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI) { - auto *CallInst = CS.getInstruction(); - if (!CS) - return false; - // Check if there is a profile metadata on the instruction. If it is present, - // determine hotness solely based on that. - uint64_t TotalCount; - if (extractProfTotalWeight(CallInst, TotalCount)) - return isHotCount(TotalCount); - return BFI && isHotBB(CallInst->getParent(), BFI); + auto C = getCallSiteCount(CS, BFI); + return C && isHotCount(*C); } bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI) { - auto *CallInst = CS.getInstruction(); - if (!CS) - return false; - // Check if there is a profile metadata on the instruction. If it is present, - // and tells that the callsite is not cold, then return false; - uint64_t TotalCount; - if (extractProfTotalWeight(CallInst, TotalCount) && !isColdCount(TotalCount)) - return false; - return BFI && isColdBB(CallInst->getParent(), BFI); + auto C = getCallSiteCount(CS, BFI); + return C && isColdCount(*C); } INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", Index: unittests/Analysis/ProfileSummaryInfoTest.cpp =================================================================== --- unittests/Analysis/ProfileSummaryInfoTest.cpp +++ unittests/Analysis/ProfileSummaryInfoTest.cpp @@ -162,12 +162,6 @@ EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI)); EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI)); - - // Test that adding an MD_prof metadata with a hot count on CS2 does not - // change itas hotness as it has no effect in instrumented profiling. - MDBuilder MDB(M->getContext()); - CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400})); - EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI)); } TEST_F(ProfileSummaryInfoTest, SampleProf) {