#ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
#define LLVM_PROFILEDATA_INSTRPROFWRITER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cstdint>
#include <memory>
namespace llvm {
class InstrProfRecordWriterTrait;
class ProfOStream;
class raw_fd_ostream;
class InstrProfWriter {
public:
using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
private:
bool Sparse;
StringMap<ProfilingData> FunctionData;
InstrProfKind ProfileKind = InstrProfKind::Unknown;
InstrProfRecordWriterTrait *InfoObj;
public:
InstrProfWriter(bool Sparse = false);
~InstrProfWriter();
StringMap<ProfilingData> &getProfileData() { return FunctionData; }
void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
function_ref<void(Error)> Warn);
void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
addRecord(std::move(I), 1, Warn);
}
void mergeRecordsFromWriter(InstrProfWriter &&IPW,
function_ref<void(Error)> Warn);
Error write(raw_fd_ostream &OS);
Error writeText(raw_fd_ostream &OS);
Error validateRecord(const InstrProfRecord &Func);
static void writeRecordInText(StringRef Name, uint64_t Hash,
const InstrProfRecord &Counters,
InstrProfSymtab &Symtab, raw_fd_ostream &OS);
std::unique_ptr<MemoryBuffer> writeBuffer();
Error mergeProfileKind(const InstrProfKind Other) {
if (ProfileKind == InstrProfKind::Unknown) {
ProfileKind = Other;
return Error::success();
}
auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
return (static_cast<bool>(ProfileKind & A) &&
static_cast<bool>(Other & B)) ||
(static_cast<bool>(ProfileKind & B) &&
static_cast<bool>(Other & A));
};
if (static_cast<bool>((ProfileKind & InstrProfKind::FE) ^
(Other & InstrProfKind::FE))) {
return make_error<InstrProfError>(instrprof_error::unsupported_version);
}
if (testIncompatible(InstrProfKind::FunctionEntryOnly, InstrProfKind::BB)) {
return make_error<InstrProfError>(
instrprof_error::unsupported_version,
"cannot merge FunctionEntryOnly profiles and BB profiles together");
}
ProfileKind |= Other;
return Error::success();
}
void setValueProfDataEndianness(support::endianness Endianness);
void setOutputSparse(bool Sparse);
void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
OverlapStats &FuncLevelOverlap,
const OverlapFuncFilters &FuncFilter);
private:
void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
uint64_t Weight, function_ref<void(Error)> Warn);
bool shouldEncodeData(const ProfilingData &PD);
Error writeImpl(ProfOStream &OS);
};
}
#endif