[llvm] dddd590 - [CSSPGO][llvm-profgen] Fix getCanonicalFnName usage in llvm-profgen

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 15 21:01:33 PDT 2021


Author: wlei
Date: 2021-03-15T21:00:42-07:00
New Revision: dddd590fd0bcc7e3f0cc9769fe3bad8556aa5c8a

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

LOG: [CSSPGO][llvm-profgen] Fix getCanonicalFnName usage in llvm-profgen

Previously we didn't support to keep the unique linkage name(-funique-internal-linkage-name) in llvm-profgen. As discussed in https://reviews.llvm.org/D96932, we choose to do canonicalization for it.

Now since "selected" is set as the default parameter of getCanonicalFnName in `D96932`, we don't need to add any attribute here for the previous usage and only fix the missing usage in the pseudo probe decoding.

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

Added: 
    llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfbin
    llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfscript
    llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfbin
    llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfscript
    llvm/test/tools/llvm-profgen/fname-canonicalization.test

Modified: 
    llvm/test/tools/llvm-profgen/symbolize.ll
    llvm/tools/llvm-profgen/ProfiledBinary.cpp
    llvm/tools/llvm-profgen/PseudoProbe.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfbin b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfbin
new file mode 100755
index 000000000000..1a218358e426
Binary files /dev/null and b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfbin 
diff er

diff  --git a/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfscript b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfscript
new file mode 100644
index 000000000000..439589b27f4a
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-dwarf.perfscript
@@ -0,0 +1,5 @@
+PERF_RECORD_MMAP2 3021905/3021905: [0x400000(0x1000) @ 0 00:1d 306132807 1941166]: r-xp unique-linkage-name-dwarf.perfbin
+
+            4006de
+  5541f689495641d7
+ 0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x4006fb/0x4006d0/M/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0  0x400728/0x4006de/P/-/-/0

diff  --git a/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfbin b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfbin
new file mode 100755
index 000000000000..124b1f435ed3
Binary files /dev/null and b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfbin 
diff er

diff  --git a/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfscript b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfscript
new file mode 100644
index 000000000000..21f063ed56aa
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/Inputs/unique-linkage-name-probe.perfscript
@@ -0,0 +1,5 @@
+PERF_RECORD_MMAP2 3933222/3933222: [0x201000(0x1000) @ 0 00:1d 306175638 1941562]: r-xp unique-linkage-name-probe.perfbin
+
+            2017ce
+  5541f689495641d7
+ 0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0  0x201818/0x2017ce/P/-/-/0

diff  --git a/llvm/test/tools/llvm-profgen/fname-canonicalization.test b/llvm/test/tools/llvm-profgen/fname-canonicalization.test
new file mode 100644
index 000000000000..aada4d1b1c77
--- /dev/null
+++ b/llvm/test/tools/llvm-profgen/fname-canonicalization.test
@@ -0,0 +1,54 @@
+; Test unique linkage name decoding and canonicalization from dwarf debug info
+; RUN: llvm-profgen --perfscript=%S/Inputs/unique-linkage-name-dwarf.perfscript --binary=%S/Inputs/unique-linkage-name-dwarf.perfbin --output=%t --csprof-cold-thres=0
+; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-DWARF-FNAME
+
+; CHECK-DWARF-FNAME:[main:1 @ foo]:309:0
+; CHECK-DWARF-FNAME: 2: 14
+; CHECK-DWARF-FNAME: 3: 29 bar.__uniq.26267048767521081047744692097241227776:14
+; CHECK-DWARF-FNAME:[main:1 @ foo:3 @ bar.__uniq.26267048767521081047744692097241227776]:84:0
+; CHECK-DWARF-FNAME: 1: 14
+
+
+; Test unique linkage name decoding and canonicalization from pseudo probe info
+; RUN: llvm-profgen --perfscript=%S/Inputs/unique-linkage-name-probe.perfscript --binary=%S/Inputs/unique-linkage-name-probe.perfbin --output=%t --csprof-cold-thres=0
+; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-PROBE-FNAME
+
+; CHECK-PROBE-FNAME:[main:2 @ foo]:75:0
+; CHECK-PROBE-FNAME: 2: 15
+; CHECK-PROBE-FNAME: 3: 15
+; CHECK-PROBE-FNAME: 4: 15
+; CHECK-PROBE-FNAME: 6: 15
+; CHECK-PROBE-FNAME: 8: 15 _ZL3barii.__uniq.26267048767521081047744692097241227776:15
+; CHECK-PROBE-FNAME: !CFGChecksum: 563088904013236
+; CHECK-PROBE-FNAME:[main:2 @ foo:8 @ _ZL3barii.__uniq.26267048767521081047744692097241227776]:30:15
+; CHECK-PROBE-FNAME: 1: 15
+; CHECK-PROBE-FNAME: 2: 18446744073709551615
+; CHECK-PROBE-FNAME: 3: 18446744073709551615
+; CHECK-PROBE-FNAME: 4: 15
+; CHECK-PROBE-FNAME: !CFGChecksum: 72617220756
+
+
+; Original code:
+; Dwarf:  clang -O3 -funique-internal-linkage-names -g test.c -o a.out
+; Probe:  clang -O3 -funique-internal-linkage-names  -fexperimental-new-pass-manager -fuse-ld=lld -fpseudo-probe-for-profiling -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Xclang -mdisable-tail-calls  -g test.c  -o a.out
+
+#include <stdio.h>
+
+static int bar(int x, int y) {
+  if (x % 3) {
+    return x - y;
+  }
+  return x + y;
+}
+
+void foo() {
+  int s, i = 0;
+  while (i++ < 4000 * 4000)
+    if (i % 91) s = bar(i, s); else s += 30;
+  printf("sum is %d\n", s);
+}
+
+int main() {
+  foo();
+  return 0;
+}

