LLVM 17.0.0git
OffloadEmitter.cpp
Go to the documentation of this file.
1//===- OffloadEmitter.cpp -------------------------------------------------===//
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
12#include "llvm/Support/Error.h"
14
15using namespace llvm;
16using namespace OffloadYAML;
17
18namespace llvm {
19namespace yaml {
20
22 for (const auto &Member : Doc.Members) {
24 if (Member.ImageKind)
25 Image.TheImageKind = *Member.ImageKind;
26 if (Member.OffloadKind)
27 Image.TheOffloadKind = *Member.OffloadKind;
28 if (Member.Flags)
29 Image.Flags = *Member.Flags;
30
31 if (Member.StringEntries)
32 for (const auto &Entry : *Member.StringEntries)
33 Image.StringData[Entry.Key] = Entry.Value;
34
37 if (Member.Content)
38 Member.Content->writeAsBinary(OS);
39 Image.Image = MemoryBuffer::getMemBufferCopy(OS.str());
40
41 std::unique_ptr<MemoryBuffer> Binary = object::OffloadBinary::write(Image);
42
43 // Copy the data to a new buffer so we can modify the bytes directly.
44 SmallVector<char> NewBuffer;
45 std::copy(Binary->getBufferStart(), Binary->getBufferEnd(),
46 std::back_inserter(NewBuffer));
47 auto *TheHeader =
48 reinterpret_cast<object::OffloadBinary::Header *>(&NewBuffer[0]);
49 if (Doc.Version)
50 TheHeader->Version = *Doc.Version;
51 if (Doc.Size)
52 TheHeader->Size = *Doc.Size;
53 if (Doc.EntryOffset)
54 TheHeader->EntryOffset = *Doc.EntryOffset;
55 if (Doc.EntrySize)
56 TheHeader->EntrySize = *Doc.EntrySize;
57
58 Out.write(NewBuffer.begin(), NewBuffer.size());
59 }
60
61 return true;
62}
63
64} // namespace yaml
65} // namespace llvm
This file declares classes for handling the YAML representation of offloading binaries.
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.
size_t size() const
Definition: SmallVector.h:91
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
An efficient, type-erasing, non-owning reference to a callable.
static std::unique_ptr< MemoryBuffer > write(const OffloadingImage &)
Serialize the contents of File to a binary buffer to be read later.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:697
bool yaml2offload(OffloadYAML::Binary &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< uint32_t > Version
Definition: OffloadYAML.h:41
std::optional< uint64_t > EntryOffset
Definition: OffloadYAML.h:43
std::optional< uint64_t > Size
Definition: OffloadYAML.h:42
std::optional< uint64_t > EntrySize
Definition: OffloadYAML.h:44
std::vector< Member > Members
Definition: OffloadYAML.h:45
The offloading metadata that will be serialized to a memory buffer.
Definition: OffloadBinary.h:69
Common declarations for yaml2obj.