@@ -31,42 +31,56 @@ namespace llvm {
31
31
32
32
namespace object {
33
33
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).
34
46
class CompressedOffloadBundle {
35
47
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;
48
48
static inline const llvm::StringRef MagicNumber = " CCOB" ;
49
- static inline const uint16_t Version = 2 ;
50
49
51
50
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
+
52
63
static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
53
64
compress (llvm::compression::Params P, const llvm::MemoryBuffer &Input,
54
- bool Verbose = false );
65
+ uint16_t Version, bool Verbose = false );
55
66
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 );
57
68
};
58
69
59
70
// / Bundle entry in binary clang-offload-bundler format.
60
71
struct OffloadBundleEntry {
61
72
uint64_t Offset = 0u ;
62
73
uint64_t Size = 0u ;
63
74
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
+ }
67
81
void dumpInfo (raw_ostream &OS) {
68
82
OS << " Offset = " << Offset << " , Size = " << Size
69
- << " , ID Length = " << IDLength << " , ID = " << ID;
83
+ << " , ID Length = " << IDLength << " , ID = " << ID << " \n " ;
70
84
}
71
85
void dumpURI (raw_ostream &OS, StringRef FilePath) {
72
86
OS << ID.data () << " \t file://" << FilePath << " #offset=" << Offset
@@ -81,16 +95,21 @@ class OffloadBundleFatBin {
81
95
StringRef FileName;
82
96
uint64_t NumberOfEntries;
83
97
SmallVector<OffloadBundleEntry> Entries;
98
+ bool Decompressed;
84
99
85
100
public:
101
+ std::unique_ptr<MemoryBuffer> DecompressedBuffer;
102
+
86
103
SmallVector<OffloadBundleEntry> getEntries () { return Entries; }
87
104
uint64_t getSize () const { return Size; }
88
105
StringRef getFileName () const { return FileName; }
89
106
uint64_t getNumEntries () const { return NumberOfEntries; }
107
+ bool isDecompressed () const { return Decompressed; }
90
108
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);
94
113
95
114
Error dumpEntryToCodeObject ();
96
115
@@ -105,9 +124,15 @@ class OffloadBundleFatBin {
105
124
Entry.dumpURI (outs (), FileName);
106
125
}
107
126
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
+ }
111
136
};
112
137
113
138
enum UriTypeT { FILE_URI, MEMORY_URI };
@@ -190,6 +215,10 @@ Error extractOffloadBundleFatBinary(
190
215
Error extractCodeObject (const ObjectFile &Source, int64_t Offset, int64_t Size,
191
216
StringRef OutputFileName);
192
217
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);
193
222
// / Extracts an Offload Bundle Entry given by URI
194
223
Error extractOffloadBundleByURI (StringRef URIstr);
195
224
0 commit comments