LLVM  15.0.0git
SampleProfileProbe.h
Go to the documentation of this file.
1 //===- Transforms/IPO/SampleProfileProbe.h ----------*- C++ -*-===//
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 /// \file
10 /// This file provides the interface for the pseudo probe implementation for
11 /// AutoFDO.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
16 #define LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
17 
18 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/PassManager.h"
22 #include <unordered_map>
23 
24 namespace llvm {
25 class Any;
26 class BasicBlock;
27 class Function;
28 class Instruction;
29 class Loop;
30 class PassInstrumentationCallbacks;
31 class TargetMachine;
32 
33 class Module;
34 
35 using namespace sampleprof;
36 using BlockIdMap = std::unordered_map<BasicBlock *, uint32_t>;
37 using InstructionIdMap = std::unordered_map<Instruction *, uint32_t>;
38 // Map from tuples of Probe id and inline stack hash code to distribution
39 // factors.
40 using ProbeFactorMap = std::unordered_map<std::pair<uint64_t, uint64_t>, float,
43 
44 enum class PseudoProbeReservedId { Invalid = 0, Last = Invalid };
45 
47  uint64_t FunctionGUID;
48  uint64_t FunctionHash;
49 
50 public:
52  : FunctionGUID(GUID), FunctionHash(Hash) {}
53  uint64_t getFunctionGUID() const { return FunctionGUID; }
54  uint64_t getFunctionHash() const { return FunctionHash; }
55 };
56 
57 // A pseudo probe verifier that can be run after each IR passes to detect the
58 // violation of updating probe factors. In principle, the sum of distribution
59 // factor for a probe should be identical before and after a pass. For a
60 // function pass, the factor sum for a probe would be typically 100%.
62 public:
64 
65  // Implementation of pass instrumentation callbacks for new pass manager.
66  void runAfterPass(StringRef PassID, Any IR);
67 
68 private:
69  // Allow a little bias due the rounding to integral factors.
70  constexpr static float DistributionFactorVariance = 0.02f;
71  // Distribution factors from last pass.
72  FuncProbeFactorMap FunctionProbeFactors;
73 
74  void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
75  void runAfterPass(const Module *M);
76  void runAfterPass(const LazyCallGraph::SCC *C);
77  void runAfterPass(const Function *F);
78  void runAfterPass(const Loop *L);
79  bool shouldVerifyFunction(const Function *F);
80  void verifyProbeFactors(const Function *F,
81  const ProbeFactorMap &ProbeFactors);
82 };
83 
84 // This class serves sample counts correlation for SampleProfileLoader by
85 // analyzing pseudo probes and their function descriptors injected by
86 // SampleProfileProber.
89 
90  const PseudoProbeDescriptor *getDesc(const Function &F) const;
91 
92 public:
93  PseudoProbeManager(const Module &M);
94  bool moduleIsProbed(const Module &M) const;
95  bool profileIsValid(const Function &F, const FunctionSamples &Samples) const;
96 };
97 
98 /// Sample profile pseudo prober.
99 ///
100 /// Insert pseudo probes for block sampling and value sampling.
102 public:
103  // Give an empty module id when the prober is not used for instrumentation.
104  SampleProfileProber(Function &F, const std::string &CurModuleUniqueId);
106 
107 private:
108  Function *getFunction() const { return F; }
109  uint64_t getFunctionHash() const { return FunctionHash; }
110  uint32_t getBlockId(const BasicBlock *BB) const;
111  uint32_t getCallsiteId(const Instruction *Call) const;
112  void computeCFGHash();
113  void computeProbeIdForBlocks();
114  void computeProbeIdForCallsites();
115 
116  Function *F;
117 
118  /// The current module ID that is used to name a static object as a comdat
119  /// group.
120  std::string CurModuleUniqueId;
121 
122  /// A CFG hash code used to identify a function code changes.
123  uint64_t FunctionHash;
124 
125  /// Map basic blocks to the their pseudo probe ids.
126  BlockIdMap BlockProbeIds;
127 
128  /// Map indirect calls to the their pseudo probe ids.
129  InstructionIdMap CallProbeIds;
130 
131  /// The ID of the last probe, Can be used to number a new probe.
132  uint32_t LastProbeId;
133 };
134 
135 class SampleProfileProbePass : public PassInfoMixin<SampleProfileProbePass> {
136  TargetMachine *TM;
137 
138 public:
141 };
142 
143 // Pseudo probe distribution factor updater.
144 // Sample profile annotation can happen in both LTO prelink and postlink. The
145 // postlink-time re-annotation can degrade profile quality because of prelink
146 // code duplication transformation, such as loop unrolling, jump threading,
147 // indirect call promotion etc. As such, samples corresponding to a source
148 // location may be aggregated multiple times in postlink. With a concept of
149 // distribution factor for pseudo probes, samples can be distributed among
150 // duplicated probes reasonable based on the assumption that optimizations
151 // duplicating code well-maintain the branch frequency information (BFI). This
152 // pass updates distribution factors for each pseudo probe at the end of the
153 // prelink pipeline, to reflect an estimated portion of the real execution
154 // count.
155 class PseudoProbeUpdatePass : public PassInfoMixin<PseudoProbeUpdatePass> {
157 
158 public:
159  PseudoProbeUpdatePass() = default;
161 };
162 
163 } // end namespace llvm
164 #endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::HexagonMCInstrInfo::getDesc
const MCInstrDesc & getDesc(MCInstrInfo const &MCII, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:255
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::SampleProfileProbePass
Definition: SampleProfileProbe.h:135
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
getFunction
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:233
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:546
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::PseudoProbeUpdatePass
Definition: SampleProfileProbe.h:155
DenseMap.h
PIC
PassInstrumentationCallbacks PIC
Definition: PassBuilderBindings.cpp:55
llvm::SampleProfileProber
Sample profile pseudo prober.
Definition: SampleProfileProbe.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::InstructionIdMap
std::unordered_map< Instruction *, uint32_t > InstructionIdMap
Definition: SampleProfileProbe.h:37
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::LazyCallGraph::SCC
An SCC of the call graph.
Definition: LazyCallGraph.h:419
llvm::PseudoProbeVerifier
Definition: SampleProfileProbe.h:61
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::SampleProfileProbePass::SampleProfileProbePass
SampleProfileProbePass(TargetMachine *TM)
Definition: SampleProfileProbe.h:139
SampleProf.h
llvm::Instruction
Definition: Instruction.h:42
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:751
LazyCallGraph.h
llvm::StringMap< ProbeFactorMap >
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition: PointerTypeAnalysis.cpp:101
instrumentOneFunc
static void instrumentOneFunc(Function &F, Module *M, TargetLibraryInfo &TLI, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI, std::unordered_multimap< Comdat *, GlobalValue * > &ComdatMembers, bool IsCS)
Definition: PGOInstrumentation.cpp:818
uint64_t
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:720
llvm::PseudoProbeManager
Definition: SampleProfileProbe.h:87
llvm::DenseMap
Definition: DenseMap.h:716
llvm::pair_hash
Definition: STLExtras.h:1928
llvm::BlockIdMap
std::unordered_map< BasicBlock *, uint32_t > BlockIdMap
Definition: SampleProfileProbe.h:36
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::PseudoProbeDescriptor
Definition: SampleProfileProbe.h:46
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::PseudoProbeDescriptor::getFunctionHash
uint64_t getFunctionHash() const
Definition: SampleProfileProbe.h:54
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
registerCallbacks
SI registerCallbacks(PIC, &FAM)
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::Any
Definition: Any.h:28
llvm::ProbeFactorMap
std::unordered_map< std::pair< uint64_t, uint64_t >, float, pair_hash< uint64_t, uint64_t > > ProbeFactorMap
Definition: SampleProfileProbe.h:41
PassManager.h
llvm::PseudoProbeDescriptor::PseudoProbeDescriptor
PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
Definition: SampleProfileProbe.h:51
llvm::PassInstrumentationCallbacks
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Definition: PassInstrumentation.h:66
llvm::SystemZICMP::Any
@ Any
Definition: SystemZISelLowering.h:377
llvm::PseudoProbeDescriptor::getFunctionGUID
uint64_t getFunctionGUID() const
Definition: SampleProfileProbe.h:53
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::PseudoProbeReservedId
PseudoProbeReservedId
Definition: SampleProfileProbe.h:44
llvm::codeview::PublicSymFlags::Function
@ Function