Skip to content

Commit e3f3466

Browse files
committed
add an unmodified copy of llvm Compression code
This works around clang ABI bugs on win32 with Error sret return.
1 parent a90046f commit e3f3466

File tree

4 files changed

+385
-2
lines changed

4 files changed

+385
-2
lines changed

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ CG_DEBUG_LIBS := $(COMMON_LIBPATHS) $(CG_LIBS) -ljulia-debug -ljulia-internal-de
210210
RT_RELEASE_LIBS := $(COMMON_LIBPATHS) $(WHOLE_ARCHIVE) $(BUILDDIR)/flisp/libflisp.a $(WHOLE_ARCHIVE) $(BUILDDIR)/support/libsupport.a -ljulia $(RT_LIBS)
211211
CG_RELEASE_LIBS := $(COMMON_LIBPATHS) $(CG_LIBS) -ljulia -ljulia-internal
212212

213-
OBJS := $(SRCS:%=$(BUILDDIR)/%.o)
214-
DOBJS := $(SRCS:%=$(BUILDDIR)/%.dbg.obj)
213+
OBJS := $(SRCS:%=$(BUILDDIR)/%.o) $(BUILDDIR)/llvm-Compression.o
214+
DOBJS := $(SRCS:%=$(BUILDDIR)/%.dbg.obj) $(BUILDDIR)/llvm-Compression.dbg.obj
215215

216216
CODEGEN_OBJS := $(CODEGEN_SRCS:%=$(BUILDDIR)/%.o)
217217
CODEGEN_DOBJS := $(CODEGEN_SRCS:%=$(BUILDDIR)/%.dbg.obj)

src/debuginfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <llvm/Object/MachO.h>
2121
#include <llvm/Object/COFF.h>
2222
#include <llvm/Object/ELFObjectFile.h>
23+
#include <llvm-Compression.h>
24+
#include <llvm/Support/Compression.h>
2325

2426
#ifdef _OS_DARWIN_
2527
#include <CoreFoundation/CoreFoundation.h>

