[PATCH] D159221: tmp

Lei Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 30 13:16:37 PDT 2023


wlei created this revision.
Herald added subscribers: hoy, ormris, wenlei, hiraditya.
Herald added a project: All.
wlei requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159221

Files:
  llvm/lib/Transforms/IPO/SampleProfile.cpp


Index: llvm/lib/Transforms/IPO/SampleProfile.cpp
===================================================================
--- llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -2111,20 +2111,30 @@
 
 void SampleProfileMatcher::findIRAnchors(
     const Function &F, std::map<LineLocation, StringRef> &IRAnchors) {
-  auto FindTopLevelFrame = [](const DILocation *DIL) {
-    const DILocation *PrevDIL = DIL;
-    for (; DIL->getInlinedAt(); DIL = DIL->getInlinedAt())
-      PrevDIL = DIL;
-
-    LineLocation Callsite = FunctionSamples::getCallSiteIdentifier(DIL);
-    StringRef CalleeName = PrevDIL->getSubprogramLinkageName();
-    return std::make_pair(Callsite, CalleeName);
+  // Flatten inlined IR for the matching. Recover the original callsite
+  // and call target by analyzing the inline frames from the debug info.
+  // CalleeDIL is null for non-inlined code.
+  auto FindTopTwoFrames = [](const DILocation *CallerDIL) {
+    const DILocation *CalleeDIL = nullptr;
+    for (; CallerDIL->getInlinedAt(); CallerDIL = CallerDIL->getInlinedAt())
+      CalleeDIL = CallerDIL;
+    return std::make_pair(CallerDIL, CalleeDIL);
   };
 
-  auto FindCalleeName = [](const CallBase *CB) {
-    StringRef CalleeName = UnknownIndirectCallee;
-    if (Function *Callee = CB->getCalledFunction())
-      CalleeName = FunctionSamples::getCanonicalFnName(Callee->getName());
+  auto FindCalleeName = [](const Instruction &I, const DILocation *CalleeDIL) {
+    // Use empty StringRef for non-call non-inlined instruction.
+    StringRef CalleeName;
+    if (CalleeDIL) {
+      // Use the call target of the top-level frame for inlined code.
+      CalleeName = CalleeDIL->getSubprogramLinkageName();
+    } else if (const auto *CB = dyn_cast<CallBase>(&I)) {
+      if (isa<IntrinsicInst>(&I))
+        return CalleeName;
+      if (Function *Callee = CB->getCalledFunction())
+        CalleeName = FunctionSamples::getCanonicalFnName(Callee->getName());
+      else
+        CalleeName = UnknownIndirectCallee;
+    }
     return CalleeName;
   };
 
@@ -2137,17 +2147,11 @@
 
       if (FunctionSamples::ProfileIsProbeBased) {
         if (auto Probe = extractProbe(I)) {
-          // Flatten inlined IR for the matching. Recover the original callsite
-          // and call target by analyzing the inline frames from the debug info.
-          if (DIL->getInlinedAt()) {
-            IRAnchors.emplace(FindTopLevelFrame(DIL));
-          } else {
-            // Use empty StringRef for basic block probe.
-            StringRef CalleeName;
-            if (const auto *CB = dyn_cast<CallBase>(&I))
-              CalleeName = FindCalleeName(CB);
-            IRAnchors.emplace(LineLocation(Probe->Id, 0), CalleeName);
-          }
+          const auto &[CallerDIL, CalleeDIL] = FindTopTwoFrames(DIL);
+          LineLocation Callsite =
+              CalleeDIL ? FunctionSamples::getCallSiteIdentifier(CallerDIL)
+                        : LineLocation(Probe->Id, 0);
+          IRAnchors.emplace(Callsite, FindCalleeName(I, CalleeDIL));
         }
       } else {
         // TODO: For line-number based profile(AutoFDO), currently only support
@@ -2155,14 +2159,14 @@
         // instructions to extract the line locations for profile matching.
         if (!isa<CallBase>(&I) || isa<IntrinsicInst>(&I))
           continue;
-
-        if (DIL->getInlinedAt()) {
-          IRAnchors.emplace(FindTopLevelFrame(DIL));
-        } else {
-          LineLocation IRCallsite = FunctionSamples::getCallSiteIdentifier(DIL);
-          StringRef CalleeName = FindCalleeName(dyn_cast<CallBase>(&I));
-          IRAnchors.emplace(IRCallsite, CalleeName);
-        }
+        const auto &[CallerDIL, CalleeDIL] = FindTopTwoFrames(DIL);
+        LineLocation Callsite =
+            FunctionSamples::getCallSiteIdentifier(CallerDIL);
+        StringRef CalleeName = FindCalleeName(I, CalleeDIL);
+        assert(
+            !CalleeName.empty() &&
+            "Callee name should not be empty for line-number based profile.");
+        IRAnchors.emplace(Callsite, CalleeName);
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159221.554816.patch
Type: text/x-patch
Size: 4132 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230830/283f243d/attachment.bin>


More information about the llvm-commits mailing list