r203689 - CodeGen: Move hot/cold logic out of PGOProfileData

Justin Bogner mail at justinbogner.com
Wed Mar 12 11:14:32 PDT 2014


Author: bogner
Date: Wed Mar 12 13:14:32 2014
New Revision: 203689

URL: http://llvm.org/viewvc/llvm-project?rev=203689&view=rev
Log:
CodeGen: Move hot/cold logic out of PGOProfileData

Modified:
    cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
    cfe/trunk/lib/CodeGen/CodeGenPGO.h

Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=203689&r1=203688&r2=203689&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenPGO.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.cpp Wed Mar 12 13:14:32 2014
@@ -99,32 +99,6 @@ PGOProfileData::PGOProfileData(CodeGenMo
   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<uint64_t>::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<uint64_t>::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<uint64_t> &Counts) {
   // Find the relevant section of the pgo-data file.
@@ -796,13 +770,7 @@ void CodeGenPGO::assignRegionCounters(co
   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(con
     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),

Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.h?rev=203689&r1=203688&r2=203689&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenPGO.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.h Wed Mar 12 13:14:32 2014
@@ -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<uint64_t> &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();
 





More information about the cfe-commits mailing list