Skip to content

Commit c87b1ca

Browse files
authored
[InstrProf] Fix bug when clearing traces with samples (#92310)
The `--temporal-profile-max-trace-length=0` flag in the `llvm-profdata merge` command is used to remove traces from a profile. There was a bug where traces would not be cleared if the profile was already sampled. This patch fixes that.
1 parent 3ddfb68 commit c87b1ca

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,8 @@ void InstrProfWriter::addBinaryIds(ArrayRef<llvm::object::BuildID> BIs) {
320320
}
321321

322322
void InstrProfWriter::addTemporalProfileTrace(TemporalProfTraceTy Trace) {
323-
if (Trace.FunctionNameRefs.size() > MaxTemporalProfTraceLength)
324-
Trace.FunctionNameRefs.resize(MaxTemporalProfTraceLength);
325-
if (Trace.FunctionNameRefs.empty())
326-
return;
327-
323+
assert(Trace.FunctionNameRefs.size() <= MaxTemporalProfTraceLength);
324+
assert(!Trace.FunctionNameRefs.empty());
328325
if (TemporalProfTraceStreamSize < TemporalProfTraceReservoirSize) {
329326
// Simply append the trace if we have not yet hit our reservoir size limit.
330327
TemporalProfTraces.push_back(std::move(Trace));
@@ -341,6 +338,10 @@ void InstrProfWriter::addTemporalProfileTrace(TemporalProfTraceTy Trace) {
341338

342339
void InstrProfWriter::addTemporalProfileTraces(
343340
SmallVectorImpl<TemporalProfTraceTy> &SrcTraces, uint64_t SrcStreamSize) {
341+
for (auto &Trace : SrcTraces)
342+
if (Trace.FunctionNameRefs.size() > MaxTemporalProfTraceLength)
343+
Trace.FunctionNameRefs.resize(MaxTemporalProfTraceLength);
344+
llvm::erase_if(SrcTraces, [](auto &T) { return T.FunctionNameRefs.empty(); });
344345
// Assume that the source has the same reservoir size as the destination to
345346
// avoid needing to record it in the indexed profile format.
346347
bool IsDestSampled =

llvm/test/tools/llvm-profdata/trace-limit.proftext

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# RUN: llvm-profdata merge --temporal-profile-max-trace-length=0 %s -o %t.profdata
22
# RUN: llvm-profdata show --temporal-profile-traces %t.profdata | FileCheck %s --check-prefix=NONE
33

4+
# RUN: llvm-profdata merge --temporal-profile-trace-reservoir-size=2 %s %s %s %s -o %t.profdata
5+
# RUN: llvm-profdata merge --temporal-profile-trace-reservoir-size=2 --temporal-profile-max-trace-length=0 %t.profdata -o %t.profdata
6+
# RUN: llvm-profdata show --temporal-profile-traces %t.profdata | FileCheck %s --check-prefix=NONE
7+
48
# RUN: llvm-profdata merge --temporal-profile-max-trace-length=2 %s -o %t.profdata
59
# RUN: llvm-profdata show --temporal-profile-traces %t.profdata | FileCheck %s --check-prefixes=CHECK,SOME
610

711
# RUN: llvm-profdata merge --temporal-profile-max-trace-length=1000 %s -o %t.profdata
812
# RUN: llvm-profdata show --temporal-profile-traces %t.profdata | FileCheck %s --check-prefixes=CHECK,ALL
913

10-
# NONE: Temporal Profile Traces (samples=0 seen=0):
14+
# NONE: Temporal Profile Traces (samples=0
1115
# CHECK: Temporal Profile Traces (samples=1 seen=1):
1216
# SOME: Trace 0 (weight=1 count=2):
1317
# ALL: Trace 0 (weight=1 count=3):

0 commit comments

Comments
 (0)