operand.cpp 17 KB

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