37#include <system_error>
78 if (std::error_code EC = BufferOrErr.getError())
80 return std::move(BufferOrErr.get());
98 std::vector<llvm::object::BuildID> &BinaryIds,
103 const uint8_t *BinaryIdsStart = BinaryIdsBuffer.
data();
105 if (BinaryIdsSize == 0)
108 const uint8_t *BI = BinaryIdsStart;
109 const uint8_t *BIEnd = BinaryIdsStart + BinaryIdsSize;
114 size_t Remaining = BIEnd - BI;
119 "not enough data to read binary id length");
124 "binary id length is 0");
126 Remaining = BIEnd - BI;
140 "binary id section is greater than buffer size");
148 OS <<
"Binary IDs: \n";
149 for (
const auto &BI : BinaryIds) {
160 std::optional<ProfCorrelatorKind> BIDFetcherCorrelatorKind,
161 std::function<
void(
Error)> Warn) {
164 if (
Error E = BufferOrError.takeError())
167 BIDFetcher, BIDFetcherCorrelatorKind, Warn);
173 std::optional<ProfCorrelatorKind> BIDFetcherCorrelatorKind,
174 std::function<
void(
Error)> Warn) {
175 if (Buffer->getBufferSize() == 0)
178 std::unique_ptr<InstrProfReader> Result;
184 BIDFetcher, BIDFetcherCorrelatorKind,
188 BIDFetcher, BIDFetcherCorrelatorKind,
199 return std::move(Result);
204 const Twine &RemappingPath) {
207 if (
Error E = BufferOrError.takeError())
211 std::unique_ptr<MemoryBuffer> RemappingBuffer;
212 std::string RemappingPathStr = RemappingPath.
str();
213 if (!RemappingPathStr.empty()) {
215 if (
Error E = RemappingBufferOrError.takeError())
217 RemappingBuffer = std::move(RemappingBufferOrError.get());
221 std::move(RemappingBuffer));
226 std::unique_ptr<MemoryBuffer> RemappingBuffer) {
230 auto Result = std::make_unique<IndexedInstrProfReader>(
231 std::move(Buffer), std::move(RemappingBuffer));
237 return std::move(Result);
247 [](
char c) { return isPrint(c) || isSpace(c); });
256 while (Line->starts_with(
":")) {
258 if (Str.equals_insensitive(
"ir"))
260 else if (Str.equals_insensitive(
"fe"))
262 else if (Str.equals_insensitive(
"csir")) {
265 }
else if (Str.equals_insensitive(
"entry_first"))
267 else if (Str.equals_insensitive(
"not_entry_first"))
269 else if (Str.equals_insensitive(
"instrument_loop_entries"))
271 else if (Str.equals_insensitive(
"single_byte_coverage"))
273 else if (Str.equals_insensitive(
"temporal_prof_traces")) {
275 if (
auto Err = readTemporalProfTraceData())
276 return error(std::move(Err));
289Error TextInstrProfReader::readTemporalProfTraceData() {
290 if ((++Line).is_at_end())
294 if (Line->getAsInteger(0, NumTraces))
297 if ((++Line).is_at_end())
303 for (
uint32_t i = 0; i < NumTraces; i++) {
304 if ((++Line).is_at_end())
308 if (Line->getAsInteger(0,
Trace.Weight))
311 if ((++Line).is_at_end())
315 Line->split(FuncNames,
",", -1,
false);
316 for (
auto &FuncName : FuncNames)
317 Trace.FunctionNameRefs.push_back(
327#define CHECK_LINE_END(Line) \
328 if (Line.is_at_end()) \
329 return error(instrprof_error::truncated);
330#define READ_NUM(Str, Dst) \
331 if ((Str).getAsInteger(10, (Dst))) \
332 return error(instrprof_error::malformed);
333#define VP_READ_ADVANCE(Val) \
334 CHECK_LINE_END(Line); \
336 READ_NUM((*Line), (Val)); \
339 if (Line.is_at_end())
342 uint32_t NumValueKinds;
343 if (Line->getAsInteger(10, NumValueKinds)) {
347 if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
349 "number of value kinds is invalid");
352 for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
354 if (ValueKind > IPVK_Last)
365 std::vector<InstrProfValueData> CurrentValues;
366 for (uint32_t
V = 0;
V < NumValueData;
V++) {
368 std::pair<StringRef, StringRef> VD = Line->rsplit(
':');
370 if (ValueKind == IPVK_IndirectCallTarget) {
378 }
else if (ValueKind == IPVK_VTableTarget) {
390 CurrentValues.push_back({
Value, TakenCount});
393 assert(CurrentValues.size() == NumValueData);
394 Record.addValueData(ValueKind, S, CurrentValues,
nullptr);
401#undef VP_READ_ADVANCE
406 while (!Line.is_at_end() && (Line->empty() || Line->starts_with(
"#")))
409 if (Line.is_at_end()) {
416 return error(std::move(E));
419 if (Line.is_at_end())
421 if ((Line++)->getAsInteger(0,
Record.Hash))
423 "function hash is not a valid integer");
426 uint64_t NumCounters;
427 if (Line.is_at_end())
429 if ((Line++)->getAsInteger(10, NumCounters))
431 "number of counters is not a valid integer");
432 if (NumCounters == 0)
437 Record.Counts.reserve(NumCounters);
438 for (uint64_t
I = 0;
I < NumCounters; ++
I) {
439 if (Line.is_at_end())
442 if ((Line++)->getAsInteger(10,
Count))
453 Record.BitmapBytes.clear();
456 if ((Line++)->drop_front(1).trim().getAsInteger(0,
NumBitmapBytes))
458 "number of bitmap bytes is not a valid integer");
463 if (Line.is_at_end())
466 if ((Line++)->getAsInteger(0, BitmapByte))
468 "bitmap byte is not a valid integer");
469 Record.BitmapBytes.push_back(BitmapByte);
476 return error(std::move(E));
481template <
class IntPtrT>
486template <
class IntPtrT>
489 std::optional<uint64_t> Weight) {
490 if (TemporalProfTimestamps.empty()) {
495 std::sort(TemporalProfTimestamps.begin(), TemporalProfTimestamps.end());
498 Trace.Weight = *Weight;
499 for (
auto &[TimestampValue, NameRef] : TemporalProfTimestamps)
500 Trace.FunctionNameRefs.push_back(NameRef);
505template <
class IntPtrT>
507 if (DataBuffer.getBufferSize() <
sizeof(uint64_t))
510 *
reinterpret_cast<const uint64_t *
>(DataBuffer.getBufferStart());
515template <
class IntPtrT>
521 std::string(
"profile file header is truncated"));
523 DataBuffer->getBufferStart());
528template <
class IntPtrT>
529Error RawInstrProfReader<IntPtrT>::readNextHeader(
const char *CurrentPos) {
530 const char *
End = DataBuffer->getBufferEnd();
532 while (CurrentPos !=
End && *CurrentPos == 0)
535 if (CurrentPos ==
End)
541 "not enough space for another header");
543 if (
reinterpret_cast<size_t>(CurrentPos) %
alignof(uint64_t))
545 "insufficient padding");
547 uint64_t Magic = *
reinterpret_cast<const uint64_t *
>(CurrentPos);
553 return readHeader(*Header);
556template <
class IntPtrT>
559 StringRef(VNamesStart, VNamesEnd - VNamesStart)))
560 return error(std::move(
E));
562 const IntPtrT FPtr = swap(
I->FunctionPointer);
568 if (VTableBegin !=
nullptr && VTableEnd !=
nullptr) {
570 I != VTableEnd; ++
I) {
571 const IntPtrT VPtr =
swap(
I->VTablePointer);
579 swap(
I->VTableNameHash));
585template <
class IntPtrT>
591 (
"Profile uses raw profile format version = " +
594 "\nPLEASE update this tool to version in the raw profile, or "
595 "regenerate raw profile with expected version.")
599 const uint8_t *BinaryIdStart =
601 const uint8_t *BinaryIdEnd = BinaryIdStart + BinaryIdSize;
602 const uint8_t *BufferEnd = (
const uint8_t *)DataBuffer->getBufferEnd();
603 if (BinaryIdSize %
sizeof(
uint64_t))
606 (
"BinaryIdSize (" +
Twine(BinaryIdSize) +
") is not a multiple of 8")
608 if (BinaryIdEnd > BufferEnd)
610 (
"Header.BinaryIdSize = " +
Twine(BinaryIdSize) +
" bytes; " +
611 Twine(BufferEnd - BinaryIdStart) +
" bytes available")
615 if (!BinaryIdsBuffer.empty()) {
617 BinaryIds, getDataEndianness()))
621 CountersDelta =
swap(Header.CountersDelta);
622 BitmapDelta =
swap(Header.BitmapDelta);
624 NamesDelta =
swap(Header.NamesDelta);
625 auto NumData =
swap(Header.NumData);
626 auto PaddingBytesBeforeCounters =
swap(Header.PaddingBytesBeforeCounters);
627 auto CountersSize =
swap(Header.NumCounters) * getCounterTypeSize();
628 auto PaddingBytesAfterCounters =
swap(Header.PaddingBytesAfterCounters);
630 auto PaddingBytesAfterBitmapBytes =
swap(Header.PaddingBytesAfterBitmapBytes);
631 auto NumUniformCounters =
swap(Header.NumUniformCounters);
632 auto PaddingBytesAfterUniformCounters =
633 swap(Header.PaddingBytesAfterUniformCounters);
634 auto NamesSize =
swap(Header.NamesSize);
635 auto VTableNameSize =
swap(Header.VNamesSize);
636 auto NumVTables =
swap(Header.NumVTables);
639 auto PaddingBytesAfterNames = getNumPaddingBytes(NamesSize);
640 auto PaddingBytesAfterVTableNames = getNumPaddingBytes(VTableNameSize);
642 auto VTableSectionSize =
644 auto PaddingBytesAfterVTableProfData = getNumPaddingBytes(VTableSectionSize);
645 auto UniformCountersSectionSize = NumUniformCounters *
sizeof(
uint64_t);
649 ptrdiff_t CountersOffset = DataOffset + DataSize + PaddingBytesBeforeCounters;
650 ptrdiff_t BitmapOffset =
651 CountersOffset + CountersSize + PaddingBytesAfterCounters;
652 ptrdiff_t UniformCountersOffset =
654 ptrdiff_t NamesOffset = UniformCountersOffset + UniformCountersSectionSize +
655 PaddingBytesAfterUniformCounters;
656 ptrdiff_t VTableProfDataOffset =
657 NamesOffset + NamesSize + PaddingBytesAfterNames;
658 ptrdiff_t VTableNameOffset = VTableProfDataOffset + VTableSectionSize +
659 PaddingBytesAfterVTableProfData;
660 ptrdiff_t ValueDataOffset =
661 VTableNameOffset + VTableNameSize + PaddingBytesAfterVTableNames;
663 auto *
Start =
reinterpret_cast<const char *
>(&Header);
664 if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
668 (
"profile file size (" +
Twine(DataBuffer->getBufferSize()) +
669 " bytes) smaller than expected (at least " +
Twine(ValueDataOffset) +
672 Twine(BinaryIdSize) +
"(BinaryIdSize) + " +
673 Twine(DataSize) +
"(DataSize) + " +
674 Twine(CountersSize) +
"(CountersSize) + " +
676 Twine(UniformCountersSectionSize) +
"(UniformCountersSectionSize) + " +
677 Twine(NamesSize) +
"(NamesSize) + " +
678 Twine(VTableSectionSize) +
"(VTableSectionSize) + " +
679 Twine(VTableNameSize) +
"(VTableNameSize) + " +
680 Twine(PaddingBytesBeforeCounters + PaddingBytesAfterCounters +
681 PaddingBytesAfterBitmapBytes + PaddingBytesAfterUniformCounters +
682 PaddingBytesAfterNames + PaddingBytesAfterVTableProfData +
683 PaddingBytesAfterVTableNames) +
689 std::vector<object::BuildID> BinaryIDs;
690 if (
Error E = readBinaryIds(BinaryIDs))
693 BIDFetcher, BinaryIDs)
694 .moveInto(BIDFetcherCorrelator)) {
697 if (
auto Err = BIDFetcherCorrelator->correlateProfileData(0))
704 if (!(DataSize == 0 && NamesSize == 0 && CountersDelta == 0 &&
705 BitmapDelta == 0 && NamesDelta == 0))
707 Data = Correlator->getDataPointer();
711 }
else if (BIDFetcherCorrelator) {
714 BIDFetcherCorrelator.get());
722 DataEnd =
Data + NumData;
725 Start + VTableProfDataOffset);
726 VTableEnd = VTableBegin + NumVTables;
729 VNamesStart =
Start + VTableNameOffset;
730 VNamesEnd = VNamesStart + VTableNameSize;
735 BitmapStart =
Start + BitmapOffset;
739 ValueDataStart =
reinterpret_cast<const uint8_t *
>(
Start + ValueDataOffset);
741 std::unique_ptr<InstrProfSymtab> NewSymtab = std::make_unique<InstrProfSymtab>();
742 if (
Error E = createSymtab(*NewSymtab))
745 Symtab = std::move(NewSymtab);
749template <
class IntPtrT>
755template <
class IntPtrT>
761template <
class IntPtrT>
762Error RawInstrProfReader<IntPtrT>::readRawCounts(
764 uint32_t NumCounters =
swap(
Data->NumCounters);
765 if (NumCounters == 0)
768 ptrdiff_t CounterBaseOffset =
swap(
Data->CounterPtr) - CountersDelta;
769 if (CounterBaseOffset < 0)
772 (
"counter offset " +
Twine(CounterBaseOffset) +
" is negative").str());
776 (
"counter offset " +
Twine(CounterBaseOffset) +
777 " is greater than the maximum counter offset " +
783 getCounterTypeSize();
784 if (NumCounters > MaxNumCounters)
786 (
"number of counters " +
Twine(NumCounters) +
787 " is greater than the maximum number of counters " +
788 Twine(MaxNumCounters))
792 Record.Counts.reserve(NumCounters);
793 for (uint32_t
I = 0;
I < NumCounters;
I++) {
796 if (
I == 0 && hasTemporalProfile()) {
798 if (TimestampValue != 0 &&
799 TimestampValue != std::numeric_limits<uint64_t>::max()) {
800 TemporalProfTimestamps.emplace_back(TimestampValue,
802 TemporalProfTraceStreamSize = 1;
804 if (hasSingleByteCoverage()) {
813 if (hasSingleByteCoverage()) {
815 Record.Counts.push_back(*Ptr == 0 ? 1 : 0);
818 if (CounterValue > MaxCounterValue && Warn)
822 Record.Counts.push_back(CounterValue);
829template <
class IntPtrT>
833 Record.BitmapBytes.clear();
842 ptrdiff_t BitmapOffset =
swap(
Data->BitmapPtr) - BitmapDelta;
843 if (BitmapOffset < 0)
846 (
"bitmap offset " +
Twine(BitmapOffset) +
" is negative").str());
848 if (BitmapOffset >= BitmapEnd - BitmapStart)
850 (
"bitmap offset " +
Twine(BitmapOffset) +
851 " is greater than the maximum bitmap offset " +
852 Twine(BitmapEnd - BitmapStart - 1))
856 (BitmapEnd - (BitmapStart + BitmapOffset)) /
sizeof(uint8_t);
860 " is greater than the maximum number of bitmap bytes " +
861 Twine(MaxNumBitmapBytes))
865 const char *Ptr = BitmapStart + BitmapOffset +
I;
872template <
class IntPtrT>
873Error RawInstrProfReader<IntPtrT>::readRawUniformCounters(
875 Record.UniformCounts.clear();
880 uint32_t NumCounters =
swap(
Data->NumCounters);
882 ptrdiff_t UniformCounterOffset =
884 if (UniformCounterOffset < 0)
886 (
"uniform counter offset " +
Twine(UniformCounterOffset) +
892 (
"uniform counter offset " +
Twine(UniformCounterOffset) +
893 " is greater than the maximum uniform counter offset " +
900 if (NumCounters > MaxNumCounters)
902 (
"number of uniform counters " +
Twine(NumCounters) +
903 " is greater than the maximum number of uniform counters " +
904 Twine(MaxNumCounters))
907 Record.UniformCounts.reserve(NumCounters);
908 for (uint32_t
I = 0;
I < NumCounters;
I++) {
912 Record.UniformCounts.push_back(CounterValue);
918template <
class IntPtrT>
919Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
922 CurValueDataSize = 0;
924 uint32_t NumValueKinds = 0;
925 for (uint32_t
I = 0;
I < IPVK_Last + 1;
I++)
926 NumValueKinds += (
Data->NumValueSites[
I] != 0);
932 ValueProfData::getValueProfData(
933 ValueDataStart, (
const unsigned char *)DataBuffer->getBufferEnd(),
934 getDataEndianness());
942 VDataPtrOrErr.
get()->deserializeTo(
Record, Symtab.get());
943 CurValueDataSize = VDataPtrOrErr.
get()->getSize();
947template <
class IntPtrT>
953 if (
Error E = readNextHeader(getNextHeaderPos()))
954 return error(std::move(E));
958 return error(std::move(E));
962 return error(std::move(E));
964 Record.OffloadDeviceWaveSize = swap(Data->OffloadDeviceWaveSize);
968 return error(std::move(E));
972 return error(std::move(E));
976 return error(std::move(E));
980 return error(std::move(E));
987template <
class IntPtrT>
989 std::vector<llvm::object::BuildID> &BinaryIds) {
990 BinaryIds.insert(BinaryIds.begin(), this->BinaryIds.begin(),
991 this->BinaryIds.end());
995template <
class IntPtrT>
997 if (!BinaryIds.empty())
1018 const unsigned char *&
D,
const unsigned char *
const End) {
1020 ValueProfData::getValueProfData(
D,
End, ValueProfDataEndianness);
1025 VDataPtrOrErr.
get()->deserializeTo(DataBuffer.back(),
nullptr);
1026 D += VDataPtrOrErr.
get()->TotalSize;
1036 if (
N %
sizeof(uint64_t))
1040 std::vector<uint64_t> CounterBuffer;
1041 std::vector<uint8_t> BitmapByteBuffer;
1042 std::vector<uint8_t> UniformityBitsBuffer;
1044 const unsigned char *
End =
D +
N;
1047 if (
D +
sizeof(uint64_t) >
End)
1052 uint64_t CountsSize =
N /
sizeof(uint64_t) - 1;
1055 if (
D +
sizeof(uint64_t) >
End)
1060 if (
D + CountsSize *
sizeof(uint64_t) >
End)
1063 CounterBuffer.clear();
1064 CounterBuffer.reserve(CountsSize);
1065 for (uint64_t J = 0; J < CountsSize; ++J)
1066 CounterBuffer.push_back(
1071 uint64_t BitmapBytes = 0;
1072 if (
D +
sizeof(uint64_t) >
End)
1075 BitmapByteBuffer.clear();
1076 BitmapByteBuffer.reserve(BitmapBytes);
1081 uint64_t PaddedSize =
alignTo(BitmapBytes,
sizeof(uint64_t));
1082 if (
D + PaddedSize >
End)
1084 for (uint64_t J = 0; J < BitmapBytes; ++J)
1085 BitmapByteBuffer.push_back(
1087 for (uint64_t J = BitmapBytes; J < PaddedSize; ++J)
1091 uint64_t UniformityBitsSize = 0;
1092 if (
D +
sizeof(uint64_t) >
End)
1094 UniformityBitsSize =
1096 uint64_t PaddedUniformitySize =
1097 alignTo(UniformityBitsSize,
sizeof(uint64_t));
1098 if (
D + PaddedUniformitySize >
End)
1100 UniformityBitsBuffer.clear();
1101 UniformityBitsBuffer.reserve(UniformityBitsSize);
1102 for (uint64_t J = 0; J < UniformityBitsSize; ++J)
1103 UniformityBitsBuffer.push_back(
1105 for (uint64_t J = UniformityBitsSize; J < PaddedUniformitySize; ++J)
1109 if (
D + BitmapBytes *
sizeof(uint64_t) >
End)
1111 for (uint64_t J = 0; J < BitmapBytes; ++J)
1112 BitmapByteBuffer.push_back(
static_cast<uint8_t>(
1117 DataBuffer.emplace_back(
K, Hash, std::move(CounterBuffer),
1118 std::move(BitmapByteBuffer),
1119 std::move(UniformityBitsBuffer));
1131template <
typename HashTableImpl>
1134 auto Iter = HashTable->find(FuncName);
1135 if (Iter == HashTable->end())
1141 "profile data is empty");
1146template <
typename HashTableImpl>
1152 Data = *RecordIterator;
1156 "profile data is empty");
1161template <
typename HashTableImpl>
1163 const unsigned char *Buckets,
const unsigned char *
const Payload,
1167 HashTable.reset(HashTableImpl::Create(
1168 Buckets, Payload,
Base,
1169 typename HashTableImpl::InfoType(HashType,
Version)));
1170 RecordIterator = HashTable->data_begin();
1173template <
typename HashTableImpl>
1185 : Underlying(Underlying) {}
1189 return Underlying.getRecords(FuncName,
Data);
1195template <
typename HashTableImpl>
1200 std::unique_ptr<MemoryBuffer> RemapBuffer,
1202 : RemapBuffer(
std::
move(RemapBuffer)), Underlying(Underlying) {
1211 std::pair<StringRef, StringRef> Parts = {
StringRef(), Name};
1214 if (Parts.first.starts_with(
"_Z"))
1216 if (Parts.second.empty())
1226 Out.reserve(OrigName.
size() + Replacement.
size() - ExtractedName.
size());
1227 Out.insert(Out.end(), OrigName.
begin(), ExtractedName.
begin());
1229 Out.insert(Out.end(), ExtractedName.
end(), OrigName.
end());
1233 if (
Error E = Remappings.read(*RemapBuffer))
1235 for (
StringRef Name : Underlying.HashTable->keys()) {
1237 if (
auto Key = Remappings.insert(RealName)) {
1241 MappedNames.insert({
Key, RealName});
1250 if (
auto Key = Remappings.lookup(RealName)) {
1252 if (!Remapped.
empty()) {
1254 RealName.
end() == FuncName.
end())
1255 FuncName = Remapped;
1260 Error E = Underlying.getRecords(Reconstituted,
Data);
1267 std::move(E), [](std::unique_ptr<InstrProfError> Err) {
1270 :
Error(std::move(Err));
1276 return Underlying.getRecords(FuncName,
Data);
1282 std::unique_ptr<MemoryBuffer> RemapBuffer;
1300 if (DataBuffer.getBufferSize() < 8)
1308const unsigned char *
1310 const unsigned char *Cur,
bool UseCS) {
1323 std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
1326 const uint64_t *Src =
reinterpret_cast<const uint64_t *
>(SummaryInLE);
1327 uint64_t *Dst =
reinterpret_cast<uint64_t *
>(SummaryData.get());
1328 for (
unsigned I = 0;
I < SummarySize /
sizeof(uint64_t);
I++)
1332 for (
unsigned I = 0;
I < SummaryData->NumCutoffEntries;
I++) {
1337 std::unique_ptr<llvm::ProfileSummary> &Summary =
1338 UseCS ? this->CS_Summary : this->Summary;
1341 Summary = std::make_unique<ProfileSummary>(
1343 DetailedSummary, SummaryData->get(Summary::TotalBlockCount),
1344 SummaryData->get(Summary::MaxBlockCount),
1345 SummaryData->get(Summary::MaxInternalBlockCount),
1346 SummaryData->get(Summary::MaxFunctionCount),
1347 SummaryData->get(Summary::TotalNumBlocks),
1348 SummaryData->get(Summary::TotalNumFunctions));
1349 return Cur + SummarySize;
1357 Summary = Builder.getSummary();
1365 const unsigned char *Start =
1366 (
const unsigned char *)DataBuffer->getBufferStart();
1367 const unsigned char *Cur = Start;
1368 if ((
const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
1373 return HeaderOr.takeError();
1376 Cur += Header->
size();
1390 auto IndexPtr = std::make_unique<InstrProfReaderIndex<OnDiskHashTableImplV3>>(
1391 Start + Header->HashOffset, Cur, Start, HashType, Header->Version);
1395 if (Header->getIndexedProfileVersion() >= 8 &&
1397 if (
Error E = MemProfReader.deserialize(Start, Header->MemProfOffset))
1403 if (Header->getIndexedProfileVersion() >= 9) {
1404 const unsigned char *Ptr = Start + Header->BinaryIdOffset;
1406 uint64_t BinaryIdsSize =
1408 if (BinaryIdsSize %
sizeof(uint64_t))
1411 (
"BinaryIdSize (" +
Twine(BinaryIdsSize) +
") is not a multiple of 8")
1415 if (Ptr > (
const unsigned char *)DataBuffer->getBufferEnd())
1417 "corrupted binary ids");
1420 if (Header->getIndexedProfileVersion() >= 12) {
1421 const unsigned char *Ptr = Start + Header->VTableNamesOffset;
1423 uint64_t CompressedVTableNamesLen =
1428 const char *VTableNamePtr = (
const char *)Ptr;
1429 if (VTableNamePtr > DataBuffer->getBufferEnd())
1432 VTableName =
StringRef(VTableNamePtr, CompressedVTableNamesLen);
1435 if (Header->getIndexedProfileVersion() >= 10 &&
1437 const unsigned char *Ptr = Start + Header->TemporalProfTracesOffset;
1438 const auto *PtrEnd = (
const unsigned char *)DataBuffer->getBufferEnd();
1440 if (Ptr + 2 *
sizeof(uint64_t) > PtrEnd)
1442 const uint64_t NumTraces =
1446 for (
unsigned i = 0; i < NumTraces; i++) {
1448 if (Ptr + 2 *
sizeof(uint64_t) > PtrEnd)
1453 const uint64_t NumFunctions =
1456 if (Ptr + NumFunctions *
sizeof(uint64_t) > PtrEnd)
1458 for (
unsigned j = 0; j < NumFunctions; j++) {
1459 const uint64_t NameRef =
1461 Trace.FunctionNameRefs.push_back(NameRef);
1468 if (RemappingBuffer) {
1470 std::make_unique<InstrProfReaderItaniumRemapper<OnDiskHashTableImplV3>>(
1471 std::move(RemappingBuffer), *IndexPtr);
1472 if (
Error E = Remapper->populateRemappings())
1475 Remapper = std::make_unique<InstrProfReaderNullRemapper>(*IndexPtr);
1477 Index = std::move(IndexPtr);
1486 auto NewSymtab = std::make_unique<InstrProfSymtab>();
1488 if (
Error E = NewSymtab->initVTableNamesFromCompressedStrings(VTableName)) {
1494 if (
Error E = Index->populateSymtab(*NewSymtab)) {
1499 Symtab = std::move(NewSymtab);
1505 uint64_t *MismatchedFuncSum) {
1507 uint64_t FuncSum = 0;
1508 auto Err = Remapper->getRecords(FuncName,
Data);
1516 if (
auto Err = Remapper->getRecords(DeprecatedFuncName,
Data))
1521 return std::move(Err2);
1527 bool CSBitMatch =
false;
1529 uint64_t ValueSum = 0;
1530 for (uint64_t CountValue : Counts) {
1531 if (CountValue == (uint64_t)-1)
1534 if (std::numeric_limits<uint64_t>::max() - CountValue <= ValueSum)
1535 return std::numeric_limits<uint64_t>::max();
1536 ValueSum += CountValue;
1544 return std::move(
I);
1548 if (MismatchedFuncSum ==
nullptr)
1550 FuncSum = std::max(FuncSum, getFuncSum(
I.Counts));
1554 if (MismatchedFuncSum !=
nullptr)
1555 *MismatchedFuncSum = FuncSum;
1564 if (MemProfRecordTable ==
nullptr)
1566 "no memprof data available in profile");
1567 auto Iter = MemProfRecordTable->find(FuncNameHash);
1568 if (Iter == MemProfRecordTable->end())
1571 "memprof record not found for function hash " +
Twine(FuncNameHash));
1578 assert(FrameBase &&
"FrameBase must be available");
1579 assert(CallStackBase &&
"CallStackBase must be available");
1590 formatv(
"MemProf version {} not supported; "
1591 "requires version between {} and {}, inclusive",
1598 assert(MemProfRecordTable);
1613 MemProfRecordTable->data()) {
1615 IndexedRecord.AllocSites)
1620 for (
unsigned CS : Worklist.
set_bits())
1624 std::move(Extractor.CallerCalleePairs);
1627 for (
auto &[CallerGUID, CallList] : Pairs) {
1629 CallList.erase(
llvm::unique(CallList), CallList.end());
1638 MemProfRecordTable->getNumEntries());
1639 for (uint64_t
Key : MemProfRecordTable->keys()) {
1649 if (DataAccessProfileData !=
nullptr) {
1651 DataAccessProfileData->getRecords().size());
1653 DataAccessProfileData->getKnownColdSymbols().size());
1655 DataAccessProfileData->getKnownColdHashes().size());
1656 for (
const auto &[SymHandleRef, RecordRef] :
1657 DataAccessProfileData->getRecords())
1660 RecordRef.Locations));
1661 for (
StringRef ColdSymbol : DataAccessProfileData->getKnownColdSymbols())
1664 for (uint64_t Hash : DataAccessProfileData->getKnownColdHashes())
1670 return lhs.AccessCount > rhs.AccessCount;
1674 [](
const std::string &lhs,
const std::string &rhs) {
1679 [](
const uint64_t &lhs,
const uint64_t &rhs) { return lhs < rhs; });
1681 return AllMemProfData;
1686 std::vector<uint64_t> &Counts) {
1689 return error(std::move(E));
1691 Counts =
Record.get().Counts;
1700 return error(std::move(E));
1702 const auto &BitmapBytes =
Record.get().BitmapBytes;
1703 size_t I = 0, E = BitmapBytes.size();
1704 Bitmap.
resize(E * CHAR_BIT);
1707 using XTy =
decltype(
X);
1709 size_t N = std::min(E -
I,
sizeof(W));
1710 std::memset(W, 0,
sizeof(W));
1711 std::memcpy(W, &BitmapBytes[
I],
N);
1727 return error(std::move(E));
1730 if (RecordIndex >=
Data.size()) {
1731 Index->advanceToNextKey();
1738 std::vector<llvm::object::BuildID> &BinaryIds) {
1744 std::vector<llvm::object::BuildID> BinaryIds;
1752 uint64_t NumFuncs = 0;
1753 for (
const auto &Func : *
this) {
1756 if (FuncIsCS != IsCS)
1759 Func.accumulateCounts(Sum);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
Provides ErrorOr<T> smart pointer.
InstrProfLookupTrait::offset_type offset_type
static Error initializeReader(InstrProfReader &Reader)
#define READ_NUM(Str, Dst)
#define CHECK_LINE_END(Line)
static Error readBinaryIdsInternal(const MemoryBuffer &DataBuffer, ArrayRef< uint8_t > BinaryIdsBuffer, std::vector< llvm::object::BuildID > &BinaryIds, const llvm::endianness Endian)
Read a list of binary ids from a profile that consist of a.
#define VP_READ_ADVANCE(Val)
InstrProfLookupTrait::data_type data_type
static InstrProfKind getProfileKindFromVersion(uint64_t Version)
static void printBinaryIdsInternal(raw_ostream &OS, ArrayRef< llvm::object::BuildID > BinaryIds)
#define VARIANT_MASK_CSIR_PROF
#define VARIANT_MASK_MEMPROF
#define VARIANT_MASK_TEMPORAL_PROF
#define VARIANT_MASK_IR_PROF
#define VARIANT_MASK_BYTE_COVERAGE
#define VARIANT_MASK_INSTR_ENTRY
#define VARIANT_MASK_FUNCTION_ENTRY_ONLY
#define VARIANT_MASK_INSTR_LOOP_ENTRIES
static constexpr StringLiteral Filename
static StringRef getName(Value *V)
Defines the virtual file system interface vfs::FileSystem.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
BitVector & set()
Set all bits in the bitvector.
iterator_range< const_set_bits_iterator > set_bits() const
static BitVector & apply(F &&f, BitVector &Out, BitVector const &Arg, ArgTys const &...Args)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
Reader for the indexed binary instrprof format.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
static Expected< std::unique_ptr< IndexedInstrProfReader > > create(const Twine &Path, vfs::FileSystem &FS, const Twine &RemappingPath="")
Factory method to create an indexed reader.
Error readHeader() override
Read the file header.
Error printBinaryIds(raw_ostream &OS) override
Print binary ids.
Error getFunctionBitmap(StringRef FuncName, uint64_t FuncHash, BitVector &Bitmap)
Fill Bitmap with the profile data for the given function name.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
static bool hasFormat(const MemoryBuffer &DataBuffer)
Return true if the given buffer is in an indexed instrprof format.
Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash, std::vector< uint64_t > &Counts)
Fill Counts with the profile data for the given function name.
Expected< NamedInstrProfRecord > getInstrProfRecord(StringRef FuncName, uint64_t FuncHash, StringRef DeprecatedFuncName="", uint64_t *MismatchedFuncSum=nullptr)
Return the NamedInstrProfRecord associated with FuncName and FuncHash.
Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds) override
Read a list of binary ids.
LLVM_ABI memprof::AllMemProfData getAllMemProfData() const
LLVM_ABI Expected< memprof::MemProfRecord > getMemProfRecord(const uint64_t FuncNameHash) const
LLVM_ABI DenseMap< uint64_t, SmallVector< memprof::CallEdgeTy, 0 > > getMemProfCallerCalleePairs() const
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
const RawInstrProf::ProfileData< IntPtrT > * getDataPointer() const
Return a pointer to the underlying ProfileData vector that this class constructs.
size_t getDataSize() const
Return the number of ProfileData elements.
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
const char * getNamesPointer() const
Return a pointer to the names string that this class constructs.
LLVM_ABI std::optional< size_t > getDataSize() const
Return the number of ProfileData elements.
static LLVM_ABI llvm::Expected< std::unique_ptr< InstrProfCorrelator > > get(StringRef Filename, ProfCorrelatorKind FileKind, const object::BuildIDFetcher *BIDFetcher=nullptr, const ArrayRef< llvm::object::BuildID > BIs={})
size_t getNamesSize() const
Return the number of bytes in the names string.
static std::pair< instrprof_error, std::string > take(Error E)
Consume an Error and return the raw enum value contained within it, and the optional error message.
LLVM_ABI data_type ReadData(StringRef K, const unsigned char *D, offset_type N)
LLVM_ABI bool readValueProfilingData(const unsigned char *&D, const unsigned char *const End)
LLVM_ABI hash_value_type ComputeHash(StringRef K)
ArrayRef< NamedInstrProfRecord > data_type
InstrProfKind getProfileKind() const override
Error getRecords(ArrayRef< NamedInstrProfRecord > &Data) override
InstrProfReaderIndex(const unsigned char *Buckets, const unsigned char *const Payload, const unsigned char *const Base, IndexedInstrProf::HashT HashType, uint64_t Version)
bool atEnd() const override
A remapper that applies remappings based on a symbol remapping file.
static StringRef extractName(StringRef Name)
Extract the original function name from a PGO function name.
InstrProfReaderItaniumRemapper(std::unique_ptr< MemoryBuffer > RemapBuffer, InstrProfReaderIndex< HashTableImpl > &Underlying)
Error populateRemappings() override
static void reconstituteName(StringRef OrigName, StringRef ExtractedName, StringRef Replacement, SmallVectorImpl< char > &Out)
Given a mangled name extracted from a PGO function name, and a new form for that mangled name,...
Error getRecords(StringRef FuncName, ArrayRef< NamedInstrProfRecord > &Data) override
Name matcher supporting fuzzy matching of symbol names to names in profiles.
Base class and interface for reading profiling data of any known instrprof format.
std::unique_ptr< InstrProfSymtab > Symtab
Error success()
Clear the current error and return a successful one.
static LLVM_ABI Expected< std::unique_ptr< InstrProfReader > > create(const Twine &Path, vfs::FileSystem &FS, const InstrProfCorrelator *Correlator=nullptr, const object::BuildIDFetcher *BIDFetcher=nullptr, std::optional< ProfCorrelatorKind > BIDFetcherCorrelatorKind=std::nullopt, std::function< void(Error)> Warn=nullptr)
Factory method to create an appropriately typed reader for the given instrprof file.
SmallVector< TemporalProfTraceTy > TemporalProfTraces
A list of temporal profile traces.
uint64_t TemporalProfTraceStreamSize
The total number of temporal profile traces seen.
virtual bool isIRLevelProfile() const =0
virtual Error readHeader()=0
Read the header. Required before reading first record.
LLVM_ABI void accumulateCounts(CountSumOrPercent &Sum, bool IsCS)
Compute the sum of counts and return in Sum.
A symbol table used for function [IR]PGO name look-up with keys (such as pointers,...
static bool isExternalSymbol(const StringRef &Symbol)
True if Symbol is the value used to represent external symbols.
void mapAddress(uint64_t Addr, uint64_t MD5Val)
Map a function address to its name's MD5 hash.
LLVM_ABI Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
void mapVTableAddress(uint64_t StartAddr, uint64_t EndAddr, uint64_t MD5Val)
Map the address range (i.e., [start_address, end_address)) of a variable to its names' MD5 hash.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
size_t getBufferSize() const
const char * getBufferEnd() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
const char * getBufferStart() const
static LLVM_ABI const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Reader for the raw instrprof binary format from runtime.
Error readHeader() override
Read the header. Required before reading first record.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
Error printBinaryIds(raw_ostream &OS) override
Print binary ids.
static bool hasFormat(const MemoryBuffer &DataBuffer)
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the raw instr profile.
Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds) override
Read a list of binary ids.
SmallVector< TemporalProfTraceTy > & getTemporalProfTraces(std::optional< uint64_t > Weight={}) override
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
Check if the string is empty.
constexpr size_t size() const
Get the string size.
char front() const
Get the first character in the string.
Reader for symbol remapping files.
Reader for the simple text based instrprof format.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in text instrprof format.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
Error readHeader() override
Read the header.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
BuildIDFetcher searches local cache directories for debug info.
This class implements an extremely fast bulk output stream that can only output to a stream.
The virtual file system interface.
std::unique_ptr< Summary > allocSummary(uint32_t TotalSize)
uint64_t ComputeHash(StringRef K)
constexpr uint64_t MaximumSupportedVersion
constexpr uint64_t MinimumSupportedVersion
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
value_type byte_swap(value_type value, endianness endian)
Swap the bytes of value to match the given endianness.
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
value_type readNext(const CharT *&memory, endianness endian)
Read a value of a particular endianness from a buffer, and increment the buffer past that value.
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
RawInstrProfReader< uint64_t > RawInstrProfReader64
static Expected< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(const Twine &Filename, vfs::FileSystem &FS)
RelativeUniformCounterPtr ValuesPtrExpr NumBitmapBytes
RelativeUniformCounterPtr ValuesPtrExpr NumValueSites[IPVK_Last+1]
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr UniformCountersDelta
auto unique(Range &&R, Predicate P)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr UniformCountersStart
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
auto dyn_cast_or_null(const Y &Val)
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr CountersStart
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
@ raw_profile_version_mismatch
@ counter_value_too_large
@ unexpected_correlation_info
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr NamesStart
constexpr char GlobalIdentifierDelimiter
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
std::vector< ProfileSummaryEntry > SummaryEntryVector
@ End
Run the machine verifier once, at the end of the pipeline.
void consumeError(Error Err)
Consume a Error without doing anything.
InstrProfKind
An enum describing the attributes of an instrumented profile.
@ LoopEntriesInstrumentation
@ FunctionEntryInstrumentation
@ FrontendInstrumentation
RawInstrProfReader< uint32_t > RawInstrProfReader32
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
uint64_t Cutoff
The required percentile of total execution count.
uint64_t NumBlocks
Number of blocks >= the minumum execution count.
uint64_t MinBlockCount
The minimum execution count for this percentile.
static uint32_t getSize(uint32_t NumSumFields, uint32_t NumCutoffEntries)
uint64_t NumSummaryFields
uint64_t NumCutoffEntries
Profiling information for a single function.
static bool hasCSFlagInHash(uint64_t FuncHash)
An ordered list of functions identified by their NameRef found in INSTR_PROF_DATA.
YamlDataAccessProfData YamlifiedDataAccessProfiles
std::vector< GUIDMemProfRecordPair > HeapProfileRecords
The data access profiles for a symbol.
LLVM_ABI MemProfRecord toMemProfRecord(llvm::function_ref< std::vector< Frame >(const CallStackId)> Callback) const
std::vector< memprof::DataAccessProfRecord > Records
std::vector< uint64_t > KnownColdStrHashes
std::vector< std::string > KnownColdSymbols