diff  --git a/llvm/test/tools/llvm-profgen/symbolize.ll b/llvm/test/tools/llvm-profgen/symbolize.ll
index 9a436dec4c20..1c05e0589282 100644
--- a/llvm/test/tools/llvm-profgen/symbolize.ll
+++ b/llvm/test/tools/llvm-profgen/symbolize.ll
@@ -1,18 +1,29 @@
 ; REQUIRES: x86-registered-target
 ; RUN: llc -filetype=obj %s -o %t
 ; RUN: llvm-profgen --binary=%t --perfscript=%s --output=%t1 --show-disassembly-only -x86-asm-syntax=intel --show-source-locations | FileCheck %s --match-full-lines
+; RUN: llvm-profgen --binary=%t --perfscript=%s --output=%t2 --show-disassembly-only -x86-asm-syntax=intel --show-source-locations --show-canonical-fname | FileCheck %s --match-full-lines  --check-prefix=CHECK-CANO
 
 ; CHECK: Disassembly of section .text [0x0, 0x4a]:
-; CHECK: <funcA>:
-; CHECK:        0:	mov	eax, edi                         funcA:0
-; CHECK:        2:	mov	ecx, dword ptr [rip]             funcLeaf:2 @ funcA:1
-; CHECK:        8:	lea	edx, [rcx + 3]                   fib:2 @ funcLeaf:2 @ funcA:1
-; CHECK:        b:	cmp	ecx, 3                           fib:2 @ funcLeaf:2 @ funcA:1
-; CHECK:        e:	cmovl	edx, ecx                       fib:2 @ funcLeaf:2 @ funcA:1
-; CHECK:       11:	sub	eax, edx                         funcLeaf:2 @ funcA:1
-; CHECK:       13:	ret                                  funcA:2
+; CHECK: <funcA.llvm.1000>:
+; CHECK:        0:	mov	eax, edi                         funcA.llvm.1000:0
+; CHECK:        2:	mov	ecx, dword ptr [rip]             funcLeaf:2 @ funcA.llvm.1000:1
+; CHECK:        8:	lea	edx, [rcx + 3]                   fib:2 @ funcLeaf:2 @ funcA.llvm.1000:1
+; CHECK:        b:	cmp	ecx, 3                           fib:2 @ funcLeaf:2 @ funcA.llvm.1000:1
+; CHECK:        e:	cmovl	edx, ecx                       fib:2 @ funcLeaf:2 @ funcA.llvm.1000:1
+; CHECK:       11:	sub	eax, edx                         funcLeaf:2 @ funcA.llvm.1000:1
+; CHECK:       13:	ret                                  funcA.llvm.1000:2
 ; CHECK:       14:	nop	word ptr cs:[rax + rax]
 ; CHECK:       1e:	nop
+; CHECK-CANO: <funcA>:
+; CHECK-CANO:        0:	mov	eax, edi                         funcA:0
+; CHECK-CANO:        2:	mov	ecx, dword ptr [rip]             funcLeaf:2 @ funcA:1
+; CHECK-CANO:        8:	lea	edx, [rcx + 3]                   fib:2 @ funcLeaf:2 @ funcA:1
+; CHECK-CANO:        b:	cmp	ecx, 3                           fib:2 @ funcLeaf:2 @ funcA:1
+; CHECK-CANO:        e:	cmovl	edx, ecx                       fib:2 @ funcLeaf:2 @ funcA:1
+; CHECK-CANO:       11:	sub	eax, edx                         funcLeaf:2 @ funcA:1
+; CHECK-CANO:       13:	ret                                  funcA:2
+; CHECK-CANO:       14:	nop	word ptr cs:[rax + rax]
+; CHECK-CANO:       1e:	nop
 ; CHECK: <funcLeaf>:
 ; CHECK:      20:	mov	eax, edi                           funcLeaf:1
 ; CHECK:      22:	mov	ecx, dword ptr [rip]               funcLeaf:2
