1//===--- Compression.cpp - Compression implementation ---------------------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// This file implements compression functions. 11//===----------------------------------------------------------------------===// 16#include "llvm/Config/config.h" 32case compression::Format::Zlib:
35return"LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at " 37case compression::Format::Zstd:
40return"LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at " 49case compression::Format::Zlib:
52case compression::Format::Zstd:
59uint8_t *Output,
size_t UncompressedSize) {
61case compression::Format::Zlib:
63case compression::Format::Zstd:
71size_t UncompressedSize) {
73case compression::Format::Zlib:
75case compression::Format::Zstd:
83size_t UncompressedSize) {
89staticStringRef convertZlibCodeToString(
int Code) {
92return"zlib error: Z_MEM_ERROR";
94return"zlib error: Z_BUF_ERROR";
96return"zlib error: Z_STREAM_ERROR";
98return"zlib error: Z_DATA_ERROR";
109unsignedlong CompressedSize = ::compressBound(Input.
size());
111int Res = ::compress2((Bytef *)CompressedBuffer.
data(), &CompressedSize,
112 (
const Bytef *)Input.
data(), Input.
size(), Level);
113if (Res == Z_MEM_ERROR)
116// Tell MemorySanitizer that zlib output buffer is fully initialized. 117// This avoids a false report when running LLVM with uninstrumented ZLib. 119if (CompressedSize < CompressedBuffer.
size())
120 CompressedBuffer.
truncate(CompressedSize);
124size_t &UncompressedSize) {
125int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
126 (
const Bytef *)Input.
data(), Input.
size());
127// Tell MemorySanitizer that zlib output buffer is fully initialized. 128// This avoids a false report when running LLVM with uninstrumented ZLib. 130return Res ? make_error<StringError>(convertZlibCodeToString(Res),
137size_t UncompressedSize) {
140if (UncompressedSize < Output.
size())
152size_t &UncompressedSize) {
157size_t UncompressedSize) {
166#include <zstd.h>// Ensure ZSTD library is included 171 ZSTD_CCtx *Cctx = ZSTD_createCCtx();
175if (ZSTD_isError(ZSTD_CCtx_setParameter(
176 Cctx, ZSTD_c_enableLongDistanceMatching, EnableLdm ? 1 : 0))) {
182 ZSTD_CCtx_setParameter(Cctx, ZSTD_c_compressionLevel, Level))) {
187unsignedlong CompressedBufferSize = ZSTD_compressBound(Input.
size());
190size_tconst CompressedSize =
191 ZSTD_compress2(Cctx, CompressedBuffer.
data(), CompressedBufferSize,
196if (ZSTD_isError(CompressedSize))
200if (CompressedSize < CompressedBuffer.
size())
201 CompressedBuffer.
truncate(CompressedSize);
205size_t &UncompressedSize) {
206constsize_t Res = ::ZSTD_decompress(
208 UncompressedSize = Res;
209if (ZSTD_isError(Res))
210return make_error<StringError>(ZSTD_getErrorName(Res),
212// Tell MemorySanitizer that zstd output buffer is fully initialized. 213// This avoids a false report when running LLVM with uninstrumented ZLib. 220size_t UncompressedSize) {
223if (UncompressedSize < Output.
size())
236size_t &UncompressedSize) {
241size_t UncompressedSize) {
#define __msan_unpoison(p, size)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
void truncate(size_type N)
Like resize, but requires that N is less than size().
pointer data()
Return a pointer to the vector's buffer, even if empty().
StringRef - Represent a constant reference to a string, i.e.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression, bool EnableLdm=false)
const char * getReasonIfUnsupported(Format F)
Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Format formatFor(DebugCompressionType Type)
void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
This is an optimization pass for GlobalISel generic memory operations.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.