[llvm] a8b8a93 - [llvm-profdata]Fix llvm-profdata crash on compact binary profile

Wenlei He via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 20 16:59:42 PDT 2020


Author: wlei
Date: 2020-09-20T16:58:34-07:00
New Revision: a8b8a9374a3c555ac8528fc37b92935554083b9f

URL: https://github.com/llvm/llvm-project/commit/a8b8a9374a3c555ac8528fc37b92935554083b9f
DIFF: https://github.com/llvm/llvm-project/commit/a8b8a9374a3c555ac8528fc37b92935554083b9f.diff

LOG: [llvm-profdata]Fix llvm-profdata crash on compact binary profile

llvm-profdata `show` and `overlap` will crash in `getFuncName` on compact binary profile. This change fixed this by switching to use `getName`.

 `getFuncName` is misused in llvm-profdata. As showed below, `GUIDToFuncNameMap` is only supported in compilation mode, there is no initialization in llvm-profdata. Compact profile whose MD5 is true would try to query `GUIDToFuncNameMap` then caused the crash. So fix this by switching to `getName`

Reviewed By: MaskRay, wmi, wenlei, weihe, hoy

Differential Revision: https://reviews.llvm.org/D87740

Added: 
    

Modified: 
    llvm/test/tools/llvm-profdata/compact-sample.proftext
    llvm/tools/llvm-profdata/llvm-profdata.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-profdata/compact-sample.proftext b/llvm/test/tools/llvm-profdata/compact-sample.proftext
index 6eac2a071a66..6ebb83949481 100644
--- a/llvm/test/tools/llvm-profdata/compact-sample.proftext
+++ b/llvm/test/tools/llvm-profdata/compact-sample.proftext
@@ -1,8 +1,20 @@
 # Make sure "llvm-profdata show" works for sample profile in binary compact format
 
-# RUN: llvm-profdata show -sample %S/Inputs/compat-sample.profdata  | FileCheck %s 
+# RUN: llvm-profdata show -sample %S/Inputs/compat-sample.profdata | FileCheck %s
 
-# CHECK: Function: 15822663052811949562: 17, 0, 6 sampled lines 
+# CHECK: Function: 15822663052811949562: 17, 0, 6 sampled lines
 # CHECK-NEXT: Samples collected in the function's body {
 # CHECK: Samples collected in inlined callsites {
 # CHECK-NEXT: 1: inlined callee: 6309742469962978389: 17, 0, 1
+
+# RUN: llvm-profdata show -hot-func-list -sample %S/Inputs/compat-sample.profdata | FileCheck %s --check-prefix=HOTFUNC
+# HOTFUNC:      24753993 out of 24754010 profile counts (100.00%) are from hot functions.
+# HOTFUNC:       Total sample (%)       Max sample        Entry sample    Function name
+# HOTFUNC-NEXT:  24753993 (100.00%)     1284671           1284671         16429767378996342100
+
+# Make sure "llvm-profdata overlap" works for sample profile in binary compact format
+# RUN: llvm-profdata overlap -sample %S/Inputs/compat-sample.profdata %S/Inputs/compat-sample.profdata | FileCheck %s --check-prefix=OVERLAP
+
+# OVERLAP:      Program level:
+# OVERLAP-NEXT:   Whole program profile similarity: 100.000%
+# OVERLAP-NEXT:   Whole program sample overlap: 100.000%

