-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[PGO] Add ProfileInjector and ProfileVerifier passes #147388
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===- ProfileVerify.h - Verify profile info for testing ----------*-C++-*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Inject profile information, as part of tests, to verify passes don't | ||
// accidentally drop it. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_TRANSFORMS_UTILS_PROFILEVERIFY_H | ||
#define LLVM_TRANSFORMS_UTILS_PROFILEVERIFY_H | ||
|
||
#include "llvm/IR/Analysis.h" | ||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
/// Inject MD_prof metadata where it's missing. Used for testing that passes | ||
/// don't accidentally drop this metadata. | ||
class ProfileInjectorPass : public PassInfoMixin<ProfileInjectorPass> { | ||
public: | ||
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM); | ||
}; | ||
|
||
/// Checks that MD_prof is present on every instruction that supports it. Used | ||
/// in conjunction with the ProfileInjectorPass. MD_prof "unknown" is considered | ||
/// valid (i.e. !{!"unknown"}) | ||
class ProfileVerifierPass : public PassInfoMixin<ProfileVerifierPass> { | ||
public: | ||
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM); | ||
}; | ||
|
||
} // namespace llvm | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//===- ProfileVerify.cpp - Verify profile info for testing ----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Utils/ProfileVerify.h" | ||
#include "llvm/ADT/DynamicAPInt.h" | ||
#include "llvm/ADT/PostOrderIterator.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/Analysis/BranchProbabilityInfo.h" | ||
#include "llvm/Analysis/LoopInfo.h" | ||
#include "llvm/IR/Analysis.h" | ||
#include "llvm/IR/Dominators.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/MDBuilder.h" | ||
#include "llvm/IR/ProfDataUtils.h" | ||
#include "llvm/Support/BranchProbability.h" | ||
|
||
using namespace llvm; | ||
namespace { | ||
class ProfileInjector { | ||
Function &F; | ||
FunctionAnalysisManager &FAM; | ||
|
||
public: | ||
static const Instruction * | ||
getTerminatorBenefitingFromMDProf(const BasicBlock &BB) { | ||
if (succ_size(&BB) < 2) | ||
return nullptr; | ||
auto *Term = BB.getTerminator(); | ||
return (isa<BranchInst>(Term) || isa<SwitchInst>(Term) || | ||
isa<IndirectBrInst>(Term) || isa<SelectInst>(Term) || | ||
isa<CallBrInst>(Term)) | ||
? Term | ||
: nullptr; | ||
} | ||
static Instruction *getTerminatorBenefitingFromMDProf(BasicBlock &BB) { | ||
return const_cast<Instruction *>( | ||
getTerminatorBenefitingFromMDProf(const_cast<const BasicBlock &>(BB))); | ||
} | ||
|
||
ProfileInjector(Function &F, FunctionAnalysisManager &FAM) : F(F), FAM(FAM) {} | ||
bool inject(); | ||
}; | ||
} // namespace | ||
|
||
bool ProfileInjector::inject() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should decouple the mechanism to inject synthetic weights from the heuristics to generate synthetic weights. For example, I imagine could have a base class ProfileWeightsGenerator which is derived by different generators such as the current heuristic, profi, fuzzing generated inputs etc. This class would then pick which generator to use when injecting profile weights. Wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's the intention, but no point in adding abstractions before we want to add a second profile source. Right now, the goal is to get what's needed to have the system described in the first phase in the RFC - checking profile data isn't dropped (so a binary check). In the next phase, when we want to evaluate its numerical preservation, we will start wanting to have such pluggability. It's also debatable where that abstraction should live. I could see it be a mode of the BPI analysis rather. |
||
auto &BPI = FAM.getResult<BranchProbabilityAnalysis>(F); | ||
|
||
bool Changed = false; | ||
for (auto &BB : F) { | ||
auto *Term = getTerminatorBenefitingFromMDProf(BB); | ||
if (!Term) | ||
continue; | ||
if (Term->getMetadata(LLVMContext::MD_prof)) | ||
continue; | ||
SmallVector<BranchProbability> Probs; | ||
Probs.reserve(Term->getNumSuccessors()); | ||
for (auto I = 0U, E = Term->getNumSuccessors(); I < E; ++I) | ||
Probs.emplace_back(BPI.getEdgeProbability(&BB, Term->getSuccessor(I))); | ||
|
||
const auto *FirstZeroDenominator = | ||
find_if(Probs, [](const BranchProbability &P) { | ||
return P.getDenominator() == 0; | ||
}); | ||
Comment on lines
+67
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard with NDEBUG? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, because only used in assert - done (something similar) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cast FirstZeroDenominator to void, which would suppress any unused warning. I was thinking of avoiding useless code execution... unless we can trust the compiler will optimize it out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole expression is const, so it should be optimizable out. I'd prefer relying on that, and fixing the DCE that if that's not the case. |
||
(void)FirstZeroDenominator; | ||
assert(FirstZeroDenominator == Probs.end()); | ||
const auto *FirstNonzeroNumerator = | ||
find_if(Probs, [](const BranchProbability &P) { | ||
return P.getNumerator() != 0; | ||
}); | ||
assert(FirstNonzeroNumerator != Probs.end()); | ||
DynamicAPInt LCM(Probs[0].getDenominator()); | ||
DynamicAPInt GCD(FirstNonzeroNumerator->getNumerator()); | ||
for (const auto &Prob : drop_begin(Probs)) { | ||
if (!Prob.getNumerator()) | ||
continue; | ||
LCM = llvm::lcm(LCM, DynamicAPInt(Prob.getDenominator())); | ||
mtrofin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GCD = llvm::lcm(GCD, DynamicAPInt(Prob.getNumerator())); | ||
} | ||
SmallVector<uint32_t> Weights; | ||
Weights.reserve(Term->getNumSuccessors()); | ||
for (const auto &Prob : Probs) { | ||
auto W = Prob.getNumerator() * LCM / GCD; | ||
Weights.emplace_back(static_cast<int32_t>((int64_t)W)); | ||
} | ||
setBranchWeights(*Term, Weights, false); | ||
Changed = true; | ||
} | ||
return Changed; | ||
} | ||
|
||
PreservedAnalyses ProfileInjectorPass::run(Function &F, | ||
FunctionAnalysisManager &FAM) { | ||
ProfileInjector PI(F, FAM); | ||
if (!PI.inject()) | ||
return PreservedAnalyses::all(); | ||
|
||
return PreservedAnalyses::none(); | ||
} | ||
|
||
PreservedAnalyses ProfileVerifierPass::run(Function &F, | ||
FunctionAnalysisManager &FAM) { | ||
for (const auto &BB : F) | ||
if (const auto *Term = | ||
ProfileInjector::getTerminatorBenefitingFromMDProf(BB)) | ||
if (!Term->getMetadata(LLVMContext::MD_prof)) | ||
F.getContext().emitError("Profile verification failed"); | ||
|
||
return PreservedAnalyses::none(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
; Test that prof-inject only injects missing metadata | ||
|
||
; 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 | ||
yes2: | ||
ret void | ||
no: | ||
ret void | ||
} | ||
|
||
!0 = !{!"branch_weights", i32 1, i32 2} | ||
; CHECK: br i1 %c, label %yes, label %no, !prof !0 | ||
mtrofin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; CHECK: br i1 %c, label %yes2, label %no, !prof !1 | ||
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2} | ||
; CHECK: !1 = !{!"branch_weights", i32 429496729, i32 715827882} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you conceptually explain to me where these weights come from, or give me a link? I didn't follow the calculation in inject. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In inject, we get the Branch Probability Info. If there's no profile info, that analysis will compute some guesstimate (we can use profi later, but same idea). Inject will turn around and insert those branch probabilities as metadata. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was looking for an explanation of the guesstimate. Because I don't understand inject's calculation, these new branch weights look arbitrary to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! I'd look at So right now these are those numbers passes would see when looking at BPI, serialized in metadata. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
; 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) { | ||
%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: br i1 %c, label %yes, label %no, !prof !0 | ||
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2} | ||
; CHECK: !1 = !{!"unknown"} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
; Test prof-inject and prof-verify | ||
|
||
; RUN: opt -passes=prof-inject %s -S -o - | FileCheck %s --check-prefix=INJECT | ||
; RUN: not opt -passes=prof-verify %s -S -o - 2>&1 | FileCheck %s --check-prefix=VERIFY | ||
; RUN: opt -passes=prof-inject,prof-verify %s --disable-output | ||
|
||
define void @foo(i32 %i) { | ||
%c = icmp eq i32 %i, 0 | ||
br i1 %c, label %yes, label %no | ||
yes: | ||
ret void | ||
no: | ||
ret void | ||
} | ||
|
||
; INJECT: br i1 %c, label %yes, label %no, !prof !0 | ||
; INJECT: !0 = !{!"branch_weights", i32 429496729, i32 715827882} | ||
|
||
; VERIFY: Profile verification failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can SamplePGO's weight propagation be reused here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(capturing offline chat) this would be e.g. profi, and yes, we could use it in a number of ways:
MD_prof unknown
cases (I think these 2 items basically amount to the same thing - improving the BPI estimator)