Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / tools / obj2yaml / offload2yaml.cpp
blob2b63e1278cd228e0bed30465ae42c8511c46168c
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 //===----------------------------------------------------------------------===//
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"
15 using namespace llvm;
17 namespace {
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);
41 if (!OB)
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>();
49 uint64_t Offset = 0;
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);
54 if (!BinaryOrErr)
55 return BinaryOrErr.takeError();
57 object::OffloadBinary &Binary = **BinaryOrErr;
59 populateYAML(*YAMLBinary, Binary, Saver);
61 Offset += Binary.getSize();
64 return YAMLBinary.release();
67 } // namespace
69 Error offload2yaml(raw_ostream &Out, MemoryBufferRef Source) {
70 BumpPtrAllocator Alloc;
71 UniqueStringSaver Saver(Alloc);
73 Expected<OffloadYAML::Binary *> YAMLOrErr = dump(Source, Saver);
74 if (!YAMLOrErr)
75 return YAMLOrErr.takeError();
77 std::unique_ptr<OffloadYAML::Binary> YAML(YAMLOrErr.get());
78 yaml::Output Yout(Out);
79 Yout << *YAML;
81 return Error::success();