Skip to content

Commit b9297da

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 eea2802 commit b9297da

File tree

9 files changed

+1030
-36
lines changed

9 files changed

+1030
-36
lines changed

llvm/include/llvm/Object/OffloadBinary.h

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,51 +210,54 @@ 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;
218235
int64_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; }
240242
int64_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 (int64_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();
260+
SmallVectorImpl<BundleEntry>::iterator it = Entries.begin();
258261
for (int64_t I = 0; I < NumberOfEntries; I++) {
259262
it->dumpURI(outs(), FileName);
260263
++it;
@@ -263,7 +266,20 @@ class OffloadFatBinBundle {
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 (int64_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: 4 additions & 10 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"
@@ -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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// RUN: clang++ -x hip --hip-link --offload-arch=gfx1100 --offload-arch=gfx1101 -o %t %s
2+
// RUN: llvm-objdump %t --offload-fatbin
3+
// RUN: llvm-objdump %t --offload-fatbin --arch-name=gfx1100
4+
5+
#include <stdio.h>
6+
7+
#include <iostream>
8+
#include "hip/hip_runtime.h"
9+
10+
__global__ void simpleAdd(uint32_t* A_d, const uint32_t* B_d, size_t N)
11+
{
12+
size_t i = (blockIdx.x * blockDim.x + threadIdx.x);
13+
A_d[i] += B_d[i];
14+
}
15+
16+
int main()
17+
{
18+
int device_count = 0;
19+
hipGetDeviceCount(&device_count);
20+
21+
std::cout<< "Found " << device_count << " HIP devices." << std::endl;
22+
for( int i = 0; i < device_count; i++ )
23+
{
24+
std::cout << "Device " << i << std::endl;
25+
hipDeviceProp_t props;
26+
hipGetDeviceProperties(&props, i);
27+
std::cout << " Name: " << props.name << std::endl;
28+
}
29+
30+
hipSetDevice(0);
31+
32+
uint32_t *A_d, *B_d;
33+
uint32_t *A_h, *B_h;
34+
35+
size_t N = 1000;
36+
size_t Nbytes = N * sizeof( uint32_t );
37+
38+
// Allocating host memory
39+
A_h = (uint32_t*) malloc(Nbytes);
40+
B_h = (uint32_t*) malloc(Nbytes);
41+
42+
for( size_t i = 0; i < N ; i++ )
43+
{
44+
A_h[i] = i;
45+
B_h[i] = 2*i;
46+
}
47+
48+
// Allocating device memory
49+
hipMalloc(&A_d, Nbytes);
50+
hipMalloc(&B_d, Nbytes);
51+
52+
// Copy host to device
53+
hipMemcpy(A_d, A_h, Nbytes, hipMemcpyHostToDevice);
54+
hipMemcpy(B_d, B_h, Nbytes, hipMemcpyHostToDevice);
55+
56+
// launch kernel
57+
const unsigned blocks = 512;
58+
const unsigned threadsPerBlock = 256;
59+
hipLaunchKernelGGL(simpleAdd, dim3(blocks), dim3(threadsPerBlock), 0, 0, A_d, B_d, N);
60+
61+
// Copy device to host
62+
hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost);
63+
64+
// Verify
65+
for( size_t i = 0; i < N; i++ )
66+
{
67+
std::cout << A_h[i] << " ";
68+
uint32_t A_ref = 3*i;
69+
if( A_h[i] != A_ref )
70+
{
71+
std::cout<< "Mismatch occured at " << i << ": " << A_h[i] << " != " << A_ref << std::endl;
72+
break;
73+
}
74+
}
75+
std::cout << std::endl;
76+
77+
// free up host memory
78+
free( A_h );
79+
free( B_h );
80+
}

0 commit comments

Comments
 (0)