operand.cpp 17 KB

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