1 //===-- llvm/Support/Compression.h ---Compression----------------*- C++ -*-===//
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 contains basic functions for compression/decompression.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_COMPRESSION_H
14 #define LLVM_SUPPORT_COMPRESSION_H
15
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataTypes.h"
19
20 namespace llvm {
21 template <typename T> class SmallVectorImpl;
22 class Error;
23
24 // None indicates no compression. The other members are a subset of
25 // compression::Format, which is used for compressed debug sections in some
26 // object file formats (e.g. ELF). This is a separate class as we may add new
27 // compression::Format members for non-debugging purposes.
28 enum class DebugCompressionType {
29 None, ///< No compression
30 Zlib, ///< zlib
31 Zstd, ///< Zstandard
32 };
33
34 namespace compression {
35 namespace zlib {
36
37 constexpr int NoCompression = 0;
38 constexpr int BestSpeedCompression = 1;
39 constexpr int DefaultCompression = 6;
40 constexpr int BestSizeCompression = 9;
41
42 LLVM_ABI bool isAvailable();
43
44 LLVM_ABI void compress(ArrayRef<uint8_t> Input,
45 SmallVectorImpl<uint8_t> &CompressedBuffer,
46 int Level = DefaultCompression);
47
48 LLVM_ABI Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
49 size_t &UncompressedSize);
50
51 LLVM_ABI Error decompress(ArrayRef<uint8_t> Input,
52 SmallVectorImpl<uint8_t> &Output,
53 size_t UncompressedSize);
54
55 } // End of namespace zlib
56
57 namespace zstd {
58
59 constexpr int NoCompression = -5;
60 constexpr int BestSpeedCompression = 1;
61 constexpr int DefaultCompression = 5;
62 constexpr int BestSizeCompression = 12;
63
64 LLVM_ABI bool isAvailable();
65
66 LLVM_ABI void compress(ArrayRef<uint8_t> Input,
67 SmallVectorImpl<uint8_t> &CompressedBuffer,
68 int Level = DefaultCompression, bool EnableLdm = false);
69
70 LLVM_ABI Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
71 size_t &UncompressedSize);
72
73 LLVM_ABI Error decompress(ArrayRef<uint8_t> Input,
74 SmallVectorImpl<uint8_t> &Output,
75 size_t UncompressedSize);
76
77 } // End of namespace zstd
78
79 enum class Format {
80 Zlib,
81 Zstd,
82 };
83
formatFor(DebugCompressionType Type)84 inline Format formatFor(DebugCompressionType Type) {
85 switch (Type) {
86 case DebugCompressionType::None:
87 llvm_unreachable("not a compression type");
88 case DebugCompressionType::Zlib:
89 return Format::Zlib;
90 case DebugCompressionType::Zstd:
91 return Format::Zstd;
92 }
93 llvm_unreachable("");
94 }
95
96 struct Params {
ParamsParams97 constexpr Params(Format F)
98 : format(F), level(F == Format::Zlib ? zlib::DefaultCompression
99 : zstd::DefaultCompression) {}
100 constexpr Params(Format F, int L, bool Ldm = false)
formatParams101 : format(F), level(L), zstdEnableLdm(Ldm) {}
ParamsParams102 Params(DebugCompressionType Type) : Params(formatFor(Type)) {}
103
104 Format format;
105 int level;
106 bool zstdEnableLdm = false; // Enable zstd long distance matching
107 // This may support multi-threading for zstd in the future. Note that
108 // different threads may produce different output, so be careful if certain
109 // output determinism is desired.
110 };
111
112 // Return nullptr if LLVM was built with support (LLVM_ENABLE_ZLIB,
113 // LLVM_ENABLE_ZSTD) for the specified compression format; otherwise
114 // return a string literal describing the reason.
115 LLVM_ABI const char *getReasonIfUnsupported(Format F);
116
117 // Compress Input with the specified format P.Format. If Level is -1, use
118 // *::DefaultCompression for the format.
119 LLVM_ABI void compress(Params P, ArrayRef<uint8_t> Input,
120 SmallVectorImpl<uint8_t> &Output);
121
122 // Decompress Input. The uncompressed size must be available.
123 LLVM_ABI Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
124 uint8_t *Output, size_t UncompressedSize);
125 LLVM_ABI Error decompress(Format F, ArrayRef<uint8_t> Input,
126 SmallVectorImpl<uint8_t> &Output,
127 size_t UncompressedSize);
128 LLVM_ABI Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
129 SmallVectorImpl<uint8_t> &Output,
130 size_t UncompressedSize);
131
132 } // End of namespace compression
133
134 } // End of namespace llvm
135
136 #endif
137