Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
Compression.cpp
Go to the documentation of this file.
1//===--- Compression.cpp - Compression implementation ---------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements compression functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/Compression.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Config/config.h"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Error.h"
19#include "llvm/Support/ErrorHandling.h"
20#if LLVM_ENABLE_ZLIB
21#include <zlib.h>
22#endif
23#if LLVM_ENABLE_ZSTD
24#include <zstd.h>
25#endif
26
27using namespacellvm;
28using namespacellvm::compression;
29
30constchar *compression::getReasonIfUnsupported(compression::FormatF) {
31switch (F) {
32case compression::Format::Zlib:
33if (zlib::isAvailable())
34returnnullptr;
35return"LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
36"build time";
37case compression::Format::Zstd:
38if (zstd::isAvailable())
39returnnullptr;
40return"LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
41"build time";
42 }
43llvm_unreachable("");
44}
45
46voidcompression::compress(ParamsP,ArrayRef<uint8_t> Input,
47SmallVectorImpl<uint8_t> &Output) {
48switch (P.format) {
49case compression::Format::Zlib:
50zlib::compress(Input, Output,P.level);
51break;
52case compression::Format::Zstd:
53zstd::compress(Input, Output,P.level,P.zstdEnableLdm);
54break;
55 }
56}
57
58Errorcompression::decompress(DebugCompressionTypeT,ArrayRef<uint8_t> Input,
59uint8_t *Output,size_t UncompressedSize) {
60switch (formatFor(T)) {
61case compression::Format::Zlib:
62returnzlib::decompress(Input, Output, UncompressedSize);
63case compression::Format::Zstd:
64returnzstd::decompress(Input, Output, UncompressedSize);
65 }
66llvm_unreachable("");
67}
68
69Errorcompression::decompress(compression::FormatF,ArrayRef<uint8_t> Input,
70SmallVectorImpl<uint8_t> &Output,
71size_t UncompressedSize) {
72switch (F) {
73case compression::Format::Zlib:
74returnzlib::decompress(Input, Output, UncompressedSize);
75case compression::Format::Zstd:
76returnzstd::decompress(Input, Output, UncompressedSize);
77 }
78llvm_unreachable("");
79}
80
81Errorcompression::decompress(DebugCompressionTypeT,ArrayRef<uint8_t> Input,
82SmallVectorImpl<uint8_t> &Output,
83size_t UncompressedSize) {
84returndecompress(formatFor(T), Input, Output, UncompressedSize);
85}
86
87#if LLVM_ENABLE_ZLIB
88
89staticStringRef convertZlibCodeToString(int Code) {
90switch (Code) {
91case Z_MEM_ERROR:
92return"zlib error: Z_MEM_ERROR";
93case Z_BUF_ERROR:
94return"zlib error: Z_BUF_ERROR";
95case Z_STREAM_ERROR:
96return"zlib error: Z_STREAM_ERROR";
97case Z_DATA_ERROR:
98return"zlib error: Z_DATA_ERROR";
99case Z_OK:
100default:
101llvm_unreachable("unknown or unexpected zlib status code");
102 }
103}
104
105boolzlib::isAvailable() {returntrue; }
106
107voidzlib::compress(ArrayRef<uint8_t> Input,
108SmallVectorImpl<uint8_t> &CompressedBuffer,int Level) {
109unsignedlong CompressedSize = ::compressBound(Input.size());
110 CompressedBuffer.resize_for_overwrite(CompressedSize);
111int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
112 (const Bytef *)Input.data(), Input.size(), Level);
113if (Res == Z_MEM_ERROR)
114report_bad_alloc_error("Allocation failed");
115assert(Res == Z_OK);
116// Tell MemorySanitizer that zlib output buffer is fully initialized.
117// This avoids a false report when running LLVM with uninstrumented ZLib.
118__msan_unpoison(CompressedBuffer.data(), CompressedSize);
119if (CompressedSize < CompressedBuffer.size())
120 CompressedBuffer.truncate(CompressedSize);
121}
122
123Errorzlib::decompress(ArrayRef<uint8_t> Input,uint8_t *Output,
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.
129__msan_unpoison(Output, UncompressedSize);
130return Res ? make_error<StringError>(convertZlibCodeToString(Res),
131inconvertibleErrorCode())
132 :Error::success();
133}
134
135Errorzlib::decompress(ArrayRef<uint8_t> Input,
136SmallVectorImpl<uint8_t> &Output,
137size_t UncompressedSize) {
138 Output.resize_for_overwrite(UncompressedSize);
139Error E =zlib::decompress(Input, Output.data(), UncompressedSize);
140if (UncompressedSize < Output.size())
141 Output.truncate(UncompressedSize);
142return E;
143}
144
145#else
146boolzlib::isAvailable() {returnfalse; }
147voidzlib::compress(ArrayRef<uint8_t> Input,
148SmallVectorImpl<uint8_t> &CompressedBuffer,int Level) {
149llvm_unreachable("zlib::compress is unavailable");
150}
151Errorzlib::decompress(ArrayRef<uint8_t> Input,uint8_t *UncompressedBuffer,
152size_t &UncompressedSize) {
153llvm_unreachable("zlib::decompress is unavailable");
154}
155Errorzlib::decompress(ArrayRef<uint8_t> Input,
156SmallVectorImpl<uint8_t> &UncompressedBuffer,
157size_t UncompressedSize) {
158llvm_unreachable("zlib::decompress is unavailable");
159}
160#endif
161
162#if LLVM_ENABLE_ZSTD
163
164boolzstd::isAvailable() {returntrue; }
165
166#include <zstd.h>// Ensure ZSTD library is included
167
168voidzstd::compress(ArrayRef<uint8_t> Input,
169SmallVectorImpl<uint8_t> &CompressedBuffer,int Level,
170bool EnableLdm) {
171 ZSTD_CCtx *Cctx = ZSTD_createCCtx();
172if (!Cctx)
173report_bad_alloc_error("Failed to create ZSTD_CCtx");
174
175if (ZSTD_isError(ZSTD_CCtx_setParameter(
176 Cctx, ZSTD_c_enableLongDistanceMatching, EnableLdm ? 1 : 0))) {
177 ZSTD_freeCCtx(Cctx);
178report_bad_alloc_error("Failed to set ZSTD_c_enableLongDistanceMatching");
179 }
180
181if (ZSTD_isError(
182 ZSTD_CCtx_setParameter(Cctx, ZSTD_c_compressionLevel, Level))) {
183 ZSTD_freeCCtx(Cctx);
184report_bad_alloc_error("Failed to set ZSTD_c_compressionLevel");
185 }
186
187unsignedlong CompressedBufferSize = ZSTD_compressBound(Input.size());
188 CompressedBuffer.resize_for_overwrite(CompressedBufferSize);
189
190size_tconst CompressedSize =
191 ZSTD_compress2(Cctx, CompressedBuffer.data(), CompressedBufferSize,
192 Input.data(), Input.size());
193
194 ZSTD_freeCCtx(Cctx);
195
196if (ZSTD_isError(CompressedSize))
197report_bad_alloc_error("Compression failed");
198
199__msan_unpoison(CompressedBuffer.data(), CompressedSize);
200if (CompressedSize < CompressedBuffer.size())
201 CompressedBuffer.truncate(CompressedSize);
202}
203
204Errorzstd::decompress(ArrayRef<uint8_t> Input,uint8_t *Output,
205size_t &UncompressedSize) {
206constsize_t Res = ::ZSTD_decompress(
207 Output, UncompressedSize, (constuint8_t *)Input.data(), Input.size());
208 UncompressedSize = Res;
209if (ZSTD_isError(Res))
210return make_error<StringError>(ZSTD_getErrorName(Res),
211inconvertibleErrorCode());
212// Tell MemorySanitizer that zstd output buffer is fully initialized.
213// This avoids a false report when running LLVM with uninstrumented ZLib.
214__msan_unpoison(Output, UncompressedSize);
215returnError::success();
216}
217
218Errorzstd::decompress(ArrayRef<uint8_t> Input,
219SmallVectorImpl<uint8_t> &Output,
220size_t UncompressedSize) {
221 Output.resize_for_overwrite(UncompressedSize);
222Error E =zstd::decompress(Input, Output.data(), UncompressedSize);
223if (UncompressedSize < Output.size())
224 Output.truncate(UncompressedSize);
225return E;
226}
227
228#else
229boolzstd::isAvailable() {returnfalse; }
230voidzstd::compress(ArrayRef<uint8_t> Input,
231SmallVectorImpl<uint8_t> &CompressedBuffer,int Level,
232bool EnableLdm) {
233llvm_unreachable("zstd::compress is unavailable");
234}
235Errorzstd::decompress(ArrayRef<uint8_t> Input,uint8_t *Output,
236size_t &UncompressedSize) {
237llvm_unreachable("zstd::decompress is unavailable");
238}
239Errorzstd::decompress(ArrayRef<uint8_t> Input,
240SmallVectorImpl<uint8_t> &Output,
241size_t UncompressedSize) {
242llvm_unreachable("zstd::decompress is unavailable");
243}
244#endif
Compiler.h
__msan_unpoison
#define __msan_unpoison(p, size)
Definition:Compiler.h:528
Compression.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
P
#define P(N)
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector.h
This file defines the SmallVector class.
StringRef.h
T
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::data
const T * data() const
Definition:ArrayRef.h:165
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition:Error.h:337
llvm::SmallVectorBase::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::resize_for_overwrite
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition:SmallVector.h:641
llvm::SmallVectorImpl::truncate
void truncate(size_type N)
Like resize, but requires that N is less than size().
Definition:SmallVector.h:644
llvm::SmallVectorTemplateCommon::data
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition:SmallVector.h:286
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
uint8_t
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
Error.h
llvm::compression::zlib::compress
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
Definition:Compression.cpp:147
llvm::compression::zlib::decompress
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
Definition:Compression.cpp:151
llvm::compression::zlib::isAvailable
bool isAvailable()
Definition:Compression.cpp:146
llvm::compression::zstd::decompress
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
Definition:Compression.cpp:235
llvm::compression::zstd::isAvailable
bool isAvailable()
Definition:Compression.cpp:229
llvm::compression::zstd::compress
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression, bool EnableLdm=false)
Definition:Compression.cpp:230
llvm::compression
Definition:Compression.h:33
llvm::compression::getReasonIfUnsupported
const char * getReasonIfUnsupported(Format F)
Definition:Compression.cpp:30
llvm::compression::decompress
Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Definition:Compression.cpp:58
llvm::compression::Format
Format
Definition:Compression.h:76
llvm::compression::formatFor
Format formatFor(DebugCompressionType Type)
Definition:Compression.h:81
llvm::compression::compress
void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
Definition:Compression.cpp:46
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition:Error.cpp:98
llvm::cgdata_error::success
@ success
llvm::DebugCompressionType
DebugCompressionType
Definition:Compression.h:27
llvm::report_bad_alloc_error
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
Definition:ErrorHandling.cpp:148
llvm::compression::Params
Definition:Compression.h:93

Generated on Thu Jul 17 2025 12:37:26 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp