operand.cpp 21 KB

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