forked from OSchip/llvm-project
[Support] Change zlib::compress to return void
With a sufficiently large output buffer, the only failure is Z_MEM_ERROR. Check it and call the noreturn report_bad_alloc_error if applicable. resize_for_overwrite may call report_bad_alloc_error as well. Now that there is no other error type, we can replace the return type with void and simplify call sites. Reviewed By: ikudrin Differential Revision: https://reviews.llvm.org/D121512
This commit is contained in:
parent
b0a76b0162
commit
407c721ceb
|
@ -192,7 +192,7 @@ public:
|
|||
}
|
||||
if (llvm::zlib::isAvailable()) {
|
||||
llvm::SmallString<1> Compressed;
|
||||
llvm::cantFail(llvm::zlib::compress(RawTable, Compressed));
|
||||
llvm::zlib::compress(RawTable, Compressed);
|
||||
write32(RawTable.size(), OS);
|
||||
OS << Compressed;
|
||||
} else {
|
||||
|
|
|
@ -2007,15 +2007,11 @@ static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
|
|||
// consumers will not want its contents.
|
||||
SmallString<0> CompressedBuffer;
|
||||
if (llvm::zlib::isAvailable()) {
|
||||
llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
|
||||
if (!E) {
|
||||
RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
|
||||
Blob.size() - 1};
|
||||
Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
|
||||
CompressedBuffer);
|
||||
return;
|
||||
}
|
||||
llvm::consumeError(std::move(E));
|
||||
llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
|
||||
RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
|
||||
Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
|
||||
CompressedBuffer);
|
||||
return;
|
||||
}
|
||||
|
||||
RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
|
||||
|
|
|
@ -55,7 +55,6 @@ enum class sampleprof_error {
|
|||
not_implemented,
|
||||
counter_overflow,
|
||||
ostream_seek_unsupported,
|
||||
compress_failed,
|
||||
uncompress_failed,
|
||||
zlib_unavailable,
|
||||
hash_mismatch
|
||||
|
|
|
@ -29,8 +29,8 @@ static constexpr int BestSizeCompression = 9;
|
|||
|
||||
bool isAvailable();
|
||||
|
||||
Error compress(StringRef InputBuffer, SmallVectorImpl<char> &CompressedBuffer,
|
||||
int Level = DefaultCompression);
|
||||
void compress(StringRef InputBuffer, SmallVectorImpl<char> &CompressedBuffer,
|
||||
int Level = DefaultCompression);
|
||||
|
||||
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
||||
size_t &UncompressedSize);
|
||||
|
|
|
@ -864,13 +864,8 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
|
|||
Asm.writeSectionData(VecOS, &Section, Layout);
|
||||
|
||||
SmallVector<char, 128> CompressedContents;
|
||||
if (Error E = zlib::compress(
|
||||
StringRef(UncompressedData.data(), UncompressedData.size()),
|
||||
CompressedContents)) {
|
||||
consumeError(std::move(E));
|
||||
W.OS << UncompressedData;
|
||||
return;
|
||||
}
|
||||
zlib::compress(StringRef(UncompressedData.data(), UncompressedData.size()),
|
||||
CompressedContents);
|
||||
|
||||
bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
|
||||
if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
|
||||
|
|
|
@ -548,13 +548,7 @@ Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
|
|||
Expected<CompressedSection>
|
||||
CompressedSection::create(const SectionBase &Sec,
|
||||
DebugCompressionType CompressionType) {
|
||||
Error Err = Error::success();
|
||||
CompressedSection Section(Sec, CompressionType, Err);
|
||||
|
||||
if (Err)
|
||||
return std::move(Err);
|
||||
|
||||
return Section;
|
||||
return CompressedSection(Sec, CompressionType);
|
||||
}
|
||||
Expected<CompressedSection>
|
||||
CompressedSection::create(ArrayRef<uint8_t> CompressedData,
|
||||
|
@ -564,20 +558,12 @@ CompressedSection::create(ArrayRef<uint8_t> CompressedData,
|
|||
}
|
||||
|
||||
CompressedSection::CompressedSection(const SectionBase &Sec,
|
||||
DebugCompressionType CompressionType,
|
||||
Error &OutErr)
|
||||
DebugCompressionType CompressionType)
|
||||
: SectionBase(Sec), CompressionType(CompressionType),
|
||||
DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
|
||||
ErrorAsOutParameter EAO(&OutErr);
|
||||
|
||||
if (Error Err = zlib::compress(
|
||||
StringRef(reinterpret_cast<const char *>(OriginalData.data()),
|
||||
OriginalData.size()),
|
||||
CompressedData)) {
|
||||
OutErr = createStringError(llvm::errc::invalid_argument,
|
||||
"'" + Name + "': " + toString(std::move(Err)));
|
||||
return;
|
||||
}
|
||||
zlib::compress(StringRef(reinterpret_cast<const char *>(OriginalData.data()),
|
||||
OriginalData.size()),
|
||||
CompressedData);
|
||||
|
||||
size_t ChdrSize;
|
||||
if (CompressionType == DebugCompressionType::GNU) {
|
||||
|
|
|
@ -561,7 +561,7 @@ public:
|
|||
|
||||
private:
|
||||
CompressedSection(const SectionBase &Sec,
|
||||
DebugCompressionType CompressionType, Error &Err);
|
||||
DebugCompressionType CompressionType);
|
||||
CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
|
||||
uint64_t DecompressedAlign);
|
||||
};
|
||||
|
|
|
@ -49,12 +49,8 @@ void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) {
|
|||
SmallString<128> CompressedStr;
|
||||
bool doCompression =
|
||||
Compress && zlib::isAvailable() && DoInstrProfNameCompression;
|
||||
if (doCompression) {
|
||||
auto E =
|
||||
zlib::compress(FilenamesStr, CompressedStr, zlib::BestSizeCompression);
|
||||
if (E)
|
||||
report_bad_alloc_error("Failed to zlib compress coverage data");
|
||||
}
|
||||
if (doCompression)
|
||||
zlib::compress(FilenamesStr, CompressedStr, zlib::BestSizeCompression);
|
||||
|
||||
// ::= <num-filenames>
|
||||
// <uncompressed-len>
|
||||
|
|
|
@ -467,12 +467,8 @@ Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
|
|||
}
|
||||
|
||||
SmallString<128> CompressedNameStrings;
|
||||
Error E = zlib::compress(StringRef(UncompressedNameStrings),
|
||||
CompressedNameStrings, zlib::BestSizeCompression);
|
||||
if (E) {
|
||||
consumeError(std::move(E));
|
||||
return make_error<InstrProfError>(instrprof_error::compress_failed);
|
||||
}
|
||||
zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
|
||||
zlib::BestSizeCompression);
|
||||
|
||||
return WriteStringToResult(CompressedNameStrings.size(),
|
||||
CompressedNameStrings);
|
||||
|
|
|
@ -85,8 +85,6 @@ class SampleProfErrorCategoryType : public std::error_category {
|
|||
return "Counter overflow";
|
||||
case sampleprof_error::ostream_seek_unsupported:
|
||||
return "Ostream does not support seek";
|
||||
case sampleprof_error::compress_failed:
|
||||
return "Compress failure";
|
||||
case sampleprof_error::uncompress_failed:
|
||||
return "Uncompress failure";
|
||||
case sampleprof_error::zlib_unavailable:
|
||||
|
|
|
@ -86,10 +86,8 @@ std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
|
|||
return sampleprof_error::success;
|
||||
auto &OS = *OutputStream;
|
||||
SmallString<128> CompressedStrings;
|
||||
llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
|
||||
zlib::BestSizeCompression);
|
||||
if (E)
|
||||
return sampleprof_error::compress_failed;
|
||||
zlib::compress(UncompressedStrings, CompressedStrings,
|
||||
zlib::BestSizeCompression);
|
||||
encodeULEB128(UncompressedStrings.size(), OS);
|
||||
encodeULEB128(CompressedStrings.size(), OS);
|
||||
OS << CompressedStrings.str();
|
||||
|
|
|
@ -46,18 +46,20 @@ static StringRef convertZlibCodeToString(int Code) {
|
|||
|
||||
bool zlib::isAvailable() { return true; }
|
||||
|
||||
Error zlib::compress(StringRef InputBuffer,
|
||||
SmallVectorImpl<char> &CompressedBuffer, int Level) {
|
||||
void zlib::compress(StringRef InputBuffer,
|
||||
SmallVectorImpl<char> &CompressedBuffer, int Level) {
|
||||
unsigned long CompressedSize = ::compressBound(InputBuffer.size());
|
||||
CompressedBuffer.resize_for_overwrite(CompressedSize);
|
||||
int Res =
|
||||
::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
|
||||
(const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
|
||||
if (Res == Z_MEM_ERROR)
|
||||
report_bad_alloc_error("Allocation failed");
|
||||
assert(Res == Z_OK);
|
||||
// Tell MemorySanitizer that zlib output buffer is fully initialized.
|
||||
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
||||
__msan_unpoison(CompressedBuffer.data(), CompressedSize);
|
||||
CompressedBuffer.truncate(CompressedSize);
|
||||
return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
|
||||
}
|
||||
|
||||
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
||||
|
@ -87,8 +89,8 @@ uint32_t zlib::crc32(StringRef Buffer) {
|
|||
|
||||
#else
|
||||
bool zlib::isAvailable() { return false; }
|
||||
Error zlib::compress(StringRef InputBuffer,
|
||||
SmallVectorImpl<char> &CompressedBuffer, int Level) {
|
||||
void zlib::compress(StringRef InputBuffer,
|
||||
SmallVectorImpl<char> &CompressedBuffer, int Level) {
|
||||
llvm_unreachable("zlib::compress is unavailable");
|
||||
}
|
||||
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
||||
|
|
|
@ -27,13 +27,10 @@ void TestZlibCompression(StringRef Input, int Level) {
|
|||
SmallString<32> Compressed;
|
||||
SmallString<32> Uncompressed;
|
||||
|
||||
Error E = zlib::compress(Input, Compressed, Level);
|
||||
EXPECT_FALSE(E);
|
||||
consumeError(std::move(E));
|
||||
zlib::compress(Input, Compressed, Level);
|
||||
|
||||
// Check that uncompressed buffer is the same as original.
|
||||
E = zlib::uncompress(Compressed, Uncompressed, Input.size());
|
||||
EXPECT_FALSE(E);
|
||||
Error E = zlib::uncompress(Compressed, Uncompressed, Input.size());
|
||||
consumeError(std::move(E));
|
||||
|
||||
EXPECT_EQ(Input, Uncompressed);
|
||||
|
|
Loading…
Reference in New Issue