Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:41

0001 //===- Transforms/IPO/SampleProfileProbe.h ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// This file provides the interface for the pseudo probe implementation for
0011 /// AutoFDO.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
0016 #define LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
0017 
0018 #include "llvm/Analysis/LazyCallGraph.h"
0019 #include "llvm/IR/PassManager.h"
0020 #include "llvm/IR/PassInstrumentation.h"
0021 #include "llvm/ProfileData/SampleProf.h"
0022 #include <unordered_map>
0023 
0024 namespace llvm {
0025 class BasicBlock;
0026 class Function;
0027 class Instruction;
0028 class Loop;
0029 class PassInstrumentationCallbacks;
0030 class TargetMachine;
0031 
0032 class Module;
0033 
0034 using namespace sampleprof;
0035 using BlockIdMap = std::unordered_map<BasicBlock *, uint32_t>;
0036 using InstructionIdMap = std::unordered_map<Instruction *, uint32_t>;
0037 // Map from tuples of Probe id and inline stack hash code to distribution
0038 // factors.
0039 using ProbeFactorMap = std::unordered_map<std::pair<uint64_t, uint64_t>, float,
0040                                           pair_hash<uint64_t, uint64_t>>;
0041 using FuncProbeFactorMap = StringMap<ProbeFactorMap>;
0042 
0043 
0044 // A pseudo probe verifier that can be run after each IR passes to detect the
0045 // violation of updating probe factors. In principle, the sum of distribution
0046 // factor for a probe should be identical before and after a pass. For a
0047 // function pass, the factor sum for a probe would be typically 100%.
0048 class PseudoProbeVerifier {
0049 public:
0050   void registerCallbacks(PassInstrumentationCallbacks &PIC);
0051 
0052   // Implementation of pass instrumentation callbacks for new pass manager.
0053   void runAfterPass(StringRef PassID, Any IR);
0054 
0055 private:
0056   // Allow a little bias due the rounding to integral factors.
0057   constexpr static float DistributionFactorVariance = 0.02f;
0058   // Distribution factors from last pass.
0059   FuncProbeFactorMap FunctionProbeFactors;
0060 
0061   void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
0062   void runAfterPass(const Module *M);
0063   void runAfterPass(const LazyCallGraph::SCC *C);
0064   void runAfterPass(const Function *F);
0065   void runAfterPass(const Loop *L);
0066   bool shouldVerifyFunction(const Function *F);
0067   void verifyProbeFactors(const Function *F,
0068                           const ProbeFactorMap &ProbeFactors);
0069 };
0070 
0071 /// Sample profile pseudo prober.
0072 ///
0073 /// Insert pseudo probes for block sampling and value sampling.
0074 class SampleProfileProber {
0075 public:
0076   // Give an empty module id when the prober is not used for instrumentation.
0077   SampleProfileProber(Function &F, const std::string &CurModuleUniqueId);
0078   void instrumentOneFunc(Function &F, TargetMachine *TM);
0079 
0080 private:
0081   Function *getFunction() const { return F; }
0082   uint64_t getFunctionHash() const { return FunctionHash; }
0083   uint32_t getBlockId(const BasicBlock *BB) const;
0084   uint32_t getCallsiteId(const Instruction *Call) const;
0085   void findUnreachableBlocks(DenseSet<BasicBlock *> &BlocksToIgnore);
0086   void findInvokeNormalDests(DenseSet<BasicBlock *> &InvokeNormalDests);
0087   void computeBlocksToIgnore(DenseSet<BasicBlock *> &BlocksToIgnore,
0088                              DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
0089   const Instruction *
0090   getOriginalTerminator(const BasicBlock *Head,
0091                         const DenseSet<BasicBlock *> &BlocksToIgnore);
0092   void computeCFGHash(const DenseSet<BasicBlock *> &BlocksToIgnore);
0093   void computeProbeId(const DenseSet<BasicBlock *> &BlocksToIgnore,
0094                       const DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
0095 
0096   Function *F;
0097 
0098   /// The current module ID that is used to name a static object as a comdat
0099   /// group.
0100   std::string CurModuleUniqueId;
0101 
0102   /// A CFG hash code used to identify a function code changes.
0103   uint64_t FunctionHash;
0104 
0105   /// Map basic blocks to the their pseudo probe ids.
0106   BlockIdMap BlockProbeIds;
0107 
0108   /// Map indirect calls to the their pseudo probe ids.
0109   InstructionIdMap CallProbeIds;
0110 
0111   /// The ID of the last probe, Can be used to number a new probe.
0112   uint32_t LastProbeId;
0113 };
0114 
0115 class SampleProfileProbePass : public PassInfoMixin<SampleProfileProbePass> {
0116   TargetMachine *TM;
0117 
0118 public:
0119   SampleProfileProbePass(TargetMachine *TM) : TM(TM) {}
0120   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0121 };
0122 
0123 // Pseudo probe distribution factor updater.
0124 // Sample profile annotation can happen in both LTO prelink and postlink. The
0125 // postlink-time re-annotation can degrade profile quality because of prelink
0126 // code duplication transformation, such as loop unrolling, jump threading,
0127 // indirect call promotion etc. As such, samples corresponding to a source
0128 // location may be aggregated multiple times in postlink. With a concept of
0129 // distribution factor for pseudo probes, samples can be distributed among
0130 // duplicated probes reasonable based on the assumption that optimizations
0131 // duplicating code well-maintain the branch frequency information (BFI). This
0132 // pass updates distribution factors for each pseudo probe at the end of the
0133 // prelink pipeline, to reflect an estimated portion of the real execution
0134 // count.
0135 class PseudoProbeUpdatePass : public PassInfoMixin<PseudoProbeUpdatePass> {
0136   void runOnFunction(Function &F, FunctionAnalysisManager &FAM);
0137 
0138 public:
0139   PseudoProbeUpdatePass() = default;
0140   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0141 };
0142 
0143 } // end namespace llvm
0144 #endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H