#ifndef LLVM_PROFILEDATA_INSTRPROF_H
#define LLVM_PROFILEDATA_INSTRPROF_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Triple.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/ProfileSummary.h"
#include "llvm/ProfileData/InstrProfData.inc"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <list>
#include <memory>
#include <string>
#include <system_error>
#include <utility>
#include <vector>
namespace llvm {
class Function;
class GlobalVariable;
struct InstrProfRecord;
class InstrProfSymtab;
class Instruction;
class MDNode;
class Module;
enum InstrProfSectKind {
#define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Kind,
#include "llvm/ProfileData/InstrProfData.inc"
};
std::string getInstrProfSectionName(InstrProfSectKind IPSK,
Triple::ObjectFormatType OF,
bool AddSegmentInfo = true);
inline StringRef getInstrProfValueProfFuncName() {
return INSTR_PROF_VALUE_PROF_FUNC_STR;
}
inline StringRef getInstrProfValueProfMemOpFuncName() {
return INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR;
}
inline StringRef getInstrProfNameVarPrefix() { return "__profn_"; }
inline StringRef getInstrProfDataVarPrefix() { return "__profd_"; }
inline StringRef getInstrProfCountersVarPrefix() { return "__profc_"; }
inline StringRef getInstrProfValuesVarPrefix() { return "__profvp_"; }
inline StringRef getInstrProfVNodesVarName() { return "__llvm_prf_vnodes"; }
inline StringRef getInstrProfNamesVarName() {
return "__llvm_prf_nm";
}
inline StringRef getCoverageMappingVarName() {
return "__llvm_coverage_mapping";
}
inline StringRef getCoverageUnusedNamesVarName() {
return "__llvm_coverage_names";
}
inline StringRef getInstrProfRegFuncsName() {
return "__llvm_profile_register_functions";
}
inline StringRef getInstrProfRegFuncName() {
return "__llvm_profile_register_function";
}
inline StringRef getInstrProfNamesRegFuncName() {
return "__llvm_profile_register_names_function";
}
inline StringRef getInstrProfInitFuncName() { return "__llvm_profile_init"; }
inline StringRef getInstrProfRuntimeHookVarName() {
return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_RUNTIME_VAR);
}
inline StringRef getInstrProfRuntimeHookVarUseFuncName() {
return "__llvm_profile_runtime_user";
}
inline StringRef getInstrProfCounterBiasVarName() {
return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR);
}
inline StringRef getInstrProfNameSeparator() { return "\01"; }
std::string getPGOFuncName(const Function &F, bool InLTO = false,
uint64_t Version = INSTR_PROF_INDEX_VERSION);
std::string getPGOFuncName(StringRef RawFuncName,
GlobalValue::LinkageTypes Linkage,
StringRef FileName,
uint64_t Version = INSTR_PROF_INDEX_VERSION);
std::string getPGOFuncNameVarName(StringRef FuncName,
GlobalValue::LinkageTypes Linkage);
GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName);
GlobalVariable *createPGOFuncNameVar(Module &M,
GlobalValue::LinkageTypes Linkage,
StringRef PGOFuncName);
StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar);
StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
StringRef FileName = "<unknown>");
Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
bool doCompression, std::string &Result);
Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
std::string &Result, bool doCompression = true);
Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab);
bool isIRPGOFlagSet(const Module *M);
bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken = false);
enum InstrProfValueKind : uint32_t {
#define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
#include "llvm/ProfileData/InstrProfData.inc"
};
void annotateValueSite(Module &M, Instruction &Inst,
const InstrProfRecord &InstrProfR,
InstrProfValueKind ValueKind, uint32_t SiteIndx,
uint32_t MaxMDCount = 3);
void annotateValueSite(Module &M, Instruction &Inst,
ArrayRef<InstrProfValueData> VDs, uint64_t Sum,
InstrProfValueKind ValueKind, uint32_t MaxMDCount);
bool getValueProfDataFromInst(const Instruction &Inst,
InstrProfValueKind ValueKind,
uint32_t MaxNumValueData,
InstrProfValueData ValueData[],
uint32_t &ActualNumValueData, uint64_t &TotalC,
bool GetNoICPValue = false);
inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }
MDNode *getPGOFuncNameMetadata(const Function &F);
void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName);
bool needsComdatForCounter(const Function &F, const Module &M);
enum class InstrProfKind {
Unknown = 0x0,
FrontendInstrumentation = 0x1,
IRInstrumentation = 0x2,
FunctionEntryInstrumentation = 0x4,
ContextSensitive = 0x8,
SingleByteCoverage = 0x10,
FunctionEntryOnly = 0x20,
MemProf = 0x40,
LLVM_MARK_AS_BITMASK_ENUM(MemProf)
};
const std::error_category &instrprof_category();
enum class instrprof_error {
success = 0,
eof,
unrecognized_format,
bad_magic,
bad_header,
unsupported_version,
unsupported_hash_type,
too_large,
truncated,
malformed,
missing_debug_info_for_correlation,
unexpected_debug_info_for_correlation,
unable_to_correlate_profile,
unknown_function,
invalid_prof,
hash_mismatch,
count_mismatch,
counter_overflow,
value_site_count_mismatch,
compress_failed,
uncompress_failed,
empty_raw_profile,
zlib_unavailable
};
inline std::error_code make_error_code(instrprof_error E) {
return std::error_code(static_cast<int>(E), instrprof_category());
}
class InstrProfError : public ErrorInfo<InstrProfError> {
public:
InstrProfError(instrprof_error Err, const Twine &ErrStr = Twine())
: Err(Err), Msg(ErrStr.str()) {
assert(Err != instrprof_error::success && "Not an error");
}
std::string message() const override;
void log(raw_ostream &OS) const override { OS << message(); }
std::error_code convertToErrorCode() const override {
return make_error_code(Err);
}
instrprof_error get() const { return Err; }
const std::string &getMessage() const { return Msg; }
static instrprof_error take(Error E) {
auto Err = instrprof_error::success;
handleAllErrors(std::move(E), [&Err](const InstrProfError &IPE) {
assert(Err == instrprof_error::success && "Multiple errors encountered");
Err = IPE.get();
});
return Err;
}
static char ID;
private:
instrprof_error Err;
std::string Msg;
};
class SoftInstrProfErrors {
instrprof_error FirstError = instrprof_error::success;
unsigned NumHashMismatches = 0;
unsigned NumCountMismatches = 0;
unsigned NumCounterOverflows = 0;
unsigned NumValueSiteCountMismatches = 0;
public:
SoftInstrProfErrors() = default;
~SoftInstrProfErrors() {
assert(FirstError == instrprof_error::success &&
"Unchecked soft error encountered");
}
void addError(instrprof_error IE);
unsigned getNumHashMismatches() const { return NumHashMismatches; }
unsigned getNumCountMismatches() const { return NumCountMismatches; }
unsigned getNumCounterOverflows() const { return NumCounterOverflows; }
unsigned getNumValueSiteCountMismatches() const {
return NumValueSiteCountMismatches;
}
Error takeError() {
if (FirstError == instrprof_error::success)
return Error::success();
auto E = make_error<InstrProfError>(FirstError);
FirstError = instrprof_error::success;
return E;
}
};
namespace object {
class SectionRef;
}
namespace IndexedInstrProf {
uint64_t ComputeHash(StringRef K);
}
class InstrProfSymtab {
public:
using AddrHashMap = std::vector<std::pair<uint64_t, uint64_t>>;
private:
StringRef Data;
uint64_t Address = 0;
StringSet<> NameTab;
std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;
AddrHashMap AddrToMD5Map;
bool Sorted = false;
static StringRef getExternalSymbol() {
return "** External Symbol **";
}
inline void finalizeSymtab();
public:
InstrProfSymtab() = default;
Error create(object::SectionRef &Section);
inline Error create(StringRef D, uint64_t BaseAddr);
inline Error create(StringRef NameStrings);
Error create(Module &M, bool InLTO = false);
template <typename NameIterRange> Error create(const NameIterRange &IterRange);
Error addFuncName(StringRef FuncName) {
if (FuncName.empty())
return make_error<InstrProfError>(instrprof_error::malformed,
"function name is empty");
auto Ins = NameTab.insert(FuncName);
if (Ins.second) {
MD5NameMap.push_back(std::make_pair(
IndexedInstrProf::ComputeHash(FuncName), Ins.first->getKey()));
Sorted = false;
}
return Error::success();
}
void mapAddress(uint64_t Addr, uint64_t MD5Val) {
AddrToMD5Map.push_back(std::make_pair(Addr, MD5Val));
}
uint64_t getFunctionHashFromAddress(uint64_t Address);
StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
inline StringRef getFuncName(uint64_t FuncMD5Hash);
inline StringRef getFuncNameOrExternalSymbol(uint64_t FuncMD5Hash);
static bool isExternalSymbol(const StringRef &Symbol) {
return Symbol == InstrProfSymtab::getExternalSymbol();
}
inline Function *getFunction(uint64_t FuncMD5Hash);
inline StringRef getOrigFuncName(uint64_t FuncMD5Hash);
inline StringRef getNameData() const { return Data; }
void dumpNames(raw_ostream &OS) const {
for (StringRef S : NameTab.keys())
OS << S << "\n";
}
};
Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
Data = D;
Address = BaseAddr;
return Error::success();
}
Error InstrProfSymtab::create(StringRef NameStrings) {
return readPGOFuncNameStrings(NameStrings, *this);
}
template <typename NameIterRange>
Error InstrProfSymtab::create(const NameIterRange &IterRange) {
for (auto Name : IterRange)
if (Error E = addFuncName(Name))
return E;
finalizeSymtab();
return Error::success();
}
void InstrProfSymtab::finalizeSymtab() {
if (Sorted)
return;
llvm::sort(MD5NameMap, less_first());
llvm::sort(MD5FuncMap, less_first());
llvm::sort(AddrToMD5Map, less_first());
AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()),
AddrToMD5Map.end());
Sorted = true;
}
StringRef InstrProfSymtab::getFuncNameOrExternalSymbol(uint64_t FuncMD5Hash) {
StringRef ret = getFuncName(FuncMD5Hash);
if (ret.empty())
return InstrProfSymtab::getExternalSymbol();
return ret;
}
StringRef InstrProfSymtab::getFuncName(uint64_t FuncMD5Hash) {
finalizeSymtab();
auto Result = llvm::lower_bound(MD5NameMap, FuncMD5Hash,
[](const std::pair<uint64_t, StringRef> &LHS,
uint64_t RHS) { return LHS.first < RHS; });
if (Result != MD5NameMap.end() && Result->first == FuncMD5Hash)
return Result->second;
return StringRef();
}
Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
finalizeSymtab();
auto Result = llvm::lower_bound(MD5FuncMap, FuncMD5Hash,
[](const std::pair<uint64_t, Function *> &LHS,
uint64_t RHS) { return LHS.first < RHS; });
if (Result != MD5FuncMap.end() && Result->first == FuncMD5Hash)
return Result->second;
return nullptr;
}
StringRef InstrProfSymtab::getOrigFuncName(uint64_t FuncMD5Hash) {
StringRef PGOName = getFuncName(FuncMD5Hash);
size_t S = PGOName.find_first_of(':');
if (S == StringRef::npos)
return PGOName;
return PGOName.drop_front(S + 1);
}
struct CountSumOrPercent {
uint64_t NumEntries;
double CountSum;
double ValueCounts[IPVK_Last - IPVK_First + 1];
CountSumOrPercent() : NumEntries(0), CountSum(0.0f), ValueCounts() {}
void reset() {
NumEntries = 0;
CountSum = 0.0f;
for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++)
ValueCounts[I] = 0.0f;
}
};
struct OverlapStats {
enum OverlapStatsLevel { ProgramLevel, FunctionLevel };
CountSumOrPercent Base;
CountSumOrPercent Test;
CountSumOrPercent Overlap;
CountSumOrPercent Mismatch;
CountSumOrPercent Unique;
OverlapStatsLevel Level;
const std::string *BaseFilename;
const std::string *TestFilename;
StringRef FuncName;
uint64_t FuncHash;
bool Valid;
OverlapStats(OverlapStatsLevel L = ProgramLevel)
: Level(L), BaseFilename(nullptr), TestFilename(nullptr), FuncHash(0),
Valid(false) {}
void dump(raw_fd_ostream &OS) const;
void setFuncInfo(StringRef Name, uint64_t Hash) {
FuncName = Name;
FuncHash = Hash;
}
Error accumulateCounts(const std::string &BaseFilename,
const std::string &TestFilename, bool IsCS);
void addOneMismatch(const CountSumOrPercent &MismatchFunc);
void addOneUnique(const CountSumOrPercent &UniqueFunc);
static inline double score(uint64_t Val1, uint64_t Val2, double Sum1,
double Sum2) {
if (Sum1 < 1.0f || Sum2 < 1.0f)
return 0.0f;
return std::min(Val1 / Sum1, Val2 / Sum2);
}
};
struct OverlapFuncFilters {
uint64_t ValueCutoff;
const std::string NameFilter;
};
struct InstrProfValueSiteRecord {
std::list<InstrProfValueData> ValueData;
InstrProfValueSiteRecord() { ValueData.clear(); }
template <class InputIterator>
InstrProfValueSiteRecord(InputIterator F, InputIterator L)
: ValueData(F, L) {}
void sortByTargetValues() {
ValueData.sort(
[](const InstrProfValueData &left, const InstrProfValueData &right) {
return left.Value < right.Value;
});
}
inline void sortByCount();
void merge(InstrProfValueSiteRecord &Input, uint64_t Weight,
function_ref<void(instrprof_error)> Warn);
void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
void overlap(InstrProfValueSiteRecord &Input, uint32_t ValueKind,
OverlapStats &Overlap, OverlapStats &FuncLevelOverlap);
};
struct InstrProfRecord {
std::vector<uint64_t> Counts;
InstrProfRecord() = default;
InstrProfRecord(std::vector<uint64_t> Counts) : Counts(std::move(Counts)) {}
InstrProfRecord(InstrProfRecord &&) = default;
InstrProfRecord(const InstrProfRecord &RHS)
: Counts(RHS.Counts),
ValueData(RHS.ValueData
? std::make_unique<ValueProfData>(*RHS.ValueData)
: nullptr) {}
InstrProfRecord &operator=(InstrProfRecord &&) = default;
InstrProfRecord &operator=(const InstrProfRecord &RHS) {
Counts = RHS.Counts;
if (!RHS.ValueData) {
ValueData = nullptr;
return *this;
}
if (!ValueData)
ValueData = std::make_unique<ValueProfData>(*RHS.ValueData);
else
*ValueData = *RHS.ValueData;
return *this;
}
inline uint32_t getNumValueKinds() const;
inline uint32_t getNumValueSites(uint32_t ValueKind) const;
inline uint32_t getNumValueData(uint32_t ValueKind) const;
inline uint32_t getNumValueDataForSite(uint32_t ValueKind,
uint32_t Site) const;
inline std::unique_ptr<InstrProfValueData[]>
getValueForSite(uint32_t ValueKind, uint32_t Site,
uint64_t *TotalC = nullptr) const;
inline uint64_t getValueForSite(InstrProfValueData Dest[], uint32_t ValueKind,
uint32_t Site) const;
inline void reserveSites(uint32_t ValueKind, uint32_t NumValueSites);
void addValueData(uint32_t ValueKind, uint32_t Site,
InstrProfValueData *VData, uint32_t N,
InstrProfSymtab *SymTab);
void merge(InstrProfRecord &Other, uint64_t Weight,
function_ref<void(instrprof_error)> Warn);
void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
void sortValueData() {
for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
for (auto &SR : getValueSitesForKind(Kind))
SR.sortByCount();
}
void Clear() {
Counts.clear();
clearValueData();
}
void clearValueData() { ValueData = nullptr; }
void accumulateCounts(CountSumOrPercent &Sum) const;
void overlap(InstrProfRecord &Other, OverlapStats &Overlap,
OverlapStats &FuncLevelOverlap, uint64_t ValueCutoff);
void overlapValueProfData(uint32_t ValueKind, InstrProfRecord &Src,
OverlapStats &Overlap,
OverlapStats &FuncLevelOverlap);
private:
struct ValueProfData {
std::vector<InstrProfValueSiteRecord> IndirectCallSites;
std::vector<InstrProfValueSiteRecord> MemOPSizes;
};
std::unique_ptr<ValueProfData> ValueData;
MutableArrayRef<InstrProfValueSiteRecord>
getValueSitesForKind(uint32_t ValueKind) {
auto AR = const_cast<const InstrProfRecord *>(this)->getValueSitesForKind(
ValueKind);
return makeMutableArrayRef(
const_cast<InstrProfValueSiteRecord *>(AR.data()), AR.size());
}
ArrayRef<InstrProfValueSiteRecord>
getValueSitesForKind(uint32_t ValueKind) const {
if (!ValueData)
return None;
switch (ValueKind) {
case IPVK_IndirectCallTarget:
return ValueData->IndirectCallSites;
case IPVK_MemOPSize:
return ValueData->MemOPSizes;
default:
llvm_unreachable("Unknown value kind!");
}
}
std::vector<InstrProfValueSiteRecord> &
getOrCreateValueSitesForKind(uint32_t ValueKind) {
if (!ValueData)
ValueData = std::make_unique<ValueProfData>();
switch (ValueKind) {
case IPVK_IndirectCallTarget:
return ValueData->IndirectCallSites;
case IPVK_MemOPSize:
return ValueData->MemOPSizes;
default:
llvm_unreachable("Unknown value kind!");
}
}
uint64_t remapValue(uint64_t Value, uint32_t ValueKind,
InstrProfSymtab *SymTab);
void mergeValueProfData(uint32_t ValkeKind, InstrProfRecord &Src,
uint64_t Weight,
function_ref<void(instrprof_error)> Warn);
void scaleValueProfData(uint32_t ValueKind, uint64_t N, uint64_t D,
function_ref<void(instrprof_error)> Warn);
};
struct NamedInstrProfRecord : InstrProfRecord {
StringRef Name;
uint64_t Hash;
static const int CS_FLAG_IN_FUNC_HASH = 60;
NamedInstrProfRecord() = default;
NamedInstrProfRecord(StringRef Name, uint64_t Hash,
std::vector<uint64_t> Counts)
: InstrProfRecord(std::move(Counts)), Name(Name), Hash(Hash) {}
static bool hasCSFlagInHash(uint64_t FuncHash) {
return ((FuncHash >> CS_FLAG_IN_FUNC_HASH) & 1);
}
static void setCSFlagInHash(uint64_t &FuncHash) {
FuncHash |= ((uint64_t)1 << CS_FLAG_IN_FUNC_HASH);
}
};
uint32_t InstrProfRecord::getNumValueKinds() const {
uint32_t NumValueKinds = 0;
for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
NumValueKinds += !(getValueSitesForKind(Kind).empty());
return NumValueKinds;
}
uint32_t InstrProfRecord::getNumValueData(uint32_t ValueKind) const {
uint32_t N = 0;
for (auto &SR : getValueSitesForKind(ValueKind))
N += SR.ValueData.size();
return N;
}
uint32_t InstrProfRecord::getNumValueSites(uint32_t ValueKind) const {
return getValueSitesForKind(ValueKind).size();
}
uint32_t InstrProfRecord::getNumValueDataForSite(uint32_t ValueKind,
uint32_t Site) const {
return getValueSitesForKind(ValueKind)[Site].ValueData.size();
}
std::unique_ptr<InstrProfValueData[]>
InstrProfRecord::getValueForSite(uint32_t ValueKind, uint32_t Site,
uint64_t *TotalC) const {
uint64_t Dummy = 0;
uint64_t &TotalCount = (TotalC == nullptr ? Dummy : *TotalC);
uint32_t N = getNumValueDataForSite(ValueKind, Site);
if (N == 0) {
TotalCount = 0;
return std::unique_ptr<InstrProfValueData[]>(nullptr);
}
auto VD = std::make_unique<InstrProfValueData[]>(N);
TotalCount = getValueForSite(VD.get(), ValueKind, Site);
return VD;
}
uint64_t InstrProfRecord::getValueForSite(InstrProfValueData Dest[],
uint32_t ValueKind,
uint32_t Site) const {
uint32_t I = 0;
uint64_t TotalCount = 0;
for (auto V : getValueSitesForKind(ValueKind)[Site].ValueData) {
Dest[I].Value = V.Value;
Dest[I].Count = V.Count;
TotalCount = SaturatingAdd(TotalCount, V.Count);
I++;
}
return TotalCount;
}
void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {
if (!NumValueSites)
return;
getOrCreateValueSitesForKind(ValueKind).reserve(NumValueSites);
}
inline support::endianness getHostEndianness() {
return sys::IsLittleEndianHost ? support::little : support::big;
}
#define INSTR_PROF_VALUE_PROF_DATA
#include "llvm/ProfileData/InstrProfData.inc"
void InstrProfValueSiteRecord::sortByCount() {
ValueData.sort(
[](const InstrProfValueData &left, const InstrProfValueData &right) {
return left.Count > right.Count;
});
size_t max_s = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
if (ValueData.size() > max_s)
ValueData.resize(max_s);
}
namespace IndexedInstrProf {
enum class HashT : uint32_t {
MD5,
Last = MD5
};
inline uint64_t ComputeHash(HashT Type, StringRef K) {
switch (Type) {
case HashT::MD5:
return MD5Hash(K);
}
llvm_unreachable("Unhandled hash type");
}
const uint64_t Magic = 0x8169666f72706cff;
enum ProfVersion {
Version1 = 1,
Version2 = 2,
Version3 = 3,
Version4 = 4,
Version5 = 5,
Version6 = 6,
Version7 = 7,
Version8 = 8,
CurrentVersion = INSTR_PROF_INDEX_VERSION
};
const uint64_t Version = ProfVersion::CurrentVersion;
const HashT HashType = HashT::MD5;
inline uint64_t ComputeHash(StringRef K) { return ComputeHash(HashType, K); }
struct Header {
uint64_t Magic;
uint64_t Version;
uint64_t Unused; uint64_t HashType;
uint64_t HashOffset;
uint64_t MemProfOffset;
static Expected<Header> readFromBuffer(const unsigned char *Buffer);
size_t size() const;
uint64_t formatVersion() const;
};
struct Summary {
struct Entry {
uint64_t Cutoff; uint64_t
MinBlockCount; uint64_t NumBlocks; };
enum SummaryFieldKind {
TotalNumFunctions = 0,
TotalNumBlocks = 1,
MaxFunctionCount = 2,
MaxBlockCount = 3,
MaxInternalBlockCount = 4,
TotalBlockCount = 5,
NumKinds = TotalBlockCount + 1
};
uint64_t NumSummaryFields;
uint64_t NumCutoffEntries;
Summary() = delete;
Summary(uint32_t Size) { memset(this, 0, Size); }
void operator delete(void *ptr) { ::operator delete(ptr); }
static uint32_t getSize(uint32_t NumSumFields, uint32_t NumCutoffEntries) {
return sizeof(Summary) + NumCutoffEntries * sizeof(Entry) +
NumSumFields * sizeof(uint64_t);
}
const uint64_t *getSummaryDataBase() const {
return reinterpret_cast<const uint64_t *>(this + 1);
}
uint64_t *getSummaryDataBase() {
return reinterpret_cast<uint64_t *>(this + 1);
}
const Entry *getCutoffEntryBase() const {
return reinterpret_cast<const Entry *>(
&getSummaryDataBase()[NumSummaryFields]);
}
Entry *getCutoffEntryBase() {
return reinterpret_cast<Entry *>(&getSummaryDataBase()[NumSummaryFields]);
}
uint64_t get(SummaryFieldKind K) const {
return getSummaryDataBase()[K];
}
void set(SummaryFieldKind K, uint64_t V) {
getSummaryDataBase()[K] = V;
}
const Entry &getEntry(uint32_t I) const { return getCutoffEntryBase()[I]; }
void setEntry(uint32_t I, const ProfileSummaryEntry &E) {
Entry &ER = getCutoffEntryBase()[I];
ER.Cutoff = E.Cutoff;
ER.MinBlockCount = E.MinCount;
ER.NumBlocks = E.NumCounts;
}
};
inline std::unique_ptr<Summary> allocSummary(uint32_t TotalSize) {
return std::unique_ptr<Summary>(new (::operator new(TotalSize))
Summary(TotalSize));
}
}
namespace RawInstrProf {
const uint64_t Version = INSTR_PROF_RAW_VERSION;
template <class IntPtrT> inline uint64_t getMagic();
template <> inline uint64_t getMagic<uint64_t>() {
return INSTR_PROF_RAW_MAGIC_64;
}
template <> inline uint64_t getMagic<uint32_t>() {
return INSTR_PROF_RAW_MAGIC_32;
}
template <class IntPtrT> struct alignas(8) ProfileData {
#define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Type Name;
#include "llvm/ProfileData/InstrProfData.inc"
};
struct Header {
#define INSTR_PROF_RAW_HEADER(Type, Name, Init) const Type Name;
#include "llvm/ProfileData/InstrProfData.inc"
};
}
void getMemOPSizeRangeFromOption(StringRef Str, int64_t &RangeStart,
int64_t &RangeLast);
void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput);
extern cl::opt<bool> DoInstrProfNameCompression;
} #endif