[llvm] [SampleProf] Accumulate vtable type counts for FS-discriminator-collapsed locations (PR #230380)
Mingming Liu via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 21:19:32 PDT 2026
https://github.com/mingmingl-llvm updated https://github.com/llvm/llvm-project/pull/230380
>From 544ca64a959198b5cd5c7a3aef60952a2363376c Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Fri, 9 Oct 2026 00:11:17 -0700
Subject: [PATCH] [SampleProf] Accumulate vtable type counts for
FS-discriminator-collapsed locations
With flow-sensitive discriminators, the IR-level sample loader masks profile
discriminators down to the base discriminator, so distinct raw locations in
the profile (e.g. `3.1` and `3.257`, or `3` and `3.256`) legitimately collapse
into one `LineLocation`. Body and callsite samples are accumulated in that case
by `readProfile`, but `readVTableTypeCountMap` still inserted into the per-
location type map and diagnosed the second copy of a type as
warning: Duplicate vtable type ... at the same location
which `-Werror=backend-plugin` turns into a build failure for any TU whose
profile has such a collapsed virtual callsite.
`readCallsiteVTableProf` now passes `Merge = true` to `readVTableTypeCountMap`
when the reader is in FS mode and the masked location already has a type map,
in which case counts are accumulated with saturating add. Genuine duplicates in
non-FS profiles are still diagnosed.
Test: `vtable-prof-fs-discriminator.ll` merges an extbinary profile with
`-profile-isfs` whose vtable records at `3.1` and `3.257` collapse to `3.1`,
and checks that no duplicate-vtable diagnostic is emitted.
---
.../llvm/ProfileData/SampleProfReader.h | 6 +-
llvm/lib/ProfileData/SampleProfReader.cpp | 27 ++++++-
.../Inputs/vtable-prof-fs-discriminator.prof | 12 +++
.../vtable-prof-fs-discriminator.ll | 79 +++++++++++++++++++
4 files changed, 119 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/Transforms/SampleProfile/Inputs/vtable-prof-fs-discriminator.prof
create mode 100644 llvm/test/Transforms/SampleProfile/vtable-prof-fs-discriminator.ll
diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h
index 2bb616bb94b1f..c58aeb8fb37bb 100644
--- a/llvm/include/llvm/ProfileData/SampleProfReader.h
+++ b/llvm/include/llvm/ProfileData/SampleProfReader.h
@@ -987,7 +987,11 @@ class LLVM_ABI SampleProfileReaderBinary : public SampleProfileReader {
/// Read bytes from the input buffer pointed by `Data` and decode them into
/// \p M. `Data` will be advanced to the end of the read bytes when this
/// function returns. Returns error if any.
- std::error_code readVTableTypeCountMap(TypeCountMap &M);
+ /// If \p Merge is true, counts for types already present in \p M are
+ /// accumulated (used when several profile locations are collapsed into one
+ /// by FS-discriminator masking); otherwise a duplicate type is diagnosed and
+ /// ignored.
+ std::error_code readVTableTypeCountMap(TypeCountMap &M, bool Merge);
/// Points to the current location in the buffer.
const uint8_t *Data = nullptr;
diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp
index 86610d5b0445b..8cab28a6bc6a0 100644
--- a/llvm/lib/ProfileData/SampleProfReader.cpp
+++ b/llvm/lib/ProfileData/SampleProfReader.cpp
@@ -673,11 +673,13 @@ SampleProfileReaderBinary::readSampleContextFromTable() {
}
std::error_code
-SampleProfileReaderBinary::readVTableTypeCountMap(TypeCountMap &M) {
+SampleProfileReaderBinary::readVTableTypeCountMap(TypeCountMap &M, bool Merge) {
auto NumVTableTypes = readNumber<uint32_t>();
if (std::error_code EC = NumVTableTypes.getError())
return EC;
- M.reserve(*NumVTableTypes);
+ // In the case of merging, M may already contain some vtable types. So
+ // reserve for both existing and potentially new vtables.
+ M.reserve(M.size() + *NumVTableTypes);
for (uint32_t I = 0; I < *NumVTableTypes; ++I) {
auto VTableType(readStringFromTable());
@@ -687,6 +689,17 @@ SampleProfileReaderBinary::readVTableTypeCountMap(TypeCountMap &M) {
auto VTableSamples = readNumber<uint64_t>();
if (std::error_code EC = VTableSamples.getError())
return EC;
+
+ // With flow-sensitive discriminators enabled in the profiled binary,
+ // several raw locations of the profiled instruction may map to the same
+ // masked location. In that case, we accumulate vtable counts for the
+ // same masked location.
+ if (Merge) {
+ uint64_t &Count = M[*VTableType];
+ Count = SaturatingAdd(Count, *VTableSamples);
+ continue;
+ }
+
// The source profile should not have duplicate vtable records at the same
// location. In case duplicate vtables are found, reader can emit a warning
// but continue processing the profile.
@@ -727,9 +740,15 @@ SampleProfileReaderBinary::readCallsiteVTableProf(FunctionSamples &FProfile) {
// Here we handle FS discriminators:
const uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
+ LineLocation Loc(*LineOffset, DiscriminatorVal);
+
+ // With FS discriminators, merge vtable counts for the same masked location
+ // like how body and callsite samples are accumulated.
+ const bool Merge =
+ ProfileIsFS && FProfile.findCallsiteTypeSamplesAt(Loc) != nullptr;
- if (std::error_code EC = readVTableTypeCountMap(FProfile.getTypeSamplesAt(
- LineLocation(*LineOffset, DiscriminatorVal))))
+ if (std::error_code EC =
+ readVTableTypeCountMap(FProfile.getTypeSamplesAt(Loc), Merge))
return EC;
}
return sampleprof_error::success;
diff --git a/llvm/test/Transforms/SampleProfile/Inputs/vtable-prof-fs-discriminator.prof b/llvm/test/Transforms/SampleProfile/Inputs/vtable-prof-fs-discriminator.prof
new file mode 100644
index 0000000000000..df051088d4e66
--- /dev/null
+++ b/llvm/test/Transforms/SampleProfile/Inputs/vtable-prof-fs-discriminator.prof
@@ -0,0 +1,12 @@
+_Z3fooP4Basei:1000:100
+ 1: 100
+ 3: 100
+ 3.1: 900 _ZN4Base1fEv:900
+ 3.1: vtables _ZTV4Base:900
+ 3.257: 100 _ZN4Base1fEv:100
+ 3.257: vtables _ZTV4Base:100
+ 4: 100
+ 5.1: _Z3barv:70
+ 2: 70
+ 5.257: _Z3barv:40
+ 2: 40
diff --git a/llvm/test/Transforms/SampleProfile/vtable-prof-fs-discriminator.ll b/llvm/test/Transforms/SampleProfile/vtable-prof-fs-discriminator.ll
new file mode 100644
index 0000000000000..a91146d1a1e6b
--- /dev/null
+++ b/llvm/test/Transforms/SampleProfile/vtable-prof-fs-discriminator.ll
@@ -0,0 +1,79 @@
+;; With FS discriminators, distinct raw profile locations (from debug info) can
+;; map to the same masked location. Body and callsite samples are accumulated in
+;; that case; test that vtable type samples are accumulated the same way, and
+;; that there are no warnings of 'Duplicate vtable type'.
+
+;; In vtable-prof-fs-discriminator.prof, 3.1 and 3.257, 5.1 and 5.257 collapse
+;; to one location ( 257 = 1 | 1 << 8 ).
+;
+; RUN: llvm-profdata merge -sample -extbinary -extbinary-write-vtable-type-prof \
+; RUN: -profile-isfs %S/Inputs/vtable-prof-fs-discriminator.prof -o %t.fs.afdo
+;
+; Reading the profile back with every discriminator bit.
+; RUN: llvm-profdata merge -sample -text -fs-discriminator-pass=PassLast \
+; RUN: %t.fs.afdo -o - 2>&1 | FileCheck %s --check-prefix=RAW --match-full-lines \
+; RUN: --implicit-check-not='Duplicate vtable type'
+;
+; Reading it back with the base mask, this is what the IR-level sample loader
+; sees.
+; RUN: llvm-profdata merge -sample -text -fs-discriminator-pass=Base \
+; RUN: %t.fs.afdo -o - 2>&1 | FileCheck %s --check-prefix=MERGED --match-full-lines \
+; RUN: --implicit-check-not='Duplicate vtable type'
+;
+; The sample loader itself consumes the merged profile without a diagnostic.
+; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.fs.afdo -S 2>&1 \
+; RUN: | FileCheck %s --implicit-check-not='Duplicate vtable type'
+
+; RAW: _Z3fooP4Basei:1000:100
+; RAW-NEXT: 1: 100
+; RAW-NEXT: 3: 100
+; RAW-NEXT: 3.1: 900 _ZN4Base1fEv:900
+; RAW-NEXT: 3.1: vtables _ZTV4Base:900
+; RAW-NEXT: 3.257: 100 _ZN4Base1fEv:100
+; RAW-NEXT: 3.257: vtables _ZTV4Base:100
+; RAW-NEXT: 4: 100
+; RAW-NEXT: 5.1: _Z3barv:70
+; RAW-NEXT: 2: 70
+; RAW-NEXT: 5.257: _Z3barv:40
+; RAW-NEXT: 2: 40
+
+;; Only base discriminators (lower 8 bits) are retained, and higher bits are
+;; masked.
+; MERGED: _Z3fooP4Basei:1000:100
+; MERGED-NEXT: 1: 100
+; MERGED-NEXT: 3: 100
+; MERGED-NEXT: 3.1: 1000 _ZN4Base1fEv:1000
+; MERGED-NEXT: 3.1: vtables _ZTV4Base:1000
+; MERGED-NEXT: 4: 100
+; MERGED-NEXT: 5.1: _Z3barv:110
+; MERGED-NEXT: 2: 110
+
+; CHECK: define {{.*}} @_Z3fooP4Basei({{.*}} !prof ![[FOO_ENTRY:[0-9]+]]
+; CHECK: ![[FOO_ENTRY]] = !{!"function_entry_count", i64 101}
+
+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"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @_Z3fooP4Basei(ptr %b, i32 %x) #0 !dbg !10 {
+entry:
+ %vtable = load ptr, ptr %b, align 8, !dbg !13
+ %fp = load ptr, ptr %vtable, align 8, !dbg !13
+ %r = call i32 %fp(ptr %b), !dbg !13
+ ret i32 %r, !dbg !14
+}
+
+attributes #0 = { "use-sample-profile" }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, debugInfoForProfiling: true)
+!1 = !DIFile(filename: "vtable-prof-fs-discriminator.cc", directory: "/tmp")
+!3 = !{i32 7, !"Dwarf Version", i32 5}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !DISubroutineType(types: !6)
+!6 = !{}
+!10 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooP4Basei", scope: !1, file: !1, line: 1, type: !5, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0)
+!13 = !DILocation(line: 4, column: 10, scope: !15)
+!14 = !DILocation(line: 5, column: 3, scope: !10)
+!15 = !DILexicalBlockFile(scope: !10, file: !1, discriminator: 1)
More information about the llvm-commits
mailing list