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
36private:
37 static inline const size_t MagicSize = 4;
38 static inline const size_t VersionFieldSize = sizeof(uint16_t);
39 static inline const size_t MethodFieldSize = sizeof(uint16_t);
40 static inline const size_t FileSizeFieldSize = sizeof(uint32_t);
41 static inline const size_t UncompressedSizeFieldSize = sizeof(uint32_t);
42 static inline const size_t HashFieldSize = sizeof(uint64_t);
43 static inline const size_t V1HeaderSize =
44 MagicSize + VersionFieldSize + MethodFieldSize +
45 UncompressedSizeFieldSize + HashFieldSize;
46 static inline const size_t V2HeaderSize =
47 MagicSize + VersionFieldSize + FileSizeFieldSize + MethodFieldSize +
48 UncompressedSizeFieldSize + HashFieldSize;
49 static inline const llvm::StringRef MagicNumber = "CCOB";
50 static inline const uint16_t Version = 2;
51
52public:
55 bool Verbose = false);
57 decompress(llvm::MemoryBufferRef &Input, bool Verbose = false);
58};
59
60/// Bundle entry in binary clang-offload-bundler format.
67 : Offset(O), Size(S), IDLength(I), ID(T) {}
69 OS << "Offset = " << Offset << ", Size = " << Size
70 << ", ID Length = " << IDLength << ", ID = " << ID;
71 }
72 void dumpURI(raw_ostream &OS, StringRef FilePath) {
73 OS << ID.data() << "\tfile://" << FilePath << "#offset=" << Offset
74 << "&size=" << Size << "\n";
75 }
76};
77
78/// Fat binary embedded in object files in clang-offload-bundler format
80
81 uint64_t Size = 0u;
82 StringRef FileName;
83 uint64_t NumberOfEntries;
85
86public:
88 uint64_t getSize() const { return Size; }
89 StringRef getFileName() const { return FileName; }
90 uint64_t getNumEntries() const { return NumberOfEntries; }
91
93 create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName);
95
97
98 LLVM_ABI Error readEntries(StringRef Section, uint64_t SectionOffset);
99 void dumpEntries() {
100 for (OffloadBundleEntry &Entry : Entries)
101 Entry.dumpInfo(outs());
102 }
103
105 for (OffloadBundleEntry &Entry : Entries)
106 Entry.dumpURI(outs(), FileName);
107 }
108
110 : FileName(File), NumberOfEntries(0),
111 Entries(SmallVector<OffloadBundleEntry>()) {}
112};
113
115
117 int64_t Offset = 0;
118 int64_t Size = 0;
122
123 // Constructors
124 // TODO: add a Copy ctor ?
125 OffloadBundleURI(StringRef File, int64_t Off, int64_t Size)
126 : Offset(Off), Size(Size), ProcessID(0), FileName(File),
127 URIType(FILE_URI) {}
128
129public:
132 switch (Type) {
133 case FILE_URI:
134 return createFileURI(Str);
135 break;
136 case MEMORY_URI:
137 return createMemoryURI(Str);
138 break;
139 }
140 llvm_unreachable("Unknown UriTypeT enum");
141 }
142
145 int64_t O = 0;
146 int64_t S = 0;
147
148 if (!Str.consume_front("file://"))
150 "Reading type of URI");
151
152 StringRef FilePathname =
153 Str.take_until([](char C) { return (C == '#') || (C == '?'); });
154 Str = Str.drop_front(FilePathname.size());
155
156 if (!Str.consume_front("#offset="))
158 "Reading 'offset' in URI");
159
160 StringRef OffsetStr = Str.take_until([](char C) { return C == '&'; });
161 OffsetStr.getAsInteger(10, O);
162 Str = Str.drop_front(OffsetStr.size());
163
164 if (Str.consume_front("&size="))
166 "Reading 'size' in URI");
167
168 Str.getAsInteger(10, S);
169 std::unique_ptr<OffloadBundleURI> OffloadingURI(
170 new OffloadBundleURI(FilePathname, O, S));
171 return std::move(OffloadingURI);
172 }
173
176 // TODO: add parseMemoryURI type
178 "Memory Type URI is not currently supported.");
179 }
180
181 StringRef getFileName() const { return FileName; }
182};
183
184/// Extracts fat binary in binary clang-offload-bundler format from object \p
185/// Obj and return it in \p Bundles
188
189/// Extract code object memory from the given \p Source object file at \p Offset
190/// and of \p Size, and copy into \p OutputFileName.
191LLVM_ABI Error extractCodeObject(const ObjectFile &Source, int64_t Offset,
192 int64_t Size, StringRef OutputFileName);
193
194/// Extracts an Offload Bundle Entry given by URI
196
197} // namespace object
198
199} // namespace llvm
200#endif
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
#define P(N)
raw_pwrite_stream & OS
This file defines the SmallString class.
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...
Definition: MemoryBuffer.h:52
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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:480
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static LLVM_ABI llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > compress(llvm::compression::Params P, const llvm::MemoryBuffer &Input, bool Verbose=false)
static LLVM_ABI llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > decompress(llvm::MemoryBufferRef &Input, bool Verbose=false)
This class is the base class for all object file types.
Definition: ObjectFile.h:231
Fat binary embedded in object files in clang-offload-bundler format.
Definition: OffloadBundle.h:79
static LLVM_ABI Expected< std::unique_ptr< OffloadBundleFatBin > > create(MemoryBufferRef, uint64_t SectionOffset, StringRef FileName)
LLVM_ABI Error dumpEntryToCodeObject()
LLVM_ABI Error readEntries(StringRef Section, uint64_t SectionOffset)
SmallVector< OffloadBundleEntry > getEntries()
Definition: OffloadBundle.h:87
OffloadBundleFatBin(MemoryBufferRef Source, StringRef File)
LLVM_ABI Error extractBundle(const ObjectFile &Source)
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
MagicNumber
Definition: XCOFF.h:49
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.
Definition: AddressRanges.h:18
@ 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
Bundle entry in binary clang-offload-bundler format.
Definition: OffloadBundle.h:61
void dumpInfo(raw_ostream &OS)
Definition: OffloadBundle.h:68
OffloadBundleEntry(uint64_t O, uint64_t S, uint64_t I, StringRef T)
Definition: OffloadBundle.h:66
void dumpURI(raw_ostream &OS, StringRef FilePath)
Definition: OffloadBundle.h:72
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)