1 //===------ offload2yaml.cpp - obj2yaml conversion tool ---*- C++ -------*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "llvm/BinaryFormat/Magic.h"
11 #include "llvm/Object/OffloadBinary.h"
12 #include "llvm/ObjectYAML/OffloadYAML.h"
13 #include "llvm/Support/StringSaver.h"
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(OffloadYAML::Binary::StringEntry(
30 {Saver
.save(Entry
.first
), Saver
.save(Entry
.second
)}));
33 if (!OB
.getImage().empty())
34 Member
.Content
= arrayRefFromStringRef(OB
.getImage());
37 Expected
<OffloadYAML::Binary
*> dump(MemoryBufferRef Source
,
38 UniqueStringSaver Saver
) {
39 Expected
<std::unique_ptr
<object::OffloadBinary
>> OB
=
40 object::OffloadBinary::create(Source
);
42 return OB
.takeError();
44 std::unique_ptr
<OffloadYAML::Binary
> YAMLBinary
=
45 std::make_unique
<OffloadYAML::Binary
>();
47 YAMLBinary
->Members
= std::vector
<OffloadYAML::Binary::Member
>();
50 while (Offset
< (*OB
)->getMemoryBufferRef().getBufferSize()) {
51 MemoryBufferRef Buffer
= MemoryBufferRef(
52 (*OB
)->getData().drop_front(Offset
), (*OB
)->getFileName());
53 auto BinaryOrErr
= object::OffloadBinary::create(Buffer
);
55 return BinaryOrErr
.takeError();
57 object::OffloadBinary
&Binary
= **BinaryOrErr
;
59 populateYAML(*YAMLBinary
, Binary
, Saver
);
61 Offset
+= Binary
.getSize();
64 return YAMLBinary
.release();
69 Error
offload2yaml(raw_ostream
&Out
, MemoryBufferRef Source
) {
70 BumpPtrAllocator Alloc
;
71 UniqueStringSaver
Saver(Alloc
);
73 Expected
<OffloadYAML::Binary
*> YAMLOrErr
= dump(Source
, Saver
);
75 return YAMLOrErr
.takeError();
77 std::unique_ptr
<OffloadYAML::Binary
> YAML(YAMLOrErr
.get());
78 yaml::Output
Yout(Out
);
81 return Error::success();