operand.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/operand.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <algorithm>
  20. #include "DebugInfo.h"
  21. #include "OpenCLDebugInfo100.h"
  22. #include "source/macro.h"
  23. #include "source/spirv_constant.h"
  24. #include "source/spirv_target_env.h"
  25. // For now, assume unified1 contains up to SPIR-V 1.3 and no later
  26. // SPIR-V version.
  27. // TODO(dneto): Make one set of tables, but with version tags on a
  28. // per-item basis. https://github.com/KhronosGroup/SPIRV-Tools/issues/1195
  29. #include "operand.kinds-unified1.inc"
  30. #include "spirv-tools/libspirv.h"
  31. static const spv_operand_table_t kOperandTable = {
  32. ARRAY_SIZE(pygen_variable_OperandInfoTable),
  33. pygen_variable_OperandInfoTable};
  34. spv_result_t spvOperandTableGet(spv_operand_table* pOperandTable,
  35. spv_target_env) {
  36. if (!pOperandTable) return SPV_ERROR_INVALID_POINTER;
  37. *pOperandTable = &kOperandTable;
  38. return SPV_SUCCESS;
  39. }
  40. spv_result_t spvOperandTableNameLookup(spv_target_env env,
  41. const spv_operand_table table,
  42. const spv_operand_type_t type,
  43. const char* name,
  44. const size_t nameLength,
  45. spv_operand_desc* pEntry) {
  46. if (!table) return SPV_ERROR_INVALID_TABLE;
  47. if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
  48. const auto version = spvVersionForTargetEnv(env);
  49. for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) {
  50. const auto& group = table->types[typeIndex];
  51. if (type != group.type) continue;
  52. for (uint64_t index = 0; index < group.count; ++index) {
  53. const auto& entry = group.entries[index];
  54. // We consider the current operand as available as long as
  55. // 1. The target environment satisfies the minimal requirement of the
  56. // operand; or
  57. // 2. There is at least one extension enabling this operand; or
  58. // 3. There is at least one capability enabling this operand.
  59. //
  60. // Note that the second rule assumes the extension enabling this operand
  61. // is indeed requested in the SPIR-V code; checking that should be
  62. // validator's work.
  63. if (((version >= entry.minVersion && version <= entry.lastVersion) ||
  64. entry.numExtensions > 0u || entry.numCapabilities > 0u) &&
  65. nameLength == strlen(entry.name) &&
  66. !strncmp(entry.name, name, nameLength)) {
  67. *pEntry = &entry;
  68. return SPV_SUCCESS;
  69. }
  70. }
  71. }
  72. return SPV_ERROR_INVALID_LOOKUP;
  73. }
  74. spv_result_t spvOperandTableValueLookup(spv_target_env env,
  75. const spv_operand_table table,
  76. const spv_operand_type_t type,
  77. const uint32_t value,
  78. spv_operand_desc* pEntry) {
  79. if (!table) return SPV_ERROR_INVALID_TABLE;
  80. if (!pEntry) return SPV_ERROR_INVALID_POINTER;
  81. spv_operand_desc_t needle = {"", value, 0, nullptr, 0, nullptr, {}, ~0u, ~0u};
  82. auto comp = [](const spv_operand_desc_t& lhs, const spv_operand_desc_t& rhs) {
  83. return lhs.value < rhs.value;
  84. };
  85. for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) {
  86. const auto& group = table->types[typeIndex];
  87. if (type != group.type) continue;
  88. const auto beg = group.entries;
  89. const auto end = group.entries + group.count;
  90. // We need to loop here because there can exist multiple symbols for the
  91. // same operand value, and they can be introduced in different target
  92. // environments, which means they can have different minimal version
  93. // requirements. For example, SubgroupEqMaskKHR can exist in any SPIR-V
  94. // version as long as the SPV_KHR_shader_ballot extension is there; but
  95. // starting from SPIR-V 1.3, SubgroupEqMask, which has the same numeric
  96. // value as SubgroupEqMaskKHR, is available in core SPIR-V without extension
  97. // requirements.
  98. // Assumes the underlying table is already sorted ascendingly according to
  99. // opcode value.
  100. const auto version = spvVersionForTargetEnv(env);
  101. for (auto it = std::lower_bound(beg, end, needle, comp);
  102. it != end && it->value == value; ++it) {
  103. // We consider the current operand as available as long as
  104. // 1. The target environment satisfies the minimal requirement of the
  105. // operand; or
  106. // 2. There is at least one extension enabling this operand; or
  107. // 3. There is at least one capability enabling this operand.
  108. //
  109. // Note that the second rule assumes the extension enabling this operand
  110. // is indeed requested in the SPIR-V code; checking that should be
  111. // validator's work.
  112. if ((version >= it->minVersion && version <= it->lastVersion) ||
  113. it->numExtensions > 0u || it->numCapabilities > 0u) {
  114. *pEntry = it;
  115. return SPV_SUCCESS;
  116. }
  117. }
  118. }
  119. return SPV_ERROR_INVALID_LOOKUP;
  120. }
  121. const char* spvOperandTypeStr(spv_operand_type_t type) {
  122. switch (type) {
  123. case SPV_OPERAND_TYPE_ID:
  124. case SPV_OPERAND_TYPE_OPTIONAL_ID:
  125. return "ID";
  126. case SPV_OPERAND_TYPE_TYPE_ID:
  127. return "type ID";
  128. case SPV_OPERAND_TYPE_RESULT_ID:
  129. return "result ID";
  130. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  131. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
  132. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
  133. return "literal number";
  134. case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
  135. return "possibly multi-word literal integer";
  136. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
  137. return "possibly multi-word literal number";
  138. case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER:
  139. return "extension instruction number";
  140. case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER:
  141. return "OpSpecConstantOp opcode";
  142. case SPV_OPERAND_TYPE_LITERAL_STRING:
  143. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING:
  144. return "literal string";
  145. case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
  146. return "source language";
  147. case SPV_OPERAND_TYPE_EXECUTION_MODEL:
  148. return "execution model";
  149. case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
  150. return "addressing model";
  151. case SPV_OPERAND_TYPE_MEMORY_MODEL:
  152. return "memory model";
  153. case SPV_OPERAND_TYPE_EXECUTION_MODE:
  154. return "execution mode";
  155. case SPV_OPERAND_TYPE_STORAGE_CLASS:
  156. return "storage class";
  157. case SPV_OPERAND_TYPE_DIMENSIONALITY:
  158. return "dimensionality";
  159. case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
  160. return "sampler addressing mode";
  161. case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
  162. return "sampler filter mode";
  163. case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
  164. return "image format";
  165. case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
  166. return "floating-point fast math mode";
  167. case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
  168. return "floating-point rounding mode";
  169. case SPV_OPERAND_TYPE_LINKAGE_TYPE:
  170. return "linkage type";
  171. case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
  172. case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
  173. return "access qualifier";
  174. case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
  175. return "function parameter attribute";
  176. case SPV_OPERAND_TYPE_DECORATION:
  177. return "decoration";
  178. case SPV_OPERAND_TYPE_BUILT_IN:
  179. return "built-in";
  180. case SPV_OPERAND_TYPE_SELECTION_CONTROL:
  181. return "selection control";
  182. case SPV_OPERAND_TYPE_LOOP_CONTROL:
  183. return "loop control";
  184. case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
  185. return "function control";
  186. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  187. return "memory semantics ID";
  188. case SPV_OPERAND_TYPE_MEMORY_ACCESS:
  189. case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
  190. return "memory access";
  191. case SPV_OPERAND_TYPE_SCOPE_ID:
  192. return "scope ID";
  193. case SPV_OPERAND_TYPE_GROUP_OPERATION:
  194. return "group operation";
  195. case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
  196. return "kernel enqeue flags";
  197. case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
  198. return "kernel profiling info";
  199. case SPV_OPERAND_TYPE_CAPABILITY:
  200. return "capability";
  201. case SPV_OPERAND_TYPE_RAY_FLAGS:
  202. return "ray flags";
  203. case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
  204. return "ray query intersection";
  205. case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
  206. return "ray query committed intersection type";
  207. case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
  208. return "ray query candidate intersection type";
  209. case SPV_OPERAND_TYPE_IMAGE:
  210. case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
  211. return "image";
  212. case SPV_OPERAND_TYPE_OPTIONAL_CIV:
  213. return "context-insensitive value";
  214. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
  215. return "debug info flags";
  216. case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  217. return "debug base type encoding";
  218. case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
  219. return "debug composite type";
  220. case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
  221. return "debug type qualifier";
  222. case SPV_OPERAND_TYPE_DEBUG_OPERATION:
  223. return "debug operation";
  224. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
  225. return "OpenCL.DebugInfo.100 debug info flags";
  226. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  227. return "OpenCL.DebugInfo.100 debug base type encoding";
  228. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
  229. return "OpenCL.DebugInfo.100 debug composite type";
  230. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
  231. return "OpenCL.DebugInfo.100 debug type qualifier";
  232. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
  233. return "OpenCL.DebugInfo.100 debug operation";
  234. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
  235. return "OpenCL.DebugInfo.100 debug imported entity";
  236. // The next values are for values returned from an instruction, not actually
  237. // an operand. So the specific strings don't matter. But let's add them
  238. // for completeness and ease of testing.
  239. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
  240. return "image channel order";
  241. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
  242. return "image channel data type";
  243. case SPV_OPERAND_TYPE_NONE:
  244. return "NONE";
  245. default:
  246. break;
  247. }
  248. return "unknown";
  249. }
  250. void spvPushOperandTypes(const spv_operand_type_t* types,
  251. spv_operand_pattern_t* pattern) {
  252. const spv_operand_type_t* endTypes;
  253. for (endTypes = types; *endTypes != SPV_OPERAND_TYPE_NONE; ++endTypes) {
  254. }
  255. while (endTypes-- != types) {
  256. pattern->push_back(*endTypes);
  257. }
  258. }
  259. void spvPushOperandTypesForMask(spv_target_env env,
  260. const spv_operand_table operandTable,
  261. const spv_operand_type_t type,
  262. const uint32_t mask,
  263. spv_operand_pattern_t* pattern) {
  264. // Scan from highest bits to lowest bits because we will append in LIFO
  265. // fashion, and we need the operands for lower order bits to be consumed first
  266. for (uint32_t candidate_bit = (1u << 31u); candidate_bit;
  267. candidate_bit >>= 1) {
  268. if (candidate_bit & mask) {
  269. spv_operand_desc entry = nullptr;
  270. if (SPV_SUCCESS == spvOperandTableValueLookup(env, operandTable, type,
  271. candidate_bit, &entry)) {
  272. spvPushOperandTypes(entry->operandTypes, pattern);
  273. }
  274. }
  275. }
  276. }
  277. bool spvOperandIsConcrete(spv_operand_type_t type) {
  278. if (spvIsIdType(type) || spvOperandIsConcreteMask(type)) {
  279. return true;
  280. }
  281. switch (type) {
  282. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  283. case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER:
  284. case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER:
  285. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
  286. case SPV_OPERAND_TYPE_LITERAL_STRING:
  287. case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
  288. case SPV_OPERAND_TYPE_EXECUTION_MODEL:
  289. case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
  290. case SPV_OPERAND_TYPE_MEMORY_MODEL:
  291. case SPV_OPERAND_TYPE_EXECUTION_MODE:
  292. case SPV_OPERAND_TYPE_STORAGE_CLASS:
  293. case SPV_OPERAND_TYPE_DIMENSIONALITY:
  294. case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
  295. case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
  296. case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
  297. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
  298. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
  299. case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
  300. case SPV_OPERAND_TYPE_LINKAGE_TYPE:
  301. case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
  302. case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
  303. case SPV_OPERAND_TYPE_DECORATION:
  304. case SPV_OPERAND_TYPE_BUILT_IN:
  305. case SPV_OPERAND_TYPE_GROUP_OPERATION:
  306. case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
  307. case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
  308. case SPV_OPERAND_TYPE_CAPABILITY:
  309. case SPV_OPERAND_TYPE_RAY_FLAGS:
  310. case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
  311. case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
  312. case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
  313. case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  314. case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
  315. case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
  316. case SPV_OPERAND_TYPE_DEBUG_OPERATION:
  317. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  318. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
  319. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
  320. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
  321. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
  322. return true;
  323. default:
  324. break;
  325. }
  326. return false;
  327. }
  328. bool spvOperandIsConcreteMask(spv_operand_type_t type) {
  329. switch (type) {
  330. case SPV_OPERAND_TYPE_IMAGE:
  331. case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
  332. case SPV_OPERAND_TYPE_SELECTION_CONTROL:
  333. case SPV_OPERAND_TYPE_LOOP_CONTROL:
  334. case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
  335. case SPV_OPERAND_TYPE_MEMORY_ACCESS:
  336. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
  337. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
  338. return true;
  339. default:
  340. break;
  341. }
  342. return false;
  343. }
  344. bool spvOperandIsOptional(spv_operand_type_t type) {
  345. switch (type) {
  346. case SPV_OPERAND_TYPE_OPTIONAL_ID:
  347. case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
  348. case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
  349. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
  350. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
  351. case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
  352. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING:
  353. case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
  354. case SPV_OPERAND_TYPE_OPTIONAL_CIV:
  355. return true;
  356. default:
  357. break;
  358. }
  359. // Any variable operand is also optional.
  360. return spvOperandIsVariable(type);
  361. }
  362. bool spvOperandIsVariable(spv_operand_type_t type) {
  363. switch (type) {
  364. case SPV_OPERAND_TYPE_VARIABLE_ID:
  365. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
  366. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
  367. case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
  368. return true;
  369. default:
  370. break;
  371. }
  372. return false;
  373. }
  374. bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
  375. spv_operand_pattern_t* pattern) {
  376. switch (type) {
  377. case SPV_OPERAND_TYPE_VARIABLE_ID:
  378. pattern->push_back(type);
  379. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
  380. return true;
  381. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
  382. pattern->push_back(type);
  383. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER);
  384. return true;
  385. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
  386. // Represents Zero or more (Literal number, Id) pairs,
  387. // where the literal number must be a scalar integer.
  388. pattern->push_back(type);
  389. pattern->push_back(SPV_OPERAND_TYPE_ID);
  390. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER);
  391. return true;
  392. case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
  393. // Represents Zero or more (Id, Literal number) pairs.
  394. pattern->push_back(type);
  395. pattern->push_back(SPV_OPERAND_TYPE_LITERAL_INTEGER);
  396. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
  397. return true;
  398. default:
  399. break;
  400. }
  401. return false;
  402. }
  403. spv_operand_type_t spvTakeFirstMatchableOperand(
  404. spv_operand_pattern_t* pattern) {
  405. assert(!pattern->empty());
  406. spv_operand_type_t result;
  407. do {
  408. result = pattern->back();
  409. pattern->pop_back();
  410. } while (spvExpandOperandSequenceOnce(result, pattern));
  411. return result;
  412. }
  413. spv_operand_pattern_t spvAlternatePatternFollowingImmediate(
  414. const spv_operand_pattern_t& pattern) {
  415. auto it =
  416. std::find(pattern.crbegin(), pattern.crend(), SPV_OPERAND_TYPE_RESULT_ID);
  417. if (it != pattern.crend()) {
  418. spv_operand_pattern_t alternatePattern(it - pattern.crbegin() + 2,
  419. SPV_OPERAND_TYPE_OPTIONAL_CIV);
  420. alternatePattern[1] = SPV_OPERAND_TYPE_RESULT_ID;
  421. return alternatePattern;
  422. }
  423. // No result-id found, so just expect CIVs.
  424. return {SPV_OPERAND_TYPE_OPTIONAL_CIV};
  425. }
  426. bool spvIsIdType(spv_operand_type_t type) {
  427. switch (type) {
  428. case SPV_OPERAND_TYPE_ID:
  429. case SPV_OPERAND_TYPE_TYPE_ID:
  430. case SPV_OPERAND_TYPE_RESULT_ID:
  431. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  432. case SPV_OPERAND_TYPE_SCOPE_ID:
  433. return true;
  434. default:
  435. return false;
  436. }
  437. }
  438. bool spvIsInIdType(spv_operand_type_t type) {
  439. if (!spvIsIdType(type)) {
  440. // If it is not an ID it cannot be an input ID.
  441. return false;
  442. }
  443. switch (type) {
  444. // Deny non-input IDs.
  445. case SPV_OPERAND_TYPE_TYPE_ID:
  446. case SPV_OPERAND_TYPE_RESULT_ID:
  447. return false;
  448. default:
  449. return true;
  450. }
  451. }
  452. std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction(
  453. SpvOp opcode) {
  454. std::function<bool(unsigned index)> out;
  455. switch (opcode) {
  456. case SpvOpExecutionMode:
  457. case SpvOpExecutionModeId:
  458. case SpvOpEntryPoint:
  459. case SpvOpName:
  460. case SpvOpMemberName:
  461. case SpvOpSelectionMerge:
  462. case SpvOpDecorate:
  463. case SpvOpMemberDecorate:
  464. case SpvOpDecorateId:
  465. case SpvOpDecorateStringGOOGLE:
  466. case SpvOpMemberDecorateStringGOOGLE:
  467. case SpvOpTypeStruct:
  468. case SpvOpBranch:
  469. case SpvOpLoopMerge:
  470. out = [](unsigned) { return true; };
  471. break;
  472. case SpvOpGroupDecorate:
  473. case SpvOpGroupMemberDecorate:
  474. case SpvOpBranchConditional:
  475. case SpvOpSwitch:
  476. out = [](unsigned index) { return index != 0; };
  477. break;
  478. case SpvOpFunctionCall:
  479. // The Function parameter.
  480. out = [](unsigned index) { return index == 2; };
  481. break;
  482. case SpvOpPhi:
  483. out = [](unsigned index) { return index > 1; };
  484. break;
  485. case SpvOpEnqueueKernel:
  486. // The Invoke parameter.
  487. out = [](unsigned index) { return index == 8; };
  488. break;
  489. case SpvOpGetKernelNDrangeSubGroupCount:
  490. case SpvOpGetKernelNDrangeMaxSubGroupSize:
  491. // The Invoke parameter.
  492. out = [](unsigned index) { return index == 3; };
  493. break;
  494. case SpvOpGetKernelWorkGroupSize:
  495. case SpvOpGetKernelPreferredWorkGroupSizeMultiple:
  496. // The Invoke parameter.
  497. out = [](unsigned index) { return index == 2; };
  498. break;
  499. case SpvOpTypeForwardPointer:
  500. out = [](unsigned index) { return index == 0; };
  501. break;
  502. case SpvOpTypeArray:
  503. out = [](unsigned index) { return index == 1; };
  504. break;
  505. default:
  506. out = [](unsigned) { return false; };
  507. break;
  508. }
  509. return out;
  510. }
  511. std::function<bool(unsigned)> spvDbgInfoExtOperandCanBeForwardDeclaredFunction(
  512. spv_ext_inst_type_t ext_type, uint32_t key) {
  513. // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/532): Forward
  514. // references for debug info instructions are still in discussion. We must
  515. // update the following lines of code when we conclude the spec.
  516. std::function<bool(unsigned index)> out;
  517. if (ext_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  518. switch (OpenCLDebugInfo100Instructions(key)) {
  519. case OpenCLDebugInfo100DebugFunction:
  520. out = [](unsigned index) { return index == 13; };
  521. break;
  522. case OpenCLDebugInfo100DebugTypeComposite:
  523. out = [](unsigned index) { return index >= 13; };
  524. break;
  525. default:
  526. out = [](unsigned) { return false; };
  527. break;
  528. }
  529. } else {
  530. switch (DebugInfoInstructions(key)) {
  531. case DebugInfoDebugFunction:
  532. out = [](unsigned index) { return index == 13; };
  533. break;
  534. case DebugInfoDebugTypeComposite:
  535. out = [](unsigned index) { return index >= 12; };
  536. break;
  537. default:
  538. out = [](unsigned) { return false; };
  539. break;
  540. }
  541. }
  542. return out;
  543. }