1 //===- ProfileSummary.h - Profile summary data structure. -------*- 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 // This file defines the profile summary data structure. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_IR_PROFILESUMMARY_H 14 #define LLVM_IR_PROFILESUMMARY_H 15 16 #include "llvm/Support/Compiler.h" 17 #include <algorithm> 18 #include <cassert> 19 #include <cstdint> 20 #include <vector> 21 22 namespace llvm { 23 24 class LLVMContext; 25 class Metadata; 26 class raw_ostream; 27 28 // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets. 29 // The semantics of counts depend on the type of profile. For instrumentation 30 // profile, counts are block counts and for sample profile, counts are 31 // per-line samples. Given a target counts percentile, we compute the minimum 32 // number of counts needed to reach this target and the minimum among these 33 // counts. 34 struct ProfileSummaryEntry { 35 const uint32_t Cutoff; ///< The required percentile of counts. 36 const uint64_t MinCount; ///< The minimum count for this percentile. 37 const uint64_t NumCounts; ///< Number of counts >= the minimum count. 38 ProfileSummaryEntryProfileSummaryEntry39 ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount, 40 uint64_t TheNumCounts) 41 : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {} 42 }; 43 44 using SummaryEntryVector = std::vector<ProfileSummaryEntry>; 45 46 class ProfileSummary { 47 public: 48 enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample }; 49 50 private: 51 const Kind PSK; 52 const SummaryEntryVector DetailedSummary; 53 const uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount; 54 const uint32_t NumCounts, NumFunctions; 55 /// If 'Partial' is false, it means the profile being used to optimize 56 /// a target is collected from the same target. 57 /// If 'Partial' is true, it means the profile is for common/shared 58 /// code. The common profile is usually merged from profiles collected 59 /// from running other targets. 60 bool Partial = false; 61 /// This approximately represents the ratio of the number of profile counters 62 /// of the program being built to the number of profile counters in the 63 /// partial sample profile. When 'Partial' is false, it is undefined. This is 64 /// currently only available under thin LTO mode. 65 double PartialProfileRatio = 0.0; 66 /// Return detailed summary as metadata. 67 Metadata *getDetailedSummaryMD(LLVMContext &Context); 68 69 public: 70 static const int Scale = 1000000; 71 72 ProfileSummary(Kind K, const SummaryEntryVector &DetailedSummary, 73 uint64_t TotalCount, uint64_t MaxCount, 74 uint64_t MaxInternalCount, uint64_t MaxFunctionCount, 75 uint32_t NumCounts, uint32_t NumFunctions, 76 bool Partial = false, double PartialProfileRatio = 0) PSK(K)77 : PSK(K), DetailedSummary(DetailedSummary), TotalCount(TotalCount), 78 MaxCount(MaxCount), MaxInternalCount(MaxInternalCount), 79 MaxFunctionCount(MaxFunctionCount), NumCounts(NumCounts), 80 NumFunctions(NumFunctions), Partial(Partial), 81 PartialProfileRatio(PartialProfileRatio) {} 82 getKind()83 Kind getKind() const { return PSK; } 84 /// Return summary information as metadata. 85 LLVM_ABI Metadata *getMD(LLVMContext &Context, bool AddPartialField = true, 86 bool AddPartialProfileRatioField = true); 87 /// Construct profile summary from metdata. 88 LLVM_ABI static ProfileSummary *getFromMD(Metadata *MD); getDetailedSummary()89 const SummaryEntryVector &getDetailedSummary() { return DetailedSummary; } getNumFunctions()90 uint32_t getNumFunctions() const { return NumFunctions; } getMaxFunctionCount()91 uint64_t getMaxFunctionCount() const { return MaxFunctionCount; } getNumCounts()92 uint32_t getNumCounts() const { return NumCounts; } getTotalCount()93 uint64_t getTotalCount() const { return TotalCount; } getMaxCount()94 uint64_t getMaxCount() const { return MaxCount; } getMaxInternalCount()95 uint64_t getMaxInternalCount() const { return MaxInternalCount; } setPartialProfile(bool PP)96 void setPartialProfile(bool PP) { Partial = PP; } isPartialProfile()97 bool isPartialProfile() const { return Partial; } getPartialProfileRatio()98 double getPartialProfileRatio() const { return PartialProfileRatio; } setPartialProfileRatio(double R)99 void setPartialProfileRatio(double R) { 100 assert(isPartialProfile() && "Unexpected when not partial profile"); 101 PartialProfileRatio = R; 102 } 103 LLVM_ABI void printSummary(raw_ostream &OS) const; 104 LLVM_ABI void printDetailedSummary(raw_ostream &OS) const; 105 }; 106 107 } // end namespace llvm 108 109 #endif // LLVM_IR_PROFILESUMMARY_H 110