LLVM 20.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
19#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 findUnreachableBlocks(DenseSet<BasicBlock *> &BlocksToIgnore);
86 void findInvokeNormalDests(DenseSet<BasicBlock *> &InvokeNormalDests);
87 void computeBlocksToIgnore(DenseSet<BasicBlock *> &BlocksToIgnore,
88 DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
89 const Instruction *
90 getOriginalTerminator(const BasicBlock *Head,
91 const DenseSet<BasicBlock *> &BlocksToIgnore);
92 void computeCFGHash(const DenseSet<BasicBlock *> &BlocksToIgnore);
93 void computeProbeId(const DenseSet<BasicBlock *> &BlocksToIgnore,
94 const DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
95
96 Function *F;
97
98 /// The current module ID that is used to name a static object as a comdat
99 /// group.
100 std::string CurModuleUniqueId;
101
102 /// A CFG hash code used to identify a function code changes.
103 uint64_t FunctionHash;
104
105 /// Map basic blocks to the their pseudo probe ids.
106 BlockIdMap BlockProbeIds;
107
108 /// Map indirect calls to the their pseudo probe ids.
109 InstructionIdMap CallProbeIds;
110
111 /// The ID of the last probe, Can be used to number a new probe.
112 uint32_t LastProbeId;
113};
114
115class SampleProfileProbePass : public PassInfoMixin<SampleProfileProbePass> {
116 TargetMachine *TM;
117
118public:
121};
122
123// Pseudo probe distribution factor updater.
124// Sample profile annotation can happen in both LTO prelink and postlink. The
125// postlink-time re-annotation can degrade profile quality because of prelink
126// code duplication transformation, such as loop unrolling, jump threading,
127// indirect call promotion etc. As such, samples corresponding to a source
128// location may be aggregated multiple times in postlink. With a concept of
129// distribution factor for pseudo probes, samples can be distributed among
130// duplicated probes reasonable based on the assumption that optimizations
131// duplicating code well-maintain the branch frequency information (BFI). This
132// pass updates distribution factors for each pseudo probe at the end of the
133// prelink pipeline, to reflect an estimated portion of the real execution
134// count.
135class PseudoProbeUpdatePass : public PassInfoMixin<PseudoProbeUpdatePass> {
136 void runOnFunction(Function &F, FunctionAnalysisManager &FAM);
137
138public:
141};
142
143} // end namespace llvm
144#endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
This header defines various interfaces for pass management in LLVM.
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:80
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
FunctionAnalysisManager FAM
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Definition: Any.h:28
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Implements a dense probed hash-table based set.
Definition: DenseSet.h:278
An SCC of the call graph.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
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:51
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
@ 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:69
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...