Skip to content

Commit cb96a3d

Browse files
[memprof] Dump the number of matched frames (#137082)
This patch teaches readMemprof to dump the number of frames for each allocation site match. This information helps us analyze what part of the call stack in the MemProf profile has matched the IR. Aside from updating existing test cases, this patch adds one more test case, memprof-dump-matched-alloc-site.ll, because none of the existing test cases has the number of frames greater than one.
1 parent cd92d8d commit cb96a3d

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ static bool isAllocationWithHotColdVariant(const Function *Callee,
818818

819819
struct AllocMatchInfo {
820820
uint64_t TotalSize = 0;
821+
size_t NumFramesMatched = 0;
821822
AllocationType AllocType = AllocationType::None;
822823
bool Matched = false;
823824
};
@@ -1152,7 +1153,8 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
11521153
if (ClPrintMemProfMatchInfo) {
11531154
assert(FullStackId != 0);
11541155
FullStackIdToAllocMatchInfo[FullStackId] = {
1155-
AllocInfo->Info.getTotalSize(), AllocType, /*Matched=*/true};
1156+
AllocInfo->Info.getTotalSize(), InlinedCallStack.size(),
1157+
AllocType, /*Matched=*/true};
11561158
}
11571159
}
11581160
}
@@ -1285,7 +1287,7 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
12851287
errs() << "MemProf " << getAllocTypeAttributeString(Info.AllocType)
12861288
<< " context with id " << Id << " has total profiled size "
12871289
<< Info.TotalSize << (Info.Matched ? " is" : " not")
1288-
<< " matched\n";
1290+
<< " matched with " << Info.NumFramesMatched << " frames\n";
12891291

