Skip to content

Commit a4b543a

Browse files
committed
[llvm-profdata] Check for all duplicate entries in MemOpSize table
Previously, we only checked for duplicate zero entries when merging a MemOPSize table (see D92074), but a user recently provided a reproducer demonstrating that other entries can also be duplicated. As demonstrated by the test in this patch, PGOMemOPSizeOpt can potentially generate invalid IR for non-zero, non-consecutive duplicate entries. This seems to be a rare case, since the duplicate entry is often below the threshold, but possible. This patch extends the existing warning to check for any duplicate values in the table, both in the optimization and in llvm-profdata. Differential Revision: https://reviews.llvm.org/D136211
1 parent d6b224e commit a4b543a

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,10 @@ Error InstrProfWriter::validateRecord(const InstrProfRecord &Func) {
530530
for (uint32_t S = 0; S < NS; S++) {
531531
uint32_t ND = Func.getNumValueDataForSite(VK, S);
532532
std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
533-
bool WasZero = false;
533+
DenseSet<uint64_t> SeenValues;
534534
for (uint32_t I = 0; I < ND; I++)
535-
if ((VK != IPVK_IndirectCallTarget) && (VD[I].Value == 0)) {
536-
if (WasZero)
537-
return make_error<InstrProfError>(instrprof_error::invalid_prof);
538-
WasZero = true;
539-
}
535+
if ((VK != IPVK_IndirectCallTarget) && !SeenValues.insert(VD[I].Value).second)
536+
return make_error<InstrProfError>(instrprof_error::invalid_prof);
540537
}
541538
}
542539

llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ bool MemOPSizeOpt::perform(MemOp MO) {
291291
uint64_t SavedRemainCount = SavedTotalCount;
292292
SmallVector<uint64_t, 16> SizeIds;
293293
SmallVector<uint64_t, 16> CaseCounts;
294+
SmallDenseSet<uint64_t, 16> SeenSizeId;
294295
uint64_t MaxCount = 0;
295296
unsigned Version = 0;
296-
int64_t LastV = -1;
297297
// Default case is in the front -- save the slot here.
298298
CaseCounts.push_back(0);
299299
SmallVector<InstrProfValueData, 24> RemainingVDs;
@@ -316,15 +316,12 @@ bool MemOPSizeOpt::perform(MemOp MO) {
316316
break;
317317
}
318318

319-
if (V == LastV) {
320-
LLVM_DEBUG(dbgs() << "Invalid Profile Data in Function " << Func.getName()
321-
<< ": Two consecutive, identical values in MemOp value"
322-
"counts.\n");
319+
if (!SeenSizeId.insert(V).second) {
320+
errs() << "Invalid Profile Data in Function " << Func.getName()
321+
<< ": Two identical values in MemOp value counts.\n";
323322
return false;
324323
}
325324

326-
LastV = V;
327-
328325
SizeIds.push_back(V);
329326
CaseCounts.push_back(C);
330327
if (C > MaxCount)

llvm/test/Transforms/PGOProfile/consecutive-zeros.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
; REQUIRES: asserts
21
; RUN: llvm-profdata merge %S/Inputs/consecutive-zeros.proftext -o %t.profdata
3-
; RUN: opt < %s -debug -passes=pgo-instr-use,pgo-memop-opt -pgo-memop-count-threshold=0 -pgo-memop-percent-threshold=0 -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s
2+
; RUN: opt < %s -passes=pgo-instr-use,pgo-memop-opt -pgo-memop-count-threshold=0 -pgo-memop-percent-threshold=0 -pgo-test-profile-file=%t.profdata -S 2>&1 | FileCheck %s
43

54
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
65
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)