diff --git a/llvm/include/llvm/Transforms/Utils/SizeOpts.h b/llvm/include/llvm/Transforms/Utils/SizeOpts.h index 6e6ec6e109cc3..df3fe04048c97 100644 --- a/llvm/include/llvm/Transforms/Utils/SizeOpts.h +++ b/llvm/include/llvm/Transforms/Utils/SizeOpts.h @@ -62,9 +62,13 @@ bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, // Even if the working set size isn't large, size-optimize cold code. return AdapterT::isFunctionColdInCallGraph(F, PSI, *BFI); } - return !AdapterT::isFunctionHotInCallGraphNthPercentile( - PSI->hasSampleProfile() ? PgsoCutoffSampleProf : PgsoCutoffInstrProf, - F, PSI, *BFI); + if (PSI->hasSampleProfile()) + // The "isCold" check seems to work better for Sample PGO as it could have + // many profile-unannotated functions. + return AdapterT::isFunctionColdInCallGraphNthPercentile( + PgsoCutoffSampleProf, F, PSI, *BFI); + return !AdapterT::isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf, + F, PSI, *BFI); } template @@ -88,9 +92,13 @@ bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryIn // Even if the working set size isn't large, size-optimize cold code. return AdapterT::isColdBlock(BBOrBlockFreq, PSI, BFI); } - return !AdapterT::isHotBlockNthPercentile( - PSI->hasSampleProfile() ? PgsoCutoffSampleProf : PgsoCutoffInstrProf, - BBOrBlockFreq, PSI, BFI); + if (PSI->hasSampleProfile()) + // The "isCold" check seems to work better for Sample PGO as it could have + // many profile-unannotated functions. + return AdapterT::isColdBlockNthPercentile(PgsoCutoffSampleProf, + BBOrBlockFreq, PSI, BFI); + return !AdapterT::isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, + PSI, BFI); } /// Returns true if function \p F is suggested to be size-optimized based on the diff --git a/llvm/lib/CodeGen/MachineSizeOpts.cpp b/llvm/lib/CodeGen/MachineSizeOpts.cpp index 29aabf1ad52eb..584d43b420044 100644 --- a/llvm/lib/CodeGen/MachineSizeOpts.cpp +++ b/llvm/lib/CodeGen/MachineSizeOpts.cpp @@ -59,6 +59,22 @@ static bool isHotBlockNthPercentile(int PercentileCutoff, return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count); } +static bool isColdBlockNthPercentile(int PercentileCutoff, + const MachineBasicBlock *MBB, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI) { + auto Count = MBFI->getBlockProfileCount(MBB); + return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count); +} + +static bool isColdBlockNthPercentile(int PercentileCutoff, + BlockFrequency BlockFreq, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI) { + auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency()); + return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count); +} + /// Like ProfileSummaryInfo::isFunctionColdInCallGraph but for /// MachineFunction. bool isFunctionColdInCallGraph( @@ -90,6 +106,19 @@ bool isFunctionHotInCallGraphNthPercentile( return true; return false; } + +bool isFunctionColdInCallGraphNthPercentile( + int PercentileCutoff, const MachineFunction *MF, ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo &MBFI) { + if (auto FunctionCount = MF->getFunction().getEntryCount()) + if (!PSI->isColdCountNthPercentile(PercentileCutoff, + FunctionCount.getCount())) + return false; + for (const auto &MBB : *MF) + if (!isColdBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI)) + return false; + return true; +} } // namespace machine_size_opts_detail struct MachineBasicBlockBFIAdapter { @@ -106,6 +135,12 @@ struct MachineBasicBlockBFIAdapter { return machine_size_opts_detail::isFunctionHotInCallGraphNthPercentile( CutOff, MF, PSI, MBFI); } + static bool isFunctionColdInCallGraphNthPercentile( + int CutOff, const MachineFunction *MF, ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo &MBFI) { + return machine_size_opts_detail::isFunctionColdInCallGraphNthPercentile( + CutOff, MF, PSI, MBFI); + } static bool isColdBlock(const MachineBasicBlock *MBB, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *MBFI) { @@ -130,6 +165,18 @@ struct MachineBasicBlockBFIAdapter { return machine_size_opts_detail::isHotBlockNthPercentile( CutOff, BlockFreq, PSI, MBFI); } + static bool isColdBlockNthPercentile(int CutOff, const MachineBasicBlock *MBB, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI) { + return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, MBB, PSI, + MBFI); + } + static bool isColdBlockNthPercentile(int CutOff, BlockFrequency BlockFreq, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI) { + return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, BlockFreq, + PSI, MBFI); + } }; } // end anonymous namespace diff --git a/llvm/lib/Transforms/Utils/SizeOpts.cpp b/llvm/lib/Transforms/Utils/SizeOpts.cpp index 951de8d5732d0..f299681ef779b 100644 --- a/llvm/lib/Transforms/Utils/SizeOpts.cpp +++ b/llvm/lib/Transforms/Utils/SizeOpts.cpp @@ -70,6 +70,12 @@ struct BasicBlockBFIAdapter { BlockFrequencyInfo &BFI) { return PSI->isFunctionHotInCallGraphNthPercentile(CutOff, F, BFI); } + static bool isFunctionColdInCallGraphNthPercentile(int CutOff, + const Function *F, + ProfileSummaryInfo *PSI, + BlockFrequencyInfo &BFI) { + return PSI->isFunctionColdInCallGraphNthPercentile(CutOff, F, BFI); + } static bool isColdBlock(const BasicBlock *BB, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) { @@ -81,6 +87,11 @@ struct BasicBlockBFIAdapter { BlockFrequencyInfo *BFI) { return PSI->isHotBlockNthPercentile(CutOff, BB, BFI); } + static bool isColdBlockNthPercentile(int CutOff, const BasicBlock *BB, + ProfileSummaryInfo *PSI, + BlockFrequencyInfo *BFI) { + return PSI->isColdBlockNthPercentile(CutOff, BB, BFI); + } }; } // end anonymous namespace