From e413f1a0fc5b01d405e64ceec5cf855b4d38e346 Mon Sep 17 00:00:00 2001 From: Xinliang David Li Date: Thu, 31 Dec 2015 07:57:16 +0000 Subject: [PGO]: Implement Func PGO name string compression This is part of the effort/prepration to reduce the size instr-pgo (object, binary, memory footprint, and raw data). The functionality is currently off by default and not yet used by any clients. llvm-svn: 256667 --- llvm/lib/ProfileData/InstrProf.cpp | 101 ++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) (limited to 'llvm/lib/ProfileData') diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index f5acd23129d..df3f8fade3b 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -12,12 +12,14 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ProfileData/InstrProf.h" #include "llvm/IR/Constants.h" #include "llvm/IR/Function.h" -#include "llvm/IR/Module.h" #include "llvm/IR/GlobalVariable.h" -#include "llvm/ProfileData/InstrProf.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/Compression.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/LEB128.h" #include "llvm/Support/ManagedStatic.h" using namespace llvm; @@ -162,6 +164,101 @@ GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) { return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName); } +int collectPGOFuncNameStrings(const std::vector &NameStrs, + bool doCompression, std::string &Result) { + uint8_t Header[16], *P = Header; + std::string UncompressedNameStrings; + + for (auto NameStr : NameStrs) { + UncompressedNameStrings += NameStr; + UncompressedNameStrings.append(" "); + } + unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P); + P += EncLen; + if (!doCompression) { + EncLen = encodeULEB128(0, P); + P += EncLen; + Result.append(reinterpret_cast(&Header[0]), P - &Header[0]); + Result += UncompressedNameStrings; + return 0; + } + SmallVector CompressedNameStrings; + zlib::Status Success = + zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, + zlib::BestSizeCompression); + assert(Success == zlib::StatusOK); + if (Success != zlib::StatusOK) + return 1; + EncLen = encodeULEB128(CompressedNameStrings.size(), P); + P += EncLen; + Result.append(reinterpret_cast(&Header[0]), P - &Header[0]); + Result += + std::string(CompressedNameStrings.data(), CompressedNameStrings.size()); + return 0; +} + +int collectPGOFuncNameStrings(const std::vector &NameVars, + std::string &Result) { + std::vector NameStrs; + for (auto *NameVar : NameVars) { + auto *Arr = cast(NameVar->getInitializer()); + StringRef NameStr = + Arr->isCString() ? Arr->getAsCString() : Arr->getAsString(); + NameStrs.push_back(NameStr.str()); + } + return collectPGOFuncNameStrings(NameStrs, zlib::isAvailable(), Result); +} + +int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { + const uint8_t *P = reinterpret_cast(NameStrings.data()); + const uint8_t *EndP = reinterpret_cast(NameStrings.data() + + NameStrings.size()); + 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); + SmallString<128> UncompressedNameStrings; + StringRef NameStrings; + if (isCompressed) { + StringRef CompressedNameStrings(reinterpret_cast(P), + CompressedSize); + if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, + UncompressedSize) != zlib::StatusOK) + return 1; + P += CompressedSize; + NameStrings = StringRef(UncompressedNameStrings.data(), + UncompressedNameStrings.size()); + } else { + NameStrings = + StringRef(reinterpret_cast(P), UncompressedSize); + P += UncompressedSize; + } + // Now parse the name strings. + size_t NameStart = 0; + bool isLast = false; + do { + size_t NameStop = NameStrings.find(' ', NameStart); + if (NameStop == StringRef::npos) + return 1; + if (NameStop == NameStrings.size() - 1) + isLast = true; + StringRef Name = NameStrings.substr(NameStart, NameStop - NameStart); + Symtab.addFuncName(Name); + if (isLast) + break; + NameStart = NameStop + 1; + } while (true); + + while (P < EndP && *P == 0) + P++; + } + Symtab.finalizeSymtab(); + return 0; +} + instrprof_error InstrProfValueSiteRecord::mergeValueData(InstrProfValueSiteRecord &Input, uint64_t Weight) { -- cgit v1.2.3