|
| 1 | +//===- ProfileVerify.cpp - Verify profile info for testing ----------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Transforms/Utils/ProfileVerify.h" |
| 10 | +#include "llvm/ADT/DynamicAPInt.h" |
| 11 | +#include "llvm/ADT/PostOrderIterator.h" |
| 12 | +#include "llvm/ADT/STLExtras.h" |
| 13 | +#include "llvm/Analysis/BranchProbabilityInfo.h" |
| 14 | +#include "llvm/Analysis/LoopInfo.h" |
| 15 | +#include "llvm/IR/Analysis.h" |
| 16 | +#include "llvm/IR/Dominators.h" |
| 17 | +#include "llvm/IR/Function.h" |
| 18 | +#include "llvm/IR/Instructions.h" |
| 19 | +#include "llvm/IR/LLVMContext.h" |
| 20 | +#include "llvm/IR/MDBuilder.h" |
| 21 | +#include "llvm/IR/ProfDataUtils.h" |
| 22 | +#include "llvm/Support/BranchProbability.h" |
| 23 | + |
| 24 | +using namespace llvm; |
| 25 | +namespace { |
| 26 | +class ProfileInjector { |
| 27 | + Function &F; |
| 28 | + FunctionAnalysisManager &FAM; |
| 29 | + |
| 30 | +public: |
| 31 | + static const Instruction * |
| 32 | + getTerminatorBenefitingFromMDProf(const BasicBlock &BB) { |
| 33 | + if (succ_size(&BB) < 2) |
| 34 | + return nullptr; |
| 35 | + auto *Term = BB.getTerminator(); |
| 36 | + return (isa<BranchInst>(Term) || isa<SwitchInst>(Term) || |
| 37 | + isa<IndirectBrInst>(Term) || isa<CallBrInst>(Term)) |
| 38 | + ? Term |
| 39 | + : nullptr; |
| 40 | + } |
| 41 | + |
| 42 | + static Instruction *getTerminatorBenefitingFromMDProf(BasicBlock &BB) { |
| 43 | + return const_cast<Instruction *>( |
| 44 | + getTerminatorBenefitingFromMDProf(const_cast<const BasicBlock &>(BB))); |
| 45 | + } |
| 46 | + |
| 47 | + ProfileInjector(Function &F, FunctionAnalysisManager &FAM) : F(F), FAM(FAM) {} |
| 48 | + bool inject(); |
| 49 | +}; |
| 50 | +} // namespace |
| 51 | + |
| 52 | +// FIXME: currently this injects only for terminators. Select isn't yet |
| 53 | +// supported. |
| 54 | +bool ProfileInjector::inject() { |
| 55 | + // Get whatever branch probability info can be derived from the given IR - |
| 56 | + // whether it has or not metadata. The main intention for this pass is to |
| 57 | + // ensure that other passes don't drop or "forget" to update MD_prof. We do |
| 58 | + // this as a mode in which lit tests would run. We want to avoid changing the |
| 59 | + // behavior of those tests. A pass may use BPI (or BFI, which is computed from |
| 60 | + // BPI). If no metadata is present, BPI is guesstimated by |
| 61 | + // BranchProbabilityAnalysis. The injector (this pass) only persists whatever |
| 62 | + // information the analysis provides, in other words, the pass being tested |
| 63 | + // will get the same BPI it does if the injector wasn't running. |
| 64 | + auto &BPI = FAM.getResult<BranchProbabilityAnalysis>(F); |
| 65 | + |
| 66 | + bool Changed = false; |
| 67 | + for (auto &BB : F) { |
| 68 | + auto *Term = getTerminatorBenefitingFromMDProf(BB); |
| 69 | + if (!Term || Term->getMetadata(LLVMContext::MD_prof)) |
| 70 | + continue; |
| 71 | + SmallVector<BranchProbability> Probs; |
| 72 | + Probs.reserve(Term->getNumSuccessors()); |
| 73 | + for (auto I = 0U, E = Term->getNumSuccessors(); I < E; ++I) |
| 74 | + Probs.emplace_back(BPI.getEdgeProbability(&BB, Term->getSuccessor(I))); |
| 75 | + |
| 76 | + assert(llvm::find_if(Probs, |
| 77 | + [](const BranchProbability &P) { |
| 78 | + return P.isUnknown(); |
| 79 | + }) == Probs.end() && |
| 80 | + "All branch probabilities should be valid"); |
| 81 | + const auto *FirstZeroDenominator = |
| 82 | + find_if(Probs, [](const BranchProbability &P) { |
| 83 | + return P.getDenominator() == 0; |
| 84 | + }); |
| 85 | + (void)FirstZeroDenominator; |
| 86 | + assert(FirstZeroDenominator == Probs.end()); |
| 87 | + const auto *FirstNonZeroNumerator = |
| 88 | + find_if(Probs, [](const BranchProbability &P) { return !P.isZero(); }); |
| 89 | + assert(FirstNonZeroNumerator != Probs.end()); |
| 90 | + DynamicAPInt LCM(Probs[0].getDenominator()); |
| 91 | + DynamicAPInt GCD(FirstNonZeroNumerator->getNumerator()); |
| 92 | + for (const auto &Prob : drop_begin(Probs)) { |
| 93 | + if (!Prob.getNumerator()) |
| 94 | + continue; |
| 95 | + LCM = llvm::lcm(LCM, DynamicAPInt(Prob.getDenominator())); |
| 96 | + GCD = llvm::gcd(GCD, DynamicAPInt(Prob.getNumerator())); |
| 97 | + } |
| 98 | + SmallVector<uint32_t> Weights; |
| 99 | + Weights.reserve(Term->getNumSuccessors()); |
| 100 | + for (const auto &Prob : Probs) { |
| 101 | + DynamicAPInt W = |
| 102 | + (Prob.getNumerator() * LCM / GCD) / Prob.getDenominator(); |
| 103 | + Weights.emplace_back(static_cast<uint32_t>((int64_t)W)); |
| 104 | + } |
| 105 | + setBranchWeights(*Term, Weights, /*IsExpected=*/false); |
| 106 | + Changed = true; |
| 107 | + } |
| 108 | + return Changed; |
| 109 | +} |
| 110 | + |
| 111 | +PreservedAnalyses ProfileInjectorPass::run(Function &F, |
| 112 | + FunctionAnalysisManager &FAM) { |
| 113 | + ProfileInjector PI(F, FAM); |
| 114 | + if (!PI.inject()) |
| 115 | + return PreservedAnalyses::all(); |
| 116 | + |
| 117 | + return PreservedAnalyses::none(); |
| 118 | +} |
| 119 | + |
| 120 | +PreservedAnalyses ProfileVerifierPass::run(Function &F, |
| 121 | + FunctionAnalysisManager &FAM) { |
| 122 | + for (const auto &BB : F) |
| 123 | + if (const auto *Term = |
| 124 | + ProfileInjector::getTerminatorBenefitingFromMDProf(BB)) |
| 125 | + if (!Term->getMetadata(LLVMContext::MD_prof)) |
| 126 | + F.getContext().emitError("Profile verification failed"); |
| 127 | + |
| 128 | + return PreservedAnalyses::none(); |
| 129 | +} |
0 commit comments