LLVM 18.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 BasicBlock;
26class Function;
27class Instruction;
28class Loop;
29class PassInstrumentationCallbacks;
30class TargetMachine;
31
32class Module;
33
34using namespace sampleprof;
35using BlockIdMap = std::unordered_map<BasicBlock *, uint32_t>;
36using InstructionIdMap = std::unordered_map<Instruction *, uint32_t>;
37// Map from tuples of Probe id and inline stack hash code to distribution
38// factors.
39using ProbeFactorMap = std::unordered_map<std::pair<uint64_t, uint64_t>, float,
42
43
44// A pseudo probe verifier that can be run after each IR passes to detect the
45// violation of updating probe factors. In principle, the sum of distribution
46// factor for a probe should be identical before and after a pass. For a
47// function pass, the factor sum for a probe would be typically 100%.
49public:
51
52 // Implementation of pass instrumentation callbacks for new pass manager.
53 void runAfterPass(StringRef PassID, Any IR);
54
55private:
56 // Allow a little bias due the rounding to integral factors.
57 constexpr static float DistributionFactorVariance = 0.02f;
58 // Distribution factors from last pass.
59 FuncProbeFactorMap FunctionProbeFactors;
60
61 void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
62 void runAfterPass(const Module *M);
64 void runAfterPass(const Function *F);
65 void runAfterPass(const Loop *L);
66 bool shouldVerifyFunction(const Function *F);
67 void verifyProbeFactors(const Function *F,
68 const ProbeFactorMap &ProbeFactors);
69};
70
71/// Sample profile pseudo prober.
72///
73/// Insert pseudo probes for block sampling and value sampling.
75public:
76 // Give an empty module id when the prober is not used for instrumentation.
77 SampleProfileProber(Function &F, const std::string &CurModuleUniqueId);
79
80private:
81 Function *getFunction() const { return F; }
82 uint64_t getFunctionHash() const { return FunctionHash; }
83 uint32_t getBlockId(const BasicBlock *BB) const;
84 uint32_t getCallsiteId(const Instruction *Call) const;
85 void computeCFGHash();
86 void computeProbeIdForBlocks();
87 void computeProbeIdForCallsites();
88
89 Function *F;
90
91 /// The current module ID that is used to name a static object as a comdat
92 /// group.
93 std::string CurModuleUniqueId;
94
95 /// A CFG hash code used to identify a function code changes.
96 uint64_t FunctionHash;
97
98 /// Map basic blocks to the their pseudo probe ids.
99 BlockIdMap BlockProbeIds;
100
101 /// Map indirect calls to the their pseudo probe ids.
102 InstructionIdMap CallProbeIds;
103
104 /// The ID of the last probe, Can be used to number a new probe.
105 uint32_t LastProbeId;
106};
107
108class SampleProfileProbePass : public PassInfoMixin<SampleProfileProbePass> {
109 TargetMachine *TM;
110
111public:
114};
115
116// Pseudo probe distribution factor updater.
117// Sample profile annotation can happen in both LTO prelink and postlink. The
118// postlink-time re-annotation can degrade profile quality because of prelink
119// code duplication transformation, such as loop unrolling, jump threading,
120// indirect call promotion etc. As such, samples corresponding to a source
121// location may be aggregated multiple times in postlink. With a concept of
122// distribution factor for pseudo probes, samples can be distributed among
123// duplicated probes reasonable based on the assumption that optimizations
124// duplicating code well-maintain the branch frequency information (BFI). This
125// pass updates distribution factors for each pseudo probe at the end of the
126// prelink pipeline, to reflect an estimated portion of the real execution
127// count.
128class PseudoProbeUpdatePass : public PassInfoMixin<PseudoProbeUpdatePass> {
129 void runOnFunction(Function &F, FunctionAnalysisManager &FAM);
130
131public:
134};
135
136} // end namespace llvm
137#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.
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:81
#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:47
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
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
@ 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