Skip to content

Commit b488ce0

Browse files
[memprof] Improve call site matching (#129770)
Suppose we have a call instruction satisfying: - AllocInfoIter != LocHashToAllocInfo.end() - CallSitesIter != LocHashToCallSites.end() - !isAllocationWithHotColdVariant(CI->getCalledFunction(), TLI) In this case this patch, we would take: if (AllocInfoIter != LocHashToAllocInfo.end() but end up discarding the opportunity because of the call to isAllocationWithHotColdVariant. This can happen in C++ code like: new Something[100]; which is lowered to two calls -- new and the constructor. This patch fixes the problem by falling back to the call site annotation if we have !isAllocationWithHotColdVariant.
1 parent 1506f2e commit b488ce0

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,9 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
11221122
// instruction's leaf location in that map, and if the rest of the
11231123
// instruction's locations match the prefix Frame locations on an
11241124
// allocation context with the same leaf.
1125-
if (AllocInfoIter != LocHashToAllocInfo.end()) {
1126-
// Only consider allocations which support hinting.
1127-
if (!isAllocationWithHotColdVariant(CI->getCalledFunction(), TLI))
1128-
continue;
1125+
if (AllocInfoIter != LocHashToAllocInfo.end() &&
1126+
// Only consider allocations which support hinting.
1127+
isAllocationWithHotColdVariant(CI->getCalledFunction(), TLI)) {
11291128
// We may match this instruction's location list to multiple MIB
11301129
// contexts. Add them to a Trie specialized for trimming the contexts to
11311130
// the minimal needed to disambiguate contexts with unique behavior.
@@ -1189,10 +1188,12 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
11891188
continue;
11901189
}
11911190

1191+
if (CallSitesIter == LocHashToCallSites.end())
1192+
continue;
1193+
11921194
// Otherwise, add callsite metadata. If we reach here then we found the
11931195
// instruction's leaf location in the callsites map and not the allocation
11941196
// map.
1195-
assert(CallSitesIter != LocHashToCallSites.end());
11961197
for (auto CallStackIdx : CallSitesIter->second) {
11971198
// If we found and thus matched all frames on the call, create and
11981199
// attach call stack metadata.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; Verifies that a call site gets annotated even when a call site matches an
2+
; allocation call stack but does not call one of the memory allocation
3+
; functions.
4+
5+
; REQUIRES: x86_64-linux
6+
; RUN: split-file %s %t
7+
; RUN: llvm-profdata merge %t/memprof-call-site-at-alloc-site.yaml -o %t/memprof-call-site-at-alloc-site.memprofdata
8+
; RUN: opt < %t/memprof-call-site-at-alloc-site.ll -passes='memprof-use<profile-filename=%t/memprof-call-site-at-alloc-site.memprofdata>' -memprof-print-match-info -S 2>&1 | FileCheck %s
9+
10+
;--- memprof-call-site-at-alloc-site.yaml
11+
---
12+
HeapProfileRecords:
13+
- GUID: _Z3foov
14+
AllocSites:
15+
- Callstack:
16+
- { Function: _Z3foov, LineOffset: 6, Column: 12, IsInlineFrame: false }
17+
MemInfoBlock:
18+
AllocCount: 1
19+
TotalSize: 898709270
20+
TotalLifetime: 1000000
21+
TotalLifetimeAccessDensity: 1
22+
CallSites:
23+
- - { Function: _Z3foov, LineOffset: 6, Column: 12, IsInlineFrame: false }
24+
...
25+
26+
;--- memprof-call-site-at-alloc-site.ll
27+
; CHECK: MemProf callsite match for inline call stack 774294594974568741
28+
29+
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"
30+
target triple = "x86_64-grtev4-linux-gnu"
31+
32+
declare void @_ZN9SomethingC1Ev()
33+
34+
define void @_Z3foov() {
35+
%1 = call ptr @_Znam(), !dbg !3
36+
call void @_ZN9SomethingC1Ev(), !dbg !3
37+
ret void
38+
}
39+
40+
declare ptr @_Znam()
41+
42+
!llvm.dbg.cu = !{!0}
43+
!llvm.module.flags = !{!2}
44+
45+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
46+
!1 = !DIFile(filename: "something.cc", directory: "/")
47+
!2 = !{i32 2, !"Debug Info Version", i32 3}
48+
!3 = !DILocation(line: 106, column: 12, scope: !4)
49+
!4 = distinct !DISubprogram(name: "Init", linkageName: "_Z3foov", scope: !5, file: !1, line: 100, type: !7, scopeLine: 100, spFlags: DISPFlagDefinition, unit: !0)
50+
!5 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "Something", file: !1)
51+
!6 = !{}
52+
!7 = distinct !DISubroutineType(types: !6)

0 commit comments

Comments
 (0)