LLVM 23.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
20#include "llvm/IR/PassManager.h"
23
24namespace llvm {
25class BasicBlock;
26class Function;
27class Instruction;
28class Loop;
30class TargetMachine;
31
32class Module;
33
34using namespace sampleprof;
37// Map from tuples of Probe id and inline stack hash code to distribution
38// factors.
41
42
43// A pseudo probe verifier that can be run after each IR passes to detect the
44// violation of updating probe factors. In principle, the sum of distribution
45// factor for a probe should be identical before and after a pass. For a
46// function pass, the factor sum for a probe would be typically 100%.
48public:
50
51 // Implementation of pass instrumentation callbacks for new pass manager.
52 LLVM_ABI void runAfterPass(StringRef PassID, Any IR);
53
54private:
55 // Allow a little bias due the rounding to integral factors.
56 constexpr static float DistributionFactorVariance = 0.02f;
57 // Distribution factors from last pass.
58 FuncProbeFactorMap FunctionProbeFactors;
59
60 void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
61 void runAfterPass(const Module *M);
63 void runAfterPass(const Function *F);
64 void runAfterPass(const Loop *L);
65 bool shouldVerifyFunction(const Function *F);
66 void verifyProbeFactors(const Function *F,
67 const ProbeFactorMap &ProbeFactors);
68};
69
70/// Sample profile pseudo prober.
71///
72/// Insert pseudo probes for block sampling and value sampling.
74public:
75 // Give an empty module id when the prober is not used for instrumentation.
78
79private:
80 Function *getFunction() const { return F; }
81 uint64_t getFunctionHash() const { return FunctionHash; }
82 uint32_t getBlockId(const BasicBlock *BB) const;
83 uint32_t getCallsiteId(const Instruction *Call) const;
84 void findUnreachableBlocks(DenseSet<BasicBlock *> &BlocksToIgnore);
85 void findInvokeNormalDests(DenseSet<BasicBlock *> &InvokeNormalDests);
86 void computeBlocksToIgnore(DenseSet<BasicBlock *> &BlocksToIgnore,
87 DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
88 const Instruction *
89 getOriginalTerminator(const BasicBlock *Head,
90 const DenseSet<BasicBlock *> &BlocksToIgnore);
91 void computeCFGHash(const DenseSet<BasicBlock *> &BlocksToIgnore);
92 void computeProbeId(const DenseSet<BasicBlock *> &BlocksToIgnore,
93 const DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
94
95 Function *F;
96
97 /// The current module ID that is used to name a static object as a comdat
98 /// group.
99 std::string CurModuleUniqueId;
100
101 /// A CFG hash code used to identify a function code changes.
102 uint64_t FunctionHash;
103
104 /// Map basic blocks to the their pseudo probe ids.
105 BlockIdMap BlockProbeIds;
106
107 /// Map indirect calls to the their pseudo probe ids.
108 InstructionIdMap CallProbeIds;
109
110 /// The ID of the last probe, Can be used to number a new probe.
111 uint32_t LastProbeId;
112};
113
115 : public OptionalPassInfoMixin<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.
136 : public OptionalPassInfoMixin<PseudoProbeUpdatePass> {
137 void runOnFunction(Function &F, FunctionAnalysisManager &FAM);
138
139public:
142};
143
144} // end namespace llvm
145#endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
#define LLVM_ABI
Definition Compiler.h:215
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:81
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
An SCC of the call graph.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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 Analysis.h:112
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC)
LLVM_ABI void runAfterPass(StringRef PassID, Any IR)
SampleProfileProbePass(TargetMachine *TM)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI SampleProfileProber(Function &F)
LLVM_ABI void instrumentOneFunc(Function &F, TargetMachine *TM)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:128
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
CallInst * Call
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
DenseMap< std::pair< uint64_t, uint64_t >, float > ProbeFactorMap
DenseMap< Instruction *, uint32_t > InstructionIdMap
DenseMap< BasicBlock *, uint32_t > BlockIdMap
StringMap< ProbeFactorMap > FuncProbeFactorMap
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in for passes that can be skipped.