1 | //===------ offload2yaml.cpp - obj2yaml conversion tool ---*- 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 | #include "obj2yaml.h" |
10 | #include "llvm/BinaryFormat/Magic.h" |
11 | #include "llvm/Object/OffloadBinary.h" |
12 | #include "llvm/ObjectYAML/OffloadYAML.h" |
13 | #include "llvm/Support/StringSaver.h" |
14 | |
15 | using namespace llvm; |
16 | |
17 | namespace { |
18 | |
19 | void populateYAML(OffloadYAML::Binary &YAMLBinary, object::OffloadBinary &OB, |
20 | UniqueStringSaver Saver) { |
21 | YAMLBinary.Members.emplace_back(); |
22 | auto &Member = YAMLBinary.Members.back(); |
23 | Member.ImageKind = OB.getImageKind(); |
24 | Member.OffloadKind = OB.getOffloadKind(); |
25 | Member.Flags = OB.getFlags(); |
26 | if (!OB.strings().empty()) { |
27 | Member.StringEntries = std::vector<OffloadYAML::Binary::StringEntry>(); |
28 | for (const auto &Entry : OB.strings()) |
29 | Member.StringEntries->emplace_back(args: OffloadYAML::Binary::StringEntry( |
30 | {.Key: Saver.save(S: Entry.first), .Value: Saver.save(S: Entry.second)})); |
31 | } |
32 | |
33 | if (!OB.getImage().empty()) |
34 | Member.Content = arrayRefFromStringRef(Input: OB.getImage()); |
35 | } |
36 | |
37 | Expected<OffloadYAML::Binary *> dump(MemoryBufferRef Source, |
38 | UniqueStringSaver Saver) { |
39 | Expected<std::unique_ptr<object::OffloadBinary>> OB = |
40 | object::OffloadBinary::create(Source); |
41 | if (!OB) |
42 | return OB.takeError(); |
43 | |
44 | std::unique_ptr<OffloadYAML::Binary> YAMLBinary = |
45 | std::make_unique<OffloadYAML::Binary>(); |
46 | |
47 | YAMLBinary->Members = std::vector<OffloadYAML::Binary::Member>(); |
48 | |
49 | uint64_t Offset = 0; |
50 | while (Offset < (*OB)->getMemoryBufferRef().getBufferSize()) { |
51 | MemoryBufferRef Buffer = MemoryBufferRef( |
52 | (*OB)->getData().drop_front(N: Offset), (*OB)->getFileName()); |
53 | auto BinaryOrErr = object::OffloadBinary::create(Buffer); |
54 | if (!BinaryOrErr) |
55 | return BinaryOrErr.takeError(); |
56 | |
57 | object::OffloadBinary &Binary = **BinaryOrErr; |
58 | |
59 | populateYAML(YAMLBinary&: *YAMLBinary, OB&: Binary, Saver); |
60 | |
61 | Offset += Binary.getSize(); |
62 | } |
63 | |
64 | return YAMLBinary.release(); |
65 | } |
66 | |
67 | } // namespace |
68 | |
69 | Error offload2yaml(raw_ostream &Out, MemoryBufferRef Source) { |
70 | BumpPtrAllocator Alloc; |
71 | UniqueStringSaver Saver(Alloc); |
72 | |
73 | Expected<OffloadYAML::Binary *> YAMLOrErr = dump(Source, Saver); |
74 | if (!YAMLOrErr) |
75 | return YAMLOrErr.takeError(); |
76 | |
77 | std::unique_ptr<OffloadYAML::Binary> YAML(YAMLOrErr.get()); |
78 | yaml::Output Yout(Out); |
79 | Yout << *YAML; |
80 | |
81 | return Error::success(); |
82 | } |
83 | |