Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- InstrProfWriter.h - Instrumented profiling writer --------*- 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 contains support for writing profiling data for instrumentation |
| 10 | // based PGO and coverage. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H |
| 15 | #define LLVM_PROFILEDATA_INSTRPROFWRITER_H |
| 16 | |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ProfileData/InstrProf.h" |
| 20 | #include "llvm/Support/Endian.h" |
| 21 | #include "llvm/Support/Error.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include <cstdint> |
| 24 | #include <memory> |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
| 28 | /// Writer for instrumentation based profile data. |
| 29 | class InstrProfRecordWriterTrait; |
| 30 | class ProfOStream; |
| 31 | class raw_fd_ostream; |
| 32 | |
| 33 | class InstrProfWriter { |
| 34 | public: |
| 35 | using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 36 | // PF_IRLevelWithCS is the profile from context sensitive IR instrumentation. |
| 37 | enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel, PF_IRLevelWithCS }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | bool Sparse; |
| 41 | StringMap<ProfilingData> FunctionData; |
| 42 | ProfKind ProfileKind = PF_Unknown; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 43 | bool InstrEntryBBEnabled; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | // Use raw pointer here for the incomplete type object. |
| 45 | InstrProfRecordWriterTrait *InfoObj; |
| 46 | |
| 47 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 48 | InstrProfWriter(bool Sparse = false, bool InstrEntryBBEnabled = false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | ~InstrProfWriter(); |
| 50 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 51 | StringMap<ProfilingData> &getProfileData() { return FunctionData; } |
| 52 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 53 | /// Add function counts for the given function. If there are already counts |
| 54 | /// for this function and the hash and number of counts match, each counter is |
| 55 | /// summed. Optionally scale counts by \p Weight. |
| 56 | void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, |
| 57 | function_ref<void(Error)> Warn); |
| 58 | void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) { |
| 59 | addRecord(std::move(I), 1, Warn); |
| 60 | } |
| 61 | |
| 62 | /// Merge existing function counts from the given writer. |
| 63 | void mergeRecordsFromWriter(InstrProfWriter &&IPW, |
| 64 | function_ref<void(Error)> Warn); |
| 65 | |
| 66 | /// Write the profile to \c OS |
| 67 | void write(raw_fd_ostream &OS); |
| 68 | |
| 69 | /// Write the profile in text format to \c OS |
| 70 | Error writeText(raw_fd_ostream &OS); |
| 71 | |
| 72 | /// Write \c Record in text format to \c OS |
| 73 | static void writeRecordInText(StringRef Name, uint64_t Hash, |
| 74 | const InstrProfRecord &Counters, |
| 75 | InstrProfSymtab &Symtab, raw_fd_ostream &OS); |
| 76 | |
| 77 | /// Write the profile, returning the raw data. For testing. |
| 78 | std::unique_ptr<MemoryBuffer> writeBuffer(); |
| 79 | |
| 80 | /// Set the ProfileKind. Report error if mixing FE and IR level profiles. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 81 | /// \c WithCS indicates if this is for contenxt sensitive instrumentation. |
| 82 | Error setIsIRLevelProfile(bool IsIRLevel, bool WithCS) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | if (ProfileKind == PF_Unknown) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 84 | if (IsIRLevel) |
| 85 | ProfileKind = WithCS ? PF_IRLevelWithCS : PF_IRLevel; |
| 86 | else |
| 87 | ProfileKind = PF_FE; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | return Error::success(); |
| 89 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 90 | |
| 91 | if (((ProfileKind != PF_FE) && !IsIRLevel) || |
| 92 | ((ProfileKind == PF_FE) && IsIRLevel)) |
| 93 | return make_error<InstrProfError>(instrprof_error::unsupported_version); |
| 94 | |
| 95 | // When merging a context-sensitive profile (WithCS == true) with an IRLevel |
| 96 | // profile, set the kind to PF_IRLevelWithCS. |
| 97 | if (ProfileKind == PF_IRLevel && WithCS) |
| 98 | ProfileKind = PF_IRLevelWithCS; |
| 99 | |
| 100 | return Error::success(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 103 | void setInstrEntryBBEnabled(bool Enabled) { InstrEntryBBEnabled = Enabled; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 104 | // Internal interface for testing purpose only. |
| 105 | void setValueProfDataEndianness(support::endianness Endianness); |
| 106 | void setOutputSparse(bool Sparse); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 107 | // Compute the overlap b/w this object and Other. Program level result is |
| 108 | // stored in Overlap and function level result is stored in FuncLevelOverlap. |
| 109 | void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, |
| 110 | OverlapStats &FuncLevelOverlap, |
| 111 | const OverlapFuncFilters &FuncFilter); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I, |
| 115 | uint64_t Weight, function_ref<void(Error)> Warn); |
| 116 | bool shouldEncodeData(const ProfilingData &PD); |
| 117 | void writeImpl(ProfOStream &OS); |
| 118 | }; |
| 119 | |
| 120 | } // end namespace llvm |
| 121 | |
| 122 | #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H |