Skip to content

Commit bb642f8

Browse files
[NFC][InstrProf]Refactor readPGOFuncNameStrings (#71566)
Refactor this function to take a callback for each decoded string, rename it and change it to a static function in cpp. Move its (sole) caller definition from header to cpp. - This is a split of patch #66825; to minimize the diff created in a big PR.
1 parent 90a9e9f commit bb642f8

File tree

2 files changed

+58
-56
lines changed

2 files changed

+58
-56
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,6 @@ Error collectGlobalObjectNameStrings(ArrayRef<std::string> NameStrs,
242242
Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
243243
std::string &Result, bool doCompression = true);
244244

245-
/// \c NameStrings is a string composed of one of more sub-strings encoded in
246-
/// the format described above. The substrings are separated by 0 or more zero
247-
/// bytes. This method decodes the string and populates the \c Symtab.
248-
Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab);
249-
250245
/// Check if INSTR_PROF_RAW_VERSION_VAR is defined. This global is only being
251246
/// set in IR PGO compilation.
252247
bool isIRPGOFlagSet(const Module *M);
@@ -469,14 +464,14 @@ class InstrProfSymtab {
469464
/// until before it is used. See also \c create(StringRef) method.
470465
Error create(object::SectionRef &Section);
471466

472-
/// This interface is used by reader of CoverageMapping test
473-
/// format.
474-
inline Error create(StringRef D, uint64_t BaseAddr);
475-
476467
/// \c NameStrings is a string composed of one of more sub-strings
477468
/// encoded in the format described in \c collectPGOFuncNameStrings.
478469
/// This method is a wrapper to \c readPGOFuncNameStrings method.
479-
inline Error create(StringRef NameStrings);
470+
Error create(StringRef NameStrings);
471+
472+
/// This interface is used by reader of CoverageMapping test
473+
/// format.
474+
inline Error create(StringRef D, uint64_t BaseAddr);
480475

481476
/// A wrapper interface to populate the PGO symtab with functions
482477
/// decls from module \c M. This interface is used by transformation
@@ -547,10 +542,6 @@ Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
547542
return Error::success();
548543
}
549544

550-
Error InstrProfSymtab::create(StringRef NameStrings) {
551-
return readPGOFuncNameStrings(NameStrings, *this);
552-
}
553-
554545
template <typename NameIterRange>
555546
Error InstrProfSymtab::create(const NameIterRange &IterRange) {
556547
for (auto Name : IterRange)

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,59 @@ Error InstrProfSymtab::create(Module &M, bool InLTO) {
458458
return Error::success();
459459
}
460460

461+
/// \c NameStrings is a string composed of one of more possibly encoded
462+
/// sub-strings. The substrings are separated by 0 or more zero bytes. This
463+
/// method decodes the string and calls `NameCallback` for each substring.
464+
static Error
465+
readAndDecodeStrings(StringRef NameStrings,
466+
std::function<Error(StringRef)> NameCallback) {
467+
const uint8_t *P = NameStrings.bytes_begin();
468+
const uint8_t *EndP = NameStrings.bytes_end();
469+
while (P < EndP) {
470+
uint32_t N;
471+
uint64_t UncompressedSize = decodeULEB128(P, &N);
472+
P += N;
473+
uint64_t CompressedSize = decodeULEB128(P, &N);
474+
P += N;
475+
bool isCompressed = (CompressedSize != 0);
476+
SmallVector<uint8_t, 128> UncompressedNameStrings;
477+
StringRef NameStrings;
478+
if (isCompressed) {
479+
if (!llvm::compression::zlib::isAvailable())
480+
return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
481+
482+
if (Error E = compression::zlib::decompress(ArrayRef(P, CompressedSize),
483+
UncompressedNameStrings,
484+
UncompressedSize)) {
485+
consumeError(std::move(E));
486+
return make_error<InstrProfError>(instrprof_error::uncompress_failed);
487+
}
488+
P += CompressedSize;
489+
NameStrings = toStringRef(UncompressedNameStrings);
490+
} else {
491+
NameStrings =
492+
StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
493+
P += UncompressedSize;
494+
}
495+
// Now parse the name strings.
496+
SmallVector<StringRef, 0> Names;
497+
NameStrings.split(Names, getInstrProfNameSeparator());
498+
for (StringRef &Name : Names)
499+
if (Error E = NameCallback(Name))
500+
return E;
501+
502+
while (P < EndP && *P == 0)
503+
P++;
504+
}
505+
return Error::success();
506+
}
507+
508+
Error InstrProfSymtab::create(StringRef NameStrings) {
509+
return readAndDecodeStrings(
510+
NameStrings,
511+
std::bind(&InstrProfSymtab::addFuncName, this, std::placeholders::_1));
512+
}
513+
461514
Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
462515
if (Error E = addFuncName(PGOFuncName))
463516
return E;
@@ -566,48 +619,6 @@ Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
566619
NameStrs, compression::zlib::isAvailable() && doCompression, Result);
567620
}
568621

569-
Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
570-
const uint8_t *P = NameStrings.bytes_begin();
571-
const uint8_t *EndP = NameStrings.bytes_end();
572-
while (P < EndP) {
573-
uint32_t N;
574-
uint64_t UncompressedSize = decodeULEB128(P, &N);
575-
P += N;
576-
uint64_t CompressedSize = decodeULEB128(P, &N);
577-
P += N;
578-
bool isCompressed = (CompressedSize != 0);
579-
SmallVector<uint8_t, 128> UncompressedNameStrings;
580-
StringRef NameStrings;
581-
if (isCompressed) {
582-
if (!llvm::compression::zlib::isAvailable())
583-
return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
584-
585-
if (Error E = compression::zlib::decompress(ArrayRef(P, CompressedSize),
586-
UncompressedNameStrings,
587-
UncompressedSize)) {
588-
consumeError(std::move(E));
589-
return make_error<InstrProfError>(instrprof_error::uncompress_failed);
590-
}
591-
P += CompressedSize;
592-
NameStrings = toStringRef(UncompressedNameStrings);
593-
} else {
594-
NameStrings =
595-
StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
596-
P += UncompressedSize;
597-
}
598-
// Now parse the name strings.
599-
SmallVector<StringRef, 0> Names;
600-
NameStrings.split(Names, getInstrProfNameSeparator());
601-
for (StringRef &Name : Names)
602-
if (Error E = Symtab.addFuncName(Name))
603-
return E;
604-
605-
while (P < EndP && *P == 0)
606-
P++;
607-
}
608-
return Error::success();
609-
}
610-
611622
void InstrProfRecord::accumulateCounts(CountSumOrPercent &Sum) const {
612623
uint64_t FuncSum = 0;
613624
Sum.NumEntries += Counts.size();

0 commit comments

Comments
 (0)