#ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
#define LLVM_PROFILEDATA_INSTRPROFWRITER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/MemProf.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include <cstdint>
#include <memory>
namespace llvm {
class InstrProfRecordWriterTrait;
class ProfOStream;
class MemoryBuffer;
class raw_fd_ostream;
class InstrProfWriter {
public:
using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
private:
bool Sparse;
StringMap<ProfilingData> FunctionData;
llvm::MapVector<GlobalValue::GUID, memprof::IndexedMemProfRecord>
MemProfRecordData;
llvm::MapVector<memprof::FrameId, memprof::Frame> MemProfFrameData;
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 addMemProfRecord(const GlobalValue::GUID Id,
const memprof::IndexedMemProfRecord &Record);
bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F,
function_ref<void(Error)> 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::FrontendInstrumentation) ^
(Other & InstrProfKind::FrontendInstrumentation))) {
return make_error<InstrProfError>(instrprof_error::unsupported_version);
}
if (testIncompatible(InstrProfKind::FunctionEntryOnly,
InstrProfKind::FunctionEntryInstrumentation)) {
return make_error<InstrProfError>(
instrprof_error::unsupported_version,
"cannot merge FunctionEntryOnly profiles and BB profiles together");
}
ProfileKind |= Other;
return Error::success();
}
InstrProfKind getProfileKind() const { return ProfileKind; }
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