validate_composites.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // Copyright (c) 2017 Google Inc.
  2. // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
  3. // reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Validates correctness of composite SPIR-V instructions.
  17. #include "source/opcode.h"
  18. #include "source/spirv_target_env.h"
  19. #include "source/val/instruction.h"
  20. #include "source/val/validate.h"
  21. #include "source/val/validation_state.h"
  22. namespace spvtools {
  23. namespace val {
  24. namespace {
  25. // Returns the type of the value accessed by OpCompositeExtract or
  26. // OpCompositeInsert instruction. The function traverses the hierarchy of
  27. // nested data structures (structs, arrays, vectors, matrices) as directed by
  28. // the sequence of indices in the instruction. May return error if traversal
  29. // fails (encountered non-composite, out of bounds, no indices, nesting too
  30. // deep).
  31. spv_result_t GetExtractInsertValueType(ValidationState_t& _,
  32. const Instruction* inst,
  33. uint32_t* member_type) {
  34. const spv::Op opcode = inst->opcode();
  35. assert(opcode == spv::Op::OpCompositeExtract ||
  36. opcode == spv::Op::OpCompositeInsert);
  37. uint32_t word_index = opcode == spv::Op::OpCompositeExtract ? 4 : 5;
  38. const uint32_t num_words = static_cast<uint32_t>(inst->words().size());
  39. const uint32_t composite_id_index = word_index - 1;
  40. const uint32_t num_indices = num_words - word_index;
  41. const uint32_t kCompositeExtractInsertMaxNumIndices = 255;
  42. if (num_indices == 0) {
  43. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  44. << "Expected at least one index to Op"
  45. << spvOpcodeString(inst->opcode()) << ", zero found";
  46. } else if (num_indices > kCompositeExtractInsertMaxNumIndices) {
  47. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  48. << "The number of indexes in Op" << spvOpcodeString(opcode)
  49. << " may not exceed " << kCompositeExtractInsertMaxNumIndices
  50. << ". Found " << num_indices << " indexes.";
  51. }
  52. *member_type = _.GetTypeId(inst->word(composite_id_index));
  53. if (*member_type == 0) {
  54. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  55. << "Expected Composite to be an object of composite type";
  56. }
  57. for (; word_index < num_words; ++word_index) {
  58. const uint32_t component_index = inst->word(word_index);
  59. const Instruction* const type_inst = _.FindDef(*member_type);
  60. assert(type_inst);
  61. switch (type_inst->opcode()) {
  62. case spv::Op::OpTypeVector: {
  63. *member_type = type_inst->word(2);
  64. const uint32_t vector_size = type_inst->word(3);
  65. if (component_index >= vector_size) {
  66. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  67. << "Vector access is out of bounds, vector size is "
  68. << vector_size << ", but access index is " << component_index;
  69. }
  70. break;
  71. }
  72. case spv::Op::OpTypeMatrix: {
  73. *member_type = type_inst->word(2);
  74. const uint32_t num_cols = type_inst->word(3);
  75. if (component_index >= num_cols) {
  76. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  77. << "Matrix access is out of bounds, matrix has " << num_cols
  78. << " columns, but access index is " << component_index;
  79. }
  80. break;
  81. }
  82. case spv::Op::OpTypeArray: {
  83. uint64_t array_size = 0;
  84. auto size = _.FindDef(type_inst->word(3));
  85. *member_type = type_inst->word(2);
  86. if (spvOpcodeIsSpecConstant(size->opcode())) {
  87. // Cannot verify against the size of this array.
  88. break;
  89. }
  90. if (!_.EvalConstantValUint64(type_inst->word(3), &array_size)) {
  91. assert(0 && "Array type definition is corrupt");
  92. }
  93. if (component_index >= array_size) {
  94. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  95. << "Array access is out of bounds, array size is "
  96. << array_size << ", but access index is " << component_index;
  97. }
  98. break;
  99. }
  100. case spv::Op::OpTypeRuntimeArray:
  101. case spv::Op::OpTypeNodePayloadArrayAMDX: {
  102. *member_type = type_inst->word(2);
  103. // Array size is unknown.
  104. break;
  105. }
  106. case spv::Op::OpTypeStruct: {
  107. const size_t num_struct_members = type_inst->words().size() - 2;
  108. if (component_index >= num_struct_members) {
  109. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  110. << "Index is out of bounds, can not find index "
  111. << component_index << " in the structure <id> '"
  112. << type_inst->id() << "'. This structure has "
  113. << num_struct_members << " members. Largest valid index is "
  114. << num_struct_members - 1 << ".";
  115. }
  116. *member_type = type_inst->word(component_index + 2);
  117. break;
  118. }
  119. case spv::Op::OpTypeCooperativeVectorNV:
  120. case spv::Op::OpTypeCooperativeMatrixKHR:
  121. case spv::Op::OpTypeCooperativeMatrixNV: {
  122. *member_type = type_inst->word(2);
  123. break;
  124. }
  125. default:
  126. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  127. << "Reached non-composite type while indexes still remain to "
  128. "be traversed.";
  129. }
  130. }
  131. return SPV_SUCCESS;
  132. }
  133. spv_result_t ValidateVectorExtractDynamic(ValidationState_t& _,
  134. const Instruction* inst) {
  135. const uint32_t result_type = inst->type_id();
  136. const spv::Op result_opcode = _.GetIdOpcode(result_type);
  137. if (!spvOpcodeIsScalarType(result_opcode)) {
  138. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  139. << "Expected Result Type to be a scalar type";
  140. }
  141. const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
  142. const spv::Op vector_opcode = _.GetIdOpcode(vector_type);
  143. if (vector_opcode != spv::Op::OpTypeVector &&
  144. vector_opcode != spv::Op::OpTypeCooperativeVectorNV) {
  145. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  146. << "Expected Vector type to be OpTypeVector";
  147. }
  148. if (_.GetComponentType(vector_type) != result_type) {
  149. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  150. << "Expected Vector component type to be equal to Result Type";
  151. }
  152. const auto index = _.FindDef(inst->GetOperandAs<uint32_t>(3));
  153. if (!index || index->type_id() == 0 || !_.IsIntScalarType(index->type_id())) {
  154. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  155. << "Expected Index to be int scalar";
  156. }
  157. if (_.HasCapability(spv::Capability::Shader) &&
  158. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  159. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  160. << "Cannot extract from a vector of 8- or 16-bit types";
  161. }
  162. return SPV_SUCCESS;
  163. }
  164. spv_result_t ValidateVectorInsertDyanmic(ValidationState_t& _,
  165. const Instruction* inst) {
  166. const uint32_t result_type = inst->type_id();
  167. const spv::Op result_opcode = _.GetIdOpcode(result_type);
  168. if (result_opcode != spv::Op::OpTypeVector &&
  169. result_opcode != spv::Op::OpTypeCooperativeVectorNV) {
  170. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  171. << "Expected Result Type to be OpTypeVector";
  172. }
  173. const uint32_t vector_type = _.GetOperandTypeId(inst, 2);
  174. if (vector_type != result_type) {
  175. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  176. << "Expected Vector type to be equal to Result Type";
  177. }
  178. const uint32_t component_type = _.GetOperandTypeId(inst, 3);
  179. if (_.GetComponentType(result_type) != component_type) {
  180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  181. << "Expected Component type to be equal to Result Type "
  182. << "component type";
  183. }
  184. const uint32_t index_type = _.GetOperandTypeId(inst, 4);
  185. if (!_.IsIntScalarType(index_type)) {
  186. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  187. << "Expected Index to be int scalar";
  188. }
  189. if (_.HasCapability(spv::Capability::Shader) &&
  190. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  191. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  192. << "Cannot insert into a vector of 8- or 16-bit types";
  193. }
  194. return SPV_SUCCESS;
  195. }
  196. spv_result_t ValidateCompositeConstruct(ValidationState_t& _,
  197. const Instruction* inst) {
  198. const uint32_t num_operands = static_cast<uint32_t>(inst->operands().size());
  199. const uint32_t result_type = inst->type_id();
  200. const spv::Op result_opcode = _.GetIdOpcode(result_type);
  201. switch (result_opcode) {
  202. case spv::Op::OpTypeVector:
  203. case spv::Op::OpTypeCooperativeVectorNV: {
  204. uint32_t num_result_components = _.GetDimension(result_type);
  205. const uint32_t result_component_type = _.GetComponentType(result_type);
  206. uint32_t given_component_count = 0;
  207. bool comp_is_int32 = true, comp_is_const_int32 = true;
  208. if (result_opcode == spv::Op::OpTypeVector) {
  209. if (num_operands <= 3) {
  210. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  211. << "Expected number of constituents to be at least 2";
  212. }
  213. } else {
  214. uint32_t comp_count_id =
  215. _.FindDef(result_type)->GetOperandAs<uint32_t>(2);
  216. std::tie(comp_is_int32, comp_is_const_int32, num_result_components) =
  217. _.EvalInt32IfConst(comp_count_id);
  218. }
  219. for (uint32_t operand_index = 2; operand_index < num_operands;
  220. ++operand_index) {
  221. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  222. if (operand_type == result_component_type) {
  223. ++given_component_count;
  224. } else {
  225. if (_.GetIdOpcode(operand_type) != spv::Op::OpTypeVector ||
  226. _.GetComponentType(operand_type) != result_component_type) {
  227. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  228. << "Expected Constituents to be scalars or vectors of"
  229. << " the same type as Result Type components";
  230. }
  231. given_component_count += _.GetDimension(operand_type);
  232. }
  233. }
  234. if (comp_is_const_int32 &&
  235. num_result_components != given_component_count) {
  236. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  237. << "Expected total number of given components to be equal "
  238. << "to the size of Result Type vector";
  239. }
  240. break;
  241. }
  242. case spv::Op::OpTypeMatrix: {
  243. uint32_t result_num_rows = 0;
  244. uint32_t result_num_cols = 0;
  245. uint32_t result_col_type = 0;
  246. uint32_t result_component_type = 0;
  247. if (!_.GetMatrixTypeInfo(result_type, &result_num_rows, &result_num_cols,
  248. &result_col_type, &result_component_type)) {
  249. assert(0);
  250. }
  251. if (result_num_cols + 2 != num_operands) {
  252. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  253. << "Expected total number of Constituents to be equal "
  254. << "to the number of columns of Result Type matrix";
  255. }
  256. for (uint32_t operand_index = 2; operand_index < num_operands;
  257. ++operand_index) {
  258. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  259. if (operand_type != result_col_type) {
  260. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  261. << "Expected Constituent type to be equal to the column "
  262. << "type Result Type matrix";
  263. }
  264. }
  265. break;
  266. }
  267. case spv::Op::OpTypeArray: {
  268. const Instruction* const array_inst = _.FindDef(result_type);
  269. assert(array_inst);
  270. assert(array_inst->opcode() == spv::Op::OpTypeArray);
  271. auto size = _.FindDef(array_inst->word(3));
  272. if (spvOpcodeIsSpecConstant(size->opcode())) {
  273. // Cannot verify against the size of this array.
  274. break;
  275. }
  276. uint64_t array_size = 0;
  277. if (!_.EvalConstantValUint64(array_inst->word(3), &array_size)) {
  278. assert(0 && "Array type definition is corrupt");
  279. }
  280. if (array_size + 2 != num_operands) {
  281. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  282. << "Expected total number of Constituents to be equal "
  283. << "to the number of elements of Result Type array";
  284. }
  285. const uint32_t result_component_type = array_inst->word(2);
  286. for (uint32_t operand_index = 2; operand_index < num_operands;
  287. ++operand_index) {
  288. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  289. if (operand_type != result_component_type) {
  290. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  291. << "Expected Constituent type to be equal to the column "
  292. << "type Result Type array";
  293. }
  294. }
  295. break;
  296. }
  297. case spv::Op::OpTypeStruct: {
  298. const Instruction* const struct_inst = _.FindDef(result_type);
  299. assert(struct_inst);
  300. assert(struct_inst->opcode() == spv::Op::OpTypeStruct);
  301. if (struct_inst->operands().size() + 1 != num_operands) {
  302. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  303. << "Expected total number of Constituents to be equal "
  304. << "to the number of members of Result Type struct";
  305. }
  306. for (uint32_t operand_index = 2; operand_index < num_operands;
  307. ++operand_index) {
  308. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  309. const uint32_t member_type = struct_inst->word(operand_index);
  310. if (operand_type != member_type) {
  311. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  312. << "Expected Constituent type to be equal to the "
  313. << "corresponding member type of Result Type struct";
  314. }
  315. }
  316. break;
  317. }
  318. case spv::Op::OpTypeCooperativeMatrixKHR: {
  319. const auto result_type_inst = _.FindDef(result_type);
  320. assert(result_type_inst);
  321. const auto component_type_id =
  322. result_type_inst->GetOperandAs<uint32_t>(1);
  323. if (3 != num_operands) {
  324. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  325. << "Must be only one constituent";
  326. }
  327. const uint32_t operand_type_id = _.GetOperandTypeId(inst, 2);
  328. if (operand_type_id != component_type_id) {
  329. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  330. << "Expected Constituent type to be equal to the component type";
  331. }
  332. break;
  333. }
  334. case spv::Op::OpTypeCooperativeMatrixNV: {
  335. const auto result_type_inst = _.FindDef(result_type);
  336. assert(result_type_inst);
  337. const auto component_type_id =
  338. result_type_inst->GetOperandAs<uint32_t>(1);
  339. if (3 != num_operands) {
  340. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  341. << "Expected single constituent";
  342. }
  343. const uint32_t operand_type_id = _.GetOperandTypeId(inst, 2);
  344. if (operand_type_id != component_type_id) {
  345. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  346. << "Expected Constituent type to be equal to the component type";
  347. }
  348. break;
  349. }
  350. default: {
  351. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  352. << "Expected Result Type to be a composite type";
  353. }
  354. }
  355. if (_.HasCapability(spv::Capability::Shader) &&
  356. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  357. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  358. << "Cannot create a composite containing 8- or 16-bit types";
  359. }
  360. return SPV_SUCCESS;
  361. }
  362. spv_result_t ValidateCompositeExtract(ValidationState_t& _,
  363. const Instruction* inst) {
  364. uint32_t member_type = 0;
  365. if (spv_result_t error = GetExtractInsertValueType(_, inst, &member_type)) {
  366. return error;
  367. }
  368. const uint32_t result_type = inst->type_id();
  369. if (result_type != member_type) {
  370. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  371. << "Result type (Op" << spvOpcodeString(_.GetIdOpcode(result_type))
  372. << ") does not match the type that results from indexing into "
  373. "the composite (Op"
  374. << spvOpcodeString(_.GetIdOpcode(member_type)) << ").";
  375. }
  376. if (_.HasCapability(spv::Capability::Shader) &&
  377. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  378. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  379. << "Cannot extract from a composite of 8- or 16-bit types";
  380. }
  381. return SPV_SUCCESS;
  382. }
  383. spv_result_t ValidateCompositeInsert(ValidationState_t& _,
  384. const Instruction* inst) {
  385. const uint32_t object_type = _.GetOperandTypeId(inst, 2);
  386. const uint32_t composite_type = _.GetOperandTypeId(inst, 3);
  387. const uint32_t result_type = inst->type_id();
  388. if (result_type != composite_type) {
  389. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  390. << "The Result Type must be the same as Composite type in Op"
  391. << spvOpcodeString(inst->opcode()) << " yielding Result Id "
  392. << result_type << ".";
  393. }
  394. uint32_t member_type = 0;
  395. if (spv_result_t error = GetExtractInsertValueType(_, inst, &member_type)) {
  396. return error;
  397. }
  398. if (object_type != member_type) {
  399. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  400. << "The Object type (Op"
  401. << spvOpcodeString(_.GetIdOpcode(object_type))
  402. << ") does not match the type that results from indexing into the "
  403. "Composite (Op"
  404. << spvOpcodeString(_.GetIdOpcode(member_type)) << ").";
  405. }
  406. if (_.HasCapability(spv::Capability::Shader) &&
  407. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  408. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  409. << "Cannot insert into a composite of 8- or 16-bit types";
  410. }
  411. return SPV_SUCCESS;
  412. }
  413. spv_result_t ValidateCopyObject(ValidationState_t& _, const Instruction* inst) {
  414. const uint32_t result_type = inst->type_id();
  415. const uint32_t operand_type = _.GetOperandTypeId(inst, 2);
  416. if (operand_type != result_type) {
  417. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  418. << "Expected Result Type and Operand type to be the same";
  419. }
  420. if (_.IsVoidType(result_type)) {
  421. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  422. << "OpCopyObject cannot have void result type";
  423. }
  424. return SPV_SUCCESS;
  425. }
  426. spv_result_t ValidateTranspose(ValidationState_t& _, const Instruction* inst) {
  427. uint32_t result_num_rows = 0;
  428. uint32_t result_num_cols = 0;
  429. uint32_t result_col_type = 0;
  430. uint32_t result_component_type = 0;
  431. const uint32_t result_type = inst->type_id();
  432. if (!_.GetMatrixTypeInfo(result_type, &result_num_rows, &result_num_cols,
  433. &result_col_type, &result_component_type)) {
  434. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  435. << "Expected Result Type to be a matrix type";
  436. }
  437. const uint32_t matrix_type = _.GetOperandTypeId(inst, 2);
  438. uint32_t matrix_num_rows = 0;
  439. uint32_t matrix_num_cols = 0;
  440. uint32_t matrix_col_type = 0;
  441. uint32_t matrix_component_type = 0;
  442. if (!_.GetMatrixTypeInfo(matrix_type, &matrix_num_rows, &matrix_num_cols,
  443. &matrix_col_type, &matrix_component_type)) {
  444. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  445. << "Expected Matrix to be of type OpTypeMatrix";
  446. }
  447. if (result_component_type != matrix_component_type) {
  448. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  449. << "Expected component types of Matrix and Result Type to be "
  450. << "identical";
  451. }
  452. if (result_num_rows != matrix_num_cols ||
  453. result_num_cols != matrix_num_rows) {
  454. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  455. << "Expected number of columns and the column size of Matrix "
  456. << "to be the reverse of those of Result Type";
  457. }
  458. if (_.HasCapability(spv::Capability::Shader) &&
  459. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  460. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  461. << "Cannot transpose matrices of 16-bit floats";
  462. }
  463. return SPV_SUCCESS;
  464. }
  465. spv_result_t ValidateVectorShuffle(ValidationState_t& _,
  466. const Instruction* inst) {
  467. auto resultType = _.FindDef(inst->type_id());
  468. if (!resultType || resultType->opcode() != spv::Op::OpTypeVector) {
  469. return _.diag(SPV_ERROR_INVALID_ID, inst)
  470. << "The Result Type of OpVectorShuffle must be"
  471. << " OpTypeVector. Found Op"
  472. << spvOpcodeString(static_cast<spv::Op>(resultType->opcode()))
  473. << ".";
  474. }
  475. // The number of components in Result Type must be the same as the number of
  476. // Component operands.
  477. auto componentCount = inst->operands().size() - 4;
  478. auto resultVectorDimension = resultType->GetOperandAs<uint32_t>(2);
  479. if (componentCount != resultVectorDimension) {
  480. return _.diag(SPV_ERROR_INVALID_ID, inst)
  481. << "OpVectorShuffle component literals count does not match "
  482. "Result Type <id> "
  483. << _.getIdName(resultType->id()) << "s vector component count.";
  484. }
  485. // Vector 1 and Vector 2 must both have vector types, with the same Component
  486. // Type as Result Type.
  487. auto vector1Object = _.FindDef(inst->GetOperandAs<uint32_t>(2));
  488. auto vector1Type = _.FindDef(vector1Object->type_id());
  489. auto vector2Object = _.FindDef(inst->GetOperandAs<uint32_t>(3));
  490. auto vector2Type = _.FindDef(vector2Object->type_id());
  491. if (!vector1Type || vector1Type->opcode() != spv::Op::OpTypeVector) {
  492. return _.diag(SPV_ERROR_INVALID_ID, inst)
  493. << "The type of Vector 1 must be OpTypeVector.";
  494. }
  495. if (!vector2Type || vector2Type->opcode() != spv::Op::OpTypeVector) {
  496. return _.diag(SPV_ERROR_INVALID_ID, inst)
  497. << "The type of Vector 2 must be OpTypeVector.";
  498. }
  499. auto resultComponentType = resultType->GetOperandAs<uint32_t>(1);
  500. if (vector1Type->GetOperandAs<uint32_t>(1) != resultComponentType) {
  501. return _.diag(SPV_ERROR_INVALID_ID, inst)
  502. << "The Component Type of Vector 1 must be the same as ResultType.";
  503. }
  504. if (vector2Type->GetOperandAs<uint32_t>(1) != resultComponentType) {
  505. return _.diag(SPV_ERROR_INVALID_ID, inst)
  506. << "The Component Type of Vector 2 must be the same as ResultType.";
  507. }
  508. // All Component literals must either be FFFFFFFF or in [0, N - 1].
  509. auto vector1ComponentCount = vector1Type->GetOperandAs<uint32_t>(2);
  510. auto vector2ComponentCount = vector2Type->GetOperandAs<uint32_t>(2);
  511. auto N = vector1ComponentCount + vector2ComponentCount;
  512. auto firstLiteralIndex = 4;
  513. for (size_t i = firstLiteralIndex; i < inst->operands().size(); ++i) {
  514. auto literal = inst->GetOperandAs<uint32_t>(i);
  515. if (literal != 0xFFFFFFFF && literal >= N) {
  516. return _.diag(SPV_ERROR_INVALID_ID, inst)
  517. << "Component index " << literal << " is out of bounds for "
  518. << "combined (Vector1 + Vector2) size of " << N << ".";
  519. }
  520. }
  521. if (_.HasCapability(spv::Capability::Shader) &&
  522. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  523. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  524. << "Cannot shuffle a vector of 8- or 16-bit types";
  525. }
  526. return SPV_SUCCESS;
  527. }
  528. spv_result_t ValidateCopyLogical(ValidationState_t& _,
  529. const Instruction* inst) {
  530. const auto result_type = _.FindDef(inst->type_id());
  531. const auto source = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
  532. const auto source_type = _.FindDef(source->type_id());
  533. if (!source_type || !result_type || source_type == result_type) {
  534. return _.diag(SPV_ERROR_INVALID_ID, inst)
  535. << "Result Type must not equal the Operand type";
  536. }
  537. if (!_.LogicallyMatch(source_type, result_type, false)) {
  538. return _.diag(SPV_ERROR_INVALID_ID, inst)
  539. << "Result Type does not logically match the Operand type";
  540. }
  541. if (_.HasCapability(spv::Capability::Shader) &&
  542. _.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
  543. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  544. << "Cannot copy composites of 8- or 16-bit types";
  545. }
  546. return SPV_SUCCESS;
  547. }
  548. } // anonymous namespace
  549. // Validates correctness of composite instructions.
  550. spv_result_t CompositesPass(ValidationState_t& _, const Instruction* inst) {
  551. switch (inst->opcode()) {
  552. case spv::Op::OpVectorExtractDynamic:
  553. return ValidateVectorExtractDynamic(_, inst);
  554. case spv::Op::OpVectorInsertDynamic:
  555. return ValidateVectorInsertDyanmic(_, inst);
  556. case spv::Op::OpVectorShuffle:
  557. return ValidateVectorShuffle(_, inst);
  558. case spv::Op::OpCompositeConstruct:
  559. return ValidateCompositeConstruct(_, inst);
  560. case spv::Op::OpCompositeExtract:
  561. return ValidateCompositeExtract(_, inst);
  562. case spv::Op::OpCompositeInsert:
  563. return ValidateCompositeInsert(_, inst);
  564. case spv::Op::OpCopyObject:
  565. return ValidateCopyObject(_, inst);
  566. case spv::Op::OpTranspose:
  567. return ValidateTranspose(_, inst);
  568. case spv::Op::OpCopyLogical:
  569. return ValidateCopyLogical(_, inst);
  570. default:
  571. break;
  572. }
  573. return SPV_SUCCESS;
  574. }
  575. } // namespace val
  576. } // namespace spvtools