LLVM  13.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"
21 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/IR/PseudoProbe.h"
27 #include <unordered_map>
28 
29 namespace llvm {
30 
31 class Module;
32 
33 using namespace sampleprof;
34 using BlockIdMap = std::unordered_map<BasicBlock *, uint32_t>;
35 using InstructionIdMap = std::unordered_map<Instruction *, uint32_t>;
36 using ProbeFactorMap = std::unordered_map<uint64_t, float>;
38 
39 enum class PseudoProbeReservedId { Invalid = 0, Last = Invalid };
40 
42  uint64_t FunctionGUID;
43  uint64_t FunctionHash;
44 
45 public:
46  PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
47  : FunctionGUID(GUID), FunctionHash(Hash) {}
48  uint64_t getFunctionGUID() const { return FunctionGUID; }
49  uint64_t getFunctionHash() const { return FunctionHash; }
50 };
51 
52 // A pseudo probe verifier that can be run after each IR passes to detect the
53 // violation of updating probe factors. In principle, the sum of distribution
54 // factor for a probe should be identical before and after a pass. For a
55 // function pass, the factor sum for a probe would be typically 100%.
57 public:
58  void registerCallbacks(PassInstrumentationCallbacks &PIC);
59 
60  // Implementation of pass instrumentation callbacks for new pass manager.
61  void runAfterPass(StringRef PassID, Any IR);
62 
63 private:
64  // Allow a little bias due the rounding to integral factors.
65  constexpr static float DistributionFactorVariance = 0.02f;
66  // Distribution factors from last pass.
67  FuncProbeFactorMap FunctionProbeFactors;
68 
69  void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
70  void runAfterPass(const Module *M);
71  void runAfterPass(const LazyCallGraph::SCC *C);
72  void runAfterPass(const Function *F);
73  void runAfterPass(const Loop *L);
74  bool shouldVerifyFunction(const Function *F);
75  void verifyProbeFactors(const Function *F,
76  const ProbeFactorMap &ProbeFactors);
77 };
78 
79 // This class serves sample counts correlation for SampleProfileLoader by
80 // analyzing pseudo probes and their function descriptors injected by
81 // SampleProfileProber.
84 
85  const PseudoProbeDescriptor *getDesc(const Function &F) const;
86 
87 public:
88  PseudoProbeManager(const Module &M);
89  bool moduleIsProbed(const Module &M) const;
90  bool profileIsValid(const Function &F, const FunctionSamples &Samples) const;
91 };
92 
93 /// Sample profile pseudo prober.
94 ///
95 /// Insert pseudo probes for block sampling and value sampling.
97 public:
98  // Give an empty module id when the prober is not used for instrumentation.
99  SampleProfileProber(Function &F, const std::string &CurModuleUniqueId);
101 
102 private:
103  Function *getFunction() const { return F; }
104  uint64_t getFunctionHash() const { return FunctionHash; }
105  uint32_t getBlockId(const BasicBlock *BB) const;
106  uint32_t getCallsiteId(const Instruction *Call) const;
107  void computeCFGHash();
108  void computeProbeIdForBlocks();
109  void computeProbeIdForCallsites();
110 
111  Function *F;
112 
113  /// The current module ID that is used to name a static object as a comdat
114  /// group.
115  std::string CurModuleUniqueId;
116 
117  /// A CFG hash code used to identify a function code changes.
118  uint64_t FunctionHash;
119 
120  /// Map basic blocks to the their pseudo probe ids.
121  BlockIdMap BlockProbeIds;
122 
123  /// Map indirect calls to the their pseudo probe ids.
124  InstructionIdMap CallProbeIds;
125 
126  /// The ID of the last probe, Can be used to number a new probe.
127  uint32_t LastProbeId;
128 };
129 
130 class SampleProfileProbePass : public PassInfoMixin<SampleProfileProbePass> {
131  TargetMachine *TM;
132 
133 public:
136 };
137 
138 class PseudoProbeUpdatePass : public PassInfoMixin<PseudoProbeUpdatePass> {
140 
141 public:
144 };
145 
146 } // end namespace llvm
147 #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:155
llvm
Definition: AllocatorList.h:23
llvm::HexagonMCInstrInfo::getDesc
const MCInstrDesc & getDesc(MCInstrInfo const &MCII, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:248
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:130
llvm::SystemZISD::TM
@ TM
Definition: SystemZISelLowering.h:65
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:61
getFunction
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:253
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:530
llvm::PseudoProbeUpdatePass
Definition: SampleProfileProbe.h:138
DenseMap.h
llvm::SampleProfileProber
Sample profile pseudo prober.
Definition: SampleProfileProbe.h:96
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::InstructionIdMap
std::unordered_map< Instruction *, uint32_t > InstructionIdMap
Definition: SampleProfileProbe.h:35
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
TargetMachine.h
llvm::LazyCallGraph::SCC
An SCC of the call graph.
Definition: LazyCallGraph.h:421
llvm::PseudoProbeVerifier
Definition: SampleProfileProbe.h:56
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::SampleProfileProbePass::SampleProfileProbePass
SampleProfileProbePass(TargetMachine *TM)
Definition: SampleProfileProbe.h:134
PassInstrumentation.h
SampleProf.h
llvm::Instruction
Definition: Instruction.h:45
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:744
LazyCallGraph.h
llvm::StringMap< ProbeFactorMap >
LoopInfo.h
llvm::PseudoProbeUpdatePass::PseudoProbeUpdatePass
PseudoProbeUpdatePass()
Definition: SampleProfileProbe.h:142
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:894
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:530
llvm::PseudoProbeManager
Definition: SampleProfileProbe.h:82
llvm::DenseMap
Definition: DenseMap.h:714
llvm::BlockIdMap
std::unordered_map< BasicBlock *, uint32_t > BlockIdMap
Definition: SampleProfileProbe.h:34
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::PseudoProbeDescriptor
Definition: SampleProfileProbe.h:41
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::PseudoProbeDescriptor::getFunctionHash
uint64_t getFunctionHash() const
Definition: SampleProfileProbe.h:49
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
CallGraphSCCPass.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::Any
Definition: Any.h:26
PassManager.h
PseudoProbe.h
llvm::PseudoProbeDescriptor::PseudoProbeDescriptor
PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
Definition: SampleProfileProbe.h:46
llvm::PassInstrumentationCallbacks
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Definition: PassInstrumentation.h:65
llvm::PseudoProbeDescriptor::getFunctionGUID
uint64_t getFunctionGUID() const
Definition: SampleProfileProbe.h:48
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
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::ProbeFactorMap
std::unordered_map< uint64_t, float > ProbeFactorMap
Definition: SampleProfileProbe.h:36
llvm::PseudoProbeReservedId
PseudoProbeReservedId
Definition: SampleProfileProbe.h:39