Skip to content

Commit 6a214ec

Browse files
[MemProf] Fix an assertion when writing distributed index for aliasee (#122946)
The ThinLTO index bitcode writer uses a helper forEachSummary to manage preparation and writing of summaries needed for each distributed index file. For alias summaries, it invokes the provided callback for the aliasee as well, as we at least need to produce a value id for the alias's summary. However, all summary generation for the aliasee itself should be skipped on calls when IsAliasee is true. We invoke the callback again if that value's summary is to be written as well. We were asserting in debug mode when invoking collectMemProfCallStacks, because a given stack id index was not in the StackIdIndicesToIndex map. It was not added because the forEachSummary invocation that records these ids in the map (invoked from the IndexBitcodeWriter constructor) was correctly skipping this handling when invoked for aliasees. We need the same guard in the invocation that calls collectMemProfCallStacks. Note that this doesn't cause any real problems in a non-asserts build as the missing map lookup will return the default 0 value from the map, which isn't used since we don't actually write the corresponding summary.
1 parent f09db6a commit 6a214ec

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
494494
// are currently saved in the index in terms of GUID.
495495
forEachSummary([&](GVInfo I, bool IsAliasee) {
496496
GUIDToValueIdMap[I.first] = ++GlobalValueId;
497+
// If this is invoked for an aliasee, we want to record the above mapping,
498+
// but not the information needed for its summary entry (if the aliasee is
499+
// to be imported, we will invoke this separately with IsAliasee=false).
497500
if (IsAliasee)
498501
return;
499502
auto *FS = dyn_cast<FunctionSummary>(I.second);
@@ -4847,6 +4850,11 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
48474850
// radix tree array are identified based on this order.
48484851
MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> CallStacks;
48494852
forEachSummary([&](GVInfo I, bool IsAliasee) {
4853+
// Don't collect this when invoked for an aliasee, as it is not needed for
4854+
// the alias summary. If the aliasee is to be imported, we will invoke this
4855+
// separately with IsAliasee=false.
4856+
if (IsAliasee)
4857+
return;
48504858
GlobalValueSummary *S = I.second;
48514859
assert(S);
48524860
auto *FS = dyn_cast<FunctionSummary>(S);

llvm/test/ThinLTO/X86/memprof_direct_recursion.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
; RUN: -supports-hot-cold-new \
3535
; RUN: -thinlto-distributed-indexes \
3636
; RUN: -r=%t/b.o,_Z3fooi,plx \
37+
; RUN: -r=%t/b.o,aliasee,plx \
3738
; RUN: -r=%t/b.o,a \
3839
; RUN: -r=%t/b.o,b \
3940
; RUN: -r=%t/b.o,_Znam \
@@ -65,11 +66,15 @@ source_filename = "b.cpp"
6566
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"
6667
target triple = "x86_64-unknown-linux-gnu"
6768

69+
;; Make sure the distributed summary bitcode writing succeeds when the memprof
70+
;; metadata is in an aliasee.
71+
@_Z3fooi = alias void (), ptr @aliasee
72+
6873
@a = external local_unnamed_addr global ptr, align 8
6974
@b = external local_unnamed_addr global i32, align 4
7075

7176
; Function Attrs: mustprogress uwtable
72-
define dso_local void @_Z3fooi(i32 noundef %0) local_unnamed_addr #0 !dbg !9 {
77+
define dso_local void @aliasee(i32 noundef %0) local_unnamed_addr #0 !dbg !9 {
7378
br label %2, !dbg !12
7479

7580
2: ; preds = %7, %1
@@ -222,4 +227,4 @@ attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "t
222227
!19 = !DILocation(line: 7, column: 5, scope: !9)
223228
!20 = !{i64 8256520048276991898}
224229
!21 = !DILocation(line: 8, column: 5, scope: !9)
225-
!22 = !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 1, type: !10, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
230+
!22 = !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 1, type: !10, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)

0 commit comments

Comments
 (0)