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 LLVM_ABI extern cl::opt<bool> DebugInfoCorrelate; 30 31 class Function; 32 class Instruction; 33 class Module; 34 35 /// The instrumentation (profile-instr-gen) pass for IR based PGO. 36 // We use this pass to create COMDAT profile variables for context 37 // sensitive PGO (CSPGO). The reason to have a pass for this is CSPGO 38 // can be run after LTO/ThinLTO linking. Lld linker needs to see 39 // all the COMDAT variables before linking. So we have this pass 40 // always run before linking for CSPGO. 41 class PGOInstrumentationGenCreateVar 42 : public PassInfoMixin<PGOInstrumentationGenCreateVar> { 43 public: 44 PGOInstrumentationGenCreateVar(std::string CSInstrName = "", 45 bool Sampling = false) CSInstrName(CSInstrName)46 : CSInstrName(CSInstrName), ProfileSampling(Sampling) {} 47 LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); 48 49 private: 50 std::string CSInstrName; 51 bool ProfileSampling; 52 }; 53 54 enum class PGOInstrumentationType { Invalid = 0, FDO, CSFDO, CTXPROF }; 55 /// The instrumentation (profile-instr-gen) pass for IR based PGO. 56 class PGOInstrumentationGen : public PassInfoMixin<PGOInstrumentationGen> { 57 public: 58 PGOInstrumentationGen( 59 PGOInstrumentationType InstrumentationType = PGOInstrumentationType ::FDO) InstrumentationType(InstrumentationType)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 : public PassInfoMixin<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 : public PassInfoMixin<PGOIndirectCallPromotion> { 88 public: 89 PGOIndirectCallPromotion(bool IsInLTO = false, bool SamplePGO = false) InLTO(IsInLTO)90 : InLTO(IsInLTO), SamplePGO(SamplePGO) {} 91 92 LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); 93 94 private: 95 bool InLTO; 96 bool SamplePGO; 97 }; 98 99 /// The profile size based optimization pass for memory intrinsics. 100 class PGOMemOPSizeOpt : public PassInfoMixin<PGOMemOPSizeOpt> { 101 public: 102 PGOMemOPSizeOpt() = default; 103 104 LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &MAM); 105 }; 106 107 LLVM_ABI void setProfMetadata(Module *M, Instruction *TI, 108 ArrayRef<uint64_t> EdgeCounts, uint64_t MaxCount); 109 110 LLVM_ABI void setIrrLoopHeaderMetadata(Module *M, Instruction *TI, 111 uint64_t Count); 112 113 } // end namespace llvm 114 115 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H 116