LLVM 24.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/StringSet.h"
20#include "llvm/IR/IRUnitRef.h"
21#include "llvm/IR/PassManager.h"
24
25namespace llvm {
26class BasicBlock;
27class Function;
28class Instruction;
29class Loop;
30class LLVMContext;
32class TargetMachine;
33
34class Module;
35
36using namespace sampleprof;
39// Map from tuples of Probe id and inline stack hash code to distribution
40// factors.
43
44
45// A pseudo probe verifier that can be run after each IR passes to detect the
46// violation of updating probe factors. In principle, the sum of distribution
47// factor for a probe should be identical before and after a pass. For a
48// function pass, the factor sum for a probe would be typically 100%.
50public:
53
54 // Implementation of pass instrumentation callbacks for new pass manager.
56
57private:
58 bool Enabled;
59 StringSet<> VerifyFuncNames;
60 // Allow a little bias due the rounding to integral factors.
61 constexpr static float DistributionFactorVariance = 0.02f;
62 // Distribution factors from last pass.
63 FuncProbeFactorMap FunctionProbeFactors;
64
65 void collectProbeFactors(const BasicBlock *BB, ProbeFactorMap &ProbeFactors);
66 void runAfterPass(const Module *M);
68 void runAfterPass(const Function *F);
69 void runAfterPass(const Loop *L);
70 bool shouldVerifyFunction(const Function *F);
71 void verifyProbeFactors(const Function *F,
72 const ProbeFactorMap &ProbeFactors);
73};
74
75/// Sample profile pseudo prober.
76///
77/// Insert pseudo probes for block sampling and value sampling.
79public:
80 // Give an empty module id when the prober is not used for instrumentation.
83
84private:
85 Function *getFunction() const { return F; }
86 uint64_t getFunctionHash() const { return FunctionHash; }
87 uint32_t getBlockId(const BasicBlock *BB) const;
88 uint32_t getCallsiteId(const Instruction *Call) const;
89 void findUnreachableBlocks(DenseSet<BasicBlock *> &BlocksToIgnore);
90 void findInvokeNormalDests(DenseSet<BasicBlock *> &InvokeNormalDests);
91 void computeBlocksToIgnore(DenseSet<BasicBlock *> &BlocksToIgnore,
92 DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
93 const Instruction *
94 getOriginalTerminator(const BasicBlock *Head,
95 const DenseSet<BasicBlock *> &BlocksToIgnore);
96 void computeCFGHash(const DenseSet<BasicBlock *> &BlocksToIgnore);
97 void computeProbeId(const DenseSet<BasicBlock *> &BlocksToIgnore,
98 const DenseSet<BasicBlock *> &BlocksAndCallsToIgnore);
99
100 Function *F;
101
102 /// The current module ID that is used to name a static object as a comdat
103 /// group.
104 std::string CurModuleUniqueId;
105
106 /// A CFG hash code used to identify a function code changes.
107 uint64_t FunctionHash;
108
109 /// Map basic blocks to the their pseudo probe ids.
110 BlockIdMap BlockProbeIds;
111
112 /// Map indirect calls to the their pseudo probe ids.
113 InstructionIdMap CallProbeIds;
114
115 /// The ID of the last probe, Can be used to number a new probe.
116 uint32_t LastProbeId;
117};
118
120 : public OptionalPassInfoMixin<SampleProfileProbePass> {
121 TargetMachine *TM;
122
123public:
126};
127
128// Pseudo probe distribution factor updater.
129// Sample profile annotation can happen in both LTO prelink and postlink. The
130// postlink-time re-annotation can degrade profile quality because of prelink
131// code duplication transformation, such as loop unrolling, jump threading,
132// indirect call promotion etc. As such, samples corresponding to a source
133// location may be aggregated multiple times in postlink. With a concept of
134// distribution factor for pseudo probes, samples can be distributed among
135// duplicated probes reasonable based on the assumption that optimizations
136// duplicating code well-maintain the branch frequency information (BFI). This
137// pass updates distribution factors for each pseudo probe at the end of the
138// prelink pipeline, to reflect an estimated portion of the real execution
139// count.
141 : public OptionalPassInfoMixin<PseudoProbeUpdatePass> {
142 void runOnFunction(Function &F, FunctionAnalysisManager &FAM);
143
144public:
147};
148
149} // end namespace llvm
150#endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEPROBE_H
uint64_t uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
This file defines IRUnitRef, a type-erased reference to the IR unit a pass or analysis is running on,...
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:85
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
PassInstrumentationCallbacks PIC
StringSet - A set-like wrapper for the StringMap.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
Definition IRUnitRef.h:60
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
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:68
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, IRUnitRef IR)
LLVM_ABI PseudoProbeVerifier(const LLVMContext &C)
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:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:27
Primary interface to the complete machine description for the target machine.
CallInst * Call
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.