@@ -34,7 +45,7 @@ target triple = "x86_64-unknown-linux-gnu"
 
 @factor = dso_local global i32 3
 
-define dso_local i32 @funcA(i32 %x) !dbg !12 {
+define dso_local i32 @funcA.llvm.1000(i32 %x) !dbg !12 {
 entry:
   call void @llvm.dbg.value(metadata i32 %x, metadata !16, metadata !DIExpression()), !dbg !18
   call void @llvm.dbg.value(metadata i32 %x, metadata !19, metadata !DIExpression()), !dbg !22
@@ -85,7 +96,7 @@ declare void @llvm.dbg.value(metadata, metadata, metadata) #2
 !8 = !{i32 7, !"Dwarf Version", i32 4}
 !9 = !{i32 2, !"Debug Info Version", i32 3}
 !10 = !{i32 1, !"wchar_size", i32 4}
-!12 = distinct !DISubprogram(name: "funcA", scope: !3, file: !3, line: 6, type: !13, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !15)
+!12 = distinct !DISubprogram(name: "funcA.llvm.1000", scope: !3, file: !3, line: 6, type: !13, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !15)
 !13 = !DISubroutineType(types: !14)
 !14 = !{!7, !7}
 !15 = !{!16, !17}

diff  --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
index 4c252943c1ac..2d6cbfe474fd 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -30,6 +30,10 @@ cl::opt<bool> ShowSourceLocations("show-source-locations", cl::ReallyHidden,
                                   cl::init(false), cl::ZeroOrMore,
                                   cl::desc("Print source locations."));
 
+cl::opt<bool> ShowCanonicalFnName("show-canonical-fname", cl::ReallyHidden,
+                                  cl::init(false), cl::ZeroOrMore,
+                                  cl::desc("Print canonical function name."));
+
 cl::opt<bool> ShowPseudoProbe(
     "show-pseudo-probe", cl::ReallyHidden, cl::init(false), cl::ZeroOrMore,
     cl::desc("Print pseudo probe section and disassembled info."));
@@ -213,7 +217,10 @@ bool ProfiledBinary::dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
   if (StartOffset >= EndOffset)
     return true;
 
-  std::string &&SymbolName = Symbols[SI].Name.str();
+  StringRef SymbolName =
+      ShowCanonicalFnName
+          ? FunctionSamples::getCanonicalFnName(Symbols[SI].Name)
+          : Symbols[SI].Name;
   if (ShowDisassemblyOnly)
     outs() << '<' << SymbolName << ">:\n";
 
@@ -253,7 +260,7 @@ bool ProfiledBinary::dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
         if (Cur < 40)
           outs().indent(40 - Cur);
         InstructionPointer IP(this, Offset);
-        outs() << getReversedLocWithContext(symbolize(IP));
+        outs() << getReversedLocWithContext(symbolize(IP, ShowCanonicalFnName));
       }
       outs() << "\n";
     }

diff  --git a/llvm/tools/llvm-profgen/PseudoProbe.cpp b/llvm/tools/llvm-profgen/PseudoProbe.cpp
index a537d9012e6d..a9040c9c5aa0 100644
--- a/llvm/tools/llvm-profgen/PseudoProbe.cpp
+++ b/llvm/tools/llvm-profgen/PseudoProbe.cpp
@@ -162,7 +162,7 @@ void PseudoProbeDecoder::buildGUID2FuncDescMap(const uint8_t *Start,
     uint64_t GUID = readUnencodedNumber<uint64_t>();
     uint64_t Hash = readUnencodedNumber<uint64_t>();
     uint32_t NameSize = readUnsignedNumber<uint32_t>();
-    StringRef Name = readString(NameSize);
+    StringRef Name = FunctionSamples::getCanonicalFnName(readString(NameSize));
 
     // Initialize PseudoProbeFuncDesc and populate it into GUID2FuncDescMap
     GUID2FuncDescMap.emplace(GUID, PseudoProbeFuncDesc(GUID, Hash, Name));


        


More information about the llvm-commits mailing list