From: Justin Bogner Date: Wed, 12 Mar 2014 18:14:32 +0000 (+0000) Subject: CodeGen: Move hot/cold logic out of PGOProfileData X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4142aa7cc7146ae947ddcd0500afc4ea274275d5;p=clang CodeGen: Move hot/cold logic out of PGOProfileData git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203689 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenPGO.cpp b/lib/CodeGen/CodeGenPGO.cpp index ed92de6e6d..3206daa76a 100644 --- a/lib/CodeGen/CodeGenPGO.cpp +++ b/lib/CodeGen/CodeGenPGO.cpp @@ -99,32 +99,6 @@ PGOProfileData::PGOProfileData(CodeGenModule &CGM, std::string Path) MaxFunctionCount = MaxCount; } -/// Return true if a function is hot. If we know nothing about the function, -/// return false. -bool PGOProfileData::isHotFunction(StringRef FuncName) { - llvm::StringMap::const_iterator CountIter = - FunctionCounts.find(FuncName); - // If we know nothing about the function, return false. - if (CountIter == FunctionCounts.end()) - return false; - // FIXME: functions with >= 30% of the maximal function count are - // treated as hot. This number is from preliminary tuning on SPEC. - return CountIter->getValue() >= (uint64_t)(0.3 * (double)MaxFunctionCount); -} - -/// Return true if a function is cold. If we know nothing about the function, -/// return false. -bool PGOProfileData::isColdFunction(StringRef FuncName) { - llvm::StringMap::const_iterator CountIter = - FunctionCounts.find(FuncName); - // If we know nothing about the function, return false. - if (CountIter == FunctionCounts.end()) - return false; - // FIXME: functions with <= 1% of the maximal function count are treated as - // cold. This number is from preliminary tuning on SPEC. - return CountIter->getValue() <= (uint64_t)(0.01 * (double)MaxFunctionCount); -} - bool PGOProfileData::getFunctionCounts(StringRef FuncName, std::vector &Counts) { // Find the relevant section of the pgo-data file. @@ -796,13 +770,7 @@ void CodeGenPGO::assignRegionCounters(const Decl *D, llvm::Function *Fn) { if (PGOData) { loadRegionCounts(PGOData); computeRegionCounts(D); - - // Turn on InlineHint attribute for hot functions. - if (PGOData->isHotFunction(getFuncName())) - Fn->addFnAttr(llvm::Attribute::InlineHint); - // Turn on Cold attribute for cold functions. - else if (PGOData->isColdFunction(getFuncName())) - Fn->addFnAttr(llvm::Attribute::Cold); + applyFunctionAttributes(PGOData, Fn); } } @@ -829,6 +797,23 @@ void CodeGenPGO::computeRegionCounts(const Decl *D) { Walker.VisitBlockDecl(BD); } +void CodeGenPGO::applyFunctionAttributes(PGOProfileData *PGOData, + llvm::Function *Fn) { + if (!haveRegionCounts()) + return; + + uint64_t MaxFunctionCount = PGOData->getMaximumFunctionCount(); + uint64_t FunctionCount = getRegionCount(0); + if (FunctionCount >= (uint64_t)(0.3 * (double)MaxFunctionCount)) + // Turn on InlineHint attribute for hot functions. + // FIXME: 30% is from preliminary tuning on SPEC, it may not be optimal. + Fn->addFnAttr(llvm::Attribute::InlineHint); + else if (FunctionCount <= (uint64_t)(0.01 * (double)MaxFunctionCount)) + // Turn on Cold attribute for cold functions. + // FIXME: 1% is from preliminary tuning on SPEC, it may not be optimal. + Fn->addFnAttr(llvm::Attribute::Cold); +} + void CodeGenPGO::emitCounterVariables() { llvm::LLVMContext &Ctx = CGM.getLLVMContext(); llvm::ArrayType *CounterTy = llvm::ArrayType::get(llvm::Type::getInt64Ty(Ctx), diff --git a/lib/CodeGen/CodeGenPGO.h b/lib/CodeGen/CodeGenPGO.h index 7ee1b27573..51d59cf9a9 100644 --- a/lib/CodeGen/CodeGenPGO.h +++ b/lib/CodeGen/CodeGenPGO.h @@ -43,12 +43,8 @@ public: /// Fill Counts with the profile data for the given function name. Returns /// false on success. bool getFunctionCounts(StringRef FuncName, std::vector &Counts); - /// Return true if a function is hot. If we know nothing about the function, - /// return false. - bool isHotFunction(StringRef FuncName); - /// Return true if a function is cold. If we know nothing about the function, - /// return false. - bool isColdFunction(StringRef FuncName); + /// Return the maximum of all known function counts. + uint64_t getMaximumFunctionCount() { return MaxFunctionCount; } }; /// Per-function PGO state. This class should generally not be used directly, @@ -140,6 +136,7 @@ private: void setFuncName(llvm::Function *Fn); void mapRegionCounters(const Decl *D); void computeRegionCounts(const Decl *D); + void applyFunctionAttributes(PGOProfileData *PGOData, llvm::Function *Fn); void loadRegionCounts(PGOProfileData *PGOData); void emitCounterVariables();