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