validate_logicals.cpp 10 KB

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