LLVM 23.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).
72
73/// Bundle entry in binary clang-offload-bundler format.
78 std::string ID;
82 OS << "Offset = " << Offset << ", Size = " << Size
83 << ", ID Length = " << IDLength << ", ID = " << ID << "\n";
84 }
85 void dumpURI(raw_ostream &OS, StringRef FilePath) {
86 OS << ID.data() << "\tfile://" << FilePath << "#offset=" << Offset
87 << "&size=" << Size << "\n";
88 }
89};
90
91/// Fat binary embedded in object files in clang-offload-bundler format
93
94 uint64_t Size = 0u;
95 StringRef FileName;
96 uint64_t NumberOfEntries;
97 bool Decompressed;
99
100public:
101 std::unique_ptr<MemoryBuffer> DecompressedBuffer;
102
104 uint64_t getSize() const { return Size; }
105 StringRef getFileName() const { return FileName; }
106 uint64_t getNumEntries() const { return NumberOfEntries; }
107 bool isDecompressed() const { return Decompressed; }
108
110 create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName,
111 bool Decompress = false);
112 LLVM_ABI Error extractBundle(const ObjectFile &Source);
113
115
116 LLVM_ABI Error readEntries(StringRef Section, uint64_t SectionOffset);
117 void dumpEntries() {
118 for (OffloadBundleEntry &Entry : Entries)
119 Entry.dumpInfo(outs());
120 }
121
123 for (OffloadBundleEntry &Entry : Entries)
124 Entry.dumpURI(outs(), FileName);
125 }
126
128 bool Decompress = false)
129 : FileName(File), NumberOfEntries(0), Decompressed(Decompress),
130 Entries(SmallVector<OffloadBundleEntry>()) {
131 if (Decompress)
133 MemoryBuffer::getMemBufferCopy(Source.getBuffer(), File);
134 }
135};
136
138
140 int64_t Offset = 0;
141 int64_t Size = 0;
145
146 // Constructors
147 // TODO: add a Copy ctor ?
148 OffloadBundleURI(StringRef File, int64_t Off, int64_t Size)
149 : Offset(Off), Size(Size), ProcessID(0), FileName(File),
150 URIType(FILE_URI) {}
151
152public:
155 switch (Type) {
156 case FILE_URI:
157 return createFileURI(Str);
158 break;
159 case MEMORY_URI:
160 return createMemoryURI(Str);
161 break;
162 }
163 llvm_unreachable("Unknown UriTypeT enum");
164 }
165
168 int64_t O = 0;
169 int64_t S = 0;
170
171 if (!Str.consume_front("file://"))
173 "Reading type of URI");
174
175 StringRef FilePathname =
176 Str.take_until([](char C) { return (C == '#') || (C == '?'); });
177 Str = Str.drop_front(FilePathname.size());
178
179 if (!Str.consume_front("#offset="))
181 "Reading 'offset' in URI");
182
183 StringRef OffsetStr = Str.take_until([](char C) { return C == '&'; });
184 OffsetStr.getAsInteger(10, O);
185 Str = Str.drop_front(OffsetStr.size());
186
187 if (!Str.consume_front("&size="))
189 "Reading 'size' in URI");
190
191 Str.getAsInteger(10, S);
192 std::unique_ptr<OffloadBundleURI> OffloadingURI(
193 new OffloadBundleURI(FilePathname, O, S));
194 return std::move(OffloadingURI);
195 }
196
199 // TODO: add parseMemoryURI type
201 "Memory Type URI is not currently supported.");
202 }
203
204 StringRef getFileName() const { return FileName; }
205};
206
207/// Extracts fat binary in binary clang-offload-bundler format from object \p
208/// Obj and return it in \p Bundles
211
212/// Extract code object memory from the given \p Source object file at \p Offset
213/// and of \p Size, and copy into \p OutputFileName.
214LLVM_ABI Error extractCodeObject(const ObjectFile &Source, size_t Offset,
215 size_t Size, StringRef OutputFileName);
216
217/// Extract code object memory from the given \p Source object file at \p Offset
218/// and of \p Size, and copy into \p OutputFileName.
220 int64_t Size, StringRef OutputFileName);
221/// Extracts an Offload Bundle Entry given by URI
223
224} // namespace object
225
226} // namespace llvm
227#endif
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:57
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:490
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:629
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
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:623
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > decompress(const llvm::MemoryBuffer &Input, raw_ostream *VerboseStream=nullptr)
static LLVM_ABI 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 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.
LLVM_ABI Error extractCodeObject(const ObjectFile &Source, size_t Offset, size_t Size, StringRef OutputFileName)
Extract code object memory from the given Source object file at Offset and of Size,...
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
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:1321
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:334
static LLVM_ABI 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)