validate_logicals.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 SpvOp type_opcode = type_inst->opcode();
  130. switch (type_opcode) {
  131. case SpvOpTypePointer: {
  132. if (_.addressing_model() == SpvAddressingModelLogical &&
  133. !_.features().variable_pointers &&
  134. !_.features().variable_pointers_storage_buffer)
  135. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  136. << "Using pointers with OpSelect requires capability "
  137. << "VariablePointers or VariablePointersStorageBuffer";
  138. break;
  139. }
  140. case SpvOpTypeVector: {
  141. dimension = type_inst->word(3);
  142. break;
  143. }
  144. case SpvOpTypeBool:
  145. case SpvOpTypeInt:
  146. case SpvOpTypeFloat: {
  147. break;
  148. }
  149. default: {
  150. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  151. << "Expected scalar or vector type as Result Type: "
  152. << spvOpcodeString(opcode);
  153. }
  154. }
  155. }
  156. const uint32_t condition_type = _.GetOperandTypeId(inst, 2);
  157. const uint32_t left_type = _.GetOperandTypeId(inst, 3);
  158. const uint32_t right_type = _.GetOperandTypeId(inst, 4);
  159. if (!condition_type || (!_.IsBoolScalarType(condition_type) &&
  160. !_.IsBoolVectorType(condition_type)))
  161. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  162. << "Expected bool scalar or vector type as condition: "
  163. << spvOpcodeString(opcode);
  164. if (_.GetDimension(condition_type) != dimension)
  165. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  166. << "Expected vector sizes of Result Type and the condition to be"
  167. << " equal: " << spvOpcodeString(opcode);
  168. if (result_type != left_type || result_type != right_type)
  169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  170. << "Expected both objects to be of Result Type: "
  171. << spvOpcodeString(opcode);
  172. break;
  173. }
  174. case SpvOpIEqual:
  175. case SpvOpINotEqual:
  176. case SpvOpUGreaterThan:
  177. case SpvOpUGreaterThanEqual:
  178. case SpvOpULessThan:
  179. case SpvOpULessThanEqual:
  180. case SpvOpSGreaterThan:
  181. case SpvOpSGreaterThanEqual:
  182. case SpvOpSLessThan:
  183. case SpvOpSLessThanEqual: {
  184. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  185. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  186. << "Expected bool scalar or vector type as Result Type: "
  187. << spvOpcodeString(opcode);
  188. const uint32_t left_type = _.GetOperandTypeId(inst, 2);
  189. const uint32_t right_type = _.GetOperandTypeId(inst, 3);
  190. if (!left_type ||
  191. (!_.IsIntScalarType(left_type) && !_.IsIntVectorType(left_type)))
  192. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  193. << "Expected operands to be scalar or vector int: "
  194. << spvOpcodeString(opcode);
  195. if (_.GetDimension(result_type) != _.GetDimension(left_type))
  196. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  197. << "Expected vector sizes of Result Type and the operands to be"
  198. << " equal: " << spvOpcodeString(opcode);
  199. if (!right_type ||
  200. (!_.IsIntScalarType(right_type) && !_.IsIntVectorType(right_type)))
  201. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  202. << "Expected operands to be scalar or vector int: "
  203. << spvOpcodeString(opcode);
  204. if (_.GetDimension(result_type) != _.GetDimension(right_type))
  205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  206. << "Expected vector sizes of Result Type and the operands to be"
  207. << " equal: " << spvOpcodeString(opcode);
  208. if (_.GetBitWidth(left_type) != _.GetBitWidth(right_type))
  209. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  210. << "Expected both operands to have the same component bit "
  211. "width: "
  212. << spvOpcodeString(opcode);
  213. break;
  214. }
  215. default:
  216. break;
  217. }
  218. return SPV_SUCCESS;
  219. }
  220. } // namespace val
  221. } // namespace spvtools