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