LLVM 24.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
13#include "llvm/Support/Error.h"
15
16using namespace llvm;
17using namespace OffloadYAML;
18
19namespace llvm {
20namespace yaml {
21
24 for (const auto &Member : Doc.Members) {
26 if (Member.ImageKind)
27 Image.TheImageKind = *Member.ImageKind;
28 if (Member.OffloadKind)
29 Image.TheOffloadKind = *Member.OffloadKind;
30 if (Member.Flags)
31 Image.Flags = *Member.Flags;
32
33 if (Member.StringEntries)
34 for (const auto &Entry : *Member.StringEntries)
35 Image.StringData[Entry.Key] = Entry.Value;
36
39 if (Member.Content)
40 Member.Content->writeAsBinary(OS);
41 Image.Image = MemoryBuffer::getMemBufferCopy(OS.str());
42 Images.push_back(std::move(Image));
43 }
44
45 // Copy the data to a new buffer so we can modify the bytes directly.
46 SmallString<0> Buffer;
47 if (Doc.Compression) {
49 Images, compression::Params(*Doc.Compression));
50 if (!CompressedOrErr) {
51 EH(toString(CompressedOrErr.takeError()));
52 return false;
53 }
54 Buffer = std::move(*CompressedOrErr);
55 } else {
56 Buffer = object::OffloadBinary::write(Images);
57 }
58 auto *TheHeader =
59 reinterpret_cast<object::OffloadBinary::Header *>(&Buffer[0]);
60 if (Doc.Version)
61 TheHeader->Version = *Doc.Version;
62 if (Doc.Size)
63 TheHeader->Size = *Doc.Size;
64 if (Doc.EntriesOffset)
65 TheHeader->EntriesOffset = *Doc.EntriesOffset;
66 if (Doc.EntriesCount)
67 TheHeader->EntriesCount = *Doc.EntriesCount;
68
69 Out.write(Buffer.begin(), Buffer.size());
70
71 return true;
72}
73
74} // namespace yaml
75} // namespace llvm
This file declares classes for handling the YAML representation of offloading binaries.
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
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.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI SmallString< 0 > write(ArrayRef< OffloadingImage > OffloadingData)
Serialize the contents of OffloadingData 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:53
A raw_ostream that writes to an SmallVector or SmallString.
StringRef str() const
Return a StringRef for the vector contents.
llvm::function_ref< void(const Twine &Msg)> ErrorHandler
Definition yaml2obj.h:68
LLVM_ABI bool yaml2offload(OffloadYAML::Binary &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
std::optional< compression::Format > Compression
Definition OffloadYAML.h:44
std::optional< uint64_t > EntriesOffset
Definition OffloadYAML.h:42
std::optional< uint32_t > Version
Definition OffloadYAML.h:40
std::optional< uint64_t > Size
Definition OffloadYAML.h:41
std::vector< Member > Members
Definition OffloadYAML.h:45
std::optional< uint64_t > EntriesCount
Definition OffloadYAML.h:43
The offloading metadata that will be serialized to a memory buffer.
Common declarations for yaml2obj.