operand.cpp 17 KB

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