[SampleFDO] Add compression support for any section in ExtBinary profile format
Previously ExtBinary profile format only supports compression using zlib for
profile symbol list. In this patch, we extend the compression support to any
section. User can select some or all of the sections to compress. In an
experiment, for a 45M profile in ExtBinary format, compressing name table
reduced its size to 24M, and compressing all the sections reduced its size
to 11M.
Differential Revision: https://reviews.llvm.org/D68253
llvm-svn: 373914
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp
index e94848d..2150c5d 100644
--- a/llvm/lib/ProfileData/SampleProf.cpp
+++ b/llvm/lib/ProfileData/SampleProf.cpp
@@ -15,7 +15,6 @@
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/Support/Compiler.h"
-#include "llvm/Support/Compression.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
@@ -198,66 +197,34 @@
LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
#endif
-std::error_code ProfileSymbolList::read(uint64_t CompressSize,
- uint64_t UncompressSize,
- const uint8_t *Data) {
+std::error_code ProfileSymbolList::read(const uint8_t *Data,
+ uint64_t ListSize) {
const char *ListStart = reinterpret_cast<const char *>(Data);
- // CompressSize being non-zero means the profile is compressed and
- // needs to be uncompressed first.
- if (CompressSize) {
- if (!llvm::zlib::isAvailable())
- return sampleprof_error::zlib_unavailable;
-
- StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
- CompressSize);
- char *Buffer = Allocator.Allocate<char>(UncompressSize);
- size_t UCSize = UncompressSize;
- llvm::Error E = zlib::uncompress(CompressedStrings, Buffer, UCSize);
- if (E)
- return sampleprof_error::uncompress_failed;
- ListStart = Buffer;
- }
-
uint64_t Size = 0;
- while (Size < UncompressSize) {
+ while (Size < ListSize) {
StringRef Str(ListStart + Size);
add(Str);
Size += Str.size() + 1;
}
+ if (Size != ListSize)
+ return sampleprof_error::malformed;
return sampleprof_error::success;
}
std::error_code ProfileSymbolList::write(raw_ostream &OS) {
- // Sort the symbols before doing compression. It will make the
- // compression much more effective.
+ // Sort the symbols before output. If doing compression.
+ // It will make the compression much more effective.
std::vector<StringRef> SortedList;
SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end());
llvm::sort(SortedList);
- std::string UncompressedStrings;
+ std::string OutputString;
for (auto &Sym : SortedList) {
- UncompressedStrings.append(Sym.str());
- UncompressedStrings.append(1, '\0');
+ OutputString.append(Sym.str());
+ OutputString.append(1, '\0');
}
- if (ToCompress) {
- if (!llvm::zlib::isAvailable())
- return sampleprof_error::zlib_unavailable;
- SmallString<128> CompressedStrings;
- llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
- zlib::BestSizeCompression);
- if (E)
- return sampleprof_error::compress_failed;
- encodeULEB128(UncompressedStrings.size(), OS);
- encodeULEB128(CompressedStrings.size(), OS);
- OS << CompressedStrings.str();
- } else {
- encodeULEB128(UncompressedStrings.size(), OS);
- // If profile symbol list is not compressed, we will still save
- // a compressed size value, but the value of the size is 0.
- encodeULEB128(0, OS);
- OS << UncompressedStrings;
- }
+ OS << OutputString;
return sampleprof_error::success;
}