Skip to content

Commit e1a0ac4

Browse files
david-salinasdsalinas_amdeng
authored andcommitted
Add --offoading option to llvm-readobj
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. Change-Id: Ib24a722be16f499ffb56bf46e4b82e90d8775040
1 parent e27876a commit e1a0ac4

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

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 using URI syntax.
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/test/tools/llvm-readobj/ELF/AMDGPU/offloading.test

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

llvm/tools/llvm-readobj/ObjDumper.cpp

Lines changed: 19 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,21 @@ void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
230232
}
231233
}
232234

235+
// TODO: add proper error handling.
236+
void ObjDumper::printOffloading(const object::ObjectFile &Obj) {
237+
// we can use an argument to let user select which offloading section they
238+
// want to print. but for now, we're hardcoding ELF and "hip_fatbin".
239+
assert((Obj.isELF() || Obj.isCOFF()) && "Invalid file type");
240+
241+
SmallVector<llvm::object::OffloadBundleFatBin> Bundles;
242+
if (Error Err = llvm::object::extractOffloadBundleFatBinary(Obj, Bundles))
243+
reportWarning(createError("Cannot extract Fatbin Binary from Object."),
244+
Obj.getFileName());
245+
246+
// Print out all the FatBin Bundles that are contained in this buffer.
247+
for (const auto &[Index, Bundle] : llvm::enumerate(Bundles)) {
248+
Bundle.printEntriesAsURI();
249+
}
250+
}
251+
233252
} // 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>
@@ -186,6 +187,7 @@ class ObjDumper {
186187
std::function<Error(const Twine &Msg)> WarningHandler;
187188
void reportUniqueWarning(Error Err) const;
188189
void reportUniqueWarning(const Twine &Msg) const;
190+
void printOffloading(const object::ObjectFile &Obj);
189191

190192
protected:
191193
ScopedPrinter &W;

llvm/tools/llvm-readobj/Opts.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def notes : FF<"notes", "Display notes">, Group<grp_elf>;
6464
def program_headers : FF<"program-headers", "Display program headers">, Group<grp_elf>;
6565
def version_info : FF<"version-info", "Display version sections">, Group<grp_elf>;
6666

67+
def offloading : Flag<["--"], "offloading">,
68+
HelpText<"Display the content of the offloading section">;
69+
6770
// Mach-O specific options.
6871
def grp_mach_o : OptionGroup<"kind">, HelpText<"OPTIONS (Mach-O specific)">;
6972
def macho_data_in_code : FF<"macho-data-in-code", "Display Data in Code command">, Group<grp_mach_o>;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static bool Notes;
138138
static bool ProgramHeaders;
139139
static bool SectionGroups;
140140
static bool VersionInfo;
141+
static bool Offloading;
141142

142143
// Mach-O specific options.
143144
static bool MachODataInCode;
@@ -288,6 +289,7 @@ static void parseOptions(const opt::InputArgList &Args) {
288289
}
289290
}
290291
opts::VersionInfo = Args.hasArg(OPT_version_info);
292+
opts::Offloading = Args.hasArg(OPT_offloading);
291293

292294
// Mach-O specific options.
293295
opts::MachODataInCode = Args.hasArg(OPT_macho_data_in_code);
@@ -455,6 +457,8 @@ static void dumpObject(ObjectFile &Obj, ScopedPrinter &Writer,
455457
Dumper->printGnuHashTable();
456458
if (opts::VersionInfo)
457459
Dumper->printVersionInfo();
460+
if (opts::Offloading)
461+
Dumper->printOffloading(Obj);
458462
if (opts::StringTable)
459463
Dumper->printStringTable();
460464
if (Obj.isELF()) {
@@ -699,6 +703,7 @@ int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
699703
opts::DynamicTable = true;
700704
opts::Notes = true;
701705
opts::VersionInfo = true;
706+
opts::Offloading = true;
702707
opts::UnwindInfo = true;
703708
opts::SectionGroups = true;
704709
opts::HashHistogram = true;

0 commit comments

Comments
 (0)