annotate llvm/lib/Support/Compression.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents c4bab56944e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- Compression.cpp - Compression implementation ---------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements compression functions.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "llvm/Support/Compression.h"
anatofuz
parents:
diff changeset
14 #include "llvm/ADT/SmallVector.h"
anatofuz
parents:
diff changeset
15 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
16 #include "llvm/Config/config.h"
anatofuz
parents:
diff changeset
17 #include "llvm/Support/Compiler.h"
anatofuz
parents:
diff changeset
18 #include "llvm/Support/Error.h"
anatofuz
parents:
diff changeset
19 #include "llvm/Support/ErrorHandling.h"
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
20 #if LLVM_ENABLE_ZLIB
150
anatofuz
parents:
diff changeset
21 #include <zlib.h>
anatofuz
parents:
diff changeset
22 #endif
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
23 #if LLVM_ENABLE_ZSTD
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
24 #include <zstd.h>
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
25 #endif
150
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 using namespace llvm;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
28 using namespace llvm::compression;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
29
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
30 const char *compression::getReasonIfUnsupported(compression::Format F) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
31 switch (F) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
32 case compression::Format::Zlib:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
33 if (zlib::isAvailable())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
34 return nullptr;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
35 return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
36 "build time";
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
37 case compression::Format::Zstd:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
38 if (zstd::isAvailable())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
39 return nullptr;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
40 return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
41 "build time";
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
42 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
43 llvm_unreachable("");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
44 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
45
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
46 void compression::compress(Params P, ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
47 SmallVectorImpl<uint8_t> &Output) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
48 switch (P.format) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
49 case compression::Format::Zlib:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
50 zlib::compress(Input, Output, P.level);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
51 break;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
52 case compression::Format::Zstd:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
53 zstd::compress(Input, Output, P.level);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
54 break;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
55 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
56 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
57
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
58 Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
59 uint8_t *Output, size_t UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
60 switch (formatFor(T)) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
61 case compression::Format::Zlib:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
62 return zlib::decompress(Input, Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
63 case compression::Format::Zstd:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
64 return zstd::decompress(Input, Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
65 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
66 llvm_unreachable("");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
67 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
68
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
69 Error compression::decompress(compression::Format F, ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
70 SmallVectorImpl<uint8_t> &Output,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
71 size_t UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
72 switch (F) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
73 case compression::Format::Zlib:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
74 return zlib::decompress(Input, Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
75 case compression::Format::Zstd:
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
76 return zstd::decompress(Input, Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
77 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
78 llvm_unreachable("");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
79 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
80
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
81 Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
82 SmallVectorImpl<uint8_t> &Output,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
83 size_t UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
84 return decompress(formatFor(T), Input, Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
85 }
150
anatofuz
parents:
diff changeset
86
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 #if LLVM_ENABLE_ZLIB
150
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 static StringRef convertZlibCodeToString(int Code) {
anatofuz
parents:
diff changeset
90 switch (Code) {
anatofuz
parents:
diff changeset
91 case Z_MEM_ERROR:
anatofuz
parents:
diff changeset
92 return "zlib error: Z_MEM_ERROR";
anatofuz
parents:
diff changeset
93 case Z_BUF_ERROR:
anatofuz
parents:
diff changeset
94 return "zlib error: Z_BUF_ERROR";
anatofuz
parents:
diff changeset
95 case Z_STREAM_ERROR:
anatofuz
parents:
diff changeset
96 return "zlib error: Z_STREAM_ERROR";
anatofuz
parents:
diff changeset
97 case Z_DATA_ERROR:
anatofuz
parents:
diff changeset
98 return "zlib error: Z_DATA_ERROR";
anatofuz
parents:
diff changeset
99 case Z_OK:
anatofuz
parents:
diff changeset
100 default:
anatofuz
parents:
diff changeset
101 llvm_unreachable("unknown or unexpected zlib status code");
anatofuz
parents:
diff changeset
102 }
anatofuz
parents:
diff changeset
103 }
anatofuz
parents:
diff changeset
104
anatofuz
parents:
diff changeset
105 bool zlib::isAvailable() { return true; }
anatofuz
parents:
diff changeset
106
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
107 void zlib::compress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
108 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
109 unsigned long CompressedSize = ::compressBound(Input.size());
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
110 CompressedBuffer.resize_for_overwrite(CompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
111 int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
112 (const Bytef *)Input.data(), Input.size(), Level);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
113 if (Res == Z_MEM_ERROR)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
114 report_bad_alloc_error("Allocation failed");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
115 assert(Res == Z_OK);
150
anatofuz
parents:
diff changeset
116 // Tell MemorySanitizer that zlib output buffer is fully initialized.
anatofuz
parents:
diff changeset
117 // This avoids a false report when running LLVM with uninstrumented ZLib.
anatofuz
parents:
diff changeset
118 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
119 if (CompressedSize < CompressedBuffer.size())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
120 CompressedBuffer.truncate(CompressedSize);
150
anatofuz
parents:
diff changeset
121 }
anatofuz
parents:
diff changeset
122
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
123 Error zlib::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
150
anatofuz
parents:
diff changeset
124 size_t &UncompressedSize) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
125 int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
126 (const Bytef *)Input.data(), Input.size());
150
anatofuz
parents:
diff changeset
127 // Tell MemorySanitizer that zlib output buffer is fully initialized.
anatofuz
parents:
diff changeset
128 // This avoids a false report when running LLVM with uninstrumented ZLib.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
129 __msan_unpoison(Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
130 return Res ? make_error<StringError>(convertZlibCodeToString(Res),
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
131 inconvertibleErrorCode())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
132 : Error::success();
150
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
135 Error zlib::decompress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
136 SmallVectorImpl<uint8_t> &Output,
150
anatofuz
parents:
diff changeset
137 size_t UncompressedSize) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
138 Output.resize_for_overwrite(UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
139 Error E = zlib::decompress(Input, Output.data(), UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
140 if (UncompressedSize < Output.size())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
141 Output.truncate(UncompressedSize);
150
anatofuz
parents:
diff changeset
142 return E;
anatofuz
parents:
diff changeset
143 }
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 #else
anatofuz
parents:
diff changeset
146 bool zlib::isAvailable() { return false; }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
147 void zlib::compress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
148 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
150
anatofuz
parents:
diff changeset
149 llvm_unreachable("zlib::compress is unavailable");
anatofuz
parents:
diff changeset
150 }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
151 Error zlib::decompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
150
anatofuz
parents:
diff changeset
152 size_t &UncompressedSize) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
153 llvm_unreachable("zlib::decompress is unavailable");
150
anatofuz
parents:
diff changeset
154 }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
155 Error zlib::decompress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
156 SmallVectorImpl<uint8_t> &UncompressedBuffer,
150
anatofuz
parents:
diff changeset
157 size_t UncompressedSize) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
158 llvm_unreachable("zlib::decompress is unavailable");
150
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160 #endif
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
161
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
162 #if LLVM_ENABLE_ZSTD
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
163
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
164 bool zstd::isAvailable() { return true; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
165
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
166 void zstd::compress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
167 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
168 unsigned long CompressedBufferSize = ::ZSTD_compressBound(Input.size());
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
169 CompressedBuffer.resize_for_overwrite(CompressedBufferSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
170 unsigned long CompressedSize =
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
171 ::ZSTD_compress((char *)CompressedBuffer.data(), CompressedBufferSize,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
172 (const char *)Input.data(), Input.size(), Level);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
173 if (ZSTD_isError(CompressedSize))
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
174 report_bad_alloc_error("Allocation failed");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
175 // Tell MemorySanitizer that zstd output buffer is fully initialized.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
176 // This avoids a false report when running LLVM with uninstrumented ZLib.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
177 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
178 if (CompressedSize < CompressedBuffer.size())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
179 CompressedBuffer.truncate(CompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
180 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
181
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
182 Error zstd::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
183 size_t &UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
184 const size_t Res = ::ZSTD_decompress(
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
185 Output, UncompressedSize, (const uint8_t *)Input.data(), Input.size());
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
186 UncompressedSize = Res;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
187 // Tell MemorySanitizer that zstd output buffer is fully initialized.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
188 // This avoids a false report when running LLVM with uninstrumented ZLib.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
189 __msan_unpoison(Output, UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
190 return ZSTD_isError(Res) ? make_error<StringError>(ZSTD_getErrorName(Res),
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
191 inconvertibleErrorCode())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
192 : Error::success();
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
193 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
194
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
195 Error zstd::decompress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
196 SmallVectorImpl<uint8_t> &Output,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
197 size_t UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
198 Output.resize_for_overwrite(UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
199 Error E = zstd::decompress(Input, Output.data(), UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
200 if (UncompressedSize < Output.size())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
201 Output.truncate(UncompressedSize);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
202 return E;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
203 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
204
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
205 #else
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
206 bool zstd::isAvailable() { return false; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
207 void zstd::compress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
208 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
209 llvm_unreachable("zstd::compress is unavailable");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
210 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
211 Error zstd::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
212 size_t &UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
213 llvm_unreachable("zstd::decompress is unavailable");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
214 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
215 Error zstd::decompress(ArrayRef<uint8_t> Input,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
216 SmallVectorImpl<uint8_t> &Output,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
217 size_t UncompressedSize) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
218 llvm_unreachable("zstd::decompress is unavailable");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
219 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
220 #endif