xref: /llvm-project/llvm/include/llvm/Transforms/Instrumentation/PGOInstrumentation.h (revision 3f18a0a71cc29c502591a3d29a1845a011415f2a)
1 //===- Transforms/Instrumentation/PGOInstrumentation.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 IR based instrumentation passes (
11 /// (profile-gen, and profile-use).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H
16 #define LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Support/CommandLine.h"
22 #include <cstdint>
23 #include <string>
24 
25 namespace llvm {
26 
27 extern cl::opt<bool> DebugInfoCorrelate;
28 
29 class Function;
30 class Instruction;
31 class Module;
32 
33 namespace vfs {
34 class FileSystem;
35 } // namespace vfs
36 
37 /// The instrumentation (profile-instr-gen) pass for IR based PGO.
38 // We use this pass to create COMDAT profile variables for context
39 // sensitive PGO (CSPGO). The reason to have a pass for this is CSPGO
40 // can be run after LTO/ThinLTO linking. Lld linker needs to see
41 // all the COMDAT variables before linking. So we have this pass
42 // always run before linking for CSPGO.
43 class PGOInstrumentationGenCreateVar
44     : public PassInfoMixin<PGOInstrumentationGenCreateVar> {
45 public:
46   PGOInstrumentationGenCreateVar(std::string CSInstrName = "",
47                                  bool Sampling = false)
48       : CSInstrName(CSInstrName), ProfileSampling(Sampling) {}
49   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
50 
51 private:
52   std::string CSInstrName;
53   bool ProfileSampling;
54 };
55 
56 enum class PGOInstrumentationType { Invalid = 0, FDO, CSFDO, CTXPROF };
57 /// The instrumentation (profile-instr-gen) pass for IR based PGO.
58 class PGOInstrumentationGen : public PassInfoMixin<PGOInstrumentationGen> {
59 public:
60   PGOInstrumentationGen(
61       PGOInstrumentationType InstrumentationType = PGOInstrumentationType ::FDO)
62       : InstrumentationType(InstrumentationType) {}
63   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
64 
65 private:
66   // If this is a context sensitive instrumentation.
67   const PGOInstrumentationType InstrumentationType;
68 };
69 
70 /// The profile annotation (profile-instr-use) pass for IR based PGO.
71 class PGOInstrumentationUse : public PassInfoMixin<PGOInstrumentationUse> {
72 public:
73   PGOInstrumentationUse(std::string Filename = "",
74                         std::string RemappingFilename = "", bool IsCS = false,
75                         IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
76 
77   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
78 
79 private:
80   std::string ProfileFileName;
81   std::string ProfileRemappingFileName;
82   // If this is a context sensitive instrumentation.
83   bool IsCS;
84   IntrusiveRefCntPtr<vfs::FileSystem> FS;
85 };
86 
87 /// The indirect function call promotion pass.
88 class PGOIndirectCallPromotion : public PassInfoMixin<PGOIndirectCallPromotion> {
89 public:
90   PGOIndirectCallPromotion(bool IsInLTO = false, bool SamplePGO = false)
91       : InLTO(IsInLTO), SamplePGO(SamplePGO) {}
92 
93   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
94 
95 private:
96   bool InLTO;
97   bool SamplePGO;
98 };
99 
100 /// The profile size based optimization pass for memory intrinsics.
101 class PGOMemOPSizeOpt : public PassInfoMixin<PGOMemOPSizeOpt> {
102 public:
103   PGOMemOPSizeOpt() = default;
104 
105   PreservedAnalyses run(Function &F, FunctionAnalysisManager &MAM);
106 };
107 
108 void setProfMetadata(Module *M, Instruction *TI, ArrayRef<uint64_t> EdgeCounts,
109                      uint64_t MaxCount);
110 
111 void setIrrLoopHeaderMetadata(Module *M, Instruction *TI, uint64_t Count);
112 
113 } // end namespace llvm
114 
115 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H
116