| 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 | |
| 33 | /// The instrumentation (profile-instr-gen) pass for IR based PGO. |
| 34 | // We use this pass to create COMDAT profile variables for context |
| 35 | // sensitive PGO (CSPGO). The reason to have a pass for this is CSPGO |
| 36 | // can be run after LTO/ThinLTO linking. Lld linker needs to see |
| 37 | // all the COMDAT variables before linking. So we have this pass |
| 38 | // always run before linking for CSPGO. |
| 39 | class PGOInstrumentationGenCreateVar |
| 40 | : public OptionalPassInfoMixin<PGOInstrumentationGenCreateVar> { |
| 41 | public: |
| 42 | PGOInstrumentationGenCreateVar(std::string CSInstrName = "" , |
| 43 | bool Sampling = false) |
| 44 | : CSInstrName(CSInstrName), ProfileSampling(Sampling) {} |
| 45 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 46 | |
| 47 | private: |
| 48 | std::string CSInstrName; |
| 49 | bool ProfileSampling; |
| 50 | }; |
| 51 | |
| 52 | enum class PGOInstrumentationType { Invalid = 0, FDO, CSFDO, CTXPROF }; |
| 53 | /// The instrumentation (profile-instr-gen) pass for IR based PGO. |
| 54 | class PGOInstrumentationGen |
| 55 | : public OptionalPassInfoMixin<PGOInstrumentationGen> { |
| 56 | public: |
| 57 | PGOInstrumentationGen( |
| 58 | PGOInstrumentationType InstrumentationType = PGOInstrumentationType ::FDO) |
| 59 | : InstrumentationType(InstrumentationType) {} |
| 60 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 61 | |
| 62 | private: |
| 63 | // If this is a context sensitive instrumentation. |
| 64 | const PGOInstrumentationType InstrumentationType; |
| 65 | }; |
| 66 | |
| 67 | /// The profile annotation (profile-instr-use) pass for IR based PGO. |
| 68 | class PGOInstrumentationUse |
| 69 | : public OptionalPassInfoMixin<PGOInstrumentationUse> { |
| 70 | public: |
| 71 | LLVM_ABI |
| 72 | PGOInstrumentationUse(std::string Filename = "" , |
| 73 | std::string RemappingFilename = "" , bool IsCS = false, |
| 74 | IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr); |
| 75 | |
| 76 | LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
| 77 | |
| 78 | private: |
| 79 | std::string ProfileFileName; |
| 80 | std::string ProfileRemappingFileName; |
| 81 | // If this is a context sensitive instrumentation. |
| 82 | bool IsCS; |
| 83 | IntrusiveRefCntPtr<vfs::FileSystem> FS; |
| 84 | }; |
| 85 | |
| 86 | /// The indirect function call promotion pass. |
| 87 | class PGOIndirectCallPromotion |
| 88 | : public OptionalPassInfoMixin<PGOIndirectCallPromotion> { |
| 89 | public: |
| 90 | PGOIndirectCallPromotion(bool IsInLTO = false, bool SamplePGO = false) |
| 91 | : InLTO(IsInLTO), SamplePGO(SamplePGO) {} |
| 92 | |
| 93 | LLVM_ABI 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 OptionalPassInfoMixin<PGOMemOPSizeOpt> { |
| 102 | public: |
| 103 | PGOMemOPSizeOpt() = default; |
| 104 | |
| 105 | LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &MAM); |
| 106 | }; |
| 107 | |
| 108 | LLVM_ABI void setProfMetadata(Instruction *TI, ArrayRef<uint64_t> EdgeCounts, |
| 109 | uint64_t MaxCount); |
| 110 | |
| 111 | LLVM_ABI void (Module *M, Instruction *TI, |
| 112 | uint64_t Count); |
| 113 | |
| 114 | } // end namespace llvm |
| 115 | |
| 116 | #endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H |
| 117 | |