35 SharedMemoryBuffer(std::shared_ptr<MemoryBuffer> Buf)
36 : SharedBuf(std::
move(Buf)) {
37 init(SharedBuf->getBufferStart(), SharedBuf->getBufferEnd(),
41 BufferKind getBufferKind()
const override {
return MemoryBuffer_Malloc; }
43 StringRef getBufferIdentifier()
const override {
44 return SharedBuf->getBufferIdentifier();
48 const std::shared_ptr<MemoryBuffer> SharedBuf;
59 std::unique_ptr<MemoryBuffer> Buffer =
63 Buffer->getBufferStart()))
65 Buffer->getBufferIdentifier());
69 return HeaderOrErr.takeError();
74 Buffer->getBuffer().take_front(Header->Size),
79 return BinariesOrErr.takeError();
82 std::shared_ptr<MemoryBuffer> SharedBuffer =
83 std::shared_ptr<MemoryBuffer>(std::move(BufferCopy));
85 for (
auto &
Binary : *BinariesOrErr) {
86 std::unique_ptr<SharedMemoryBuffer> SharedBufferPtr =
87 std::make_unique<SharedMemoryBuffer>(SharedBuffer);
100 assert((
Obj.isELF() ||
Obj.isCOFF()) &&
"Invalid file type");
114 if (!NameOrErr->starts_with(
".llvm.offloading"))
123 if (
Error Err = extractOffloadFiles(Contents, Binaries))
139 "Failed to create module");
143 auto *MD = M->getNamedMetadata(
"llvm.embedded.objects");
147 for (
const MDNode *
Op : MD->operands()) {
148 if (
Op->getNumOperands() < 2)
152 if (!SectionID || SectionID->
getString() !=
".llvm.offloading")
165 if (
Error Err = extractOffloadFiles(Contents, Binaries))
176 for (
auto Child : Library.
children(Err)) {
177 auto ChildBufferOrErr = Child.getMemoryBufferRef();
178 if (!ChildBufferOrErr)
179 return ChildBufferOrErr.takeError();
180 std::unique_ptr<MemoryBuffer> ChildBuffer =
185 ChildBuffer->getBufferStart()))
187 ChildBufferOrErr->getBuffer(),
188 ChildBufferOrErr->getBufferIdentifier());
215 const Header *TheHeader =
reinterpret_cast<const Header *
>(Start);
220 TheHeader->Size <
sizeof(
Entry) || TheHeader->Size <
sizeof(
Header))
224 (TheHeader->Version == 1) ? 1 : TheHeader->EntriesCount;
226 if (TheHeader->EntriesOffset > TheHeader->Size - EntriesSize ||
227 EntriesSize > TheHeader->Size -
sizeof(
Header))
237 return HeaderOrErr.takeError();
238 const Header *TheHeader = *HeaderOrErr;
241 const Entry *Entries =
242 reinterpret_cast<const Entry *
>(&Start[TheHeader->EntriesOffset]);
244 auto validateEntry = [&](
const Entry *TheEntry) ->
Error {
247 TheEntry->StringOffset + TheEntry->NumStrings *
sizeof(
StringEntry) >
254 if (TheHeader->Version > 1 && Index.has_value()) {
255 if (*Index >= TheHeader->EntriesCount)
257 const Entry *TheEntry = &Entries[*Index];
258 if (
auto Err = validateEntry(TheEntry))
259 return std::move(Err);
261 Binaries.
emplace_back(
new OffloadBinary(Buf, TheHeader, TheEntry, *Index));
262 return std::move(Binaries);
265 uint64_t EntriesCount = TheHeader->Version == 1 ? 1 : TheHeader->EntriesCount;
267 const Entry *TheEntry = &Entries[
I];
268 if (
auto Err = validateEntry(TheEntry))
269 return std::move(Err);
271 Binaries.
emplace_back(
new OffloadBinary(Buf, TheHeader, TheEntry,
I));
274 return std::move(Binaries);
279 assert(EntriesCount > 0 &&
"At least one offloading image is required");
287 for (
auto &KeyAndValue : Img.StringData) {
288 StrTab.
add(KeyAndValue.first);
289 StrTab.
add(KeyAndValue.second);
291 TotalStringEntries += Img.StringData.size();
292 TotalImagesSize += Img.Image->getBufferSize();
298 uint64_t StrTabOffset =
sizeof(
Header) + EntriesSize + StringEntrySize;
309 TheHeader.EntriesOffset =
sizeof(
Header);
310 TheHeader.EntriesCount = EntriesCount;
313 Data.reserve(TheHeader.Size);
315 OS << StringRef(reinterpret_cast<char *>(&TheHeader),
sizeof(
Header));
321 uint64_t ImageOffset = BinaryDataSize;
326 TheEntry.TheOffloadKind = Img.TheOffloadKind;
327 TheEntry.Flags = Img.Flags;
329 TheEntry.StringOffset = StringEntryOffset;
330 StringEntryOffset +=
sizeof(
StringEntry) * Img.StringData.size();
331 TheEntry.NumStrings = Img.StringData.size();
333 TheEntry.ImageOffset = ImageOffset;
334 ImageOffset += Img.Image->getBufferSize();
335 TheEntry.ImageSize = Img.Image->getBufferSize();
337 OS << StringRef(reinterpret_cast<char *>(&TheEntry),
sizeof(
Entry));
342 for (
auto &KeyAndValue : Img.StringData) {
344 StrTabOffset + StrTab.
getOffset(KeyAndValue.second),
345 KeyAndValue.second.size()};
346 OS << StringRef(reinterpret_cast<char *>(&Map),
sizeof(
StringEntry));
355 OS << Img.Image->getBuffer();
358 assert(TheHeader.Size >= OS.
tell() &&
"Too much data written?");
360 assert(TheHeader.Size == OS.
tell() &&
"Size mismatch");
370 return extractFromBitcode(Buffer, Binaries);
379 return extractFromObject(*ObjFile->
get(), Binaries);
386 return extractFromArchive(*LibFile->
get(), Binaries);
389 return extractOffloadFiles(Buffer, Binaries);
457 if (LHS.first != RHS.first)
461 if (LHS.second ==
"generic" || RHS.second ==
"generic")
470 if (LHS.second.split(
":").first != RHS.second.split(
":").first)
474 if (LHS.second.contains(
"xnack+") && RHS.second.contains(
"xnack-"))
476 if (LHS.second.contains(
"xnack-") && RHS.second.contains(
"xnack+"))
478 if (LHS.second.contains(
"sramecc-") && RHS.second.contains(
"sramecc+"))
480 if (LHS.second.contains(
"sramecc+") && RHS.second.contains(
"sramecc-"))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This is an important class for using LLVM in a threaded context.
LLVM_ABI StringRef getString() const
size_t getBufferSize() const
StringRef getBufferIdentifier() const
const char * getBufferStart() const
StringRef getBuffer() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
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.
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
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.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Utility for building string tables with deduplicated suffixes.
LLVM_ABI size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
LLVM_ABI size_t add(CachedHashStringRef S, uint8_t Priority=0)
Add a string to the builder.
LLVM_ABI void write(raw_ostream &OS) const
LLVM_ABI void finalize()
Analyze the strings and build the final table.
Triple - Helper class for working with autoconf configuration names.
The instances of the Type class are immutable: once they are created, they are never changed.
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
This class is the base class for all object file types.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
static uint64_t getAlignment()
static LLVM_ABI Expected< SmallVector< std::unique_ptr< OffloadBinary > > > create(MemoryBufferRef Buf, std::optional< uint64_t > Index=std::nullopt)
Attempt to parse the offloading binary stored in Buf.
static LLVM_ABI SmallString< 0 > write(ArrayRef< OffloadingImage > OffloadingData)
Serialize the contents of OffloadingData to a binary buffer to be read later.
static LLVM_ABI Expected< const Header * > extractHeader(MemoryBufferRef Buf)
Attempt to extract and validate the header from the offloading binary in Buf.
static const uint32_t Version
The current version of the binary used for backwards compatibility.
std::pair< StringRef, StringRef > TargetID
This is a value type class that represents a single section in the list of sections in the object fil...
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
A raw_ostream that writes to an SmallVector or SmallString.
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract_or_null(Y &&MD)
Extract a Value from Metadata, if any, allowing null.
LLVM_ABI Error extractOffloadBinaries(MemoryBufferRef Buffer, SmallVectorImpl< OffloadFile > &Binaries)
Extracts embedded device offloading code from a memory Buffer to a list of Binaries.
LLVM_ABI ImageKind getImageKind(StringRef Name)
Convert a string Name to an image kind.
LLVM_ABI bool areTargetsCompatible(const OffloadFile::TargetID &LHS, const OffloadFile::TargetID &RHS)
If the target is AMD we check the target IDs for mutual compatibility.
OffloadKind
The producer of the associated offloading image.
LLVM_ABI OffloadKind getOffloadKind(StringRef Name)
Convert a string Name to an offload kind.
LLVM_ABI StringRef getImageKindName(ImageKind Name)
Convert an image kind to its string representation.
ImageKind
The type of contents the offloading image contains.
LLVM_ABI StringRef getOffloadKindName(OffloadKind Name)
Convert an offload kind to its string representation.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
LLVM_ABI std::unique_ptr< Module > getLazyIRModule(std::unique_ptr< MemoryBuffer > Buffer, SMDiagnostic &Err, LLVMContext &Context, bool ShouldLazyLoadMetadata=false)
If the given MemoryBuffer holds a bitcode image, return a Module for it which does lazy deserializati...
This struct is a compact representation of a valid (non-zero power of two) alignment.
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
@ elf_relocatable
ELF Relocatable object file.
@ archive
ar style archive file
@ elf_shared_object
ELF dynamically linked shared lib.
@ elf_executable
ELF Executable image.
@ offload_binary
LLVM offload object file.
@ coff_object
COFF object file.
The offloading metadata that will be serialized to a memory buffer.