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>; |
| 36 | enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel }; |
| 37 | |
| 38 | private: |
| 39 | bool Sparse; |
| 40 | StringMap<ProfilingData> FunctionData; |
| 41 | ProfKind ProfileKind = PF_Unknown; |
| 42 | // Use raw pointer here for the incomplete type object. |
| 43 | InstrProfRecordWriterTrait *InfoObj; |
| 44 | |
| 45 | public: |
| 46 | InstrProfWriter(bool Sparse = false); |
| 47 | ~InstrProfWriter(); |
| 48 | |
| 49 | /// Add function counts for the given function. If there are already counts |
| 50 | /// for this function and the hash and number of counts match, each counter is |
| 51 | /// summed. Optionally scale counts by \p Weight. |
| 52 | void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, |
| 53 | function_ref<void(Error)> Warn); |
| 54 | void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) { |
| 55 | addRecord(std::move(I), 1, Warn); |
| 56 | } |
| 57 | |
| 58 | /// Merge existing function counts from the given writer. |
| 59 | void mergeRecordsFromWriter(InstrProfWriter &&IPW, |
| 60 | function_ref<void(Error)> Warn); |
| 61 | |
| 62 | /// Write the profile to \c OS |
| 63 | void write(raw_fd_ostream &OS); |
| 64 | |
| 65 | /// Write the profile in text format to \c OS |
| 66 | Error writeText(raw_fd_ostream &OS); |
| 67 | |
| 68 | /// Write \c Record in text format to \c OS |
| 69 | static void writeRecordInText(StringRef Name, uint64_t Hash, |
| 70 | const InstrProfRecord &Counters, |
| 71 | InstrProfSymtab &Symtab, raw_fd_ostream &OS); |
| 72 | |
| 73 | /// Write the profile, returning the raw data. For testing. |
| 74 | std::unique_ptr<MemoryBuffer> writeBuffer(); |
| 75 | |
| 76 | /// Set the ProfileKind. Report error if mixing FE and IR level profiles. |
| 77 | Error setIsIRLevelProfile(bool IsIRLevel) { |
| 78 | if (ProfileKind == PF_Unknown) { |
| 79 | ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE; |
| 80 | return Error::success(); |
| 81 | } |
| 82 | return (IsIRLevel == (ProfileKind == PF_IRLevel)) |
| 83 | ? Error::success() |
| 84 | : make_error<InstrProfError>( |
| 85 | instrprof_error::unsupported_version); |
| 86 | } |
| 87 | |
| 88 | // Internal interface for testing purpose only. |
| 89 | void setValueProfDataEndianness(support::endianness Endianness); |
| 90 | void setOutputSparse(bool Sparse); |
| 91 | |
| 92 | private: |
| 93 | void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I, |
| 94 | uint64_t Weight, function_ref<void(Error)> Warn); |
| 95 | bool shouldEncodeData(const ProfilingData &PD); |
| 96 | void writeImpl(ProfOStream &OS); |
| 97 | }; |
| 98 | |
| 99 | } // end namespace llvm |
| 100 | |
| 101 | #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H |