blob: 7c5fbb15089d17beb756834743949079157cf775 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
dan sinclaireda2cfb2018-08-03 15:06:09 -040015#include "source/opcode.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010016
17#include <assert.h>
18#include <string.h>
19
David Neto5a0b5ca2016-12-09 14:01:43 -050020#include <algorithm>
Lei Zhang972788b2015-11-12 13:48:30 -050021#include <cstdlib>
22
dan sinclaireda2cfb2018-08-03 15:06:09 -040023#include "source/instruction.h"
24#include "source/macro.h"
25#include "source/spirv_constant.h"
26#include "source/spirv_endian.h"
27#include "source/spirv_target_env.h"
David Neto5a703352016-02-17 14:44:00 -050028#include "spirv-tools/libspirv.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050029
David Neto78c3b432015-08-27 13:03:52 -040030namespace {
Lei Zhang16981f82017-09-21 17:24:57 -040031struct OpcodeDescPtrLen {
32 const spv_opcode_desc_t* ptr;
33 uint32_t len;
34};
David Neto78c3b432015-08-27 13:03:52 -040035
dan sinclaireda2cfb2018-08-03 15:06:09 -040036#include "core.insts-unified1.inc"
Lei Zhang16981f82017-09-21 17:24:57 -040037
Lei Zhang1ef6b192018-03-14 13:06:18 -040038static const spv_opcode_table_t kOpcodeTable = {ARRAY_SIZE(kOpcodeTableEntries),
39 kOpcodeTableEntries};
David Neto78c3b432015-08-27 13:03:52 -040040
David Neto5a0b5ca2016-12-09 14:01:43 -050041// Represents a vendor tool entry in the SPIR-V XML Regsitry.
42struct VendorTool {
43 uint32_t value;
44 const char* vendor;
Lei Zhang16981f82017-09-21 17:24:57 -040045 const char* tool; // Might be empty string.
46 const char* vendor_tool; // Combiantion of vendor and tool.
David Neto5a0b5ca2016-12-09 14:01:43 -050047};
48
49const VendorTool vendor_tools[] = {
50#include "generators.inc"
51};
52
Lei Zhanga94701d2015-09-14 10:05:37 -040053} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -040054
David Neto5a0b5ca2016-12-09 14:01:43 -050055// TODO(dneto): Move this to another file. It doesn't belong with opcode
56// processing.
Lei Zhang1a0334e2015-11-02 09:41:20 -050057const char* spvGeneratorStr(uint32_t generator) {
David Neto5a0b5ca2016-12-09 14:01:43 -050058 auto where = std::find_if(
59 std::begin(vendor_tools), std::end(vendor_tools),
60 [generator](const VendorTool& vt) { return generator == vt.value; });
61 if (where != std::end(vendor_tools)) return where->vendor_tool;
62 return "Unknown";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063}
64
Lei Zhangb36e7042015-10-28 13:40:52 -040065uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010066 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
67}
68
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040069void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount,
70 uint16_t* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010071 if (pWordCount) {
72 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
73 }
74 if (pOpcode) {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040075 *pOpcode = 0x0000ffff & word;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010076 }
77}
78
Lei Zhang1ef6b192018-03-14 13:06:18 -040079spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable, spv_target_env) {
Lei Zhang40056702015-09-11 14:31:27 -040080 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010081
Lei Zhang16981f82017-09-21 17:24:57 -040082 // Descriptions of each opcode. Each entry describes the format of the
83 // instruction that follows a particular opcode.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010084
Lei Zhang1ef6b192018-03-14 13:06:18 -040085 *pInstTable = &kOpcodeTable;
86 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010087}
88
Lei Zhang1ef6b192018-03-14 13:06:18 -040089spv_result_t spvOpcodeTableNameLookup(spv_target_env env,
90 const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -050091 const char* name,
92 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -040093 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
94 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010095
96 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
97 // preferable but the table requires sorting on the Opcode name, but it's
Lei Zhang1ef6b192018-03-14 13:06:18 -040098 // static const initialized and matches the order of the spec.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010099 const size_t nameLength = strlen(name);
100 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
Lei Zhang1ef6b192018-03-14 13:06:18 -0400101 const spv_opcode_desc_t& entry = table->entries[opcodeIndex];
102 // We considers the current opcode as available as long as
103 // 1. The target environment satisfies the minimal requirement of the
104 // opcode; or
105 // 2. There is at least one extension enabling this opcode.
106 //
107 // Note that the second rule assumes the extension enabling this instruction
108 // is indeed requested in the SPIR-V code; checking that should be
109 // validator's work.
Lei Zhangddbaf322018-03-28 16:42:44 -0400110 if ((spvVersionForTargetEnv(env) >= entry.minVersion ||
David Neto8d65c892018-06-19 09:54:33 -0400111 entry.numExtensions > 0u || entry.numCapabilities > 0u) &&
Lei Zhang1ef6b192018-03-14 13:06:18 -0400112 nameLength == strlen(entry.name) &&
113 !strncmp(name, entry.name, nameLength)) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100114 // NOTE: Found out Opcode!
Lei Zhang1ef6b192018-03-14 13:06:18 -0400115 *pEntry = &entry;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116 return SPV_SUCCESS;
117 }
118 }
119
120 return SPV_ERROR_INVALID_LOOKUP;
121}
122
Lei Zhang1ef6b192018-03-14 13:06:18 -0400123spv_result_t spvOpcodeTableValueLookup(spv_target_env env,
124 const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400125 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500126 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400127 if (!table) return SPV_ERROR_INVALID_TABLE;
128 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100129
Jesus Carabanof063f912017-10-27 15:28:50 +0300130 const auto beg = table->entries;
131 const auto end = table->entries + table->count;
Lei Zhang1ef6b192018-03-14 13:06:18 -0400132
133 spv_opcode_desc_t needle = {"", opcode, 0, nullptr, 0, {},
134 false, false, 0, nullptr, ~0u};
135
Jesus Carabanof063f912017-10-27 15:28:50 +0300136 auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) {
137 return lhs.opcode < rhs.opcode;
138 };
Lei Zhang1ef6b192018-03-14 13:06:18 -0400139
140 // We need to loop here because there can exist multiple symbols for the same
141 // opcode value, and they can be introduced in different target environments,
142 // which means they can have different minimal version requirements.
143 // Assumes the underlying table is already sorted ascendingly according to
144 // opcode value.
145 for (auto it = std::lower_bound(beg, end, needle, comp);
146 it != end && it->opcode == opcode; ++it) {
147 // We considers the current opcode as available as long as
148 // 1. The target environment satisfies the minimal requirement of the
149 // opcode; or
150 // 2. There is at least one extension enabling this opcode.
151 //
152 // Note that the second rule assumes the extension enabling this instruction
153 // is indeed requested in the SPIR-V code; checking that should be
154 // validator's work.
Lei Zhangddbaf322018-03-28 16:42:44 -0400155 if (spvVersionForTargetEnv(env) >= it->minVersion ||
David Neto8d65c892018-06-19 09:54:33 -0400156 it->numExtensions > 0u || it->numCapabilities > 0u) {
Lei Zhang1ef6b192018-03-14 13:06:18 -0400157 *pEntry = it;
158 return SPV_SUCCESS;
159 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100160 }
161
162 return SPV_ERROR_INVALID_LOOKUP;
163}
164
Lei Zhang1a0334e2015-11-02 09:41:20 -0500165void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100166 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500167 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100168 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400169 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100170 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
171 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
172 if (!wordIndex) {
173 uint16_t thisWordCount;
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400174 uint16_t thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100175 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400176 assert(opcode == static_cast<SpvOp>(thisOpcode) &&
177 wordCount == thisWordCount && "Endianness failed!");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100178 }
179 }
180}
181
Lei Zhang1a0334e2015-11-02 09:41:20 -0500182const char* spvOpcodeString(const SpvOp opcode) {
Lei Zhang1ef6b192018-03-14 13:06:18 -0400183 const auto beg = kOpcodeTableEntries;
184 const auto end = kOpcodeTableEntries + ARRAY_SIZE(kOpcodeTableEntries);
185 spv_opcode_desc_t needle = {"", opcode, 0, nullptr, 0, {},
186 false, false, 0, nullptr, ~0u};
Jesus Carabanof063f912017-10-27 15:28:50 +0300187 auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) {
188 return lhs.opcode < rhs.opcode;
189 };
Lei Zhang1ef6b192018-03-14 13:06:18 -0400190 auto it = std::lower_bound(beg, end, needle, comp);
Diego Novillod2938e42017-11-08 12:40:02 -0500191 if (it != end && it->opcode == opcode) {
Jesus Carabanof063f912017-10-27 15:28:50 +0300192 return it->name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100193 }
Lei Zhang16981f82017-09-21 17:24:57 -0400194
Lei Zhang4f293b72016-03-21 16:36:14 -0400195 assert(0 && "Unreachable!");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100196 return "unknown";
197}
198
Lei Zhangb36e7042015-10-28 13:40:52 -0400199int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100200 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400201 case SpvOpTypeInt:
202 case SpvOpTypeFloat:
Dejan Mircevski276a7242016-01-21 15:55:43 -0500203 case SpvOpTypeBool:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100204 return true;
205 default:
206 return false;
207 }
208}
209
Alan Baker0a2ee652018-03-23 14:18:54 -0400210int32_t spvOpcodeIsSpecConstant(const SpvOp opcode) {
211 switch (opcode) {
212 case SpvOpSpecConstantTrue:
213 case SpvOpSpecConstantFalse:
214 case SpvOpSpecConstant:
215 case SpvOpSpecConstantComposite:
216 case SpvOpSpecConstantOp:
217 return true;
218 default:
219 return false;
220 }
221}
222
Lei Zhangb36e7042015-10-28 13:40:52 -0400223int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100224 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400225 case SpvOpConstantTrue:
226 case SpvOpConstantFalse:
227 case SpvOpConstant:
228 case SpvOpConstantComposite:
229 case SpvOpConstantSampler:
Lei Zhangb36e7042015-10-28 13:40:52 -0400230 case SpvOpConstantNull:
231 case SpvOpSpecConstantTrue:
232 case SpvOpSpecConstantFalse:
233 case SpvOpSpecConstant:
234 case SpvOpSpecConstantComposite:
Dejan Mircevski3fb26762016-04-04 15:55:05 -0400235 case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100236 return true;
237 default:
238 return false;
239 }
240}
241
David Neto1f3fb502016-09-14 11:57:20 -0400242bool spvOpcodeIsConstantOrUndef(const SpvOp opcode) {
243 return opcode == SpvOpUndef || spvOpcodeIsConstant(opcode);
244}
245
Aliya Pazylbekovaedb52642017-02-24 20:43:28 -0500246bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode) {
247 switch (opcode) {
248 case SpvOpSpecConstantTrue:
249 case SpvOpSpecConstantFalse:
250 case SpvOpSpecConstant:
251 return true;
252 default:
253 return false;
254 }
255}
256
Lei Zhangb36e7042015-10-28 13:40:52 -0400257int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100258 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400259 case SpvOpTypeVector:
260 case SpvOpTypeMatrix:
261 case SpvOpTypeArray:
262 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100263 return true;
264 default:
265 return false;
266 }
267}
268
Ehsan Nasiri23af06c2017-02-01 15:37:39 -0500269bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode) {
270 switch (opcode) {
271 case SpvOpVariable:
272 case SpvOpAccessChain:
273 case SpvOpInBoundsAccessChain:
274 case SpvOpFunctionParameter:
275 case SpvOpImageTexelPointer:
276 case SpvOpCopyObject:
277 case SpvOpSelect:
278 case SpvOpPhi:
279 case SpvOpFunctionCall:
280 case SpvOpPtrAccessChain:
281 case SpvOpLoad:
282 case SpvOpConstantNull:
283 return true;
284 default:
285 return false;
286 }
287}
288
Florian Ziesche66fcb452016-03-02 22:17:54 +0100289int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100290 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400291 case SpvOpVariable:
292 case SpvOpAccessChain:
293 case SpvOpInBoundsAccessChain:
294 case SpvOpFunctionParameter:
Florian Ziesche66fcb452016-03-02 22:17:54 +0100295 case SpvOpImageTexelPointer:
296 case SpvOpCopyObject:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100297 return true;
298 default:
299 return false;
300 }
301}
302
Lei Zhangb36e7042015-10-28 13:40:52 -0400303int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500304 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400305 case SpvOpTypeVoid:
306 case SpvOpTypeBool:
307 case SpvOpTypeInt:
308 case SpvOpTypeFloat:
309 case SpvOpTypeVector:
310 case SpvOpTypeMatrix:
311 case SpvOpTypeImage:
312 case SpvOpTypeSampler:
313 case SpvOpTypeSampledImage:
314 case SpvOpTypeArray:
315 case SpvOpTypeRuntimeArray:
316 case SpvOpTypeStruct:
317 case SpvOpTypeOpaque:
318 case SpvOpTypePointer:
319 case SpvOpTypeFunction:
320 case SpvOpTypeEvent:
321 case SpvOpTypeDeviceEvent:
322 case SpvOpTypeReserveId:
323 case SpvOpTypeQueue:
324 case SpvOpTypePipe:
Andrey Tuganov4ef3b3e2017-02-28 11:53:47 -0500325 case SpvOpTypePipeStorage:
326 case SpvOpTypeNamedBarrier:
Chao Chen6e2dab22018-09-19 11:53:33 -0700327 case SpvOpTypeAccelerationStructureNVX:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400328 return true;
David Netoaef608c2015-11-02 14:59:02 -0500329 default:
330 // In particular, OpTypeForwardPointer does not generate a type,
331 // but declares a storage class for a pointer type generated
332 // by a different instruction.
333 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400334 }
335 return 0;
336}
Steven Perroneb4653a2017-11-13 15:31:43 -0500337
338bool spvOpcodeIsDecoration(const SpvOp opcode) {
Steven Perron28c41552017-11-10 20:26:55 -0500339 switch (opcode) {
Steven Perroneb4653a2017-11-13 15:31:43 -0500340 case SpvOpDecorate:
341 case SpvOpDecorateId:
342 case SpvOpMemberDecorate:
343 case SpvOpGroupDecorate:
344 case SpvOpGroupMemberDecorate:
David Neto5f69f752018-03-05 13:34:13 -0500345 case SpvOpDecorateStringGOOGLE:
346 case SpvOpMemberDecorateStringGOOGLE:
Steven Perroneb4653a2017-11-13 15:31:43 -0500347 return true;
348 default:
349 break;
350 }
351 return false;
352}
Steven Perron28c41552017-11-10 20:26:55 -0500353
354bool spvOpcodeIsLoad(const SpvOp opcode) {
355 switch (opcode) {
356 case SpvOpLoad:
357 case SpvOpImageSampleExplicitLod:
358 case SpvOpImageSampleImplicitLod:
359 case SpvOpImageSampleDrefImplicitLod:
360 case SpvOpImageSampleDrefExplicitLod:
361 case SpvOpImageSampleProjImplicitLod:
362 case SpvOpImageSampleProjExplicitLod:
363 case SpvOpImageSampleProjDrefImplicitLod:
364 case SpvOpImageSampleProjDrefExplicitLod:
365 case SpvOpImageFetch:
366 case SpvOpImageGather:
367 case SpvOpImageDrefGather:
368 case SpvOpImageRead:
369 case SpvOpImageSparseSampleImplicitLod:
370 case SpvOpImageSparseSampleExplicitLod:
371 case SpvOpImageSparseSampleDrefExplicitLod:
372 case SpvOpImageSparseSampleDrefImplicitLod:
373 case SpvOpImageSparseFetch:
374 case SpvOpImageSparseGather:
375 case SpvOpImageSparseDrefGather:
376 case SpvOpImageSparseRead:
377 return true;
378 default:
379 return false;
380 }
381}
382
Diego Novillo74327842017-11-17 08:59:25 -0500383bool spvOpcodeIsBranch(SpvOp opcode) {
384 switch (opcode) {
385 case SpvOpBranch:
386 case SpvOpBranchConditional:
387 case SpvOpSwitch:
388 return true;
389 default:
390 return false;
391 }
392}
393
Steven Perron28c41552017-11-10 20:26:55 -0500394bool spvOpcodeIsAtomicOp(const SpvOp opcode) {
395 switch (opcode) {
396 case SpvOpAtomicLoad:
397 case SpvOpAtomicStore:
398 case SpvOpAtomicExchange:
399 case SpvOpAtomicCompareExchange:
400 case SpvOpAtomicCompareExchangeWeak:
401 case SpvOpAtomicIIncrement:
402 case SpvOpAtomicIDecrement:
403 case SpvOpAtomicIAdd:
404 case SpvOpAtomicISub:
405 case SpvOpAtomicSMin:
406 case SpvOpAtomicUMin:
407 case SpvOpAtomicSMax:
408 case SpvOpAtomicUMax:
409 case SpvOpAtomicAnd:
410 case SpvOpAtomicOr:
411 case SpvOpAtomicXor:
412 case SpvOpAtomicFlagTestAndSet:
413 case SpvOpAtomicFlagClear:
414 return true;
415 default:
416 return false;
417 }
418}
Diego Novillo74327842017-11-17 08:59:25 -0500419
420bool spvOpcodeIsReturn(SpvOp opcode) {
421 switch (opcode) {
422 case SpvOpReturn:
423 case SpvOpReturnValue:
424 return true;
425 default:
426 return false;
427 }
428}
429
Diego Novillo5f100782018-01-03 15:25:03 -0500430bool spvOpcodeIsReturnOrAbort(SpvOp opcode) {
431 return spvOpcodeIsReturn(opcode) || opcode == SpvOpKill ||
432 opcode == SpvOpUnreachable;
433}
434
Diego Novillo74327842017-11-17 08:59:25 -0500435bool spvOpcodeIsBlockTerminator(SpvOp opcode) {
Diego Novillo5f100782018-01-03 15:25:03 -0500436 return spvOpcodeIsBranch(opcode) || spvOpcodeIsReturnOrAbort(opcode);
Diego Novillo74327842017-11-17 08:59:25 -0500437}
Steven Perron79a00642017-12-11 13:10:24 -0500438
439bool spvOpcodeIsBaseOpaqueType(SpvOp opcode) {
440 switch (opcode) {
441 case SpvOpTypeImage:
442 case SpvOpTypeSampler:
443 case SpvOpTypeSampledImage:
444 case SpvOpTypeOpaque:
445 case SpvOpTypeEvent:
446 case SpvOpTypeDeviceEvent:
447 case SpvOpTypeReserveId:
448 case SpvOpTypeQueue:
449 case SpvOpTypePipe:
450 case SpvOpTypeForwardPointer:
451 case SpvOpTypePipeStorage:
452 case SpvOpTypeNamedBarrier:
453 return true;
454 default:
455 return false;
456 }
457}
Alan Baker09c206b2018-04-17 10:18:59 -0400458
459bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode) {
460 switch (opcode) {
461 case SpvOpGroupNonUniformElect:
462 case SpvOpGroupNonUniformAll:
463 case SpvOpGroupNonUniformAny:
464 case SpvOpGroupNonUniformAllEqual:
465 case SpvOpGroupNonUniformBroadcast:
466 case SpvOpGroupNonUniformBroadcastFirst:
467 case SpvOpGroupNonUniformBallot:
468 case SpvOpGroupNonUniformInverseBallot:
469 case SpvOpGroupNonUniformBallotBitExtract:
470 case SpvOpGroupNonUniformBallotBitCount:
471 case SpvOpGroupNonUniformBallotFindLSB:
472 case SpvOpGroupNonUniformBallotFindMSB:
473 case SpvOpGroupNonUniformShuffle:
474 case SpvOpGroupNonUniformShuffleXor:
475 case SpvOpGroupNonUniformShuffleUp:
476 case SpvOpGroupNonUniformShuffleDown:
477 case SpvOpGroupNonUniformIAdd:
478 case SpvOpGroupNonUniformFAdd:
479 case SpvOpGroupNonUniformIMul:
480 case SpvOpGroupNonUniformFMul:
481 case SpvOpGroupNonUniformSMin:
482 case SpvOpGroupNonUniformUMin:
483 case SpvOpGroupNonUniformFMin:
484 case SpvOpGroupNonUniformSMax:
485 case SpvOpGroupNonUniformUMax:
486 case SpvOpGroupNonUniformFMax:
487 case SpvOpGroupNonUniformBitwiseAnd:
488 case SpvOpGroupNonUniformBitwiseOr:
489 case SpvOpGroupNonUniformBitwiseXor:
490 case SpvOpGroupNonUniformLogicalAnd:
491 case SpvOpGroupNonUniformLogicalOr:
492 case SpvOpGroupNonUniformLogicalXor:
493 case SpvOpGroupNonUniformQuadBroadcast:
494 case SpvOpGroupNonUniformQuadSwap:
495 return true;
496 default:
497 return false;
498 }
499}
Steven Perron2c0ce872018-04-23 11:13:07 -0400500
501bool spvOpcodeIsScalarizable(SpvOp opcode) {
502 switch (opcode) {
503 case SpvOpPhi:
504 case SpvOpCopyObject:
505 case SpvOpConvertFToU:
506 case SpvOpConvertFToS:
507 case SpvOpConvertSToF:
508 case SpvOpConvertUToF:
509 case SpvOpUConvert:
510 case SpvOpSConvert:
511 case SpvOpFConvert:
512 case SpvOpQuantizeToF16:
513 case SpvOpVectorInsertDynamic:
514 case SpvOpSNegate:
515 case SpvOpFNegate:
516 case SpvOpIAdd:
517 case SpvOpFAdd:
518 case SpvOpISub:
519 case SpvOpFSub:
520 case SpvOpIMul:
521 case SpvOpFMul:
522 case SpvOpUDiv:
523 case SpvOpSDiv:
524 case SpvOpFDiv:
525 case SpvOpUMod:
526 case SpvOpSRem:
527 case SpvOpSMod:
528 case SpvOpFRem:
529 case SpvOpFMod:
530 case SpvOpVectorTimesScalar:
531 case SpvOpIAddCarry:
532 case SpvOpISubBorrow:
533 case SpvOpUMulExtended:
534 case SpvOpSMulExtended:
535 case SpvOpShiftRightLogical:
536 case SpvOpShiftRightArithmetic:
537 case SpvOpShiftLeftLogical:
538 case SpvOpBitwiseOr:
539 case SpvOpBitwiseAnd:
540 case SpvOpNot:
541 case SpvOpBitFieldInsert:
542 case SpvOpBitFieldSExtract:
543 case SpvOpBitFieldUExtract:
544 case SpvOpBitReverse:
545 case SpvOpBitCount:
546 case SpvOpIsNan:
547 case SpvOpIsInf:
548 case SpvOpIsFinite:
549 case SpvOpIsNormal:
550 case SpvOpSignBitSet:
551 case SpvOpLessOrGreater:
552 case SpvOpOrdered:
553 case SpvOpUnordered:
554 case SpvOpLogicalEqual:
555 case SpvOpLogicalNotEqual:
556 case SpvOpLogicalOr:
557 case SpvOpLogicalAnd:
558 case SpvOpLogicalNot:
559 case SpvOpSelect:
560 case SpvOpIEqual:
561 case SpvOpINotEqual:
562 case SpvOpUGreaterThan:
563 case SpvOpSGreaterThan:
564 case SpvOpUGreaterThanEqual:
565 case SpvOpSGreaterThanEqual:
566 case SpvOpULessThan:
567 case SpvOpSLessThan:
568 case SpvOpULessThanEqual:
569 case SpvOpSLessThanEqual:
570 case SpvOpFOrdEqual:
571 case SpvOpFUnordEqual:
572 case SpvOpFOrdNotEqual:
573 case SpvOpFUnordNotEqual:
574 case SpvOpFOrdLessThan:
575 case SpvOpFUnordLessThan:
576 case SpvOpFOrdGreaterThan:
577 case SpvOpFUnordGreaterThan:
578 case SpvOpFOrdLessThanEqual:
579 case SpvOpFUnordLessThanEqual:
580 case SpvOpFOrdGreaterThanEqual:
581 case SpvOpFUnordGreaterThanEqual:
582 return true;
583 default:
584 return false;
585 }
586}