File: validate_conversion.cpp

package info (click to toggle)
spirv-tools 2025.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,588 kB
  • sloc: cpp: 470,407; javascript: 5,893; python: 3,326; ansic: 488; sh: 450; ruby: 88; makefile: 18; lisp: 9
file content (771 lines) | stat: -rw-r--r-- 33,876 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Validates correctness of conversion instructions.

#include <climits>

#include "source/opcode.h"
#include "source/spirv_constant.h"
#include "source/spirv_target_env.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validation_state.h"

namespace spvtools {
namespace val {

// Validates correctness of conversion instructions.
spv_result_t ConversionPass(ValidationState_t& _, const Instruction* inst) {
  const spv::Op opcode = inst->opcode();
  const uint32_t result_type = inst->type_id();

  switch (opcode) {
    case spv::Op::OpConvertFToU: {
      if (!_.IsUnsignedIntScalarType(result_type) &&
          !_.IsUnsignedIntVectorType(result_type) &&
          !_.IsUnsignedIntCooperativeMatrixType(result_type) &&
          !_.IsUnsignedIntCooperativeVectorNVType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected unsigned int scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type || (!_.IsFloatScalarType(input_type) &&
                          !_.IsFloatVectorType(input_type) &&
                          !_.IsFloatCooperativeMatrixType(input_type) &&
                          !_.IsFloatCooperativeVectorNVType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be float scalar or vector: "
               << spvOpcodeString(opcode);

      if (_.IsCooperativeVectorNVType(result_type) ||
          _.IsCooperativeVectorNVType(input_type)) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      } else if (_.IsCooperativeMatrixType(result_type) ||
                 _.IsCooperativeMatrixType(input_type)) {
        spv_result_t ret =
            _.CooperativeMatrixShapesMatch(inst, result_type, input_type, true);
        if (ret != SPV_SUCCESS) return ret;
      } else {
        if (_.GetDimension(result_type) != _.GetDimension(input_type))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same dimension as Result Type: "
                 << spvOpcodeString(opcode);
      }

      break;
    }

    case spv::Op::OpConvertFToS: {
      if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
          !_.IsIntCooperativeMatrixType(result_type) &&
          !_.IsIntCooperativeVectorNVType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected int scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type || (!_.IsFloatScalarType(input_type) &&
                          !_.IsFloatVectorType(input_type) &&
                          !_.IsFloatCooperativeMatrixType(input_type) &&
                          !_.IsFloatCooperativeVectorNVType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be float scalar or vector: "
               << spvOpcodeString(opcode);

      if (_.IsCooperativeVectorNVType(result_type) ||
          _.IsCooperativeVectorNVType(input_type)) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      } else if (_.IsCooperativeMatrixType(result_type) ||
                 _.IsCooperativeMatrixType(input_type)) {
        spv_result_t ret =
            _.CooperativeMatrixShapesMatch(inst, result_type, input_type, true);
        if (ret != SPV_SUCCESS) return ret;
      } else {
        if (_.GetDimension(result_type) != _.GetDimension(input_type))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same dimension as Result Type: "
                 << spvOpcodeString(opcode);
      }

      break;
    }

    case spv::Op::OpConvertSToF:
    case spv::Op::OpConvertUToF: {
      if (!_.IsFloatScalarType(result_type) &&
          !_.IsFloatVectorType(result_type) &&
          !_.IsFloatCooperativeMatrixType(result_type) &&
          !_.IsFloatCooperativeVectorNVType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected float scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type ||
          (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type) &&
           !_.IsIntCooperativeMatrixType(input_type) &&
           !_.IsIntCooperativeVectorNVType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be int scalar or vector: "
               << spvOpcodeString(opcode);

      if (_.IsCooperativeVectorNVType(result_type) ||
          _.IsCooperativeVectorNVType(input_type)) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      } else if (_.IsCooperativeMatrixType(result_type) ||
                 _.IsCooperativeMatrixType(input_type)) {
        spv_result_t ret =
            _.CooperativeMatrixShapesMatch(inst, result_type, input_type, true);
        if (ret != SPV_SUCCESS) return ret;
      } else {
        if (_.GetDimension(result_type) != _.GetDimension(input_type))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same dimension as Result Type: "
                 << spvOpcodeString(opcode);
      }

      break;
    }

    case spv::Op::OpUConvert: {
      if (!_.IsUnsignedIntScalarType(result_type) &&
          !_.IsUnsignedIntVectorType(result_type) &&
          !_.IsUnsignedIntCooperativeMatrixType(result_type) &&
          !_.IsUnsignedIntCooperativeVectorNVType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected unsigned int scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type ||
          (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type) &&
           !_.IsIntCooperativeMatrixType(input_type) &&
           !_.IsIntCooperativeVectorNVType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be int scalar or vector: "
               << spvOpcodeString(opcode);

      if (_.IsCooperativeVectorNVType(result_type) ||
          _.IsCooperativeVectorNVType(input_type)) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      } else if (_.IsCooperativeMatrixType(result_type) ||
                 _.IsCooperativeMatrixType(input_type)) {
        spv_result_t ret =
            _.CooperativeMatrixShapesMatch(inst, result_type, input_type, true);
        if (ret != SPV_SUCCESS) return ret;
      } else {
        if (_.GetDimension(result_type) != _.GetDimension(input_type))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same dimension as Result Type: "
                 << spvOpcodeString(opcode);
      }

      if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have different bit width from Result "
                  "Type: "
               << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpSConvert: {
      if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
          !_.IsIntCooperativeMatrixType(result_type) &&
          !_.IsIntCooperativeVectorNVType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected int scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type ||
          (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type) &&
           !_.IsIntCooperativeMatrixType(input_type) &&
           !_.IsIntCooperativeVectorNVType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be int scalar or vector: "
               << spvOpcodeString(opcode);

      if (_.IsCooperativeVectorNVType(result_type) ||
          _.IsCooperativeVectorNVType(input_type)) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      } else if (_.IsCooperativeMatrixType(result_type) ||
                 _.IsCooperativeMatrixType(input_type)) {
        spv_result_t ret =
            _.CooperativeMatrixShapesMatch(inst, result_type, input_type, true);
        if (ret != SPV_SUCCESS) return ret;
      } else {
        if (_.GetDimension(result_type) != _.GetDimension(input_type))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same dimension as Result Type: "
                 << spvOpcodeString(opcode);
      }

      if (_.GetBitWidth(result_type) == _.GetBitWidth(input_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have different bit width from Result "
                  "Type: "
               << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpFConvert: {
      if (!_.IsFloatScalarType(result_type) &&
          !_.IsFloatVectorType(result_type) &&
          !_.IsFloatCooperativeMatrixType(result_type) &&
          !_.IsFloatCooperativeVectorNVType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected float scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type || (!_.IsFloatScalarType(input_type) &&
                          !_.IsFloatVectorType(input_type) &&
                          !_.IsFloatCooperativeMatrixType(input_type) &&
                          !_.IsFloatCooperativeVectorNVType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be float scalar or vector: "
               << spvOpcodeString(opcode);

      if (_.IsCooperativeVectorNVType(result_type) ||
          _.IsCooperativeVectorNVType(input_type)) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      } else if (_.IsCooperativeMatrixType(result_type) ||
                 _.IsCooperativeMatrixType(input_type)) {
        spv_result_t ret =
            _.CooperativeMatrixShapesMatch(inst, result_type, input_type, true);
        if (ret != SPV_SUCCESS) return ret;
      } else {
        if (_.GetDimension(result_type) != _.GetDimension(input_type))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same dimension as Result Type: "
                 << spvOpcodeString(opcode);
      }

      // Scalar type
      const uint32_t resScalarType = _.GetComponentType(result_type);
      const uint32_t inputScalartype = _.GetComponentType(input_type);
      if (resScalarType == inputScalartype) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected component type of Value to be different from "
                  "component type of Result Type: "
               << spvOpcodeString(opcode);
      }
      break;
    }

    case spv::Op::OpQuantizeToF16: {
      if ((!_.IsFloatScalarType(result_type) &&
           !_.IsFloatVectorType(result_type)) ||
          _.GetBitWidth(result_type) != 32)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected 32-bit float scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (input_type != result_type)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input type to be equal to Result Type: "
               << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpConvertPtrToU: {
      if (!_.IsUnsignedIntScalarType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected unsigned int scalar type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!_.IsPointerType(input_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be a pointer: " << spvOpcodeString(opcode);

      if (_.addressing_model() == spv::AddressingModel::Logical)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Logical addressing not supported: "
               << spvOpcodeString(opcode);

      if (_.addressing_model() ==
          spv::AddressingModel::PhysicalStorageBuffer64) {
        spv::StorageClass input_storage_class;
        uint32_t input_data_type = 0;
        _.GetPointerTypeInfo(input_type, &input_data_type,
                             &input_storage_class);
        if (input_storage_class != spv::StorageClass::PhysicalStorageBuffer)
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Pointer storage class must be PhysicalStorageBuffer: "
                 << spvOpcodeString(opcode);

        if (spvIsVulkanEnv(_.context()->target_env)) {
          if (_.GetBitWidth(result_type) != 64) {
            return _.diag(SPV_ERROR_INVALID_DATA, inst)
                   << _.VkErrorID(4710)
                   << "PhysicalStorageBuffer64 addressing mode requires the "
                      "result integer type to have a 64-bit width for Vulkan "
                      "environment.";
          }
        }
      }
      break;
    }

    case spv::Op::OpSatConvertSToU:
    case spv::Op::OpSatConvertUToS: {
      if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected int scalar or vector type as Result Type: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type ||
          (!_.IsIntScalarType(input_type) && !_.IsIntVectorType(input_type)))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected int scalar or vector as input: "
               << spvOpcodeString(opcode);

      if (_.GetDimension(result_type) != _.GetDimension(input_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have the same dimension as Result Type: "
               << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpConvertUToPtr: {
      if (!_.IsPointerType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be a pointer: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type || !_.IsIntScalarType(input_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected int scalar as input: " << spvOpcodeString(opcode);

      if (_.addressing_model() == spv::AddressingModel::Logical)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Logical addressing not supported: "
               << spvOpcodeString(opcode);

      if (_.addressing_model() ==
          spv::AddressingModel::PhysicalStorageBuffer64) {
        spv::StorageClass result_storage_class;
        uint32_t result_data_type = 0;
        _.GetPointerTypeInfo(result_type, &result_data_type,
                             &result_storage_class);
        if (result_storage_class != spv::StorageClass::PhysicalStorageBuffer)
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Pointer storage class must be PhysicalStorageBuffer: "
                 << spvOpcodeString(opcode);

        if (spvIsVulkanEnv(_.context()->target_env)) {
          if (_.GetBitWidth(input_type) != 64) {
            return _.diag(SPV_ERROR_INVALID_DATA, inst)
                   << _.VkErrorID(4710)
                   << "PhysicalStorageBuffer64 addressing mode requires the "
                      "input integer to have a 64-bit width for Vulkan "
                      "environment.";
          }
        }
      }
      break;
    }

    case spv::Op::OpPtrCastToGeneric: {
      spv::StorageClass result_storage_class;
      uint32_t result_data_type = 0;
      if (!_.GetPointerTypeInfo(result_type, &result_data_type,
                                &result_storage_class))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be a pointer: "
               << spvOpcodeString(opcode);

      if (result_storage_class != spv::StorageClass::Generic)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to have storage class Generic: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      spv::StorageClass input_storage_class;
      uint32_t input_data_type = 0;
      if (!_.GetPointerTypeInfo(input_type, &input_data_type,
                                &input_storage_class))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be a pointer: " << spvOpcodeString(opcode);

      if (input_storage_class != spv::StorageClass::Workgroup &&
          input_storage_class != spv::StorageClass::CrossWorkgroup &&
          input_storage_class != spv::StorageClass::Function)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have storage class Workgroup, "
               << "CrossWorkgroup or Function: " << spvOpcodeString(opcode);

      if (result_data_type != input_data_type)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input and Result Type to point to the same type: "
               << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpGenericCastToPtr: {
      spv::StorageClass result_storage_class;
      uint32_t result_data_type = 0;
      if (!_.GetPointerTypeInfo(result_type, &result_data_type,
                                &result_storage_class))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be a pointer: "
               << spvOpcodeString(opcode);

      if (result_storage_class != spv::StorageClass::Workgroup &&
          result_storage_class != spv::StorageClass::CrossWorkgroup &&
          result_storage_class != spv::StorageClass::Function)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to have storage class Workgroup, "
               << "CrossWorkgroup or Function: " << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      spv::StorageClass input_storage_class;
      uint32_t input_data_type = 0;
      if (!_.GetPointerTypeInfo(input_type, &input_data_type,
                                &input_storage_class))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be a pointer: " << spvOpcodeString(opcode);

      if (input_storage_class != spv::StorageClass::Generic)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have storage class Generic: "
               << spvOpcodeString(opcode);

      if (result_data_type != input_data_type)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input and Result Type to point to the same type: "
               << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpGenericCastToPtrExplicit: {
      spv::StorageClass result_storage_class;
      uint32_t result_data_type = 0;
      if (!_.GetPointerTypeInfo(result_type, &result_data_type,
                                &result_storage_class))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be a pointer: "
               << spvOpcodeString(opcode);

      const auto target_storage_class =
          inst->GetOperandAs<spv::StorageClass>(3);
      if (result_storage_class != target_storage_class)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be of target storage class: "
               << spvOpcodeString(opcode);

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      spv::StorageClass input_storage_class;
      uint32_t input_data_type = 0;
      if (!_.GetPointerTypeInfo(input_type, &input_data_type,
                                &input_storage_class))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be a pointer: " << spvOpcodeString(opcode);

      if (input_storage_class != spv::StorageClass::Generic)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have storage class Generic: "
               << spvOpcodeString(opcode);

      if (result_data_type != input_data_type)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input and Result Type to point to the same type: "
               << spvOpcodeString(opcode);

      if (target_storage_class != spv::StorageClass::Workgroup &&
          target_storage_class != spv::StorageClass::CrossWorkgroup &&
          target_storage_class != spv::StorageClass::Function)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected target storage class to be Workgroup, "
               << "CrossWorkgroup or Function: " << spvOpcodeString(opcode);
      break;
    }

    case spv::Op::OpBitcast: {
      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to have a type: " << spvOpcodeString(opcode);

      const bool result_is_pointer = _.IsPointerType(result_type);
      const bool result_is_int_scalar = _.IsIntScalarType(result_type);
      const bool input_is_pointer = _.IsPointerType(input_type);
      const bool input_is_int_scalar = _.IsIntScalarType(input_type);

      const bool result_is_coopmat = _.IsCooperativeMatrixType(result_type);
      const bool input_is_coopmat = _.IsCooperativeMatrixType(input_type);
      const bool result_is_coopvec = _.IsCooperativeVectorNVType(result_type);
      const bool input_is_coopvec = _.IsCooperativeVectorNVType(input_type);

      if (!result_is_pointer && !result_is_int_scalar && !result_is_coopmat &&
          !result_is_coopvec && !_.IsIntVectorType(result_type) &&
          !_.IsFloatScalarType(result_type) &&
          !_.IsFloatVectorType(result_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be a pointer or int or float vector "
               << "or scalar type: " << spvOpcodeString(opcode);

      if (!input_is_pointer && !input_is_int_scalar && !input_is_coopmat &&
          !input_is_coopvec && !_.IsIntVectorType(input_type) &&
          !_.IsFloatScalarType(input_type) && !_.IsFloatVectorType(input_type))
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected input to be a pointer or int or float vector "
               << "or scalar: " << spvOpcodeString(opcode);

      if (result_is_coopvec != input_is_coopvec)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Cooperative vector can only be cast to another cooperative "
               << "vector: " << spvOpcodeString(opcode);

      if (result_is_coopmat != input_is_coopmat)
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Cooperative matrix can only be cast to another cooperative "
               << "matrix: " << spvOpcodeString(opcode);

      if (result_is_coopvec) {
        spv_result_t ret =
            _.CooperativeVectorDimensionsMatch(inst, result_type, input_type);
        if (ret != SPV_SUCCESS) return ret;
      }

      if (result_is_coopmat) {
        spv_result_t ret = _.CooperativeMatrixShapesMatch(inst, result_type,
                                                          input_type, false);
        if (ret != SPV_SUCCESS) return ret;
      }

      if (_.version() >= SPV_SPIRV_VERSION_WORD(1, 5) ||
          _.HasExtension(kSPV_KHR_physical_storage_buffer)) {
        const bool result_is_int_vector = _.IsIntVectorType(result_type);
        const bool result_has_int32 =
            _.ContainsSizedIntOrFloatType(result_type, spv::Op::OpTypeInt, 32);
        const bool input_is_int_vector = _.IsIntVectorType(input_type);
        const bool input_has_int32 =
            _.ContainsSizedIntOrFloatType(input_type, spv::Op::OpTypeInt, 32);
        if (result_is_pointer && !input_is_pointer && !input_is_int_scalar &&
            !(input_is_int_vector && input_has_int32))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "In SPIR-V 1.5 or later (or with "
                    "SPV_KHR_physical_storage_buffer), expected input to be a "
                    "pointer, "
                    "int scalar or 32-bit int "
                    "vector if Result Type is pointer: "
                 << spvOpcodeString(opcode);

        if (input_is_pointer && !result_is_pointer && !result_is_int_scalar &&
            !(result_is_int_vector && result_has_int32))
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "In SPIR-V 1.5 or later (or with "
                    "SPV_KHR_physical_storage_buffer), pointer can only be "
                    "converted to "
                    "another pointer, int "
                    "scalar or 32-bit int vector: "
                 << spvOpcodeString(opcode);
      } else {
        if (result_is_pointer && !input_is_pointer && !input_is_int_scalar)
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "In SPIR-V 1.4 or earlier (and without "
                    "SPV_KHR_physical_storage_buffer), expected input to be a "
                    "pointer "
                    "or int scalar if Result "
                    "Type is pointer: "
                 << spvOpcodeString(opcode);

        if (input_is_pointer && !result_is_pointer && !result_is_int_scalar)
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "In SPIR-V 1.4 or earlier (and without "
                    "SPV_KHR_physical_storage_buffer), pointer can only be "
                    "converted "
                    "to another pointer or int "
                    "scalar: "
                 << spvOpcodeString(opcode);
      }

      if (!result_is_pointer && !input_is_pointer) {
        const uint32_t result_size =
            _.GetBitWidth(result_type) * _.GetDimension(result_type);
        const uint32_t input_size =
            _.GetBitWidth(input_type) * _.GetDimension(input_type);
        if (result_size != input_size)
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Expected input to have the same total bit width as "
                 << "Result Type: " << spvOpcodeString(opcode);
      }
      break;
    }

    case spv::Op::OpConvertUToAccelerationStructureKHR: {
      if (!_.IsAccelerationStructureType(result_type)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected Result Type to be a Acceleration Structure: "
               << spvOpcodeString(opcode);
      }

      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!input_type || !_.IsUnsigned64BitHandle(input_type)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected 64-bit uint scalar or 2-component 32-bit uint "
                  "vector as input: "
               << spvOpcodeString(opcode);
      }

      break;
    }

    case spv::Op::OpCooperativeMatrixConvertNV:
    case spv::Op::OpCooperativeMatrixTransposeNV: {
      if (!_.IsCooperativeMatrixType(result_type)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected cooperative matrix Result Type: "
               << spvOpcodeString(opcode);
      }
      const uint32_t input_type = _.GetOperandTypeId(inst, 2);
      if (!_.IsCooperativeMatrixType(input_type)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Expected cooperative matrix type for Matrix input: "
               << spvOpcodeString(opcode);
      }

      bool swap_row_col = (opcode == spv::Op::OpCooperativeMatrixTransposeNV);
      if (auto error = _.CooperativeMatrixShapesMatch(
              inst, result_type, input_type, true, swap_row_col))
        return error;

      if (opcode == spv::Op::OpCooperativeMatrixConvertNV) {
        if (_.FindDef(result_type)->GetOperandAs<uint32_t>(1) !=
            _.FindDef(input_type)->GetOperandAs<uint32_t>(1)) {
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Result Type and Matrix component types mismatch: "
                 << spvOpcodeString(opcode);
        }
      }

      if (opcode == spv::Op::OpCooperativeMatrixTransposeNV) {
        if (!_.IsCooperativeMatrixBType(result_type)) {
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "Result Type must have UseB: " << spvOpcodeString(opcode);
        }
      }
      break;
    }

    case spv::Op::OpBitCastArrayQCOM: {
      const auto result_type_inst = _.FindDef(inst->type_id());
      const auto source = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
      const auto source_type_inst = _.FindDef(source->type_id());

      // Are the input and the result arrays?
      if (result_type_inst->opcode() != spv::Op::OpTypeArray ||
          source_type_inst->opcode() != spv::Op::OpTypeArray) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Opcode " << spvOpcodeString(inst->opcode())
               << " requires OpTypeArray operands for the input and the "
                  "result.";
      }

      const auto source_elt_type = _.GetComponentType(source_type_inst->id());
      const auto result_elt_type = _.GetComponentType(result_type_inst->id());

      if (!_.IsIntNOrFP32OrFP16<32>(source_elt_type)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Opcode " << spvOpcodeString(inst->opcode())
               << " requires the source element type be one of 32-bit "
                  "OpTypeInt "
                  "(signed/unsigned), 32-bit OpTypeFloat and 16-bit "
                  "OpTypeFloat";
      }

      if (!_.IsIntNOrFP32OrFP16<32>(result_elt_type)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Opcode " << spvOpcodeString(inst->opcode())
               << " requires the result element type be one of 32-bit "
                  "OpTypeInt "
                  "(signed/unsigned), 32-bit OpTypeFloat and 16-bit "
                  "OpTypeFloat";
      }

      unsigned src_arr_len_id = source_type_inst->GetOperandAs<unsigned>(2u);
      unsigned res_arr_len_id = result_type_inst->GetOperandAs<unsigned>(2u);

      // Are the input and result element types compatible?
      unsigned src_arr_len = UINT_MAX, res_arr_len = UINT_MAX;
      bool src_arr_len_status =
          _.GetConstantValueAs<unsigned>(src_arr_len_id, src_arr_len);
      bool res_arr_len_status =
          _.GetConstantValueAs<unsigned>(res_arr_len_id, res_arr_len);

      bool is_src_arr_len_spec_const =
          spvOpcodeIsSpecConstant(_.FindDef(src_arr_len_id)->opcode());
      bool is_res_arr_len_spec_const =
          spvOpcodeIsSpecConstant(_.FindDef(res_arr_len_id)->opcode());

      unsigned source_bitlen = _.GetBitWidth(source_elt_type) * src_arr_len;
      unsigned result_bitlen = _.GetBitWidth(result_elt_type) * res_arr_len;
      if (!is_src_arr_len_spec_const && !is_res_arr_len_spec_const &&
          (!src_arr_len_status || !res_arr_len_status ||
           source_bitlen != result_bitlen)) {
        return _.diag(SPV_ERROR_INVALID_DATA, inst)
               << "Opcode " << spvOpcodeString(inst->opcode())
               << " requires source and result types be compatible for "
                  "conversion.";
      }
      break;
    }

    default:
      break;
  }

  if (_.HasCapability(spv::Capability::Shader)) {
    switch (inst->opcode()) {
      case spv::Op::OpConvertFToU:
      case spv::Op::OpConvertFToS:
      case spv::Op::OpConvertSToF:
      case spv::Op::OpConvertUToF:
      case spv::Op::OpBitcast:
        if (_.ContainsLimitedUseIntOrFloatType(inst->type_id()) ||
            _.ContainsLimitedUseIntOrFloatType(_.GetOperandTypeId(inst, 2u))) {
          return _.diag(SPV_ERROR_INVALID_DATA, inst)
                 << "8- or 16-bit types can only be used with width-only "
                    "conversions";
        }
        break;
      default:
        break;
    }
  }

  return SPV_SUCCESS;
}

}  // namespace val
}  // namespace spvtools