operand.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. assert(0 && "Unhandled operand type!");
  247. break;
  248. }
  249. return "unknown";
  250. }
  251. void spvPushOperandTypes(const spv_operand_type_t* types,
  252. spv_operand_pattern_t* pattern) {
  253. const spv_operand_type_t* endTypes;
  254. for (endTypes = types; *endTypes != SPV_OPERAND_TYPE_NONE; ++endTypes) {
  255. }
  256. while (endTypes-- != types) {
  257. pattern->push_back(*endTypes);
  258. }
  259. }
  260. void spvPushOperandTypesForMask(spv_target_env env,
  261. const spv_operand_table operandTable,
  262. const spv_operand_type_t type,
  263. const uint32_t mask,
  264. spv_operand_pattern_t* pattern) {
  265. // Scan from highest bits to lowest bits because we will append in LIFO
  266. // fashion, and we need the operands for lower order bits to be consumed first
  267. for (uint32_t candidate_bit = (1u << 31u); candidate_bit;
  268. candidate_bit >>= 1) {
  269. if (candidate_bit & mask) {
  270. spv_operand_desc entry = nullptr;
  271. if (SPV_SUCCESS == spvOperandTableValueLookup(env, operandTable, type,
  272. candidate_bit, &entry)) {
  273. spvPushOperandTypes(entry->operandTypes, pattern);
  274. }
  275. }
  276. }
  277. }
  278. bool spvOperandIsConcrete(spv_operand_type_t type) {
  279. if (spvIsIdType(type) || spvOperandIsConcreteMask(type)) {
  280. return true;
  281. }
  282. switch (type) {
  283. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  284. case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER:
  285. case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER:
  286. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
  287. case SPV_OPERAND_TYPE_LITERAL_STRING:
  288. case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
  289. case SPV_OPERAND_TYPE_EXECUTION_MODEL:
  290. case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
  291. case SPV_OPERAND_TYPE_MEMORY_MODEL:
  292. case SPV_OPERAND_TYPE_EXECUTION_MODE:
  293. case SPV_OPERAND_TYPE_STORAGE_CLASS:
  294. case SPV_OPERAND_TYPE_DIMENSIONALITY:
  295. case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
  296. case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
  297. case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
  298. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
  299. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
  300. case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
  301. case SPV_OPERAND_TYPE_LINKAGE_TYPE:
  302. case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
  303. case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
  304. case SPV_OPERAND_TYPE_DECORATION:
  305. case SPV_OPERAND_TYPE_BUILT_IN:
  306. case SPV_OPERAND_TYPE_GROUP_OPERATION:
  307. case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
  308. case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
  309. case SPV_OPERAND_TYPE_CAPABILITY:
  310. case SPV_OPERAND_TYPE_RAY_FLAGS:
  311. case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
  312. case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
  313. case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
  314. case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  315. case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
  316. case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
  317. case SPV_OPERAND_TYPE_DEBUG_OPERATION:
  318. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  319. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
  320. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
  321. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
  322. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
  323. return true;
  324. default:
  325. break;
  326. }
  327. return false;
  328. }
  329. bool spvOperandIsConcreteMask(spv_operand_type_t type) {
  330. switch (type) {
  331. case SPV_OPERAND_TYPE_IMAGE:
  332. case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
  333. case SPV_OPERAND_TYPE_SELECTION_CONTROL:
  334. case SPV_OPERAND_TYPE_LOOP_CONTROL:
  335. case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
  336. case SPV_OPERAND_TYPE_MEMORY_ACCESS:
  337. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
  338. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
  339. return true;
  340. default:
  341. break;
  342. }
  343. return false;
  344. }
  345. bool spvOperandIsOptional(spv_operand_type_t type) {
  346. return SPV_OPERAND_TYPE_FIRST_OPTIONAL_TYPE <= type &&
  347. type <= SPV_OPERAND_TYPE_LAST_OPTIONAL_TYPE;
  348. }
  349. bool spvOperandIsVariable(spv_operand_type_t type) {
  350. return SPV_OPERAND_TYPE_FIRST_VARIABLE_TYPE <= type &&
  351. type <= SPV_OPERAND_TYPE_LAST_VARIABLE_TYPE;
  352. }
  353. bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
  354. spv_operand_pattern_t* pattern) {
  355. switch (type) {
  356. case SPV_OPERAND_TYPE_VARIABLE_ID:
  357. pattern->push_back(type);
  358. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
  359. return true;
  360. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
  361. pattern->push_back(type);
  362. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER);
  363. return true;
  364. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
  365. // Represents Zero or more (Literal number, Id) pairs,
  366. // where the literal number must be a scalar integer.
  367. pattern->push_back(type);
  368. pattern->push_back(SPV_OPERAND_TYPE_ID);
  369. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER);
  370. return true;
  371. case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
  372. // Represents Zero or more (Id, Literal number) pairs.
  373. pattern->push_back(type);
  374. pattern->push_back(SPV_OPERAND_TYPE_LITERAL_INTEGER);
  375. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
  376. return true;
  377. default:
  378. break;
  379. }
  380. return false;
  381. }
  382. spv_operand_type_t spvTakeFirstMatchableOperand(
  383. spv_operand_pattern_t* pattern) {
  384. assert(!pattern->empty());
  385. spv_operand_type_t result;
  386. do {
  387. result = pattern->back();
  388. pattern->pop_back();
  389. } while (spvExpandOperandSequenceOnce(result, pattern));
  390. return result;
  391. }
  392. spv_operand_pattern_t spvAlternatePatternFollowingImmediate(
  393. const spv_operand_pattern_t& pattern) {
  394. auto it =
  395. std::find(pattern.crbegin(), pattern.crend(), SPV_OPERAND_TYPE_RESULT_ID);
  396. if (it != pattern.crend()) {
  397. spv_operand_pattern_t alternatePattern(it - pattern.crbegin() + 2,
  398. SPV_OPERAND_TYPE_OPTIONAL_CIV);
  399. alternatePattern[1] = SPV_OPERAND_TYPE_RESULT_ID;
  400. return alternatePattern;
  401. }
  402. // No result-id found, so just expect CIVs.
  403. return {SPV_OPERAND_TYPE_OPTIONAL_CIV};
  404. }
  405. bool spvIsIdType(spv_operand_type_t type) {
  406. switch (type) {
  407. case SPV_OPERAND_TYPE_ID:
  408. case SPV_OPERAND_TYPE_TYPE_ID:
  409. case SPV_OPERAND_TYPE_RESULT_ID:
  410. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  411. case SPV_OPERAND_TYPE_SCOPE_ID:
  412. return true;
  413. default:
  414. return false;
  415. }
  416. }
  417. bool spvIsInIdType(spv_operand_type_t type) {
  418. if (!spvIsIdType(type)) {
  419. // If it is not an ID it cannot be an input ID.
  420. return false;
  421. }
  422. switch (type) {
  423. // Blacklist non-input IDs.
  424. case SPV_OPERAND_TYPE_TYPE_ID:
  425. case SPV_OPERAND_TYPE_RESULT_ID:
  426. return false;
  427. default:
  428. return true;
  429. }
  430. }
  431. std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction(
  432. SpvOp opcode) {
  433. std::function<bool(unsigned index)> out;
  434. switch (opcode) {
  435. case SpvOpExecutionMode:
  436. case SpvOpExecutionModeId:
  437. case SpvOpEntryPoint:
  438. case SpvOpName:
  439. case SpvOpMemberName:
  440. case SpvOpSelectionMerge:
  441. case SpvOpDecorate:
  442. case SpvOpMemberDecorate:
  443. case SpvOpDecorateId:
  444. case SpvOpDecorateStringGOOGLE:
  445. case SpvOpMemberDecorateStringGOOGLE:
  446. case SpvOpTypeStruct:
  447. case SpvOpBranch:
  448. case SpvOpLoopMerge:
  449. out = [](unsigned) { return true; };
  450. break;
  451. case SpvOpGroupDecorate:
  452. case SpvOpGroupMemberDecorate:
  453. case SpvOpBranchConditional:
  454. case SpvOpSwitch:
  455. out = [](unsigned index) { return index != 0; };
  456. break;
  457. case SpvOpFunctionCall:
  458. // The Function parameter.
  459. out = [](unsigned index) { return index == 2; };
  460. break;
  461. case SpvOpPhi:
  462. out = [](unsigned index) { return index > 1; };
  463. break;
  464. case SpvOpEnqueueKernel:
  465. // The Invoke parameter.
  466. out = [](unsigned index) { return index == 8; };
  467. break;
  468. case SpvOpGetKernelNDrangeSubGroupCount:
  469. case SpvOpGetKernelNDrangeMaxSubGroupSize:
  470. // The Invoke parameter.
  471. out = [](unsigned index) { return index == 3; };
  472. break;
  473. case SpvOpGetKernelWorkGroupSize:
  474. case SpvOpGetKernelPreferredWorkGroupSizeMultiple:
  475. // The Invoke parameter.
  476. out = [](unsigned index) { return index == 2; };
  477. break;
  478. case SpvOpTypeForwardPointer:
  479. out = [](unsigned index) { return index == 0; };
  480. break;
  481. case SpvOpTypeArray:
  482. out = [](unsigned index) { return index == 1; };
  483. break;
  484. default:
  485. out = [](unsigned) { return false; };
  486. break;
  487. }
  488. return out;
  489. }
  490. std::function<bool(unsigned)> spvDbgInfoExtOperandCanBeForwardDeclaredFunction(
  491. spv_ext_inst_type_t ext_type, uint32_t key) {
  492. // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/532): Forward
  493. // references for debug info instructions are still in discussion. We must
  494. // update the following lines of code when we conclude the spec.
  495. std::function<bool(unsigned index)> out;
  496. if (ext_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  497. switch (OpenCLDebugInfo100Instructions(key)) {
  498. case OpenCLDebugInfo100DebugFunction:
  499. out = [](unsigned index) { return index == 13; };
  500. break;
  501. case OpenCLDebugInfo100DebugTypeComposite:
  502. out = [](unsigned index) { return index >= 13; };
  503. break;
  504. default:
  505. out = [](unsigned) { return false; };
  506. break;
  507. }
  508. } else {
  509. switch (DebugInfoInstructions(key)) {
  510. case DebugInfoDebugFunction:
  511. out = [](unsigned index) { return index == 13; };
  512. break;
  513. case DebugInfoDebugTypeComposite:
  514. out = [](unsigned index) { return index >= 12; };
  515. break;
  516. default:
  517. out = [](unsigned) { return false; };
  518. break;
  519. }
  520. }
  521. return out;
  522. }