assembly_grammar.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/assembly_grammar.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <cstring>
  18. #include "source/ext_inst.h"
  19. #include "source/opcode.h"
  20. #include "source/operand.h"
  21. #include "source/spirv_target_env.h"
  22. #include "source/table.h"
  23. #include "source/table2.h"
  24. namespace spvtools {
  25. namespace {
  26. /// @brief Parses a mask expression string for the given operand type.
  27. ///
  28. /// A mask expression is a sequence of one or more terms separated by '|',
  29. /// where each term a named enum value for the given type. No whitespace
  30. /// is permitted.
  31. ///
  32. /// On success, the value is written to pValue.
  33. ///
  34. /// @param[in] type of the operand
  35. /// @param[in] textValue word of text to be parsed
  36. /// @param[out] pValue where the resulting value is written
  37. ///
  38. /// @return result code
  39. spv_result_t spvTextParseMaskOperand(const spv_operand_type_t type,
  40. const char* textValue, uint32_t* pValue) {
  41. if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
  42. size_t text_length = strlen(textValue);
  43. if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
  44. const char* text_end = textValue + text_length;
  45. // We only support mask expressions in ASCII, so the separator value is a
  46. // char.
  47. const char separator = '|';
  48. // Accumulate the result by interpreting one word at a time, scanning
  49. // from left to right.
  50. uint32_t value = 0;
  51. const char* begin = textValue; // The left end of the current word.
  52. const char* end = nullptr; // One character past the end of the current word.
  53. do {
  54. end = std::find(begin, text_end, separator);
  55. const spvtools::OperandDesc* entry = nullptr;
  56. if (auto error =
  57. spvtools::LookupOperand(type, begin, end - begin, &entry)) {
  58. return error;
  59. }
  60. value |= entry->value;
  61. // Advance to the next word by skipping over the separator.
  62. begin = end + 1;
  63. } while (end != text_end);
  64. *pValue = value;
  65. return SPV_SUCCESS;
  66. }
  67. // Associates an opcode with its name.
  68. struct SpecConstantOpcodeEntry {
  69. spv::Op opcode;
  70. const char* name;
  71. };
  72. // All the opcodes allowed as the operation for OpSpecConstantOp.
  73. // The name does not have the usual "Op" prefix. For example opcode
  74. // spv::Op::IAdd is associated with the name "IAdd".
  75. //
  76. // clang-format off
  77. #define CASE(NAME) { spv::Op::Op##NAME, #NAME }
  78. const SpecConstantOpcodeEntry kOpSpecConstantOpcodes[] = {
  79. // Conversion
  80. CASE(SConvert),
  81. CASE(FConvert),
  82. CASE(ConvertFToS),
  83. CASE(ConvertSToF),
  84. CASE(ConvertFToU),
  85. CASE(ConvertUToF),
  86. CASE(UConvert),
  87. CASE(ConvertPtrToU),
  88. CASE(ConvertUToPtr),
  89. CASE(GenericCastToPtr),
  90. CASE(PtrCastToGeneric),
  91. CASE(Bitcast),
  92. CASE(QuantizeToF16),
  93. // Arithmetic
  94. CASE(SNegate),
  95. CASE(Not),
  96. CASE(IAdd),
  97. CASE(ISub),
  98. CASE(IMul),
  99. CASE(UDiv),
  100. CASE(SDiv),
  101. CASE(UMod),
  102. CASE(SRem),
  103. CASE(SMod),
  104. CASE(ShiftRightLogical),
  105. CASE(ShiftRightArithmetic),
  106. CASE(ShiftLeftLogical),
  107. CASE(BitwiseOr),
  108. CASE(BitwiseAnd),
  109. CASE(BitwiseXor),
  110. CASE(FNegate),
  111. CASE(FAdd),
  112. CASE(FSub),
  113. CASE(FMul),
  114. CASE(FDiv),
  115. CASE(FRem),
  116. CASE(FMod),
  117. // Composite
  118. CASE(VectorShuffle),
  119. CASE(CompositeExtract),
  120. CASE(CompositeInsert),
  121. // Logical
  122. CASE(LogicalOr),
  123. CASE(LogicalAnd),
  124. CASE(LogicalNot),
  125. CASE(LogicalEqual),
  126. CASE(LogicalNotEqual),
  127. CASE(Select),
  128. // Comparison
  129. CASE(IEqual),
  130. CASE(INotEqual),
  131. CASE(ULessThan),
  132. CASE(SLessThan),
  133. CASE(UGreaterThan),
  134. CASE(SGreaterThan),
  135. CASE(ULessThanEqual),
  136. CASE(SLessThanEqual),
  137. CASE(UGreaterThanEqual),
  138. CASE(SGreaterThanEqual),
  139. // Memory
  140. CASE(AccessChain),
  141. CASE(InBoundsAccessChain),
  142. CASE(PtrAccessChain),
  143. CASE(InBoundsPtrAccessChain),
  144. CASE(CooperativeMatrixLengthNV),
  145. CASE(CooperativeMatrixLengthKHR)
  146. };
  147. // The 60 is determined by counting the opcodes listed in the spec.
  148. static_assert(61 == sizeof(kOpSpecConstantOpcodes)/sizeof(kOpSpecConstantOpcodes[0]),
  149. "OpSpecConstantOp opcode table is incomplete");
  150. #undef CASE
  151. // clang-format on
  152. const size_t kNumOpSpecConstantOpcodes =
  153. sizeof(kOpSpecConstantOpcodes) / sizeof(kOpSpecConstantOpcodes[0]);
  154. } // namespace
  155. CapabilitySet AssemblyGrammar::filterCapsAgainstTargetEnv(
  156. const spv::Capability* cap_array, uint32_t count) const {
  157. CapabilitySet cap_set;
  158. const auto version = spvVersionForTargetEnv(target_env_);
  159. for (uint32_t i = 0; i < count; ++i) {
  160. const spvtools::OperandDesc* entry = nullptr;
  161. if (SPV_SUCCESS ==
  162. spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
  163. static_cast<uint32_t>(cap_array[i]), &entry)) {
  164. // This token is visible in this environment if it's in an appropriate
  165. // core version, or it is enabled by a capability or an extension.
  166. if ((version >= entry->minVersion && version <= entry->lastVersion) ||
  167. entry->extensions_range.count() > 0u ||
  168. entry->capabilities_range.count() > 0u) {
  169. cap_set.insert(cap_array[i]);
  170. }
  171. }
  172. }
  173. return cap_set;
  174. }
  175. const char* AssemblyGrammar::lookupOperandName(spv_operand_type_t type,
  176. uint32_t operand) const {
  177. const spvtools::OperandDesc* desc = nullptr;
  178. if (spvtools::LookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
  179. return "Unknown";
  180. }
  181. return desc->name().data();
  182. }
  183. spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(const char* name,
  184. spv::Op* opcode) const {
  185. const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
  186. const auto* found =
  187. std::find_if(kOpSpecConstantOpcodes, last,
  188. [name](const SpecConstantOpcodeEntry& entry) {
  189. return 0 == strcmp(name, entry.name);
  190. });
  191. if (found == last) return SPV_ERROR_INVALID_LOOKUP;
  192. *opcode = found->opcode;
  193. return SPV_SUCCESS;
  194. }
  195. spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(spv::Op opcode) const {
  196. const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
  197. const auto* found =
  198. std::find_if(kOpSpecConstantOpcodes, last,
  199. [opcode](const SpecConstantOpcodeEntry& entry) {
  200. return opcode == entry.opcode;
  201. });
  202. if (found == last) return SPV_ERROR_INVALID_LOOKUP;
  203. return SPV_SUCCESS;
  204. }
  205. spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
  206. const char* textValue,
  207. uint32_t* pValue) const {
  208. return spvTextParseMaskOperand(type, textValue, pValue);
  209. }
  210. void AssemblyGrammar::pushOperandTypesForMask(
  211. const spv_operand_type_t type, const uint32_t mask,
  212. spv_operand_pattern_t* pattern) const {
  213. spvPushOperandTypesForMask(type, mask, pattern);
  214. }
  215. } // namespace spvtools