|
| 1 | +//===-- SPIRVAPI.cpp - SPIR-V Backend API ---------------------*- 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 | +#include "SPIRVCommandLine.h" |
| 10 | +#include "SPIRVSubtarget.h" |
| 11 | +#include "llvm/Analysis/TargetLibraryInfo.h" |
| 12 | +#include "llvm/CodeGen/CommandFlags.h" |
| 13 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 14 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 15 | +#include "llvm/CodeGen/TargetPassConfig.h" |
| 16 | +#include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 17 | +#include "llvm/IR/DataLayout.h" |
| 18 | +#include "llvm/IR/LLVMContext.h" |
| 19 | +#include "llvm/IR/LegacyPassManager.h" |
| 20 | +#include "llvm/IR/Module.h" |
| 21 | +#include "llvm/IR/Verifier.h" |
| 22 | +#include "llvm/InitializePasses.h" |
| 23 | +#include "llvm/MC/MCTargetOptionsCommandFlags.h" |
| 24 | +#include "llvm/MC/TargetRegistry.h" |
| 25 | +#include "llvm/Pass.h" |
| 26 | +#include "llvm/Support/CommandLine.h" |
| 27 | +#include "llvm/Support/FormattedStream.h" |
| 28 | +#include "llvm/Support/InitLLVM.h" |
| 29 | +#include "llvm/Support/TargetSelect.h" |
| 30 | +#include "llvm/Target/TargetLoweringObjectFile.h" |
| 31 | +#include "llvm/Target/TargetMachine.h" |
| 32 | +#include "llvm/TargetParser/SubtargetFeature.h" |
| 33 | +#include "llvm/TargetParser/Triple.h" |
| 34 | +#include <optional> |
| 35 | +#include <string> |
| 36 | +#include <utility> |
| 37 | +#include <vector> |
| 38 | + |
| 39 | +using namespace llvm; |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +// Mimic limited number of command line flags from llc to provide a better |
| 44 | +// user experience when passing options into the translate API call. |
| 45 | +static cl::opt<char> SpvOptLevel(" O", cl::Hidden, cl::Prefix, cl::init('0')); |
| 46 | +static cl::opt<std::string> SpvTargetTriple(" mtriple", cl::Hidden, |
| 47 | + cl::init("")); |
| 48 | + |
| 49 | +// Utility to accept options in a command line style. |
| 50 | +void parseSPIRVCommandLineOptions(const std::vector<std::string> &Options, |
| 51 | + raw_ostream *Errs) { |
| 52 | + static constexpr const char *Origin = "SPIRVTranslateModule"; |
| 53 | + if (!Options.empty()) { |
| 54 | + std::vector<const char *> Argv(1, Origin); |
| 55 | + for (const auto &Arg : Options) |
| 56 | + Argv.push_back(Arg.c_str()); |
| 57 | + cl::ParseCommandLineOptions(Argv.size(), Argv.data(), Origin, Errs); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +std::once_flag InitOnceFlag; |
| 62 | +void InitializeSPIRVTarget() { |
| 63 | + std::call_once(InitOnceFlag, []() { |
| 64 | + LLVMInitializeSPIRVTargetInfo(); |
| 65 | + LLVMInitializeSPIRVTarget(); |
| 66 | + LLVMInitializeSPIRVTargetMC(); |
| 67 | + LLVMInitializeSPIRVAsmPrinter(); |
| 68 | + }); |
| 69 | +} |
| 70 | +} // namespace |
| 71 | + |
| 72 | +namespace llvm { |
| 73 | + |
| 74 | +// The goal of this function is to facilitate integration of SPIRV Backend into |
| 75 | +// tools and libraries by means of exposing an API call that translate LLVM |
| 76 | +// module to SPIR-V and write results into a string as binary SPIR-V output, |
| 77 | +// providing diagnostics on fail and means of configuring translation in a style |
| 78 | +// of command line options. |
| 79 | +extern "C" LLVM_EXTERNAL_VISIBILITY bool |
| 80 | +SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg, |
| 81 | + const std::vector<std::string> &AllowExtNames, |
| 82 | + const std::vector<std::string> &Opts) { |
| 83 | + // Fallbacks for option values. |
| 84 | + static const std::string DefaultTriple = "spirv64-unknown-unknown"; |
| 85 | + static const std::string DefaultMArch = ""; |
| 86 | + |
| 87 | + // Parse Opts as if it'd be command line arguments. |
| 88 | + std::string Errors; |
| 89 | + raw_string_ostream ErrorStream(Errors); |
| 90 | + parseSPIRVCommandLineOptions(Opts, &ErrorStream); |
| 91 | + if (!Errors.empty()) { |
| 92 | + ErrMsg = Errors; |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + llvm::CodeGenOptLevel OLevel; |
| 97 | + if (auto Level = CodeGenOpt::parseLevel(SpvOptLevel)) { |
| 98 | + OLevel = *Level; |
| 99 | + } else { |
| 100 | + ErrMsg = "Invalid optimization level!"; |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // Overrides/ammends `-spirv-ext` command line switch (if present) by the |
| 105 | + // explicit list of allowed SPIR-V extensions. |
| 106 | + std::set<SPIRV::Extension::Extension> AllowedExtIds; |
| 107 | + StringRef UnknownExt = |
| 108 | + SPIRVExtensionsParser::checkExtensions(AllowExtNames, AllowedExtIds); |
| 109 | + if (!UnknownExt.empty()) { |
| 110 | + ErrMsg = "Unknown SPIR-V extension: " + UnknownExt.str(); |
| 111 | + return false; |
| 112 | + } |
| 113 | + SPIRVSubtarget::addExtensionsToClOpt(AllowedExtIds); |
| 114 | + |
| 115 | + // SPIR-V-specific target initialization. |
| 116 | + InitializeSPIRVTarget(); |
| 117 | + |
| 118 | + Triple TargetTriple(SpvTargetTriple.empty() |
| 119 | + ? M->getTargetTriple() |
| 120 | + : Triple::normalize(SpvTargetTriple)); |
| 121 | + if (TargetTriple.getTriple().empty()) { |
| 122 | + TargetTriple.setTriple(DefaultTriple); |
| 123 | + M->setTargetTriple(DefaultTriple); |
| 124 | + } |
| 125 | + const Target *TheTarget = |
| 126 | + TargetRegistry::lookupTarget(DefaultMArch, TargetTriple, ErrMsg); |
| 127 | + if (!TheTarget) |
| 128 | + return false; |
| 129 | + |
| 130 | + // A call to codegen::InitTargetOptionsFromCodeGenFlags(TargetTriple) |
| 131 | + // hits the following assertion: llvm/lib/CodeGen/CommandFlags.cpp:78: |
| 132 | + // llvm::FPOpFusion::FPOpFusionMode llvm::codegen::getFuseFPOps(): Assertion |
| 133 | + // `FuseFPOpsView && "RegisterCodeGenFlags not created."' failed. |
| 134 | + TargetOptions Options; |
| 135 | + std::optional<Reloc::Model> RM; |
| 136 | + std::optional<CodeModel::Model> CM; |
| 137 | + std::unique_ptr<TargetMachine> Target = |
| 138 | + std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 139 | + TargetTriple.getTriple(), "", "", Options, RM, CM, OLevel)); |
| 140 | + if (!Target) { |
| 141 | + ErrMsg = "Could not allocate target machine!"; |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + if (M->getCodeModel()) |
| 146 | + Target->setCodeModel(*M->getCodeModel()); |
| 147 | + |
| 148 | + std::string DLStr = M->getDataLayoutStr(); |
| 149 | + Expected<DataLayout> MaybeDL = DataLayout::parse( |
| 150 | + DLStr.empty() ? Target->createDataLayout().getStringRepresentation() |
| 151 | + : DLStr); |
| 152 | + if (!MaybeDL) { |
| 153 | + ErrMsg = toString(MaybeDL.takeError()); |
| 154 | + return false; |
| 155 | + } |
| 156 | + M->setDataLayout(MaybeDL.get()); |
| 157 | + |
| 158 | + TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple())); |
| 159 | + legacy::PassManager PM; |
| 160 | + PM.add(new TargetLibraryInfoWrapperPass(TLII)); |
| 161 | + LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine &>(*Target); |
| 162 | + MachineModuleInfoWrapperPass *MMIWP = |
| 163 | + new MachineModuleInfoWrapperPass(&LLVMTM); |
| 164 | + const_cast<TargetLoweringObjectFile *>(LLVMTM.getObjFileLowering()) |
| 165 | + ->Initialize(MMIWP->getMMI().getContext(), *Target); |
| 166 | + |
| 167 | + SmallString<4096> OutBuffer; |
| 168 | + raw_svector_ostream OutStream(OutBuffer); |
| 169 | + if (Target->addPassesToEmitFile(PM, OutStream, nullptr, |
| 170 | + CodeGenFileType::ObjectFile)) { |
| 171 | + ErrMsg = "Target machine cannot emit a file of this type"; |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + PM.run(*M); |
| 176 | + SpirvObj = OutBuffer.str(); |
| 177 | + |
| 178 | + return true; |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace llvm |
0 commit comments