Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/include/LLVMSPIRVLib.h

raw
1 //===- LLVMSPIRVLib.h - Read and write SPIR-V binary ------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 /// \file LLVMSPIRVLib.h
35 ///
36 /// This files declares functions and passes for translating between LLVM and
37 /// SPIR-V.
38 ///
39 ///
40 //===----------------------------------------------------------------------===//
41 #ifndef SPIRV_H
42 #define SPIRV_H
43
44 #include "LLVMSPIRVOpts.h"
45
46 #include <iostream>
47 #include <string>
48
49 namespace llvm {
50 // Pass initialization functions need to be declared before inclusion of
51 // PassSupport.h.
52 class PassRegistry;
53 void initializeLLVMToSPIRVLegacyPass(PassRegistry &);
54 void initializeOCLToSPIRVLegacyPass(PassRegistry &);
55 void initializeOCLTypeToSPIRVLegacyPass(PassRegistry &);
56 void initializeSPIRVLowerBoolLegacyPass(PassRegistry &);
57 void initializeSPIRVLowerConstExprLegacyPass(PassRegistry &);
58 void initializeSPIRVLowerOCLBlocksLegacyPass(PassRegistry &);
59 void initializeSPIRVLowerMemmoveLegacyPass(PassRegistry &);
60 void initializeSPIRVLowerSaddWithOverflowLegacyPass(PassRegistry &);
61 void initializeSPIRVRegularizeLLVMLegacyPass(PassRegistry &);
62 void initializeSPIRVToOCL12LegacyPass(PassRegistry &);
63 void initializeSPIRVToOCL20LegacyPass(PassRegistry &);
64 void initializePreprocessMetadataLegacyPass(PassRegistry &);
65 void initializeSPIRVLowerBitCastToNonStandardTypeLegacyPass(PassRegistry &);
66
67 class ModulePass;
68 class FunctionPass;
69 } // namespace llvm
70
71 #include "llvm/IR/Module.h"
72
73 namespace SPIRV {
74
75 class SPIRVModule;
76
77 /// \brief Check if a string contains SPIR-V binary.
78 bool isSpirvBinary(const std::string &Img);
79
80 #ifdef _SPIRV_SUPPORT_TEXT_FMT
81 /// \brief Convert SPIR-V between binary and internal textual formats.
82 /// This function is not thread safe and should not be used in multi-thread
83 /// applications unless guarded by a critical section.
84 /// \returns true if succeeds.
85 bool convertSpirv(std::istream &IS, std::ostream &OS, std::string &ErrMsg,
86 bool FromText, bool ToText);
87
88 /// \brief Convert SPIR-V between binary and internal text formats.
89 /// This function is not thread safe and should not be used in multi-thread
90 /// applications unless guarded by a critical section.
91 bool convertSpirv(std::string &Input, std::string &Out, std::string &ErrMsg,
92 bool ToText);
93
94 /// \brief Check if a string contains SPIR-V in internal text format.
95 bool isSpirvText(std::string &Img);
96 #endif
97
98 /// \brief Load SPIR-V from istream as a SPIRVModule.
99 /// \returns null on failure.
100 std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
101 std::string &ErrMsg);
102
103 /// \brief Load SPIR-V from istream as a SPIRVModule.
104 /// \returns null on failure.
105 std::unique_ptr<SPIRVModule> readSpirvModule(std::istream &IS,
106 const SPIRV::TranslatorOpts &Opts,
107 std::string &ErrMsg);
108
109 struct SPIRVModuleReport {
110 SPIRV::VersionNumber Version;
111 uint32_t MemoryModel;
112 uint32_t AddrModel;
113 std::vector<std::string> Extensions;
114 std::vector<std::string> ExtendedInstructionSets;
115 std::vector<uint32_t> Capabilities;
116 };
117 /// \brief Partially load SPIR-V from the stream and decode only selected
118 /// instructions that are needed to retrieve general information
119 /// about the module. If this call fails, readSPIRVModule is
120 /// expected to fail as well.
121 /// \returns nullopt on failure.
122 std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS);
123 std::optional<SPIRVModuleReport> getSpirvReport(std::istream &IS, int &ErrCode);
124
125 struct SPIRVModuleTextReport {
126 std::string Version;
127 std::string MemoryModel;
128 std::string AddrModel;
129 std::vector<std::string> Extensions;
130 std::vector<std::string> ExtendedInstructionSets;
131 std::vector<std::string> Capabilities;
132 };
133 /// \brief Create a human-readable form of the report returned by a call to
134 /// getSpirvReport by decoding its binary fields.
135 /// \returns String with the human-readable report.
136 SPIRVModuleTextReport formatSpirvReport(const SPIRVModuleReport &Report);
137
138 } // End namespace SPIRV
139
140 namespace llvm {
141
142 /// \brief Translate LLVM module to SPIR-V and write to ostream.
143 /// \returns true if succeeds.
144 bool writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg);
145
146 /// \brief Load SPIR-V from istream and translate to LLVM module.
147 /// \returns true if succeeds.
148 bool readSpirv(LLVMContext &C, std::istream &IS, Module *&M,
149 std::string &ErrMsg);
150
151 /// \brief Translate LLVM module to SPIR-V and write to ostream.
152 /// \returns true if succeeds.
153 bool writeSpirv(Module *M, const SPIRV::TranslatorOpts &Opts, std::ostream &OS,
154 std::string &ErrMsg);
155
156 /// \brief Load SPIR-V from istream and translate to LLVM module.
157 /// \returns true if succeeds.
158 bool readSpirv(LLVMContext &C, const SPIRV::TranslatorOpts &Opts,
159 std::istream &IS, Module *&M, std::string &ErrMsg);
160
161 /// \brief Partially load SPIR-V from the stream and decode only instructions
162 /// needed to get information about specialization constants.
163 /// \returns true if succeeds.
164 struct SpecConstInfoTy {
165 uint32_t ID;
166 uint32_t Size;
167 std::string Type;
168 };
169 bool getSpecConstInfo(std::istream &IS,
170 std::vector<SpecConstInfoTy> &SpecConstInfo);
171
172 /// \brief Convert a SPIRVModule into LLVM IR.
173 /// \returns null on failure.
174 std::unique_ptr<Module>
175 convertSpirvToLLVM(LLVMContext &C, SPIRV::SPIRVModule &BM, std::string &ErrMsg);
176
177 /// \brief Convert a SPIRVModule into LLVM IR using specified options
178 /// \returns null on failure.
179 std::unique_ptr<Module> convertSpirvToLLVM(LLVMContext &C,
180 SPIRV::SPIRVModule &BM,
181 const SPIRV::TranslatorOpts &Opts,
182 std::string &ErrMsg);
183
184 /// \brief Regularize LLVM module by removing entities not representable by
185 /// SPIRV.
186 bool regularizeLlvmForSpirv(Module *M, std::string &ErrMsg);
187
188 bool regularizeLlvmForSpirv(Module *M, std::string &ErrMsg,
189 const SPIRV::TranslatorOpts &Opts);
190
191 /// \brief Mangle OpenCL builtin function function name.
192 /// If any type in ArgTypes is a pointer type, it should be represented as a
193 /// TypedPointerType instead, to faithfully represent the pointer element types
194 /// for name mangling.
195 void mangleOpenClBuiltin(const std::string &UnmangledName,
196 ArrayRef<Type *> ArgTypes, std::string &MangledName);
197
198 /// Create a pass for translating LLVM to SPIR-V.
199 ModulePass *createLLVMToSPIRVLegacy(SPIRV::SPIRVModule *);
200
201 /// Create a pass for translating OCL C builtin functions to SPIR-V builtin
202 /// functions.
203 ModulePass *createOCLToSPIRVLegacy();
204
205 /// Create a pass for adapting OCL types for SPIRV.
206 ModulePass *createOCLTypeToSPIRVLegacy();
207
208 /// Create a pass for lowering cast instructions of i1 type.
209 ModulePass *createSPIRVLowerBoolLegacy();
210
211 /// Create a pass for lowering constant expressions to instructions.
212 ModulePass *createSPIRVLowerConstExprLegacy();
213
214 /// Create a pass for removing function pointers related to OCL 2.0 blocks
215 ModulePass *createSPIRVLowerOCLBlocksLegacy();
216
217 /// Create a pass for lowering llvm.memmove to llvm.memcpys with a temporary
218 /// variable.
219 ModulePass *createSPIRVLowerMemmoveLegacy();
220
221 /// Create a pass for lowering llvm.sadd.with.overflow
222 ModulePass *createSPIRVLowerSaddWithOverflowLegacy();
223
224 /// Create a pass for regularize LLVM module to be translated to SPIR-V.
225 ModulePass *createSPIRVRegularizeLLVMLegacy();
226
227 /// Create a pass for translating SPIR-V Instructions to desired
228 /// representation in LLVM IR (OpenCL built-ins, SPIR-V Friendly IR, etc.)
229 ModulePass *createSPIRVBIsLoweringPass(Module &, SPIRV::BIsRepresentation);
230
231 /// Create a pass for translating SPIR-V builtin functions to OCL 1.2 builtin
232 /// functions.
233 ModulePass *createSPIRVToOCL12Legacy();
234
235 /// Create a pass for translating SPIR-V builtin functions to OCL 2.0 builtin
236 /// functions.
237 ModulePass *createSPIRVToOCL20Legacy();
238
239 /// Create a pass for translating SPIR 1.2/2.0 metadata to SPIR-V friendly
240 /// metadata.
241 ModulePass *createPreprocessMetadataLegacy();
242
243 /// Create and return a pass that writes the module to the specified
244 /// ostream.
245 ModulePass *createSPIRVWriterPass(std::ostream &Str);
246
247 /// Create and return a pass that writes the module to the specified
248 /// ostream.
249 ModulePass *createSPIRVWriterPass(std::ostream &Str,
250 const SPIRV::TranslatorOpts &Opts);
251
252 /// Create a pass for removing bitcast instructions to non-standard SPIR-V
253 /// types
254 FunctionPass *createSPIRVLowerBitCastToNonStandardTypeLegacy(
255 const SPIRV::TranslatorOpts &Opts);
256
257 } // namespace llvm
258
259 #endif // SPIRV_H