Skip to content

Commit 27f2e97

Browse files
dsalinasDavid Salinas
authored andcommitted
Extend llvm-objdump to support FatBins
add option --offload-fatbin SWDEV-333176 - Shift functionality of 'roc-obj-*' perl scripts into llvm-objdump Change-Id: Ibc865f80e30aa1a6e5495ecfe617be68a5e15fcf
1 parent 3a10064 commit 27f2e97

File tree

8 files changed

+961
-46
lines changed

8 files changed

+961
-46
lines changed

llvm/include/llvm/Object/OffloadBinary.h

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,60 +210,76 @@ class OffloadFile : public OwningBinary<OffloadBinary> {
210210
}
211211
};
212212

213+
struct BundleEntry {
214+
uint64_t Offset = 0u;
215+
uint64_t Size = 0u;
216+
uint64_t IDLength = 0u;
217+
StringRef ID;
218+
BundleEntry(uint64_t O, uint64_t S, uint64_t I, StringRef T)
219+
: Offset(O), Size(S), IDLength(I), ID(T) {}
220+
void dumpInfo(raw_ostream &OS) {
221+
OS << "Offset = " << Offset << ", Size = " << Size
222+
<< ", ID Length = " << IDLength << ", ID = " << ID;
223+
}
224+
void dumpURI(raw_ostream &OS, StringRef filePath) {
225+
OS << ID.data() << "\tfile:\/\/" << filePath << "#offset=" << Offset
226+
<< "&size=" << Size << "\n";
227+
}
228+
};
229+
213230
class OffloadFatBinBundle {
214231

215232
private:
216233
uint64_t Size = 0u;
217234
StringRef FileName;
218-
int64_t NumberOfEntries;
235+
uint64_t NumberOfEntries;
236+
SmallVector<BundleEntry> Entries;
219237

220238
public:
221-
struct BundleEntry {
222-
uint64_t Offset = 0u;
223-
uint64_t Size = 0u;
224-
uint64_t IDLength = 0u;
225-
StringRef ID;
226-
BundleEntry(uint64_t O, uint64_t S, uint64_t I, StringRef T)
227-
: Offset(O), Size(S), IDLength(I), ID(T) {}
228-
void dump(raw_ostream &OS) {
229-
OS << "Offset = " << Offset << ", Size = " << Size
230-
<< ", ID Length = " << IDLength << ", ID = " << ID;
231-
}
232-
void dumpURI(raw_ostream &OS, StringRef filePath) {
233-
OS << ID.data() << "\tfile:\/\/" << filePath << "#offset=" << Offset
234-
<< "&size=" << Size << "\n";
235-
}
236-
};
237-
239+
SmallVector<BundleEntry> getEntries() { return Entries; }
238240
uint64_t getSize() const { return Size; }
239241
StringRef getFileName() const { return FileName; }
240-
int64_t getNumEntries() const { return NumberOfEntries; }
242+
uint64_t getNumEntries() const { return NumberOfEntries; }
241243

242-
std::unique_ptr<SmallVector<BundleEntry>> Entries;
243244
static Expected<std::unique_ptr<OffloadFatBinBundle>>
244245
create(MemoryBufferRef, uint64_t SectionOffset, StringRef fileName);
245246
Error extractBundle(const ObjectFile &Source);
246247

248+
Error DumpEntryToCodeObject();
249+
247250
Error ReadEntries(StringRef Section, uint64_t SectionOffset);
248251
void DumpEntries() {
249-
SmallVectorImpl<BundleEntry>::iterator it = Entries->begin();
250-
for (int64_t I = 0; I < Entries->size(); I++) {
251-
it->dump(outs());
252+
SmallVectorImpl<BundleEntry>::iterator it = Entries.begin();
253+
for (uint64_t I = 0; I < Entries.size(); I++) {
254+
it->dumpInfo(outs());
252255
++it;
253256
}
254257
}
255258

256259
void PrintEntriesAsURI() {
257-
SmallVectorImpl<BundleEntry>::iterator it = Entries->begin();
258-
for (int64_t I = 0; I < NumberOfEntries; I++) {
260+
SmallVectorImpl<BundleEntry>::iterator it = Entries.begin();
261+
for (uint64_t I = 0; I < NumberOfEntries; I++) {
259262
it->dumpURI(outs(), FileName);
260263
++it;
261264
}
262265
}
263266

264267
OffloadFatBinBundle(MemoryBufferRef Source, StringRef file) : FileName(file) {
265268
NumberOfEntries = 0;
266-
Entries = std::make_unique<SmallVector<BundleEntry>>();
269+
Entries = SmallVector<BundleEntry>();
270+
}
271+
272+
SmallVector<BundleEntry> EntryIDContains(StringRef str) {
273+
SmallVector<BundleEntry> found = SmallVector<BundleEntry>();
274+
SmallVectorImpl<BundleEntry>::iterator it = Entries.begin();
275+
for (uint64_t I = 0; I < NumberOfEntries; I++) {
276+
if (it->ID.contains(str)) {
277+
found.push_back(*it);
278+
}
279+
280+
++it;
281+
}
282+
return found;
267283
}
268284
};
269285

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/lib/Object/OffloadBinary.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/Object/Archive.h"
1919
#include "llvm/Object/ArchiveWriter.h"
2020
#include "llvm/Object/Binary.h"
21-
#include "llvm/BinaryFormat/COFF.h"
2221
#include "llvm/Object/COFF.h"
2322
#include "llvm/Object/ELFObjectFile.h"
2423
#include "llvm/Object/Error.h"
@@ -36,8 +35,8 @@ using namespace llvm::object;
3635
namespace {
3736

3837
static llvm::TimerGroup
39-
ClangOffloadBundlerTimerGroup("Clang Offload Bundler Timer Group",
40-
"Timer group for clang offload bundler");
38+
OffloadBundlerTimerGroup("Offload Bundler Timer Group",
39+
"Timer group for offload bundler");
4140

4241
/// Attempts to extract all the embedded device images contained inside the
4342
/// buffer \p Contents. The buffer is expected to contain a valid offloading
@@ -107,7 +106,7 @@ Error extractFromObject(const ObjectFile &Obj,
107106
return Error::success();
108107
}
109108

110-
// Extract an Offload bundle (usually a Clang Offload Bundle) from a fat_bin
109+
// Extract an Offload bundle (usually a Offload Bundle) from a fat_bin
111110
// section
112111
Error extractOffloadBundle(MemoryBufferRef Contents, uint64_t SectionOffset,
113112
StringRef fileName,
@@ -267,10 +266,10 @@ Error OffloadFatBinBundle::ReadEntries(StringRef Buffer,
267266
}
268267

269268
// create a Bundle Entry object:
270-
auto entry = new OffloadFatBinBundle::BundleEntry(
271-
EntryOffset + SectionOffset, EntrySize, EntryIDSize, EntryID);
269+
auto entry = new BundleEntry(EntryOffset + SectionOffset, EntrySize,
270+
EntryIDSize, EntryID);
272271

273-
Entries->push_back(*entry);
272+
Entries.push_back(*entry);
274273
} // end of for loop
275274

276275
return Error::success();
@@ -298,8 +297,7 @@ OffloadFatBinBundle::create(MemoryBufferRef Buf, uint64_t SectionOffset,
298297

299298
Error OffloadFatBinBundle::extractBundle(const ObjectFile &Source) {
300299
// This will extract all entries in the Bundle
301-
SmallVectorImpl<OffloadFatBinBundle::BundleEntry>::iterator it =
302-
Entries->begin();
300+
SmallVectorImpl<BundleEntry>::iterator it = Entries.begin();
303301
for (int64_t I = 0; I < getNumEntries(); I++) {
304302

305303
if (it->Size > 0) {
@@ -467,10 +465,6 @@ Error object::extractFatBinaryFromObject(
467465
} else if (Obj.isCOFF()) {
468466
if (const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(&Obj)) {
469467
const coff_section *CoffSection = COFFObj->getCOFFSection(Sec);
470-
fprintf(
471-
stderr, "DAVE: COFF viritual address =0x%llX\n",
472-
CoffSection
473-
->VirtualAddress); // COFFObj->getCOFFSection(Sec)->VirtualAddress);
474468
}
475469
}
476470

@@ -698,7 +692,7 @@ CompressedOffloadBundle::decompress(llvm::MemoryBufferRef &Input,
698692
"Unknown compressing method");
699693

700694
llvm::Timer DecompressTimer("Decompression Timer", "Decompression time",
701-
ClangOffloadBundlerTimerGroup);
695+
OffloadBundlerTimerGroup);
702696
if (Verbose)
703697
DecompressTimer.startTimer();
704698

@@ -720,7 +714,7 @@ CompressedOffloadBundle::decompress(llvm::MemoryBufferRef &Input,
720714
// Recalculate MD5 hash for integrity check
721715
llvm::Timer HashRecalcTimer("Hash Recalculation Timer",
722716
"Hash recalculation time",
723-
ClangOffloadBundlerTimerGroup);
717+
OffloadBundlerTimerGroup);
724718
HashRecalcTimer.startTimer();
725719
llvm::MD5 Hash;
726720
llvm::MD5::MD5Result Result;
@@ -775,7 +769,7 @@ CompressedOffloadBundle::compress(llvm::compression::Params P,
775769
"Compression not supported");
776770

777771
llvm::Timer HashTimer("Hash Calculation Timer", "Hash calculation time",
778-
ClangOffloadBundlerTimerGroup);
772+
OffloadBundlerTimerGroup);
779773
if (Verbose)
780774
HashTimer.startTimer();
781775
llvm::MD5 Hash;
@@ -792,7 +786,7 @@ CompressedOffloadBundle::compress(llvm::compression::Params P,
792786
Input.getBuffer().size());
793787

794788
llvm::Timer CompressTimer("Compression Timer", "Compression time",
795-
ClangOffloadBundlerTimerGroup);
789+
OffloadBundlerTimerGroup);
796790
if (Verbose)
797791
CompressTimer.startTimer();
798792
llvm::compression::compress(P, BufferUint8, CompressedBuffer);

0 commit comments

Comments
 (0)