Skip to content

Commit b26e5d7

Browse files
authored
SWDEV-522811 - fix compress/decompress in LLVM Offloading API (llvm#3275)
2 parents 82aed4e + 6ffb215 commit b26e5d7

File tree

3 files changed

+391
-200
lines changed

3 files changed

+391
-200
lines changed

llvm/include/llvm/Object/OffloadBundle.h

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,56 @@ namespace llvm {
3131

3232
namespace object {
3333

34+
// CompressedOffloadBundle represents the format for the compressed offload
35+
// bundles.
36+
//
37+
// The format is as follows:
38+
// - Magic Number (4 bytes) - A constant "CCOB".
39+
// - Version (2 bytes)
40+
// - Compression Method (2 bytes) - Uses the values from
41+
// llvm::compression::Format.
42+
// - Total file size (4 bytes in V2, 8 bytes in V3).
43+
// - Uncompressed Size (4 bytes in V1/V2, 8 bytes in V3).
44+
// - Truncated MD5 Hash (8 bytes).
45+
// - Compressed Data (variable length).
3446
class CompressedOffloadBundle {
3547
private:
36-
static inline const size_t MagicSize = 4;
37-
static inline const size_t VersionFieldSize = sizeof(uint16_t);
38-
static inline const size_t MethodFieldSize = sizeof(uint16_t);
39-
static inline const size_t FileSizeFieldSize = sizeof(uint32_t);
40-
static inline const size_t UncompressedSizeFieldSize = sizeof(uint32_t);
41-
static inline const size_t HashFieldSize = sizeof(uint64_t);
42-
static inline const size_t V1HeaderSize =
43-
MagicSize + VersionFieldSize + MethodFieldSize +
44-
UncompressedSizeFieldSize + HashFieldSize;
45-
static inline const size_t V2HeaderSize =
46-
MagicSize + VersionFieldSize + FileSizeFieldSize + MethodFieldSize +
47-
UncompressedSizeFieldSize + HashFieldSize;
4848
static inline const llvm::StringRef MagicNumber = "CCOB";
49-
static inline const uint16_t Version = 2;
5049

5150
public:
51+
struct CompressedBundleHeader {
52+
unsigned Version;
53+
llvm::compression::Format CompressionFormat;
54+
std::optional<size_t> FileSize;
55+
size_t UncompressedFileSize;
56+
uint64_t Hash;
57+
58+
static llvm::Expected<CompressedBundleHeader> tryParse(llvm::StringRef);
59+
};
60+
61+
static inline const uint16_t DefaultVersion = 2;
62+
5263
static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
5364
compress(llvm::compression::Params P, const llvm::MemoryBuffer &Input,
54-
bool Verbose = false);
65+
uint16_t Version, bool Verbose = false);
5566
static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
56-
decompress(llvm::MemoryBufferRef &Input, bool Verbose = false);
67+
decompress(const llvm::MemoryBuffer &Input, bool Verbose = false);
5768
};
5869

5970
/// Bundle entry in binary clang-offload-bundler format.
6071
struct OffloadBundleEntry {
6172
uint64_t Offset = 0u;
6273
uint64_t Size = 0u;
6374
uint64_t IDLength = 0u;
64-
StringRef ID;
65-
OffloadBundleEntry(uint64_t O, uint64_t S, uint64_t I, StringRef T)
66-
: Offset(O), Size(S), IDLength(I), ID(T) {}
75+
std::string ID;
76+
OffloadBundleEntry(uint64_t O, uint64_t S, uint64_t I, std::string T)
77+
: Offset(O), Size(S), IDLength(I) {
78+
ID.reserve(T.size());
79+
ID = T;
80+
}
6781
void dumpInfo(raw_ostream &OS) {
6882
OS << "Offset = " << Offset << ", Size = " << Size
69-
<< ", ID Length = " << IDLength << ", ID = " << ID;
83+
<< ", ID Length = " << IDLength << ", ID = " << ID << "\n";
7084
}
7185
void dumpURI(raw_ostream &OS, StringRef FilePath) {
7286
OS << ID.data() << "\tfile://" << FilePath << "#offset=" << Offset
@@ -81,16 +95,21 @@ class OffloadBundleFatBin {
8195
StringRef FileName;
8296
uint64_t NumberOfEntries;
8397
SmallVector<OffloadBundleEntry> Entries;
98+
bool Decompressed;
8499

85100
public:
101+
std::unique_ptr<MemoryBuffer> DecompressedBuffer;
102+
86103
SmallVector<OffloadBundleEntry> getEntries() { return Entries; }
87104
uint64_t getSize() const { return Size; }
88105
StringRef getFileName() const { return FileName; }
89106
uint64_t getNumEntries() const { return NumberOfEntries; }
107+
bool isDecompressed() const { return Decompressed; }
90108

91-
static Expected<std::unique_ptr<OffloadBundleFatBin>>
92-
create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName);
93-
Error extractBundle(const ObjectFile &Source);
109+
LLVM_ABI static Expected<std::unique_ptr<OffloadBundleFatBin>>
110+
create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName,
111+
bool Decompress = false);
112+
LLVM_ABI Error extractBundle(const ObjectFile &Source);
94113

95114
Error dumpEntryToCodeObject();
96115

@@ -105,9 +124,15 @@ class OffloadBundleFatBin {
105124
Entry.dumpURI(outs(), FileName);
106125
}
107126

108-
OffloadBundleFatBin(MemoryBufferRef Source, StringRef File)
109-
: FileName(File), NumberOfEntries(0),
110-
Entries(SmallVector<OffloadBundleEntry>()) {}
127+
OffloadBundleFatBin(MemoryBufferRef Source, StringRef File,
128+
bool Decompress = false)
129+
: FileName(File), Decompressed(Decompress), NumberOfEntries(0),
130+
Entries(SmallVector<OffloadBundleEntry>()) {
131+
if (Decompress) {
132+
DecompressedBuffer =
133+
MemoryBuffer::getMemBufferCopy(Source.getBuffer(), File);
134+
}
135+
}
111136
};
112137

113138
enum UriTypeT { FILE_URI, MEMORY_URI };
@@ -190,6 +215,10 @@ Error extractOffloadBundleFatBinary(
190215
Error extractCodeObject(const ObjectFile &Source, int64_t Offset, int64_t Size,
191216
StringRef OutputFileName);
192217

218+
/// Extract code object memory from the given \p Source object file at \p Offset
219+
/// and of \p Size, and copy into \p OutputFileName.
220+
LLVM_ABI Error extractCodeObject(MemoryBufferRef Buffer, int64_t Offset,
221+
int64_t Size, StringRef OutputFileName);
193222
/// Extracts an Offload Bundle Entry given by URI
194223
Error extractOffloadBundleByURI(StringRef URIstr);
195224

0 commit comments

Comments
 (0)