Index: include/llvm/Analysis/ProfileSummaryInfo.h =================================================================== --- include/llvm/Analysis/ProfileSummaryInfo.h +++ include/llvm/Analysis/ProfileSummaryInfo.h @@ -27,6 +27,8 @@ #include namespace llvm { +class BasicBlock; +class BlockFrequencyInfo; class ProfileSummary; /// \brief Analysis providing profile information. /// @@ -59,6 +61,8 @@ bool isHotCount(uint64_t C); /// \brief Returns true if count \p C is considered cold. bool isColdCount(uint64_t C); + /// \brief Returns true if BasicBlock \p B is considered hot. + bool isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI); }; /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo. Index: lib/Analysis/ProfileSummaryInfo.cpp =================================================================== --- lib/Analysis/ProfileSummaryInfo.cpp +++ lib/Analysis/ProfileSummaryInfo.cpp @@ -12,7 +12,9 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Analysis/BlockFrequencyInfo.h" #include "llvm/Analysis/ProfileSummaryInfo.h" +#include "llvm/IR/BasicBlock.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" #include "llvm/IR/ProfileSummary.h" @@ -121,6 +123,24 @@ return ColdCountThreshold && C <= ColdCountThreshold.getValue(); } +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. Optimizations that clones code + // (e.g. inlining) need to update branch_probability to ensure + // extractProfTotalWeight is up-to-date. Otherwise, cold BB may be mis-labeled + // as hot by this function. + uint64_t TotalCount; + if (B->getTerminator()->extractProfTotalWeight(TotalCount) && + isHotCount(TotalCount)) + return true; + return false; +} + INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", "Profile summary info", false, true)