|
| 1 | +//===- llvm-offload-wrapper: Create runtime registration code for devices -===// |
| 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 | +// Provides a utility for generating runtime registration code for device code. |
| 10 | +// We take a binary image (CUDA fatbinary, HIP offload bundle, LLVM binary) and |
| 11 | +// create a new IR module that calls the respective runtime to load it on the |
| 12 | +// device. |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "llvm/Bitcode/BitcodeWriter.h" |
| 17 | +#include "llvm/Frontend/Offloading/OffloadWrapper.h" |
| 18 | +#include "llvm/Frontend/Offloading/Utility.h" |
| 19 | +#include "llvm/Object/OffloadBinary.h" |
| 20 | +#include "llvm/Support/CommandLine.h" |
| 21 | +#include "llvm/Support/FileOutputBuffer.h" |
| 22 | +#include "llvm/Support/FileSystem.h" |
| 23 | +#include "llvm/Support/InitLLVM.h" |
| 24 | +#include "llvm/Support/MemoryBuffer.h" |
| 25 | +#include "llvm/Support/Path.h" |
| 26 | +#include "llvm/Support/Signals.h" |
| 27 | +#include "llvm/Support/StringSaver.h" |
| 28 | +#include "llvm/Support/WithColor.h" |
| 29 | +#include "llvm/TargetParser/Host.h" |
| 30 | + |
| 31 | +using namespace llvm; |
| 32 | + |
| 33 | +static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); |
| 34 | + |
| 35 | +static cl::OptionCategory |
| 36 | + OffloadWrapeprCategory("llvm-offload-wrapper options"); |
| 37 | + |
| 38 | +static cl::opt<object::OffloadKind> Kind( |
| 39 | + "kind", cl::desc("Wrap for offload kind:"), cl::cat(OffloadWrapeprCategory), |
| 40 | + cl::Required, |
| 41 | + cl::values(clEnumValN(object::OFK_OpenMP, "openmp", "Wrap OpenMP binaries"), |
| 42 | + clEnumValN(object::OFK_Cuda, "cuda", "Wrap CUDA binaries"), |
| 43 | + clEnumValN(object::OFK_HIP, "hip", "Wrap HIP binaries"))); |
| 44 | + |
| 45 | +static cl::opt<std::string> OutputFile("o", cl::desc("Write output to <file>."), |
| 46 | + cl::value_desc("file"), |
| 47 | + cl::cat(OffloadWrapeprCategory)); |
| 48 | + |
| 49 | +static cl::list<std::string> InputFiles(cl::Positional, |
| 50 | + cl::desc("Wrap input from <file>"), |
| 51 | + cl::value_desc("file"), cl::OneOrMore, |
| 52 | + cl::cat(OffloadWrapeprCategory)); |
| 53 | + |
| 54 | +static cl::opt<std::string> |
| 55 | + TheTriple("triple", cl::desc("Target triple for the wrapper module"), |
| 56 | + cl::init(sys::getDefaultTargetTriple()), |
| 57 | + cl::cat(OffloadWrapeprCategory)); |
| 58 | + |
| 59 | +static Error wrapImages(ArrayRef<ArrayRef<char>> BuffersToWrap) { |
| 60 | + if (BuffersToWrap.size() > 1 && |
| 61 | + (Kind == llvm::object::OFK_Cuda || Kind == llvm::object::OFK_HIP)) |
| 62 | + return createStringError( |
| 63 | + "CUDA / HIP offloading uses a single fatbinary or offload bundle"); |
| 64 | + |
| 65 | + LLVMContext Context; |
| 66 | + Module M("offload.wrapper.module", Context); |
| 67 | + M.setTargetTriple(Triple()); |
| 68 | + |
| 69 | + switch (Kind) { |
| 70 | + case llvm::object::OFK_OpenMP: |
| 71 | + if (Error Err = offloading::wrapOpenMPBinaries( |
| 72 | + M, BuffersToWrap, offloading::getOffloadEntryArray(M), |
| 73 | + /*Suffix=*/"", /*Relocatable=*/false)) |
| 74 | + return Err; |
| 75 | + break; |
| 76 | + case llvm::object::OFK_Cuda: |
| 77 | + if (Error Err = offloading::wrapCudaBinary( |
| 78 | + M, BuffersToWrap.front(), offloading::getOffloadEntryArray(M), |
| 79 | + /*Suffix=*/"", /*EmitSurfacesAndTextures=*/false)) |
| 80 | + return Err; |
| 81 | + break; |
| 82 | + case llvm::object::OFK_HIP: |
| 83 | + if (Error Err = offloading::wrapHIPBinary( |
| 84 | + M, BuffersToWrap.front(), offloading::getOffloadEntryArray(M))) |
| 85 | + return Err; |
| 86 | + break; |
| 87 | + default: |
| 88 | + return createStringError(getOffloadKindName(Kind) + |
| 89 | + " wrapping is not supported"); |
| 90 | + } |
| 91 | + |
| 92 | + int FD = -1; |
| 93 | + if (std::error_code EC = sys::fs::openFileForWrite(OutputFile, FD)) |
| 94 | + return errorCodeToError(EC); |
| 95 | + llvm::raw_fd_ostream OS(FD, true); |
| 96 | + WriteBitcodeToFile(M, OS); |
| 97 | + |
| 98 | + return Error::success(); |
| 99 | +} |
| 100 | + |
| 101 | +int main(int argc, char **argv) { |
| 102 | + InitLLVM X(argc, argv); |
| 103 | + cl::HideUnrelatedOptions(OffloadWrapeprCategory); |
| 104 | + cl::ParseCommandLineOptions( |
| 105 | + argc, argv, |
| 106 | + "Generate runtime registration code for a device binary image\n"); |
| 107 | + |
| 108 | + if (Help) { |
| 109 | + cl::PrintHelpMessage(); |
| 110 | + return EXIT_SUCCESS; |
| 111 | + } |
| 112 | + |
| 113 | + auto ReportError = [argv](Error E) { |
| 114 | + logAllUnhandledErrors(std::move(E), WithColor::error(errs(), argv[0])); |
| 115 | + exit(EXIT_FAILURE); |
| 116 | + }; |
| 117 | + |
| 118 | + SmallVector<std::unique_ptr<MemoryBuffer>> Buffers; |
| 119 | + SmallVector<ArrayRef<char>> BuffersToWrap; |
| 120 | + for (StringRef Input : InputFiles) { |
| 121 | + ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 122 | + MemoryBuffer::getFileOrSTDIN(Input); |
| 123 | + if (std::error_code EC = BufferOrErr.getError()) |
| 124 | + ReportError(createFileError(Input, EC)); |
| 125 | + std::unique_ptr<MemoryBuffer> &Buffer = |
| 126 | + Buffers.emplace_back(std::move(*BufferOrErr)); |
| 127 | + BuffersToWrap.emplace_back( |
| 128 | + ArrayRef<char>(Buffer->getBufferStart(), Buffer->getBufferSize())); |
| 129 | + } |
| 130 | + |
| 131 | + if (Error Err = wrapImages(BuffersToWrap)) |
| 132 | + ReportError(std::move(Err)); |
| 133 | + |
| 134 | + return EXIT_SUCCESS; |
| 135 | +} |
0 commit comments