| 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 "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/VirtualFileSystem.h" |
| 24 | #include <cstdint> |
| 25 | #include <string> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | class Function; |
| 30 | class Instruction; |
| 31 | class Module; |
| 32 | class ModuleSummaryIndex; |
| 33 | |
| 34 | /// The instrumentation (profile-instr-gen) pass for IR based PGO. |
| 35 | // We use this pass to create COMDAT profile variables for context |
| 36 | // sensitive PGO (CSPGO). The reason to have a pass for this is CSPGO |
| 37 | // can be run after LTO/ThinLTO linking. Lld linker needs to see |
| 38 | // all the COMDAT variables before linking. So we have this pass |
| 39 | // always run before linking for CSPGO. |
| 40 | class PGOInstrumentationGenCreateVar |
| 41 | : public OptionalPassInfoMixin<PGOInstrumentationGenCreateVar> { |
| 42 | public: |
| 43 | PGOInstrumentationGenCreateVar(std::string CSInstrName = "" , |
| 44 | bool Sampling = false) |
| 45 | : CSInstrName(CSInstrName), ProfileSampling(Sampling) {} |
| 46 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 47 | |
| 48 | private: |
| 49 | std::string CSInstrName; |
| 50 | bool ProfileSampling; |
| 51 | }; |
| 52 | |
| 53 | enum class PGOInstrumentationType { Invalid = 0, FDO, CSFDO, CTXPROF }; |
| 54 | /// The instrumentation (profile-instr-gen) pass for IR based PGO. |
| 55 | class PGOInstrumentationGen |
| 56 | : public OptionalPassInfoMixin<PGOInstrumentationGen> { |
| 57 | public: |
| 58 | PGOInstrumentationGen( |
| 59 | PGOInstrumentationType InstrumentationType = PGOInstrumentationType ::FDO) |
| 60 | : InstrumentationType(InstrumentationType) {} |
| 61 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 62 | |
| 63 | private: |
| 64 | // If this is a context sensitive instrumentation. |
| 65 | const PGOInstrumentationType InstrumentationType; |
| 66 | }; |
| 67 | |
| 68 | /// The profile annotation (profile-instr-use) pass for IR based PGO. |
| 69 | class PGOInstrumentationUse |
| 70 | : public OptionalPassInfoMixin<PGOInstrumentationUse> { |
| 71 | public: |
| 72 | LLVM_ABI |
| 73 | PGOInstrumentationUse(std::string Filename = "" , |
| 74 | std::string RemappingFilename = "" , bool IsCS = false, |
| 75 | IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr); |
| 76 | |
| 77 | LLVM_ABI 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 |
| 89 | : public OptionalPassInfoMixin<PGOIndirectCallPromotion> { |
| 90 | public: |
| 91 | /// \p ImportSummary is the combined summary index of a ThinLTO backend |
| 92 | /// compile, if any. When present, promotion to a function that is only |
| 93 | /// declared in the module requires a summary for the target in the index |
| 94 | /// that indicates it is live (see -icp-allow-decls). |
| 95 | PGOIndirectCallPromotion(bool IsInLTO = false, bool SamplePGO = false, |
| 96 | const ModuleSummaryIndex *ImportSummary = nullptr) |
| 97 | : InLTO(IsInLTO), SamplePGO(SamplePGO), ImportSummary(ImportSummary) {} |
| 98 | |
| 99 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 100 | |
| 101 | private: |
| 102 | bool InLTO; |
| 103 | bool SamplePGO; |
| 104 | const ModuleSummaryIndex *ImportSummary; |
| 105 | }; |
| 106 | |
| 107 | /// The profile size based optimization pass for memory intrinsics. |
| 108 | class PGOMemOPSizeOpt : public OptionalPassInfoMixin<PGOMemOPSizeOpt> { |
| 109 | public: |
| 110 | PGOMemOPSizeOpt() = default; |
| 111 | |
| 112 | LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &MAM); |
| 113 | }; |
| 114 | |
| 115 | LLVM_ABI void setProfMetadata(Instruction *TI, ArrayRef<uint64_t> EdgeCounts, |
| 116 | uint64_t MaxCount); |
| 117 | |
| 118 | LLVM_ABI void (Module *M, Instruction *TI, |
| 119 | uint64_t Count); |
| 120 | |
| 121 | /// Return the value of -pgo-instrument-cold-function-only in LibraryOptions. |
| 122 | LLVM_ABI bool isPGOInstrumentColdFunctionOnly( |
| 123 | const cl::LibraryOptionsContext &LibraryOptions); |
| 124 | |
| 125 | } // end namespace llvm |
| 126 | |
| 127 | #endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H |
| 128 | |