|
| 1 | +//===--- spirv-to-ir-wrapper.cpp - Utility to convert to ir if needed -----===// |
| 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 | +// This utility checks if the input file is SPIR-V based. If so, convert to IR |
| 10 | +// The input can be either SPIR-V or LLVM-IR. When LLVM-IR, copy the file to |
| 11 | +// the specified output. |
| 12 | +// |
| 13 | +// Uses llvm-spirv to perform the conversion if needed. |
| 14 | +// |
| 15 | +// The output file is used to allow for proper input and output flow within |
| 16 | +// the driver toolchain. |
| 17 | +// |
| 18 | +// Usage: spirv-to-ir-wrapper input.spv -o output.bc |
| 19 | +// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | + |
| 22 | +#include "LLVMSPIRVLib.h" |
| 23 | +#include "llvm/BinaryFormat/Magic.h" |
| 24 | +#include "llvm/Support/CommandLine.h" |
| 25 | +#include "llvm/Support/Error.h" |
| 26 | +#include "llvm/Support/FileSystem.h" |
| 27 | +#include "llvm/Support/InitLLVM.h" |
| 28 | +#include "llvm/Support/Path.h" |
| 29 | +#include "llvm/Support/Program.h" |
| 30 | +#include "llvm/Support/SourceMgr.h" |
| 31 | +#include "llvm/Support/StringSaver.h" |
| 32 | + |
| 33 | +using namespace llvm; |
| 34 | + |
| 35 | +// InputFilename - The filename to read from. |
| 36 | +static cl::opt<std::string> InputFilename(cl::Positional, |
| 37 | + cl::value_desc("<input spv file>"), |
| 38 | + cl::desc("<input file>")); |
| 39 | + |
| 40 | +// Output - The filename to output to. |
| 41 | +static cl::opt<std::string> Output("o", cl::value_desc("output IR filename"), |
| 42 | + cl::desc("output filename")); |
| 43 | + |
| 44 | +// LlvmSpirvOpts - The filename to output to. |
| 45 | +static cl::opt<std::string> |
| 46 | + LlvmSpirvOpts("llvm-spirv-opts", cl::value_desc("llvm-spirv options"), |
| 47 | + cl::desc("options to pass to llvm-spirv")); |
| 48 | + |
| 49 | +static void error(const Twine &Message) { |
| 50 | + llvm::errs() << "spirv-to-ir-wrapper: " << Message << '\n'; |
| 51 | + exit(1); |
| 52 | +} |
| 53 | + |
| 54 | +// Convert the SPIR-V to LLVM-IR. |
| 55 | +static int convertSPIRVToLLVMIR(const char *Argv0) { |
| 56 | + // Find llvm-spirv. It is expected this resides in the same directory |
| 57 | + // as spirv-to-ir-wrapper. |
| 58 | + StringRef ParentPath = llvm::sys::path::parent_path(Argv0); |
| 59 | + llvm::ErrorOr<std::string> LlvmSpirvBinary = |
| 60 | + llvm::sys::findProgramByName("llvm-spirv", ParentPath); |
| 61 | + if (!LlvmSpirvBinary) |
| 62 | + LlvmSpirvBinary = llvm::sys::findProgramByName("llvm-spirv"); |
| 63 | + |
| 64 | + SmallVector<StringRef, 6> LlvmSpirvArgs = {"llvm-spirv", "-r", InputFilename, |
| 65 | + "-o", Output}; |
| 66 | + |
| 67 | + // Add any additional options specified by the user. |
| 68 | + SmallVector<const char *, 8> TargetArgs; |
| 69 | + llvm::BumpPtrAllocator BPA; |
| 70 | + llvm::StringSaver S(BPA); |
| 71 | + if (!LlvmSpirvOpts.empty()) { |
| 72 | + // Tokenize the string. |
| 73 | + llvm::cl::TokenizeGNUCommandLine(LlvmSpirvOpts, S, TargetArgs); |
| 74 | + std::copy(TargetArgs.begin(), TargetArgs.end(), |
| 75 | + std::back_inserter(LlvmSpirvArgs)); |
| 76 | + } |
| 77 | + |
| 78 | + return llvm::sys::ExecuteAndWait(LlvmSpirvBinary.get(), LlvmSpirvArgs); |
| 79 | +} |
| 80 | + |
| 81 | +static int copyInputToOutput() { |
| 82 | + return llvm::sys::fs::copy_file(InputFilename, Output).value(); |
| 83 | +} |
| 84 | + |
| 85 | +static bool isSPIRVBinary(const std::string &File) { |
| 86 | + auto FileOrError = MemoryBuffer::getFile(File, /*IsText=*/false, |
| 87 | + /*RequiresNullTerminator=*/false); |
| 88 | + if (!FileOrError) |
| 89 | + return false; |
| 90 | + std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError); |
| 91 | + return SPIRV::isSpirvBinary(FileBuffer->getBuffer().str()); |
| 92 | +} |
| 93 | + |
| 94 | +static bool isLLVMIRBinary(const std::string &File) { |
| 95 | + if (File.size() < sizeof(unsigned)) |
| 96 | + return false; |
| 97 | + |
| 98 | + StringRef Ext = llvm::sys::path::has_extension(File) |
| 99 | + ? llvm::sys::path::extension(File).drop_front() |
| 100 | + : ""; |
| 101 | + llvm::file_magic Magic; |
| 102 | + llvm::identify_magic(File, Magic); |
| 103 | + |
| 104 | + // Only .bc and bitcode files are to be considered. |
| 105 | + return (Ext == "bc" || Magic == llvm::file_magic::bitcode); |
| 106 | +} |
| 107 | + |
| 108 | +static int checkInputFileIsAlreadyLLVM(const char *Argv0) { |
| 109 | + StringRef Ext = llvm::sys::path::has_extension(InputFilename) |
| 110 | + ? llvm::sys::path::extension(InputFilename).drop_front() |
| 111 | + : ""; |
| 112 | + if (Ext == "bc" || isLLVMIRBinary(InputFilename)) |
| 113 | + return copyInputToOutput(); |
| 114 | + if (Ext == "spv" || isSPIRVBinary(InputFilename)) |
| 115 | + return convertSPIRVToLLVMIR(Argv0); |
| 116 | + |
| 117 | + // We could not directly determine the input file, so we just copy it |
| 118 | + // to the output file. |
| 119 | + return copyInputToOutput(); |
| 120 | +} |
| 121 | + |
| 122 | +int main(int argc, char **argv) { |
| 123 | + InitLLVM X(argc, argv); |
| 124 | + |
| 125 | + LLVMContext Context; |
| 126 | + cl::ParseCommandLineOptions(argc, argv, "spirv-to-ir-wrapper\n"); |
| 127 | + |
| 128 | + if (InputFilename.empty()) |
| 129 | + error("No input file provided"); |
| 130 | + |
| 131 | + if (!llvm::sys::fs::exists(InputFilename)) |
| 132 | + error("Input file \'" + InputFilename + "\' not found"); |
| 133 | + |
| 134 | + if (Output.empty()) |
| 135 | + error("Output file not provided"); |
| 136 | + |
| 137 | + return checkInputFileIsAlreadyLLVM(argv[0]); |
| 138 | +} |
0 commit comments