Debian Codegraph @

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

raw
1 //===- LLVMSPIRVOpts.h - Specify options for translation --------*- 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) 2019 Intel Corporation. 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 LLVMSPIRVOpts.h
35 ///
36 /// This files declares helper classes to handle SPIR-V versions and extensions.
37 ///
38 //===----------------------------------------------------------------------===//
39 #ifndef SPIRV_LLVMSPIRVOPTS_H
40 #define SPIRV_LLVMSPIRVOPTS_H
41
42 #include <llvm/ADT/SmallVector.h>
43 #include <llvm/ADT/StringRef.h>
44
45 #include <cassert>
46 #include <cstdint>
47 #include <map>
48 #include <optional>
49 #include <unordered_map>
50
51 namespace llvm {
52 class IntrinsicInst;
53 } // namespace llvm
54
55 namespace SPIRV {
56
57 /// SPIR-V versions known to translator.
58 enum class VersionNumber : uint32_t {
59 // See section 2.3 of SPIR-V spec: Physical Layout of a SPIR_V Module and
60 // Instruction
61 SPIRV_1_0 = 0x00010000,
62 SPIRV_1_1 = 0x00010100,
63 SPIRV_1_2 = 0x00010200,
64 SPIRV_1_3 = 0x00010300,
65 SPIRV_1_4 = 0x00010400,
66 SPIRV_1_5 = 0x00010500,
67 SPIRV_1_6 = 0x00010600,
68 MinimumVersion = SPIRV_1_0,
69 MaximumVersion = SPIRV_1_6
70 };
71
72 inline constexpr std::string_view formatVersionNumber(uint32_t Version) {
73 switch (Version) {
74 case static_cast<uint32_t>(VersionNumber::SPIRV_1_0):
75 return "1.0";
76 case static_cast<uint32_t>(VersionNumber::SPIRV_1_1):
77 return "1.1";
78 case static_cast<uint32_t>(VersionNumber::SPIRV_1_2):
79 return "1.2";
80 case static_cast<uint32_t>(VersionNumber::SPIRV_1_3):
81 return "1.3";
82 case static_cast<uint32_t>(VersionNumber::SPIRV_1_4):
83 return "1.4";
84 }
85 return "unknown";
86 }
87
88 inline bool isSPIRVVersionKnown(uint32_t Ver) {
89 return Ver >= static_cast<uint32_t>(VersionNumber::MinimumVersion) &&
90 Ver <= static_cast<uint32_t>(VersionNumber::MaximumVersion);
91 }
92
93 enum class ExtensionID : uint32_t {
94 First,
95 #define EXT(X) X,
96 #include "LLVMSPIRVExtensions.inc"
97 #undef EXT
98 Last,
99 };
100
101 enum class BIsRepresentation : uint32_t { OpenCL12, OpenCL20, SPIRVFriendlyIR };
102
103 enum class FPContractMode : uint32_t { On, Off, Fast };
104
105 enum class DebugInfoEIS : uint32_t {
106 SPIRV_Debug,
107 OpenCL_DebugInfo_100,
108 NonSemantic_Shader_DebugInfo_100,
109 NonSemantic_Shader_DebugInfo_200
110 };
111
112 enum class BuiltinFormat : uint32_t { Function, Global };
113
114 /// \brief Helper class to manage SPIR-V translation
115 class TranslatorOpts {
116 public:
117 // Unset optional means not directly specified by user
118 using ExtensionsStatusMap = std::map<ExtensionID, std::optional<bool>>;
119
120 using ArgList = llvm::SmallVector<llvm::StringRef, 4>;
121
122 TranslatorOpts() = default;
123
124 TranslatorOpts(VersionNumber Max, const ExtensionsStatusMap &Map = {})
125 : MaxVersion(Max), ExtStatusMap(Map) {}
126
127 bool isAllowedToUseVersion(VersionNumber RequestedVersion) const {
128 return RequestedVersion <= MaxVersion;
129 }
130
131 bool isAllowedToUseExtension(ExtensionID Extension) const {
132 auto I = ExtStatusMap.find(Extension);
133 if (ExtStatusMap.end() == I)
134 return false;
135
136 return I->second && *I->second;
137 }
138
139 void setAllowedToUseExtension(ExtensionID Extension, bool Allow = true) {
140 // Only allow using the extension if it has not already been disabled
141 auto I = ExtStatusMap.find(Extension);
142 if (I == ExtStatusMap.end() || !I->second || (*I->second) == true)
143 ExtStatusMap[Extension] = Allow;
144 }
145
146 VersionNumber getMaxVersion() const { return MaxVersion; }
147
148 bool isGenArgNameMDEnabled() const { return GenKernelArgNameMD; }
149
150 bool isSPIRVMemToRegEnabled() const { return SPIRVMemToReg; }
151
152 void setMemToRegEnabled(bool Mem2Reg) { SPIRVMemToReg = Mem2Reg; }
153
154 bool preserveAuxData() const { return PreserveAuxData; }
155
156 void setPreserveAuxData(bool ArgValue) { PreserveAuxData = ArgValue; }
157
158 void setGenKernelArgNameMDEnabled(bool ArgNameMD) {
159 GenKernelArgNameMD = ArgNameMD;
160 }
161
162 void enableAllExtensions() {
163 #define EXT(X) ExtStatusMap[ExtensionID::X] = true;
164 #include "LLVMSPIRVExtensions.inc"
165 #undef EXT
166 }
167
168 void enableGenArgNameMD() { GenKernelArgNameMD = true; }
169
170 void setSpecConst(uint32_t SpecId, uint64_t SpecValue) {
171 ExternalSpecialization[SpecId] = SpecValue;
172 }
173
174 bool getSpecializationConstant(uint32_t SpecId, uint64_t &Value) const {
175 auto It = ExternalSpecialization.find(SpecId);
176 if (It == ExternalSpecialization.end())
177 return false;
178 Value = It->second;
179 return true;
180 }
181
182 void setDesiredBIsRepresentation(BIsRepresentation Value) {
183 DesiredRepresentationOfBIs = Value;
184 }
185
186 BIsRepresentation getDesiredBIsRepresentation() const {
187 return DesiredRepresentationOfBIs;
188 }
189
190 void setFPContractMode(FPContractMode Mode) { FPCMode = Mode; }
191
192 FPContractMode getFPContractMode() const { return FPCMode; }
193
194 bool isUnknownIntrinsicAllowed(llvm::IntrinsicInst *II) const noexcept;
195 bool isSPIRVAllowUnknownIntrinsicsEnabled() const noexcept;
196 void setSPIRVAllowUnknownIntrinsics(ArgList IntrinsicPrefixList) noexcept;
197
198 bool allowExtraDIExpressions() const noexcept {
199 return AllowExtraDIExpressions;
200 }
201
202 void setAllowExtraDIExpressionsEnabled(bool Allow) noexcept {
203 AllowExtraDIExpressions = Allow;
204 }
205
206 DebugInfoEIS getDebugInfoEIS() const { return DebugInfoVersion; }
207
208 void setDebugInfoEIS(DebugInfoEIS EIS) { DebugInfoVersion = EIS; }
209
210 bool shouldReplaceLLVMFmulAddWithOpenCLMad() const noexcept {
211 return ReplaceLLVMFmulAddWithOpenCLMad;
212 }
213
214 void setReplaceLLVMFmulAddWithOpenCLMad(bool Value) noexcept {
215 ReplaceLLVMFmulAddWithOpenCLMad = Value;
216 }
217
218 bool shouldPreserveOCLKernelArgTypeMetadataThroughString() const noexcept {
219 return PreserveOCLKernelArgTypeMetadataThroughString;
220 }
221
222 void setPreserveOCLKernelArgTypeMetadataThroughString(bool Value) noexcept {
223 PreserveOCLKernelArgTypeMetadataThroughString = Value;
224 }
225
226 bool shouldEmitFunctionPtrAddrSpace() const noexcept {
227 return EmitFunctionPtrAddrSpace;
228 }
229
230 void setEmitFunctionPtrAddrSpace(bool Value) noexcept {
231 EmitFunctionPtrAddrSpace = Value;
232 }
233
234 void setBuiltinFormat(BuiltinFormat Value) noexcept {
235 SPIRVBuiltinFormat = Value;
236 }
237 BuiltinFormat getBuiltinFormat() const noexcept { return SPIRVBuiltinFormat; }
238
239 void setFnVarCategory(uint32_t Category) noexcept {
240 FnVarCategory = Category;
241 }
242 std::optional<uint32_t> getFnVarCategory() const noexcept {
243 return FnVarCategory;
244 }
245
246 void setFnVarFamily(uint32_t Family) noexcept { FnVarFamily = Family; }
247 std::optional<uint32_t> getFnVarFamily() const noexcept {
248 return FnVarFamily;
249 }
250
251 void setFnVarArch(uint32_t Arch) noexcept { FnVarArch = Arch; }
252 std::optional<uint32_t> getFnVarArch() const noexcept { return FnVarArch; }
253
254 void setFnVarTarget(uint32_t Target) noexcept { FnVarTarget = Target; }
255 std::optional<uint32_t> getFnVarTarget() const noexcept {
256 return FnVarTarget;
257 }
258
259 void setFnVarFeatures(std::vector<uint32_t> Features) noexcept {
260 FnVarFeatures = Features;
261 }
262 std::vector<uint32_t> getFnVarFeatures() const noexcept {
263 return FnVarFeatures;
264 }
265
266 void setFnVarCapabilities(std::vector<uint32_t> Capabilities) noexcept {
267 FnVarCapabilities = Capabilities;
268 }
269 std::vector<uint32_t> getFnVarCapabilities() const noexcept {
270 return FnVarCapabilities;
271 }
272
273 void setFnVarSpecEnable(bool Val) noexcept { FnVarSpecEnable = Val; }
274 bool getFnVarSpecEnable() const noexcept { return FnVarSpecEnable; }
275
276 void setFnVarSpvOut(std::string Val) noexcept { FnVarSpvOut = Val; }
277 std::string getFnVarSpvOut() const noexcept { return FnVarSpvOut; }
278
279 // Check that options passed to --fnvar-xxx flags make sense. Return true on
280 // success, false on failure.
281 bool validateFnVarOpts() const;
282
283 private:
284 // Common translation options
285 VersionNumber MaxVersion = VersionNumber::MaximumVersion;
286 ExtensionsStatusMap ExtStatusMap;
287 // SPIRVMemToReg option affects LLVM IR regularization phase
288 bool SPIRVMemToReg = false;
289 // SPIR-V to LLVM translation options
290 bool GenKernelArgNameMD = false;
291 std::unordered_map<uint32_t, uint64_t> ExternalSpecialization;
292 // Representation of built-ins, which should be used while translating from
293 // SPIR-V to back to LLVM IR
294 BIsRepresentation DesiredRepresentationOfBIs = BIsRepresentation::OpenCL12;
295 // Controls floating point contraction.
296 //
297 // - FPContractMode::On allows to choose a mode according to
298 // presence of fused LLVM intrinsics
299 //
300 // - FPContractMode::Off disables contratction for all entry points
301 //
302 // - FPContractMode::Fast allows *all* operations to be contracted
303 // for all entry points
304 FPContractMode FPCMode = FPContractMode::On;
305
306 // Unknown LLVM intrinsics will be translated as external function calls in
307 // SPIR-V
308 std::optional<ArgList> SPIRVAllowUnknownIntrinsics{};
309
310 // Enable support for extra DIExpression opcodes not listed in the SPIR-V
311 // DebugInfo specification.
312 bool AllowExtraDIExpressions = false;
313
314 DebugInfoEIS DebugInfoVersion = DebugInfoEIS::OpenCL_DebugInfo_100;
315
316 // Controls whether llvm.fmuladd.* should be replaced with mad from OpenCL
317 // extended instruction set or with a simple fmul + fadd
318 bool ReplaceLLVMFmulAddWithOpenCLMad = true;
319
320 // Add a workaround to preserve OpenCL kernel_arg_type and
321 // kernel_arg_type_qual metadata through OpString
322 bool PreserveOCLKernelArgTypeMetadataThroughString = false;
323
324 // Controls if CodeSectionINTEL can be emitted and consumed with a dedicated
325 // address space
326 bool EmitFunctionPtrAddrSpace = false;
327
328 bool PreserveAuxData = false;
329
330 std::optional<uint32_t> FnVarCategory = std::nullopt;
331 std::optional<uint32_t> FnVarFamily = std::nullopt;
332 std::optional<uint32_t> FnVarArch = std::nullopt;
333 std::optional<uint32_t> FnVarTarget = std::nullopt;
334 std::vector<uint32_t> FnVarFeatures = {};
335 std::vector<uint32_t> FnVarCapabilities = {};
336 std::string FnVarSpvOut = "";
337 bool FnVarSpecEnable = false;
338
339 BuiltinFormat SPIRVBuiltinFormat = BuiltinFormat::Function;
340 };
341
342 } // namespace SPIRV
343
344 #endif // SPIRV_LLVMSPIRVOPTS_H