opcode.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 SpvOpTypeRayQueryProvisionalKHR:
  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 SpvOpAtomicISub:
  376. case SpvOpAtomicSMin:
  377. case SpvOpAtomicUMin:
  378. case SpvOpAtomicSMax:
  379. case SpvOpAtomicUMax:
  380. case SpvOpAtomicAnd:
  381. case SpvOpAtomicOr:
  382. case SpvOpAtomicXor:
  383. case SpvOpAtomicFlagTestAndSet:
  384. return true;
  385. default:
  386. return false;
  387. }
  388. }
  389. bool spvOpcodeIsAtomicOp(const SpvOp opcode) {
  390. return (spvOpcodeIsAtomicWithLoad(opcode) || opcode == SpvOpAtomicStore ||
  391. opcode == SpvOpAtomicFlagClear);
  392. }
  393. bool spvOpcodeIsReturn(SpvOp opcode) {
  394. switch (opcode) {
  395. case SpvOpReturn:
  396. case SpvOpReturnValue:
  397. return true;
  398. default:
  399. return false;
  400. }
  401. }
  402. bool spvOpcodeIsReturnOrAbort(SpvOp opcode) {
  403. return spvOpcodeIsReturn(opcode) || opcode == SpvOpKill ||
  404. opcode == SpvOpUnreachable;
  405. }
  406. bool spvOpcodeIsBlockTerminator(SpvOp opcode) {
  407. return spvOpcodeIsBranch(opcode) || spvOpcodeIsReturnOrAbort(opcode);
  408. }
  409. bool spvOpcodeIsBaseOpaqueType(SpvOp opcode) {
  410. switch (opcode) {
  411. case SpvOpTypeImage:
  412. case SpvOpTypeSampler:
  413. case SpvOpTypeSampledImage:
  414. case SpvOpTypeOpaque:
  415. case SpvOpTypeEvent:
  416. case SpvOpTypeDeviceEvent:
  417. case SpvOpTypeReserveId:
  418. case SpvOpTypeQueue:
  419. case SpvOpTypePipe:
  420. case SpvOpTypeForwardPointer:
  421. case SpvOpTypePipeStorage:
  422. case SpvOpTypeNamedBarrier:
  423. return true;
  424. default:
  425. return false;
  426. }
  427. }
  428. bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode) {
  429. switch (opcode) {
  430. case SpvOpGroupNonUniformElect:
  431. case SpvOpGroupNonUniformAll:
  432. case SpvOpGroupNonUniformAny:
  433. case SpvOpGroupNonUniformAllEqual:
  434. case SpvOpGroupNonUniformBroadcast:
  435. case SpvOpGroupNonUniformBroadcastFirst:
  436. case SpvOpGroupNonUniformBallot:
  437. case SpvOpGroupNonUniformInverseBallot:
  438. case SpvOpGroupNonUniformBallotBitExtract:
  439. case SpvOpGroupNonUniformBallotBitCount:
  440. case SpvOpGroupNonUniformBallotFindLSB:
  441. case SpvOpGroupNonUniformBallotFindMSB:
  442. case SpvOpGroupNonUniformShuffle:
  443. case SpvOpGroupNonUniformShuffleXor:
  444. case SpvOpGroupNonUniformShuffleUp:
  445. case SpvOpGroupNonUniformShuffleDown:
  446. case SpvOpGroupNonUniformIAdd:
  447. case SpvOpGroupNonUniformFAdd:
  448. case SpvOpGroupNonUniformIMul:
  449. case SpvOpGroupNonUniformFMul:
  450. case SpvOpGroupNonUniformSMin:
  451. case SpvOpGroupNonUniformUMin:
  452. case SpvOpGroupNonUniformFMin:
  453. case SpvOpGroupNonUniformSMax:
  454. case SpvOpGroupNonUniformUMax:
  455. case SpvOpGroupNonUniformFMax:
  456. case SpvOpGroupNonUniformBitwiseAnd:
  457. case SpvOpGroupNonUniformBitwiseOr:
  458. case SpvOpGroupNonUniformBitwiseXor:
  459. case SpvOpGroupNonUniformLogicalAnd:
  460. case SpvOpGroupNonUniformLogicalOr:
  461. case SpvOpGroupNonUniformLogicalXor:
  462. case SpvOpGroupNonUniformQuadBroadcast:
  463. case SpvOpGroupNonUniformQuadSwap:
  464. return true;
  465. default:
  466. return false;
  467. }
  468. }
  469. bool spvOpcodeIsScalarizable(SpvOp opcode) {
  470. switch (opcode) {
  471. case SpvOpPhi:
  472. case SpvOpCopyObject:
  473. case SpvOpConvertFToU:
  474. case SpvOpConvertFToS:
  475. case SpvOpConvertSToF:
  476. case SpvOpConvertUToF:
  477. case SpvOpUConvert:
  478. case SpvOpSConvert:
  479. case SpvOpFConvert:
  480. case SpvOpQuantizeToF16:
  481. case SpvOpVectorInsertDynamic:
  482. case SpvOpSNegate:
  483. case SpvOpFNegate:
  484. case SpvOpIAdd:
  485. case SpvOpFAdd:
  486. case SpvOpISub:
  487. case SpvOpFSub:
  488. case SpvOpIMul:
  489. case SpvOpFMul:
  490. case SpvOpUDiv:
  491. case SpvOpSDiv:
  492. case SpvOpFDiv:
  493. case SpvOpUMod:
  494. case SpvOpSRem:
  495. case SpvOpSMod:
  496. case SpvOpFRem:
  497. case SpvOpFMod:
  498. case SpvOpVectorTimesScalar:
  499. case SpvOpIAddCarry:
  500. case SpvOpISubBorrow:
  501. case SpvOpUMulExtended:
  502. case SpvOpSMulExtended:
  503. case SpvOpShiftRightLogical:
  504. case SpvOpShiftRightArithmetic:
  505. case SpvOpShiftLeftLogical:
  506. case SpvOpBitwiseOr:
  507. case SpvOpBitwiseAnd:
  508. case SpvOpNot:
  509. case SpvOpBitFieldInsert:
  510. case SpvOpBitFieldSExtract:
  511. case SpvOpBitFieldUExtract:
  512. case SpvOpBitReverse:
  513. case SpvOpBitCount:
  514. case SpvOpIsNan:
  515. case SpvOpIsInf:
  516. case SpvOpIsFinite:
  517. case SpvOpIsNormal:
  518. case SpvOpSignBitSet:
  519. case SpvOpLessOrGreater:
  520. case SpvOpOrdered:
  521. case SpvOpUnordered:
  522. case SpvOpLogicalEqual:
  523. case SpvOpLogicalNotEqual:
  524. case SpvOpLogicalOr:
  525. case SpvOpLogicalAnd:
  526. case SpvOpLogicalNot:
  527. case SpvOpSelect:
  528. case SpvOpIEqual:
  529. case SpvOpINotEqual:
  530. case SpvOpUGreaterThan:
  531. case SpvOpSGreaterThan:
  532. case SpvOpUGreaterThanEqual:
  533. case SpvOpSGreaterThanEqual:
  534. case SpvOpULessThan:
  535. case SpvOpSLessThan:
  536. case SpvOpULessThanEqual:
  537. case SpvOpSLessThanEqual:
  538. case SpvOpFOrdEqual:
  539. case SpvOpFUnordEqual:
  540. case SpvOpFOrdNotEqual:
  541. case SpvOpFUnordNotEqual:
  542. case SpvOpFOrdLessThan:
  543. case SpvOpFUnordLessThan:
  544. case SpvOpFOrdGreaterThan:
  545. case SpvOpFUnordGreaterThan:
  546. case SpvOpFOrdLessThanEqual:
  547. case SpvOpFUnordLessThanEqual:
  548. case SpvOpFOrdGreaterThanEqual:
  549. case SpvOpFUnordGreaterThanEqual:
  550. return true;
  551. default:
  552. return false;
  553. }
  554. }
  555. bool spvOpcodeIsDebug(SpvOp opcode) {
  556. switch (opcode) {
  557. case SpvOpName:
  558. case SpvOpMemberName:
  559. case SpvOpSource:
  560. case SpvOpSourceContinued:
  561. case SpvOpSourceExtension:
  562. case SpvOpString:
  563. case SpvOpLine:
  564. case SpvOpNoLine:
  565. return true;
  566. default:
  567. return false;
  568. }
  569. }
  570. bool spvOpcodeIsCommutativeBinaryOperator(SpvOp opcode) {
  571. switch (opcode) {
  572. case SpvOpPtrEqual:
  573. case SpvOpPtrNotEqual:
  574. case SpvOpIAdd:
  575. case SpvOpFAdd:
  576. case SpvOpIMul:
  577. case SpvOpFMul:
  578. case SpvOpDot:
  579. case SpvOpIAddCarry:
  580. case SpvOpUMulExtended:
  581. case SpvOpSMulExtended:
  582. case SpvOpBitwiseOr:
  583. case SpvOpBitwiseXor:
  584. case SpvOpBitwiseAnd:
  585. case SpvOpOrdered:
  586. case SpvOpUnordered:
  587. case SpvOpLogicalEqual:
  588. case SpvOpLogicalNotEqual:
  589. case SpvOpLogicalOr:
  590. case SpvOpLogicalAnd:
  591. case SpvOpIEqual:
  592. case SpvOpINotEqual:
  593. case SpvOpFOrdEqual:
  594. case SpvOpFUnordEqual:
  595. case SpvOpFOrdNotEqual:
  596. case SpvOpFUnordNotEqual:
  597. return true;
  598. default:
  599. return false;
  600. }
  601. }
  602. bool spvOpcodeIsLinearAlgebra(SpvOp opcode) {
  603. switch (opcode) {
  604. case SpvOpTranspose:
  605. case SpvOpVectorTimesScalar:
  606. case SpvOpMatrixTimesScalar:
  607. case SpvOpVectorTimesMatrix:
  608. case SpvOpMatrixTimesVector:
  609. case SpvOpMatrixTimesMatrix:
  610. case SpvOpOuterProduct:
  611. case SpvOpDot:
  612. return true;
  613. default:
  614. return false;
  615. }
  616. }
  617. bool spvOpcodeIsImageSample(const SpvOp opcode) {
  618. switch (opcode) {
  619. case SpvOpImageSampleImplicitLod:
  620. case SpvOpImageSampleExplicitLod:
  621. case SpvOpImageSampleDrefImplicitLod:
  622. case SpvOpImageSampleDrefExplicitLod:
  623. case SpvOpImageSampleProjImplicitLod:
  624. case SpvOpImageSampleProjExplicitLod:
  625. case SpvOpImageSampleProjDrefImplicitLod:
  626. case SpvOpImageSampleProjDrefExplicitLod:
  627. case SpvOpImageSparseSampleImplicitLod:
  628. case SpvOpImageSparseSampleExplicitLod:
  629. case SpvOpImageSparseSampleDrefImplicitLod:
  630. case SpvOpImageSparseSampleDrefExplicitLod:
  631. return true;
  632. default:
  633. return false;
  634. }
  635. }
  636. std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode) {
  637. switch (opcode) {
  638. case SpvOpMemoryBarrier:
  639. return {1};
  640. case SpvOpAtomicStore:
  641. case SpvOpControlBarrier:
  642. case SpvOpAtomicFlagClear:
  643. case SpvOpMemoryNamedBarrier:
  644. return {2};
  645. case SpvOpAtomicLoad:
  646. case SpvOpAtomicExchange:
  647. case SpvOpAtomicIIncrement:
  648. case SpvOpAtomicIDecrement:
  649. case SpvOpAtomicIAdd:
  650. case SpvOpAtomicISub:
  651. case SpvOpAtomicSMin:
  652. case SpvOpAtomicUMin:
  653. case SpvOpAtomicSMax:
  654. case SpvOpAtomicUMax:
  655. case SpvOpAtomicAnd:
  656. case SpvOpAtomicOr:
  657. case SpvOpAtomicXor:
  658. case SpvOpAtomicFlagTestAndSet:
  659. return {4};
  660. case SpvOpAtomicCompareExchange:
  661. case SpvOpAtomicCompareExchangeWeak:
  662. return {4, 5};
  663. default:
  664. return {};
  665. }
  666. }