Skip to content

Commit 186dcd4

Browse files
author
Snehasish Kumar
committed
[instrprof][NFC] Refactor out the common logic for getProfileKind.
The logic for getProfileKind for RawInstrProfReader and InstrProfReaderIndex is similar. To avoid duplication, move the logic from the header to InstrProfReader.cpp and introduce a static method which implements the common code. Differential Revision: https://reviews.llvm.org/D118656
1 parent 8306968 commit 186dcd4

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

llvm/include/llvm/ProfileData/InstrProfReader.h

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,7 @@ class RawInstrProfReader : public InstrProfReader {
310310
}
311311

312312
/// Returns a BitsetEnum describing the attributes of the raw instr profile.
313-
InstrProfKind getProfileKind() const override {
314-
InstrProfKind ProfileKind = InstrProfKind::Unknown;
315-
if (Version & VARIANT_MASK_IR_PROF) {
316-
ProfileKind |= InstrProfKind::IR;
317-
}
318-
if (Version & VARIANT_MASK_CSIR_PROF) {
319-
ProfileKind |= InstrProfKind::CS;
320-
}
321-
if (Version & VARIANT_MASK_INSTR_ENTRY) {
322-
ProfileKind |= InstrProfKind::BB;
323-
}
324-
if (Version & VARIANT_MASK_BYTE_COVERAGE) {
325-
ProfileKind |= InstrProfKind::SingleByteCoverage;
326-
}
327-
if (Version & VARIANT_MASK_FUNCTION_ENTRY_ONLY) {
328-
ProfileKind |= InstrProfKind::FunctionEntryOnly;
329-
}
330-
return ProfileKind;
331-
}
313+
InstrProfKind getProfileKind() const override;
332314

333315
InstrProfSymtab &getSymtab() override {
334316
assert(Symtab.get());
@@ -532,25 +514,7 @@ class InstrProfReaderIndex : public InstrProfReaderIndexBase {
532514
return (FormatVersion & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
533515
}
534516

535-
InstrProfKind getProfileKind() const override {
536-
InstrProfKind ProfileKind = InstrProfKind::Unknown;
537-
if (FormatVersion & VARIANT_MASK_IR_PROF) {
538-
ProfileKind |= InstrProfKind::IR;
539-
}
540-
if (FormatVersion & VARIANT_MASK_CSIR_PROF) {
541-
ProfileKind |= InstrProfKind::CS;
542-
}
543-
if (FormatVersion & VARIANT_MASK_INSTR_ENTRY) {
544-
ProfileKind |= InstrProfKind::BB;
545-
}
546-
if (FormatVersion & VARIANT_MASK_BYTE_COVERAGE) {
547-
ProfileKind |= InstrProfKind::SingleByteCoverage;
548-
}
549-
if (FormatVersion & VARIANT_MASK_FUNCTION_ENTRY_ONLY) {
550-
ProfileKind |= InstrProfKind::FunctionEntryOnly;
551-
}
552-
return ProfileKind;
553-
}
517+
InstrProfKind getProfileKind() const override;
554518

555519
Error populateSymtab(InstrProfSymtab &Symtab) override {
556520
return Symtab.create(HashTable->keys());

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@
3838

3939
using namespace llvm;
4040

41+
// Extracts the variant information from the top 8 bits in the version and
42+
// returns an enum specifying the variants present.
43+
static InstrProfKind getProfileKindFromVersion(uint64_t Version) {
44+
InstrProfKind ProfileKind = InstrProfKind::Unknown;
45+
if (Version & VARIANT_MASK_IR_PROF) {
46+
ProfileKind |= InstrProfKind::IR;
47+
}
48+
if (Version & VARIANT_MASK_CSIR_PROF) {
49+
ProfileKind |= InstrProfKind::CS;
50+
}
51+
if (Version & VARIANT_MASK_INSTR_ENTRY) {
52+
ProfileKind |= InstrProfKind::BB;
53+
}
54+
if (Version & VARIANT_MASK_BYTE_COVERAGE) {
55+
ProfileKind |= InstrProfKind::SingleByteCoverage;
56+
}
57+
if (Version & VARIANT_MASK_FUNCTION_ENTRY_ONLY) {
58+
ProfileKind |= InstrProfKind::FunctionEntryOnly;
59+
}
60+
return ProfileKind;
61+
}
62+
4163
static Expected<std::unique_ptr<MemoryBuffer>>
4264
setupMemoryBuffer(const Twine &Path) {
4365
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
@@ -297,6 +319,11 @@ Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
297319
return success();
298320
}
299321

322+
template <class IntPtrT>
323+
InstrProfKind RawInstrProfReader<IntPtrT>::getProfileKind() const {
324+
return getProfileKindFromVersion(Version);
325+
}
326+
300327
template <class IntPtrT>
301328
bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
302329
if (DataBuffer.getBufferSize() < sizeof(uint64_t))
@@ -718,6 +745,11 @@ InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
718745
RecordIterator = HashTable->data_begin();
719746
}
720747

748+
template <typename HashTableImpl>
749+
InstrProfKind InstrProfReaderIndex<HashTableImpl>::getProfileKind() const {
750+
return getProfileKindFromVersion(FormatVersion);
751+
}
752+
721753
namespace {
722754
/// A remapper that does not apply any remappings.
723755
class InstrProfReaderNullRemapper : public InstrProfReaderRemapper {

0 commit comments

Comments
 (0)