operand.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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_PACKED_VECTOR_FORMAT:
  213. case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT:
  214. return "packed vector format";
  215. case SPV_OPERAND_TYPE_IMAGE:
  216. case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
  217. return "image";
  218. case SPV_OPERAND_TYPE_OPTIONAL_CIV:
  219. return "context-insensitive value";
  220. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
  221. return "debug info flags";
  222. case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  223. return "debug base type encoding";
  224. case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
  225. return "debug composite type";
  226. case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
  227. return "debug type qualifier";
  228. case SPV_OPERAND_TYPE_DEBUG_OPERATION:
  229. return "debug operation";
  230. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
  231. return "OpenCL.DebugInfo.100 debug info flags";
  232. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  233. return "OpenCL.DebugInfo.100 debug base type encoding";
  234. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
  235. return "OpenCL.DebugInfo.100 debug composite type";
  236. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
  237. return "OpenCL.DebugInfo.100 debug type qualifier";
  238. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
  239. return "OpenCL.DebugInfo.100 debug operation";
  240. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
  241. return "OpenCL.DebugInfo.100 debug imported entity";
  242. // The next values are for values returned from an instruction, not actually
  243. // an operand. So the specific strings don't matter. But let's add them
  244. // for completeness and ease of testing.
  245. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
  246. return "image channel order";
  247. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
  248. return "image channel data type";
  249. case SPV_OPERAND_TYPE_FPDENORM_MODE:
  250. return "FP denorm mode";
  251. case SPV_OPERAND_TYPE_FPOPERATION_MODE:
  252. return "FP operation mode";
  253. case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
  254. return "quantization mode";
  255. case SPV_OPERAND_TYPE_OVERFLOW_MODES:
  256. return "overflow mode";
  257. case SPV_OPERAND_TYPE_NONE:
  258. return "NONE";
  259. default:
  260. break;
  261. }
  262. return "unknown";
  263. }
  264. void spvPushOperandTypes(const spv_operand_type_t* types,
  265. spv_operand_pattern_t* pattern) {
  266. const spv_operand_type_t* endTypes;
  267. for (endTypes = types; *endTypes != SPV_OPERAND_TYPE_NONE; ++endTypes) {
  268. }
  269. while (endTypes-- != types) {
  270. pattern->push_back(*endTypes);
  271. }
  272. }
  273. void spvPushOperandTypesForMask(spv_target_env env,
  274. const spv_operand_table operandTable,
  275. const spv_operand_type_t type,
  276. const uint32_t mask,
  277. spv_operand_pattern_t* pattern) {
  278. // Scan from highest bits to lowest bits because we will append in LIFO
  279. // fashion, and we need the operands for lower order bits to be consumed first
  280. for (uint32_t candidate_bit = (1u << 31u); candidate_bit;
  281. candidate_bit >>= 1) {
  282. if (candidate_bit & mask) {
  283. spv_operand_desc entry = nullptr;
  284. if (SPV_SUCCESS == spvOperandTableValueLookup(env, operandTable, type,
  285. candidate_bit, &entry)) {
  286. spvPushOperandTypes(entry->operandTypes, pattern);
  287. }
  288. }
  289. }
  290. }
  291. bool spvOperandIsConcrete(spv_operand_type_t type) {
  292. if (spvIsIdType(type) || spvOperandIsConcreteMask(type)) {
  293. return true;
  294. }
  295. switch (type) {
  296. case SPV_OPERAND_TYPE_LITERAL_INTEGER:
  297. case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER:
  298. case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER:
  299. case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
  300. case SPV_OPERAND_TYPE_LITERAL_STRING:
  301. case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
  302. case SPV_OPERAND_TYPE_EXECUTION_MODEL:
  303. case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
  304. case SPV_OPERAND_TYPE_MEMORY_MODEL:
  305. case SPV_OPERAND_TYPE_EXECUTION_MODE:
  306. case SPV_OPERAND_TYPE_STORAGE_CLASS:
  307. case SPV_OPERAND_TYPE_DIMENSIONALITY:
  308. case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
  309. case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
  310. case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
  311. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER:
  312. case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE:
  313. case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
  314. case SPV_OPERAND_TYPE_LINKAGE_TYPE:
  315. case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
  316. case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
  317. case SPV_OPERAND_TYPE_DECORATION:
  318. case SPV_OPERAND_TYPE_BUILT_IN:
  319. case SPV_OPERAND_TYPE_GROUP_OPERATION:
  320. case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
  321. case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
  322. case SPV_OPERAND_TYPE_CAPABILITY:
  323. case SPV_OPERAND_TYPE_RAY_FLAGS:
  324. case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
  325. case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
  326. case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
  327. case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  328. case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
  329. case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
  330. case SPV_OPERAND_TYPE_DEBUG_OPERATION:
  331. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
  332. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
  333. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
  334. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
  335. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
  336. case SPV_OPERAND_TYPE_FPDENORM_MODE:
  337. case SPV_OPERAND_TYPE_FPOPERATION_MODE:
  338. case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
  339. case SPV_OPERAND_TYPE_OVERFLOW_MODES:
  340. case SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT:
  341. return true;
  342. default:
  343. break;
  344. }
  345. return false;
  346. }
  347. bool spvOperandIsConcreteMask(spv_operand_type_t type) {
  348. switch (type) {
  349. case SPV_OPERAND_TYPE_IMAGE:
  350. case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
  351. case SPV_OPERAND_TYPE_SELECTION_CONTROL:
  352. case SPV_OPERAND_TYPE_LOOP_CONTROL:
  353. case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
  354. case SPV_OPERAND_TYPE_MEMORY_ACCESS:
  355. case SPV_OPERAND_TYPE_FRAGMENT_SHADING_RATE:
  356. case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
  357. case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
  358. return true;
  359. default:
  360. break;
  361. }
  362. return false;
  363. }
  364. bool spvOperandIsOptional(spv_operand_type_t type) {
  365. switch (type) {
  366. case SPV_OPERAND_TYPE_OPTIONAL_ID:
  367. case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
  368. case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
  369. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
  370. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
  371. case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
  372. case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING:
  373. case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
  374. case SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT:
  375. case SPV_OPERAND_TYPE_OPTIONAL_CIV:
  376. return true;
  377. default:
  378. break;
  379. }
  380. // Any variable operand is also optional.
  381. return spvOperandIsVariable(type);
  382. }
  383. bool spvOperandIsVariable(spv_operand_type_t type) {
  384. switch (type) {
  385. case SPV_OPERAND_TYPE_VARIABLE_ID:
  386. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
  387. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
  388. case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
  389. return true;
  390. default:
  391. break;
  392. }
  393. return false;
  394. }
  395. bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
  396. spv_operand_pattern_t* pattern) {
  397. switch (type) {
  398. case SPV_OPERAND_TYPE_VARIABLE_ID:
  399. pattern->push_back(type);
  400. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
  401. return true;
  402. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER:
  403. pattern->push_back(type);
  404. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER);
  405. return true;
  406. case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID:
  407. // Represents Zero or more (Literal number, Id) pairs,
  408. // where the literal number must be a scalar integer.
  409. pattern->push_back(type);
  410. pattern->push_back(SPV_OPERAND_TYPE_ID);
  411. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER);
  412. return true;
  413. case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER:
  414. // Represents Zero or more (Id, Literal number) pairs.
  415. pattern->push_back(type);
  416. pattern->push_back(SPV_OPERAND_TYPE_LITERAL_INTEGER);
  417. pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID);
  418. return true;
  419. default:
  420. break;
  421. }
  422. return false;
  423. }
  424. spv_operand_type_t spvTakeFirstMatchableOperand(
  425. spv_operand_pattern_t* pattern) {
  426. assert(!pattern->empty());
  427. spv_operand_type_t result;
  428. do {
  429. result = pattern->back();
  430. pattern->pop_back();
  431. } while (spvExpandOperandSequenceOnce(result, pattern));
  432. return result;
  433. }
  434. spv_operand_pattern_t spvAlternatePatternFollowingImmediate(
  435. const spv_operand_pattern_t& pattern) {
  436. auto it =
  437. std::find(pattern.crbegin(), pattern.crend(), SPV_OPERAND_TYPE_RESULT_ID);
  438. if (it != pattern.crend()) {
  439. spv_operand_pattern_t alternatePattern(it - pattern.crbegin() + 2,
  440. SPV_OPERAND_TYPE_OPTIONAL_CIV);
  441. alternatePattern[1] = SPV_OPERAND_TYPE_RESULT_ID;
  442. return alternatePattern;
  443. }
  444. // No result-id found, so just expect CIVs.
  445. return {SPV_OPERAND_TYPE_OPTIONAL_CIV};
  446. }
  447. bool spvIsIdType(spv_operand_type_t type) {
  448. switch (type) {
  449. case SPV_OPERAND_TYPE_ID:
  450. case SPV_OPERAND_TYPE_TYPE_ID:
  451. case SPV_OPERAND_TYPE_RESULT_ID:
  452. case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
  453. case SPV_OPERAND_TYPE_SCOPE_ID:
  454. return true;
  455. default:
  456. return false;
  457. }
  458. }
  459. bool spvIsInIdType(spv_operand_type_t type) {
  460. if (!spvIsIdType(type)) {
  461. // If it is not an ID it cannot be an input ID.
  462. return false;
  463. }
  464. switch (type) {
  465. // Deny non-input IDs.
  466. case SPV_OPERAND_TYPE_TYPE_ID:
  467. case SPV_OPERAND_TYPE_RESULT_ID:
  468. return false;
  469. default:
  470. return true;
  471. }
  472. }
  473. std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction(
  474. SpvOp opcode) {
  475. std::function<bool(unsigned index)> out;
  476. if (spvOpcodeGeneratesType(opcode)) {
  477. // All types can use forward pointers.
  478. out = [](unsigned) { return true; };
  479. return out;
  480. }
  481. switch (opcode) {
  482. case SpvOpExecutionMode:
  483. case SpvOpExecutionModeId:
  484. case SpvOpEntryPoint:
  485. case SpvOpName:
  486. case SpvOpMemberName:
  487. case SpvOpSelectionMerge:
  488. case SpvOpDecorate:
  489. case SpvOpMemberDecorate:
  490. case SpvOpDecorateId:
  491. case SpvOpDecorateStringGOOGLE:
  492. case SpvOpMemberDecorateStringGOOGLE:
  493. case SpvOpBranch:
  494. case SpvOpLoopMerge:
  495. out = [](unsigned) { return true; };
  496. break;
  497. case SpvOpGroupDecorate:
  498. case SpvOpGroupMemberDecorate:
  499. case SpvOpBranchConditional:
  500. case SpvOpSwitch:
  501. out = [](unsigned index) { return index != 0; };
  502. break;
  503. case SpvOpFunctionCall:
  504. // The Function parameter.
  505. out = [](unsigned index) { return index == 2; };
  506. break;
  507. case SpvOpPhi:
  508. out = [](unsigned index) { return index > 1; };
  509. break;
  510. case SpvOpEnqueueKernel:
  511. // The Invoke parameter.
  512. out = [](unsigned index) { return index == 8; };
  513. break;
  514. case SpvOpGetKernelNDrangeSubGroupCount:
  515. case SpvOpGetKernelNDrangeMaxSubGroupSize:
  516. // The Invoke parameter.
  517. out = [](unsigned index) { return index == 3; };
  518. break;
  519. case SpvOpGetKernelWorkGroupSize:
  520. case SpvOpGetKernelPreferredWorkGroupSizeMultiple:
  521. // The Invoke parameter.
  522. out = [](unsigned index) { return index == 2; };
  523. break;
  524. case SpvOpTypeForwardPointer:
  525. out = [](unsigned index) { return index == 0; };
  526. break;
  527. case SpvOpTypeArray:
  528. out = [](unsigned index) { return index == 1; };
  529. break;
  530. default:
  531. out = [](unsigned) { return false; };
  532. break;
  533. }
  534. return out;
  535. }
  536. std::function<bool(unsigned)> spvDbgInfoExtOperandCanBeForwardDeclaredFunction(
  537. spv_ext_inst_type_t ext_type, uint32_t key) {
  538. // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/532): Forward
  539. // references for debug info instructions are still in discussion. We must
  540. // update the following lines of code when we conclude the spec.
  541. std::function<bool(unsigned index)> out;
  542. if (ext_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  543. switch (OpenCLDebugInfo100Instructions(key)) {
  544. case OpenCLDebugInfo100DebugFunction:
  545. out = [](unsigned index) { return index == 13; };
  546. break;
  547. case OpenCLDebugInfo100DebugTypeComposite:
  548. out = [](unsigned index) { return index >= 13; };
  549. break;
  550. default:
  551. out = [](unsigned) { return false; };
  552. break;
  553. }
  554. } else {
  555. switch (DebugInfoInstructions(key)) {
  556. case DebugInfoDebugFunction:
  557. out = [](unsigned index) { return index == 13; };
  558. break;
  559. case DebugInfoDebugTypeComposite:
  560. out = [](unsigned index) { return index >= 12; };
  561. break;
  562. default:
  563. out = [](unsigned) { return false; };
  564. break;
  565. }
  566. }
  567. return out;
  568. }