Skip to content

Commit 4742936

Browse files
dsalinasDavid Salinas
authored andcommitted
Extend llvm-objdump to support clang offload fat bins
add option --offload-fatbin Change-Id: Ibc865f80e30aa1a6e5495ecfe617be68a5e15fcf
1 parent a6e8e21 commit 4742936

File tree

6 files changed

+908
-1
lines changed

6 files changed

+908
-1
lines changed

llvm/lib/Object/ObjectFile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ ObjectFile::createObjectFile(StringRef ObjectPath) {
212212
if (std::error_code EC = FileOrErr.getError())
213213
return errorCodeToError(EC);
214214
std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
215-
216215
Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
217216
createObjectFile(Buffer->getMemBufferRef());
218217
if (Error Err = ObjOrErr.takeError())

llvm/test/tools/llvm-objdump/Offloading/fatbin.test

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

llvm/tools/llvm-objdump/ObjdumpOpts.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def fault_map_section : Flag<["--"], "fault-map-section">,
108108
def offloading : Flag<["--"], "offloading">,
109109
HelpText<"Display the content of the offloading section">;
110110

111+
def offload_fatbin : Flag<["--"], "offload-fatbin">,
112+
HelpText<"Display the content of the offload FatBin section">;
113+
111114
def file_headers : Flag<["--"], "file-headers">,
112115
HelpText<"Display the contents of the overall file header">;
113116
def : Flag<["-"], "f">, Alias<file_headers>,

llvm/tools/llvm-objdump/OffloadDump.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
#include "OffloadDump.h"
1515
#include "llvm-objdump.h"
1616
#include "llvm/Object/ELFObjectFile.h"
17+
#include "llvm/Object/OffloadBinary.h"
1718
#include "llvm/Support/Alignment.h"
1819

1920
using namespace llvm;
2021
using namespace llvm::object;
2122
using namespace llvm::objdump;
2223

24+
void disassembleObject(llvm::object::ObjectFile *, bool InlineRelocs);
25+
2326
/// Get the printable name of the image kind.
2427
static StringRef getImageName(const OffloadBinary &OB) {
2528
switch (OB.getImageKind()) {
@@ -66,6 +69,55 @@ void llvm::dumpOffloadBinary(const ObjectFile &O) {
6669
printBinary(*Binaries[I].getBinary(), I);
6770
}
6871

72+
// Given an Object file, collect all Bundles of FatBin Binaries
73+
// and dump them into Code Object files
74+
// if -d is specified, disassemble the Code Object Files
75+
// if -arch=-name is specified, only dump the Entries that match the target arch
76+
void llvm::dumpOffloadBundleFatBinary(const ObjectFile &O, std::string ArchName,
77+
bool Disassemble) {
78+
assert((O.isELF() || O.isCOFF()) && "Invalid file type");
79+
// Collect all Bundles and their Entries ....
80+
SmallVector<llvm::object::OffloadBundleFatBin> FoundBundles;
81+
SmallVector<OffloadBundleEntry> FoundEntries;
82+
83+
if (Error Err = llvm::object::extractOffloadBundleFatBinary(O, FoundBundles))
84+
reportError(O.getFileName(), "while extracting offload FatBin bundles: " +
85+
toString(std::move(Err)));
86+
87+
// Now filter based on if arch-name is specified
88+
SmallVectorImpl<llvm::object::OffloadBundleFatBin>::iterator BundleIter =
89+
FoundBundles.begin();
90+
for (uint64_t bundle_num = 0; bundle_num < FoundBundles.size();
91+
bundle_num++) {
92+
if (!ArchName.empty())
93+
FoundEntries = BundleIter->EntryIDContains(StringRef(ArchName));
94+
else
95+
FoundEntries = BundleIter->getEntries();
96+
97+
// now we have a list of Found Entries .... dump them
98+
SmallVectorImpl<OffloadBundleEntry>::iterator FoundIter =
99+
FoundEntries.begin();
100+
for (int64_t entry_num = 0; entry_num < FoundEntries.size(); entry_num++) {
101+
// create file name for this object file: <source-filename>:<Bundle
102+
// Number>.<EntryID>
103+
std::string str = BundleIter->getFileName().str() + ":" +
104+
itostr(bundle_num) + "." + FoundIter->ID.str();
105+
StringRef OutputFilename = StringRef(str);
106+
if (Error Err = object::extractCodeObject(
107+
O, FoundIter->Offset, FoundIter->Size, OutputFilename))
108+
reportError(O.getFileName(),
109+
"while extracting offload Bundle Entries: " +
110+
toString(std::move(Err)));
111+
112+
// TODO: If -d was specified, disasseble the Code Object too
113+
114+
++FoundIter;
115+
} // end of for found_entries loop
116+
117+
++BundleIter;
118+
} // end of for Bundles loop
119+
}
120+
69121
/// Print the contents of an offload binary file \p OB. This may contain
70122
/// multiple binaries stored in the same buffer.
71123
void llvm::dumpOffloadSections(const OffloadBinary &OB) {

llvm/tools/llvm-objdump/OffloadDump.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ namespace llvm {
1717
void dumpOffloadSections(const object::OffloadBinary &OB);
1818
void dumpOffloadBinary(const object::ObjectFile &O);
1919

20+
/// Dump fat binary in binary clang-offload-bundler format
21+
void dumpOffloadBundleFatBinary(const object::ObjectFile &O,
22+
std::string ArchName, bool Disassemble);
2023
} // namespace llvm
2124

2225
#endif

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ std::vector<std::string> objdump::MAttrs;
324324
bool objdump::ShowRawInsn;
325325
bool objdump::LeadingAddr;
326326
static bool Offloading;
327+
static bool OffloadFatBin;
327328
static bool RawClangAST;
328329
bool objdump::Relocations;
329330
bool objdump::PrintImmHex;
@@ -3315,6 +3316,8 @@ static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
33153316
D.printDynamicRelocations();
33163317
if (SectionContents)
33173318
printSectionContents(O);
3319+
if (OffloadFatBin)
3320+
dumpOffloadBundleFatBinary(*O, ArchName, Disassemble);
33183321
if (Disassemble)
33193322
disassembleObject(O, Relocations);
33203323
if (UnwindInfo)
@@ -3521,6 +3524,7 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
35213524
DynamicRelocations = InputArgs.hasArg(OBJDUMP_dynamic_reloc);
35223525
FaultMapSection = InputArgs.hasArg(OBJDUMP_fault_map_section);
35233526
Offloading = InputArgs.hasArg(OBJDUMP_offloading);
3527+
OffloadFatBin = InputArgs.hasArg(OBJDUMP_offload_fatbin);
35243528
FileHeaders = InputArgs.hasArg(OBJDUMP_file_headers);
35253529
SectionContents = InputArgs.hasArg(OBJDUMP_full_contents);
35263530
PrintLines = InputArgs.hasArg(OBJDUMP_line_numbers);
@@ -3732,6 +3736,7 @@ int llvm_objdump_main(int argc, char **argv, const llvm::ToolContext &) {
37323736
!DynamicRelocations && !FileHeaders && !PrivateHeaders && !RawClangAST &&
37333737
!Relocations && !SectionHeaders && !SectionContents && !SymbolTable &&
37343738
!DynamicSymbolTable && !UnwindInfo && !FaultMapSection && !Offloading &&
3739+
!OffloadFatBin &&
37353740
!(MachOOpt &&
37363741
(Bind || DataInCode || ChainedFixups || DyldInfo || DylibId ||
37373742
DylibsUsed || ExportsTrie || FirstPrivateHeader ||

0 commit comments

Comments
 (0)