Skip to content

Commit 07f8f08

Browse files
david-salinasdsalinas_amdeng
andauthored
Add --offoading option to llvm-readobj (#143342)
Utilize new extensions to LLVM Offloading API to handle offloading fatbin Bundles. The tool will output a list of available offload bundles using URI syntax. --------- Co-authored-by: dsalinas_amdeng <david.salinas@amd.com>
1 parent 045e09f commit 07f8f08

File tree

10 files changed

+91
-10
lines changed

10 files changed

+91
-10
lines changed

llvm/docs/CommandGuide/llvm-readelf.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ OPTIONS
143143

144144
Display all notes.
145145

146+
.. option:: --offloading
147+
148+
Display list of HIP offload bundles.
149+
146150
.. option:: --pretty-print
147151

148152
When used with :option:`--elf-output-style`, JSON output will be formatted in

llvm/docs/CommandGuide/llvm-readobj.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ file formats.
104104
Do not demangle symbol names in the output. This option is only for ELF and
105105
XCOFF file formats. The option is enabled by default.
106106

107+
.. option:: --offloading
108+
109+
Display list of HIP offload bundles.
110+
107111
.. option:: --relocations, --relocs, -r
108112

109113
Display the relocation entries in the file.

llvm/include/llvm/Object/OffloadBundle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ struct OffloadBundleURI {
161161
OffsetStr.getAsInteger(10, O);
162162
Str = Str.drop_front(OffsetStr.size());
163163

164-
if (Str.consume_front("&size="))
164+
if (!Str.consume_front("&size="))
165165
return createStringError(object_error::parse_failed,
166166
"Reading 'size' in URI");
167167

llvm/lib/Object/OffloadBundle.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ Error OffloadBundleFatBin::readEntries(StringRef Buffer,
8989
uint64_t EntryIDSize;
9090
StringRef EntryID;
9191

92-
if (auto EC = Reader.readInteger(EntryOffset))
93-
return errorCodeToError(object_error::parse_failed);
92+
if (Error Err = Reader.readInteger(EntryOffset))
93+
return Err;
9494

95-
if (auto EC = Reader.readInteger(EntrySize))
96-
return errorCodeToError(object_error::parse_failed);
95+
if (Error Err = Reader.readInteger(EntrySize))
96+
return Err;
9797

98-
if (auto EC = Reader.readInteger(EntryIDSize))
99-
return errorCodeToError(object_error::parse_failed);
98+
if (Error Err = Reader.readInteger(EntryIDSize))
99+
return Err;
100100

101-
if (auto EC = Reader.readFixedString(EntryID, EntryIDSize))
102-
return errorCodeToError(object_error::parse_failed);
101+
if (Error Err = Reader.readFixedString(EntryID, EntryIDSize))
102+
return Err;
103103

104104
auto Entry = std::make_unique<OffloadBundleEntry>(
105105
EntryOffset + SectionOffset, EntrySize, EntryIDSize, EntryID);
@@ -125,7 +125,7 @@ OffloadBundleFatBin::create(MemoryBufferRef Buf, uint64_t SectionOffset,
125125
// Read the Bundle Entries
126126
Error Err = TheBundle->readEntries(Buf.getBuffer(), SectionOffset);
127127
if (Err)
128-
return errorCodeToError(object_error::parse_failed);
128+
return Err;
129129

130130
return std::unique_ptr<OffloadBundleFatBin>(TheBundle);
131131
}

llvm/test/tools/llvm-readobj/ELF/AMDGPU/offloading-fail.test

Lines changed: 26 additions & 0 deletions
Large diffs are not rendered by default.

llvm/test/tools/llvm-readobj/ELF/AMDGPU/offloading.test

Lines changed: 27 additions & 0 deletions
Large diffs are not rendered by default.

llvm/tools/llvm-readobj/ObjDumper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "llvm/Object/Archive.h"
1717
#include "llvm/Object/Decompressor.h"
1818
#include "llvm/Object/ObjectFile.h"
19+
#include "llvm/Object/OffloadBinary.h"
20+
#include "llvm/Object/OffloadBundle.h"
1921
#include "llvm/Support/Error.h"
2022
#include "llvm/Support/FormatVariadic.h"
2123
#include "llvm/Support/ScopedPrinter.h"
@@ -230,4 +232,14 @@ void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
230232
}
231233
}
232234

235+
void ObjDumper::printOffloading(const object::ObjectFile &Obj) {
236+
SmallVector<llvm::object::OffloadBundleFatBin> Bundles;
237+
if (Error Err = object::extractOffloadBundleFatBinary(Obj, Bundles))
238+
reportWarning(std::move(Err), Obj.getFileName());
239+
240+
// Print out all the FatBin Bundles that are contained in this buffer.
241+
for (const auto &[Index, Bundle] : llvm::enumerate(Bundles))
242+
Bundle.printEntriesAsURI();
243+
}
244+
233245
} // namespace llvm

