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/opcode.h"
  16. #include "source/val/instruction.h"
  17. #include "source/val/validate.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. // Validates correctness of logical instructions.
  22. spv_result_t LogicalsPass(ValidationState_t& _, const Instruction* inst) {
  23. const spv::Op opcode = inst->opcode();
  24. const uint32_t result_type = inst->type_id();
  25. switch (opcode) {
  26. case spv::Op::OpAny:
  27. case spv::Op::OpAll: {
  28. if (!_.IsBoolScalarType(result_type))
  29. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  30. << "Expected bool scalar type as Result Type: "
  31. << spvOpcodeString(opcode);
  32. const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
  33. if (!vector_type || !_.IsBoolVectorType(vector_type))
  34. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  35. << "Expected operand to be vector bool: "
  36. << spvOpcodeString(opcode);
  37. break;
  38. }
  39. case spv::Op::OpIsNan:
  40. case spv::Op::OpIsInf:
  41. case spv::Op::OpIsFinite:
  42. case spv::Op::OpIsNormal:
  43. case spv::Op::OpSignBitSet: {
  44. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  45. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  46. << "Expected bool scalar or vector type as Result Type: "
  47. << spvOpcodeString(opcode);
  48. const uint32_t operand_type = _.GetOperandTypeId(inst, 2);
  49. if (!operand_type || (!_.IsFloatScalarType(operand_type) &&
  50. !_.IsFloatVectorType(operand_type)))
  51. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  52. << "Expected operand to be scalar or vector float: "
  53. << spvOpcodeString(opcode);
  54. if (_.GetDimension(result_type) != _.GetDimension(operand_type))
  55. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  56. << "Expected vector sizes of Result Type and the operand to be "
  57. "equal: "
  58. << spvOpcodeString(opcode);
  59. break;
  60. }
  61. case spv::Op::OpFOrdEqual:
  62. case spv::Op::OpFUnordEqual:
  63. case spv::Op::OpFOrdNotEqual:
  64. case spv::Op::OpFUnordNotEqual:
  65. case spv::Op::OpFOrdLessThan:
  66. case spv::Op::OpFUnordLessThan:
  67. case spv::Op::OpFOrdGreaterThan:
  68. case spv::Op::OpFUnordGreaterThan:
  69. case spv::Op::OpFOrdLessThanEqual:
  70. case spv::Op::OpFUnordLessThanEqual:
  71. case spv::Op::OpFOrdGreaterThanEqual:
  72. case spv::Op::OpFUnordGreaterThanEqual:
  73. case spv::Op::OpLessOrGreater:
  74. case spv::Op::OpOrdered:
  75. case spv::Op::OpUnordered: {
  76. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  77. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  78. << "Expected bool scalar or vector type as Result Type: "
  79. << spvOpcodeString(opcode);
  80. const uint32_t left_operand_type = _.GetOperandTypeId(inst, 2);
  81. if (!left_operand_type || (!_.IsFloatScalarType(left_operand_type) &&
  82. !_.IsFloatVectorType(left_operand_type)))
  83. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  84. << "Expected operands to be scalar or vector float: "
  85. << spvOpcodeString(opcode);
  86. if (_.GetDimension(result_type) != _.GetDimension(left_operand_type))
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << "Expected vector sizes of Result Type and the operands to be "
  89. "equal: "
  90. << spvOpcodeString(opcode);
  91. if (left_operand_type != _.GetOperandTypeId(inst, 3))
  92. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  93. << "Expected left and right operands to have the same type: "
  94. << spvOpcodeString(opcode);
  95. break;
  96. }
  97. case spv::Op::OpLogicalEqual:
  98. case spv::Op::OpLogicalNotEqual:
  99. case spv::Op::OpLogicalOr:
  100. case spv::Op::OpLogicalAnd: {
  101. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  102. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  103. << "Expected bool scalar or vector type as Result Type: "
  104. << spvOpcodeString(opcode);
  105. if (result_type != _.GetOperandTypeId(inst, 2) ||
  106. result_type != _.GetOperandTypeId(inst, 3))
  107. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  108. << "Expected both operands to be of Result Type: "
  109. << spvOpcodeString(opcode);
  110. break;
  111. }
  112. case spv::Op::OpLogicalNot: {
  113. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  114. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  115. << "Expected bool scalar or vector type as Result Type: "
  116. << spvOpcodeString(opcode);
  117. if (result_type != _.GetOperandTypeId(inst, 2))
  118. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  119. << "Expected operand to be of Result Type: "
  120. << spvOpcodeString(opcode);
  121. break;
  122. }
  123. case spv::Op::OpSelect: {
  124. uint32_t dimension = 1;
  125. {
  126. const Instruction* type_inst = _.FindDef(result_type);
  127. assert(type_inst);
  128. const auto composites = _.features().select_between_composites;
  129. auto fail = [&_, composites, inst, opcode]() -> spv_result_t {
  130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  131. << "Expected scalar or "
  132. << (composites ? "composite" : "vector")
  133. << " type as Result Type: " << spvOpcodeString(opcode);
  134. };
  135. const spv::Op type_opcode = type_inst->opcode();
  136. switch (type_opcode) {
  137. case spv::Op::OpTypeUntypedPointerKHR:
  138. case spv::Op::OpTypePointer: {
  139. if (_.addressing_model() == spv::AddressingModel::Logical &&
  140. !_.HasCapability(
  141. spv::Capability::VariablePointersStorageBuffer))
  142. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  143. << "Using pointers with OpSelect requires capability "
  144. << "VariablePointers or VariablePointersStorageBuffer";
  145. break;
  146. }
  147. case spv::Op::OpTypeSampledImage:
  148. case spv::Op::OpTypeImage:
  149. case spv::Op::OpTypeSampler: {
  150. if (!_.HasCapability(spv::Capability::BindlessTextureNV))
  151. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  152. << "Using image/sampler with OpSelect requires capability "
  153. << "BindlessTextureNV";
  154. break;
  155. }
  156. case spv::Op::OpTypeVector: {
  157. dimension = type_inst->word(3);
  158. break;
  159. }
  160. case spv::Op::OpTypeBool:
  161. case spv::Op::OpTypeInt:
  162. case spv::Op::OpTypeFloat: {
  163. break;
  164. }
  165. // Not RuntimeArray because of other rules.
  166. case spv::Op::OpTypeArray:
  167. case spv::Op::OpTypeMatrix:
  168. case spv::Op::OpTypeStruct: {
  169. if (!composites) return fail();
  170. break;
  171. }
  172. default:
  173. return fail();
  174. }
  175. const uint32_t condition_type = _.GetOperandTypeId(inst, 2);
  176. const uint32_t left_type = _.GetOperandTypeId(inst, 3);
  177. const uint32_t right_type = _.GetOperandTypeId(inst, 4);
  178. if (!condition_type || (!_.IsBoolScalarType(condition_type) &&
  179. !_.IsBoolVectorType(condition_type)))
  180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  181. << "Expected bool scalar or vector type as condition: "
  182. << spvOpcodeString(opcode);
  183. if (_.GetDimension(condition_type) != dimension) {
  184. // If the condition is a vector type, then the result must also be a
  185. // vector with matching dimensions. In SPIR-V 1.4, a scalar condition
  186. // can be used to select between vector types. |composites| is a
  187. // proxy for SPIR-V 1.4 functionality.
  188. if (!composites || _.IsBoolVectorType(condition_type)) {
  189. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  190. << "Expected vector sizes of Result Type and the condition "
  191. "to be equal: "
  192. << spvOpcodeString(opcode);
  193. }
  194. }
  195. if (result_type != left_type || result_type != right_type)
  196. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  197. << "Expected both objects to be of Result Type: "
  198. << spvOpcodeString(opcode);
  199. break;
  200. }
  201. }
  202. case spv::Op::OpIEqual:
  203. case spv::Op::OpINotEqual:
  204. case spv::Op::OpUGreaterThan:
  205. case spv::Op::OpUGreaterThanEqual:
  206. case spv::Op::OpULessThan:
  207. case spv::Op::OpULessThanEqual:
  208. case spv::Op::OpSGreaterThan:
  209. case spv::Op::OpSGreaterThanEqual:
  210. case spv::Op::OpSLessThan:
  211. case spv::Op::OpSLessThanEqual: {
  212. if (!_.IsBoolScalarType(result_type) && !_.IsBoolVectorType(result_type))
  213. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  214. << "Expected bool scalar or vector type as Result Type: "
  215. << spvOpcodeString(opcode);
  216. const uint32_t left_type = _.GetOperandTypeId(inst, 2);
  217. const uint32_t right_type = _.GetOperandTypeId(inst, 3);
  218. if (!left_type ||
  219. (!_.IsIntScalarType(left_type) && !_.IsIntVectorType(left_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(left_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 (!right_type ||
  228. (!_.IsIntScalarType(right_type) && !_.IsIntVectorType(right_type)))
  229. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  230. << "Expected operands to be scalar or vector int: "
  231. << spvOpcodeString(opcode);
  232. if (_.GetDimension(result_type) != _.GetDimension(right_type))
  233. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  234. << "Expected vector sizes of Result Type and the operands to be"
  235. << " equal: " << spvOpcodeString(opcode);
  236. if (_.GetBitWidth(left_type) != _.GetBitWidth(right_type))
  237. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  238. << "Expected both operands to have the same component bit "
  239. "width: "
  240. << spvOpcodeString(opcode);
  241. break;
  242. }
  243. default:
  244. break;
  245. }
  246. return SPV_SUCCESS;
  247. }
  248. } // namespace val
  249. } // namespace spvtools