OffloadDump.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "OffloadDump.h"
  14. #include "llvm-objdump.h"
  15. #include "llvm/Object/ELFObjectFile.h"
  16. #include "llvm/Support/Alignment.h"
  17. using namespace llvm;
  18. using namespace llvm::object;
  19. using namespace llvm::objdump;
  20. /// Get the printable name of the image kind.
  21. static StringRef getImageName(const OffloadBinary &OB) {
  22. switch (OB.getImageKind()) {
  23. case IMG_Object:
  24. return "elf";
  25. case IMG_Bitcode:
  26. return "llvm ir";
  27. case IMG_Cubin:
  28. return "cubin";
  29. case IMG_Fatbinary:
  30. return "fatbinary";
  31. case IMG_PTX:
  32. return "ptx";
  33. default:
  34. return "<none>";
  35. }
  36. }
  37. static void printBinary(const OffloadBinary &OB, uint64_t Index) {
  38. outs() << "\nOFFLOADING IMAGE [" << Index << "]:\n";
  39. outs() << left_justify("kind", 16) << getImageName(OB) << "\n";
  40. outs() << left_justify("arch", 16) << OB.getArch() << "\n";
  41. outs() << left_justify("triple", 16) << OB.getTriple() << "\n";
  42. outs() << left_justify("producer", 16)
  43. << getOffloadKindName(OB.getOffloadKind()) << "\n";
  44. }
  45. /// Print the embedded offloading contents of an ObjectFile \p O.
  46. void llvm::dumpOffloadBinary(const ObjectFile &O) {
  47. if (!O.isELF() && !O.isCOFF()) {
  48. reportWarning(
  49. "--offloading is currently only supported for COFF and ELF targets",
  50. O.getFileName());
  51. return;
  52. }
  53. SmallVector<OffloadFile> Binaries;
  54. if (Error Err = extractOffloadBinaries(O.getMemoryBufferRef(), Binaries))
  55. reportError(O.getFileName(), "while extracting offloading files: " +
  56. toString(std::move(Err)));
  57. // Print out all the binaries that are contained in this buffer.
  58. for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
  59. printBinary(*Binaries[I].getBinary(), I);
  60. }
  61. /// Print the contents of an offload binary file \p OB. This may contain
  62. /// multiple binaries stored in the same buffer.
  63. void llvm::dumpOffloadSections(const OffloadBinary &OB) {
  64. SmallVector<OffloadFile> Binaries;
  65. if (Error Err = extractOffloadBinaries(OB.getMemoryBufferRef(), Binaries))
  66. reportError(OB.getFileName(), "while extracting offloading files: " +
  67. toString(std::move(Err)));
  68. // Print out all the binaries that are contained in this buffer.
  69. for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
  70. printBinary(*Binaries[I].getBinary(), I);
  71. }