diff  --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 670af164290e..49dd1bbdde22 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -1540,14 +1540,14 @@ void SampleOverlapAggregator::computeSampleProfileOverlap(raw_fd_ostream &OS) {
   StringMap<const FunctionSamples *> BaseFuncProf;
   const auto &BaseProfiles = BaseReader->getProfiles();
   for (const auto &BaseFunc : BaseProfiles) {
-    BaseFuncProf.try_emplace(BaseFunc.second.getFuncName(), &(BaseFunc.second));
+    BaseFuncProf.try_emplace(BaseFunc.second.getName(), &(BaseFunc.second));
   }
   ProfOverlap.UnionCount = BaseFuncProf.size();
 
   const auto &TestProfiles = TestReader->getProfiles();
   for (const auto &TestFunc : TestProfiles) {
     SampleOverlapStats FuncOverlap;
-    FuncOverlap.TestName = TestFunc.second.getFuncName();
+    FuncOverlap.TestName = TestFunc.second.getName();
     assert(TestStats.count(FuncOverlap.TestName) &&
            "TestStats should have records for all functions in test profile "
            "except inlinees");
@@ -1574,7 +1574,7 @@ void SampleOverlapAggregator::computeSampleProfileOverlap(raw_fd_ostream &OS) {
 
       // Two functions match with each other. Compute function-level overlap and
       // aggregate them into profile-level overlap.
-      FuncOverlap.BaseName = Match->second->getFuncName();
+      FuncOverlap.BaseName = Match->second->getName();
       assert(BaseStats.count(FuncOverlap.BaseName) &&
              "BaseStats should have records for all functions in base profile "
              "except inlinees");
@@ -1623,10 +1623,10 @@ void SampleOverlapAggregator::computeSampleProfileOverlap(raw_fd_ostream &OS) {
 
   // Traverse through functions in base profile but not in test profile.
   for (const auto &F : BaseFuncProf) {
-    assert(BaseStats.count(F.second->getFuncName()) &&
+    assert(BaseStats.count(F.second->getName()) &&
            "BaseStats should have records for all functions in base profile "
            "except inlinees");
-    const FuncSampleStats &FuncStats = BaseStats[F.second->getFuncName()];
+    const FuncSampleStats &FuncStats = BaseStats[F.second->getName()];
     ++ProfOverlap.BaseUniqueCount;
     ProfOverlap.BaseUniqueSample += FuncStats.SampleSum;
 
@@ -1657,7 +1657,7 @@ void SampleOverlapAggregator::initializeSampleProfileOverlap() {
     FuncSampleStats FuncStats;
     getFuncSampleStats(I.second, FuncStats, BaseHotThreshold);
     ProfOverlap.BaseSample += FuncStats.SampleSum;
-    BaseStats.try_emplace(I.second.getFuncName(), FuncStats);
+    BaseStats.try_emplace(I.second.getName(), FuncStats);
   }
 
   const auto &TestProf = TestReader->getProfiles();
@@ -1666,7 +1666,7 @@ void SampleOverlapAggregator::initializeSampleProfileOverlap() {
     FuncSampleStats FuncStats;
     getFuncSampleStats(I.second, FuncStats, TestHotThreshold);
     ProfOverlap.TestSample += FuncStats.SampleSum;
-    TestStats.try_emplace(I.second.getFuncName(), FuncStats);
+    TestStats.try_emplace(I.second.getName(), FuncStats);
   }
 
   ProfOverlap.BaseName = StringRef(BaseFilename);
@@ -2297,9 +2297,9 @@ showHotFunctionList(const StringMap<sampleprof::FunctionSamples> &Profiles,
         (ProfileTotalSample > 0)
             ? (Func.getTotalSamples() * 100.0) / ProfileTotalSample
             : 0;
-    PrintValues.emplace_back(HotFuncInfo(
-        Func.getFuncName(), Func.getTotalSamples(), TotalSamplePercent,
-        FuncPair.second.second, Func.getEntrySamples()));
+    PrintValues.emplace_back(
+        HotFuncInfo(Func.getName(), Func.getTotalSamples(), TotalSamplePercent,
+                    FuncPair.second.second, Func.getEntrySamples()));
   }
   dumpHotFunctionList(ColumnTitle, ColumnOffset, PrintValues, HotFuncCount,
                       Profiles.size(), HotFuncSample, ProfileTotalSample,


        


More information about the llvm-commits mailing list