| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 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 |
|
| 37 |
private: |
| 38 |
bool Sparse; |
| 39 |
StringMap<ProfilingData> FunctionData; |
| 40 |
|
| 41 |
InstrProfKind ProfileKind = InstrProfKind::Unknown; |
| 42 |
|
| 43 |
InstrProfRecordWriterTrait *InfoObj; |
| 44 |
|
| 45 |
public: |
| 46 |
InstrProfWriter(bool Sparse = false); |
| 47 |
~InstrProfWriter(); |
| 48 |
|
| 49 |
StringMap<ProfilingData> &getProfileData() { return FunctionData; } |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, |
| 55 |
function_ref<void(Error)> Warn); |
| 56 |
void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) { |
| 57 |
addRecord(std::move(I), 1, Warn); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
void mergeRecordsFromWriter(InstrProfWriter &&IPW, |
| 62 |
function_ref<void(Error)> Warn); |
| 63 |
|
| 64 |
|
| 65 |
Error write(raw_fd_ostream &OS); |
| 66 |
|
| 67 |
|
| 68 |
Error writeText(raw_fd_ostream &OS); |
| 69 |
|
| 70 |
Error validateRecord(const InstrProfRecord &Func); |
| 71 |
|
| 72 |
|
| 73 |
static void writeRecordInText(StringRef Name, uint64_t Hash, |
| 74 |
const InstrProfRecord &Counters, |
| 75 |
InstrProfSymtab &Symtab, raw_fd_ostream &OS); |
| 76 |
|
| 77 |
|
| 78 |
std::unique_ptr<MemoryBuffer> writeBuffer(); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
Error mergeProfileKind(const InstrProfKind Other) { |
| 83 |
|
| 84 |
|
| 85 |
if (ProfileKind == InstrProfKind::Unknown) { |
| 86 |
ProfileKind = Other; |
| 87 |
return Error::success(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) { |
| 92 |
return (static_cast<bool>(ProfileKind & A) && |
| 93 |
static_cast<bool>(Other & B)) || |
| 94 |
(static_cast<bool>(ProfileKind & B) && |
| 95 |
static_cast<bool>(Other & A)); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if (static_cast<bool>((ProfileKind & InstrProfKind::FE) ^ |
| 101 |
(Other & InstrProfKind::FE))) { |
| 102 |
return make_error<InstrProfError>(instrprof_error::unsupported_version); |
| 103 |
} |
| 104 |
if (testIncompatible(InstrProfKind::FunctionEntryOnly, InstrProfKind::BB)) { |
| 105 |
return make_error<InstrProfError>( |
| 106 |
instrprof_error::unsupported_version, |
| 107 |
"cannot merge FunctionEntryOnly profiles and BB profiles together"); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
ProfileKind |= Other; |
| 112 |
return Error::success(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void setValueProfDataEndianness(support::endianness Endianness); |
| 117 |
void setOutputSparse(bool Sparse); |
| 118 |
|
| 119 |
|
| 120 |
void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, |
| 121 |
OverlapStats &FuncLevelOverlap, |
| 122 |
const OverlapFuncFilters &FuncFilter); |
| 123 |
|
| 124 |
private: |
| 125 |
void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I, |
| 126 |
uint64_t Weight, function_ref<void(Error)> Warn); |
| 127 |
bool shouldEncodeData(const ProfilingData &PD); |
| 128 |
|
| 129 |
Error writeImpl(ProfOStream &OS); |
| 130 |
}; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
#endif |