opcode.cpp 19 KB

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