validate_logicals.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  142. << "Using pointers with OpSelect requires capability "
  143. << "VariablePointers or VariablePointersStorageBuffer";
  144. break;
  145. }
  146. case SpvOpTypeSampledImage:
  147. case SpvOpTypeImage:
  148. case SpvOpTypeSampler: {
  149. if (!_.HasCapability(SpvCapabilityBindlessTextureNV))
  150. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  151. << "Using image/sampler with OpSelect requires capability "
  152. << "BindlessTextureNV";
  153. break;
  154. }
  155. case SpvOpTypeVector: {
  156. dimension = type_inst->word(3);
  157. break;
  158. }
  159. case SpvOpTypeBool:
  160. case SpvOpTypeInt:
  161. case SpvOpTypeFloat: {
  162. break;
  163. }
  164. // Not RuntimeArray because of other rules.
  165. case SpvOpTypeArray:
  166. case SpvOpTypeMatrix:
  167. case SpvOpTypeStruct: {
  168. if (!composites) return fail();
  169. break;
  170. }
  171. default:
  172. return fail();
  173. }
  174. const uint32_t condition_type = _.GetOperandTypeId(inst, 2);
  175. const uint32_t left_type = _.GetOperandTypeId(inst, 3);
  176. const uint32_t right_type = _.GetOperandTypeId(inst, 4);
  177. if (!condition_type || (!_.IsBoolScalarType(condition_type) &&
  178. !_.IsBoolVectorType(condition_type)))
  179. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  180. << "Expected bool scalar or vector type as condition: "
  181. << spvOpcodeString(opcode);
  182. if (_.GetDimension(condition_type) != dimension) {
  183. // If the condition is a vector type, then the result must also be a
  184. // vector with matching dimensions. In SPIR-V 1.4, a scalar condition
  185. // can be used to select between vector types. |composites| is a
  186. // proxy for SPIR-V 1.4 functionality.
  187. if (!composites || _.IsBoolVectorType(condition_type)) {
  188. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  189. << "Expected vector sizes of Result Type and the condition "
  190. "to be equal: "
  191. << spvOpcodeString(opcode);
  192. }
  193. }
  194. if (result_type != left_type || result_type != right_type)
  195. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  196. << "Expected both objects to be of Result Type: "
  197. << spvOpcodeString(opcode);
  198. break;
  199. }
  200. }
  201. case SpvOpIEqual:
  202. case SpvOpINotEqual:
  203. case SpvOpUGreaterThan:
  204. case SpvOpUGreaterThanEqual:
  205. case SpvOpULessThan:
  206. case SpvOpULessThanEqual:
  207. case SpvOpSGreaterThan:
  208. case SpvOpSGreaterThanEqual:
  209. case SpvOpSLessThan:
  210. case SpvOpSLessThanEqual: {
  211. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  212. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  213. << "Expected bool scalar or vector type as Result Type: "
  214. << spvOpcodeString(opcode);
  215. const uint32_t left_type = _.GetOperandTypeId(inst, 2);
  216. const uint32_t right_type = _.GetOperandTypeId(inst, 3);
  217. if (!left_type ||
  218. (!_.IsIntScalarType(left_type) && !_.IsIntVectorType(left_type)))
  219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  220. << "Expected operands to be scalar or vector int: "
  221. << spvOpcodeString(opcode);
  222. if (_.GetDimension(result_type) != _.GetDimension(left_type))
  223. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  224. << "Expected vector sizes of Result Type and the operands to be"
  225. << " equal: " << spvOpcodeString(opcode);
  226. if (!right_type ||
  227. (!_.IsIntScalarType(right_type) && !_.IsIntVectorType(right_type)))
  228. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  229. << "Expected operands to be scalar or vector int: "
  230. << spvOpcodeString(opcode);
  231. if (_.GetDimension(result_type) != _.GetDimension(right_type))
  232. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  233. << "Expected vector sizes of Result Type and the operands to be"
  234. << " equal: " << spvOpcodeString(opcode);
  235. if (_.GetBitWidth(left_type) != _.GetBitWidth(right_type))
  236. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  237. << "Expected both operands to have the same component bit "
  238. "width: "
  239. << spvOpcodeString(opcode);
  240. break;
  241. }
  242. default:
  243. break;
  244. }
  245. return SPV_SUCCESS;
  246. }
  247. } // namespace val
  248. } // namespace spvtools