Skip to content

[PGO][profcheck] ignore explicitly cold functions #151778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions llvm/lib/Transforms/Utils/ProfileVerify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"

using namespace llvm;
static cl::opt<int64_t>
DefaultFunctionEntryCount("profcheck-default-function-entry-count",
cl::init(1000));
namespace {
class ProfileInjector {
Function &F;
Expand Down Expand Up @@ -63,6 +67,19 @@ bool ProfileInjector::inject() {
// will get the same BPI it does if the injector wasn't running.
auto &BPI = FAM.getResult<BranchProbabilityAnalysis>(F);

// Inject a function count if there's none. It's reasonable for a pass to
// want to clear the MD_prof of a function with zero entry count. If the
// original profile (iFDO or AFDO) is empty for a function, it's simpler to
// require assigning it the 0-entry count explicitly than to mark every branch
// as cold (we do want some explicit information in the spirit of what this
// verifier wants to achieve - make dropping / corrupting MD_prof
// unit-testable)
if (!F.getEntryCount(/*AllowSynthetic=*/true))
F.setEntryCount(DefaultFunctionEntryCount);
// If there is an entry count that's 0, then don't bother injecting. We won't
// verify these either.
if (F.getEntryCount(/*AllowSynthetic=*/true)->getCount() == 0)
return false;
bool Changed = false;
for (auto &BB : F) {
auto *Term = getTerminatorBenefitingFromMDProf(BB);
Expand Down Expand Up @@ -119,11 +136,20 @@ PreservedAnalyses ProfileInjectorPass::run(Function &F,

PreservedAnalyses ProfileVerifierPass::run(Function &F,
FunctionAnalysisManager &FAM) {
const auto EntryCount = F.getEntryCount(/*AllowSynthetic=*/true);
if (!EntryCount) {
F.getContext().emitError("Profile verification failed: function entry "
"count missing (set to 0 if cold)");
return PreservedAnalyses::all();
}
if (EntryCount->getCount() == 0)
return PreservedAnalyses::all();
for (const auto &BB : F)
if (const auto *Term =
ProfileInjector::getTerminatorBenefitingFromMDProf(BB))
if (!Term->getMetadata(LLVMContext::MD_prof))
F.getContext().emitError("Profile verification failed");
F.getContext().emitError(
"Profile verification failed: branch annotation missing");

return PreservedAnalyses::none();
return PreservedAnalyses::all();
}
22 changes: 22 additions & 0 deletions llvm/test/Transforms/PGOProfile/prof-inject-existing.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; Test that prof-inject does not modify existing metadata (incl. "unknown")

; RUN: opt -passes=prof-inject %s -S -o - | FileCheck %s

define void @foo(i32 %i) {
%c = icmp eq i32 %i, 0
br i1 %c, label %yes, label %no, !prof !0
yes:
br i1 %c, label %yes2, label %no, !prof !1
yes2:
ret void
no:
ret void
}

!0 = !{!"branch_weights", i32 1, i32 2}
!1 = !{!"unknown"}
; CHECK: define void @foo(i32 %i) !prof !0
; CHECK: br i1 %c, label %yes, label %no, !prof !1
; CHECK: !0 = !{!"function_entry_count", i64 1000}
; CHECK: !1 = !{!"branch_weights", i32 1, i32 2}
; CHECK: !2 = !{!"unknown"}
28 changes: 23 additions & 5 deletions llvm/test/Transforms/PGOProfile/prof-verify-as-needed.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; Test that prof-inject only injects missing metadata

; RUN: opt -passes=prof-inject %s -S -o - | FileCheck %s
; RUN: opt -passes=prof-inject -profcheck-default-function-entry-count=10 %s -S -o - | FileCheck %s

define void @foo(i32 %i) {
%c = icmp eq i32 %i, 0
Expand All @@ -13,8 +13,26 @@ no:
ret void
}

define void @cold(i32 %i) !prof !1 {
%c = icmp eq i32 %i, 0
br i1 %c, label %yes, label %no
yes:
br i1 %c, label %yes2, label %no
yes2:
ret void
no:
ret void
}
!0 = !{!"branch_weights", i32 1, i32 2}
; CHECK: br i1 %c, label %yes, label %no, !prof !0
; CHECK: br i1 %c, label %yes2, label %no, !prof !1
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2}
; CHECK: !1 = !{!"branch_weights", i32 3, i32 5}
!1 = !{!"function_entry_count", i32 0}

; CHECK-LABEL: @foo
; CHECK: br i1 %c, label %yes, label %no, !prof !1
; CHECK: br i1 %c, label %yes2, label %no, !prof !2
; CHECK-LABEL: @cold
; CHECK: br i1 %c, label %yes, label %no{{$}}
; CHECK: br i1 %c, label %yes2, label %no{{$}}
; CHECK: !0 = !{!"function_entry_count", i64 10}
; CHECK: !1 = !{!"branch_weights", i32 1, i32 2}
; CHECK: !2 = !{!"branch_weights", i32 3, i32 5}
; CHECK: !3 = !{!"function_entry_count", i32 0}
20 changes: 11 additions & 9 deletions llvm/test/Transforms/PGOProfile/prof-verify-existing.ll
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
; Test that prof-inject does not modify existing metadata (incl. "unknown")

; RUN: opt -passes=prof-inject %s -S -o - | FileCheck %s
; RUN: opt -passes=prof-verify %s -S --disable-output

define void @foo(i32 %i) {
define void @foo(i32 %i) !prof !0 {
%c = icmp eq i32 %i, 0
br i1 %c, label %yes, label %no, !prof !0
br i1 %c, label %yes, label %no, !prof !1
yes:
br i1 %c, label %yes2, label %no, !prof !1
br i1 %c, label %yes2, label %no, !prof !2
yes2:
ret void
no:
ret void
}

!0 = !{!"branch_weights", i32 1, i32 2}
!1 = !{!"unknown"}
; CHECK: br i1 %c, label %yes, label %no, !prof !0
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2}
; CHECK: !1 = !{!"unknown"}
!0 = !{!"function_entry_count", i32 1}
!1 = !{!"branch_weights", i32 1, i32 2}
!2 = !{!"unknown"}
; CHECK: define void @foo(i32 %i) !prof !0
; CHECK: br i1 %c, label %yes, label %no, !prof !1
; CHECK: !0 = !{!"function_entry_count", i64 1}
; CHECK: !1 = !{!"branch_weights", i32 1, i32 2}
; CHECK: !2 = !{!"unknown"}
15 changes: 15 additions & 0 deletions llvm/test/Transforms/PGOProfile/prof-verify-known-cold.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; Test prof-verify for functions explicitly marked as cold

; RUN: opt -passes=prof-inject,prof-verify %s -o - 2>&1 | FileCheck %s

define void @foo(i32 %i) !prof !0 {
%c = icmp eq i32 %i, 0
br i1 %c, label %yes, label %no
yes:
ret void
no:
ret void
}
!0 = !{!"function_entry_count", i32 0}

; CHECK-NOT: Profile verification failed
14 changes: 14 additions & 0 deletions llvm/test/Transforms/PGOProfile/prof-verify-no-entrycount.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; Test prof-verify for functions without entry count

; RUN: not opt -passes=prof-verify %s -o - 2>&1 | FileCheck %s

define void @foo(i32 %i) {
%c = icmp eq i32 %i, 0
br i1 %c, label %yes, label %no
yes:
ret void
no:
ret void
}

; CHECK: Profile verification failed: function entry count missing (set to 0 if cold)
9 changes: 5 additions & 4 deletions llvm/test/Transforms/PGOProfile/prof-verify.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
; RUN: opt -passes=prof-inject,prof-verify %s --disable-output
; RUN: opt -enable-profcheck %s -S -o - | FileCheck %s --check-prefix=INJECT

define void @foo(i32 %i) {
define void @foo(i32 %i) !prof !0 {
%c = icmp eq i32 %i, 0
br i1 %c, label %yes, label %no
yes:
ret void
no:
ret void
}
!0 = !{!"function_entry_count", i32 1}

; INJECT: br i1 %c, label %yes, label %no, !prof !0
; INJECT: !0 = !{!"branch_weights", i32 3, i32 5}
; INJECT: br i1 %c, label %yes, label %no, !prof !1
; INJECT: !1 = !{!"branch_weights", i32 3, i32 5}

; VERIFY: Profile verification failed
; VERIFY: Profile verification failed: branch annotation missing