Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for writing profiling data for instrumentation |
| 11 | // based PGO and coverage. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H |
| 16 | #define LLVM_PROFILEDATA_INSTRPROFWRITER_H |
| 17 | |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
| 20 | #include "llvm/ProfileData/InstrProf.h" |
| 21 | #include "llvm/Support/Endian.h" |
| 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include <cstdint> |
| 25 | #include <memory> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | /// Writer for instrumentation based profile data. |
| 30 | class InstrProfRecordWriterTrait; |
| 31 | class ProfOStream; |
| 32 | class raw_fd_ostream; |
| 33 | |
| 34 | class InstrProfWriter { |
| 35 | public: |
| 36 | using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>; |
| 37 | enum ProfKind { PF_Unknown = 0, PF_FE, PF_IRLevel }; |
| 38 | |
| 39 | private: |
| 40 | bool Sparse; |
| 41 | StringMap<ProfilingData> FunctionData; |
| 42 | ProfKind ProfileKind = PF_Unknown; |
| 43 | // Use raw pointer here for the incomplete type object. |
| 44 | InstrProfRecordWriterTrait *InfoObj; |
| 45 | |
| 46 | public: |
| 47 | InstrProfWriter(bool Sparse = false); |
| 48 | ~InstrProfWriter(); |
| 49 | |
| 50 | /// Add function counts for the given function. If there are already counts |
| 51 | /// for this function and the hash and number of counts match, each counter is |
| 52 | /// summed. Optionally scale counts by \p Weight. |
| 53 | void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, |
| 54 | function_ref<void(Error)> Warn); |
| 55 | void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) { |
| 56 | addRecord(std::move(I), 1, Warn); |
| 57 | } |
| 58 | |
| 59 | /// Merge existing function counts from the given writer. |
| 60 | void mergeRecordsFromWriter(InstrProfWriter &&IPW, |
| 61 | function_ref<void(Error)> Warn); |
| 62 | |
| 63 | /// Write the profile to \c OS |
| 64 | void write(raw_fd_ostream &OS); |
| 65 | |
| 66 | /// Write the profile in text format to \c OS |
| 67 | Error writeText(raw_fd_ostream &OS); |
| 68 | |
| 69 | /// Write \c Record in text format to \c OS |
| 70 | static void writeRecordInText(StringRef Name, uint64_t Hash, |
| 71 | const InstrProfRecord &Counters, |
| 72 | InstrProfSymtab &Symtab, raw_fd_ostream &OS); |
| 73 | |
| 74 | /// Write the profile, returning the raw data. For testing. |
| 75 | std::unique_ptr<MemoryBuffer> writeBuffer(); |
| 76 | |
| 77 | /// Set the ProfileKind. Report error if mixing FE and IR level profiles. |
| 78 | Error setIsIRLevelProfile(bool IsIRLevel) { |
| 79 | if (ProfileKind == PF_Unknown) { |
| 80 | ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE; |
| 81 | return Error::success(); |
| 82 | } |
| 83 | return (IsIRLevel == (ProfileKind == PF_IRLevel)) |
| 84 | ? Error::success() |
| 85 | : make_error<InstrProfError>( |
| 86 | instrprof_error::unsupported_version); |
| 87 | } |
| 88 | |
| 89 | // Internal interface for testing purpose only. |
| 90 | void setValueProfDataEndianness(support::endianness Endianness); |
| 91 | void setOutputSparse(bool Sparse); |
| 92 | |
| 93 | private: |
| 94 | void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I, |
| 95 | uint64_t Weight, function_ref<void(Error)> Warn); |
| 96 | bool shouldEncodeData(const ProfilingData &PD); |
| 97 | void writeImpl(ProfOStream &OS); |
| 98 | }; |
| 99 | |
| 100 | } // end namespace llvm |
| 101 | |
| 102 | #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H |