• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015-2020 The Khronos Group Inc.
2 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
3 // reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #include "source/operand.h"
18 
19 #include <assert.h>
20 #include <string.h>
21 
22 #include <algorithm>
23 
24 #include "DebugInfo.h"
25 #include "OpenCLDebugInfo100.h"
26 #include "source/macro.h"
27 #include "source/opcode.h"
28 #include "source/spirv_constant.h"
29 #include "source/spirv_target_env.h"
30 
31 // For now, assume unified1 contains up to SPIR-V 1.3 and no later
32 // SPIR-V version.
33 // TODO(dneto): Make one set of tables, but with version tags on a
34 // per-item basis. https://github.com/KhronosGroup/SPIRV-Tools/issues/1195
35 
36 #include "operand.kinds-unified1.inc"
37 #include "spirv-tools/libspirv.h"
38 
39 static const spv_operand_table_t kOperandTable = {
40     ARRAY_SIZE(pygen_variable_OperandInfoTable),
41     pygen_variable_OperandInfoTable};
42 
spvOperandTableGet(spv_operand_table * pOperandTable,spv_target_env)43 spv_result_t spvOperandTableGet(spv_operand_table* pOperandTable,
44                                 spv_target_env) {
45   if (!pOperandTable) return SPV_ERROR_INVALID_POINTER;
46 
47   *pOperandTable = &kOperandTable;
48   return SPV_SUCCESS;
49 }
50 
spvOperandTableNameLookup(spv_target_env env,const spv_operand_table table,const spv_operand_type_t type,const char * name,const size_t nameLength,spv_operand_desc * pEntry)51 spv_result_t spvOperandTableNameLookup(spv_target_env env,
52                                        const spv_operand_table table,
53                                        const spv_operand_type_t type,
54                                        const char* name,
55                                        const size_t nameLength,
56                                        spv_operand_desc* pEntry) {
57   if (!table) return SPV_ERROR_INVALID_TABLE;
58   if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
59 
60   const auto version = spvVersionForTargetEnv(env);
61   for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) {
62     const auto& group = table->types[typeIndex];
63     if (type != group.type) continue;
64     for (uint64_t index = 0; index < group.count; ++index) {
65       const auto& entry = group.entries[index];
66       // We consider the current operand as available as long as
67       // 1. The target environment satisfies the minimal requirement of the
68       //    operand; or
69       // 2. There is at least one extension enabling this operand; or
70       // 3. There is at least one capability enabling this operand.
71       //
72       // Note that the second rule assumes the extension enabling this operand
73       // is indeed requested in the SPIR-V code; checking that should be
74       // validator's work.
75       if (((version >= entry.minVersion && version <= entry.lastVersion) ||
76            entry.numExtensions > 0u || entry.numCapabilities > 0u) &&
77           nameLength == strlen(entry.name) &&
78           !strncmp(entry.name, name, nameLength)) {
79         *pEntry = &entry;
80         return SPV_SUCCESS;
81       }
82     }
83   }
84 
85   return SPV_ERROR_INVALID_LOOKUP;
86 }
87 
spvOperandTableValueLookup(spv_target_env env,const spv_operand_table table,const spv_operand_type_t type,const uint32_t value,spv_operand_desc * pEntry)88 spv_result_t spvOperandTableValueLookup(spv_target_env env,
89                                         const spv_operand_table table,
90                                         const spv_operand_type_t type,
91                                         const uint32_t value,
92                                         spv_operand_desc* pEntry) {
93   if (!table) return SPV_ERROR_INVALID_TABLE;
94   if (!pEntry) return SPV_ERROR_INVALID_POINTER;
95 
96   spv_operand_desc_t needle = {"", value, 0, nullptr, 0, nullptr, {}, ~0u, ~0u};
97 
98   auto comp = [](const spv_operand_desc_t& lhs, const spv_operand_desc_t& rhs) {
99     return lhs.value < rhs.value;
100   };
101 
102   for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) {
103     const auto& group = table->types[typeIndex];
104     if (type != group.type) continue;
105 
106     const auto beg = group.entries;
107     const auto end = group.entries + group.count;
108 
109     // We need to loop here because there can exist multiple symbols for the
110     // same operand value, and they can be introduced in different target
111     // environments, which means they can have different minimal version
112     // requirements. For example, SubgroupEqMaskKHR can exist in any SPIR-V
113     // version as long as the SPV_KHR_shader_ballot extension is there; but
114     // starting from SPIR-V 1.3, SubgroupEqMask, which has the same numeric
115     // value as SubgroupEqMaskKHR, is available in core SPIR-V without extension
116     // requirements.
117     // Assumes the underlying table is already sorted ascendingly according to
118     // opcode value.
119     const auto version = spvVersionForTargetEnv(env);
120     for (auto it = std::lower_bound(beg, end, needle, comp);
121          it != end && it->value == value; ++it) {
122       // We consider the current operand as available as long as
123       // 1. The target environment satisfies the minimal requirement of the
124       //    operand; or
125       // 2. There is at least one extension enabling this operand; or
126       // 3. There is at least one capability enabling this operand.
127       //
128       // Note that the second rule assumes the extension enabling this operand
129       // is indeed requested in the SPIR-V code; checking that should be
130       // validator's work.
131       if ((version >= it->minVersion && version <= it->lastVersion) ||
132           it->numExtensions > 0u || it->numCapabilities > 0u) {
133         *pEntry = it;
134         return SPV_SUCCESS;
135       }
136     }
137   }
138 
139   return SPV_ERROR_INVALID_LOOKUP;
140 }
141 
spvOperandTypeStr(spv_operand_type_t type)142 const char* spvOperandTypeStr(spv_operand_type_t type) {
143   switch (type) {
144     case SPV_OPERAND_TYPE_ID:
145     case SPV_OPERAND_TYPE_OPTIONAL_ID:
146       return "ID";
147     case SPV_OPERAND_TYPE_TYPE_ID:
148       return "type ID";
149     case SPV_OPERAND_TYPE_RESULT_ID:
150       return "result ID";
151     case SPV_OPERAND_TYPE_LITERAL_INTEGER:
152     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
153     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
154       return "literal number";
155     case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
156       return "possibly multi-word literal integer";
157     case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
158       return "possibly multi-word literal number";
159     case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER:
160       return "extension instruction number";
161     case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER:
162       return "OpSpecConstantOp opcode";
163     case SPV_OPERAND_TYPE_LITERAL_STRING:
164     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING:
165       return "literal string";
166     case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
167       return "source language";
168     case SPV_OPERAND_TYPE_EXECUTION_MODEL:
169       return "execution model";
170     case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
171       return "addressing model";
172     case SPV_OPERAND_TYPE_MEMORY_MODEL:
173       return "memory model";
174     case SPV_OPERAND_TYPE_EXECUTION_MODE:
175       return "execution mode";
176     case SPV_OPERAND_TYPE_STORAGE_CLASS:
177       return "storage class";
178     case SPV_OPERAND_TYPE_DIMENSIONALITY:
179       return "dimensionality";
180     case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
181       return "sampler addressing mode";
182     case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
183       return "sampler filter mode";
184     case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
185       return "image format";
186     case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
187       return "floating-point fast math mode";
188     case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
189       return "floating-point rounding mode";
190     case SPV_OPERAND_TYPE_LINKAGE_TYPE:
191       return "linkage type";
192     case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
193     case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
194       return "access qualifier";
195     case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
196       return "function parameter attribute";
197     case SPV_OPERAND_TYPE_DECORATION:
198       return "decoration";
199     case SPV_OPERAND_TYPE_BUILT_IN:
200       return "built-in";
201     case SPV_OPERAND_TYPE_SELECTION_CONTROL:
202       return "selection control";
203     case SPV_OPERAND_TYPE_LOOP_CONTROL:
204       return "loop control";
205     case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
206       return "function control";
207     case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
208       return "memory semantics ID";
209     case SPV_OPERAND_TYPE_MEMORY_ACCESS:
210     case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
211       return "memory access";
212     case SPV_OPERAND_TYPE_FRAGMENT_SHADING_RATE:
213       return "shading rate";
214     case SPV_OPERAND_TYPE_SCOPE_ID:
215       return "scope ID";
216     case SPV_OPERAND_TYPE_GROUP_OPERATION:
217       return "group operation";
218     case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
219       return "kernel enqeue flags";
220     case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
221       return "kernel profiling info";
222     case SPV_OPERAND_TYPE_CAPABILITY:
223       return "capability";
224     case SPV_OPERAND_TYPE_RAY_FLAGS:
225       return "ray flags";
226     case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
227       return "ray query intersection";
228     case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
229       return "ray query committed intersection type";
230     case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
231       return "ray query candidate intersection type";
232     case SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT:
233     case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT:
234       return "packed vector format";
235     case SPV_OPERAND_TYPE_IMAGE:
236     case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
237       return "image";
238     case SPV_OPERAND_TYPE_OPTIONAL_CIV:
239       return "context-insensitive value";
240     case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
241       return "debug info flags";
242     case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
243       return "debug base type encoding";
244     case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
245       return "debug composite type";
246     case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
247       return "debug type qualifier";
248     case SPV_OPERAND_TYPE_DEBUG_OPERATION:
249       return "debug operation";
250     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
251       return "OpenCL.DebugInfo.100 debug info flags";
252     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
253       return "OpenCL.DebugInfo.100 debug base type encoding";
254     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
255       return "OpenCL.DebugInfo.100 debug composite type";
256     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
257       return "OpenCL.DebugInfo.100 debug type qualifier";
258     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
259       return "OpenCL.DebugInfo.100 debug operation";
260     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
261       return "OpenCL.DebugInfo.100 debug imported entity";
262 
263     // The next values are for values returned from an instruction, not actually
264     // an operand.  So the specific strings don't matter.  But let's add them
265     // for completeness and ease of testing.
266     case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
267       return "image channel order";
268     case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
269       return "image channel data type";
270 
271     case SPV_OPERAND_TYPE_FPDENORM_MODE:
272       return "FP denorm mode";
273     case SPV_OPERAND_TYPE_FPOPERATION_MODE:
274       return "FP operation mode";
275     case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
276       return "quantization mode";
277     case SPV_OPERAND_TYPE_OVERFLOW_MODES:
278       return "overflow mode";
279 
280     case SPV_OPERAND_TYPE_NONE:
281       return "NONE";
282     default:
283       break;
284   }
285   return "unknown";
286 }
287 
spvPushOperandTypes(const spv_operand_type_t * types,spv_operand_pattern_t * pattern)288 void spvPushOperandTypes(const spv_operand_type_t* types,
289                          spv_operand_pattern_t* pattern) {
290   const spv_operand_type_t* endTypes;
291   for (endTypes = types; *endTypes != SPV_OPERAND_TYPE_NONE; ++endTypes) {
292   }
293 
294   while (endTypes-- != types) {
295     pattern->push_back(*endTypes);
296   }
297 }
298 
spvPushOperandTypesForMask(spv_target_env env,const spv_operand_table operandTable,const spv_operand_type_t type,const uint32_t mask,spv_operand_pattern_t * pattern)299 void spvPushOperandTypesForMask(spv_target_env env,
300                                 const spv_operand_table operandTable,
301                                 const spv_operand_type_t type,
302                                 const uint32_t mask,
303                                 spv_operand_pattern_t* pattern) {
304   // Scan from highest bits to lowest bits because we will append in LIFO
305   // fashion, and we need the operands for lower order bits to be consumed first
306   for (uint32_t candidate_bit = (1u << 31u); candidate_bit;
307        candidate_bit >>= 1) {
308     if (candidate_bit & mask) {
309       spv_operand_desc entry = nullptr;
310       if (SPV_SUCCESS == spvOperandTableValueLookup(env, operandTable, type,
311                                                     candidate_bit, &entry)) {
312         spvPushOperandTypes(entry->operandTypes, pattern);
313       }
314     }
315   }
316 }
317 
spvOperandIsConcrete(spv_operand_type_t type)318 bool spvOperandIsConcrete(spv_operand_type_t type) {
319   if (spvIsIdType(type) || spvOperandIsConcreteMask(type)) {
320     return true;
321   }
322   switch (type) {
323     case SPV_OPERAND_TYPE_LITERAL_INTEGER:
324     case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER:
325     case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER:
326     case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
327     case SPV_OPERAND_TYPE_LITERAL_STRING:
328     case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
329     case SPV_OPERAND_TYPE_EXECUTION_MODEL:
330     case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
331     case SPV_OPERAND_TYPE_MEMORY_MODEL:
332     case SPV_OPERAND_TYPE_EXECUTION_MODE:
333     case SPV_OPERAND_TYPE_STORAGE_CLASS:
334     case SPV_OPERAND_TYPE_DIMENSIONALITY:
335     case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
336     case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
337     case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
338     case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
339     case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
340     case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
341     case SPV_OPERAND_TYPE_LINKAGE_TYPE:
342     case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
343     case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
344     case SPV_OPERAND_TYPE_DECORATION:
345     case SPV_OPERAND_TYPE_BUILT_IN:
346     case SPV_OPERAND_TYPE_GROUP_OPERATION:
347     case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
348     case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
349     case SPV_OPERAND_TYPE_CAPABILITY:
350     case SPV_OPERAND_TYPE_RAY_FLAGS:
351     case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
352     case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
353     case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
354     case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
355     case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
356     case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
357     case SPV_OPERAND_TYPE_DEBUG_OPERATION:
358     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
359     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
360     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
361     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
362     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
363     case SPV_OPERAND_TYPE_FPDENORM_MODE:
364     case SPV_OPERAND_TYPE_FPOPERATION_MODE:
365     case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
366     case SPV_OPERAND_TYPE_OVERFLOW_MODES:
367     case SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT:
368       return true;
369     default:
370       break;
371   }
372   return false;
373 }
374 
spvOperandIsConcreteMask(spv_operand_type_t type)375 bool spvOperandIsConcreteMask(spv_operand_type_t type) {
376   switch (type) {
377     case SPV_OPERAND_TYPE_IMAGE:
378     case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
379     case SPV_OPERAND_TYPE_SELECTION_CONTROL:
380     case SPV_OPERAND_TYPE_LOOP_CONTROL:
381     case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
382     case SPV_OPERAND_TYPE_MEMORY_ACCESS:
383     case SPV_OPERAND_TYPE_FRAGMENT_SHADING_RATE:
384     case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
385     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
386       return true;
387     default:
388       break;
389   }
390   return false;
391 }
392 
spvOperandIsOptional(spv_operand_type_t type)393 bool spvOperandIsOptional(spv_operand_type_t type) {
394   switch (type) {
395     case SPV_OPERAND_TYPE_OPTIONAL_ID:
396     case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
397     case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
398     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
399     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
400     case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
401     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING:
402     case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
403     case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT:
404     case SPV_OPERAND_TYPE_OPTIONAL_CIV:
405       return true;
406     default:
407       break;
408   }
409   // Any variable operand is also optional.
410   return spvOperandIsVariable(type);
411 }
412 
spvOperandIsVariable(spv_operand_type_t type)413 bool spvOperandIsVariable(spv_operand_type_t type) {
414   switch (type) {
415     case SPV_OPERAND_TYPE_VARIABLE_ID:
416     case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
417     case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
418     case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
419       return true;
420     default:
421       break;
422   }
423   return false;
424 }
425 
spvExpandOperandSequenceOnce(spv_operand_type_t type,spv_operand_pattern_t * pattern)426 bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
427                                   spv_operand_pattern_t* pattern) {
428   switch (type) {
429     case SPV_OPERAND_TYPE_VARIABLE_ID:
430       pattern->push_back(type);
431       pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
432       return true;
433     case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
434       pattern->push_back(type);
435       pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER);
436       return true;
437     case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
438       // Represents Zero or more (Literal number, Id) pairs,
439       // where the literal number must be a scalar integer.
440       pattern->push_back(type);
441       pattern->push_back(SPV_OPERAND_TYPE_ID);
442       pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER);
443       return true;
444     case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
445       // Represents Zero or more (Id, Literal number) pairs.
446       pattern->push_back(type);
447       pattern->push_back(SPV_OPERAND_TYPE_LITERAL_INTEGER);
448       pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
449       return true;
450     default:
451       break;
452   }
453   return false;
454 }
455 
spvTakeFirstMatchableOperand(spv_operand_pattern_t * pattern)456 spv_operand_type_t spvTakeFirstMatchableOperand(
457     spv_operand_pattern_t* pattern) {
458   assert(!pattern->empty());
459   spv_operand_type_t result;
460   do {
461     result = pattern->back();
462     pattern->pop_back();
463   } while (spvExpandOperandSequenceOnce(result, pattern));
464   return result;
465 }
466 
spvAlternatePatternFollowingImmediate(const spv_operand_pattern_t & pattern)467 spv_operand_pattern_t spvAlternatePatternFollowingImmediate(
468     const spv_operand_pattern_t& pattern) {
469   auto it =
470       std::find(pattern.crbegin(), pattern.crend(), SPV_OPERAND_TYPE_RESULT_ID);
471   if (it != pattern.crend()) {
472     spv_operand_pattern_t alternatePattern(it - pattern.crbegin() + 2,
473                                            SPV_OPERAND_TYPE_OPTIONAL_CIV);
474     alternatePattern[1] = SPV_OPERAND_TYPE_RESULT_ID;
475     return alternatePattern;
476   }
477 
478   // No result-id found, so just expect CIVs.
479   return {SPV_OPERAND_TYPE_OPTIONAL_CIV};
480 }
481 
spvIsIdType(spv_operand_type_t type)482 bool spvIsIdType(spv_operand_type_t type) {
483   switch (type) {
484     case SPV_OPERAND_TYPE_ID:
485     case SPV_OPERAND_TYPE_TYPE_ID:
486     case SPV_OPERAND_TYPE_RESULT_ID:
487     case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
488     case SPV_OPERAND_TYPE_SCOPE_ID:
489       return true;
490     default:
491       return false;
492   }
493 }
494 
spvIsInIdType(spv_operand_type_t type)495 bool spvIsInIdType(spv_operand_type_t type) {
496   if (!spvIsIdType(type)) {
497     // If it is not an ID it cannot be an input ID.
498     return false;
499   }
500   switch (type) {
501     // Deny non-input IDs.
502     case SPV_OPERAND_TYPE_TYPE_ID:
503     case SPV_OPERAND_TYPE_RESULT_ID:
504       return false;
505     default:
506       return true;
507   }
508 }
509 
spvOperandCanBeForwardDeclaredFunction(SpvOp opcode)510 std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction(
511     SpvOp opcode) {
512   std::function<bool(unsigned index)> out;
513   if (spvOpcodeGeneratesType(opcode)) {
514     // All types can use forward pointers.
515     out = [](unsigned) { return true; };
516     return out;
517   }
518   switch (opcode) {
519     case SpvOpExecutionMode:
520     case SpvOpExecutionModeId:
521     case SpvOpEntryPoint:
522     case SpvOpName:
523     case SpvOpMemberName:
524     case SpvOpSelectionMerge:
525     case SpvOpDecorate:
526     case SpvOpMemberDecorate:
527     case SpvOpDecorateId:
528     case SpvOpDecorateStringGOOGLE:
529     case SpvOpMemberDecorateStringGOOGLE:
530     case SpvOpBranch:
531     case SpvOpLoopMerge:
532       out = [](unsigned) { return true; };
533       break;
534     case SpvOpGroupDecorate:
535     case SpvOpGroupMemberDecorate:
536     case SpvOpBranchConditional:
537     case SpvOpSwitch:
538       out = [](unsigned index) { return index != 0; };
539       break;
540 
541     case SpvOpFunctionCall:
542       // The Function parameter.
543       out = [](unsigned index) { return index == 2; };
544       break;
545 
546     case SpvOpPhi:
547       out = [](unsigned index) { return index > 1; };
548       break;
549 
550     case SpvOpEnqueueKernel:
551       // The Invoke parameter.
552       out = [](unsigned index) { return index == 8; };
553       break;
554 
555     case SpvOpGetKernelNDrangeSubGroupCount:
556     case SpvOpGetKernelNDrangeMaxSubGroupSize:
557       // The Invoke parameter.
558       out = [](unsigned index) { return index == 3; };
559       break;
560 
561     case SpvOpGetKernelWorkGroupSize:
562     case SpvOpGetKernelPreferredWorkGroupSizeMultiple:
563       // The Invoke parameter.
564       out = [](unsigned index) { return index == 2; };
565       break;
566     case SpvOpTypeForwardPointer:
567       out = [](unsigned index) { return index == 0; };
568       break;
569     case SpvOpTypeArray:
570       out = [](unsigned index) { return index == 1; };
571       break;
572     default:
573       out = [](unsigned) { return false; };
574       break;
575   }
576   return out;
577 }
578 
spvDbgInfoExtOperandCanBeForwardDeclaredFunction(spv_ext_inst_type_t ext_type,uint32_t key)579 std::function<bool(unsigned)> spvDbgInfoExtOperandCanBeForwardDeclaredFunction(
580     spv_ext_inst_type_t ext_type, uint32_t key) {
581   // The Vulkan debug info extended instruction set is non-semantic so allows no
582   // forward references ever
583   if (ext_type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
584     return [](unsigned) { return false; };
585   }
586 
587   // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/532): Forward
588   // references for debug info instructions are still in discussion. We must
589   // update the following lines of code when we conclude the spec.
590   std::function<bool(unsigned index)> out;
591   if (ext_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
592     switch (OpenCLDebugInfo100Instructions(key)) {
593       case OpenCLDebugInfo100DebugFunction:
594         out = [](unsigned index) { return index == 13; };
595         break;
596       case OpenCLDebugInfo100DebugTypeComposite:
597         out = [](unsigned index) { return index >= 13; };
598         break;
599       default:
600         out = [](unsigned) { return false; };
601         break;
602     }
603   } else {
604     switch (DebugInfoInstructions(key)) {
605       case DebugInfoDebugFunction:
606         out = [](unsigned index) { return index == 13; };
607         break;
608       case DebugInfoDebugTypeComposite:
609         out = [](unsigned index) { return index >= 12; };
610         break;
611       default:
612         out = [](unsigned) { return false; };
613         break;
614     }
615   }
616   return out;
617 }
618