src/llvm-Compression.cpp

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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-Compression.h"
14+
#include "llvm/Support/Compression.h"
15+
#include "llvm/ADT/SmallVector.h"
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Config/llvm-config.h"
18+
#include "llvm/Support/Compiler.h"
19+
#include "llvm/Support/Error.h"
20+
#include "llvm/Support/ErrorHandling.h"
21+
#if LLVM_ENABLE_ZLIB
22+
#include <zlib.h>
23+
#endif
24+
#if LLVM_ENABLE_ZSTD
25+
#include <zstd.h>
26+
#endif
27+
28+
using namespace llvm;
29+
using namespace llvm::compression;
30+
31+
const char *compression::getReasonIfUnsupported(compression::Format F) {
32+
switch (F) {
33+
case compression::Format::Zlib:
34+
if (zlib::isAvailable())
35+
return nullptr;
36+
return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
37+
"build time";
38+
case compression::Format::Zstd:
39+
if (zstd::isAvailable())
40+
return nullptr;
41+
return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
42+
"build time";
43+
}
44+
llvm_unreachable("");
45+
}
46+
47+
void compression::compress(Params P, ArrayRef<uint8_t> Input,
48+
SmallVectorImpl<uint8_t> &Output) {
49+
switch (P.format) {
50+
case compression::Format::Zlib:
51+
zlib::compress(Input, Output, P.level);
52+
break;
53+
case compression::Format::Zstd:
54+
zstd::compress(Input, Output, P.level, P.zstdEnableLdm);
55+
break;
56+
}
57+
}
58+
59+
Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
60+
uint8_t *Output, size_t UncompressedSize) {
61+
switch (formatFor(T)) {
62+
case compression::Format::Zlib:
63+
return zlib::decompress(Input, Output, UncompressedSize);
64+
case compression::Format::Zstd:
65+
return zstd::decompress(Input, Output, UncompressedSize);
66+
}
67+
llvm_unreachable("");
68+
}
69+
70+
Error compression::decompress(compression::Format F, ArrayRef<uint8_t> Input,
71+
SmallVectorImpl<uint8_t> &Output,
72+
size_t UncompressedSize) {
73+
switch (F) {
74+
case compression::Format::Zlib:
75+
return zlib::decompress(Input, Output, UncompressedSize);
76+
case compression::Format::Zstd:
77+
return zstd::decompress(Input, Output, UncompressedSize);
78+
}
79+
llvm_unreachable("");
80+
}
81+
82+
Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
83+
SmallVectorImpl<uint8_t> &Output,
84+
size_t UncompressedSize) {
85+
return decompress(formatFor(T), Input, Output, UncompressedSize);
86+
}
87+
88+
#if LLVM_ENABLE_ZLIB
89+
90+
static StringRef convertZlibCodeToString(int Code) {
91+
switch (Code) {
92+
case Z_MEM_ERROR:
93+
return "zlib error: Z_MEM_ERROR";
94+
case Z_BUF_ERROR:
95+
return "zlib error: Z_BUF_ERROR";
96+
case Z_STREAM_ERROR:
97+
return "zlib error: Z_STREAM_ERROR";
98+
case Z_DATA_ERROR:
99+
return "zlib error: Z_DATA_ERROR";
100+
case Z_OK:
101+
default:
102+
llvm_unreachable("unknown or unexpected zlib status code");
103+
}
104+
}
105+
106+
bool zlib::isAvailable() { return true; }
107+
108+
void zlib::compress(ArrayRef<uint8_t> Input,
109+
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
110+
unsigned long CompressedSize = ::compressBound(Input.size());
111+
CompressedBuffer.resize_for_overwrite(CompressedSize);
112+
int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
113+
(const Bytef *)Input.data(), Input.size(), Level);
114+
if (Res == Z_MEM_ERROR)
115+
report_bad_alloc_error("Allocation failed");
116+
assert(Res == Z_OK);
117+
// Tell MemorySanitizer that zlib output buffer is fully initialized.
118+
// This avoids a false report when running LLVM with uninstrumented ZLib.
119+
__msan_unpoison(CompressedBuffer.data(), CompressedSize);
120+
if (CompressedSize < CompressedBuffer.size())
121+
CompressedBuffer.truncate(CompressedSize);
122+
}
123+
124+
Error zlib::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
125+
size_t &UncompressedSize) {
126+
int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
127+
(const Bytef *)Input.data(), Input.size());
128+
// Tell MemorySanitizer that zlib output buffer is fully initialized.
129+
// This avoids a false report when running LLVM with uninstrumented ZLib.
130+
__msan_unpoison(Output, UncompressedSize);
131+
return Res ? make_error<StringError>(convertZlibCodeToString(Res),
132+
inconvertibleErrorCode())
133+
: Error::success();
134+
}
135+
136+
Error zlib::decompress(ArrayRef<uint8_t> Input,
137+
SmallVectorImpl<uint8_t> &Output,
138+
size_t UncompressedSize) {
139+
Output.resize_for_overwrite(UncompressedSize);
140+
Error E = zlib::decompress(Input, Output.data(), UncompressedSize);
141+
if (UncompressedSize < Output.size())
142+
Output.truncate(UncompressedSize);
143+
return E;
144+
}
145+
146+
#else
147+
bool zlib::isAvailable() { return false; }
148+
void zlib::compress(ArrayRef<uint8_t> Input,
149+
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
150+
llvm_unreachable("zlib::compress is unavailable");
151+
}
152+
Error zlib::decompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
153+
size_t &UncompressedSize) {
154+
llvm_unreachable("zlib::decompress is unavailable");
155+
}
156+
Error zlib::decompress(ArrayRef<uint8_t> Input,
157+
SmallVectorImpl<uint8_t> &UncompressedBuffer,
158+
size_t UncompressedSize) {
159+
llvm_unreachable("zlib::decompress is unavailable");
160+
}
161+
#endif
162+
163+
#if LLVM_ENABLE_ZSTD
164+
165+
bool zstd::isAvailable() { return true; }
166+
167+
#include <zstd.h> // Ensure ZSTD library is included
168+
169+
void zstd::compress(ArrayRef<uint8_t> Input,
170+
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level,
171+
bool EnableLdm) {
172+
ZSTD_CCtx *Cctx = ZSTD_createCCtx();
173+
if (!Cctx)
174+
report_bad_alloc_error("Failed to create ZSTD_CCtx");
175+
176+
if (ZSTD_isError(ZSTD_CCtx_setParameter(
177+
Cctx, ZSTD_c_enableLongDistanceMatching, EnableLdm ? 1 : 0))) {
178+
ZSTD_freeCCtx(Cctx);
179+
report_bad_alloc_error("Failed to set ZSTD_c_enableLongDistanceMatching");
180+
}
181+
182+
if (ZSTD_isError(
183+
ZSTD_CCtx_setParameter(Cctx, ZSTD_c_compressionLevel, Level))) {
184+
ZSTD_freeCCtx(Cctx);
185+
report_bad_alloc_error("Failed to set ZSTD_c_compressionLevel");
186+
}
187+
188+
unsigned long CompressedBufferSize = ZSTD_compressBound(Input.size());
189+
CompressedBuffer.resize_for_overwrite(CompressedBufferSize);
190+
191+
size_t const CompressedSize =
192+
ZSTD_compress2(Cctx, CompressedBuffer.data(), CompressedBufferSize,
193+
Input.data(), Input.size());
194+
195+
ZSTD_freeCCtx(Cctx);
196+
197+
if (ZSTD_isError(CompressedSize))
198+
report_bad_alloc_error("Compression failed");
199+
200+
__msan_unpoison(CompressedBuffer.data(), CompressedSize);
201+
if (CompressedSize < CompressedBuffer.size())
202+
CompressedBuffer.truncate(CompressedSize);
203+
}
204+
205+
Error zstd::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
206+
size_t &UncompressedSize) {
207+
const size_t Res = ::ZSTD_decompress(
208+
Output, UncompressedSize, (const uint8_t *)Input.data(), Input.size());
209+
UncompressedSize = Res;
210+
if (ZSTD_isError(Res))
211+
return make_error<StringError>(ZSTD_getErrorName(Res),
212+
inconvertibleErrorCode());
213+
// Tell MemorySanitizer that zstd output buffer is fully initialized.
214+
// This avoids a false report when running LLVM with uninstrumented ZLib.
215+
__msan_unpoison(Output, UncompressedSize);
216+
return Error::success();
217+
}
218+
219+
Error zstd::decompress(ArrayRef<uint8_t> Input,
220+
SmallVectorImpl<uint8_t> &Output,
221+
size_t UncompressedSize) {
222+
Output.resize_for_overwrite(UncompressedSize);
223+
Error E = zstd::decompress(Input, Output.data(), UncompressedSize);
224+
if (UncompressedSize < Output.size())
225+
Output.truncate(UncompressedSize);
226+
return E;
227+
}
228+
229+
#else
230+
bool zstd::isAvailable() { return false; }
231+
void zstd::compress(ArrayRef<uint8_t> Input,
232+
SmallVectorImpl<uint8_t> &CompressedBuffer, int Level,
233+
bool EnableLdm) {
234+
llvm_unreachable("zstd::compress is unavailable");
235+
}
236+
Error zstd::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
237+
size_t &UncompressedSize) {
238+
llvm_unreachable("zstd::decompress is unavailable");
239+
}
240+
Error zstd::decompress(ArrayRef<uint8_t> Input,
241+
SmallVectorImpl<uint8_t> &Output,
242+
size_t UncompressedSize) {
243+
llvm_unreachable("zstd::decompress is unavailable");
244+
}
245+
#endif

0 commit comments

Comments
 (0)