-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathre-spirv.cpp
More file actions
3397 lines (3024 loc) · 151 KB
/
re-spirv.cpp
File metadata and controls
3397 lines (3024 loc) · 151 KB
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// re-spirv
//
// Copyright (c) 2024 renderbag and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file for details.
//
#include "re-spirv.h"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <unordered_map>
#define SPV_ENABLE_UTILITY_CODE
#include "spirv/unified1/spirv.h"
// Enables more extensive output on errors.
#define RESPV_VERBOSE_ERRORS 0
namespace respv {
// Common.
static bool SpvIsSupported(SpvOp pOpCode) {
switch (pOpCode) {
case SpvOpUndef:
case SpvOpSource:
case SpvOpName:
case SpvOpMemberName:
case SpvOpExtension:
case SpvOpExtInstImport:
case SpvOpExtInst:
case SpvOpMemoryModel:
case SpvOpEntryPoint:
case SpvOpExecutionMode:
case SpvOpCapability:
case SpvOpTypeVoid:
case SpvOpTypeBool:
case SpvOpTypeInt:
case SpvOpTypeFloat:
case SpvOpTypeVector:
case SpvOpTypeMatrix:
case SpvOpTypeImage:
case SpvOpTypeSampler:
case SpvOpTypeSampledImage:
case SpvOpTypeArray:
case SpvOpTypeRuntimeArray:
case SpvOpTypeStruct:
case SpvOpTypePointer:
case SpvOpTypeFunction:
case SpvOpConstantTrue:
case SpvOpConstantFalse:
case SpvOpConstant:
case SpvOpConstantComposite:
case SpvOpConstantNull:
case SpvOpSpecConstantTrue:
case SpvOpSpecConstantFalse:
case SpvOpSpecConstant:
case SpvOpSpecConstantOp:
case SpvOpFunction:
case SpvOpFunctionParameter:
case SpvOpFunctionEnd:
case SpvOpFunctionCall:
case SpvOpVariable:
case SpvOpImageTexelPointer:
case SpvOpLoad:
case SpvOpStore:
case SpvOpAccessChain:
case SpvOpDecorate:
case SpvOpMemberDecorate:
case SpvOpVectorShuffle:
case SpvOpCompositeConstruct:
case SpvOpCompositeExtract:
case SpvOpCompositeInsert:
case SpvOpCopyObject:
case SpvOpTranspose:
case SpvOpSampledImage:
case SpvOpImageSampleImplicitLod:
case SpvOpImageSampleExplicitLod:
case SpvOpImageSampleDrefImplicitLod:
case SpvOpImageSampleDrefExplicitLod:
case SpvOpImageSampleProjImplicitLod:
case SpvOpImageSampleProjExplicitLod:
case SpvOpImageSampleProjDrefImplicitLod:
case SpvOpImageSampleProjDrefExplicitLod:
case SpvOpImageFetch:
case SpvOpImageGather:
case SpvOpImageDrefGather:
case SpvOpImageRead:
case SpvOpImageWrite:
case SpvOpImage:
case SpvOpImageQueryFormat:
case SpvOpImageQueryOrder:
case SpvOpImageQuerySizeLod:
case SpvOpImageQuerySize:
case SpvOpImageQueryLod:
case SpvOpImageQueryLevels:
case SpvOpImageQuerySamples:
case SpvOpConvertFToU:
case SpvOpConvertFToS:
case SpvOpConvertSToF:
case SpvOpConvertUToF:
case SpvOpUConvert:
case SpvOpSConvert:
case SpvOpFConvert:
case SpvOpBitcast:
case SpvOpSNegate:
case SpvOpFNegate:
case SpvOpIAdd:
case SpvOpFAdd:
case SpvOpISub:
case SpvOpFSub:
case SpvOpIMul:
case SpvOpFMul:
case SpvOpUDiv:
case SpvOpSDiv:
case SpvOpFDiv:
case SpvOpUMod:
case SpvOpSRem:
case SpvOpSMod:
case SpvOpFRem:
case SpvOpFMod:
case SpvOpVectorTimesScalar:
case SpvOpMatrixTimesScalar:
case SpvOpVectorTimesMatrix:
case SpvOpMatrixTimesVector:
case SpvOpMatrixTimesMatrix:
case SpvOpOuterProduct:
case SpvOpDot:
case SpvOpIAddCarry:
case SpvOpISubBorrow:
case SpvOpUMulExtended:
case SpvOpSMulExtended:
case SpvOpAny:
case SpvOpAll:
case SpvOpIsNan:
case SpvOpIsInf:
case SpvOpIsFinite:
case SpvOpIsNormal:
case SpvOpLogicalEqual:
case SpvOpLogicalNotEqual:
case SpvOpLogicalOr:
case SpvOpLogicalAnd:
case SpvOpLogicalNot:
case SpvOpSelect:
case SpvOpIEqual:
case SpvOpINotEqual:
case SpvOpUGreaterThan:
case SpvOpSGreaterThan:
case SpvOpUGreaterThanEqual:
case SpvOpSGreaterThanEqual:
case SpvOpULessThan:
case SpvOpSLessThan:
case SpvOpULessThanEqual:
case SpvOpSLessThanEqual:
case SpvOpFOrdEqual:
case SpvOpFUnordEqual:
case SpvOpFOrdNotEqual:
case SpvOpFUnordNotEqual:
case SpvOpFOrdLessThan:
case SpvOpFUnordLessThan:
case SpvOpFOrdGreaterThan:
case SpvOpFUnordGreaterThan:
case SpvOpFOrdLessThanEqual:
case SpvOpFUnordLessThanEqual:
case SpvOpFOrdGreaterThanEqual:
case SpvOpFUnordGreaterThanEqual:
case SpvOpShiftRightLogical:
case SpvOpShiftRightArithmetic:
case SpvOpShiftLeftLogical:
case SpvOpBitwiseOr:
case SpvOpBitwiseXor:
case SpvOpBitwiseAnd:
case SpvOpNot:
case SpvOpBitFieldInsert:
case SpvOpBitFieldSExtract:
case SpvOpBitFieldUExtract:
case SpvOpBitReverse:
case SpvOpBitCount:
case SpvOpDPdx:
case SpvOpDPdy:
case SpvOpFwidth:
case SpvOpDPdxFine:
case SpvOpDPdyFine:
case SpvOpFwidthFine:
case SpvOpDPdxCoarse:
case SpvOpDPdyCoarse:
case SpvOpFwidthCoarse:
case SpvOpControlBarrier:
case SpvOpMemoryBarrier:
case SpvOpAtomicLoad:
case SpvOpAtomicStore:
case SpvOpAtomicExchange:
case SpvOpAtomicCompareExchange:
case SpvOpAtomicCompareExchangeWeak:
case SpvOpAtomicIIncrement:
case SpvOpAtomicIDecrement:
case SpvOpAtomicIAdd:
case SpvOpAtomicISub:
case SpvOpAtomicSMin:
case SpvOpAtomicUMin:
case SpvOpAtomicSMax:
case SpvOpAtomicUMax:
case SpvOpAtomicAnd:
case SpvOpAtomicOr:
case SpvOpAtomicXor:
case SpvOpPhi:
case SpvOpLoopMerge:
case SpvOpSelectionMerge:
case SpvOpLabel:
case SpvOpBranch:
case SpvOpBranchConditional:
case SpvOpSwitch:
case SpvOpKill:
case SpvOpReturn:
case SpvOpReturnValue:
case SpvOpUnreachable:
case SpvOpGroupNonUniformElect:
case SpvOpGroupNonUniformAll:
case SpvOpGroupNonUniformAny:
case SpvOpGroupNonUniformAllEqual:
case SpvOpGroupNonUniformBroadcast:
case SpvOpGroupNonUniformBroadcastFirst:
case SpvOpGroupNonUniformBallot:
case SpvOpGroupNonUniformInverseBallot:
case SpvOpGroupNonUniformBallotBitExtract:
case SpvOpGroupNonUniformBallotBitCount:
case SpvOpGroupNonUniformBallotFindLSB:
case SpvOpGroupNonUniformBallotFindMSB:
case SpvOpGroupNonUniformShuffle:
case SpvOpGroupNonUniformShuffleXor:
case SpvOpGroupNonUniformShuffleUp:
case SpvOpGroupNonUniformShuffleDown:
case SpvOpGroupNonUniformIAdd:
case SpvOpGroupNonUniformFAdd:
case SpvOpGroupNonUniformIMul:
case SpvOpGroupNonUniformFMul:
case SpvOpGroupNonUniformSMin:
case SpvOpGroupNonUniformUMin:
case SpvOpGroupNonUniformFMin:
case SpvOpGroupNonUniformSMax:
case SpvOpGroupNonUniformUMax:
case SpvOpGroupNonUniformFMax:
case SpvOpGroupNonUniformBitwiseAnd:
case SpvOpGroupNonUniformBitwiseOr:
case SpvOpGroupNonUniformBitwiseXor:
case SpvOpGroupNonUniformLogicalAnd:
case SpvOpGroupNonUniformLogicalOr:
case SpvOpGroupNonUniformLogicalXor:
case SpvOpGroupNonUniformQuadBroadcast:
case SpvOpGroupNonUniformQuadSwap:
case SpvOpCopyLogical:
return true;
default:
return false;
}
}
static bool SpvIsIgnored(SpvOp pOpCode) {
switch (pOpCode) {
case SpvOpSource:
case SpvOpName:
case SpvOpMemberName:
return true;
default:
return false;
}
}
static bool SpvHasOperands(SpvOp pOpCode, uint32_t &rOperandWordStart, uint32_t &rOperandWordCount, uint32_t &rOperandWordStride, uint32_t &rOperandWordSkip, bool &rOperandWordSkipString, bool pIncludePhi) {
switch (pOpCode) {
case SpvOpExecutionMode:
case SpvOpBranchConditional:
case SpvOpSwitch:
case SpvOpReturnValue:
case SpvOpDecorate:
case SpvOpMemberDecorate:
rOperandWordStart = 1;
rOperandWordCount = 1;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpStore:
case SpvOpMemoryBarrier:
rOperandWordStart = 1;
rOperandWordCount = 2;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpControlBarrier:
rOperandWordStart = 1;
rOperandWordCount = 3;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpTypeVector:
case SpvOpTypeMatrix:
case SpvOpTypeImage:
case SpvOpTypeSampledImage:
case SpvOpTypeRuntimeArray:
rOperandWordStart = 2;
rOperandWordCount = 1;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpTypeArray:
rOperandWordStart = 2;
rOperandWordCount = 2;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpTypeStruct:
case SpvOpTypeFunction:
rOperandWordStart = 2;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpEntryPoint:
rOperandWordStart = 2;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = 1;
rOperandWordSkipString = true;
return true;
case SpvOpTypePointer:
case SpvOpLoad:
case SpvOpCompositeExtract:
case SpvOpCopyObject:
case SpvOpTranspose:
case SpvOpImage:
case SpvOpImageQueryFormat:
case SpvOpImageQueryOrder:
case SpvOpImageQuerySize:
case SpvOpImageQueryLevels:
case SpvOpImageQuerySamples:
case SpvOpConvertFToU:
case SpvOpConvertFToS:
case SpvOpConvertSToF:
case SpvOpConvertUToF:
case SpvOpUConvert:
case SpvOpSConvert:
case SpvOpFConvert:
case SpvOpBitcast:
case SpvOpSNegate:
case SpvOpFNegate:
case SpvOpAny:
case SpvOpAll:
case SpvOpIsNan:
case SpvOpIsInf:
case SpvOpIsFinite:
case SpvOpIsNormal:
case SpvOpLogicalNot:
case SpvOpNot:
case SpvOpBitReverse:
case SpvOpBitCount:
case SpvOpDPdx:
case SpvOpDPdy:
case SpvOpFwidth:
case SpvOpDPdxFine:
case SpvOpDPdyFine:
case SpvOpFwidthFine:
case SpvOpDPdxCoarse:
case SpvOpDPdyCoarse:
case SpvOpFwidthCoarse:
case SpvOpGroupNonUniformElect:
case SpvOpCopyLogical:
rOperandWordStart = 3;
rOperandWordCount = 1;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpVectorShuffle:
case SpvOpCompositeInsert:
case SpvOpSampledImage:
case SpvOpImageQuerySizeLod:
case SpvOpImageQueryLod:
case SpvOpIAdd:
case SpvOpFAdd:
case SpvOpISub:
case SpvOpFSub:
case SpvOpIMul:
case SpvOpFMul:
case SpvOpUDiv:
case SpvOpSDiv:
case SpvOpFDiv:
case SpvOpUMod:
case SpvOpSRem:
case SpvOpSMod:
case SpvOpFRem:
case SpvOpFMod:
case SpvOpVectorTimesScalar:
case SpvOpMatrixTimesScalar:
case SpvOpVectorTimesMatrix:
case SpvOpMatrixTimesVector:
case SpvOpMatrixTimesMatrix:
case SpvOpOuterProduct:
case SpvOpDot:
case SpvOpIAddCarry:
case SpvOpISubBorrow:
case SpvOpUMulExtended:
case SpvOpSMulExtended:
case SpvOpLogicalEqual:
case SpvOpLogicalNotEqual:
case SpvOpLogicalOr:
case SpvOpLogicalAnd:
case SpvOpIEqual:
case SpvOpINotEqual:
case SpvOpUGreaterThan:
case SpvOpSGreaterThan:
case SpvOpUGreaterThanEqual:
case SpvOpSGreaterThanEqual:
case SpvOpULessThan:
case SpvOpSLessThan:
case SpvOpULessThanEqual:
case SpvOpSLessThanEqual:
case SpvOpFOrdEqual:
case SpvOpFUnordEqual:
case SpvOpFOrdNotEqual:
case SpvOpFUnordNotEqual:
case SpvOpFOrdLessThan:
case SpvOpFUnordLessThan:
case SpvOpFOrdGreaterThan:
case SpvOpFUnordGreaterThan:
case SpvOpFOrdLessThanEqual:
case SpvOpFUnordLessThanEqual:
case SpvOpFOrdGreaterThanEqual:
case SpvOpFUnordGreaterThanEqual:
case SpvOpShiftRightLogical:
case SpvOpShiftRightArithmetic:
case SpvOpShiftLeftLogical:
case SpvOpBitwiseOr:
case SpvOpBitwiseAnd:
case SpvOpBitwiseXor:
case SpvOpGroupNonUniformAll:
case SpvOpGroupNonUniformAny:
case SpvOpGroupNonUniformAllEqual:
case SpvOpGroupNonUniformBroadcastFirst:
case SpvOpGroupNonUniformBallot:
case SpvOpGroupNonUniformInverseBallot:
case SpvOpGroupNonUniformBallotFindLSB:
case SpvOpGroupNonUniformBallotFindMSB:
rOperandWordStart = 3;
rOperandWordCount = 2;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpImageTexelPointer:
case SpvOpSelect:
case SpvOpBitFieldSExtract:
case SpvOpBitFieldUExtract:
case SpvOpAtomicLoad:
case SpvOpAtomicIIncrement:
case SpvOpAtomicIDecrement:
case SpvOpGroupNonUniformBroadcast:
case SpvOpGroupNonUniformBallotBitExtract:
case SpvOpGroupNonUniformShuffle:
case SpvOpGroupNonUniformShuffleXor:
case SpvOpGroupNonUniformShuffleUp:
case SpvOpGroupNonUniformShuffleDown:
case SpvOpGroupNonUniformQuadBroadcast:
case SpvOpGroupNonUniformQuadSwap:
rOperandWordStart = 3;
rOperandWordCount = 3;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpGroupNonUniformBallotBitCount:
rOperandWordStart = 3;
rOperandWordCount = 3;
rOperandWordStride = 1;
rOperandWordSkip = 1;
rOperandWordSkipString = false;
return true;
case SpvOpAtomicStore:
rOperandWordStart = 1;
rOperandWordCount = 4;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpBitFieldInsert:
case SpvOpAtomicExchange:
case SpvOpAtomicIAdd:
case SpvOpAtomicISub:
case SpvOpAtomicSMin:
case SpvOpAtomicUMin:
case SpvOpAtomicSMax:
case SpvOpAtomicUMax:
case SpvOpAtomicAnd:
case SpvOpAtomicOr:
case SpvOpAtomicXor:
rOperandWordStart = 3;
rOperandWordCount = 4;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpAtomicCompareExchange:
case SpvOpAtomicCompareExchangeWeak:
rOperandWordStart = 3;
rOperandWordCount = 6;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpConstantComposite:
case SpvOpFunctionCall:
case SpvOpAccessChain:
case SpvOpCompositeConstruct:
rOperandWordStart = 3;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpSpecConstantOp:
rOperandWordStart = 3;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = 0;
rOperandWordSkipString = false;
return true;
case SpvOpExtInst:
case SpvOpGroupNonUniformIAdd:
case SpvOpGroupNonUniformFAdd:
case SpvOpGroupNonUniformIMul:
case SpvOpGroupNonUniformFMul:
case SpvOpGroupNonUniformSMin:
case SpvOpGroupNonUniformUMin:
case SpvOpGroupNonUniformFMin:
case SpvOpGroupNonUniformSMax:
case SpvOpGroupNonUniformUMax:
case SpvOpGroupNonUniformFMax:
case SpvOpGroupNonUniformBitwiseAnd:
case SpvOpGroupNonUniformBitwiseOr:
case SpvOpGroupNonUniformBitwiseXor:
case SpvOpGroupNonUniformLogicalAnd:
case SpvOpGroupNonUniformLogicalOr:
case SpvOpGroupNonUniformLogicalXor:
rOperandWordStart = 3;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = 1;
rOperandWordSkipString = false;
return true;
case SpvOpImageWrite:
rOperandWordStart = 1;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = 3;
rOperandWordSkipString = false;
return true;
case SpvOpImageSampleImplicitLod:
case SpvOpImageSampleExplicitLod:
case SpvOpImageSampleProjImplicitLod:
case SpvOpImageSampleProjExplicitLod:
case SpvOpImageFetch:
case SpvOpImageRead:
rOperandWordStart = 3;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = 2;
rOperandWordSkipString = false;
return true;
case SpvOpImageSampleDrefImplicitLod:
case SpvOpImageSampleDrefExplicitLod:
case SpvOpImageSampleProjDrefImplicitLod:
case SpvOpImageSampleProjDrefExplicitLod:
case SpvOpImageGather:
case SpvOpImageDrefGather:
rOperandWordStart = 3;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 1;
rOperandWordSkip = 3;
rOperandWordSkipString = false;
return true;
case SpvOpPhi:
if (pIncludePhi) {
rOperandWordStart = 3;
rOperandWordCount = UINT32_MAX;
rOperandWordStride = 2;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
}
else {
rOperandWordStart = 0;
rOperandWordCount = 0;
rOperandWordStride = 0;
rOperandWordSkip = 0;
rOperandWordSkipString = false;
return true;
}
case SpvOpFunction:
case SpvOpVariable:
rOperandWordStart = 4;
rOperandWordCount = 1;
rOperandWordStride = 1;
rOperandWordSkip = UINT32_MAX;
rOperandWordSkipString = false;
return true;
case SpvOpLabel:
case SpvOpBranch:
case SpvOpConstantTrue:
case SpvOpConstantFalse:
case SpvOpConstant:
case SpvOpConstantSampler:
case SpvOpConstantNull:
case SpvOpSpecConstantTrue:
case SpvOpSpecConstantFalse:
case SpvOpSpecConstant:
case SpvOpCapability:
case SpvOpExtInstImport:
case SpvOpMemoryModel:
case SpvOpTypeVoid:
case SpvOpTypeBool:
case SpvOpTypeInt:
case SpvOpTypeFloat:
case SpvOpTypeSampler:
case SpvOpLoopMerge:
case SpvOpSelectionMerge:
case SpvOpKill:
case SpvOpReturn:
case SpvOpUnreachable:
case SpvOpFunctionParameter:
case SpvOpFunctionEnd:
case SpvOpExtension:
case SpvOpUndef:
case SpvOpSource:
case SpvOpName:
case SpvOpMemberName:
rOperandWordStart = 0;
rOperandWordCount = 0;
rOperandWordStride = 0;
rOperandWordSkip = 0;
rOperandWordSkipString = false;
return true;
default:
return false;
}
}
static bool SpvHasLabels(SpvOp pOpCode, uint32_t &rLabelWordStart, uint32_t &rLabelWordCount, uint32_t &rLabelWordStride, bool pIncludePhi) {
switch (pOpCode) {
case SpvOpSelectionMerge:
case SpvOpBranch:
rLabelWordStart = 1;
rLabelWordCount = 1;
rLabelWordStride = 1;
return true;
case SpvOpLoopMerge:
rLabelWordStart = 1;
rLabelWordCount = 2;
rLabelWordStride = 1;
return true;
case SpvOpBranchConditional:
rLabelWordStart = 2;
rLabelWordCount = 2;
rLabelWordStride = 1;
return true;
case SpvOpSwitch:
rLabelWordStart = 2;
rLabelWordCount = UINT32_MAX;
rLabelWordStride = 2;
return true;
case SpvOpPhi:
if (pIncludePhi) {
rLabelWordStart = 4;
rLabelWordCount = UINT32_MAX;
rLabelWordStride = 2;
return true;
}
else {
return false;
}
default:
return false;
}
}
// Used to indicate which operations have side effects and can't be discarded if their result is not used.
static bool SpvHasSideEffects(SpvOp pOpCode) {
switch (pOpCode) {
case SpvOpFunctionCall:
case SpvOpAtomicExchange:
case SpvOpAtomicCompareExchange:
case SpvOpAtomicCompareExchangeWeak:
case SpvOpAtomicIIncrement:
case SpvOpAtomicIDecrement:
case SpvOpAtomicIAdd:
case SpvOpAtomicISub:
case SpvOpAtomicSMin:
case SpvOpAtomicUMin:
case SpvOpAtomicSMax:
case SpvOpAtomicUMax:
case SpvOpAtomicAnd:
case SpvOpAtomicOr:
case SpvOpAtomicXor:
case SpvOpAtomicFlagTestAndSet:
case SpvOpAtomicFlagClear:
return true;
default:
return false;
}
}
static bool SpvOpIsTerminator(SpvOp pOpCode) {
switch (pOpCode) {
case SpvOpBranch:
case SpvOpBranchConditional:
case SpvOpSwitch:
case SpvOpReturn:
case SpvOpReturnValue:
case SpvOpKill:
case SpvOpUnreachable:
return true;
default:
return false;
}
}
static bool checkOperandWordSkip(uint32_t pWordIndex, const uint32_t *pSpirvWords, uint32_t pRelativeWordIndex, uint32_t pOperandWordSkip, bool pOperandWordSkipString, uint32_t &rOperandWordIndex) {
if (pRelativeWordIndex == pOperandWordSkip) {
if (pOperandWordSkipString) {
const char *operandString = reinterpret_cast<const char *>(&pSpirvWords[pWordIndex + rOperandWordIndex]);
uint32_t stringLengthInWords = (strlen(operandString) + sizeof(uint32_t)) / sizeof(uint32_t);
rOperandWordIndex += stringLengthInWords;
}
else {
rOperandWordIndex++;
}
return true;
}
else {
return false;
}
}
static uint32_t addToList(uint32_t pInstructionIndex, uint32_t pListIndex, std::vector<ListNode> &rListNodes) {
rListNodes.emplace_back(pInstructionIndex, pListIndex);
return uint32_t(rListNodes.size() - 1);
}
// Shader
Shader::Shader() {
// Empty.
}
Shader::Shader(const void *pData, size_t pSize, bool pInlineFunctions) {
parse(pData, pSize, pInlineFunctions);
}
void Shader::clear() {
extSpirvWords = nullptr;
extSpirvWordCount = 0;
inlinedSpirvWords.clear();
instructions.clear();
instructionAdjacentListIndices.clear();
instructionInDegrees.clear();
instructionOutDegrees.clear();
instructionOrder.clear();
blocks.clear();
blockPreOrderIndices.clear();
blockPostOrderIndices.clear();
functions.clear();
variableOrder.clear();
results.clear();
specializations.clear();
decorations.clear();
phis.clear();
loopHeaders.clear();
listNodes.clear();
defaultSwitchOpConstantInt = UINT32_MAX;
}
constexpr uint32_t SpvStartWordIndex = 5;
bool Shader::checkData(const void *pData, size_t pSize) {
const uint32_t *words = reinterpret_cast<const uint32_t *>(pData);
const size_t wordCount = pSize / sizeof(uint32_t);
if (wordCount < SpvStartWordIndex) {
fprintf(stderr, "Not enough words in SPIR-V.\n");
return false;
}
if (words[0] != SpvMagicNumber) {
fprintf(stderr, "Invalid SPIR-V Magic Number on header.\n");
return false;
}
if (words[1] > SpvVersion) {
fprintf(stderr, "SPIR-V Version is too new for the library. Max version for the library is 0x%X.\n", SpvVersion);
return false;
}
return true;
}
bool Shader::inlineData(const void *pData, size_t pSize) {
assert(pData != nullptr);
assert(pSize > 0);
struct CallItem {
uint32_t wordIndex = 0;
uint32_t functionId = UINT32_MAX;
uint32_t blockId = UINT32_MAX;
uint32_t startBlockId = UINT32_MAX;
uint32_t loopBlockId = UINT32_MAX;
uint32_t continueBlockId = UINT32_MAX;
uint32_t returnBlockId = UINT32_MAX;
uint32_t resultType = UINT32_MAX;
uint32_t resultId = UINT32_MAX;
uint32_t parameterIndex = 0;
uint32_t remapsPendingCount = 0;
uint32_t returnParametersCount = 0;
uint32_t sameBlockOperationsCount = 0;
bool startBlockIdAssigned = false;
bool functionInlined = false;
CallItem(uint32_t wordIndex, uint32_t functionId = UINT32_MAX, bool functionInlined = false, uint32_t startBlockId = UINT32_MAX, uint32_t loopBlockId = UINT32_MAX, uint32_t continueBlockId = UINT32_MAX, uint32_t returnBlockId = UINT32_MAX, uint32_t resultType = UINT32_MAX, uint32_t resultId = UINT32_MAX)
: wordIndex(wordIndex), functionId(functionId), functionInlined(functionInlined), startBlockId(startBlockId), loopBlockId(loopBlockId), continueBlockId(continueBlockId), returnBlockId(returnBlockId), resultType(resultType), resultId(resultId)
{
// Regular constructor.
}
};
struct FunctionDefinition {
uint32_t wordIndex = 0;
uint32_t wordCount = 0;
uint32_t resultId = UINT32_MAX;
uint32_t functionWordCount = 0;
uint32_t codeWordCount = 0;
uint32_t variableWordCount = 0;
uint32_t decorationWordCount = 0;
uint32_t inlineWordCount = 0;
uint32_t returnValueCount = 0;
uint32_t callIndex = 0;
uint32_t callCount = 0;
uint32_t parameterIndex = 0;
uint32_t parameterCount = 0;
uint32_t inlinedVariableWordCount = 0;
bool canInline = true;
FunctionDefinition() {
// Default empty constructor.
}
FunctionDefinition(uint32_t resultId) : resultId(resultId) {
// Constructor for sorting.
}
bool operator<(const FunctionDefinition &other) const {
return resultId < other.resultId;
}
};
struct FunctionParameter {
uint32_t resultId = 0;
FunctionParameter(uint32_t resultId) : resultId(resultId) {
// Regular constructor.
}
};
struct FunctionCall {
uint32_t wordIndex = 0;
uint32_t functionId = 0;
uint32_t sameBlockWordCount = 0;
FunctionCall(uint32_t wordIndex, uint32_t functionId, uint32_t sameBlockWordCount) : wordIndex(wordIndex), functionId(functionId), sameBlockWordCount(sameBlockWordCount) {
// Regular constructor.
}
};
struct FunctionResult {
uint32_t wordIndex = UINT32_MAX;
uint32_t decorationIndex = UINT32_MAX;
};
typedef std::vector<FunctionDefinition>::iterator FunctionDefinitionIterator;
struct FunctionItem {
FunctionDefinitionIterator function = {};
FunctionDefinitionIterator rootFunction = {};
uint32_t callIndex = 0;
FunctionItem(FunctionDefinitionIterator function, FunctionDefinitionIterator rootFunction, uint32_t callIndex) : function(function), rootFunction(rootFunction), callIndex(callIndex) {
// Regular constructor.
}
};
struct ResultDecoration {
uint32_t wordIndex = 0;
uint32_t nextDecorationIndex = 0;
ResultDecoration(uint32_t wordIndex, uint32_t nextDecorationIndex) : wordIndex(wordIndex), nextDecorationIndex(nextDecorationIndex) {
// Regular constructor.
}
};
thread_local std::vector<FunctionResult> functionResultMap;
thread_local std::vector<ResultDecoration> resultDecorations;
thread_local std::vector<uint32_t> loopMergeIdStack;
thread_local std::vector<FunctionDefinition> functionDefinitions;
thread_local std::vector<FunctionParameter> functionParameters;
thread_local std::vector<FunctionCall> functionCalls;
thread_local std::vector<FunctionItem> functionStack;
thread_local std::vector<CallItem> callStack;
thread_local std::vector<uint32_t> shaderResultMap;
thread_local std::vector<uint32_t> storeMap;
thread_local std::vector<uint32_t> storeMapChanges;
thread_local std::vector<uint32_t> loadMap;
thread_local std::vector<uint32_t> loadMapChanges;
thread_local std::vector<uint32_t> phiMap;
thread_local std::vector<uint32_t> opPhis;
thread_local std::vector<uint32_t> remapsPending;
thread_local std::vector<uint32_t> returnParameters;
thread_local std::vector<uint32_t> sameBlockOperations;
functionResultMap.clear();
resultDecorations.clear();
loopMergeIdStack.clear();
functionDefinitions.clear();
functionParameters.clear();
functionCalls.clear();
callStack.clear();
shaderResultMap.clear();
storeMap.clear();
storeMapChanges.clear();
loadMap.clear();
loadMapChanges.clear();
phiMap.clear();
opPhis.clear();
remapsPending.clear();
returnParameters.clear();
sameBlockOperations.clear();
// Parse all instructions in the shader first.
const uint32_t *dataWords = reinterpret_cast<const uint32_t *>(pData);
const size_t dataWordCount = pSize / sizeof(uint32_t);
const uint32_t dataIdBound = dataWords[3];
functionResultMap.resize(dataIdBound);
FunctionDefinition currentFunction;
uint32_t parseWordIndex = SpvStartWordIndex;
uint32_t entryPointFunctionId = UINT32_MAX;
uint32_t globalWordCount = 0;
uint32_t sameBlockWordCount = 0;
while (parseWordIndex < dataWordCount) {
SpvOp opCode = SpvOp(dataWords[parseWordIndex] & 0xFFFFU);
uint32_t wordCount = (dataWords[parseWordIndex] >> 16U) & 0xFFFFU;
if (wordCount == 0) {
fprintf(stderr, "Invalid word count found at %d.\n", parseWordIndex);
return false;
}
switch (opCode) {
case SpvOpFunction:
if (currentFunction.resultId != UINT32_MAX) {
fprintf(stderr, "Found function start without the previous function ending.\n");
return false;
}
currentFunction.resultId = dataWords[parseWordIndex + 2];
currentFunction.wordIndex = parseWordIndex;
currentFunction.functionWordCount = wordCount;
break;
case SpvOpFunctionEnd:
if (currentFunction.resultId == UINT32_MAX) {
fprintf(stderr, "Found function end without a function start.\n");
return false;
}
currentFunction.wordCount = parseWordIndex + wordCount - currentFunction.wordIndex;
currentFunction.functionWordCount += wordCount;
functionDefinitions.emplace_back(currentFunction);
// Reset the current function to being empty again.
currentFunction = FunctionDefinition();
break;
case SpvOpFunctionParameter:
if (currentFunction.resultId == UINT32_MAX) {
fprintf(stderr, "Found function parameter without a function start.\n");
return false;
}
currentFunction.functionWordCount += wordCount;