validate_arithmetics.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 "source/val/validate.h"
  16. #include <vector>
  17. #include "source/diagnostic.h"
  18. #include "source/opcode.h"
  19. #include "source/val/instruction.h"
  20. #include "source/val/validation_state.h"
  21. namespace spvtools {
  22. namespace val {
  23. // Validates correctness of arithmetic instructions.
  24. spv_result_t ArithmeticsPass(ValidationState_t& _, const Instruction* inst) {
  25. const spv::Op opcode = inst->opcode();
  26. const uint32_t result_type = inst->type_id();
  27. switch (opcode) {
  28. case spv::Op::OpFAdd:
  29. case spv::Op::OpFSub:
  30. case spv::Op::OpFMul:
  31. case spv::Op::OpFDiv:
  32. case spv::Op::OpFRem:
  33. case spv::Op::OpFMod:
  34. case spv::Op::OpFNegate: {
  35. bool supportsCoopMat =
  36. (opcode != spv::Op::OpFMul && opcode != spv::Op::OpFRem &&
  37. opcode != spv::Op::OpFMod);
  38. if (!_.IsFloatScalarType(result_type) &&
  39. !_.IsFloatVectorType(result_type) &&
  40. !(supportsCoopMat && _.IsFloatCooperativeMatrixType(result_type)))
  41. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  42. << "Expected floating scalar or vector type as Result Type: "
  43. << spvOpcodeString(opcode);
  44. for (size_t operand_index = 2; operand_index < inst->operands().size();
  45. ++operand_index) {
  46. if (_.GetOperandTypeId(inst, operand_index) != result_type)
  47. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  48. << "Expected arithmetic operands to be of Result Type: "
  49. << spvOpcodeString(opcode) << " operand index "
  50. << operand_index;
  51. }
  52. break;
  53. }
  54. case spv::Op::OpUDiv:
  55. case spv::Op::OpUMod: {
  56. bool supportsCoopMat = (opcode == spv::Op::OpUDiv);
  57. if (!_.IsUnsignedIntScalarType(result_type) &&
  58. !_.IsUnsignedIntVectorType(result_type) &&
  59. !(supportsCoopMat &&
  60. _.IsUnsignedIntCooperativeMatrixType(result_type)))
  61. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  62. << "Expected unsigned int scalar or vector type as Result Type: "
  63. << spvOpcodeString(opcode);
  64. for (size_t operand_index = 2; operand_index < inst->operands().size();
  65. ++operand_index) {
  66. if (_.GetOperandTypeId(inst, operand_index) != result_type)
  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. break;
  73. }
  74. case spv::Op::OpISub:
  75. case spv::Op::OpIAdd:
  76. case spv::Op::OpIMul:
  77. case spv::Op::OpSDiv:
  78. case spv::Op::OpSMod:
  79. case spv::Op::OpSRem:
  80. case spv::Op::OpSNegate: {
  81. bool supportsCoopMat =
  82. (opcode != spv::Op::OpIMul && opcode != spv::Op::OpSRem &&
  83. opcode != spv::Op::OpSMod);
  84. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
  85. !(supportsCoopMat && _.IsIntCooperativeMatrixType(result_type)))
  86. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  87. << "Expected int scalar or vector type as Result Type: "
  88. << spvOpcodeString(opcode);
  89. const uint32_t dimension = _.GetDimension(result_type);
  90. const uint32_t bit_width = _.GetBitWidth(result_type);
  91. for (size_t operand_index = 2; operand_index < inst->operands().size();
  92. ++operand_index) {
  93. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  94. if (!type_id ||
  95. (!_.IsIntScalarType(type_id) && !_.IsIntVectorType(type_id) &&
  96. !(supportsCoopMat && _.IsIntCooperativeMatrixType(result_type))))
  97. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  98. << "Expected int scalar or vector type as operand: "
  99. << spvOpcodeString(opcode) << " operand index "
  100. << operand_index;
  101. if (_.GetDimension(type_id) != dimension)
  102. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  103. << "Expected arithmetic operands to have the same dimension "
  104. << "as Result Type: " << spvOpcodeString(opcode)
  105. << " operand index " << operand_index;
  106. if (_.GetBitWidth(type_id) != bit_width)
  107. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  108. << "Expected arithmetic operands to have the same bit width "
  109. << "as Result Type: " << spvOpcodeString(opcode)
  110. << " operand index " << operand_index;
  111. }
  112. break;
  113. }
  114. case spv::Op::OpDot: {
  115. if (!_.IsFloatScalarType(result_type))
  116. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  117. << "Expected float scalar type as Result Type: "
  118. << spvOpcodeString(opcode);
  119. uint32_t first_vector_num_components = 0;
  120. for (size_t operand_index = 2; operand_index < inst->operands().size();
  121. ++operand_index) {
  122. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  123. if (!type_id || !_.IsFloatVectorType(type_id))
  124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  125. << "Expected float vector as operand: "
  126. << spvOpcodeString(opcode) << " operand index "
  127. << operand_index;
  128. const uint32_t component_type = _.GetComponentType(type_id);
  129. if (component_type != result_type)
  130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  131. << "Expected component type to be equal to Result Type: "
  132. << spvOpcodeString(opcode) << " operand index "
  133. << operand_index;
  134. const uint32_t num_components = _.GetDimension(type_id);
  135. if (operand_index == 2) {
  136. first_vector_num_components = num_components;
  137. } else if (num_components != first_vector_num_components) {
  138. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  139. << "Expected operands to have the same number of components: "
  140. << spvOpcodeString(opcode);
  141. }
  142. }
  143. break;
  144. }
  145. case spv::Op::OpVectorTimesScalar: {
  146. if (!_.IsFloatVectorType(result_type))
  147. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  148. << "Expected float vector type as Result Type: "
  149. << spvOpcodeString(opcode);
  150. const uint32_t vector_type_id = _.GetOperandTypeId(inst, 2);
  151. if (result_type != vector_type_id)
  152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  153. << "Expected vector operand type to be equal to Result Type: "
  154. << spvOpcodeString(opcode);
  155. const uint32_t component_type = _.GetComponentType(vector_type_id);
  156. const uint32_t scalar_type_id = _.GetOperandTypeId(inst, 3);
  157. if (component_type != scalar_type_id)
  158. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  159. << "Expected scalar operand type to be equal to the component "
  160. << "type of the vector operand: " << spvOpcodeString(opcode);
  161. break;
  162. }
  163. case spv::Op::OpMatrixTimesScalar: {
  164. if (!_.IsFloatMatrixType(result_type) &&
  165. !_.IsCooperativeMatrixType(result_type))
  166. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  167. << "Expected float matrix type as Result Type: "
  168. << spvOpcodeString(opcode);
  169. const uint32_t matrix_type_id = _.GetOperandTypeId(inst, 2);
  170. if (result_type != matrix_type_id)
  171. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  172. << "Expected matrix operand type to be equal to Result Type: "
  173. << spvOpcodeString(opcode);
  174. const uint32_t component_type = _.GetComponentType(matrix_type_id);
  175. const uint32_t scalar_type_id = _.GetOperandTypeId(inst, 3);
  176. if (component_type != scalar_type_id)
  177. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  178. << "Expected scalar operand type to be equal to the component "
  179. << "type of the matrix operand: " << spvOpcodeString(opcode);
  180. break;
  181. }
  182. case spv::Op::OpVectorTimesMatrix: {
  183. const uint32_t vector_type_id = _.GetOperandTypeId(inst, 2);
  184. const uint32_t matrix_type_id = _.GetOperandTypeId(inst, 3);
  185. if (!_.IsFloatVectorType(result_type))
  186. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  187. << "Expected float vector type as Result Type: "
  188. << spvOpcodeString(opcode);
  189. const uint32_t res_component_type = _.GetComponentType(result_type);
  190. if (!vector_type_id || !_.IsFloatVectorType(vector_type_id))
  191. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  192. << "Expected float vector type as left operand: "
  193. << spvOpcodeString(opcode);
  194. if (res_component_type != _.GetComponentType(vector_type_id))
  195. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  196. << "Expected component types of Result Type and vector to be "
  197. << "equal: " << spvOpcodeString(opcode);
  198. uint32_t matrix_num_rows = 0;
  199. uint32_t matrix_num_cols = 0;
  200. uint32_t matrix_col_type = 0;
  201. uint32_t matrix_component_type = 0;
  202. if (!_.GetMatrixTypeInfo(matrix_type_id, &matrix_num_rows,
  203. &matrix_num_cols, &matrix_col_type,
  204. &matrix_component_type))
  205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  206. << "Expected float matrix type as right operand: "
  207. << spvOpcodeString(opcode);
  208. if (res_component_type != matrix_component_type)
  209. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  210. << "Expected component types of Result Type and matrix to be "
  211. << "equal: " << spvOpcodeString(opcode);
  212. if (matrix_num_cols != _.GetDimension(result_type))
  213. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  214. << "Expected number of columns of the matrix to be equal to "
  215. << "Result Type vector size: " << spvOpcodeString(opcode);
  216. if (matrix_num_rows != _.GetDimension(vector_type_id))
  217. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  218. << "Expected number of rows of the matrix to be equal to the "
  219. << "vector operand size: " << spvOpcodeString(opcode);
  220. break;
  221. }
  222. case spv::Op::OpMatrixTimesVector: {
  223. const uint32_t matrix_type_id = _.GetOperandTypeId(inst, 2);
  224. const uint32_t vector_type_id = _.GetOperandTypeId(inst, 3);
  225. if (!_.IsFloatVectorType(result_type))
  226. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  227. << "Expected float vector type as Result Type: "
  228. << spvOpcodeString(opcode);
  229. uint32_t matrix_num_rows = 0;
  230. uint32_t matrix_num_cols = 0;
  231. uint32_t matrix_col_type = 0;
  232. uint32_t matrix_component_type = 0;
  233. if (!_.GetMatrixTypeInfo(matrix_type_id, &matrix_num_rows,
  234. &matrix_num_cols, &matrix_col_type,
  235. &matrix_component_type))
  236. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  237. << "Expected float matrix type as left operand: "
  238. << spvOpcodeString(opcode);
  239. if (result_type != matrix_col_type)
  240. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  241. << "Expected column type of the matrix to be equal to Result "
  242. "Type: "
  243. << spvOpcodeString(opcode);
  244. if (!vector_type_id || !_.IsFloatVectorType(vector_type_id))
  245. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  246. << "Expected float vector type as right operand: "
  247. << spvOpcodeString(opcode);
  248. if (matrix_component_type != _.GetComponentType(vector_type_id))
  249. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  250. << "Expected component types of the operands to be equal: "
  251. << spvOpcodeString(opcode);
  252. if (matrix_num_cols != _.GetDimension(vector_type_id))
  253. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  254. << "Expected number of columns of the matrix to be equal to the "
  255. << "vector size: " << spvOpcodeString(opcode);
  256. break;
  257. }
  258. case spv::Op::OpMatrixTimesMatrix: {
  259. const uint32_t left_type_id = _.GetOperandTypeId(inst, 2);
  260. const uint32_t right_type_id = _.GetOperandTypeId(inst, 3);
  261. uint32_t res_num_rows = 0;
  262. uint32_t res_num_cols = 0;
  263. uint32_t res_col_type = 0;
  264. uint32_t res_component_type = 0;
  265. if (!_.GetMatrixTypeInfo(result_type, &res_num_rows, &res_num_cols,
  266. &res_col_type, &res_component_type))
  267. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  268. << "Expected float matrix type as Result Type: "
  269. << spvOpcodeString(opcode);
  270. uint32_t left_num_rows = 0;
  271. uint32_t left_num_cols = 0;
  272. uint32_t left_col_type = 0;
  273. uint32_t left_component_type = 0;
  274. if (!_.GetMatrixTypeInfo(left_type_id, &left_num_rows, &left_num_cols,
  275. &left_col_type, &left_component_type))
  276. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  277. << "Expected float matrix type as left operand: "
  278. << spvOpcodeString(opcode);
  279. uint32_t right_num_rows = 0;
  280. uint32_t right_num_cols = 0;
  281. uint32_t right_col_type = 0;
  282. uint32_t right_component_type = 0;
  283. if (!_.GetMatrixTypeInfo(right_type_id, &right_num_rows, &right_num_cols,
  284. &right_col_type, &right_component_type))
  285. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  286. << "Expected float matrix type as right operand: "
  287. << spvOpcodeString(opcode);
  288. if (!_.IsFloatScalarType(res_component_type))
  289. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  290. << "Expected float matrix type as Result Type: "
  291. << spvOpcodeString(opcode);
  292. if (res_col_type != left_col_type)
  293. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  294. << "Expected column types of Result Type and left matrix to be "
  295. << "equal: " << spvOpcodeString(opcode);
  296. if (res_component_type != right_component_type)
  297. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  298. << "Expected component types of Result Type and right matrix to "
  299. "be "
  300. << "equal: " << spvOpcodeString(opcode);
  301. if (res_num_cols != right_num_cols)
  302. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  303. << "Expected number of columns of Result Type and right matrix "
  304. "to "
  305. << "be equal: " << spvOpcodeString(opcode);
  306. if (left_num_cols != right_num_rows)
  307. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  308. << "Expected number of columns of left matrix and number of "
  309. "rows "
  310. << "of right matrix to be equal: " << spvOpcodeString(opcode);
  311. assert(left_num_rows == res_num_rows);
  312. break;
  313. }
  314. case spv::Op::OpOuterProduct: {
  315. const uint32_t left_type_id = _.GetOperandTypeId(inst, 2);
  316. const uint32_t right_type_id = _.GetOperandTypeId(inst, 3);
  317. uint32_t res_num_rows = 0;
  318. uint32_t res_num_cols = 0;
  319. uint32_t res_col_type = 0;
  320. uint32_t res_component_type = 0;
  321. if (!_.GetMatrixTypeInfo(result_type, &res_num_rows, &res_num_cols,
  322. &res_col_type, &res_component_type))
  323. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  324. << "Expected float matrix type as Result Type: "
  325. << spvOpcodeString(opcode);
  326. if (left_type_id != res_col_type)
  327. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  328. << "Expected column type of Result Type to be equal to the type "
  329. << "of the left operand: " << spvOpcodeString(opcode);
  330. if (!right_type_id || !_.IsFloatVectorType(right_type_id))
  331. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  332. << "Expected float vector type as right operand: "
  333. << spvOpcodeString(opcode);
  334. if (res_component_type != _.GetComponentType(right_type_id))
  335. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  336. << "Expected component types of the operands to be equal: "
  337. << spvOpcodeString(opcode);
  338. if (res_num_cols != _.GetDimension(right_type_id))
  339. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  340. << "Expected number of columns of the matrix to be equal to the "
  341. << "vector size of the right operand: "
  342. << spvOpcodeString(opcode);
  343. break;
  344. }
  345. case spv::Op::OpIAddCarry:
  346. case spv::Op::OpISubBorrow:
  347. case spv::Op::OpUMulExtended:
  348. case spv::Op::OpSMulExtended: {
  349. std::vector<uint32_t> result_types;
  350. if (!_.GetStructMemberTypes(result_type, &result_types))
  351. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  352. << "Expected a struct as Result Type: "
  353. << spvOpcodeString(opcode);
  354. if (result_types.size() != 2)
  355. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  356. << "Expected Result Type struct to have two members: "
  357. << spvOpcodeString(opcode);
  358. if (opcode == spv::Op::OpSMulExtended) {
  359. if (!_.IsIntScalarType(result_types[0]) &&
  360. !_.IsIntVectorType(result_types[0]))
  361. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  362. << "Expected Result Type struct member types to be integer "
  363. "scalar "
  364. << "or vector: " << spvOpcodeString(opcode);
  365. } else {
  366. if (!_.IsUnsignedIntScalarType(result_types[0]) &&
  367. !_.IsUnsignedIntVectorType(result_types[0]))
  368. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  369. << "Expected Result Type struct member types to be unsigned "
  370. << "integer scalar or vector: " << spvOpcodeString(opcode);
  371. }
  372. if (result_types[0] != result_types[1])
  373. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  374. << "Expected Result Type struct member types to be identical: "
  375. << spvOpcodeString(opcode);
  376. const uint32_t left_type_id = _.GetOperandTypeId(inst, 2);
  377. const uint32_t right_type_id = _.GetOperandTypeId(inst, 3);
  378. if (left_type_id != result_types[0] || right_type_id != result_types[0])
  379. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  380. << "Expected both operands to be of Result Type member type: "
  381. << spvOpcodeString(opcode);
  382. break;
  383. }
  384. case spv::Op::OpCooperativeMatrixMulAddNV: {
  385. const uint32_t D_type_id = _.GetOperandTypeId(inst, 1);
  386. const uint32_t A_type_id = _.GetOperandTypeId(inst, 2);
  387. const uint32_t B_type_id = _.GetOperandTypeId(inst, 3);
  388. const uint32_t C_type_id = _.GetOperandTypeId(inst, 4);
  389. if (!_.IsCooperativeMatrixType(A_type_id)) {
  390. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  391. << "Expected cooperative matrix type as A Type: "
  392. << spvOpcodeString(opcode);
  393. }
  394. if (!_.IsCooperativeMatrixType(B_type_id)) {
  395. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  396. << "Expected cooperative matrix type as B Type: "
  397. << spvOpcodeString(opcode);
  398. }
  399. if (!_.IsCooperativeMatrixType(C_type_id)) {
  400. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  401. << "Expected cooperative matrix type as C Type: "
  402. << spvOpcodeString(opcode);
  403. }
  404. if (!_.IsCooperativeMatrixType(D_type_id)) {
  405. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  406. << "Expected cooperative matrix type as Result Type: "
  407. << spvOpcodeString(opcode);
  408. }
  409. const auto A = _.FindDef(A_type_id);
  410. const auto B = _.FindDef(B_type_id);
  411. const auto C = _.FindDef(C_type_id);
  412. const auto D = _.FindDef(D_type_id);
  413. std::tuple<bool, bool, uint32_t> A_scope, B_scope, C_scope, D_scope,
  414. A_rows, B_rows, C_rows, D_rows, A_cols, B_cols, C_cols, D_cols;
  415. A_scope = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(2));
  416. B_scope = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(2));
  417. C_scope = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(2));
  418. D_scope = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(2));
  419. A_rows = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(3));
  420. B_rows = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(3));
  421. C_rows = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(3));
  422. D_rows = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(3));
  423. A_cols = _.EvalInt32IfConst(A->GetOperandAs<uint32_t>(4));
  424. B_cols = _.EvalInt32IfConst(B->GetOperandAs<uint32_t>(4));
  425. C_cols = _.EvalInt32IfConst(C->GetOperandAs<uint32_t>(4));
  426. D_cols = _.EvalInt32IfConst(D->GetOperandAs<uint32_t>(4));
  427. const auto notEqual = [](std::tuple<bool, bool, uint32_t> X,
  428. std::tuple<bool, bool, uint32_t> Y) {
  429. return (std::get<1>(X) && std::get<1>(Y) &&
  430. std::get<2>(X) != std::get<2>(Y));
  431. };
  432. if (notEqual(A_scope, B_scope) || notEqual(A_scope, C_scope) ||
  433. notEqual(A_scope, D_scope) || notEqual(B_scope, C_scope) ||
  434. notEqual(B_scope, D_scope) || notEqual(C_scope, D_scope)) {
  435. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  436. << "Cooperative matrix scopes must match: "
  437. << spvOpcodeString(opcode);
  438. }
  439. if (notEqual(A_rows, C_rows) || notEqual(A_rows, D_rows) ||
  440. notEqual(C_rows, D_rows)) {
  441. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  442. << "Cooperative matrix 'M' mismatch: "
  443. << spvOpcodeString(opcode);
  444. }
  445. if (notEqual(B_cols, C_cols) || notEqual(B_cols, D_cols) ||
  446. notEqual(C_cols, D_cols)) {
  447. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  448. << "Cooperative matrix 'N' mismatch: "
  449. << spvOpcodeString(opcode);
  450. }
  451. if (notEqual(A_cols, B_rows)) {
  452. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  453. << "Cooperative matrix 'K' mismatch: "
  454. << spvOpcodeString(opcode);
  455. }
  456. break;
  457. }
  458. default:
  459. break;
  460. }
  461. return SPV_SUCCESS;
  462. }
  463. } // namespace val
  464. } // namespace spvtools