LLVM 17.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
24namespace llvm {
25class Any;
26class BasicBlock;
27class Function;
28class Instruction;
29class Loop;
30class PassInstrumentationCallbacks;
31class TargetMachine;
32
33class Module;
34
35using namespace sampleprof;
36using BlockIdMap = std::unordered_map<BasicBlock *, uint32_t>;
37using InstructionIdMap = std::unordered_map<Instruction *, uint32_t>;
38// Map from tuples of Probe id and inline stack hash code to distribution
39// factors.
40using ProbeFactorMap = std::unordered_map<std::pair<uint64_t, uint64_t>, float,
43
45 uint64_t FunctionGUID;
46 uint64_t FunctionHash;
47
48public:
50 : FunctionGUID(GUID), FunctionHash(Hash) {}
51 uint64_t getFunctionGUID() const { return FunctionGUID; }
52 uint64_t getFunctionHash() const { return FunctionHash; }
53};
54
55// A pseudo probe verifier that can be run after each IR passes to detect the
56// violation of updating probe factors. In principle, the sum of distribution
57// factor for a probe should be identical before and after a pass. For a
58// function pass, the factor sum for a probe would be typically 100%.
60public:
62
63 // Implementation of pass instrumentation callbacks for new pass manager.
64 void runAfterPass(StringRef PassID, Any IR);
65
66private:
67 // Allow a little bias due the rounding to integral factors.
68 constexpr static float DistributionFactorVariance = 0.02f;
69 // Distribution factors from last pass.
70 FuncProbeFactorMap FunctionProbeFactors;
71
72 void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
73 void runAfterPass(const Module *M);
75 void runAfterPass(const Function *F);
76 void runAfterPass(const Loop *L);
77 bool shouldVerifyFunction(const Function *F);
78 void verifyProbeFactors(const Function *F,
79 const ProbeFactorMap &ProbeFactors);
80};
81
82// This class serves sample counts correlation for SampleProfileLoader by
83// analyzing pseudo probes and their function descriptors injected by
84// SampleProfileProber.
87
88 const PseudoProbeDescriptor *getDesc(const Function &F) const;
89
90public:
91 PseudoProbeManager(const Module &M);
92 bool moduleIsProbed(const Module &M) const;
93 bool profileIsValid(const Function &F, const FunctionSamples &Samples) const;
94};
95
96/// Sample profile pseudo prober.
97///
98/// Insert pseudo probes for block sampling and value sampling.
100public:
101 // Give an empty module id when the prober is not used for instrumentation.
102 SampleProfileProber(Function &F, const std::string &CurModuleUniqueId);
104
105private:
106 Function *getFunction() const { return F; }
107 uint64_t getFunctionHash() const { return FunctionHash; }
108 uint32_t getBlockId(const BasicBlock *BB) const;
109 uint32_t getCallsiteId(const Instruction *Call) const;
110 void computeCFGHash();
111 void computeProbeIdForBlocks();
112 void computeProbeIdForCallsites();
113
114 Function *F;
115
116 /// The current module ID that is used to name a static object as a comdat
117 /// group.
118 std::string CurModuleUniqueId;
119
120 /// A CFG hash code used to identify a function code changes.
121 uint64_t FunctionHash;
122
123 /// Map basic blocks to the their pseudo probe ids.
124 BlockIdMap BlockProbeIds;
125
126 /// Map indirect calls to the their pseudo probe ids.
127 InstructionIdMap CallProbeIds;
128
129 /// The ID of the last probe, Can be used to number a new probe.
130 uint32_t LastProbeId;
131};
132
133class SampleProfileProbePass : public PassInfoMixin<SampleProfileProbePass> {
134 TargetMachine *TM;
135
136public:
139};
140
141// Pseudo probe distribution factor updater.
142// Sample profile annotation can happen in both LTO prelink and postlink. The
143// postlink-time re-annotation can degrade profile quality because of prelink
144// code duplication transformation, such as loop unrolling, jump threading,
145// indirect call promotion etc. As such, samples corresponding to a source
146// location may be aggregated multiple times in postlink. With a concept of
147// distribution factor for pseudo probes, samples can be distributed among
148// duplicated probes reasonable based on the assumption that optimizations
149// duplicating code well-maintain the branch frequency information (BFI). This
150// pass updates distribution factors for each pseudo probe at the end of the
151// prelink pipeline, to reflect an estimated portion of the real execution
152// count.
153class PseudoProbeUpdatePass : public PassInfoMixin<PseudoProbeUpdatePass> {
154 void runOnFunction(Function &F, FunctionAnalysisManager &FAM);
155
156public:
159};
160
161} // end namespace llvm
162#endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
This file defines the DenseMap class.
Implements a lazy call graph analysis and related passes for the new pass manager.
Statically lint checks LLVM IR
Definition: Lint.cpp:746
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
FunctionAnalysisManager FAM
const char LLVMTargetMachineRef TM
PassInstrumentationCallbacks PIC
This header defines various interfaces for pass management in LLVM.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
Definition: Any.h:28
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
An SCC of the call graph.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:547
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
PseudoProbeDescriptor(uint64_t GUID, uint64_t Hash)
bool profileIsValid(const Function &F, const FunctionSamples &Samples) const
bool moduleIsProbed(const Module &M) const
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
void registerCallbacks(PassInstrumentationCallbacks &PIC)
void runAfterPass(StringRef PassID, Any IR)
SampleProfileProbePass(TargetMachine *TM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Sample profile pseudo prober.
void instrumentOneFunc(Function &F, TargetMachine *TM)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
Representation of the samples collected for a function.
Definition: SampleProf.h:718
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unordered_map< BasicBlock *, uint32_t > BlockIdMap
std::unordered_map< Instruction *, uint32_t > InstructionIdMap
std::unordered_map< std::pair< uint64_t, uint64_t >, float, pair_hash< uint64_t, uint64_t > > ProbeFactorMap
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371