Index: llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -1422,7 +1422,8 @@ MaxCount = EdgeCount; EdgeCounts[SuccNum] = EdgeCount; } - setProfMetadata(M, TI, EdgeCounts, MaxCount); + if (MaxCount) + setProfMetadata(M, TI, EdgeCounts, MaxCount); } } Index: llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp +++ llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp @@ -440,7 +440,8 @@ DTU.applyUpdates(Updates); Updates.clear(); - setProfMetadata(Func.getParent(), SI, CaseCounts, MaxCount); + if (MaxCount) + setProfMetadata(Func.getParent(), SI, CaseCounts, MaxCount); LLVM_DEBUG(dbgs() << *BB << "\n"); LLVM_DEBUG(dbgs() << *DefaultBB << "\n"); Index: llvm/test/Transforms/PGOProfile/Inputs/maxcountzero.proftext =================================================================== --- /dev/null +++ llvm/test/Transforms/PGOProfile/Inputs/maxcountzero.proftext @@ -0,0 +1,16 @@ +# IR level Instrumentation Flag +:ir +foo +# Func Hash: +376260595403191007 +# Num Counters: +8 +# Counter Values: +0 +0 +10 +0 +0 +0 +0 +0 Index: llvm/test/Transforms/PGOProfile/maxcountzero.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/PGOProfile/maxcountzero.ll @@ -0,0 +1,80 @@ +; Make sure a degenerate profile does not cause an assertion. +; The profile consists of a non-zero count in a basic block and 0 counts +; in all succcessor blocks. + +; RUN: llvm-profdata merge %S/Inputs/maxcountzero.proftext -o %t.profdata +; RUN: opt < %s -passes=pgo-instr-use -pgo-instrument-entry=false -pgo-test-profile-file=%t.profdata -S | FileCheck %s + +%struct.II = type { ptr } +@RR = hidden global ptr null, align 8 +@kk = hidden global %struct.II zeroinitializer, align 8 + +define hidden void @foo(ptr noundef %ww) { +entry: + %tmp = alloca i32, align 4 + %ee = alloca i32, align 4 + %0 = load i32, ptr %ww, align 4 + %cmp = icmp sgt i32 %0, -1 + br i1 %cmp, label %if.end, label %if.then + +if.then: + br label %if.end + +if.end: + %stmxcsr = load i32, ptr %tmp, align 4 + %and = and i32 %stmxcsr, 32768 + %cmp2.not.not = icmp eq i32 %and, 0 + br i1 %cmp2.not.not, label %if.then3, label %if.end4 + +if.then3: + br label %if.end4 + +if.end4: + %call = call noundef zeroext i1 @_Z2NNv() + br i1 %call, label %if.end12, label %if.then5 + +if.then5: + %call6 = call noundef zeroext i1 @_Z2NNv() + br i1 %call6, label %if.then7, label %if.end11 + +if.then7: + %1 = load i32, ptr %ee, align 4 + %cmp8 = icmp eq i32 %1, 1 + br i1 %cmp8, label %if.then9, label %if.else + +if.then9: + br label %if.end11 + +if.else: + br label %if.end11 + +if.end11: + br label %if.end12 + +if.end12: + br label %for.cond.i + +for.cond.i: + %2 = load ptr, ptr @RR, align 8 + %tobool.not.i = icmp eq ptr %2, null + br i1 %tobool.not.i, label %if.end.i, label %if.then.i + +if.then.i: + %3 = load ptr, ptr @kk, align 8 + call void %3(ptr noundef null) + br label %if.end.i + +if.end.i: + br label %for.cond.i +} + +declare noundef zeroext i1 @_Z2NNv() local_unnamed_addr + +!llvm.module.flags = !{!0, !1, !2, !3} + +!0 = !{i32 1, !"wchar_size", i32 2} +!1 = !{i32 7, !"PIC Level", i32 2} +!2 = !{i32 7, !"uwtable", i32 2} +!3 = !{i32 7, !"frame-pointer", i32 1} + +; CHECK: define