|
| 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/LLVMContext.h" |
| 19 | +#include "llvm/IR/MDBuilder.h" |
| 20 | +#include "llvm/IR/ProfDataUtils.h" |
| 21 | +#include "llvm/Support/BranchProbability.h" |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | +namespace { |
| 25 | +class ProfileInjector { |
| 26 | + Function &F; |
| 27 | + FunctionAnalysisManager &FAM; |
| 28 | + |
| 29 | +public: |
| 30 | + static bool supportsBranchWeights(const Instruction &I) { |
| 31 | + return isa<BranchInst>(&I) || |
| 32 | + |
| 33 | + isa<SwitchInst>(&I) || |
| 34 | + |
| 35 | + isa<IndirectBrInst>(&I) || isa<SelectInst>(&I) || |
| 36 | + isa<CallBrInst>(&I); |
| 37 | + } |
| 38 | + |
| 39 | + ProfileInjector(Function &F, FunctionAnalysisManager &FAM) : F(F), FAM(FAM) {} |
| 40 | + bool inject(); |
| 41 | +}; |
| 42 | +} // namespace |
| 43 | + |
| 44 | +bool ProfileInjector::inject() { |
| 45 | + auto &BPI = FAM.getResult<BranchProbabilityAnalysis>(F); |
| 46 | + |
| 47 | + for (auto &BB : F) { |
| 48 | + if (succ_size(&BB) <= 1) |
| 49 | + continue; |
| 50 | + auto *Term = BB.getTerminator(); |
| 51 | + assert(Term); |
| 52 | + if (Term->getMetadata(LLVMContext::MD_prof) || |
| 53 | + !supportsBranchWeights(*Term)) |
| 54 | + continue; |
| 55 | + SmallVector<BranchProbability> Probs; |
| 56 | + Probs.reserve(Term->getNumSuccessors()); |
| 57 | + for (auto I = 0U, E = Term->getNumSuccessors(); I < E; ++I) |
| 58 | + Probs.emplace_back(BPI.getEdgeProbability(&BB, Term->getSuccessor(I))); |
| 59 | + |
| 60 | + const auto *FirstZeroDenominator = |
| 61 | + find_if(Probs, [](const BranchProbability &P) { |
| 62 | + return P.getDenominator() == 0; |
| 63 | + }); |
| 64 | + assert(FirstZeroDenominator == Probs.end()); |
| 65 | + const auto *FirstNonzeroNumerator = |
| 66 | + find_if(Probs, [](const BranchProbability &P) { |
| 67 | + return P.getNumerator() != 0; |
| 68 | + }); |
| 69 | + assert(FirstNonzeroNumerator != Probs.end()); |
| 70 | + DynamicAPInt LCM(Probs[0].getDenominator()); |
| 71 | + DynamicAPInt GCD(FirstNonzeroNumerator->getNumerator()); |
| 72 | + for (const auto &Prob : drop_begin(Probs)) { |
| 73 | + if (!Prob.getNumerator()) |
| 74 | + continue; |
| 75 | + LCM = llvm::lcm(LCM, DynamicAPInt(Prob.getDenominator())); |
| 76 | + GCD = llvm::lcm(GCD, DynamicAPInt(Prob.getNumerator())); |
| 77 | + } |
| 78 | + SmallVector<uint32_t> Weights; |
| 79 | + Weights.reserve(Term->getNumSuccessors()); |
| 80 | + for (const auto &Prob : Probs) { |
| 81 | + auto W = Prob.getNumerator() * LCM / GCD; |
| 82 | + Weights.emplace_back(static_cast<int32_t>((int64_t)W)); |
| 83 | + } |
| 84 | + setBranchWeights(*Term, Weights, false); |
| 85 | + } |
| 86 | + return true; |
| 87 | +} |
| 88 | + |
| 89 | +PreservedAnalyses ProfileInjectorPass::run(Function &F, |
| 90 | + FunctionAnalysisManager &FAM) { |
| 91 | + ProfileInjector PI(F, FAM); |
| 92 | + if (!PI.inject()) |
| 93 | + return PreservedAnalyses::all(); |
| 94 | + |
| 95 | + return PreservedAnalyses::none(); |
| 96 | +} |
| 97 | + |
| 98 | +PreservedAnalyses ProfileVerifierPass::run(Function &F, |
| 99 | + FunctionAnalysisManager &FAM) { |
| 100 | + bool Changed = false; |
| 101 | + for (auto &BB : F) |
| 102 | + if (succ_size(&BB) >= 2) |
| 103 | + if (auto *Term = BB.getTerminator()) |
| 104 | + if (ProfileInjector::supportsBranchWeights(*Term)) { |
| 105 | + if (!Term->getMetadata(LLVMContext::MD_prof)) { |
| 106 | + F.getContext().emitError("Profile verification failed"); |
| 107 | + } else { |
| 108 | + Changed = true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); |
| 113 | +} |
0 commit comments