llvm/tools/llvm-readobj/ObjDumper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/ADT/SmallVector.h"
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Object/ObjectFile.h"
19+
#include "llvm/Object/OffloadBinary.h"
1920
#include "llvm/Support/CommandLine.h"
2021

2122
#include <unordered_set>
@@ -188,6 +189,7 @@ class ObjDumper {
188189
std::function<Error(const Twine &Msg)> WarningHandler;
189190
void reportUniqueWarning(Error Err) const;
190191
void reportUniqueWarning(const Twine &Msg) const;
192+
void printOffloading(const object::ObjectFile &Obj);
191193

192194
protected:
193195
ScopedPrinter &W;

llvm/tools/llvm-readobj/Opts.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def file_header : FF<"file-header", "Display file header">;
3232
def headers : FF<"headers", "Equivalent to setting: --file-header, --program-headers, --section-headers">;
3333
defm hex_dump : Eq<"hex-dump", "Display the specified section(s) as hexadecimal bytes">, MetaVarName<"<name or index>">;
3434
def pretty_print : FF<"pretty-print", "Pretty print JSON output">;
35+
def offloading : FF<"offloading", "Display the content of the offloading section">;
3536
def relocs : FF<"relocs", "Display the relocation entries in the file">;
3637
def section_data : FF<"section-data", "Display section data for each section shown. This option has no effect for GNU style output">;
3738
def section_details : FF<"section-details", "Display the section details">;

llvm/tools/llvm-readobj/llvm-readobj.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static bool HashHistogram;
135135
static bool Memtag;
136136
static bool NeededLibraries;
137137
static bool Notes;
138+
static bool Offloading;
138139
static bool ProgramHeaders;
139140
static bool SectionGroups;
140141
static std::vector<std::string> SFrame;
@@ -274,6 +275,7 @@ static void parseOptions(const opt::InputArgList &Args) {
274275
opts::Memtag = Args.hasArg(OPT_memtag);
275276
opts::NeededLibraries = Args.hasArg(OPT_needed_libs);
276277
opts::Notes = Args.hasArg(OPT_notes);
278+
opts::Offloading = Args.hasArg(OPT_offloading);
277279
opts::PrettyPrint = Args.hasArg(OPT_pretty_print);
278280
opts::ProgramHeaders = Args.hasArg(OPT_program_headers);
279281
opts::SectionGroups = Args.hasArg(OPT_section_groups);
@@ -459,6 +461,8 @@ static void dumpObject(ObjectFile &Obj, ScopedPrinter &Writer,
459461
Dumper->printGnuHashTable();
460462
if (opts::VersionInfo)
461463
Dumper->printVersionInfo();
464+
if (opts::Offloading)
465+
Dumper->printOffloading(Obj);
462466
if (opts::StringTable)
463467
Dumper->printStringTable();
464468
if (Obj.isELF()) {
@@ -707,6 +711,7 @@ int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
707711
opts::DynamicTable = true;
708712
opts::Notes = true;
709713
opts::VersionInfo = true;
714+
opts::Offloading = true;
710715
opts::UnwindInfo = true;
711716
opts::SectionGroups = true;
712717
opts::HashHistogram = true;

0 commit comments

Comments
 (0)