LLVM 22.0.0git
OffloadBundle.h
Go to the documentation of this file.
1//===- OffloadBundle.h - Utilities for offload bundles---*- 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 the binary format used for budingling device metadata with
10// an associated device image. The data can then be stored inside a host object
11// file to create a fat binary and read by the linker. This is intended to be a
12// thin wrapper around the image itself. If this format becomes sufficiently
13// complex it should be moved to a standard binary format like msgpack or ELF.
14//
15//===-------------------------------------------------------------------------===//
16
17#ifndef LLVM_OBJECT_OFFLOADBUNDLE_H
18#define LLVM_OBJECT_OFFLOADBUNDLE_H
19
20#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Object/Binary.h"
27#include "llvm/Support/Error.h"
29#include <memory>
30
31namespace llvm {
32
33namespace object {
34
35// CompressedOffloadBundle represents the format for the compressed offload
36// bundles.
37//
38// The format is as follows:
39// - Magic Number (4 bytes) - A constant "CCOB".
40// - Version (2 bytes)
41// - Compression Method (2 bytes) - Uses the values from
42// llvm::compression::Format.
43// - Total file size (4 bytes in V2, 8 bytes in V3).
44// - Uncompressed Size (4 bytes in V1/V2, 8 bytes in V3).
45// - Truncated MD5 Hash (8 bytes).
46// - Compressed Data (variable length).
71
72/// Bundle entry in binary clang-offload-bundler format.
77 std::string ID;
81 OS << "Offset = " << Offset << ", Size = " << Size
82 << ", ID Length = " << IDLength << ", ID = " << ID << "\n";
83 }
84 void dumpURI(raw_ostream &OS, StringRef FilePath) {
85 OS << ID.data() << "\tfile://" << FilePath << "#offset=" << Offset
86 << "&size=" << Size << "\n";
87 }
88};
89
90/// Fat binary embedded in object files in clang-offload-bundler format
92
93 uint64_t Size = 0u;
94 StringRef FileName;
95 uint64_t NumberOfEntries;
96 bool Decompressed;
98
99public:
100 std::unique_ptr<MemoryBuffer> DecompressedBuffer;
101
103 uint64_t getSize() const { return Size; }
104 StringRef getFileName() const { return FileName; }
105 uint64_t getNumEntries() const { return NumberOfEntries; }
106 bool isDecompressed() const { return Decompressed; }
107
109 create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName,
110 bool Decompress = false);
111 LLVM_ABI Error extractBundle(const ObjectFile &Source);
112
114
115 LLVM_ABI Error readEntries(StringRef Section, uint64_t SectionOffset);
116 void dumpEntries() {
117 for (OffloadBundleEntry &Entry : Entries)
118 Entry.dumpInfo(outs());
119 }
120
122 for (OffloadBundleEntry &Entry : Entries)
123 Entry.dumpURI(outs(), FileName);
124 }
125
127 bool Decompress = false)
128 : FileName(File), NumberOfEntries(0), Decompressed(Decompress),
129 Entries(SmallVector<OffloadBundleEntry>()) {
130 if (Decompress)
132 MemoryBuffer::getMemBufferCopy(Source.getBuffer(), File);
133 }
134};
135
137
139 int64_t Offset = 0;
140 int64_t Size = 0;
144
145 // Constructors
146 // TODO: add a Copy ctor ?
147 OffloadBundleURI(StringRef File, int64_t Off, int64_t Size)
148 : Offset(Off), Size(Size), ProcessID(0), FileName(File),
149 URIType(FILE_URI) {}
150
151public:
154 switch (Type) {
155 case FILE_URI:
156 return createFileURI(Str);
157 break;
158 case MEMORY_URI:
159 return createMemoryURI(Str);
160 break;
161 }
162 llvm_unreachable("Unknown UriTypeT enum");
163 }
164
167 int64_t O = 0;
168 int64_t S = 0;
169
170 if (!Str.consume_front("file://"))
172 "Reading type of URI");
173
174 StringRef FilePathname =
175 Str.take_until([](char C) { return (C == '#') || (C == '?'); });
176 Str = Str.drop_front(FilePathname.size());
177
178 if (!Str.consume_front("#offset="))
180 "Reading 'offset' in URI");
181
182 StringRef OffsetStr = Str.take_until([](char C) { return C == '&'; });
183 OffsetStr.getAsInteger(10, O);
184 Str = Str.drop_front(OffsetStr.size());
185
186 if (!Str.consume_front("&size="))
188 "Reading 'size' in URI");
189
190 Str.getAsInteger(10, S);
191 std::unique_ptr<OffloadBundleURI> OffloadingURI(
192 new OffloadBundleURI(FilePathname, O, S));
193 return std::move(OffloadingURI);
194 }
195
198 // TODO: add parseMemoryURI type
200 "Memory Type URI is not currently supported.");
201 }
202
203 StringRef getFileName() const { return FileName; }
204};
205
206/// Extracts fat binary in binary clang-offload-bundler format from object \p
207/// Obj and return it in \p Bundles
210
211/// Extract code object memory from the given \p Source object file at \p Offset
212/// and of \p Size, and copy into \p OutputFileName.
213LLVM_ABI Error extractCodeObject(const ObjectFile &Source, int64_t Offset,
214 int64_t Size, StringRef OutputFileName);
215
216/// Extract code object memory from the given \p Source object file at \p Offset
217/// and of \p Size, and copy into \p OutputFileName.
219 int64_t Size, StringRef OutputFileName);
220/// Extracts an Offload Bundle Entry given by URI
222
223} // namespace object
224
225} // namespace llvm
226#endif
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:58
This file implements a map that provides insertion order iteration.
#define T
#define P(N)
This file defines the SmallString class.
The Input class is used to parse a yaml document into in-memory structs and vectors.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:472
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:611
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
StringRef take_until(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that no character in the prefix satisfies the given predicat...
Definition StringRef.h:605
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > decompress(const llvm::MemoryBuffer &Input, raw_ostream *VerboseStream=nullptr)
static llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > compress(llvm::compression::Params P, const llvm::MemoryBuffer &Input, uint16_t Version, raw_ostream *VerboseStream=nullptr)
This class is the base class for all object file types.
Definition ObjectFile.h:231
std::unique_ptr< MemoryBuffer > DecompressedBuffer
LLVM_ABI Error dumpEntryToCodeObject()
LLVM_ABI Error readEntries(StringRef Section, uint64_t SectionOffset)
OffloadBundleFatBin(MemoryBufferRef Source, StringRef File, bool Decompress=false)
SmallVector< OffloadBundleEntry > getEntries()
LLVM_ABI Error extractBundle(const ObjectFile &Source)
static LLVM_ABI Expected< std::unique_ptr< OffloadBundleFatBin > > create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName, bool Decompress=false)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI Error extractCodeObject(const ObjectFile &Source, int64_t Offset, int64_t Size, StringRef OutputFileName)
Extract code object memory from the given Source object file at Offset and of Size,...
LLVM_ABI Error extractOffloadBundleByURI(StringRef URIstr)
Extracts an Offload Bundle Entry given by URI.
LLVM_ABI Error extractOffloadBundleFatBinary(const ObjectFile &Obj, SmallVectorImpl< OffloadBundleFatBin > &Bundles)
Extracts fat binary in binary clang-offload-bundler format from object Obj and return it in Bundles.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
static llvm::Expected< CompressedBundleHeader > tryParse(llvm::StringRef)
Bundle entry in binary clang-offload-bundler format.
void dumpInfo(raw_ostream &OS)
OffloadBundleEntry(uint64_t O, uint64_t S, uint64_t I, StringRef T)
void dumpURI(raw_ostream &OS, StringRef FilePath)
static Expected< std::unique_ptr< OffloadBundleURI > > createOffloadBundleURI(StringRef Str, UriTypeT Type)
static Expected< std::unique_ptr< OffloadBundleURI > > createFileURI(StringRef Str)
static Expected< std::unique_ptr< OffloadBundleURI > > createMemoryURI(StringRef Str)
OffloadBundleURI(StringRef File, int64_t Off, int64_t Size)