12901292
for (const auto &CallStack : MatchedCallSites) {
12911293
errs() << "MemProf callsite match for inline call stack";
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
; Tests that the compiler dumps an allocation site with multiple inlined frames.
2+
;
3+
; The test case is generated from:
4+
;
5+
; // main
6+
; // |
7+
; // f1 (noinline)
8+
; // |
9+
; // f2
10+
; // |
11+
; // f3
12+
; // |
13+
; // new
14+
;
15+
; char *f1() { return new char[3]; }
16+
; char *f2() { return f1(); }
17+
; __attribute__((noinline)) char *f3() { return f2(); }
18+
;
19+
; int main() {
20+
; f3();
21+
; return 0;
22+
; }
23+
;
24+
; Here we expect to match the allocation site to encompass 3 frames.
25+
26+
; REQUIRES: x86_64-linux
27+
; RUN: split-file %s %t
28+
; RUN: llvm-profdata merge %t/memprof-dump-matched-alloc-site.yaml -o %t/memprof-dump-matched-alloc-site.memprofdata
29+
; RUN: opt < %t/memprof-dump-matched-alloc-site.ll -passes='memprof-use<profile-filename=%t/memprof-dump-matched-alloc-site.memprofdata>' -memprof-print-match-info -S 2>&1 | FileCheck %s
30+
31+
;--- memprof-dump-matched-alloc-site.yaml
32+
---
33+
HeapProfileRecords:
34+
- GUID: _Z2f3v
35+
AllocSites:
36+
- Callstack:
37+
- { Function: _ZL2f1v, LineOffset: 0, Column: 35, IsInlineFrame: true }
38+
- { Function: _ZL2f2v, LineOffset: 0, Column: 35, IsInlineFrame: true }
39+
- { Function: _Z2f3v, LineOffset: 0, Column: 47, IsInlineFrame: false }
40+
- { Function: main, LineOffset: 1, Column: 3, IsInlineFrame: false }
41+
MemInfoBlock:
42+
AllocCount: 1
43+
TotalSize: 3
44+
TotalLifetime: 0
45+
TotalLifetimeAccessDensity: 0
46+
CallSites:
47+
# Kept empty here because this section is irrelevant for this test.
48+
...
49+
;--- memprof-dump-matched-alloc-site.ll
50+
; CHECK: MemProf notcold context with id 12978026349401156968 has total profiled size 3 is matched with 3 frames
51+
52+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
53+
target triple = "x86_64-unknown-linux-gnu"
54+
55+
define ptr @_Z2f3v() {
56+
entry:
57+
%call.i.i = call ptr @_Znam(i64 0), !dbg !3
58+
ret ptr null
59+
}
60+
61+
declare ptr @_Znam(i64)
62+
63+
!llvm.dbg.cu = !{!0}
64+
!llvm.module.flags = !{!2}
65+
66+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
67+
!1 = !DIFile(filename: "memprof-dump-matched-alloc-site.cc", directory: "/")
68+
!2 = !{i32 2, !"Debug Info Version", i32 3}
69+
!3 = !DILocation(line: 1, column: 35, scope: !4, inlinedAt: !7)
70+
!4 = distinct !DISubprogram(name: "f1", linkageName: "_ZL2f1v", scope: !1, file: !1, line: 1, type: !5, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
71+
!5 = !DISubroutineType(types: !6)
72+
!6 = !{}
73+
!7 = distinct !DILocation(line: 2, column: 35, scope: !8, inlinedAt: !9)
74+
!8 = distinct !DISubprogram(name: "f2", linkageName: "_ZL2f2v", scope: !1, file: !1, line: 2, type: !5, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0)
75+
!9 = distinct !DILocation(line: 3, column: 47, scope: !10)
76+
!10 = distinct !DISubprogram(name: "f3", linkageName: "_Z2f3v", scope: !1, file: !1, line: 3, type: !5, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
77+
!11 = !DILocation(line: 6, column: 3, scope: !12)
78+
!12 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 5, type: !5, scopeLine: 5, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)

llvm/test/Transforms/PGOProfile/memprof-dump-matched-call-sites.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ HeapProfileRecords:
7171
CallSites: []
7272
...
7373
;--- memprof-dump-matched-call-site.ll
74-
; CHECK: MemProf notcold context with id 3894143216621363392 has total profiled size 4 is matched
74+
; CHECK: MemProf notcold context with id 3894143216621363392 has total profiled size 4 is matched with 1 frames
7575
; CHECK: MemProf callsite match for inline call stack 4745611964195289084 10616861955219347331
7676
; CHECK: MemProf callsite match for inline call stack 5401059281181789382
7777

llvm/test/Transforms/PGOProfile/memprof.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@
9393
;; notcold again.
9494
; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-min-ave-lifetime-access-density-hot-threshold=0 2>&1 | FileCheck %s --check-prefixes=MEMPROF,ALL
9595

96-
; MEMPROFMATCHINFO: MemProf notcold context with id 1093248920606587996 has total profiled size 10 is matched
97-
; MEMPROFMATCHINFO: MemProf notcold context with id 5725971306423925017 has total profiled size 10 is matched
98-
; MEMPROFMATCHINFO: MemProf notcold context with id 6792096022461663180 has total profiled size 10 is matched
99-
; MEMPROFMATCHINFO: MemProf cold context with id 8525406123785421946 has total profiled size 10 is matched
100-
; MEMPROFMATCHINFO: MemProf cold context with id 11714230664165068698 has total profiled size 10 is matched
101-
; MEMPROFMATCHINFO: MemProf cold context with id 15737101490731057601 has total profiled size 10 is matched
102-
; MEMPROFMATCHINFO: MemProf cold context with id 16342802530253093571 has total profiled size 10 is matched
103-
; MEMPROFMATCHINFO: MemProf cold context with id 18254812774972004394 has total profiled size 10 is matched
96+
; MEMPROFMATCHINFO: MemProf notcold context with id 1093248920606587996 has total profiled size 10 is matched with 1 frames
97+
; MEMPROFMATCHINFO: MemProf notcold context with id 5725971306423925017 has total profiled size 10 is matched with 1 frames
98+
; MEMPROFMATCHINFO: MemProf notcold context with id 6792096022461663180 has total profiled size 10 is matched with 1 frames
99+
; MEMPROFMATCHINFO: MemProf cold context with id 8525406123785421946 has total profiled size 10 is matched with 1 frames
100+
; MEMPROFMATCHINFO: MemProf cold context with id 11714230664165068698 has total profiled size 10 is matched with 1 frames
101+
; MEMPROFMATCHINFO: MemProf cold context with id 15737101490731057601 has total profiled size 10 is matched with 1 frames
102+
; MEMPROFMATCHINFO: MemProf cold context with id 16342802530253093571 has total profiled size 10 is matched with 1 frames
103+
; MEMPROFMATCHINFO: MemProf cold context with id 18254812774972004394 has total profiled size 10 is matched with 1 frames
104104
; MEMPROFMATCHINFO: MemProf callsite match for inline call stack 748269490701775343
105105
; MEMPROFMATCHINFO: MemProf callsite match for inline call stack 1544787832369987002
106106
; MEMPROFMATCHINFO: MemProf callsite match for inline call stack 2061451396820446691

0 commit comments

Comments
 (0)