aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/lib/ObjectYAML/OffloadEmitter.cpp
blob: 3ffbc4ff0e11edc9452814dddcbac892ec0fd81b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//===- OffloadEmitter.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Object/OffloadBinary.h"
#include "llvm/ObjectYAML/OffloadYAML.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;
using namespace OffloadYAML;

namespace llvm {
namespace yaml {

bool yaml2offload(Binary &Doc, raw_ostream &Out, ErrorHandler EH) {
  for (const auto &Member : Doc.Members) {
    object::OffloadBinary::OffloadingImage Image{};
    if (Member.ImageKind)
      Image.TheImageKind = *Member.ImageKind;
    if (Member.OffloadKind)
      Image.TheOffloadKind = *Member.OffloadKind;
    if (Member.Flags)
      Image.Flags = *Member.Flags;

    StringMap<StringRef> &StringData = Image.StringData;
    if (Member.StringEntries) {
      for (const auto &Entry : *Member.StringEntries) {
        StringData[Entry.Key] = Entry.Value;
      }
    }

    SmallVector<char, 1024> Data;
    raw_svector_ostream OS(Data);
    if (Member.Content)
      Member.Content->writeAsBinary(OS);
    Image.Image = MemoryBuffer::getMemBufferCopy(OS.str());

    std::unique_ptr<MemoryBuffer> Binary = object::OffloadBinary::write(Image);

    // Copy the data to a new buffer so we can modify the bytes directly.
    SmallVector<char> NewBuffer;
    std::copy(Binary->getBufferStart(), Binary->getBufferEnd(),
              std::back_inserter(NewBuffer));
    auto *TheHeader =
        reinterpret_cast<object::OffloadBinary::Header *>(&NewBuffer[0]);
    if (Doc.Version)
      TheHeader->Version = *Doc.Version;
    if (Doc.Size)
      TheHeader->Size = *Doc.Size;
    if (Doc.EntryOffset)
      TheHeader->EntryOffset = *Doc.EntryOffset;
    if (Doc.EntrySize)
      TheHeader->EntrySize = *Doc.EntrySize;

    Out.write(NewBuffer.begin(), NewBuffer.size());
  }

  return true;
}

} // namespace yaml
} // namespace llvm