validate_arithmetics.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. // Performs validation of arithmetic 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 arithmetic instructions.
  40. spv_result_t ArithmeticsPass(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 SpvOpFAdd:
  46. case SpvOpFSub:
  47. case SpvOpFMul:
  48. case SpvOpFDiv:
  49. case SpvOpFRem:
  50. case SpvOpFMod:
  51. case SpvOpFNegate: {
  52. if (!_.IsFloatScalarType(result_type) &&
  53. !_.IsFloatVectorType(result_type))
  54. return _.diag(SPV_ERROR_INVALID_DATA)
  55. << "Expected floating scalar or vector type as Result Type: "
  56. << spvOpcodeString(opcode);
  57. for (size_t operand_index = 2; operand_index < inst->num_operands;
  58. ++operand_index) {
  59. if (GetOperandTypeId(_, inst, operand_index) != result_type)
  60. return _.diag(SPV_ERROR_INVALID_DATA)
  61. << "Expected arithmetic operands to be of Result Type: "
  62. << spvOpcodeString(opcode) << " operand index "
  63. << operand_index;
  64. }
  65. break;
  66. }
  67. case SpvOpUDiv:
  68. case SpvOpUMod: {
  69. if (!_.IsUnsignedIntScalarType(result_type) &&
  70. !_.IsUnsignedIntVectorType(result_type))
  71. return _.diag(SPV_ERROR_INVALID_DATA)
  72. << "Expected unsigned int scalar or vector type as Result Type: "
  73. << spvOpcodeString(opcode);
  74. for (size_t operand_index = 2; operand_index < inst->num_operands;
  75. ++operand_index) {
  76. if (GetOperandTypeId(_, inst, operand_index) != result_type)
  77. return _.diag(SPV_ERROR_INVALID_DATA)
  78. << "Expected arithmetic operands to be of Result Type: "
  79. << spvOpcodeString(opcode) << " operand index "
  80. << operand_index;
  81. }
  82. break;
  83. }
  84. case SpvOpISub:
  85. case SpvOpIAdd:
  86. case SpvOpIMul:
  87. case SpvOpSDiv:
  88. case SpvOpSMod:
  89. case SpvOpSRem:
  90. case SpvOpSNegate: {
  91. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  92. return _.diag(SPV_ERROR_INVALID_DATA)
  93. << "Expected int scalar or vector type as Result Type: "
  94. << spvOpcodeString(opcode);
  95. const uint32_t dimension = _.GetDimension(result_type);
  96. const uint32_t bit_width = _.GetBitWidth(result_type);
  97. for (size_t operand_index = 2; operand_index < inst->num_operands;
  98. ++operand_index) {
  99. const uint32_t type_id = GetOperandTypeId(_, inst, operand_index);
  100. if (!type_id ||
  101. (!_.IsIntScalarType(type_id) && !_.IsIntVectorType(type_id)))
  102. return _.diag(SPV_ERROR_INVALID_DATA)
  103. << "Expected int scalar or vector type as operand: "
  104. << spvOpcodeString(opcode) << " operand index "
  105. << operand_index;
  106. if (_.GetDimension(type_id) != dimension)
  107. return _.diag(SPV_ERROR_INVALID_DATA)
  108. << "Expected arithmetic operands to have the same dimension "
  109. << "as Result Type: " << spvOpcodeString(opcode)
  110. << " operand index " << operand_index;
  111. if (_.GetBitWidth(type_id) != bit_width)
  112. return _.diag(SPV_ERROR_INVALID_DATA)
  113. << "Expected arithmetic operands to have the same bit width "
  114. << "as Result Type: " << spvOpcodeString(opcode)
  115. << " operand index " << operand_index;
  116. }
  117. break;
  118. }
  119. case SpvOpDot: {
  120. if (!_.IsFloatScalarType(result_type))
  121. return _.diag(SPV_ERROR_INVALID_DATA)
  122. << "Expected float scalar type as Result Type: "
  123. << spvOpcodeString(opcode);
  124. uint32_t first_vector_num_components = 0;
  125. for (size_t operand_index = 2; operand_index < inst->num_operands;
  126. ++operand_index) {
  127. const uint32_t type_id = GetOperandTypeId(_, inst, operand_index);
  128. if (!type_id || !_.IsFloatVectorType(type_id))
  129. return _.diag(SPV_ERROR_INVALID_DATA)
  130. << "Expected float vector as operand: "
  131. << spvOpcodeString(opcode) << " operand index "
  132. << operand_index;
  133. const uint32_t component_type = _.GetComponentType(type_id);
  134. if (component_type != result_type)
  135. return _.diag(SPV_ERROR_INVALID_DATA)
  136. << "Expected component type to be equal to Result Type: "
  137. << spvOpcodeString(opcode) << " operand index "
  138. << operand_index;
  139. const uint32_t num_components = _.GetDimension(type_id);
  140. if (operand_index == 2) {
  141. first_vector_num_components = num_components;
  142. } else if (num_components != first_vector_num_components) {
  143. return _.diag(SPV_ERROR_INVALID_DATA)
  144. << "Expected operands to have the same number of componenets: "
  145. << spvOpcodeString(opcode);
  146. }
  147. }
  148. break;
  149. }
  150. case SpvOpVectorTimesScalar: {
  151. if (!_.IsFloatVectorType(result_type))
  152. return _.diag(SPV_ERROR_INVALID_DATA)
  153. << "Expected float vector type as Result Type: "
  154. << spvOpcodeString(opcode);
  155. const uint32_t vector_type_id = GetOperandTypeId(_, inst, 2);
  156. if (result_type != vector_type_id)
  157. return _.diag(SPV_ERROR_INVALID_DATA)
  158. << "Expected vector operand type to be equal to Result Type: "
  159. << spvOpcodeString(opcode);
  160. const uint32_t component_type = _.GetComponentType(vector_type_id);
  161. const uint32_t scalar_type_id = GetOperandTypeId(_, inst, 3);
  162. if (component_type != scalar_type_id)
  163. return _.diag(SPV_ERROR_INVALID_DATA)
  164. << "Expected scalar operand type to be equal to the component "
  165. << "type of the vector operand: " << spvOpcodeString(opcode);
  166. break;
  167. }
  168. case SpvOpMatrixTimesScalar: {
  169. if (!_.IsFloatMatrixType(result_type))
  170. return _.diag(SPV_ERROR_INVALID_DATA)
  171. << "Expected float matrix type as Result Type: "
  172. << spvOpcodeString(opcode);
  173. const uint32_t matrix_type_id = GetOperandTypeId(_, inst, 2);
  174. if (result_type != matrix_type_id)
  175. return _.diag(SPV_ERROR_INVALID_DATA)
  176. << "Expected matrix operand type to be equal to Result Type: "
  177. << spvOpcodeString(opcode);
  178. const uint32_t component_type = _.GetComponentType(matrix_type_id);
  179. const uint32_t scalar_type_id = GetOperandTypeId(_, inst, 3);
  180. if (component_type != scalar_type_id)
  181. return _.diag(SPV_ERROR_INVALID_DATA)
  182. << "Expected scalar operand type to be equal to the component "
  183. << "type of the matrix operand: " << spvOpcodeString(opcode);
  184. break;
  185. }
  186. case SpvOpVectorTimesMatrix: {
  187. const uint32_t vector_type_id = GetOperandTypeId(_, inst, 2);
  188. const uint32_t matrix_type_id = GetOperandTypeId(_, inst, 3);
  189. if (!_.IsFloatVectorType(result_type))
  190. return _.diag(SPV_ERROR_INVALID_DATA)
  191. << "Expected float vector type as Result Type: "
  192. << spvOpcodeString(opcode);
  193. const uint32_t res_component_type = _.GetComponentType(result_type);
  194. if (!vector_type_id || !_.IsFloatVectorType(vector_type_id))
  195. return _.diag(SPV_ERROR_INVALID_DATA)
  196. << "Expected float vector type as left operand: "
  197. << spvOpcodeString(opcode);
  198. if (res_component_type != _.GetComponentType(vector_type_id))
  199. return _.diag(SPV_ERROR_INVALID_DATA)
  200. << "Expected component types of Result Type and vector to be "
  201. << "equal: " << spvOpcodeString(opcode);
  202. uint32_t matrix_num_rows = 0;
  203. uint32_t matrix_num_cols = 0;
  204. uint32_t matrix_col_type = 0;
  205. uint32_t matrix_component_type = 0;
  206. if (!_.GetMatrixTypeInfo(matrix_type_id, &matrix_num_rows,
  207. &matrix_num_cols, &matrix_col_type,
  208. &matrix_component_type))
  209. return _.diag(SPV_ERROR_INVALID_DATA)
  210. << "Expected float matrix type as right operand: "
  211. << spvOpcodeString(opcode);
  212. if (res_component_type != matrix_component_type)
  213. return _.diag(SPV_ERROR_INVALID_DATA)
  214. << "Expected component types of Result Type and matrix to be "
  215. << "equal: " << spvOpcodeString(opcode);
  216. if (matrix_num_cols != _.GetDimension(result_type))
  217. return _.diag(SPV_ERROR_INVALID_DATA)
  218. << "Expected number of columns of the matrix to be equal to "
  219. << "Result Type vector size: " << spvOpcodeString(opcode);
  220. if (matrix_num_rows != _.GetDimension(vector_type_id))
  221. return _.diag(SPV_ERROR_INVALID_DATA)
  222. << "Expected number of rows of the matrix to be equal to the "
  223. << "vector operand size: " << spvOpcodeString(opcode);
  224. break;
  225. }
  226. case SpvOpMatrixTimesVector: {
  227. const uint32_t matrix_type_id = GetOperandTypeId(_, inst, 2);
  228. const uint32_t vector_type_id = GetOperandTypeId(_, inst, 3);
  229. if (!_.IsFloatVectorType(result_type))
  230. return _.diag(SPV_ERROR_INVALID_DATA)
  231. << "Expected float vector type as Result Type: "
  232. << spvOpcodeString(opcode);
  233. uint32_t matrix_num_rows = 0;
  234. uint32_t matrix_num_cols = 0;
  235. uint32_t matrix_col_type = 0;
  236. uint32_t matrix_component_type = 0;
  237. if (!_.GetMatrixTypeInfo(matrix_type_id, &matrix_num_rows,
  238. &matrix_num_cols, &matrix_col_type,
  239. &matrix_component_type))
  240. return _.diag(SPV_ERROR_INVALID_DATA)
  241. << "Expected float matrix type as left operand: "
  242. << spvOpcodeString(opcode);
  243. if (result_type != matrix_col_type)
  244. return _.diag(SPV_ERROR_INVALID_DATA)
  245. << "Expected column type of the matrix to be equal to Result "
  246. "Type: "
  247. << spvOpcodeString(opcode);
  248. if (!vector_type_id || !_.IsFloatVectorType(vector_type_id))
  249. return _.diag(SPV_ERROR_INVALID_DATA)
  250. << "Expected float vector type as right operand: "
  251. << spvOpcodeString(opcode);
  252. if (matrix_component_type != _.GetComponentType(vector_type_id))
  253. return _.diag(SPV_ERROR_INVALID_DATA)
  254. << "Expected component types of the operands to be equal: "
  255. << spvOpcodeString(opcode);
  256. if (matrix_num_cols != _.GetDimension(vector_type_id))
  257. return _.diag(SPV_ERROR_INVALID_DATA)
  258. << "Expected number of columns of the matrix to be equal to the "
  259. << "vector size: " << spvOpcodeString(opcode);
  260. break;
  261. }
  262. case SpvOpMatrixTimesMatrix: {
  263. const uint32_t left_type_id = GetOperandTypeId(_, inst, 2);
  264. const uint32_t right_type_id = GetOperandTypeId(_, inst, 3);
  265. uint32_t res_num_rows = 0;
  266. uint32_t res_num_cols = 0;
  267. uint32_t res_col_type = 0;
  268. uint32_t res_component_type = 0;
  269. if (!_.GetMatrixTypeInfo(result_type, &res_num_rows, &res_num_cols,
  270. &res_col_type, &res_component_type))
  271. return _.diag(SPV_ERROR_INVALID_DATA)
  272. << "Expected float matrix type as Result Type: "
  273. << spvOpcodeString(opcode);
  274. uint32_t left_num_rows = 0;
  275. uint32_t left_num_cols = 0;
  276. uint32_t left_col_type = 0;
  277. uint32_t left_component_type = 0;
  278. if (!_.GetMatrixTypeInfo(left_type_id, &left_num_rows, &left_num_cols,
  279. &left_col_type, &left_component_type))
  280. return _.diag(SPV_ERROR_INVALID_DATA)
  281. << "Expected float matrix type as left operand: "
  282. << spvOpcodeString(opcode);
  283. uint32_t right_num_rows = 0;
  284. uint32_t right_num_cols = 0;
  285. uint32_t right_col_type = 0;
  286. uint32_t right_component_type = 0;
  287. if (!_.GetMatrixTypeInfo(right_type_id, &right_num_rows, &right_num_cols,
  288. &right_col_type, &right_component_type))
  289. return _.diag(SPV_ERROR_INVALID_DATA)
  290. << "Expected float matrix type as right operand: "
  291. << spvOpcodeString(opcode);
  292. if (!_.IsFloatScalarType(res_component_type))
  293. return _.diag(SPV_ERROR_INVALID_DATA)
  294. << "Expected float matrix type as Result Type: "
  295. << spvOpcodeString(opcode);
  296. if (res_col_type != left_col_type)
  297. return _.diag(SPV_ERROR_INVALID_DATA)
  298. << "Expected column types of Result Type and left matrix to be "
  299. << "equal: " << spvOpcodeString(opcode);
  300. if (res_component_type != right_component_type)
  301. return _.diag(SPV_ERROR_INVALID_DATA)
  302. << "Expected component types of Result Type and right matrix to "
  303. "be "
  304. << "equal: " << spvOpcodeString(opcode);
  305. if (res_num_cols != right_num_cols)
  306. return _.diag(SPV_ERROR_INVALID_DATA)
  307. << "Expected number of columns of Result Type and right matrix "
  308. "to "
  309. << "be equal: " << spvOpcodeString(opcode);
  310. if (left_num_cols != right_num_rows)
  311. return _.diag(SPV_ERROR_INVALID_DATA)
  312. << "Expected number of columns of left matrix and number of "
  313. "rows "
  314. << "of right matrix to be equal: " << spvOpcodeString(opcode);
  315. assert(left_num_rows == res_num_rows);
  316. break;
  317. }
  318. case SpvOpOuterProduct: {
  319. const uint32_t left_type_id = GetOperandTypeId(_, inst, 2);
  320. const uint32_t right_type_id = GetOperandTypeId(_, inst, 3);
  321. uint32_t res_num_rows = 0;
  322. uint32_t res_num_cols = 0;
  323. uint32_t res_col_type = 0;
  324. uint32_t res_component_type = 0;
  325. if (!_.GetMatrixTypeInfo(result_type, &res_num_rows, &res_num_cols,
  326. &res_col_type, &res_component_type))
  327. return _.diag(SPV_ERROR_INVALID_DATA)
  328. << "Expected float matrix type as Result Type: "
  329. << spvOpcodeString(opcode);
  330. if (left_type_id != res_col_type)
  331. return _.diag(SPV_ERROR_INVALID_DATA)
  332. << "Expected column type of Result Type to be equal to the type "
  333. << "of the left operand: " << spvOpcodeString(opcode);
  334. if (!right_type_id || !_.IsFloatVectorType(right_type_id))
  335. return _.diag(SPV_ERROR_INVALID_DATA)
  336. << "Expected float vector type as right operand: "
  337. << spvOpcodeString(opcode);
  338. if (res_component_type != _.GetComponentType(right_type_id))
  339. return _.diag(SPV_ERROR_INVALID_DATA)
  340. << "Expected component types of the operands to be equal: "
  341. << spvOpcodeString(opcode);
  342. if (res_num_cols != _.GetDimension(right_type_id))
  343. return _.diag(SPV_ERROR_INVALID_DATA)
  344. << "Expected number of columns of the matrix to be equal to the "
  345. << "vector size of the right operand: "
  346. << spvOpcodeString(opcode);
  347. break;
  348. }
  349. case SpvOpIAddCarry:
  350. case SpvOpISubBorrow:
  351. case SpvOpUMulExtended:
  352. case SpvOpSMulExtended: {
  353. std::vector<uint32_t> result_types;
  354. if (!_.GetStructMemberTypes(result_type, &result_types))
  355. return _.diag(SPV_ERROR_INVALID_DATA)
  356. << "Expected a struct as Result Type: "
  357. << spvOpcodeString(opcode);
  358. if (result_types.size() != 2)
  359. return _.diag(SPV_ERROR_INVALID_DATA)
  360. << "Expected Result Type struct to have two members: "
  361. << spvOpcodeString(opcode);
  362. if (opcode == SpvOpSMulExtended) {
  363. if (!_.IsIntScalarType(result_types[0]) &&
  364. !_.IsIntVectorType(result_types[0]))
  365. return _.diag(SPV_ERROR_INVALID_DATA)
  366. << "Expected Result Type struct member types to be integer "
  367. "scalar "
  368. << "or vector: " << spvOpcodeString(opcode);
  369. } else {
  370. if (!_.IsUnsignedIntScalarType(result_types[0]) &&
  371. !_.IsUnsignedIntVectorType(result_types[0]))
  372. return _.diag(SPV_ERROR_INVALID_DATA)
  373. << "Expected Result Type struct member types to be unsigned "
  374. << "integer scalar or vector: " << spvOpcodeString(opcode);
  375. }
  376. if (result_types[0] != result_types[1])
  377. return _.diag(SPV_ERROR_INVALID_DATA)
  378. << "Expected Result Type struct member types to be identical: "
  379. << spvOpcodeString(opcode);
  380. const uint32_t left_type_id = GetOperandTypeId(_, inst, 2);
  381. const uint32_t right_type_id = GetOperandTypeId(_, inst, 3);
  382. if (left_type_id != result_types[0] || right_type_id != result_types[0])
  383. return _.diag(SPV_ERROR_INVALID_DATA)
  384. << "Expected both operands to be of Result Type member type: "
  385. << spvOpcodeString(opcode);
  386. break;
  387. }
  388. default:
  389. break;
  390. }
  391. return SPV_SUCCESS;
  392. }
  393. } // namespace libspirv