Index: llvm/lib/ProfileData/InstrProfWriter.cpp =================================================================== --- llvm/lib/ProfileData/InstrProfWriter.cpp +++ llvm/lib/ProfileData/InstrProfWriter.cpp @@ -530,13 +530,10 @@ for (uint32_t S = 0; S < NS; S++) { uint32_t ND = Func.getNumValueDataForSite(VK, S); std::unique_ptr VD = Func.getValueForSite(VK, S); - bool WasZero = false; + DenseSet SeenValues; for (uint32_t I = 0; I < ND; I++) - if ((VK != IPVK_IndirectCallTarget) && (VD[I].Value == 0)) { - if (WasZero) - return make_error(instrprof_error::invalid_prof); - WasZero = true; - } + if ((VK != IPVK_IndirectCallTarget) && !SeenValues.insert(VD[I].Value).second) + return make_error(instrprof_error::invalid_prof); } } Index: llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp +++ llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp @@ -291,9 +291,9 @@ uint64_t SavedRemainCount = SavedTotalCount; SmallVector SizeIds; SmallVector CaseCounts; + SmallDenseSet SeenSizeId; uint64_t MaxCount = 0; unsigned Version = 0; - int64_t LastV = -1; // Default case is in the front -- save the slot here. CaseCounts.push_back(0); SmallVector RemainingVDs; @@ -316,15 +316,12 @@ break; } - if (V == LastV) { - LLVM_DEBUG(dbgs() << "Invalid Profile Data in Function " << Func.getName() - << ": Two consecutive, identical values in MemOp value" - "counts.\n"); + if (!SeenSizeId.insert(V).second) { + errs() << "Invalid Profile Data in Function " << Func.getName() + << ": Two identical values in MemOp value counts.\n"; return false; } - LastV = V; - SizeIds.push_back(V); CaseCounts.push_back(C); if (C > MaxCount) Index: llvm/test/Transforms/PGOProfile/Inputs/duplicate-values.proftext =================================================================== --- /dev/null +++ llvm/test/Transforms/PGOProfile/Inputs/duplicate-values.proftext @@ -0,0 +1,47 @@ +# IR level Instrumentation Flag +:ir +foo +# Func Hash: +687116424982578944 +# Num Counters: +3 +# Counter Values: +523 +20 +1 +# Num Value Kinds: +1 +# ValueKind = IPVK_MemOPSize: +1 +# NumValueSites: +3 +9 +1:99 +2:88 +1:77 +9:72 +4:66 +5:55 +2:44 +7:33 +8:22 +9 +7:33 +2:88 +9:72 +4:66 +1:99 +5:55 +6:44 +3:77 +8:22 +9 +7:33 +2:88 +9:72 +4:66 +1:99 +5:55 +6:44 +3:77 +8:22 Index: llvm/test/Transforms/PGOProfile/consecutive-zeros.ll =================================================================== --- llvm/test/Transforms/PGOProfile/consecutive-zeros.ll +++ llvm/test/Transforms/PGOProfile/consecutive-zeros.ll @@ -1,6 +1,5 @@ -; REQUIRES: asserts ; RUN: llvm-profdata merge %S/Inputs/consecutive-zeros.proftext -o %t.profdata -; 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 +; 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 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" Index: llvm/test/Transforms/PGOProfile/duplicate-memop-values.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/PGOProfile/duplicate-memop-values.ll @@ -0,0 +1,58 @@ +; RUN: llvm-profdata merge %S/Inputs/consecutive-zeros.proftext -o %t.profdata +; 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 + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @foo(i8* %dst, i8* %src, i32* %a, i32 %n) { +; CHECK: {{[Ii]}}nvalid {{[Pp]}}rofile +entry: + br label %for.cond + +for.cond: + %i.0 = phi i32 [ 0, %entry ], [ %inc5, %for.inc4 ] + %cmp = icmp slt i32 %i.0, %n + br i1 %cmp, label %for.body, label %for.end6 + +for.body: + br label %for.cond1 + +for.cond1: + %j.0 = phi i32 [ 0, %for.body ], [ %inc, %for.inc ] + %idx.ext = sext i32 %i.0 to i64 + %add.ptr = getelementptr inbounds i32, i32* %a, i64 %idx.ext + %0 = load i32, i32* %add.ptr, align 4 + %cmp2 = icmp slt i32 %j.0, %0 + br i1 %cmp2, label %for.body3, label %for.end + +for.body3: + %add = add nsw i32 %i.0, 1 + %conv = sext i32 %add to i64 + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %conv, i1 false) + %memcmp = call i32 @memcmp(i8* %dst, i8* %src, i64 %conv) + %bcmp = call i32 @bcmp(i8* %dst, i8* %src, i64 %conv) + br label %for.inc + +for.inc: + %inc = add nsw i32 %j.0, 1 + br label %for.cond1 + +for.end: + br label %for.inc4 + +for.inc4: + %inc5 = add nsw i32 %i.0, 1 + br label %for.cond + +for.end6: + ret void +} + +declare void @llvm.lifetime.start(i64, i8* nocapture) + +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1) + +declare i32 @memcmp(i8*, i8*, i64) +declare i32 @bcmp(i8*, i8*, i64) + +declare void @llvm.lifetime.end(i64, i8* nocapture) Index: llvm/test/tools/llvm-profdata/invalid-profile-gen-dup-entries.proftext =================================================================== --- /dev/null +++ llvm/test/tools/llvm-profdata/invalid-profile-gen-dup-entries.proftext @@ -0,0 +1,30 @@ +# RUN: llvm-profdata merge --text -j 4 %s %s %s %s -o %t 2>&1 | FileCheck %s +# RUN: llvm-profdata merge --binary -j 4 %s %s %s %s -o %t 2>&1 | FileCheck %s +# IR level Instrumentation Flag +# CHECK: warning: invalid profile created +:ir +foo +# Func Hash: +35277121310 +# Num Counters: +3 +# Counter Values: +20 +556 +1 +# Num Value Kinds: +1 +# ValueKind = IPVK_MemOPSize: +1 +# NumValueSites: +1 +9 +1:99 +3:88 +1:77 +9:72 +4:66 +5:55 +6:44 +7:33 +8:22