LLVM 22.0.0git
SPIRVAPI.cpp
Go to the documentation of this file.
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 "SPIRVTargetMachine.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/Verifier.h"
23#include "llvm/Pass.h"
29#include <optional>
30#include <string>
31#include <vector>
32
33using namespace llvm;
34
35namespace {
36
37std::once_flag InitOnceFlag;
38void InitializeSPIRVTarget() {
39 std::call_once(InitOnceFlag, []() {
43 LLVMInitializeSPIRVAsmPrinter();
44 });
45}
46} // namespace
47
48namespace llvm {
49
50// The goal of this function is to facilitate integration of SPIRV Backend into
51// tools and libraries by means of exposing an API call that translate LLVM
52// module to SPIR-V and write results into a string as binary SPIR-V output,
53// providing diagnostics on fail and means of configuring translation.
54extern "C" LLVM_EXTERNAL_VISIBILITY bool
55SPIRVTranslate(Module *M, std::string &SpirvObj, std::string &ErrMsg,
56 const std::vector<std::string> &AllowExtNames,
57 llvm::CodeGenOptLevel OLevel, Triple TargetTriple) {
58 // Fallbacks for option values.
59 static const std::string DefaultTriple = "spirv64-unknown-unknown";
60 static const std::string DefaultMArch = "";
61
62 std::set<SPIRV::Extension::Extension> AllowedExtIds;
63 StringRef UnknownExt =
64 SPIRVExtensionsParser::checkExtensions(AllowExtNames, AllowedExtIds);
65 if (!UnknownExt.empty()) {
66 ErrMsg = "Unknown SPIR-V extension: " + UnknownExt.str();
67 return false;
68 }
69
70 // SPIR-V-specific target initialization.
71 InitializeSPIRVTarget();
72
73 if (TargetTriple.getTriple().empty()) {
74 TargetTriple.setTriple(DefaultTriple);
75 M->setTargetTriple(TargetTriple);
76 }
77 const Target *TheTarget =
78 TargetRegistry::lookupTarget(DefaultMArch, TargetTriple, ErrMsg);
79 if (!TheTarget)
80 return false;
81
82 // A call to codegen::InitTargetOptionsFromCodeGenFlags(TargetTriple)
83 // hits the following assertion: llvm/lib/CodeGen/CommandFlags.cpp:78:
84 // llvm::FPOpFusion::FPOpFusionMode llvm::codegen::getFuseFPOps(): Assertion
85 // `FuseFPOpsView && "RegisterCodeGenFlags not created."' failed.
87 std::optional<Reloc::Model> RM;
88 std::optional<CodeModel::Model> CM;
89 std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
90 TargetTriple, "", "", Options, RM, CM, OLevel));
91 if (!Target) {
92 ErrMsg = "Could not allocate target machine!";
93 return false;
94 }
95
96 // Set available extensions.
97 SPIRVTargetMachine *STM = static_cast<SPIRVTargetMachine *>(Target.get());
98 const_cast<SPIRVSubtarget *>(STM->getSubtargetImpl())
99 ->initAvailableExtensions(AllowedExtIds);
100
101 if (M->getCodeModel())
102 Target->setCodeModel(*M->getCodeModel());
103
104 std::string DLStr = M->getDataLayoutStr();
106 DLStr.empty() ? Target->createDataLayout().getStringRepresentation()
107 : DLStr);
108 if (!MaybeDL) {
109 ErrMsg = toString(MaybeDL.takeError());
110 return false;
111 }
112 M->setDataLayout(MaybeDL.get());
113
114 TargetLibraryInfoImpl TLII(M->getTargetTriple());
116 PM.add(new TargetLibraryInfoWrapperPass(TLII));
117 std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP(
119 Target->getObjFileLowering()->Initialize(MMIWP->getMMI().getContext(),
120 *Target);
121
122 SmallString<4096> OutBuffer;
123 raw_svector_ostream OutStream(OutBuffer);
124 if (Target->addPassesToEmitFile(PM, OutStream, nullptr,
126 ErrMsg = "Target machine cannot emit a file of this type";
127 return false;
128 }
129
130 PM.run(*M);
131 SpirvObj = OutBuffer.str();
132
133 return true;
134}
135
136// TODO: Remove this wrapper after existing clients switch into a newer
137// implementation of SPIRVTranslate().
138extern "C" LLVM_EXTERNAL_VISIBILITY bool
139SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
140 const std::vector<std::string> &AllowExtNames,
141 const std::vector<std::string> &Opts) {
142 // optional: Opts[0] is a string representation of Triple,
143 // take Module triple otherwise
144 Triple TargetTriple = Opts.empty() || Opts[0].empty()
145 ? M->getTargetTriple()
146 : Triple(Triple::normalize(Opts[0]));
147 // optional: Opts[1] is a string representation of CodeGenOptLevel,
148 // no optimization otherwise
150 if (Opts.size() > 1 && !Opts[1].empty()) {
151 if (auto Level = CodeGenOpt::parseLevel(Opts[1][0])) {
152 OLevel = *Level;
153 } else {
154 ErrMsg = "Invalid optimization level!";
155 return false;
156 }
157 }
158 return SPIRVTranslate(M, SpirvObj, ErrMsg, AllowExtNames, OLevel,
159 std::move(TargetTriple));
160}
161
162} // namespace llvm
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
Module.h This file contains the declarations for the Module class.
static LVOptions Options
Definition: LVOptions.cpp:25
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetMC()
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTargetInfo()
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget()
Target-Independent Code Generator Pass Configuration Options pass.
static LLVM_ABI Expected< DataLayout > parse(StringRef LayoutString)
Parse a data layout string and return the layout.
Definition: DataLayout.cpp:263
Tagged union holding either a T or a Error.
Definition: Error.h:485
Error takeError()
Take ownership of the stored error.
Definition: Error.h:612
reference get()
Returns a reference to the stored T value.
Definition: Error.h:582
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
const SPIRVSubtarget * getSubtargetImpl() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:233
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
Implementation of the target library information.
Target - Wrapper for Target specific information.
TargetMachine * createTargetMachine(const Triple &TT, StringRef CPU, StringRef Features, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM=std::nullopt, CodeGenOptLevel OL=CodeGenOptLevel::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
LLVM_ABI void setTriple(const Twine &Str)
Set all components to the new triple Str.
Definition: Triple.cpp:1621
static LLVM_ABI std::string normalize(StringRef Str, CanonicalForm Form=CanonicalForm::ANY)
Turn an arbitrary machine specification into the canonical triple form (or something sensible that th...
Definition: Triple.cpp:1152
const std::string & getTriple() const
Definition: Triple.h:477
bool empty() const
Whether the triple is empty / default constructed.
Definition: Triple.h:480
PassManager manages ModulePassManagers.
void add(Pass *P) override
Add a pass to the queue of passes to run.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:692
std::optional< CodeGenOptLevel > parseLevel(char C)
Parse C as a single digit integer and get matching CodeGenLevel.
Definition: CodeGen.h:101
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_EXTERNAL_VISIBILITY bool SPIRVTranslate(Module *M, std::string &SpirvObj, std::string &ErrMsg, const std::vector< std::string > &AllowExtNames, llvm::CodeGenOptLevel OLevel, Triple TargetTriple)
Definition: SPIRVAPI.cpp:55
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:82
LLVM_EXTERNAL_VISIBILITY bool SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg, const std::vector< std::string > &AllowExtNames, const std::vector< std::string > &Opts)
Definition: SPIRVAPI.cpp:139
const char * toString(DWARFSectionKind Kind)
static StringRef checkExtensions(const std::vector< std::string > &ExtNames, std::set< SPIRV::Extension::Extension > &AllowedExtensions)
Validates and converts extension names into internal enum values.
static const Target * lookupTarget(StringRef TripleStr, std::string &Error)
lookupTarget - Lookup a target based on a target triple.