opcode.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #include "source/opcode.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <algorithm>
  20. #include <cstdlib>
  21. #include "source/instruction.h"
  22. #include "source/macro.h"
  23. #include "source/spirv_constant.h"
  24. #include "source/spirv_endian.h"
  25. #include "source/spirv_target_env.h"
  26. #include "spirv-tools/libspirv.h"
  27. namespace {
  28. struct OpcodeDescPtrLen {
  29. const spv_opcode_desc_t* ptr;
  30. uint32_t len;
  31. };
  32. #include "core.insts-unified1.inc"
  33. static const spv_opcode_table_t kOpcodeTable = {ARRAY_SIZE(kOpcodeTableEntries),
  34. kOpcodeTableEntries};
  35. // Represents a vendor tool entry in the SPIR-V XML Regsitry.
  36. struct VendorTool {
  37. uint32_t value;
  38. const char* vendor;
  39. const char* tool; // Might be empty string.
  40. const char* vendor_tool; // Combiantion of vendor and tool.
  41. };
  42. const VendorTool vendor_tools[] = {
  43. #include "generators.inc"
  44. };
  45. } // anonymous namespace
  46. // TODO(dneto): Move this to another file. It doesn't belong with opcode
  47. // processing.
  48. const char* spvGeneratorStr(uint32_t generator) {
  49. auto where = std::find_if(
  50. std::begin(vendor_tools), std::end(vendor_tools),
  51. [generator](const VendorTool& vt) { return generator == vt.value; });
  52. if (where != std::end(vendor_tools)) return where->vendor_tool;
  53. return "Unknown";
  54. }
  55. uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
  56. return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
  57. }
  58. void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount,
  59. uint16_t* pOpcode) {
  60. if (pWordCount) {
  61. *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
  62. }
  63. if (pOpcode) {
  64. *pOpcode = 0x0000ffff & word;
  65. }
  66. }
  67. spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable, spv_target_env) {
  68. if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
  69. // Descriptions of each opcode. Each entry describes the format of the
  70. // instruction that follows a particular opcode.
  71. *pInstTable = &kOpcodeTable;
  72. return SPV_SUCCESS;
  73. }
  74. spv_result_t spvOpcodeTableNameLookup(spv_target_env env,
  75. const spv_opcode_table table,
  76. const char* name,
  77. spv_opcode_desc* pEntry) {
  78. if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
  79. if (!table) return SPV_ERROR_INVALID_TABLE;
  80. // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
  81. // preferable but the table requires sorting on the Opcode name, but it's
  82. // static const initialized and matches the order of the spec.
  83. const size_t nameLength = strlen(name);
  84. const auto version = spvVersionForTargetEnv(env);
  85. for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
  86. const spv_opcode_desc_t& entry = table->entries[opcodeIndex];
  87. // We considers the current opcode as available as long as
  88. // 1. The target environment satisfies the minimal requirement of the
  89. // opcode; or
  90. // 2. There is at least one extension enabling this opcode.
  91. //
  92. // Note that the second rule assumes the extension enabling this instruction
  93. // is indeed requested in the SPIR-V code; checking that should be
  94. // validator's work.
  95. if (((version >= entry.minVersion && version <= entry.lastVersion) ||
  96. entry.numExtensions > 0u || entry.numCapabilities > 0u) &&
  97. nameLength == strlen(entry.name) &&
  98. !strncmp(name, entry.name, nameLength)) {
  99. // NOTE: Found out Opcode!
  100. *pEntry = &entry;
  101. return SPV_SUCCESS;
  102. }
  103. }
  104. return SPV_ERROR_INVALID_LOOKUP;
  105. }
  106. spv_result_t spvOpcodeTableValueLookup(spv_target_env env,
  107. const spv_opcode_table table,
  108. const SpvOp opcode,
  109. spv_opcode_desc* pEntry) {
  110. if (!table) return SPV_ERROR_INVALID_TABLE;
  111. if (!pEntry) return SPV_ERROR_INVALID_POINTER;
  112. const auto beg = table->entries;
  113. const auto end = table->entries + table->count;
  114. spv_opcode_desc_t needle = {"", opcode, 0, nullptr, 0, {},
  115. false, false, 0, nullptr, ~0u, ~0u};
  116. auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) {
  117. return lhs.opcode < rhs.opcode;
  118. };
  119. // We need to loop here because there can exist multiple symbols for the same
  120. // opcode value, and they can be introduced in different target environments,
  121. // which means they can have different minimal version requirements.
  122. // Assumes the underlying table is already sorted ascendingly according to
  123. // opcode value.
  124. const auto version = spvVersionForTargetEnv(env);
  125. for (auto it = std::lower_bound(beg, end, needle, comp);
  126. it != end && it->opcode == opcode; ++it) {
  127. // We considers the current opcode as available as long as
  128. // 1. The target environment satisfies the minimal requirement of the
  129. // opcode; or
  130. // 2. There is at least one extension enabling this opcode.
  131. //
  132. // Note that the second rule assumes the extension enabling this instruction
  133. // is indeed requested in the SPIR-V code; checking that should be
  134. // validator's work.
  135. if ((version >= it->minVersion && version <= it->lastVersion) ||
  136. it->numExtensions > 0u || it->numCapabilities > 0u) {
  137. *pEntry = it;
  138. return SPV_SUCCESS;
  139. }
  140. }
  141. return SPV_ERROR_INVALID_LOOKUP;
  142. }
  143. void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
  144. const uint16_t wordCount, const spv_endianness_t endian,
  145. spv_instruction_t* pInst) {
  146. pInst->opcode = opcode;
  147. pInst->words.resize(wordCount);
  148. for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
  149. pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
  150. if (!wordIndex) {
  151. uint16_t thisWordCount;
  152. uint16_t thisOpcode;
  153. spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
  154. assert(opcode == static_cast<SpvOp>(thisOpcode) &&
  155. wordCount == thisWordCount && "Endianness failed!");
  156. }
  157. }
  158. }
  159. const char* spvOpcodeString(const uint32_t opcode) {
  160. const auto beg = kOpcodeTableEntries;
  161. const auto end = kOpcodeTableEntries + ARRAY_SIZE(kOpcodeTableEntries);
  162. spv_opcode_desc_t needle = {"", static_cast<SpvOp>(opcode),
  163. 0, nullptr,
  164. 0, {},
  165. false, false,
  166. 0, nullptr,
  167. ~0u, ~0u};
  168. auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) {
  169. return lhs.opcode < rhs.opcode;
  170. };
  171. auto it = std::lower_bound(beg, end, needle, comp);
  172. if (it != end && it->opcode == opcode) {
  173. return it->name;
  174. }
  175. assert(0 && "Unreachable!");
  176. return "unknown";
  177. }
  178. int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
  179. switch (opcode) {
  180. case SpvOpTypeInt:
  181. case SpvOpTypeFloat:
  182. case SpvOpTypeBool:
  183. return true;
  184. default:
  185. return false;
  186. }
  187. }
  188. int32_t spvOpcodeIsSpecConstant(const SpvOp opcode) {
  189. switch (opcode) {
  190. case SpvOpSpecConstantTrue:
  191. case SpvOpSpecConstantFalse:
  192. case SpvOpSpecConstant:
  193. case SpvOpSpecConstantComposite:
  194. case SpvOpSpecConstantOp:
  195. return true;
  196. default:
  197. return false;
  198. }
  199. }
  200. int32_t spvOpcodeIsConstant(const SpvOp opcode) {
  201. switch (opcode) {
  202. case SpvOpConstantTrue:
  203. case SpvOpConstantFalse:
  204. case SpvOpConstant:
  205. case SpvOpConstantComposite:
  206. case SpvOpConstantSampler:
  207. case SpvOpConstantNull:
  208. case SpvOpSpecConstantTrue:
  209. case SpvOpSpecConstantFalse:
  210. case SpvOpSpecConstant:
  211. case SpvOpSpecConstantComposite:
  212. case SpvOpSpecConstantOp:
  213. return true;
  214. default:
  215. return false;
  216. }
  217. }
  218. bool spvOpcodeIsConstantOrUndef(const SpvOp opcode) {
  219. return opcode == SpvOpUndef || spvOpcodeIsConstant(opcode);
  220. }
  221. bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode) {
  222. switch (opcode) {
  223. case SpvOpSpecConstantTrue:
  224. case SpvOpSpecConstantFalse:
  225. case SpvOpSpecConstant:
  226. return true;
  227. default:
  228. return false;
  229. }
  230. }
  231. int32_t spvOpcodeIsComposite(const SpvOp opcode) {
  232. switch (opcode) {
  233. case SpvOpTypeVector:
  234. case SpvOpTypeMatrix:
  235. case SpvOpTypeArray:
  236. case SpvOpTypeStruct:
  237. case SpvOpTypeCooperativeMatrixNV:
  238. return true;
  239. default:
  240. return false;
  241. }
  242. }
  243. bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode) {
  244. switch (opcode) {
  245. case SpvOpVariable:
  246. case SpvOpAccessChain:
  247. case SpvOpInBoundsAccessChain:
  248. case SpvOpFunctionParameter:
  249. case SpvOpImageTexelPointer:
  250. case SpvOpCopyObject:
  251. case SpvOpSelect:
  252. case SpvOpPhi:
  253. case SpvOpFunctionCall:
  254. case SpvOpPtrAccessChain:
  255. case SpvOpLoad:
  256. case SpvOpConstantNull:
  257. return true;
  258. default:
  259. return false;
  260. }
  261. }
  262. int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode) {
  263. switch (opcode) {
  264. case SpvOpVariable:
  265. case SpvOpAccessChain:
  266. case SpvOpInBoundsAccessChain:
  267. case SpvOpFunctionParameter:
  268. case SpvOpImageTexelPointer:
  269. case SpvOpCopyObject:
  270. return true;
  271. default:
  272. return false;
  273. }
  274. }
  275. int32_t spvOpcodeGeneratesType(SpvOp op) {
  276. switch (op) {
  277. case SpvOpTypeVoid:
  278. case SpvOpTypeBool:
  279. case SpvOpTypeInt:
  280. case SpvOpTypeFloat:
  281. case SpvOpTypeVector:
  282. case SpvOpTypeMatrix:
  283. case SpvOpTypeImage:
  284. case SpvOpTypeSampler:
  285. case SpvOpTypeSampledImage:
  286. case SpvOpTypeArray:
  287. case SpvOpTypeRuntimeArray:
  288. case SpvOpTypeStruct:
  289. case SpvOpTypeOpaque:
  290. case SpvOpTypePointer:
  291. case SpvOpTypeFunction:
  292. case SpvOpTypeEvent:
  293. case SpvOpTypeDeviceEvent:
  294. case SpvOpTypeReserveId:
  295. case SpvOpTypeQueue:
  296. case SpvOpTypePipe:
  297. case SpvOpTypePipeStorage:
  298. case SpvOpTypeNamedBarrier:
  299. case SpvOpTypeAccelerationStructureNV:
  300. case SpvOpTypeCooperativeMatrixNV:
  301. // case SpvOpTypeAccelerationStructureKHR: covered by
  302. // SpvOpTypeAccelerationStructureNV
  303. case SpvOpTypeRayQueryKHR:
  304. return true;
  305. default:
  306. // In particular, OpTypeForwardPointer does not generate a type,
  307. // but declares a storage class for a pointer type generated
  308. // by a different instruction.
  309. break;
  310. }
  311. return 0;
  312. }
  313. bool spvOpcodeIsDecoration(const SpvOp opcode) {
  314. switch (opcode) {
  315. case SpvOpDecorate:
  316. case SpvOpDecorateId:
  317. case SpvOpMemberDecorate:
  318. case SpvOpGroupDecorate:
  319. case SpvOpGroupMemberDecorate:
  320. case SpvOpDecorateStringGOOGLE:
  321. case SpvOpMemberDecorateStringGOOGLE:
  322. return true;
  323. default:
  324. break;
  325. }
  326. return false;
  327. }
  328. bool spvOpcodeIsLoad(const SpvOp opcode) {
  329. switch (opcode) {
  330. case SpvOpLoad:
  331. case SpvOpImageSampleExplicitLod:
  332. case SpvOpImageSampleImplicitLod:
  333. case SpvOpImageSampleDrefImplicitLod:
  334. case SpvOpImageSampleDrefExplicitLod:
  335. case SpvOpImageSampleProjImplicitLod:
  336. case SpvOpImageSampleProjExplicitLod:
  337. case SpvOpImageSampleProjDrefImplicitLod:
  338. case SpvOpImageSampleProjDrefExplicitLod:
  339. case SpvOpImageFetch:
  340. case SpvOpImageGather:
  341. case SpvOpImageDrefGather:
  342. case SpvOpImageRead:
  343. case SpvOpImageSparseSampleImplicitLod:
  344. case SpvOpImageSparseSampleExplicitLod:
  345. case SpvOpImageSparseSampleDrefExplicitLod:
  346. case SpvOpImageSparseSampleDrefImplicitLod:
  347. case SpvOpImageSparseFetch:
  348. case SpvOpImageSparseGather:
  349. case SpvOpImageSparseDrefGather:
  350. case SpvOpImageSparseRead:
  351. return true;
  352. default:
  353. return false;
  354. }
  355. }
  356. bool spvOpcodeIsBranch(SpvOp opcode) {
  357. switch (opcode) {
  358. case SpvOpBranch:
  359. case SpvOpBranchConditional:
  360. case SpvOpSwitch:
  361. return true;
  362. default:
  363. return false;
  364. }
  365. }
  366. bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode) {
  367. switch (opcode) {
  368. case SpvOpAtomicLoad:
  369. case SpvOpAtomicExchange:
  370. case SpvOpAtomicCompareExchange:
  371. case SpvOpAtomicCompareExchangeWeak:
  372. case SpvOpAtomicIIncrement:
  373. case SpvOpAtomicIDecrement:
  374. case SpvOpAtomicIAdd:
  375. case SpvOpAtomicFAddEXT:
  376. case SpvOpAtomicISub:
  377. case SpvOpAtomicSMin:
  378. case SpvOpAtomicUMin:
  379. case SpvOpAtomicSMax:
  380. case SpvOpAtomicUMax:
  381. case SpvOpAtomicAnd:
  382. case SpvOpAtomicOr:
  383. case SpvOpAtomicXor:
  384. case SpvOpAtomicFlagTestAndSet:
  385. return true;
  386. default:
  387. return false;
  388. }
  389. }
  390. bool spvOpcodeIsAtomicOp(const SpvOp opcode) {
  391. return (spvOpcodeIsAtomicWithLoad(opcode) || opcode == SpvOpAtomicStore ||
  392. opcode == SpvOpAtomicFlagClear);
  393. }
  394. bool spvOpcodeIsReturn(SpvOp opcode) {
  395. switch (opcode) {
  396. case SpvOpReturn:
  397. case SpvOpReturnValue:
  398. return true;
  399. default:
  400. return false;
  401. }
  402. }
  403. bool spvOpcodeIsAbort(SpvOp opcode) {
  404. switch (opcode) {
  405. case SpvOpKill:
  406. case SpvOpUnreachable:
  407. case SpvOpTerminateInvocation:
  408. case SpvOpTerminateRayKHR:
  409. case SpvOpIgnoreIntersectionKHR:
  410. return true;
  411. default:
  412. return false;
  413. }
  414. }
  415. bool spvOpcodeIsReturnOrAbort(SpvOp opcode) {
  416. return spvOpcodeIsReturn(opcode) || spvOpcodeIsAbort(opcode);
  417. }
  418. bool spvOpcodeIsBlockTerminator(SpvOp opcode) {
  419. return spvOpcodeIsBranch(opcode) || spvOpcodeIsReturnOrAbort(opcode);
  420. }
  421. bool spvOpcodeTerminatesExecution(SpvOp opcode) {
  422. return opcode == SpvOpKill || opcode == SpvOpTerminateInvocation ||
  423. opcode == SpvOpTerminateRayKHR || opcode == SpvOpIgnoreIntersectionKHR;
  424. }
  425. bool spvOpcodeIsBaseOpaqueType(SpvOp opcode) {
  426. switch (opcode) {
  427. case SpvOpTypeImage:
  428. case SpvOpTypeSampler:
  429. case SpvOpTypeSampledImage:
  430. case SpvOpTypeOpaque:
  431. case SpvOpTypeEvent:
  432. case SpvOpTypeDeviceEvent:
  433. case SpvOpTypeReserveId:
  434. case SpvOpTypeQueue:
  435. case SpvOpTypePipe:
  436. case SpvOpTypeForwardPointer:
  437. case SpvOpTypePipeStorage:
  438. case SpvOpTypeNamedBarrier:
  439. return true;
  440. default:
  441. return false;
  442. }
  443. }
  444. bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode) {
  445. switch (opcode) {
  446. case SpvOpGroupNonUniformElect:
  447. case SpvOpGroupNonUniformAll:
  448. case SpvOpGroupNonUniformAny:
  449. case SpvOpGroupNonUniformAllEqual:
  450. case SpvOpGroupNonUniformBroadcast:
  451. case SpvOpGroupNonUniformBroadcastFirst:
  452. case SpvOpGroupNonUniformBallot:
  453. case SpvOpGroupNonUniformInverseBallot:
  454. case SpvOpGroupNonUniformBallotBitExtract:
  455. case SpvOpGroupNonUniformBallotBitCount:
  456. case SpvOpGroupNonUniformBallotFindLSB:
  457. case SpvOpGroupNonUniformBallotFindMSB:
  458. case SpvOpGroupNonUniformShuffle:
  459. case SpvOpGroupNonUniformShuffleXor:
  460. case SpvOpGroupNonUniformShuffleUp:
  461. case SpvOpGroupNonUniformShuffleDown:
  462. case SpvOpGroupNonUniformIAdd:
  463. case SpvOpGroupNonUniformFAdd:
  464. case SpvOpGroupNonUniformIMul:
  465. case SpvOpGroupNonUniformFMul:
  466. case SpvOpGroupNonUniformSMin:
  467. case SpvOpGroupNonUniformUMin:
  468. case SpvOpGroupNonUniformFMin:
  469. case SpvOpGroupNonUniformSMax:
  470. case SpvOpGroupNonUniformUMax:
  471. case SpvOpGroupNonUniformFMax:
  472. case SpvOpGroupNonUniformBitwiseAnd:
  473. case SpvOpGroupNonUniformBitwiseOr:
  474. case SpvOpGroupNonUniformBitwiseXor:
  475. case SpvOpGroupNonUniformLogicalAnd:
  476. case SpvOpGroupNonUniformLogicalOr:
  477. case SpvOpGroupNonUniformLogicalXor:
  478. case SpvOpGroupNonUniformQuadBroadcast:
  479. case SpvOpGroupNonUniformQuadSwap:
  480. return true;
  481. default:
  482. return false;
  483. }
  484. }
  485. bool spvOpcodeIsScalarizable(SpvOp opcode) {
  486. switch (opcode) {
  487. case SpvOpPhi:
  488. case SpvOpCopyObject:
  489. case SpvOpConvertFToU:
  490. case SpvOpConvertFToS:
  491. case SpvOpConvertSToF:
  492. case SpvOpConvertUToF:
  493. case SpvOpUConvert:
  494. case SpvOpSConvert:
  495. case SpvOpFConvert:
  496. case SpvOpQuantizeToF16:
  497. case SpvOpVectorInsertDynamic:
  498. case SpvOpSNegate:
  499. case SpvOpFNegate:
  500. case SpvOpIAdd:
  501. case SpvOpFAdd:
  502. case SpvOpISub:
  503. case SpvOpFSub:
  504. case SpvOpIMul:
  505. case SpvOpFMul:
  506. case SpvOpUDiv:
  507. case SpvOpSDiv:
  508. case SpvOpFDiv:
  509. case SpvOpUMod:
  510. case SpvOpSRem:
  511. case SpvOpSMod:
  512. case SpvOpFRem:
  513. case SpvOpFMod:
  514. case SpvOpVectorTimesScalar:
  515. case SpvOpIAddCarry:
  516. case SpvOpISubBorrow:
  517. case SpvOpUMulExtended:
  518. case SpvOpSMulExtended:
  519. case SpvOpShiftRightLogical:
  520. case SpvOpShiftRightArithmetic:
  521. case SpvOpShiftLeftLogical:
  522. case SpvOpBitwiseOr:
  523. case SpvOpBitwiseAnd:
  524. case SpvOpNot:
  525. case SpvOpBitFieldInsert:
  526. case SpvOpBitFieldSExtract:
  527. case SpvOpBitFieldUExtract:
  528. case SpvOpBitReverse:
  529. case SpvOpBitCount:
  530. case SpvOpIsNan:
  531. case SpvOpIsInf:
  532. case SpvOpIsFinite:
  533. case SpvOpIsNormal:
  534. case SpvOpSignBitSet:
  535. case SpvOpLessOrGreater:
  536. case SpvOpOrdered:
  537. case SpvOpUnordered:
  538. case SpvOpLogicalEqual:
  539. case SpvOpLogicalNotEqual:
  540. case SpvOpLogicalOr:
  541. case SpvOpLogicalAnd:
  542. case SpvOpLogicalNot:
  543. case SpvOpSelect:
  544. case SpvOpIEqual:
  545. case SpvOpINotEqual:
  546. case SpvOpUGreaterThan:
  547. case SpvOpSGreaterThan:
  548. case SpvOpUGreaterThanEqual:
  549. case SpvOpSGreaterThanEqual:
  550. case SpvOpULessThan:
  551. case SpvOpSLessThan:
  552. case SpvOpULessThanEqual:
  553. case SpvOpSLessThanEqual:
  554. case SpvOpFOrdEqual:
  555. case SpvOpFUnordEqual:
  556. case SpvOpFOrdNotEqual:
  557. case SpvOpFUnordNotEqual:
  558. case SpvOpFOrdLessThan:
  559. case SpvOpFUnordLessThan:
  560. case SpvOpFOrdGreaterThan:
  561. case SpvOpFUnordGreaterThan:
  562. case SpvOpFOrdLessThanEqual:
  563. case SpvOpFUnordLessThanEqual:
  564. case SpvOpFOrdGreaterThanEqual:
  565. case SpvOpFUnordGreaterThanEqual:
  566. return true;
  567. default:
  568. return false;
  569. }
  570. }
  571. bool spvOpcodeIsDebug(SpvOp opcode) {
  572. switch (opcode) {
  573. case SpvOpName:
  574. case SpvOpMemberName:
  575. case SpvOpSource:
  576. case SpvOpSourceContinued:
  577. case SpvOpSourceExtension:
  578. case SpvOpString:
  579. case SpvOpLine:
  580. case SpvOpNoLine:
  581. return true;
  582. default:
  583. return false;
  584. }
  585. }
  586. bool spvOpcodeIsCommutativeBinaryOperator(SpvOp opcode) {
  587. switch (opcode) {
  588. case SpvOpPtrEqual:
  589. case SpvOpPtrNotEqual:
  590. case SpvOpIAdd:
  591. case SpvOpFAdd:
  592. case SpvOpIMul:
  593. case SpvOpFMul:
  594. case SpvOpDot:
  595. case SpvOpIAddCarry:
  596. case SpvOpUMulExtended:
  597. case SpvOpSMulExtended:
  598. case SpvOpBitwiseOr:
  599. case SpvOpBitwiseXor:
  600. case SpvOpBitwiseAnd:
  601. case SpvOpOrdered:
  602. case SpvOpUnordered:
  603. case SpvOpLogicalEqual:
  604. case SpvOpLogicalNotEqual:
  605. case SpvOpLogicalOr:
  606. case SpvOpLogicalAnd:
  607. case SpvOpIEqual:
  608. case SpvOpINotEqual:
  609. case SpvOpFOrdEqual:
  610. case SpvOpFUnordEqual:
  611. case SpvOpFOrdNotEqual:
  612. case SpvOpFUnordNotEqual:
  613. return true;
  614. default:
  615. return false;
  616. }
  617. }
  618. bool spvOpcodeIsLinearAlgebra(SpvOp opcode) {
  619. switch (opcode) {
  620. case SpvOpTranspose:
  621. case SpvOpVectorTimesScalar:
  622. case SpvOpMatrixTimesScalar:
  623. case SpvOpVectorTimesMatrix:
  624. case SpvOpMatrixTimesVector:
  625. case SpvOpMatrixTimesMatrix:
  626. case SpvOpOuterProduct:
  627. case SpvOpDot:
  628. return true;
  629. default:
  630. return false;
  631. }
  632. }
  633. bool spvOpcodeIsImageSample(const SpvOp opcode) {
  634. switch (opcode) {
  635. case SpvOpImageSampleImplicitLod:
  636. case SpvOpImageSampleExplicitLod:
  637. case SpvOpImageSampleDrefImplicitLod:
  638. case SpvOpImageSampleDrefExplicitLod:
  639. case SpvOpImageSampleProjImplicitLod:
  640. case SpvOpImageSampleProjExplicitLod:
  641. case SpvOpImageSampleProjDrefImplicitLod:
  642. case SpvOpImageSampleProjDrefExplicitLod:
  643. case SpvOpImageSparseSampleImplicitLod:
  644. case SpvOpImageSparseSampleExplicitLod:
  645. case SpvOpImageSparseSampleDrefImplicitLod:
  646. case SpvOpImageSparseSampleDrefExplicitLod:
  647. return true;
  648. default:
  649. return false;
  650. }
  651. }
  652. std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode) {
  653. switch (opcode) {
  654. case SpvOpMemoryBarrier:
  655. return {1};
  656. case SpvOpAtomicStore:
  657. case SpvOpControlBarrier:
  658. case SpvOpAtomicFlagClear:
  659. case SpvOpMemoryNamedBarrier:
  660. return {2};
  661. case SpvOpAtomicLoad:
  662. case SpvOpAtomicExchange:
  663. case SpvOpAtomicIIncrement:
  664. case SpvOpAtomicIDecrement:
  665. case SpvOpAtomicIAdd:
  666. case SpvOpAtomicFAddEXT:
  667. case SpvOpAtomicISub:
  668. case SpvOpAtomicSMin:
  669. case SpvOpAtomicUMin:
  670. case SpvOpAtomicSMax:
  671. case SpvOpAtomicUMax:
  672. case SpvOpAtomicAnd:
  673. case SpvOpAtomicOr:
  674. case SpvOpAtomicXor:
  675. case SpvOpAtomicFlagTestAndSet:
  676. return {4};
  677. case SpvOpAtomicCompareExchange:
  678. case SpvOpAtomicCompareExchangeWeak:
  679. return {4, 5};
  680. default:
  681. return {};
  682. }
  683. }
  684. bool spvOpcodeIsAccessChain(SpvOp opcode) {
  685. switch (opcode) {
  686. case SpvOpAccessChain:
  687. case SpvOpInBoundsAccessChain:
  688. case SpvOpPtrAccessChain:
  689. case SpvOpInBoundsPtrAccessChain:
  690. return true;
  691. default:
  692. return false;
  693. }
  694. }
  695. bool spvOpcodeIsBit(SpvOp opcode) {
  696. switch (opcode) {
  697. case SpvOpShiftRightLogical:
  698. case SpvOpShiftRightArithmetic:
  699. case SpvOpShiftLeftLogical:
  700. case SpvOpBitwiseOr:
  701. case SpvOpBitwiseXor:
  702. case SpvOpBitwiseAnd:
  703. case SpvOpNot:
  704. case SpvOpBitReverse:
  705. case SpvOpBitCount:
  706. return true;
  707. default:
  708. return false;
  709. }
  710. }