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