Skip to content

Commit

Permalink
Revert D153927 "Resubmit with fix: [NFC] Refactor MBB hotness/coldnes…
Browse files Browse the repository at this point in the history
…s into templated PSI functions."

This reverts commit 4d8cf2a.

There is a library layering violation. LLVMAnalysis cannot depend on LLVMCodeGen.

```
llvm/include/llvm/Analysis/ProfileSummaryInfo.h:19:10: fatal error: 'llvm/CodeGen/MachineFunction.h' file not found
   19 | #include "llvm/CodeGen/MachineFunction.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
  • Loading branch information
MaskRay committed Jun 28, 2023
1 parent 8d175b3 commit 4bb3d0e
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 217 deletions.
229 changes: 32 additions & 197 deletions llvm/include/llvm/Analysis/ProfileSummaryInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#define LLVM_ANALYSIS_PROFILESUMMARYINFO_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ProfileSummary.h"
#include "llvm/Pass.h"
Expand All @@ -27,7 +23,9 @@

namespace llvm {
class BasicBlock;
class BlockFrequencyInfo;
class CallBase;
class Function;

/// Analysis providing profile information.
///
Expand Down Expand Up @@ -109,77 +107,28 @@ class ProfileSummaryInfo {
bool hasHugeWorkingSetSize() const;
/// Returns true if the working set size of the code is considered large.
bool hasLargeWorkingSetSize() const;
/// Returns true if \p F has hot function entry. 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).
template <typename FuncT> bool isFunctionEntryHot(const FuncT *F) const {
if (!F || !hasProfileSummary())
return false;
std::optional<Function::ProfileCount> FunctionCount = getEntryCount(F);
// FIXME: The heuristic used below for determining hotness is based on
// preliminary SPEC tuning for inliner. This will eventually be a
// convenience method that calls isHotCount.
return FunctionCount && isHotCount(FunctionCount->getCount());
}

/// Returns true if \p F has hot function entry.
bool isFunctionEntryHot(const Function *F) const;
/// Returns true if \p F contains hot code.
template <typename FuncT, typename BFIT>
bool isFunctionHotInCallGraph(const FuncT *F, BFIT &BFI) const {
if (!F || !hasProfileSummary())
return false;
if (auto FunctionCount = getEntryCount(F))
if (isHotCount(FunctionCount->getCount()))
return true;

if (auto TotalCallCount = getTotalCallCount(F)) {
if (isHotCount(*TotalCallCount))
return true;
}

for (const auto &BB : *F)
if (isHotBlock(&BB, &BFI))
return true;
return false;
}
bool isFunctionHotInCallGraph(const Function *F,
BlockFrequencyInfo &BFI) const;
/// Returns true if \p F has cold function entry.
bool isFunctionEntryCold(const Function *F) const;
/// Returns true if \p F contains only cold code.
template <typename FuncT, typename BFIT>
bool isFunctionColdInCallGraph(const FuncT *F, BFIT &BFI) const {
if (!F || !hasProfileSummary())
return false;
if (auto FunctionCount = getEntryCount(F))
if (!isColdCount(FunctionCount->getCount()))
return false;

if (auto TotalCallCount = getTotalCallCount(F)) {
if (!isColdCount(*TotalCallCount))
return false;
}

for (const auto &BB : *F)
if (!isColdBlock(&BB, &BFI))
return false;
return true;
}
bool isFunctionColdInCallGraph(const Function *F,
BlockFrequencyInfo &BFI) const;
/// Returns true if the hotness of \p F is unknown.
bool isFunctionHotnessUnknown(const Function &F) const;
/// Returns true if \p F contains hot code with regard to a given hot
/// percentile cutoff value.
template <typename FuncT, typename BFIT>
bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff,
const FuncT *F, BFIT &BFI) const {
return isFunctionHotOrColdInCallGraphNthPercentile<true, FuncT, BFIT>(
PercentileCutoff, F, BFI);
}
const Function *F,
BlockFrequencyInfo &BFI) const;
/// Returns true if \p F contains cold code with regard to a given cold
/// percentile cutoff value.
template <typename FuncT, typename BFIT>
bool isFunctionColdInCallGraphNthPercentile(int PercentileCutoff,
const FuncT *F, BFIT &BFI) const {
return isFunctionHotOrColdInCallGraphNthPercentile<false, FuncT, BFIT>(
PercentileCutoff, F, BFI);
}
const Function *F,
BlockFrequencyInfo &BFI) const;
/// Returns true if count \p C is considered hot.
bool isHotCount(uint64_t C) const;
/// Returns true if count \p C is considered cold.
Expand All @@ -194,57 +143,22 @@ class ProfileSummaryInfo {
/// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
/// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const;

/// Returns true if BasicBlock \p BB is considered hot.
template <typename BBType, typename BFIT>
bool isHotBlock(const BBType *BB, BFIT *BFI) const {
auto Count = BFI->getBlockProfileCount(BB);
return Count && isHotCount(*Count);
}

bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const;
/// Returns true if BasicBlock \p BB is considered cold.
template <typename BBType, typename BFIT>
bool isColdBlock(const BBType *BB, BFIT *BFI) const {
auto Count = BFI->getBlockProfileCount(BB);
return Count && isColdCount(*Count);
}

template <typename BFIT>
bool isColdBlock(BlockFrequency BlockFreq, const BFIT *BFI) const {
auto Count = BFI->getProfileCountFromFreq(BlockFreq.getFrequency());
return Count && isColdCount(*Count);
}

template <typename BBType, typename BFIT>
bool isHotBlockNthPercentile(int PercentileCutoff, const BBType *BB,
BFIT *BFI) const {
return isHotOrColdBlockNthPercentile<true, BBType, BFIT>(PercentileCutoff,
BB, BFI);
}

template <typename BFIT>
bool isHotBlockNthPercentile(int PercentileCutoff, BlockFrequency BlockFreq,
BFIT *BFI) const {
return isHotOrColdBlockNthPercentile<true, BFIT>(PercentileCutoff,
BlockFreq, BFI);
}

bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const;
/// Returns true if BasicBlock \p BB is considered hot with regard to a given
/// hot percentile cutoff value.
/// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
/// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
bool isHotBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB,
BlockFrequencyInfo *BFI) const;
/// Returns true if BasicBlock \p BB is considered cold with regard to a given
/// cold percentile cutoff value.
/// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
/// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
template <typename BBType, typename BFIT>
bool isColdBlockNthPercentile(int PercentileCutoff, const BBType *BB,
BFIT *BFI) const {
return isHotOrColdBlockNthPercentile<false, BBType, BFIT>(PercentileCutoff,
BB, BFI);
}
template <typename BFIT>
bool isColdBlockNthPercentile(int PercentileCutoff, BlockFrequency BlockFreq,
BFIT *BFI) const {
return isHotOrColdBlockNthPercentile<false, BFIT>(PercentileCutoff,
BlockFreq, BFI);
}
bool isColdBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB,
BlockFrequencyInfo *BFI) const;
/// Returns true if the call site \p CB is considered hot.
bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const;
/// Returns true if call site \p CB is considered cold.
Expand All @@ -264,97 +178,18 @@ class ProfileSummaryInfo {
return ColdCountThreshold.value_or(0);
}

private:
template <typename FuncT>
std::optional<uint64_t> getTotalCallCount(const FuncT *F) const {
return std::nullopt;
}

template <bool isHot, typename FuncT, typename BFIT>
bool isFunctionHotOrColdInCallGraphNthPercentile(int PercentileCutoff,
const FuncT *F,
BFIT &FI) const {
if (!F || !hasProfileSummary())
return false;
if (auto FunctionCount = getEntryCount(F)) {
if (isHot &&
isHotCountNthPercentile(PercentileCutoff, FunctionCount->getCount()))
return true;
if (!isHot && !isColdCountNthPercentile(PercentileCutoff,
FunctionCount->getCount()))
return false;
}
if (auto TotalCallCount = getTotalCallCount(F)) {
if (isHot && isHotCountNthPercentile(PercentileCutoff, *TotalCallCount))
return true;
if (!isHot &&
!isColdCountNthPercentile(PercentileCutoff, *TotalCallCount))
return false;
}
for (const auto &BB : *F) {
if (isHot && isHotBlockNthPercentile(PercentileCutoff, &BB, &FI))
return true;
if (!isHot && !isColdBlockNthPercentile(PercentileCutoff, &BB, &FI))
return false;
}
return !isHot;
}

template <bool isHot>
bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C) const;

template <bool isHot, typename BBType, typename BFIT>
bool isHotOrColdBlockNthPercentile(int PercentileCutoff, const BBType *BB,
BFIT *BFI) const {
auto Count = BFI->getBlockProfileCount(BB);
if (isHot)
return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
else
return Count && isColdCountNthPercentile(PercentileCutoff, *Count);
}

template <bool isHot, typename BFIT>
bool isHotOrColdBlockNthPercentile(int PercentileCutoff,
BlockFrequency BlockFreq,
BFIT *BFI) const {
auto Count = BFI->getProfileCountFromFreq(BlockFreq.getFrequency());
if (isHot)
return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
else
return Count && isColdCountNthPercentile(PercentileCutoff, *Count);
}

template <typename FuncT>
std::optional<Function::ProfileCount> getEntryCount(const FuncT *F) const {
return F->getEntryCount();
}
private:
template <bool isHot>
bool isFunctionHotOrColdInCallGraphNthPercentile(
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const;
template <bool isHot>
bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C) const;
template <bool isHot>
bool isHotOrColdBlockNthPercentile(int PercentileCutoff,
const BasicBlock *BB,
BlockFrequencyInfo *BFI) const;
};

template <>
inline std::optional<uint64_t>
ProfileSummaryInfo::getTotalCallCount<Function>(const Function *F) const {
if (!hasSampleProfile())
return std::nullopt;
uint64_t TotalCallCount = 0;
for (const auto &BB : *F) {
for (const auto &I : BB) {
if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr)) {
TotalCallCount += *CallCount;
}
}
}
}
return TotalCallCount;
}

template <>
inline std::optional<Function::ProfileCount>
ProfileSummaryInfo::getEntryCount<MachineFunction>(
const MachineFunction *F) const {
return F->getFunction().getEntryCount();
}

/// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
class ProfileSummaryInfoWrapperPass : public ImmutablePass {
std::unique_ptr<ProfileSummaryInfo> PSI;
Expand Down
28 changes: 14 additions & 14 deletions llvm/include/llvm/Transforms/Utils/SizeOpts.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
(PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
}

template <typename FuncT, typename BFIT>
template<typename AdapterT, typename FuncT, typename BFIT>
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
BFIT *BFI, PGSOQueryType QueryType) {
assert(F);
Expand All @@ -58,34 +58,34 @@ bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
if (!EnablePGSO)
return false;
if (isPGSOColdCodeOnly(PSI))
return PSI->isFunctionColdInCallGraph(F, *BFI);
return AdapterT::isFunctionColdInCallGraph(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 PSI->isFunctionColdInCallGraphNthPercentile(PgsoCutoffSampleProf, F,
*BFI);
return !PSI->isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf, F,
*BFI);
return AdapterT::isFunctionColdInCallGraphNthPercentile(
PgsoCutoffSampleProf, F, PSI, *BFI);
return !AdapterT::isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf,
F, PSI, *BFI);
}

template <typename BlockTOrBlockFreq, typename BFIT>
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
ProfileSummaryInfo *PSI, BFIT *BFI,
PGSOQueryType QueryType) {
template<typename AdapterT, typename BlockTOrBlockFreq, typename BFIT>
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI,
BFIT *BFI, PGSOQueryType QueryType) {
if (!PSI || !BFI || !PSI->hasProfileSummary())
return false;
if (ForcePGSO)
return true;
if (!EnablePGSO)
return false;
if (isPGSOColdCodeOnly(PSI))
return PSI->isColdBlock(BBOrBlockFreq, BFI);
return AdapterT::isColdBlock(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 PSI->isColdBlockNthPercentile(PgsoCutoffSampleProf, BBOrBlockFreq,
BFI);
return !PSI->isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, BFI);
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
Expand Down

0 comments on commit 4bb3d0e

Please sign in to comment.