xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/OffloadDump.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 //===-- OffloadDump.cpp - Offloading dumper ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the offloading-specific dumper for llvm-objdump.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "OffloadDump.h"
15 #include "llvm-objdump.h"
16 #include "llvm/Object/ELFObjectFile.h"
17 #include "llvm/Support/Alignment.h"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 using namespace llvm::objdump;
22 
23 /// Get the printable name of the image kind.
24 static StringRef getImageName(const OffloadBinary &OB) {
25   switch (OB.getImageKind()) {
26   case IMG_Object:
27     return "elf";
28   case IMG_Bitcode:
29     return "llvm ir";
30   case IMG_Cubin:
31     return "cubin";
32   case IMG_Fatbinary:
33     return "fatbinary";
34   case IMG_PTX:
35     return "ptx";
36   default:
37     return "<none>";
38   }
39 }
40 
41 static void printBinary(const OffloadBinary &OB, uint64_t Index) {
42   outs() << "\nOFFLOADING IMAGE [" << Index << "]:\n";
43   outs() << left_justify("kind", 16) << getImageName(OB) << "\n";
44   outs() << left_justify("arch", 16) << OB.getArch() << "\n";
45   outs() << left_justify("triple", 16) << OB.getTriple() << "\n";
46   outs() << left_justify("producer", 16)
47          << getOffloadKindName(OB.getOffloadKind()) << "\n";
48 }
49 
50 /// Print the embedded offloading contents of an ObjectFile \p O.
51 void llvm::dumpOffloadBinary(const ObjectFile &O) {
52   if (!O.isELF() && !O.isCOFF()) {
53     reportWarning(
54         "--offloading is currently only supported for COFF and ELF targets",
55         O.getFileName());
56     return;
57   }
58 
59   SmallVector<OffloadFile> Binaries;
60   if (Error Err = extractOffloadBinaries(O.getMemoryBufferRef(), Binaries))
61     reportError(O.getFileName(), "while extracting offloading files: " +
62                                      toString(std::move(Err)));
63 
64   // Print out all the binaries that are contained in this buffer.
65   for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
66     printBinary(*Binaries[I].getBinary(), I);
67 }
68 
69 /// Print the contents of an offload binary file \p OB. This may contain
70 /// multiple binaries stored in the same buffer.
71 void llvm::dumpOffloadSections(const OffloadBinary &OB) {
72   SmallVector<OffloadFile> Binaries;
73   if (Error Err = extractOffloadBinaries(OB.getMemoryBufferRef(), Binaries))
74     reportError(OB.getFileName(), "while extracting offloading files: " +
75                                       toString(std::move(Err)));
76 
77   // Print out all the binaries that are contained in this buffer.
78   for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
79     printBinary(*Binaries[I].getBinary(), I);
80 }
81