validate_logicals.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright (c) 2017 Google 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. // Validates correctness of logical SPIR-V instructions.
  15. #include "source/val/validate.h"
  16. #include "source/diagnostic.h"
  17. #include "source/opcode.h"
  18. #include "source/val/instruction.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. // Validates correctness of logical instructions.
  23. spv_result_t LogicalsPass(ValidationState_t& _, const Instruction* inst) {
  24. const SpvOp opcode = inst->opcode();
  25. const uint32_t result_type = inst->type_id();
  26. switch (opcode) {
  27. case SpvOpAny:
  28. case SpvOpAll: {
  29. if (!_.IsBoolScalarType(result_type))
  30. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  31. << "Expected bool scalar type as Result Type: "
  32. << spvOpcodeString(opcode);
  33. const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
  34. if (!vector_type || !_.IsBoolVectorType(vector_type))
  35. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  36. << "Expected operand to be vector bool: "
  37. << spvOpcodeString(opcode);
  38. break;
  39. }
  40. case SpvOpIsNan:
  41. case SpvOpIsInf:
  42. case SpvOpIsFinite:
  43. case SpvOpIsNormal:
  44. case SpvOpSignBitSet: {
  45. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  46. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  47. << "Expected bool scalar or vector type as Result Type: "
  48. << spvOpcodeString(opcode);
  49. const uint32_t operand_type = _.GetOperandTypeId(inst, 2);
  50. if (!operand_type || (!_.IsFloatScalarType(operand_type) &&
  51. !_.IsFloatVectorType(operand_type)))
  52. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  53. << "Expected operand to be scalar or vector float: "
  54. << spvOpcodeString(opcode);
  55. if (_.GetDimension(result_type) != _.GetDimension(operand_type))
  56. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  57. << "Expected vector sizes of Result Type and the operand to be "
  58. "equal: "
  59. << spvOpcodeString(opcode);
  60. break;
  61. }
  62. case SpvOpFOrdEqual:
  63. case SpvOpFUnordEqual:
  64. case SpvOpFOrdNotEqual:
  65. case SpvOpFUnordNotEqual:
  66. case SpvOpFOrdLessThan:
  67. case SpvOpFUnordLessThan:
  68. case SpvOpFOrdGreaterThan:
  69. case SpvOpFUnordGreaterThan:
  70. case SpvOpFOrdLessThanEqual:
  71. case SpvOpFUnordLessThanEqual:
  72. case SpvOpFOrdGreaterThanEqual:
  73. case SpvOpFUnordGreaterThanEqual:
  74. case SpvOpLessOrGreater:
  75. case SpvOpOrdered:
  76. case SpvOpUnordered: {
  77. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  78. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  79. << "Expected bool scalar or vector type as Result Type: "
  80. << spvOpcodeString(opcode);
  81. const uint32_t left_operand_type = _.GetOperandTypeId(inst, 2);
  82. if (!left_operand_type || (!_.IsFloatScalarType(left_operand_type) &&
  83. !_.IsFloatVectorType(left_operand_type)))
  84. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  85. << "Expected operands to be scalar or vector float: "
  86. << spvOpcodeString(opcode);
  87. if (_.GetDimension(result_type) != _.GetDimension(left_operand_type))
  88. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  89. << "Expected vector sizes of Result Type and the operands to be "
  90. "equal: "
  91. << spvOpcodeString(opcode);
  92. if (left_operand_type != _.GetOperandTypeId(inst, 3))
  93. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  94. << "Expected left and right operands to have the same type: "
  95. << spvOpcodeString(opcode);
  96. break;
  97. }
  98. case SpvOpLogicalEqual:
  99. case SpvOpLogicalNotEqual:
  100. case SpvOpLogicalOr:
  101. case SpvOpLogicalAnd: {
  102. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  103. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  104. << "Expected bool scalar or vector type as Result Type: "
  105. << spvOpcodeString(opcode);
  106. if (result_type != _.GetOperandTypeId(inst, 2) ||
  107. result_type != _.GetOperandTypeId(inst, 3))
  108. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  109. << "Expected both operands to be of Result Type: "
  110. << spvOpcodeString(opcode);
  111. break;
  112. }
  113. case SpvOpLogicalNot: {
  114. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  115. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  116. << "Expected bool scalar or vector type as Result Type: "
  117. << spvOpcodeString(opcode);
  118. if (result_type != _.GetOperandTypeId(inst, 2))
  119. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  120. << "Expected operand to be of Result Type: "
  121. << spvOpcodeString(opcode);
  122. break;
  123. }
  124. case SpvOpSelect: {
  125. uint32_t dimension = 1;
  126. {
  127. const Instruction* type_inst = _.FindDef(result_type);
  128. assert(type_inst);
  129. const auto composites = _.features().select_between_composites;
  130. auto fail = [&_, composites, inst, opcode]() -> spv_result_t {
  131. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  132. << "Expected scalar or "
  133. << (composites ? "composite" : "vector")
  134. << " type as Result Type: " << spvOpcodeString(opcode);
  135. };
  136. const SpvOp type_opcode = type_inst->opcode();
  137. switch (type_opcode) {
  138. case SpvOpTypePointer: {
  139. if (_.addressing_model() == SpvAddressingModelLogical &&
  140. !_.features().variable_pointers &&
  141. !_.features().variable_pointers_storage_buffer)
  142. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  143. << "Using pointers with OpSelect requires capability "
  144. << "VariablePointers or VariablePointersStorageBuffer";
  145. break;
  146. }
  147. case SpvOpTypeVector: {
  148. dimension = type_inst->word(3);
  149. break;
  150. }
  151. case SpvOpTypeBool:
  152. case SpvOpTypeInt:
  153. case SpvOpTypeFloat: {
  154. break;
  155. }
  156. // Not RuntimeArray because of other rules.
  157. case SpvOpTypeArray:
  158. case SpvOpTypeMatrix:
  159. case SpvOpTypeStruct: {
  160. if (!composites) return fail();
  161. break;
  162. };
  163. default:
  164. return fail();
  165. }
  166. const uint32_t condition_type = _.GetOperandTypeId(inst, 2);
  167. const uint32_t left_type = _.GetOperandTypeId(inst, 3);
  168. const uint32_t right_type = _.GetOperandTypeId(inst, 4);
  169. if (!condition_type || (!_.IsBoolScalarType(condition_type) &&
  170. !_.IsBoolVectorType(condition_type)))
  171. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  172. << "Expected bool scalar or vector type as condition: "
  173. << spvOpcodeString(opcode);
  174. if (_.GetDimension(condition_type) != dimension) {
  175. // If the condition is a vector type, then the result must also be a
  176. // vector with matching dimensions. In SPIR-V 1.4, a scalar condition
  177. // can be used to select between vector types. |composites| is a
  178. // proxy for SPIR-V 1.4 functionality.
  179. if (!composites || _.IsBoolVectorType(condition_type)) {
  180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  181. << "Expected vector sizes of Result Type and the condition "
  182. "to be equal: "
  183. << spvOpcodeString(opcode);
  184. }
  185. }
  186. if (result_type != left_type || result_type != right_type)
  187. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  188. << "Expected both objects to be of Result Type: "
  189. << spvOpcodeString(opcode);
  190. break;
  191. }
  192. }
  193. case SpvOpIEqual:
  194. case SpvOpINotEqual:
  195. case SpvOpUGreaterThan:
  196. case SpvOpUGreaterThanEqual:
  197. case SpvOpULessThan:
  198. case SpvOpULessThanEqual:
  199. case SpvOpSGreaterThan:
  200. case SpvOpSGreaterThanEqual:
  201. case SpvOpSLessThan:
  202. case SpvOpSLessThanEqual: {
  203. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  204. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  205. << "Expected bool scalar or vector type as Result Type: "
  206. << spvOpcodeString(opcode);
  207. const uint32_t left_type = _.GetOperandTypeId(inst, 2);
  208. const uint32_t right_type = _.GetOperandTypeId(inst, 3);
  209. if (!left_type ||
  210. (!_.IsIntScalarType(left_type) && !_.IsIntVectorType(left_type)))
  211. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  212. << "Expected operands to be scalar or vector int: "
  213. << spvOpcodeString(opcode);
  214. if (_.GetDimension(result_type) != _.GetDimension(left_type))
  215. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  216. << "Expected vector sizes of Result Type and the operands to be"
  217. << " equal: " << spvOpcodeString(opcode);
  218. if (!right_type ||
  219. (!_.IsIntScalarType(right_type) && !_.IsIntVectorType(right_type)))
  220. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  221. << "Expected operands to be scalar or vector int: "
  222. << spvOpcodeString(opcode);
  223. if (_.GetDimension(result_type) != _.GetDimension(right_type))
  224. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  225. << "Expected vector sizes of Result Type and the operands to be"
  226. << " equal: " << spvOpcodeString(opcode);
  227. if (_.GetBitWidth(left_type) != _.GetBitWidth(right_type))
  228. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  229. << "Expected both operands to have the same component bit "
  230. "width: "
  231. << spvOpcodeString(opcode);
  232. break;
  233. }
  234. default:
  235. break;
  236. }
  237. return SPV_SUCCESS;
  238. }
  239. } // namespace val
  240. } // namespace spvtools