validate_arithmetics.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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 <vector>
  16. #include "source/opcode.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validate.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. // Validates correctness of arithmetic instructions.
  23. spv_result_t ArithmeticsPass(ValidationState_t& _, const Instruction* inst) {
  24. const spv::Op opcode = inst->opcode();
  25. const uint32_t result_type = inst->type_id();
  26. switch (opcode) {
  27. case spv::Op::OpFAdd:
  28. case spv::Op::OpFSub:
  29. case spv::Op::OpFMul:
  30. case spv::Op::OpFDiv:
  31. case spv::Op::OpFRem:
  32. case spv::Op::OpFMod:
  33. case spv::Op::OpFNegate: {
  34. bool supportsCoopMat =
  35. (opcode != spv::Op::OpFMul && opcode != spv::Op::OpFRem &&
  36. opcode != spv::Op::OpFMod);
  37. bool supportsCoopVec =
  38. (opcode != spv::Op::OpFRem && opcode != spv::Op::OpFMod);
  39. if (!_.IsFloatScalarType(result_type) &&
  40. !_.IsFloatVectorType(result_type) &&
  41. !(supportsCoopMat && _.IsFloatCooperativeMatrixType(result_type)) &&
  42. !(opcode == spv::Op::OpFMul &&
  43. _.IsCooperativeMatrixKHRType(result_type) &&
  44. _.IsFloatCooperativeMatrixType(result_type)) &&
  45. !(supportsCoopVec && _.IsFloatCooperativeVectorNVType(result_type)))
  46. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  47. << "Expected floating scalar or vector type as Result Type: "
  48. << spvOpcodeString(opcode);
  49. for (size_t operand_index = 2; operand_index < inst->operands().size();
  50. ++operand_index) {
  51. if (supportsCoopVec && _.IsCooperativeVectorNVType(result_type)) {
  52. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  53. if (!_.IsCooperativeVectorNVType(type_id)) {
  54. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  55. << "Expected arithmetic operands to be of Result Type: "
  56. << spvOpcodeString(opcode) << " operand index "
  57. << operand_index;
  58. }
  59. spv_result_t ret =
  60. _.CooperativeVectorDimensionsMatch(inst, type_id, result_type);
  61. if (ret != SPV_SUCCESS) return ret;
  62. } else if (supportsCoopMat &&
  63. _.IsCooperativeMatrixKHRType(result_type)) {
  64. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  65. if (!_.IsCooperativeMatrixKHRType(type_id) ||
  66. !_.IsFloatCooperativeMatrixType(type_id)) {
  67. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  68. << "Expected arithmetic operands to be of Result Type: "
  69. << spvOpcodeString(opcode) << " operand index "
  70. << operand_index;
  71. }
  72. spv_result_t ret =
  73. _.CooperativeMatrixShapesMatch(inst, result_type, type_id, false);
  74. if (ret != SPV_SUCCESS) return ret;
  75. } else if (_.GetOperandTypeId(inst, operand_index) != result_type)
  76. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  77. << "Expected arithmetic operands to be of Result Type: "
  78. << spvOpcodeString(opcode) << " operand index "
  79. << operand_index;
  80. }
  81. break;
  82. }
  83. case spv::Op::OpUDiv:
  84. case spv::Op::OpUMod: {
  85. bool supportsCoopMat = (opcode == spv::Op::OpUDiv);
  86. bool supportsCoopVec = (opcode == spv::Op::OpUDiv);
  87. if (!_.IsUnsignedIntScalarType(result_type) &&
  88. !_.IsUnsignedIntVectorType(result_type) &&
  89. !(supportsCoopMat &&
  90. _.IsUnsignedIntCooperativeMatrixType(result_type)) &&
  91. !(supportsCoopVec &&
  92. _.IsUnsignedIntCooperativeVectorNVType(result_type)))
  93. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  94. << "Expected unsigned int scalar or vector type as Result Type: "
  95. << spvOpcodeString(opcode);
  96. for (size_t operand_index = 2; operand_index < inst->operands().size();
  97. ++operand_index) {
  98. if (supportsCoopVec && _.IsCooperativeVectorNVType(result_type)) {
  99. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  100. if (!_.IsCooperativeVectorNVType(type_id)) {
  101. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  102. << "Expected arithmetic operands to be of Result Type: "
  103. << spvOpcodeString(opcode) << " operand index "
  104. << operand_index;
  105. }
  106. spv_result_t ret =
  107. _.CooperativeVectorDimensionsMatch(inst, type_id, result_type);
  108. if (ret != SPV_SUCCESS) return ret;
  109. } else if (supportsCoopMat &&
  110. _.IsCooperativeMatrixKHRType(result_type)) {
  111. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  112. if (!_.IsCooperativeMatrixKHRType(type_id) ||
  113. !_.IsUnsignedIntCooperativeMatrixType(type_id)) {
  114. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  115. << "Expected arithmetic operands to be of Result Type: "
  116. << spvOpcodeString(opcode) << " operand index "
  117. << operand_index;
  118. }
  119. spv_result_t ret =
  120. _.CooperativeMatrixShapesMatch(inst, result_type, type_id, false);
  121. if (ret != SPV_SUCCESS) return ret;
  122. } else if (_.GetOperandTypeId(inst, operand_index) != result_type)
  123. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  124. << "Expected arithmetic operands to be of Result Type: "
  125. << spvOpcodeString(opcode) << " operand index "
  126. << operand_index;
  127. }
  128. break;
  129. }
  130. case spv::Op::OpISub:
  131. case spv::Op::OpIAdd:
  132. case spv::Op::OpIMul:
  133. case spv::Op::OpSDiv:
  134. case spv::Op::OpSMod:
  135. case spv::Op::OpSRem:
  136. case spv::Op::OpSNegate: {
  137. bool supportsCoopMat =
  138. (opcode != spv::Op::OpIMul && opcode != spv::Op::OpSRem &&
  139. opcode != spv::Op::OpSMod);
  140. bool supportsCoopVec =
  141. (opcode != spv::Op::OpSRem && opcode != spv::Op::OpSMod);
  142. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
  143. !(supportsCoopMat && _.IsIntCooperativeMatrixType(result_type)) &&
  144. !(opcode == spv::Op::OpIMul &&
  145. _.IsCooperativeMatrixKHRType(result_type) &&
  146. _.IsIntCooperativeMatrixType(result_type)) &&
  147. !(supportsCoopVec && _.IsIntCooperativeVectorNVType(result_type)))
  148. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  149. << "Expected int scalar or vector type as Result Type: "
  150. << spvOpcodeString(opcode);
  151. const uint32_t dimension = _.GetDimension(result_type);
  152. const uint32_t bit_width = _.GetBitWidth(result_type);
  153. for (size_t operand_index = 2; operand_index < inst->operands().size();
  154. ++operand_index) {
  155. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  156. if (supportsCoopVec && _.IsCooperativeVectorNVType(result_type)) {
  157. if (!_.IsCooperativeVectorNVType(type_id)) {
  158. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  159. << "Expected arithmetic operands to be of Result Type: "
  160. << spvOpcodeString(opcode) << " operand index "
  161. << operand_index;
  162. }
  163. spv_result_t ret =
  164. _.CooperativeVectorDimensionsMatch(inst, type_id, result_type);
  165. if (ret != SPV_SUCCESS) return ret;
  166. }
  167. if (supportsCoopMat && _.IsCooperativeMatrixKHRType(result_type)) {
  168. if (!_.IsCooperativeMatrixKHRType(type_id) ||
  169. !_.IsIntCooperativeMatrixType(type_id)) {
  170. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  171. << "Expected arithmetic operands to be of Result Type: "
  172. << spvOpcodeString(opcode) << " operand index "
  173. << operand_index;
  174. }
  175. spv_result_t ret =
  176. _.CooperativeMatrixShapesMatch(inst, result_type, type_id, false);
  177. if (ret != SPV_SUCCESS) return ret;
  178. }
  179. if (!type_id ||
  180. (!_.IsIntScalarType(type_id) && !_.IsIntVectorType(type_id) &&
  181. !(supportsCoopMat && _.IsIntCooperativeMatrixType(result_type)) &&
  182. !(opcode == spv::Op::OpIMul &&
  183. _.IsCooperativeMatrixKHRType(result_type) &&
  184. _.IsIntCooperativeMatrixType(result_type)) &&
  185. !(supportsCoopVec && _.IsIntCooperativeVectorNVType(result_type))))
  186. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  187. << "Expected int scalar or vector type as operand: "
  188. << spvOpcodeString(opcode) << " operand index "
  189. << operand_index;
  190. if (_.GetDimension(type_id) != dimension)
  191. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  192. << "Expected arithmetic operands to have the same dimension "
  193. << "as Result Type: " << spvOpcodeString(opcode)
  194. << " operand index " << operand_index;
  195. if (_.GetBitWidth(type_id) != bit_width)
  196. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  197. << "Expected arithmetic operands to have the same bit width "
  198. << "as Result Type: " << spvOpcodeString(opcode)
  199. << " operand index " << operand_index;
  200. }
  201. break;
  202. }
  203. case spv::Op::OpDot: {
  204. if (!_.IsFloatScalarType(result_type))
  205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  206. << "Expected float scalar type as Result Type: "
  207. << spvOpcodeString(opcode);
  208. uint32_t first_vector_num_components = 0;
  209. for (size_t operand_index = 2; operand_index < inst->operands().size();
  210. ++operand_index) {
  211. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  212. if (!type_id || !_.IsFloatVectorType(type_id))
  213. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  214. << "Expected float vector as operand: "
  215. << spvOpcodeString(opcode) << " operand index "
  216. << operand_index;
  217. const uint32_t component_type = _.GetComponentType(type_id);
  218. if (component_type != result_type)
  219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  220. << "Expected component type to be equal to Result Type: "
  221. << spvOpcodeString(opcode) << " operand index "
  222. << operand_index;
  223. const uint32_t num_components = _.GetDimension(type_id);
  224. if (operand_index == 2) {
  225. first_vector_num_components = num_components;
  226. } else if (num_components != first_vector_num_components) {
  227. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  228. << "Expected operands to have the same number of components: "
  229. << spvOpcodeString(opcode);
  230. }
  231. }
  232. break;
  233. }
  234. case spv::Op::OpVectorTimesScalar: {
  235. if (!_.IsFloatVectorType(result_type) &&
  236. !_.IsFloatCooperativeVectorNVType(result_type))
  237. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  238. << "Expected float vector type as Result Type: "
  239. << spvOpcodeString(opcode);
  240. const uint32_t vector_type_id = _.GetOperandTypeId(inst, 2);
  241. if (result_type != vector_type_id)
  242. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  243. << "Expected vector operand type to be equal to Result Type: "
  244. << spvOpcodeString(opcode);
  245. const uint32_t component_type = _.GetComponentType(vector_type_id);
  246. const uint32_t scalar_type_id = _.GetOperandTypeId(inst, 3);
  247. if (component_type != scalar_type_id)
  248. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  249. << "Expected scalar operand type to be equal to the component "
  250. << "type of the vector operand: " << spvOpcodeString(opcode);
  251. break;
  252. }
  253. case spv::Op::OpMatrixTimesScalar: {
  254. if (!_.IsFloatMatrixType(result_type) &&
  255. !(_.IsCooperativeMatrixType(result_type)))
  256. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  257. << "Expected float matrix type as Result Type: "
  258. << spvOpcodeString(opcode);
  259. const uint32_t matrix_type_id = _.GetOperandTypeId(inst, 2);
  260. if (result_type != matrix_type_id)
  261. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  262. << "Expected matrix operand type to be equal to Result Type: "
  263. << spvOpcodeString(opcode);
  264. const uint32_t component_type = _.GetComponentType(matrix_type_id);
  265. const uint32_t scalar_type_id = _.GetOperandTypeId(inst, 3);
  266. if (component_type != scalar_type_id)
  267. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  268. << "Expected scalar operand type to be equal to the component "
  269. << "type of the matrix operand: " << spvOpcodeString(opcode);
  270. break;
  271. }
  272. case spv::Op::OpVectorTimesMatrix: {
  273. const uint32_t vector_type_id = _.GetOperandTypeId(inst, 2);
  274. const uint32_t matrix_type_id = _.GetOperandTypeId(inst, 3);
  275. if (!_.IsFloatVectorType(result_type))
  276. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  277. << "Expected float vector type as Result Type: "
  278. << spvOpcodeString(opcode);
  279. const uint32_t res_component_type = _.GetComponentType(result_type);
  280. if (!vector_type_id || !_.IsFloatVectorType(vector_type_id))
  281. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  282. << "Expected float vector type as left operand: "
  283. << spvOpcodeString(opcode);
  284. if (res_component_type != _.GetComponentType(vector_type_id))
  285. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  286. << "Expected component types of Result Type and vector to be "
  287. << "equal: " << spvOpcodeString(opcode);
  288. uint32_t matrix_num_rows = 0;
  289. uint32_t matrix_num_cols = 0;
  290. uint32_t matrix_col_type = 0;
  291. uint32_t matrix_component_type = 0;
  292. if (!_.GetMatrixTypeInfo(matrix_type_id, &matrix_num_rows,
  293. &matrix_num_cols, &matrix_col_type,
  294. &matrix_component_type))
  295. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  296. << "Expected float matrix type as right operand: "
  297. << spvOpcodeString(opcode);
  298. if (res_component_type != matrix_component_type)
  299. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  300. << "Expected component types of Result Type and matrix to be "
  301. << "equal: " << spvOpcodeString(opcode);
  302. if (matrix_num_cols != _.GetDimension(result_type))
  303. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  304. << "Expected number of columns of the matrix to be equal to "
  305. << "Result Type vector size: " << spvOpcodeString(opcode);
  306. if (matrix_num_rows != _.GetDimension(vector_type_id))
  307. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  308. << "Expected number of rows of the matrix to be equal to the "
  309. << "vector operand size: " << spvOpcodeString(opcode);
  310. break;
  311. }
  312. case spv::Op::OpMatrixTimesVector: {
  313. const uint32_t matrix_type_id = _.GetOperandTypeId(inst, 2);
  314. const uint32_t vector_type_id = _.GetOperandTypeId(inst, 3);
  315. if (!_.IsFloatVectorType(result_type))
  316. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  317. << "Expected float vector type as Result Type: "
  318. << spvOpcodeString(opcode);
  319. uint32_t matrix_num_rows = 0;
  320. uint32_t matrix_num_cols = 0;
  321. uint32_t matrix_col_type = 0;
  322. uint32_t matrix_component_type = 0;
  323. if (!_.GetMatrixTypeInfo(matrix_type_id, &matrix_num_rows,
  324. &matrix_num_cols, &matrix_col_type,
  325. &matrix_component_type))
  326. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  327. << "Expected float matrix type as left operand: "
  328. << spvOpcodeString(opcode);
  329. if (result_type != matrix_col_type)
  330. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  331. << "Expected column type of the matrix to be equal to Result "
  332. "Type: "
  333. << spvOpcodeString(opcode);
  334. if (!vector_type_id || !_.IsFloatVectorType(vector_type_id))
  335. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  336. << "Expected float vector type as right operand: "
  337. << spvOpcodeString(opcode);
  338. if (matrix_component_type != _.GetComponentType(vector_type_id))
  339. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  340. << "Expected component types of the operands to be equal: "
  341. << spvOpcodeString(opcode);
  342. if (matrix_num_cols != _.GetDimension(vector_type_id))
  343. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  344. << "Expected number of columns of the matrix to be equal to the "
  345. << "vector size: " << spvOpcodeString(opcode);
  346. break;
  347. }
  348. case spv::Op::OpMatrixTimesMatrix: {
  349. const uint32_t left_type_id = _.GetOperandTypeId(inst, 2);
  350. const uint32_t right_type_id = _.GetOperandTypeId(inst, 3);
  351. uint32_t res_num_rows = 0;
  352. uint32_t res_num_cols = 0;
  353. uint32_t res_col_type = 0;
  354. uint32_t res_component_type = 0;
  355. if (!_.GetMatrixTypeInfo(result_type, &res_num_rows, &res_num_cols,
  356. &res_col_type, &res_component_type))
  357. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  358. << "Expected float matrix type as Result Type: "
  359. << spvOpcodeString(opcode);
  360. uint32_t left_num_rows = 0;
  361. uint32_t left_num_cols = 0;
  362. uint32_t left_col_type = 0;
  363. uint32_t left_component_type = 0;
  364. if (!_.GetMatrixTypeInfo(left_type_id, &left_num_rows, &left_num_cols,
  365. &left_col_type, &left_component_type))
  366. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  367. << "Expected float matrix type as left operand: "
  368. << spvOpcodeString(opcode);
  369. uint32_t right_num_rows = 0;
  370. uint32_t right_num_cols = 0;
  371. uint32_t right_col_type = 0;
  372. uint32_t right_component_type = 0;
  373. if (!_.GetMatrixTypeInfo(right_type_id, &right_num_rows, &right_num_cols,
  374. &right_col_type, &right_component_type))
  375. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  376. << "Expected float matrix type as right operand: "
  377. << spvOpcodeString(opcode);
  378. if (!_.IsFloatScalarType(res_component_type))
  379. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  380. << "Expected float matrix type as Result Type: "
  381. << spvOpcodeString(opcode);
  382. if (res_col_type != left_col_type)
  383. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  384. << "Expected column types of Result Type and left matrix to be "
  385. << "equal: " << spvOpcodeString(opcode);
  386. if (res_component_type != right_component_type)
  387. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  388. << "Expected component types of Result Type and right matrix to "
  389. "be "
  390. << "equal: " << spvOpcodeString(opcode);
  391. if (res_num_cols != right_num_cols)
  392. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  393. << "Expected number of columns of Result Type and right matrix "
  394. "to "
  395. << "be equal: " << spvOpcodeString(opcode);
  396. if (left_num_cols != right_num_rows)
  397. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  398. << "Expected number of columns of left matrix and number of "
  399. "rows "
  400. << "of right matrix to be equal: " << spvOpcodeString(opcode);
  401. assert(left_num_rows == res_num_rows);
  402. break;
  403. }
  404. case spv::Op::OpOuterProduct: {
  405. const uint32_t left_type_id = _.GetOperandTypeId(inst, 2);
  406. const uint32_t right_type_id = _.GetOperandTypeId(inst, 3);
  407. uint32_t res_num_rows = 0;
  408. uint32_t res_num_cols = 0;
  409. uint32_t res_col_type = 0;
  410. uint32_t res_component_type = 0;
  411. if (!_.GetMatrixTypeInfo(result_type, &res_num_rows, &res_num_cols,
  412. &res_col_type, &res_component_type))
  413. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  414. << "Expected float matrix type as Result Type: "
  415. << spvOpcodeString(opcode);
  416. if (left_type_id != res_col_type)
  417. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  418. << "Expected column type of Result Type to be equal to the type "
  419. << "of the left operand: " << spvOpcodeString(opcode);
  420. if (!right_type_id || !_.IsFloatVectorType(right_type_id))
  421. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  422. << "Expected float vector type as right operand: "
  423. << spvOpcodeString(opcode);
  424. if (res_component_type != _.GetComponentType(right_type_id))
  425. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  426. << "Expected component types of the operands to be equal: "
  427. << spvOpcodeString(opcode);
  428. if (res_num_cols != _.GetDimension(right_type_id))
  429. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  430. << "Expected number of columns of the matrix to be equal to the "
  431. << "vector size of the right operand: "
  432. << spvOpcodeString(opcode);
  433. break;
  434. }
  435. case spv::Op::OpIAddCarry:
  436. case spv::Op::OpISubBorrow:
  437. case spv::Op::OpUMulExtended:
  438. case spv::Op::OpSMulExtended: {
  439. std::vector<uint32_t> result_types;
  440. if (!_.GetStructMemberTypes(result_type, &result_types))
  441. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  442. << "Expected a struct as Result Type: "
  443. << spvOpcodeString(opcode);
  444. if (result_types.size() != 2)
  445. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  446. << "Expected Result Type struct to have two members: "
  447. << spvOpcodeString(opcode);
  448. if (opcode == spv::Op::OpSMulExtended) {
  449. if (!_.IsIntScalarType(result_types[0]) &&
  450. !_.IsIntVectorType(result_types[0]))
  451. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  452. << "Expected Result Type struct member types to be integer "
  453. "scalar "
  454. << "or vector: " << spvOpcodeString(opcode);
  455. } else {
  456. if (!_.IsUnsignedIntScalarType(result_types[0]) &&
  457. !_.IsUnsignedIntVectorType(result_types[0]))
  458. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  459. << "Expected Result Type struct member types to be unsigned "
  460. << "integer scalar or vector: " << spvOpcodeString(opcode);
  461. }
  462. if (result_types[0] != result_types[1])
  463. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  464. << "Expected Result Type struct member types to be identical: "
  465. << spvOpcodeString(opcode);
  466. const uint32_t left_type_id = _.GetOperandTypeId(inst, 2);
  467. const uint32_t right_type_id = _.GetOperandTypeId(inst, 3);
  468. if (left_type_id != result_types[0] || right_type_id != result_types[0])
  469. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  470. << "Expected both operands to be of Result Type member type: "
  471. << spvOpcodeString(opcode);
  472. break;
  473. }
  474. case spv::Op::OpCooperativeMatrixMulAddNV: {
  475. const uint32_t D_type_id = _.GetOperandTypeId(inst, 1);
  476. const uint32_t A_type_id = _.GetOperandTypeId(inst, 2);
  477. const uint32_t B_type_id = _.GetOperandTypeId(inst, 3);
  478. const uint32_t C_type_id = _.GetOperandTypeId(inst, 4);
  479. if (!_.IsCooperativeMatrixNVType(A_type_id)) {
  480. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  481. << "Expected cooperative matrix type as A Type: "
  482. << spvOpcodeString(opcode);
  483. }
  484. if (!_.IsCooperativeMatrixNVType(B_type_id)) {
  485. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  486. << "Expected cooperative matrix type as B Type: "
  487. << spvOpcodeString(opcode);
  488. }
  489. if (!_.IsCooperativeMatrixNVType(C_type_id)) {
  490. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  491. << "Expected cooperative matrix type as C Type: "
  492. << spvOpcodeString(opcode);
  493. }
  494. if (!_.IsCooperativeMatrixNVType(D_type_id)) {
  495. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  496. << "Expected cooperative matrix type as Result Type: "
  497. << spvOpcodeString(opcode);
  498. }
  499. const auto A = _.FindDef(A_type_id);
  500. const auto B = _.FindDef(B_type_id);
  501. const auto C = _.FindDef(C_type_id);
  502. const auto D = _.FindDef(D_type_id);
  503. std::tuple<bool, bool, uint32_t> A_scope, B_scope, C_scope, D_scope,
  504. A_rows, B_rows, C_rows, D_rows, A_cols, B_cols, C_cols, D_cols;
  505. A_scope = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(2));
  506. B_scope = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(2));
  507. C_scope = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(2));
  508. D_scope = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(2));
  509. A_rows = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(3));
  510. B_rows = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(3));
  511. C_rows = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(3));
  512. D_rows = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(3));
  513. A_cols = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(4));
  514. B_cols = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(4));
  515. C_cols = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(4));
  516. D_cols = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(4));
  517. const auto notEqual = [](std::tuple<bool, bool, uint32_t> X,
  518. std::tuple<bool, bool, uint32_t> Y) {
  519. return (std::get<1>(X) && std::get<1>(Y) &&
  520. std::get<2>(X) != std::get<2>(Y));
  521. };
  522. if (notEqual(A_scope, B_scope) || notEqual(A_scope, C_scope) ||
  523. notEqual(A_scope, D_scope) || notEqual(B_scope, C_scope) ||
  524. notEqual(B_scope, D_scope) || notEqual(C_scope, D_scope)) {
  525. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  526. << "Cooperative matrix scopes must match: "
  527. << spvOpcodeString(opcode);
  528. }
  529. if (notEqual(A_rows, C_rows) || notEqual(A_rows, D_rows) ||
  530. notEqual(C_rows, D_rows)) {
  531. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  532. << "Cooperative matrix 'M' mismatch: "
  533. << spvOpcodeString(opcode);
  534. }
  535. if (notEqual(B_cols, C_cols) || notEqual(B_cols, D_cols) ||
  536. notEqual(C_cols, D_cols)) {
  537. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  538. << "Cooperative matrix 'N' mismatch: "
  539. << spvOpcodeString(opcode);
  540. }
  541. if (notEqual(A_cols, B_rows)) {
  542. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  543. << "Cooperative matrix 'K' mismatch: "
  544. << spvOpcodeString(opcode);
  545. }
  546. break;
  547. }
  548. case spv::Op::OpCooperativeMatrixMulAddKHR: {
  549. const uint32_t D_type_id = _.GetOperandTypeId(inst, 1);
  550. const uint32_t A_type_id = _.GetOperandTypeId(inst, 2);
  551. const uint32_t B_type_id = _.GetOperandTypeId(inst, 3);
  552. const uint32_t C_type_id = _.GetOperandTypeId(inst, 4);
  553. if (!_.IsCooperativeMatrixAType(A_type_id)) {
  554. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  555. << "Cooperative matrix type must be A Type: "
  556. << spvOpcodeString(opcode);
  557. }
  558. if (!_.IsCooperativeMatrixBType(B_type_id)) {
  559. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  560. << "Cooperative matrix type must be B Type: "
  561. << spvOpcodeString(opcode);
  562. }
  563. if (!_.IsCooperativeMatrixAccType(C_type_id)) {
  564. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  565. << "Cooperative matrix type must be Accumulator Type: "
  566. << spvOpcodeString(opcode);
  567. }
  568. if (!_.IsCooperativeMatrixKHRType(D_type_id)) {
  569. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  570. << "Expected cooperative matrix type as Result Type: "
  571. << spvOpcodeString(opcode);
  572. }
  573. const auto A = _.FindDef(A_type_id);
  574. const auto B = _.FindDef(B_type_id);
  575. const auto C = _.FindDef(C_type_id);
  576. const auto D = _.FindDef(D_type_id);
  577. std::tuple<bool, bool, uint32_t> A_scope, B_scope, C_scope, D_scope,
  578. A_rows, B_rows, C_rows, D_rows, A_cols, B_cols, C_cols, D_cols;
  579. A_scope = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(2));
  580. B_scope = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(2));
  581. C_scope = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(2));
  582. D_scope = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(2));
  583. A_rows = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(3));
  584. B_rows = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(3));
  585. C_rows = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(3));
  586. D_rows = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(3));
  587. A_cols = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(4));
  588. B_cols = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(4));
  589. C_cols = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(4));
  590. D_cols = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(4));
  591. const auto notEqual = [](std::tuple<bool, bool, uint32_t> X,
  592. std::tuple<bool, bool, uint32_t> Y) {
  593. return (std::get<1>(X) && std::get<1>(Y) &&
  594. std::get<2>(X) != std::get<2>(Y));
  595. };
  596. if (notEqual(A_scope, B_scope) || notEqual(A_scope, C_scope) ||
  597. notEqual(A_scope, D_scope) || notEqual(B_scope, C_scope) ||
  598. notEqual(B_scope, D_scope) || notEqual(C_scope, D_scope)) {
  599. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  600. << "Cooperative matrix scopes must match: "
  601. << spvOpcodeString(opcode);
  602. }
  603. if (notEqual(A_rows, C_rows) || notEqual(A_rows, D_rows) ||
  604. notEqual(C_rows, D_rows)) {
  605. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  606. << "Cooperative matrix 'M' mismatch: "
  607. << spvOpcodeString(opcode);
  608. }
  609. if (notEqual(B_cols, C_cols) || notEqual(B_cols, D_cols) ||
  610. notEqual(C_cols, D_cols)) {
  611. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  612. << "Cooperative matrix 'N' mismatch: "
  613. << spvOpcodeString(opcode);
  614. }
  615. if (notEqual(A_cols, B_rows)) {
  616. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  617. << "Cooperative matrix 'K' mismatch: "
  618. << spvOpcodeString(opcode);
  619. }
  620. break;
  621. }
  622. case spv::Op::OpCooperativeMatrixReduceNV: {
  623. if (!_.IsCooperativeMatrixKHRType(result_type)) {
  624. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  625. << "Result Type must be a cooperative matrix type: "
  626. << spvOpcodeString(opcode);
  627. }
  628. const auto result_comp_type_id =
  629. _.FindDef(result_type)->GetOperandAs<uint32_t>(1);
  630. const auto matrix_id = inst->GetOperandAs<uint32_t>(2);
  631. const auto matrix = _.FindDef(matrix_id);
  632. const auto matrix_type_id = matrix->type_id();
  633. if (!_.IsCooperativeMatrixKHRType(matrix_type_id)) {
  634. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  635. << "Matrix must have a cooperative matrix type: "
  636. << spvOpcodeString(opcode);
  637. }
  638. const auto matrix_type = _.FindDef(matrix_type_id);
  639. const auto matrix_comp_type_id = matrix_type->GetOperandAs<uint32_t>(1);
  640. if (matrix_comp_type_id != result_comp_type_id) {
  641. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  642. << "Result Type and Matrix type must have the same component "
  643. "type: "
  644. << spvOpcodeString(opcode);
  645. }
  646. if (_.FindDef(result_type)->GetOperandAs<uint32_t>(2) !=
  647. matrix_type->GetOperandAs<uint32_t>(2)) {
  648. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  649. << "Result Type and Matrix type must have the same scope: "
  650. << spvOpcodeString(opcode);
  651. }
  652. if (!_.IsCooperativeMatrixAccType(result_type)) {
  653. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  654. << "Result Type must have UseAccumulator: "
  655. << spvOpcodeString(opcode);
  656. }
  657. if (!_.IsCooperativeMatrixAccType(matrix_type_id)) {
  658. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  659. << "Matrix type must have UseAccumulator: "
  660. << spvOpcodeString(opcode);
  661. }
  662. const auto reduce_value = inst->GetOperandAs<uint32_t>(3);
  663. if ((reduce_value &
  664. uint32_t(
  665. spv::CooperativeMatrixReduceMask::CooperativeMatrixReduce2x2)) &&
  666. (reduce_value & uint32_t(spv::CooperativeMatrixReduceMask::Row |
  667. spv::CooperativeMatrixReduceMask::Column))) {
  668. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  669. << "Reduce 2x2 must not be used with Row/Column: "
  670. << spvOpcodeString(opcode);
  671. }
  672. std::tuple<bool, bool, uint32_t> result_rows, result_cols, matrix_rows,
  673. matrix_cols;
  674. result_rows =
  675. _.EvalInt32IfConst(_.FindDef(result_type)->GetOperandAs<uint32_t>(3));
  676. result_cols =
  677. _.EvalInt32IfConst(_.FindDef(result_type)->GetOperandAs<uint32_t>(4));
  678. matrix_rows = _.EvalInt32IfConst(matrix_type->GetOperandAs<uint32_t>(3));
  679. matrix_cols = _.EvalInt32IfConst(matrix_type->GetOperandAs<uint32_t>(4));
  680. if (reduce_value &
  681. uint32_t(
  682. spv::CooperativeMatrixReduceMask::CooperativeMatrixReduce2x2)) {
  683. if (std::get<1>(result_rows) && std::get<1>(result_cols) &&
  684. std::get<1>(matrix_rows) && std::get<1>(matrix_cols) &&
  685. (std::get<2>(result_rows) != std::get<2>(matrix_rows) / 2 ||
  686. std::get<2>(result_cols) != std::get<2>(matrix_cols) / 2)) {
  687. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  688. << "For Reduce2x2, result rows/cols must be half of matrix "
  689. "rows/cols: "
  690. << spvOpcodeString(opcode);
  691. }
  692. }
  693. if (reduce_value == uint32_t(spv::CooperativeMatrixReduceMask::Row)) {
  694. if (std::get<1>(result_rows) && std::get<1>(matrix_rows) &&
  695. std::get<2>(result_rows) != std::get<2>(matrix_rows)) {
  696. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  697. << "For ReduceRow, result rows must match matrix rows: "
  698. << spvOpcodeString(opcode);
  699. }
  700. }
  701. if (reduce_value == uint32_t(spv::CooperativeMatrixReduceMask::Column)) {
  702. if (std::get<1>(result_cols) && std::get<1>(matrix_cols) &&
  703. std::get<2>(result_cols) != std::get<2>(matrix_cols)) {
  704. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  705. << "For ReduceColumn, result cols must match matrix cols: "
  706. << spvOpcodeString(opcode);
  707. }
  708. }
  709. const auto combine_func_id = inst->GetOperandAs<uint32_t>(4);
  710. const auto combine_func = _.FindDef(combine_func_id);
  711. if (!combine_func || combine_func->opcode() != spv::Op::OpFunction) {
  712. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  713. << "CombineFunc must be a function: " << spvOpcodeString(opcode);
  714. }
  715. const auto function_type_id = combine_func->GetOperandAs<uint32_t>(3);
  716. const auto function_type = _.FindDef(function_type_id);
  717. if (function_type->operands().size() != 4) {
  718. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  719. << "CombineFunc must have two parameters: "
  720. << spvOpcodeString(opcode);
  721. }
  722. for (uint32_t i = 0; i < 3; ++i) {
  723. // checks return type and two params
  724. const auto param_type_id = function_type->GetOperandAs<uint32_t>(i + 1);
  725. if (param_type_id != matrix_comp_type_id) {
  726. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  727. << "CombineFunc return type and parameters must match matrix "
  728. "component type: "
  729. << spvOpcodeString(opcode);
  730. }
  731. }
  732. break;
  733. }
  734. default:
  735. break;
  736. }
  737. return SPV_SUCCESS;
  738. }
  739. } // namespace val
  740. } // namespace spvtools