validate_composites.cpp 23 KB

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