opcode.cpp 21 KB

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