Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/libSPIRV/SPIRVFnVar.h

raw
1 //===- SPIRVFnVar.cpp -===//
2 //
3 // The LLVM/SPIRV Translator
4 //
5 // Copyright (c) 2025 The Khronos Group Inc.
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //===----------------------------------------------------------------------===//
20 /// \file
21 ///
22 /// This file defines entries from the SPV_INTEL_function_variants extension. It
23 /// also provides a function for specializing a multi-target module into a
24 /// targeted one.
25 ///
26 //===----------------------------------------------------------------------===//
27
28 #ifndef SPIRV_LIBSPIRV_SPIRVFNVAR_H
29 #define SPIRV_LIBSPIRV_SPIRVFNVAR_H
30
31 #include "SPIRVEntry.h"
32 #include "SPIRVInstruction.h"
33 #include "SPIRVValue.h"
34
35 #if _SPIRVDBG
36 #include <iomanip>
37 #endif
38
39 namespace SPIRV {
40
41 // Specialize multitarget module into a targeted one according to
42 // SPV_INTEL_function_variants.
43 //
44 // This is a SPIR-V to SPIR-V transform. After the transform, the module should
45 // not contain any notion of SPV_INTEL_function_variants and can be processed
46 // even by consumers that do not support this extension.
47 bool specializeFnVariants(SPIRVModule *BM, std::string &ErrMsg);
48
49 // Below are entries defined by the extension:
50
51 class SPIRVConditionalEntryPointINTEL : public SPIRVAnnotation {
52 public:
53 static const SPIRVWord FixedWC = 5;
54 SPIRVConditionalEntryPointINTEL(SPIRVModule *TheModule, SPIRVId Condition,
55 SPIRVExecutionModelKind TheExecModel,
56 SPIRVId TheId, const std::string &TheName,
57 std::vector<SPIRVId> Variables)
58 : SPIRVAnnotation(OpConditionalEntryPointINTEL,
59 TheModule->get<SPIRVFunction>(TheId),
60 getSizeInWords(TheName) + Variables.size() + 4),
61 Condition(Condition), ExecModel(TheExecModel), Name(TheName),
62 Variables(Variables) {}
63 SPIRVConditionalEntryPointINTEL()
64 : SPIRVAnnotation(OpConditionalEntryPointINTEL) {}
65
66 SPIRVId getCondition() const { return Condition; }
67 SPIRVExecutionModelKind getExecModel() const { return ExecModel; }
68 std::string getName() const { return Name; }
69 std::vector<SPIRVId> getVariables() const { return Variables; }
70
71 protected:
72 void encode(spv_ostream &O) const override {
73 getEncoder(O) << Condition << ExecModel << Target << Name << Variables;
74 }
75
76 void decode(std::istream &I) override {
77 getDecoder(I) >> Condition >> ExecModel >> Target >> Name;
78 Variables.resize(WordCount - FixedWC - getSizeInWords(Name) + 1);
79 getDecoder(I) >> Variables;
80 Module->setName(getOrCreateTarget(), Name);
81 Module->addConditionalEntryPoint(Condition, ExecModel, Target, Name,
82 Variables);
83 }
84
85 private:
86 SPIRVId Condition;
87 SPIRVExecutionModelKind ExecModel = ExecutionModelMax;
88 std::string Name;
89 std::vector<SPIRVId> Variables;
90 };
91
92 class SPIRVConditionalExtensionINTEL
93 : public SPIRVEntryNoId<OpConditionalExtensionINTEL> {
94 public:
95 SPIRVConditionalExtensionINTEL(SPIRVModule *M, SPIRVId C,
96 const std::string &SS)
97 : SPIRVEntryNoId(M, 2 + getSizeInWords(SS)), Condition(C), S(SS) {}
98 SPIRVConditionalExtensionINTEL() {}
99
100 std::string getExtensionName() const { return S; }
101 SPIRVId getCondition() const { return Condition; }
102
103 protected:
104 void encode(spv_ostream &O) const override {
105 getEncoder(O) << Condition << S;
106 }
107
108 void decode(std::istream &I) override {
109 getDecoder(I) >> Condition >> S;
110 Module->getConditionalExtensions().insert(std::make_pair(Condition, S));
111 }
112
113 private:
114 SPIRVId Condition;
115 std::string S;
116 };
117
118 class SPIRVConditionalCapabilityINTEL
119 : public SPIRVEntryNoId<OpConditionalCapabilityINTEL> {
120 public:
121 static const SPIRVWord FixedWC = 3;
122 SPIRVConditionalCapabilityINTEL(SPIRVModule *M, SPIRVId C,
123 SPIRVCapabilityKind K)
124 : SPIRVEntryNoId(M, 3), Condition(C), Kind(K) {
125 updateModuleVersion();
126 }
127 SPIRVConditionalCapabilityINTEL() {}
128
129 SPIRVId getCondition() const { return Condition; }
130
131 protected:
132 void encode(spv_ostream &O) const override {
133 getEncoder(O) << Condition << Kind;
134 }
135
136 void decode(std::istream &I) override {
137 getDecoder(I) >> Condition >> Kind;
138 Module->addConditionalCapability(Condition, Kind);
139 }
140
141 private:
142 SPIRVId Condition;
143 SPIRVCapabilityKind Kind;
144 };
145
146 class SPIRVConditionalCopyObjectINTEL : public SPIRVInstruction {
147 public:
148 const static Op OC = OpConditionalCopyObjectINTEL;
149 const static SPIRVWord FixedWordCount = 3;
150
151 // Complete constructor
152 SPIRVConditionalCopyObjectINTEL(SPIRVType *TheType, SPIRVId TheId,
153 const std::vector<SPIRVId> &TheConstituents,
154 SPIRVBasicBlock *TheBB)
155 : SPIRVInstruction(FixedWordCount + TheConstituents.size(), OC, TheType,
156 TheId, TheBB),
157 Constituents(TheConstituents) {
158 validate();
159 assert(TheBB && "Invalid BB");
160 }
161 // Incomplete constructor
162 SPIRVConditionalCopyObjectINTEL() : SPIRVInstruction(OC) {}
163
164 std::vector<SPIRVId> getOperandIds() { return Constituents; }
165
166 std::vector<SPIRVValue *> getOperands() override {
167 return getValues(Constituents);
168 }
169
170 protected:
171 void setWordCount(SPIRVWord TheWordCount) override {
172 SPIRVEntry::setWordCount(TheWordCount);
173 Constituents.resize(TheWordCount - FixedWordCount);
174 }
175 _SPIRV_DEF_ENCDEC3(Type, Id, Constituents)
176 void validate() const override {
177 SPIRVInstruction::validate();
178 size_t TypeOpCode = this->getType()->getOpCode();
179 assert(TypeOpCode != OpTypeVoid && "Conditional copy type cannot be void");
180 (void)(TypeOpCode);
181 assert(Constituents.size() % 2 == 0 &&
182 "Conditional copy requires condition-operand pairs");
183 assert(Constituents.size() >= 2 &&
184 "Conditional copy requires at least one condition-operand pair");
185 }
186 std::vector<SPIRVId> Constituents;
187 };
188
189 class SPIRVSpecConstantTargetINTEL : public SPIRVValue {
190 public:
191 constexpr static SPIRVWord FixedWC = 4;
192 constexpr static spv::Op OC = OpSpecConstantTargetINTEL;
193
194 // Complete constructor
195 SPIRVSpecConstantTargetINTEL(SPIRVModule *M, SPIRVType *TheType,
196 SPIRVId TheId, SPIRVWord TheTarget,
197 const std::vector<SPIRVWord> TheFeatures)
198 : SPIRVValue(M, TheFeatures.size() + FixedWC, OC, TheType, TheId) {
199 Features = TheFeatures;
200 Target = TheTarget;
201 NumWords = TheFeatures.size() + FixedWC;
202 validate();
203 }
204 // Incomplete constructor
205 SPIRVSpecConstantTargetINTEL() : SPIRVValue(OC) {}
206
207 SPIRVWord getTarget() const { return Target; }
208 bool matchesDevice() {
209 std::optional<SPIRVWord> DeviceTarget = getModule()->getFnVarTarget();
210 std::vector<SPIRVWord> DeviceFeatures = getModule()->getFnVarFeatures();
211 bool Res = true;
212 if (DeviceTarget != std::nullopt && DeviceTarget.value() != Target) {
213 Res = false;
214 }
215 if (!DeviceFeatures.empty()) {
216 for (const auto &Feature : Features) {
217 if (std::find(DeviceFeatures.cbegin(), DeviceFeatures.cend(),
218 Feature) == DeviceFeatures.cend()) {
219 Res = false;
220 }
221 }
222 }
223
224 SPIRVDBG(
225 spvdbgs() << "[FnVar] match instr Target: ";
226 spvdbgs() << std::setw(4) << Target;
227 spvdbgs() << ", Features:"; if (Features.empty()) {
228 spvdbgs() << " none";
229 } else {
230 for (const auto &Feat : Features) {
231 spvdbgs() << " " << Feat;
232 }
233 } spvdbgs() << " | ID: %"
234 << getId() << std::endl;
235 spvdbgs() << "[FnVar] device Target: "; if (DeviceTarget ==
236 std::nullopt) {
237 spvdbgs() << "none";
238 } else {
239 spvdbgs() << std::setw(4) << DeviceTarget.value();
240 } spvdbgs() << ", Features:";
241 for (const auto &Feat
242 : DeviceFeatures) { spvdbgs() << " " << Feat; } spvdbgs()
243 << std::endl;
244 spvdbgs() << "[FnVar] result: " << Res << std::endl;);
245
246 return Res;
247 }
248
249 protected:
250 _SPIRV_DEF_ENCDEC4(Type, Id, Target, Features);
251 void setWordCount(SPIRVWord WordCount) override {
252 SPIRVEntry::setWordCount(WordCount);
253 Features.resize(WordCount - FixedWC);
254 NumWords = WordCount - FixedWC;
255 }
256
257 private:
258 unsigned NumWords;
259 std::vector<SPIRVWord> Features;
260 SPIRVWord Target;
261 };
262
263 class SPIRVSpecConstantArchitectureINTEL : public SPIRVValue {
264 public:
265 constexpr static SPIRVWord FixedWC = 7;
266 constexpr static spv::Op OC = OpSpecConstantArchitectureINTEL;
267
268 // Complete constructor
269 SPIRVSpecConstantArchitectureINTEL(SPIRVModule *M, SPIRVType *TheType,
270 SPIRVId TheId, SPIRVWord TheCategory,
271 SPIRVWord TheFamily, spv::Op TheCmpOp,
272 SPIRVWord TheArchitecture)
273 : SPIRVValue(M, FixedWC, OC, TheType, TheId) {
274 Category = TheCategory;
275 Family = TheFamily;
276 CmpOp = TheCmpOp;
277 Architecture = TheArchitecture;
278 validate();
279 }
280 // Incomplete constructor
281 SPIRVSpecConstantArchitectureINTEL() : SPIRVValue(OC) {}
282
283 SPIRVWord getCategory() { return Category; }
284 SPIRVWord getFamily() { return Family; }
285 spv::Op getCmpOp() { return CmpOp; }
286 SPIRVWord getArchitecture() { return Architecture; }
287 bool matchesDevice() {
288 std::optional<SPIRVWord> DeviceCategory = getModule()->getFnVarCategory();
289 std::optional<SPIRVWord> DeviceFamily = getModule()->getFnVarFamily();
290 std::optional<SPIRVWord> DeviceArchitecture = getModule()->getFnVarArch();
291 bool Res = true;
292
293 if (DeviceCategory != std::nullopt && DeviceCategory.value() != Category) {
294 Res = false;
295 }
296 if (DeviceFamily != std::nullopt && DeviceFamily.value() != Family) {
297 Res = false;
298 }
299 if (DeviceArchitecture != std::nullopt) {
300 switch (CmpOp) {
301 case OpIEqual:
302 Res = DeviceArchitecture == Architecture;
303 break;
304 case OpINotEqual:
305 Res = DeviceArchitecture != Architecture;
306 break;
307 case OpULessThan:
308 Res = DeviceArchitecture < Architecture;
309 break;
310 case OpULessThanEqual:
311 Res = DeviceArchitecture <= Architecture;
312 break;
313 case OpUGreaterThan:
314 Res = DeviceArchitecture > Architecture;
315 break;
316 case OpUGreaterThanEqual:
317 Res = DeviceArchitecture >= Architecture;
318 break;
319 default:
320 assert(false && "Invalid checked CmpOp");
321 Res = false;
322 break;
323 }
324 }
325
326 SPIRVDBG(
327 spvdbgs() << "[FnVar] match instr Category: " << std::setw(4)
328 << Category
329
330 << ", Family: " << std::setw(4) << Family << ", CmpOp: "
331 << std::setw(4) << CmpOp << ", Architecture: " << std::setw(4)
332 << Architecture << " | ID: %" << getId() << std::endl;
333 spvdbgs() << "[FnVar] device Category: "; if (DeviceCategory ==
334 std::nullopt) {
335 spvdbgs() << "none";
336 } else {
337 spvdbgs() << std::setw(4) << DeviceCategory.value();
338 } spvdbgs() << ", Family: ";
339 if (DeviceFamily == std::nullopt) { spvdbgs() << "none"; } else {
340 spvdbgs() << std::setw(4) << DeviceFamily.value();
341 } spvdbgs()
342 << ", Architecture: ";
343 if (DeviceArchitecture == std::nullopt) { spvdbgs() << "none"; } else {
344 spvdbgs() << std::setw(4) << DeviceArchitecture.value();
345 } spvdbgs()
346 << std::endl;
347 spvdbgs() << "[FnVar] result: " << Res << std::endl;);
348
349 return Res;
350 }
351
352 protected:
353 _SPIRV_DEF_ENCDEC6(Type, Id, Category, Family, CmpOp, Architecture);
354
355 private:
356 SPIRVWord Category;
357 SPIRVWord Family;
358 spv::Op CmpOp;
359 SPIRVWord Architecture;
360 };
361
362 class SPIRVSpecConstantCapabilitiesINTEL : public SPIRVValue {
363 public:
364 constexpr static SPIRVWord FixedWC = 3;
365 constexpr static spv::Op OC = OpSpecConstantCapabilitiesINTEL;
366
367 // Complete constructor
368 SPIRVSpecConstantCapabilitiesINTEL(
369 SPIRVModule *M, SPIRVType *TheType, SPIRVId TheId,
370 const std::vector<SPIRVWord> TheCapabilities)
371 : SPIRVValue(M, TheCapabilities.size() + FixedWC, OC, TheType, TheId) {
372 Capabilities = TheCapabilities;
373 NumWords = TheCapabilities.size() + FixedWC;
374 validate();
375 }
376 // Incomplete constructor
377 SPIRVSpecConstantCapabilitiesINTEL() : SPIRVValue(OC) {}
378
379 std::vector<SPIRVWord> getCapabilities() const { return Capabilities; }
380 bool matchesDevice() {
381 std::vector<SPIRVWord> DeviceCapabilities =
382 getModule()->getFnVarCapabilities();
383 bool Res = true;
384
385 if (!DeviceCapabilities.empty()) {
386 for (const auto &Capability : Capabilities) {
387 if (std::find(DeviceCapabilities.cbegin(), DeviceCapabilities.cend(),
388 Capability) == DeviceCapabilities.cend()) {
389 Res = false;
390 }
391 }
392 }
393
394 SPIRVDBG(
395 spvdbgs() << "[FnVar] match instr Capabilities: ";
396 if (Capabilities.empty()) { spvdbgs() << "none"; } else {
397 for (const auto &Cap : Capabilities) {
398 spvdbgs() << " " << Cap;
399 }
400 } spvdbgs()
401 << " | ID: %" << getId() << std::endl;
402 spvdbgs() << "[FnVar] device Capabilities: ";
403 for (const auto &Cap
404 : DeviceCapabilities) { spvdbgs() << " " << Cap; } spvdbgs()
405 << std::endl;
406 spvdbgs() << "[FnVar] result: " << Res << std::endl;);
407
408 return Res;
409 }
410
411 protected:
412 _SPIRV_DEF_ENCDEC3(Type, Id, Capabilities);
413
414 void validate() const override {
415 SPIRVValue::validate();
416 if (Capabilities.size() < 1) {
417 std::stringstream SS;
418 SS << "Id: " << Id << ", OpCode: " << OpCodeNameMap::map(OpCode)
419 << ", Name: \"" << Name << "\". Expected at least one capability.\n";
420 getErrorLog().checkError(false, SPIRVEC_InvalidNumberOfOperands,
421 SS.str());
422 }
423 }
424
425 void setWordCount(SPIRVWord WordCount) override {
426 SPIRVEntry::setWordCount(WordCount);
427 Capabilities.resize(WordCount - FixedWC);
428 NumWords = WordCount - FixedWC;
429 }
430
431 private:
432 unsigned NumWords;
433 std::vector<SPIRVWord> Capabilities;
434 };
435
436 } // namespace SPIRV
437 #endif // SPIRV_LIBSPIRV_SPIRVFNVAR_H