[llvm] [NFC][InstrProf]Refactor readPGOFuncNameStrings (PR #71566)
Mingming Liu via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 7 10:24:51 PST 2023
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/71566
Refactor this function to take a callback for each decoded string, and change it to a static function in cpp. Move its (sole) caller from header to cpp
>From 25167ea899e3ed266fd6632c050dc8078c77549e Mon Sep 17 00:00:00 2001
From: Mingming Liu <mingmingl at google.com>
Date: Tue, 7 Nov 2023 10:23:00 -0800
Subject: [PATCH] [NFC][InstrProf]Refactor readPGOFuncNameStrings to take a
callback for each decoded string, and change it to a static function in cpp.
Move its (sole) caller from header to cpp
---
llvm/include/llvm/ProfileData/InstrProf.h | 19 +---
llvm/lib/ProfileData/InstrProf.cpp | 130 +++++++++++++++-------
2 files changed, 93 insertions(+), 56 deletions(-)
diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index 6b9d37555616c36..3bc677d5b6d8670 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -242,11 +242,6 @@ Error collectGlobalObjectNameStrings(ArrayRef<std::string> NameStrs,
Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
std::string &Result, bool doCompression = true);
-/// \c NameStrings is a string composed of one of more sub-strings encoded in
-/// the format described above. The substrings are separated by 0 or more zero
-/// bytes. This method decodes the string and populates the \c Symtab.
-Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab);
-
/// Check if INSTR_PROF_RAW_VERSION_VAR is defined. This global is only being
/// set in IR PGO compilation.
bool isIRPGOFlagSet(const Module *M);
@@ -469,14 +464,14 @@ class InstrProfSymtab {
/// until before it is used. See also \c create(StringRef) method.
Error create(object::SectionRef &Section);
- /// This interface is used by reader of CoverageMapping test
- /// format.
- inline Error create(StringRef D, uint64_t BaseAddr);
-
/// \c NameStrings is a string composed of one of more sub-strings
/// encoded in the format described in \c collectPGOFuncNameStrings.
/// This method is a wrapper to \c readPGOFuncNameStrings method.
- inline Error create(StringRef NameStrings);
+ Error create(StringRef NameStrings);
+
+ /// This interface is used by reader of CoverageMapping test
+ /// format.
+ inline Error create(StringRef D, uint64_t BaseAddr);
/// A wrapper interface to populate the PGO symtab with functions
/// decls from module \c M. This interface is used by transformation
@@ -547,10 +542,6 @@ Error InstrProfSymtab::create(StringRef D, uint64_t 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)
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index 47bd1bd93490db1..949bc39c402aecf 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -458,6 +458,94 @@ Error InstrProfSymtab::create(Module &M, bool InLTO) {
return Error::success();
}
+static instrprof_error decodeAndSplitStrings(
+ const uint8_t *Input, SmallVector<uint8_t, 128> &UncompressedNameStrings,
+ StringRef &NameStrings, uint32_t &Dist, bool &IsCompressed) {
+ Dist = 0;
+ const uint8_t *Start = Input;
+ uint32_t UncompressedSizeLen = 0;
+ uint64_t UncompressedSize = decodeULEB128(Start, &UncompressedSizeLen);
+ Start += UncompressedSizeLen;
+ Dist += UncompressedSizeLen;
+ uint32_t CompressedSizeLen = 0;
+ uint64_t CompressedSize = decodeULEB128(Start, &CompressedSizeLen);
+ Start += CompressedSizeLen;
+ Dist += CompressedSizeLen;
+ IsCompressed = (CompressedSize != 0);
+ if (!IsCompressed) {
+ NameStrings =
+ StringRef(reinterpret_cast<const char *>(Start), UncompressedSize);
+ Dist += UncompressedSize;
+ return instrprof_error::success;
+ }
+
+ if (!llvm::compression::zlib::isAvailable())
+ return instrprof_error::zlib_unavailable;
+
+ if (Error E = compression::zlib::decompress(ArrayRef(Start, CompressedSize),
+ UncompressedNameStrings,
+ UncompressedSize)) {
+ consumeError(std::move(E));
+ return instrprof_error::uncompress_failed;
+ }
+ Dist += CompressedSize;
+
+ return instrprof_error::success;
+}
+
+/// \c NameStrings is a string composed of one of more possibly encoded
+/// sub-strings. The substrings are separated by 0 or more zero bytes. This
+/// method decodes the string and calls `NameCallback` for each substring.
+static Error
+readAndDecodeStrings(StringRef NameStrings,
+ std::function<Error(StringRef)> NameCallback) {
+ const uint8_t *P = NameStrings.bytes_begin();
+ const uint8_t *EndP = NameStrings.bytes_end();
+ while (P < EndP) {
+ uint32_t N;
+ uint64_t UncompressedSize = decodeULEB128(P, &N);
+ P += N;
+ uint64_t CompressedSize = decodeULEB128(P, &N);
+ P += N;
+ bool isCompressed = (CompressedSize != 0);
+ SmallVector<uint8_t, 128> UncompressedNameStrings;
+ StringRef NameStrings;
+ if (isCompressed) {
+ if (!llvm::compression::zlib::isAvailable())
+ return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
+
+ if (Error E = compression::zlib::decompress(ArrayRef(P, CompressedSize),
+ UncompressedNameStrings,
+ UncompressedSize)) {
+ consumeError(std::move(E));
+ return make_error<InstrProfError>(instrprof_error::uncompress_failed);
+ }
+ P += CompressedSize;
+ NameStrings = toStringRef(UncompressedNameStrings);
+ } else {
+ NameStrings =
+ StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
+ P += UncompressedSize;
+ }
+ // Now parse the name strings.
+ SmallVector<StringRef, 0> Names;
+ NameStrings.split(Names, getInstrProfNameSeparator());
+ for (StringRef &Name : Names)
+ if (Error E = NameCallback(Name))
+ return E;
+
+ while (P < EndP && *P == 0)
+ P++;
+ }
+ return Error::success();
+}
+
+Error InstrProfSymtab::create(StringRef NameStrings) {
+ return readAndDecodeStrings(
+ NameStrings,
+ std::bind(&InstrProfSymtab::addFuncName, this, std::placeholders::_1));
+}
+
Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
if (Error E = addFuncName(PGOFuncName))
return E;
@@ -566,48 +654,6 @@ Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
NameStrs, compression::zlib::isAvailable() && doCompression, Result);
}
-Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
- const uint8_t *P = NameStrings.bytes_begin();
- const uint8_t *EndP = NameStrings.bytes_end();
- while (P < EndP) {
- uint32_t N;
- uint64_t UncompressedSize = decodeULEB128(P, &N);
- P += N;
- uint64_t CompressedSize = decodeULEB128(P, &N);
- P += N;
- bool isCompressed = (CompressedSize != 0);
- SmallVector<uint8_t, 128> UncompressedNameStrings;
- StringRef NameStrings;
- if (isCompressed) {
- if (!llvm::compression::zlib::isAvailable())
- return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
-
- if (Error E = compression::zlib::decompress(ArrayRef(P, CompressedSize),
- UncompressedNameStrings,
- UncompressedSize)) {
- consumeError(std::move(E));
- return make_error<InstrProfError>(instrprof_error::uncompress_failed);
- }
- P += CompressedSize;
- NameStrings = toStringRef(UncompressedNameStrings);
- } else {
- NameStrings =
- StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
- P += UncompressedSize;
- }
- // Now parse the name strings.
- SmallVector<StringRef, 0> Names;
- NameStrings.split(Names, getInstrProfNameSeparator());
- for (StringRef &Name : Names)
- if (Error E = Symtab.addFuncName(Name))
- return E;
-
- while (P < EndP && *P == 0)
- P++;
- }
- return Error::success();
-}
-
void InstrProfRecord::accumulateCounts(CountSumOrPercent &Sum) const {
uint64_t FuncSum = 0;
Sum.NumEntries += Counts.size();
More information about the llvm-commits
mailing list