LLVM 18.0.0git
InstrProfReader.h
Go to the documentation of this file.
1//===- InstrProfReader.h - Instrumented profiling readers -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for reading profiling data for instrumentation
10// based PGO and coverage.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
15#define LLVM_PROFILEDATA_INSTRPROFREADER_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
20#include "llvm/Object/BuildID.h"
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
31#include <algorithm>
32#include <cassert>
33#include <cstddef>
34#include <cstdint>
35#include <iterator>
36#include <memory>
37#include <utility>
38#include <vector>
39
40namespace llvm {
41
42class InstrProfReader;
43
44namespace vfs {
45class FileSystem;
46} // namespace vfs
47
48/// A file format agnostic iterator over profiling data.
49template <class record_type = NamedInstrProfRecord,
50 class reader_type = InstrProfReader>
52public:
53 using iterator_category = std::input_iterator_tag;
54 using value_type = record_type;
55 using difference_type = std::ptrdiff_t;
58
59private:
60 reader_type *Reader = nullptr;
62
63 void increment() {
64 if (Error E = Reader->readNextRecord(Record)) {
65 // Handle errors in the reader.
66 InstrProfError::take(std::move(E));
67 *this = InstrProfIterator();
68 }
69 }
70
71public:
72 InstrProfIterator() = default;
73 InstrProfIterator(reader_type *Reader) : Reader(Reader) { increment(); }
74
76 increment();
77 return *this;
78 }
79 bool operator==(const InstrProfIterator &RHS) const {
80 return Reader == RHS.Reader;
81 }
82 bool operator!=(const InstrProfIterator &RHS) const {
83 return Reader != RHS.Reader;
84 }
87};
88
89/// Base class and interface for reading profiling data of any known instrprof
90/// format. Provides an iterator over NamedInstrProfRecords.
93 std::string LastErrorMsg;
94
95public:
96 InstrProfReader() = default;
97 virtual ~InstrProfReader() = default;
98
99 /// Read the header. Required before reading first record.
100 virtual Error readHeader() = 0;
101
102 /// Read a single record.
104
105 /// Read a list of binary ids.
106 virtual Error readBinaryIds(std::vector<llvm::object::BuildID> &BinaryIds) {
107 return success();
108 }
109
110 /// Print binary ids.
111 virtual Error printBinaryIds(raw_ostream &OS) { return success(); };
112
113 /// Iterator over profile data.
116
117 /// Return the profile version.
118 virtual uint64_t getVersion() const = 0;
119
120 virtual bool isIRLevelProfile() const = 0;
121
122 virtual bool hasCSIRLevelProfile() const = 0;
123
124 virtual bool instrEntryBBEnabled() const = 0;
125
126 /// Return true if we must provide debug info to create PGO profiles.
127 virtual bool useDebugInfoCorrelate() const { return false; }
128
129 /// Return true if the profile has single byte counters representing coverage.
130 virtual bool hasSingleByteCoverage() const = 0;
131
132 /// Return true if the profile only instruments function entries.
133 virtual bool functionEntryOnly() const = 0;
134
135 /// Return true if profile includes a memory profile.
136 virtual bool hasMemoryProfile() const = 0;
137
138 /// Return true if this has a temporal profile.
139 virtual bool hasTemporalProfile() const = 0;
140
141 /// Returns a BitsetEnum describing the attributes of the profile. To check
142 /// individual attributes prefer using the helpers above.
143 virtual InstrProfKind getProfileKind() const = 0;
144
145 /// Return the PGO symtab. There are three different readers:
146 /// Raw, Text, and Indexed profile readers. The first two types
147 /// of readers are used only by llvm-profdata tool, while the indexed
148 /// profile reader is also used by llvm-cov tool and the compiler (
149 /// backend or frontend). Since creating PGO symtab can create
150 /// significant runtime and memory overhead (as it touches data
151 /// for the whole program), InstrProfSymtab for the indexed profile
152 /// reader should be created on demand and it is recommended to be
153 /// only used for dumping purpose with llvm-proftool, not with the
154 /// compiler.
156
157 /// Compute the sum of counts and return in Sum.
158 void accumulateCounts(CountSumOrPercent &Sum, bool IsCS);
159
160protected:
161 std::unique_ptr<InstrProfSymtab> Symtab;
162 /// A list of temporal profile traces.
164 /// The total number of temporal profile traces seen.
166
167 /// Set the current error and return same.
168 Error error(instrprof_error Err, const std::string &ErrMsg = "") {
169 LastError = Err;
170 LastErrorMsg = ErrMsg;
171 if (Err == instrprof_error::success)
172 return Error::success();
173 return make_error<InstrProfError>(Err, ErrMsg);
174 }
175
177 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
178 LastError = IPE.get();
179 LastErrorMsg = IPE.getMessage();
180 });
181 return make_error<InstrProfError>(LastError, LastErrorMsg);
182 }
183
184 /// Clear the current error and return a successful one.
186
187public:
188 /// Return true if the reader has finished reading the profile data.
189 bool isEOF() { return LastError == instrprof_error::eof; }
190
191 /// Return true if the reader encountered an error reading profiling data.
192 bool hasError() { return LastError != instrprof_error::success && !isEOF(); }
193
194 /// Get the current error.
196 if (hasError())
197 return make_error<InstrProfError>(LastError, LastErrorMsg);
198 return Error::success();
199 }
200
201 /// Factory method to create an appropriately typed reader for the given
202 /// instrprof file.
204 create(const Twine &Path, vfs::FileSystem &FS,
205 const InstrProfCorrelator *Correlator = nullptr);
206
208 create(std::unique_ptr<MemoryBuffer> Buffer,
209 const InstrProfCorrelator *Correlator = nullptr);
210
211 /// \param Weight for raw profiles use this as the temporal profile trace
212 /// weight
213 /// \returns a list of temporal profile traces.
215 getTemporalProfTraces(std::optional<uint64_t> Weight = {}) {
216 // For non-raw profiles we ignore the input weight and instead use the
217 // weights already in the traces.
218 return TemporalProfTraces;
219 }
220 /// \returns the total number of temporal profile traces seen.
223 }
224};
225
226/// Reader for the simple text based instrprof format.
227///
228/// This format is a simple text format that's suitable for test data. Records
229/// are separated by one or more blank lines, and record fields are separated by
230/// new lines.
231///
232/// Each record consists of a function name, a function hash, a number of
233/// counters, and then each counter value, in that order.
235private:
236 /// The profile data file contents.
237 std::unique_ptr<MemoryBuffer> DataBuffer;
238 /// Iterator over the profile data.
239 line_iterator Line;
240 /// The attributes of the current profile.
242
243 Error readValueProfileData(InstrProfRecord &Record);
244
245 Error readTemporalProfTraceData();
246
247public:
248 TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
249 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
252
253 /// Return true if the given buffer is in text instrprof format.
254 static bool hasFormat(const MemoryBuffer &Buffer);
255
256 // Text format does not have version, so return 0.
257 uint64_t getVersion() const override { return 0; }
258
259 bool isIRLevelProfile() const override {
260 return static_cast<bool>(ProfileKind & InstrProfKind::IRInstrumentation);
261 }
262
263 bool hasCSIRLevelProfile() const override {
264 return static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive);
265 }
266
267 bool instrEntryBBEnabled() const override {
268 return static_cast<bool>(ProfileKind &
270 }
271
272 bool hasSingleByteCoverage() const override {
273 return static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage);
274 }
275
276 bool functionEntryOnly() const override {
277 return static_cast<bool>(ProfileKind & InstrProfKind::FunctionEntryOnly);
278 }
279
280 bool hasMemoryProfile() const override {
281 // TODO: Add support for text format memory profiles.
282 return false;
283 }
284
285 bool hasTemporalProfile() const override {
286 return static_cast<bool>(ProfileKind & InstrProfKind::TemporalProfile);
287 }
288
289 InstrProfKind getProfileKind() const override { return ProfileKind; }
290
291 /// Read the header.
292 Error readHeader() override;
293
294 /// Read a single record.
296
298 assert(Symtab);
299 return *Symtab;
300 }
301};
302
303/// Reader for the raw instrprof binary format from runtime.
304///
305/// This format is a raw memory dump of the instrumentation-based profiling data
306/// from the runtime. It has no index.
307///
308/// Templated on the unsigned type whose size matches pointers on the platform
309/// that wrote the profile.
310template <class IntPtrT>
312private:
313 /// The profile data file contents.
314 std::unique_ptr<MemoryBuffer> DataBuffer;
315 /// If available, this hold the ProfileData array used to correlate raw
316 /// instrumentation data to their functions.
317 const InstrProfCorrelatorImpl<IntPtrT> *Correlator;
318 /// A list of timestamps paired with a function name reference.
319 std::vector<std::pair<uint64_t, uint64_t>> TemporalProfTimestamps;
320 bool ShouldSwapBytes;
321 // The value of the version field of the raw profile data header. The lower 56
322 // bits specifies the format version and the most significant 8 bits specify
323 // the variant types of the profile.
324 uint64_t Version;
325 uint64_t CountersDelta;
326 uint64_t BitmapDelta;
327 uint64_t NamesDelta;
330 const char *CountersStart;
331 const char *CountersEnd;
332 const char *BitmapStart;
333 const char *BitmapEnd;
334 const char *NamesStart;
335 const char *NamesEnd;
336 // After value profile is all read, this pointer points to
337 // the header of next profile data (if exists)
338 const uint8_t *ValueDataStart;
339 uint32_t ValueKindLast;
340 uint32_t CurValueDataSize;
341
342 /// Total size of binary ids.
343 uint64_t BinaryIdsSize{0};
344 /// Start address of binary id length and data pairs.
345 const uint8_t *BinaryIdsStart;
346
347public:
348 RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer,
349 const InstrProfCorrelator *Correlator)
350 : DataBuffer(std::move(DataBuffer)),
352 Correlator)) {}
355
356 static bool hasFormat(const MemoryBuffer &DataBuffer);
357 Error readHeader() override;
359 Error readBinaryIds(std::vector<llvm::object::BuildID> &BinaryIds) override;
361
362 uint64_t getVersion() const override { return Version; }
363
364 bool isIRLevelProfile() const override {
365 return (Version & VARIANT_MASK_IR_PROF) != 0;
366 }
367
368 bool hasCSIRLevelProfile() const override {
369 return (Version & VARIANT_MASK_CSIR_PROF) != 0;
370 }
371
372 bool instrEntryBBEnabled() const override {
373 return (Version & VARIANT_MASK_INSTR_ENTRY) != 0;
374 }
375
376 bool useDebugInfoCorrelate() const override {
377 return (Version & VARIANT_MASK_DBG_CORRELATE) != 0;
378 }
379
380 bool hasSingleByteCoverage() const override {
381 return (Version & VARIANT_MASK_BYTE_COVERAGE) != 0;
382 }
383
384 bool functionEntryOnly() const override {
385 return (Version & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
386 }
387
388 bool hasMemoryProfile() const override {
389 // Memory profiles have a separate raw format, so this should never be set.
390 assert(!(Version & VARIANT_MASK_MEMPROF));
391 return false;
392 }
393
394 bool hasTemporalProfile() const override {
395 return (Version & VARIANT_MASK_TEMPORAL_PROF) != 0;
396 }
397
398 /// Returns a BitsetEnum describing the attributes of the raw instr profile.
399 InstrProfKind getProfileKind() const override;
400
402 assert(Symtab.get());
403 return *Symtab.get();
404 }
405
407 getTemporalProfTraces(std::optional<uint64_t> Weight = {}) override;
408
409private:
410 Error createSymtab(InstrProfSymtab &Symtab);
411 Error readNextHeader(const char *CurrentPos);
412 Error readHeader(const RawInstrProf::Header &Header);
413
414 template <class IntT> IntT swap(IntT Int) const {
415 return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
416 }
417
418 support::endianness getDataEndianness() const {
420 if (!ShouldSwapBytes)
421 return HostEndian;
422 if (HostEndian == support::little)
423 return support::big;
424 else
425 return support::little;
426 }
427
428 inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) {
429 return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
430 }
431
432 Error readName(NamedInstrProfRecord &Record);
433 Error readFuncHash(NamedInstrProfRecord &Record);
434 Error readRawCounts(InstrProfRecord &Record);
435 Error readRawBitmapBytes(InstrProfRecord &Record);
436 Error readValueProfilingData(InstrProfRecord &Record);
437 bool atEnd() const { return Data == DataEnd; }
438
439 void advanceData() {
440 // `CountersDelta` is a constant zero when using debug info correlation.
441 if (!Correlator) {
442 // The initial CountersDelta is the in-memory address difference between
443 // the data and counts sections:
444 // start(__llvm_prf_cnts) - start(__llvm_prf_data)
445 // As we advance to the next record, we maintain the correct CountersDelta
446 // with respect to the next record.
447 CountersDelta -= sizeof(*Data);
448 BitmapDelta -= sizeof(*Data);
449 }
450 Data++;
451 ValueDataStart += CurValueDataSize;
452 }
453
454 const char *getNextHeaderPos() const {
455 assert(atEnd());
456 return (const char *)ValueDataStart;
457 }
458
459 StringRef getName(uint64_t NameRef) const {
460 return Symtab->getFuncName(swap(NameRef));
461 }
462
463 int getCounterTypeSize() const {
464 return hasSingleByteCoverage() ? sizeof(uint8_t) : sizeof(uint64_t);
465 }
466};
467
470
471namespace IndexedInstrProf {
472
473enum class HashT : uint32_t;
474
475} // end namespace IndexedInstrProf
476
477/// Trait for lookups into the on-disk hash table for the binary instrprof
478/// format.
480 std::vector<NamedInstrProfRecord> DataBuffer;
482 unsigned FormatVersion;
483 // Endianness of the input value profile data.
484 // It should be LE by default, but can be changed
485 // for testing purpose.
486 support::endianness ValueProfDataEndianness = support::little;
487
488public:
489 InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
490 : HashType(HashType), FormatVersion(FormatVersion) {}
491
493
498
499 static bool EqualKey(StringRef A, StringRef B) { return A == B; }
500 static StringRef GetInternalKey(StringRef K) { return K; }
501 static StringRef GetExternalKey(StringRef K) { return K; }
502
504
505 static std::pair<offset_type, offset_type>
506 ReadKeyDataLength(const unsigned char *&D) {
507 using namespace support;
508
509 offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D);
510 offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D);
511 return std::make_pair(KeyLen, DataLen);
512 }
513
514 StringRef ReadKey(const unsigned char *D, offset_type N) {
515 return StringRef((const char *)D, N);
516 }
517
518 bool readValueProfilingData(const unsigned char *&D,
519 const unsigned char *const End);
520 data_type ReadData(StringRef K, const unsigned char *D, offset_type N);
521
522 // Used for testing purpose only.
524 ValueProfDataEndianness = Endianness;
525 }
526};
527
529 virtual ~InstrProfReaderIndexBase() = default;
530
531 // Read all the profile records with the same key pointed to the current
532 // iterator.
534
535 // Read all the profile records with the key equal to FuncName
536 virtual Error getRecords(StringRef FuncName,
538 virtual void advanceToNextKey() = 0;
539 virtual bool atEnd() const = 0;
541 virtual uint64_t getVersion() const = 0;
542 virtual bool isIRLevelProfile() const = 0;
543 virtual bool hasCSIRLevelProfile() const = 0;
544 virtual bool instrEntryBBEnabled() const = 0;
545 virtual bool hasSingleByteCoverage() const = 0;
546 virtual bool functionEntryOnly() const = 0;
547 virtual bool hasMemoryProfile() const = 0;
548 virtual bool hasTemporalProfile() const = 0;
549 virtual InstrProfKind getProfileKind() const = 0;
551};
552
555
560
561template <typename HashTableImpl>
563
564template <typename HashTableImpl>
566private:
567 std::unique_ptr<HashTableImpl> HashTable;
568 typename HashTableImpl::data_iterator RecordIterator;
569 uint64_t FormatVersion;
570
571 friend class InstrProfReaderItaniumRemapper<HashTableImpl>;
572
573public:
574 InstrProfReaderIndex(const unsigned char *Buckets,
575 const unsigned char *const Payload,
576 const unsigned char *const Base,
577 IndexedInstrProf::HashT HashType, uint64_t Version);
578 ~InstrProfReaderIndex() override = default;
579
581 Error getRecords(StringRef FuncName,
583 void advanceToNextKey() override { RecordIterator++; }
584
585 bool atEnd() const override {
586 return RecordIterator == HashTable->data_end();
587 }
588
590 HashTable->getInfoObj().setValueProfDataEndianness(Endianness);
591 }
592
593 uint64_t getVersion() const override { return GET_VERSION(FormatVersion); }
594
595 bool isIRLevelProfile() const override {
596 return (FormatVersion & VARIANT_MASK_IR_PROF) != 0;
597 }
598
599 bool hasCSIRLevelProfile() const override {
600 return (FormatVersion & VARIANT_MASK_CSIR_PROF) != 0;
601 }
602
603 bool instrEntryBBEnabled() const override {
604 return (FormatVersion & VARIANT_MASK_INSTR_ENTRY) != 0;
605 }
606
607 bool hasSingleByteCoverage() const override {
608 return (FormatVersion & VARIANT_MASK_BYTE_COVERAGE) != 0;
609 }
610
611 bool functionEntryOnly() const override {
612 return (FormatVersion & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
613 }
614
615 bool hasMemoryProfile() const override {
616 return (FormatVersion & VARIANT_MASK_MEMPROF) != 0;
617 }
618
619 bool hasTemporalProfile() const override {
620 return (FormatVersion & VARIANT_MASK_TEMPORAL_PROF) != 0;
621 }
622
623 InstrProfKind getProfileKind() const override;
624
626 return Symtab.create(HashTable->keys());
627 }
628};
629
630/// Name matcher supporting fuzzy matching of symbol names to names in profiles.
632public:
633 virtual ~InstrProfReaderRemapper() = default;
635 virtual Error getRecords(StringRef FuncName,
637};
638
639/// Reader for the indexed binary instrprof format.
641private:
642 /// The profile data file contents.
643 std::unique_ptr<MemoryBuffer> DataBuffer;
644 /// The profile remapping file contents.
645 std::unique_ptr<MemoryBuffer> RemappingBuffer;
646 /// The index into the profile data.
647 std::unique_ptr<InstrProfReaderIndexBase> Index;
648 /// The profile remapping file contents.
649 std::unique_ptr<InstrProfReaderRemapper> Remapper;
650 /// Profile summary data.
651 std::unique_ptr<ProfileSummary> Summary;
652 /// Context sensitive profile summary data.
653 std::unique_ptr<ProfileSummary> CS_Summary;
654 /// MemProf profile schema (if available).
656 /// MemProf record profile data on-disk indexed via llvm::md5(FunctionName).
657 std::unique_ptr<MemProfRecordHashTable> MemProfRecordTable;
658 /// MemProf frame profile data on-disk indexed via frame id.
659 std::unique_ptr<MemProfFrameHashTable> MemProfFrameTable;
660 /// Total size of binary ids.
661 uint64_t BinaryIdsSize{0};
662 /// Start address of binary id length and data pairs.
663 const uint8_t *BinaryIdsStart = nullptr;
664
665 // Index to the current record in the record array.
666 unsigned RecordIndex;
667
668 // Read the profile summary. Return a pointer pointing to one byte past the
669 // end of the summary data if it exists or the input \c Cur.
670 // \c UseCS indicates whether to use the context-sensitive profile summary.
671 const unsigned char *readSummary(IndexedInstrProf::ProfVersion Version,
672 const unsigned char *Cur, bool UseCS);
673
674public:
676 std::unique_ptr<MemoryBuffer> DataBuffer,
677 std::unique_ptr<MemoryBuffer> RemappingBuffer = nullptr)
678 : DataBuffer(std::move(DataBuffer)),
679 RemappingBuffer(std::move(RemappingBuffer)), RecordIndex(0) {}
682
683 /// Return the profile version.
684 uint64_t getVersion() const override { return Index->getVersion(); }
685 bool isIRLevelProfile() const override { return Index->isIRLevelProfile(); }
686 bool hasCSIRLevelProfile() const override {
687 return Index->hasCSIRLevelProfile();
688 }
689
690 bool instrEntryBBEnabled() const override {
691 return Index->instrEntryBBEnabled();
692 }
693
694 bool hasSingleByteCoverage() const override {
695 return Index->hasSingleByteCoverage();
696 }
697
698 bool functionEntryOnly() const override { return Index->functionEntryOnly(); }
699
700 bool hasMemoryProfile() const override { return Index->hasMemoryProfile(); }
701
702 bool hasTemporalProfile() const override {
703 return Index->hasTemporalProfile();
704 }
705
706 /// Returns a BitsetEnum describing the attributes of the indexed instr
707 /// profile.
708 InstrProfKind getProfileKind() const override {
709 return Index->getProfileKind();
710 }
711
712 /// Return true if the given buffer is in an indexed instrprof format.
713 static bool hasFormat(const MemoryBuffer &DataBuffer);
714
715 /// Read the file header.
716 Error readHeader() override;
717 /// Read a single record.
719
720 /// Return the NamedInstrProfRecord associated with FuncName and FuncHash.
721 /// When return a hash_mismatch error and MismatchedFuncSum is not nullptr,
722 /// the sum of all counters in the mismatched function will be set to
723 /// MismatchedFuncSum. If there are multiple instances of mismatched
724 /// functions, MismatchedFuncSum returns the maximum. If \c FuncName is not
725 /// found, try to lookup \c DeprecatedFuncName to handle profiles built by
726 /// older compilers.
728 getInstrProfRecord(StringRef FuncName, uint64_t FuncHash,
729 StringRef DeprecatedFuncName = "",
730 uint64_t *MismatchedFuncSum = nullptr);
731
732 /// Return the memprof record for the function identified by
733 /// llvm::md5(Name).
735
736 /// Fill Counts with the profile data for the given function name.
737 Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
738 std::vector<uint64_t> &Counts);
739
740 /// Fill Bitmap Bytes with the profile data for the given function name.
742 std::vector<uint8_t> &BitmapBytes);
743
744 /// Return the maximum of all known function counts.
745 /// \c UseCS indicates whether to use the context-sensitive count.
747 if (UseCS) {
748 assert(CS_Summary && "No context sensitive profile summary");
749 return CS_Summary->getMaxFunctionCount();
750 } else {
751 assert(Summary && "No profile summary");
752 return Summary->getMaxFunctionCount();
753 }
754 }
755
756 /// Factory method to create an indexed reader.
758 create(const Twine &Path, vfs::FileSystem &FS,
759 const Twine &RemappingPath = "");
760
762 create(std::unique_ptr<MemoryBuffer> Buffer,
763 std::unique_ptr<MemoryBuffer> RemappingBuffer = nullptr);
764
765 // Used for testing purpose only.
767 Index->setValueProfDataEndianness(Endianness);
768 }
769
770 // See description in the base class. This interface is designed
771 // to be used by llvm-profdata (for dumping). Avoid using this when
772 // the client is the compiler.
773 InstrProfSymtab &getSymtab() override;
774
775 /// Return the profile summary.
776 /// \c UseCS indicates whether to use the context-sensitive summary.
778 if (UseCS) {
779 assert(CS_Summary && "No context sensitive summary");
780 return *CS_Summary;
781 } else {
782 assert(Summary && "No profile summary");
783 return *Summary;
784 }
785 }
786
787 Error readBinaryIds(std::vector<llvm::object::BuildID> &BinaryIds) override;
789};
790
791} // end namespace llvm
792
793#endif // LLVM_PROFILEDATA_INSTRPROFREADER_H
aarch64 promote const
basic Basic Alias true
This file declares a library for handling Build IDs and using them to find debug info.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool End
Definition: ELF_riscv.cpp:469
InstrProfLookupTrait::offset_type offset_type
InstrProfLookupTrait::data_type data_type
Defines facilities for reading and writing on-disk hash tables.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
#define error(X)
Value * RHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:154
static ErrorSuccess success()
Create a success value.
Definition: Error.h:328
Tagged union holding either a T or a Error.
Definition: Error.h:468
Reader for the indexed binary instrprof format.
uint64_t getVersion() const override
Return the profile version.
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.
IndexedInstrProfReader(const IndexedInstrProfReader &)=delete
Expected< memprof::MemProfRecord > getMemProfRecord(uint64_t FuncNameHash)
Return the memprof record for the function identified by llvm::md5(Name).
Error readHeader() override
Read the file header.
Error printBinaryIds(raw_ostream &OS) override
Print binary ids.
bool hasTemporalProfile() const override
Return true if this has a temporal profile.
void setValueProfDataEndianness(support::endianness Endianness)
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
ProfileSummary & getSummary(bool UseCS)
Return the profile summary.
static bool hasFormat(const MemoryBuffer &DataBuffer)
Return true if the given buffer is in an indexed instrprof format.
Expected< InstrProfRecord > getInstrProfRecord(StringRef FuncName, uint64_t FuncHash, StringRef DeprecatedFuncName="", uint64_t *MismatchedFuncSum=nullptr)
Return the NamedInstrProfRecord associated with FuncName and FuncHash.
bool hasMemoryProfile() const override
Return true if profile includes a memory profile.
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
uint64_t getMaximumFunctionCount(bool UseCS)
Return the maximum of all known function counts.
Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash, std::vector< uint64_t > &Counts)
Fill Counts with the profile data for the given function name.
Error getFunctionBitmapBytes(StringRef FuncName, uint64_t FuncHash, std::vector< uint8_t > &BitmapBytes)
Fill Bitmap Bytes with the profile data for the given function name.
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the indexed instr profile.
IndexedInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer, std::unique_ptr< MemoryBuffer > RemappingBuffer=nullptr)
Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds) override
Read a list of binary ids.
bool hasCSIRLevelProfile() const override
IndexedInstrProfReader & operator=(const IndexedInstrProfReader &)=delete
bool instrEntryBBEnabled() const override
bool isIRLevelProfile() const override
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
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.
Definition: InstrProf.h:392
const std::string & getMessage() const
Definition: InstrProf.h:387
instrprof_error get() const
Definition: InstrProf.h:386
A file format agnostic iterator over profiling data.
bool operator==(const InstrProfIterator &RHS) const
value_type & operator*()
InstrProfIterator(reader_type *Reader)
InstrProfIterator & operator++()
bool operator!=(const InstrProfIterator &RHS) const
value_type * operator->()
std::ptrdiff_t difference_type
std::input_iterator_tag iterator_category
Trait for lookups into the on-disk hash table for the binary instrprof format.
InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
void setValueProfDataEndianness(support::endianness Endianness)
StringRef ReadKey(const unsigned char *D, offset_type N)
static std::pair< offset_type, offset_type > ReadKeyDataLength(const unsigned char *&D)
static StringRef GetExternalKey(StringRef K)
data_type ReadData(StringRef K, const unsigned char *D, offset_type N)
bool readValueProfilingData(const unsigned char *&D, const unsigned char *const End)
hash_value_type ComputeHash(StringRef K)
static bool EqualKey(StringRef A, StringRef B)
static StringRef GetInternalKey(StringRef K)
Error populateSymtab(InstrProfSymtab &Symtab) override
bool hasSingleByteCoverage() const override
void advanceToNextKey() override
bool hasCSIRLevelProfile() const override
InstrProfKind getProfileKind() const override
Error getRecords(ArrayRef< NamedInstrProfRecord > &Data) override
bool functionEntryOnly() const override
~InstrProfReaderIndex() override=default
uint64_t getVersion() const override
bool isIRLevelProfile() const override
bool hasMemoryProfile() const override
bool hasTemporalProfile() const override
bool instrEntryBBEnabled() const override
void setValueProfDataEndianness(support::endianness Endianness) override
bool atEnd() const override
A remapper that applies remappings based on a symbol remapping file.
Name matcher supporting fuzzy matching of symbol names to names in profiles.
virtual Error getRecords(StringRef FuncName, ArrayRef< NamedInstrProfRecord > &Data)=0
virtual ~InstrProfReaderRemapper()=default
Base class and interface for reading profiling data of any known instrprof format.
InstrProfIterator begin()
Iterator over profile data.
virtual bool instrEntryBBEnabled() const =0
virtual Error readNextRecord(NamedInstrProfRecord &Record)=0
Read a single record.
static Expected< std::unique_ptr< InstrProfReader > > create(const Twine &Path, vfs::FileSystem &FS, const InstrProfCorrelator *Correlator=nullptr)
Factory method to create an appropriately typed reader for the given instrprof file.
Error error(Error &&E)
InstrProfIterator end()
virtual Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds)
Read a list of binary ids.
virtual bool functionEntryOnly() const =0
Return true if the profile only instruments function entries.
std::unique_ptr< InstrProfSymtab > Symtab
Error getError()
Get the current error.
virtual InstrProfSymtab & getSymtab()=0
Return the PGO symtab.
virtual bool hasSingleByteCoverage() const =0
Return true if the profile has single byte counters representing coverage.
virtual bool hasTemporalProfile() const =0
Return true if this has a temporal profile.
Error success()
Clear the current error and return a successful one.
bool hasError()
Return true if the reader encountered an error reading profiling data.
virtual InstrProfKind getProfileKind() const =0
Returns a BitsetEnum describing the attributes of the profile.
SmallVector< TemporalProfTraceTy > TemporalProfTraces
A list of temporal profile traces.
uint64_t TemporalProfTraceStreamSize
The total number of temporal profile traces seen.
virtual Error printBinaryIds(raw_ostream &OS)
Print binary ids.
virtual bool useDebugInfoCorrelate() const
Return true if we must provide debug info to create PGO profiles.
uint64_t getTemporalProfTraceStreamSize()
virtual uint64_t getVersion() const =0
Return the profile version.
virtual bool hasMemoryProfile() const =0
Return true if profile includes a memory profile.
virtual SmallVector< TemporalProfTraceTy > & getTemporalProfTraces(std::optional< uint64_t > Weight={})
virtual bool hasCSIRLevelProfile() const =0
virtual bool isIRLevelProfile() const =0
virtual ~InstrProfReader()=default
virtual Error readHeader()=0
Read the header. Required before reading first record.
Error error(instrprof_error Err, const std::string &ErrMsg="")
Set the current error and return same.
void accumulateCounts(CountSumOrPercent &Sum, bool IsCS)
Compute the sum of counts and return in Sum.
bool isEOF()
Return true if the reader has finished reading the profile data.
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:427
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
Provides lookup and iteration over an on disk hash table.
Reader for the raw instrprof binary format from runtime.
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
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)
RawInstrProfReader & operator=(const RawInstrProfReader &)=delete
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
bool useDebugInfoCorrelate() const override
Return true if we must provide debug info to create PGO profiles.
bool isIRLevelProfile() const override
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the raw instr profile.
bool hasMemoryProfile() const override
Return true if profile includes a memory profile.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds) override
Read a list of binary ids.
bool hasTemporalProfile() const override
Return true if this has a temporal profile.
bool instrEntryBBEnabled() const override
RawInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer, const InstrProfCorrelator *Correlator)
uint64_t getVersion() const override
Return the profile version.
SmallVector< TemporalProfTraceTy > & getTemporalProfTraces(std::optional< uint64_t > Weight={}) override
RawInstrProfReader(const RawInstrProfReader &)=delete
bool hasCSIRLevelProfile() const override
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Reader for the simple text based instrprof format.
bool isIRLevelProfile() const override
uint64_t getVersion() const override
Return the profile version.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in text instrprof format.
TextInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer_)
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
TextInstrProfReader(const TextInstrProfReader &)=delete
bool hasMemoryProfile() const override
Return true if profile includes a memory profile.
bool hasCSIRLevelProfile() const override
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
bool instrEntryBBEnabled() const override
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the profile.
bool hasTemporalProfile() const override
Return true if this has a temporal profile.
Error readHeader() override
Read the header.
TextInstrProfReader & operator=(const TextInstrProfReader &)=delete
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
The virtual file system interface.
unsigned char getSwappedBytes(unsigned char C)
Definition: SwapByteOrder.h:59
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:964
auto dyn_cast_or_null(const Y &Val)
Definition: Casting.h:759
support::endianness getHostEndianness()
Definition: InstrProf.h:966
instrprof_error
Definition: InstrProf.h:323
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1854
InstrProfKind
An enum describing the attributes of an instrumented profile.
Definition: InstrProf.h:300
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
virtual Error populateSymtab(InstrProfSymtab &)=0
virtual Error getRecords(ArrayRef< NamedInstrProfRecord > &Data)=0
virtual ~InstrProfReaderIndexBase()=default
virtual InstrProfKind getProfileKind() const =0
virtual bool hasTemporalProfile() const =0
virtual bool isIRLevelProfile() const =0
virtual void advanceToNextKey()=0
virtual bool hasMemoryProfile() const =0
virtual bool hasCSIRLevelProfile() const =0
virtual uint64_t getVersion() const =0
virtual bool atEnd() const =0
virtual bool instrEntryBBEnabled() const =0
virtual Error getRecords(StringRef FuncName, ArrayRef< NamedInstrProfRecord > &Data)=0
virtual void setValueProfDataEndianness(support::endianness Endianness)=0
virtual bool functionEntryOnly() const =0
virtual bool hasSingleByteCoverage() const =0
Profiling information for a single function.
Definition: InstrProf.h:695