validate_composites.cpp 23 KB

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