[PATCH] D123323: Adds diagnostics for missing opportunities of indirect call promotion.

Hyoun Kyu Cho via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 7 10:13:16 PDT 2022


netforce created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
netforce requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Some indirect branches that are important for performance might not
be promoted because there are too many targets and/or they don't have enough
counts to be promoted. Let's make them easier to understand by adding
diagnostics for such opportunities.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123323

Files:
  llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
  llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp


Index: llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
+++ llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp
@@ -65,6 +65,13 @@
     ICPCutOff("icp-cutoff", cl::init(0), cl::Hidden, cl::ZeroOrMore,
               cl::desc("Max number of promotions for this compilation"));
 
+extern cl::opt<unsigned> MaxNumPromotions;
+
+static cl::opt<unsigned> ICPMissingPercentThreshold(
+    "icp-missing-percent-threshold", cl::init(50), cl::Hidden, cl::ZeroOrMore,
+    cl::desc("The percentage threshold of unpromoted indirect call counts to "
+             "print missed remark"));
+
 // If ICPCSSkip is non zero, the first ICPCSSkip callsites will be skipped.
 // For debug use only.
 static cl::opt<unsigned>
@@ -354,8 +361,56 @@
     uint64_t TotalCount;
     auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
         CB, NumVals, TotalCount, NumCandidates);
-    if (!NumCandidates ||
-        (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
+    if (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)) {
+      if (!ICallProfDataRef.empty()) {
+        ORE.emit([&]() {
+          return OptimizationRemarkMissed(DEBUG_TYPE, "ColdCall", CB)
+                 << " Not promoted: cold call";
+        });
+      }
+      continue;
+    }
+    uint64_t RemainingCount = TotalCount;
+    for (uint32_t I = 0; I < NumCandidates; I++) {
+      RemainingCount -= ICallProfDataRef[I].Count;
+    }
+    if (ICallProfDataRef.size() > NumCandidates &&
+        RemainingCount * 100 > TotalCount * ICPMissingPercentThreshold) {
+      ORE.emit([&]() {
+        OptimizationRemarkMissed R(DEBUG_TYPE, "NotEnoughCoverage", CB);
+        if (NumCandidates == MaxNumPromotions) {
+          R << "icp-max-prom (="
+            << ore::NV("MaxNumPromotions", MaxNumPromotions)
+            << ") candidates were chosen, ";
+        } else {
+          R << ore::NV("NumCandidates", NumCandidates)
+            << " candidates were chosen and remaining are not profitable, ";
+        }
+        R << "but hasn't reached desired coverage "
+          << ore::NV("ICPMissingPercentThreshold", ICPMissingPercentThreshold)
+          << "\% for a hot indirect call, and "
+          << ore::NV("Candidates", ICallProfDataRef.size() - NumCandidates)
+          << " candidates left with remaining count "
+          << ore::NV("RemainingCount", RemainingCount) << " out of total count "
+          << ore::NV("TotalCount", TotalCount) << " (";
+        for (uint32_t I = NumCandidates; I < ICallProfDataRef.size(); I++) {
+          if (I != NumCandidates) {
+            R << ", ";
+          }
+          uint64_t Target = ICallProfDataRef[I].Value;
+          StringRef Name = Symtab->getFuncName(Target);
+          if (Name.empty()) {
+            R << ore::NV("Target md4sum", Target);
+          } else {
+            R << ore::NV("TargetName", Name);
+          }
+          R << ": " << ore::NV("Target count", ICallProfDataRef[I].Count);
+        }
+        R << ")";
+        return R;
+      });
+    }
+    if (!NumCandidates)
       continue;
     auto PromotionCandidates = getPromotionCandidatesForCallSite(
         *CB, ICallProfDataRef, TotalCount, NumCandidates);
Index: llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
+++ llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
@@ -40,7 +40,7 @@
 
 // Set the maximum number of targets to promote for a single indirect-call
 // callsite.
-static cl::opt<unsigned>
+cl::opt<unsigned>
     MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
                      cl::desc("Max number of promotions for a single indirect "
                               "call callsite"));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123323.421262.patch
Type: text/x-patch
Size: 3983 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220407/51fefa17/attachment.bin>


More information about the llvm-commits mailing list