iridia-ir 0.6.0

iridia's shader IR: typed SSA values and operations in structured regions or a control flow graph, with the interface vocabulary, a validator and a text form
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
//! The validator: types of every operation, scoping of values in regions and dominance in a graph, exits against
//! their constructs, interface and resource references against the interface, and stage rules.

use alloc::{borrow::ToOwned, format, string::String, vec, vec::Vec};
use core::fmt;

use crate::{
    AtomicOp, Barrier, Binary, Block, Body, Builtin, End, Function, GatherOffset, LabelId, Level, Memory,
    Node, Op, Program, Query, Region, Res, Scalar, Terminator, Ternary, Type, Unary, ValueId, VarId, Wave,
    WaveOp,
    interface::{
        BufferKind, Interface, Resource, ResourceKind, SampleType, ScalarType, Stage, StageFacts,
        StorageTarget, TextureDim, Value, Varying,
    },
};

/// One broken rule, with the value it is about where there is one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Problem {
    /// The value the rule is about.
    pub value: Option<ValueId>,
    /// What is wrong.
    pub message: String,
}

impl fmt::Display for Problem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.value {
            Some(v) => write!(f, "%{}: {}", v.0, self.message),
            None => f.write_str(&self.message),
        }
    }
}

/// The IR type of an interface value.
pub fn value_type(v: Value) -> Type {
    let scalar = match v.scalar {
        ScalarType::Bool => Scalar::Bool,
        ScalarType::Sint => Scalar::I32,
        ScalarType::Uint => Scalar::U32,
        ScalarType::Float => Scalar::F32,
        ScalarType::Double => Scalar::F64,
    };
    Type::vector(scalar, v.width)
}

/// The type of the lanes `mask` selects of an interface value: its scalar, one lane per bit. An error says why the
/// mask selects none of it or lanes it does not have.
pub fn masked_type(v: Value, mask: u8) -> Result<Type, String> {
    let all = value_type(v);
    let lanes = u8::try_from(mask.count_ones()).unwrap_or(0);
    if mask == 0 || lanes > all.lanes || mask >> all.lanes != 0 {
        return Err(format!("lane mask {mask:#06b} of a value of {all}"));
    }
    Ok(Type::vector(all.scalar, lanes))
}

fn same(a: Type, b: Type, what: &str) -> Result<(), String> {
    if a == b {
        Ok(())
    } else {
        Err(format!("{what}: {a} and {b} differ"))
    }
}

/// The type an operation gives from its operands' types (`ty` looks them up), or `None` where the operation's type is
/// stated rather than derived (literals, parameters, conversions, bitcasts, loads, samples). An error names the rule
/// the operands break.
pub fn derived_type(op: &Op, ty: impl Fn(ValueId) -> Option<Type>) -> Result<Option<Type>, String> {
    let get = |v: ValueId| ty(v).ok_or_else(|| format!("operand %{} is not a value of the function", v.0));
    let float = |t: Type, what: &str| {
        if t.scalar.is_float() {
            Ok(())
        } else {
            Err(format!("{what} of {t}, which is not a float"))
        }
    };
    Ok(Some(match op {
        Op::Literal(_)
        | Op::Undef
        | Op::Param
        | Op::Convert(..)
        | Op::Bitcast(_)
        | Op::LoadInput(..)
        | Op::LoadInputAt(..)
        | Op::LoadOutputAt(..)
        | Op::LoadPatch(..)
        | Op::Builtin(_)
        | Op::LoadVar(_)
        | Op::LoadElement(..)
        | Op::LoadState(_)
        | Op::LoadBuffer { .. }
        | Op::Sample { .. }
        | Op::Gather { .. }
        | Op::LoadTexel { .. }
        | Op::Query(..)
        | Op::Atomic { .. }
        | Op::Counter(..) => return Ok(None),
        Op::Wave(w) => {
            // a value read from another invocation, or gathered over them, is a scalar or vector of numbers
            let numbers = |t: Type, what: &str| {
                if t.scalar == Scalar::Bool {
                    Err(format!("{what} of {t}, which is not a number"))
                } else {
                    Ok(t)
                }
            };
            match w {
                Wave::Ballot(c) => {
                    same(get(*c)?, Type::BOOL, "a ballot's condition")?;
                    Type::U32X4
                }
                Wave::First(a) | Wave::QuadSwap(a, _) => numbers(get(*a)?, "a wave read")?,
                Wave::Lane(a, l) | Wave::QuadLane(a, l) => {
                    index_type(get(*l)?, "a lane number")?;
                    numbers(get(*a)?, "a wave read")?
                }
                Wave::Collective { op, value, .. } => {
                    let t = get(*value)?;
                    match op {
                        WaveOp::All | WaveOp::Any => {
                            same(t, Type::BOOL, "a wave's all or any")?;
                            t
                        }
                        WaveOp::And | WaveOp::Or | WaveOp::Xor => {
                            if !t.scalar.is_integer() {
                                return Err(format!("a wave bit operation of {t}"));
                            }
                            t
                        }
                        WaveOp::Add | WaveOp::Mul | WaveOp::Min | WaveOp::Max => {
                            numbers(t, "a wave operation")?
                        }
                    }
                }
            }
        }
        Op::Unary(u, a) => {
            let t = get(*a)?;
            match u {
                Unary::Not => {
                    if t.scalar != Scalar::Bool && !t.scalar.is_integer() {
                        return Err(format!("not of {t}"));
                    }
                    t
                }
                Unary::INeg => {
                    if !t.scalar.is_integer() {
                        return Err(format!("integer negation of {t}"));
                    }
                    t
                }
                Unary::Any | Unary::All => {
                    if t.scalar != Scalar::Bool {
                        return Err(format!("any/all of {t}"));
                    }
                    Type::BOOL
                }
                Unary::IsNan | Unary::IsInf => {
                    float(t, "a float test")?;
                    t.with(Scalar::Bool)
                }
                Unary::CountOnes | Unary::BitReverse => {
                    if !t.scalar.is_integer() {
                        return Err(format!("a bit operation of {t}"));
                    }
                    t
                }
                Unary::FirstBitHigh | Unary::FirstBitHighSigned | Unary::FirstBitLow => {
                    if !t.scalar.is_integer() || t.scalar.bits() != 32 {
                        return Err(format!(
                            "a first-bit operation of {t}, which is not a 32-bit integer"
                        ));
                    }
                    t
                }
                Unary::F32ToF16 => {
                    if t.scalar != Scalar::F32 {
                        return Err(format!("f32tof16 of {t}"));
                    }
                    t.with(Scalar::U32)
                }
                Unary::F16ToF32 => {
                    if t.scalar != Scalar::U32 {
                        return Err(format!("f16tof32 of {t}"));
                    }
                    t.with(Scalar::F32)
                }
                Unary::ClampRange { lo, hi, nan } => {
                    let (l, h) = (f32::from_bits(*lo), f32::from_bits(*hi));
                    if !(l.is_finite() && h.is_finite() && l <= h) {
                        return Err(format!("a clamp to {l:?}..{h:?}, which is not a finite range"));
                    }
                    if *nan == crate::SaturateNan::Zero && !(l <= 0.0 && 0.0 <= h) {
                        return Err(format!("a clamp to {l:?}..{h:?} that gives 0 for NaN"));
                    }
                    float(t, "a clamp")?;
                    if t.scalar != Scalar::F32 {
                        return Err(format!("a clamp of {t}, whose bounds are f32"));
                    }
                    t
                }
                Unary::Quantize { frac_bits, .. } => {
                    if *frac_bits > 23 {
                        return Err(format!("a quantize to {frac_bits} fractional bits, more than 23"));
                    }
                    float(t, "a quantize")?;
                    t
                }
                _ => {
                    float(t, "a float operation")?;
                    t
                }
            }
        }
        Op::Binary(Binary::Shl | Binary::ShrU | Binary::ShrS, x, y) => {
            let (a, c) = (get(*x)?, get(*y)?);
            if !a.scalar.is_integer() || !c.scalar.is_unsigned() || a.lanes != c.lanes {
                return Err(format!("a shift of {a} by {c}"));
            }
            a
        }
        Op::Binary(b, x, y) => {
            let (a, c) = (get(*x)?, get(*y)?);
            same(a, c, "binary operands")?;
            match b {
                Binary::FAdd
                | Binary::FSub
                | Binary::FMul(_)
                | Binary::FDiv
                | Binary::FRem
                | Binary::FMin(_)
                | Binary::FMax(_)
                | Binary::FPow => {
                    float(a, "a float operation")?;
                    a
                }
                Binary::FCmp(_) => {
                    float(a, "a float comparison")?;
                    a.with(Scalar::Bool)
                }
                Binary::SCmp(_) | Binary::UCmp(_) => {
                    if a.scalar.is_float() {
                        return Err(format!("an integer comparison of {a}"));
                    }
                    a.with(Scalar::Bool)
                }
                Binary::IAdd
                | Binary::ISub
                | Binary::IMul
                | Binary::UDiv(_)
                | Binary::URem(_)
                | Binary::SDiv(_)
                | Binary::SRem(_)
                | Binary::UMulHigh
                | Binary::SMulHigh
                | Binary::UAddCarry
                | Binary::USubBorrow
                | Binary::SMin
                | Binary::SMax
                | Binary::UMin
                | Binary::UMax
                | Binary::Xor => {
                    if !a.scalar.is_integer() {
                        return Err(format!("an integer operation of {a}"));
                    }
                    a
                }
                Binary::And | Binary::Or => {
                    if a.scalar.is_float() {
                        return Err(format!("a bitwise operation of {a}"));
                    }
                    a
                }
                Binary::Shl | Binary::ShrU | Binary::ShrS => unreachable!("matched above"),
            }
        }
        Op::Ternary(Ternary::Fma, x, y, z) => {
            let (a, b, c) = (get(*x)?, get(*y)?, get(*z)?);
            same(a, b, "fma operands")?;
            same(a, c, "fma operands")?;
            float(a, "a fused multiply-add")?;
            a
        }
        Op::Ternary(Ternary::BitExtract { .. }, x, offset, count) => {
            let a = get(*x)?;
            if !a.scalar.is_integer() || a.scalar.bits() != 32 {
                return Err(format!("a bit field of {a}, which is not a 32-bit integer"));
            }
            for c in [offset, count] {
                same(get(*c)?, a.with(Scalar::U32), "a bit field count")?;
            }
            a
        }
        Op::BitInsert {
            base,
            insert,
            offset,
            count,
        } => {
            let a = get(*base)?;
            same(get(*insert)?, a, "a bit field insert")?;
            if !a.scalar.is_integer() || a.scalar.bits() != 32 {
                return Err(format!("a bit field of {a}, which is not a 32-bit integer"));
            }
            for c in [offset, count] {
                same(get(*c)?, a.with(Scalar::U32), "a bit field count")?;
            }
            a
        }
        Op::Select { cond, accept, reject } => {
            let (c, a, r) = (get(*cond)?, get(*accept)?, get(*reject)?);
            same(a, r, "select arms")?;
            if c.scalar != Scalar::Bool || (c.lanes != 1 && c.lanes != a.lanes) {
                return Err(format!("select condition {c} for {a}"));
            }
            a
        }
        Op::Compose(parts) => {
            let mut scalar = None;
            let mut lanes: u32 = 0;
            for p in parts {
                let t = get(*p)?;
                if *scalar.get_or_insert(t.scalar) != t.scalar {
                    return Err("compose parts of different scalar types".into());
                }
                lanes = lanes.saturating_add(u32::from(t.lanes));
            }
            let lanes = u8::try_from(lanes).ok().filter(|n| (2..=4).contains(n));
            match (scalar, lanes) {
                (Some(s), Some(n)) => Type::vector(s, n),
                _ => return Err("compose of other than 2 to 4 lanes".into()),
            }
        }
        Op::Swizzle(a, lanes) => {
            let t = get(*a)?;
            if lanes.iter().any(|&l| l >= t.lanes) {
                return Err(format!("swizzle {lanes:?} of {t}"));
            }
            return Ok(None);
        }
        Op::Splat(a) => {
            let t = get(*a)?;
            if t.lanes != 1 {
                return Err(format!("splat of {t}"));
            }
            return Ok(None);
        }
        Op::Dot(x, y) => {
            let (a, b) = (get(*x)?, get(*y)?);
            same(a, b, "dot operands")?;
            float(a, "a dot product")?;
            if a.lanes < 2 {
                return Err("a dot product of scalars".into());
            }
            Type::scalar(a.scalar)
        }
    }))
}

/// The interface's resource of an id.
fn resource(interface: &Interface, r: Res) -> Result<&Resource, String> {
    interface
        .resources
        .iter()
        .find(|x| x.id == r.id)
        .ok_or_else(|| format!("resource {} is not in the interface", r.id.0))
}

/// The lanes of a texture coordinate without an array layer.
fn dims(dim: TextureDim) -> u8 {
    match dim {
        TextureDim::D1 => 1,
        TextureDim::D2 => 2,
        TextureDim::D3 | TextureDim::Cube => 3,
    }
}

/// The lanes of a texture's size: a cube's is its faces' width and height.
fn size_lanes(dim: TextureDim) -> u8 {
    match dim {
        TextureDim::D1 => 1,
        TextureDim::D2 | TextureDim::Cube => 2,
        TextureDim::D3 => 3,
    }
}

/// The IR scalar a texel of a sample type reads as.
fn sample_scalar(s: SampleType) -> Scalar {
    match s {
        SampleType::Float | SampleType::Depth => Scalar::F32,
        SampleType::Sint => Scalar::I32,
        SampleType::Uint => Scalar::U32,
    }
}

/// The type of a value the pipeline gives.
pub fn builtin_type(b: Builtin) -> Type {
    match b {
        Builtin::OutputControlPoint | Builtin::PrimitiveId | Builtin::GsInstance | Builtin::PhaseInstance => {
            Type::U32
        }
        Builtin::DomainLocation => Type::vector(Scalar::F32, 3),
    }
}

/// The patch constants of a hull or domain program.
pub fn patch_constants(interface: &Interface) -> &[Varying] {
    match &interface.facts {
        StageFacts::Hull(h) => &h.patch_constants,
        StageFacts::Domain(d) => &d.patch_constants,
        _ => &[],
    }
}

fn index_type(t: Type, what: &str) -> Result<(), String> {
    if t == Type::U32 || t == Type::I32 {
        Ok(())
    } else {
        Err(format!("{what} of {t}"))
    }
}

fn int_coord(t: Type, lanes: u8, what: &str) -> Result<(), String> {
    if (t.scalar == Scalar::U32 || t.scalar == Scalar::I32) && t.lanes == lanes {
        Ok(())
    } else {
        Err(format!("{what} of {t}, where {lanes} integer lanes are wanted"))
    }
}

/// Check an operation whose type is stated: the operands' types and the stated type agree with the rule.
fn stated(op: &Op, t: Type, f: &Function, interface: &Interface) -> Result<(), String> {
    let get = |v: ValueId| {
        f.ty(v)
            .ok_or_else(|| format!("operand %{} is not a value of the function", v.0))
    };
    let element = |r: Res| -> Result<(), String> {
        let res = resource(interface, r)?;
        match r.index {
            Some(i) => index_type(get(i)?, "a resource array index"),
            None if res.count == 0 => Err(format!("resource {} has no elements", res.provenance.name)),
            None => Ok(()),
        }
    };
    let var = |v: crate::VarId| {
        f.vars
            .get(v.0 as usize)
            .ok_or_else(|| format!("variable {} does not exist", v.0))
    };
    // a filtered read's texture, sampler and coordinate: the texture's dimensions (without a layer) and sample type
    let sampled = |texture: Res, sampler: Res, coord: ValueId, compare: Option<ValueId>| {
        element(texture)?;
        element(sampler)?;
        let ResourceKind::Texture {
            dim,
            arrayed,
            sample,
            multisampled,
        } = resource(interface, texture)?.kind
        else {
            return Err("sample of a resource that is not a texture".to_owned());
        };
        if multisampled {
            return Err("a sample of a multisampled texture".into());
        }
        if !matches!(resource(interface, sampler)?.kind, ResourceKind::Sampler { .. }) {
            return Err("sample through a resource that is not a sampler".into());
        }
        let n = dims(dim);
        same(
            get(coord)?,
            Type::vector(Scalar::F32, n.saturating_add(u8::from(arrayed))),
            "sample coordinate",
        )?;
        if let Some(c) = compare {
            same(get(c)?, Type::F32, "a comparison reference")?;
        }
        Ok((n, sample))
    };
    match op {
        Op::Literal(_) | Op::Undef | Op::Param => Ok(()),
        Op::Convert(a, _) => {
            let a = get(*a)?;
            if a.lanes != t.lanes || a.scalar == Scalar::Bool || t.scalar == Scalar::Bool {
                return Err(format!("conversion of {a} to {t}"));
            }
            Ok(())
        }
        Op::Bitcast(a) => {
            let a = get(*a)?;
            let bits = |t: Type| t.scalar.bits().saturating_mul(u32::from(t.lanes));
            if bits(a) != bits(t) || a.scalar == Scalar::Bool || t.scalar == Scalar::Bool {
                return Err(format!("bitcast of {a} to {t}"));
            }
            Ok(())
        }
        Op::Swizzle(a, _) => {
            let a = get(*a)?;
            if a.scalar == t.scalar {
                Ok(())
            } else {
                Err(format!("swizzle of {a} to {t}"))
            }
        }
        Op::Splat(a) => {
            if get(*a)? == Type::scalar(t.scalar) && t.lanes >= 2 {
                Ok(())
            } else {
                Err(format!("splat of {} to {t}", get(*a)?))
            }
        }
        Op::LoadInput(slot, mask) | Op::LoadInputAt(slot, mask, _) => {
            if let Op::LoadInputAt(_, _, at) = op {
                index_type(get(*at)?, "a vertex index")?;
            }
            let v = interface.inputs.get(*slot as usize).ok_or_else(|| {
                format!(
                    "input slot {slot}, where the interface has {}",
                    interface.inputs.len()
                )
            })?;
            same(masked_type(v.value, *mask)?, t, "input")
        }
        Op::LoadOutputAt(slot, mask, at) => {
            index_type(get(*at)?, "a control point index")?;
            let v = interface
                .outputs
                .get(*slot as usize)
                .ok_or_else(|| format!("output slot {slot} is not in the interface"))?;
            same(masked_type(v.value, *mask)?, t, "output control point")
        }
        Op::LoadPatch(slot, mask) => {
            let v = patch_constants(interface)
                .get(*slot as usize)
                .ok_or_else(|| format!("patch constant slot {slot} is not in the interface"))?;
            same(masked_type(v.value, *mask)?, t, "patch constant")
        }
        Op::Builtin(b) => same(builtin_type(*b), t, "a pipeline value"),
        Op::LoadVar(v) => {
            let d = var(*v)?;
            if d.len.is_some() {
                return Err(format!("a whole load of the array variable {}", d.name));
            }
            same(d.ty, t, "variable load")
        }
        Op::LoadElement(v, i, _) => {
            let d = var(*v)?;
            if d.len.is_none() {
                return Err(format!("an element load of {}, which is not an array", d.name));
            }
            index_type(get(*i)?, "an element index")?;
            same(d.ty, t, "element load")
        }
        Op::LoadState(resource) => {
            let r = interface
                .resources
                .iter()
                .find(|r| r.id == *resource)
                .ok_or_else(|| format!("resource {} is not in the interface", resource.0))?;
            match r.kind {
                ResourceKind::PipelineState(p) => same(value_type(p.value()), t, "state load"),
                _ => Err(format!("state load from {:?}", r.kind)),
            }
        }
        Op::LoadBuffer {
            resource: r, address, ..
        } => {
            element(*r)?;
            match &resource(interface, *r)?.kind {
                ResourceKind::ConstantBuffer { .. }
                | ResourceKind::Buffer(BufferKind::Raw | BufferKind::Structured { .. })
                | ResourceKind::Storage {
                    target: StorageTarget::Buffer(_),
                    ..
                } => {}
                k => return Err(format!("buffer load from {k:?}")),
            }
            if let Some(i) = address.index {
                index_type(get(i)?, "a buffer index")?;
            }
            if t.scalar.bits() != 32 || t.scalar == Scalar::Bool {
                return Err(format!("buffer load of {t}"));
            }
            Ok(())
        }
        Op::Sample {
            texture,
            sampler,
            coord,
            level,
            compare,
            clamp,
            ..
        } => {
            let (n, sample) = sampled(*texture, *sampler, *coord, *compare)?;
            match level {
                Level::Bias(x) | Level::Exact(x) => same(get(*x)?, Type::F32, "sample level")?,
                Level::Grad(x, y) => {
                    let g = Type::vector(Scalar::F32, n);
                    same(get(*x)?, g, "sample gradient along x")?;
                    same(get(*y)?, g, "sample gradient along y")?;
                }
                Level::Auto | Level::Zero => {}
            }
            if let Some(c) = clamp {
                same(get(*c)?, Type::F32, "a level clamp")?;
            }
            let want = if compare.is_some() || sample == SampleType::Depth {
                Type::F32
            } else {
                Type::vector(sample_scalar(sample), 4)
            };
            same(t, want, "sample result")
        }
        Op::Gather {
            texture,
            sampler,
            coord,
            component,
            compare,
            offset,
        } => {
            let (n, sample) = sampled(*texture, *sampler, *coord, *compare)?;
            if *component > 3 {
                return Err(format!("a gather of lane {component}"));
            }
            if let GatherOffset::Dynamic(o) = offset {
                same(get(*o)?, Type::vector(Scalar::I32, n), "a gather offset")?;
            }
            let want = if compare.is_some() {
                Type::vector(Scalar::F32, 4)
            } else {
                Type::vector(sample_scalar(sample), 4)
            };
            same(t, want, "gather result")
        }
        Op::LoadTexel {
            texture,
            coord,
            level,
            sample,
            ..
        } => {
            element(*texture)?;
            let c = get(*coord)?;
            for x in level.iter().chain(sample.iter()) {
                index_type(get(*x)?, "a texel level or sample")?;
            }
            let want = match resource(interface, *texture)?.kind {
                ResourceKind::Texture {
                    dim,
                    arrayed,
                    multisampled,
                    sample: s,
                } => {
                    int_coord(
                        c,
                        dims(dim).saturating_add(u8::from(arrayed)),
                        "a texel coordinate",
                    )?;
                    if multisampled != sample.is_some() {
                        return Err("a texel load whose sample does not match the texture".into());
                    }
                    if s == SampleType::Depth {
                        Type::F32
                    } else {
                        Type::vector(sample_scalar(s), 4)
                    }
                }
                ResourceKind::Storage {
                    target:
                        StorageTarget::Image {
                            dim,
                            arrayed,
                            sample: s,
                        },
                    ..
                } => {
                    int_coord(
                        c,
                        dims(dim).saturating_add(u8::from(arrayed)),
                        "a texel coordinate",
                    )?;
                    Type::vector(sample_scalar(s), 4)
                }
                ResourceKind::Buffer(BufferKind::Typed(v))
                | ResourceKind::Storage {
                    target: StorageTarget::Buffer(BufferKind::Typed(v)),
                    ..
                } => {
                    int_coord(c, 1, "a buffer element")?;
                    Type::vector(value_type(v).scalar, 4)
                }
                ref k => return Err(format!("texel load from {k:?}")),
            };
            same(t, want, "texel load")
        }
        Op::Query(r, q) => {
            element(*r)?;
            let kind = &resource(interface, *r)?.kind;
            let image = match kind {
                ResourceKind::Texture {
                    dim,
                    arrayed,
                    multisampled,
                    ..
                } => Some((*dim, *arrayed, *multisampled, true)),
                ResourceKind::Storage {
                    target: StorageTarget::Image { dim, arrayed, .. },
                    ..
                } => Some((*dim, *arrayed, false, false)),
                _ => None,
            };
            let buffer = matches!(
                kind,
                ResourceKind::Buffer(_)
                    | ResourceKind::Storage {
                        target: StorageTarget::Buffer(_),
                        ..
                    }
            );
            match (q, image) {
                (Query::Size(level), Some((dim, _, ms, levels))) => {
                    if level.is_some() != (levels && !ms) {
                        return Err("a size query whose level does not match the resource".into());
                    }
                    if let Some(l) = level {
                        index_type(get(*l)?, "a size query's level")?;
                    }
                    same(t, Type::vector(Scalar::U32, size_lanes(dim)), "size query")
                }
                (Query::Layers, Some((_, true, _, _))) => same(t, Type::U32, "layer query"),
                (Query::Levels, Some((_, _, false, true))) => same(t, Type::U32, "level query"),
                (Query::Samples, Some((_, _, true, _))) => same(t, Type::U32, "sample query"),
                (Query::SamplePosition(i), Some((_, _, true, _))) => {
                    index_type(get(*i)?, "a sample index")?;
                    same(t, Type::vector(Scalar::F32, 2), "sample position")
                }
                (Query::Elements, None) if buffer => same(t, Type::U32, "element query"),
                (q, _) => Err(format!("{q:?} of {kind:?}")),
            }
        }
        Op::Atomic {
            memory,
            op: aop,
            value,
            compare,
        } => {
            let v = get(*value)?;
            if !v.scalar.is_integer() || v.lanes != 1 || !(v.scalar.bits() == 32 || v.scalar.bits() == 64) {
                return Err(format!("an atomic operation of {v}"));
            }
            same(t, v, "atomic result")?;
            if (*aop == AtomicOp::CompareExchange) != compare.is_some() {
                return Err("a comparand without a compare-exchange, or the reverse".into());
            }
            if let Some(c) = compare {
                same(get(*c)?, v, "a comparand")?;
            }
            match memory {
                Memory::Buffer(r, address) => {
                    element(*r)?;
                    if !matches!(
                        resource(interface, *r)?.kind,
                        ResourceKind::Storage {
                            target: StorageTarget::Buffer(_),
                            ..
                        }
                    ) {
                        return Err("an atomic operation on a resource that is not a storage buffer".into());
                    }
                    if let Some(i) = address.index {
                        index_type(get(i)?, "a buffer index")?;
                    }
                }
                Memory::Image(r, c) => {
                    element(*r)?;
                    let (lanes, scalar) = match resource(interface, *r)?.kind {
                        ResourceKind::Storage {
                            target: StorageTarget::Image { dim, arrayed, sample },
                            ..
                        } => (dims(dim).saturating_add(u8::from(arrayed)), sample_scalar(sample)),
                        ResourceKind::Storage {
                            target: StorageTarget::Buffer(BufferKind::Typed(e)),
                            ..
                        } => (1, value_type(e).scalar),
                        _ => {
                            return Err(
                                "an atomic operation on a resource that is not a storage image or typed buffer".into()
                            );
                        }
                    };
                    int_coord(get(*c)?, lanes, "a texel coordinate")?;
                    if scalar != v.scalar {
                        return Err(format!("an atomic operation of {v} on {} texels", scalar.name()));
                    }
                }
                Memory::Var(x, i) => {
                    let d = var(*x)?;
                    if d.space == crate::Space::Payload {
                        return Err(format!(
                            "an atomic operation on task payload {}, which is read alone",
                            d.name
                        ));
                    }
                    if d.len.is_some() != i.is_some() {
                        return Err(format!("an atomic operation on {} without its element", d.name));
                    }
                    if let Some(i) = i {
                        index_type(get(*i)?, "an element index")?;
                    }
                    same(d.ty, v, "an atomic variable")?;
                }
            }
            Ok(())
        }
        Op::Counter(r, _) => {
            element(*r)?;
            if !matches!(
                resource(interface, *r)?.kind,
                ResourceKind::Storage {
                    target: StorageTarget::Buffer(BufferKind::Structured { .. }),
                    ..
                }
            ) {
                return Err("a counter of a resource that is not a structured storage buffer".into());
            }
            same(t, Type::U32, "a counter")
        }
        _ => Ok(()),
    }
}

struct Checker<'a> {
    f: &'a Function,
    interface: &'a Interface,
    stage: Stage,
    visible: Vec<bool>,
    placed: Vec<bool>,
    labels: Vec<(LabelId, Vec<ValueId>, Option<Vec<ValueId>>)>,
    problems: Vec<Problem>,
    /// Checking a hull program's patch constant function.
    patch: bool,
}

impl Checker<'_> {
    fn problem(&mut self, value: Option<ValueId>, message: impl Into<String>) {
        self.problems.push(Problem {
            value,
            message: message.into(),
        });
    }
    fn ty(&self, v: ValueId) -> Option<Type> {
        self.f.ty(v)
    }
    /// A store into variable `var`: a task payload is read alone.
    fn writable(&mut self, var: VarId, value: ValueId) {
        if let Some(d) = self.f.vars.get(var.0 as usize)
            && d.space == crate::Space::Payload
        {
            let m = format!("a store to task payload {}, which is read alone", d.name);
            self.problem(Some(value), m);
        }
    }
    fn uses(&mut self, at: Option<ValueId>, v: ValueId) {
        if !self.visible.get(v.0 as usize).copied().unwrap_or(false) {
            self.problem(
                at,
                format!(
                    "uses %{}, which is not defined before it in an enclosing region",
                    v.0
                ),
            );
        }
    }
    fn define(&mut self, v: ValueId, param: bool) {
        let Some(def) = self.f.value(v) else {
            self.problem(Some(v), "is not a value of the function");
            return;
        };
        if (def.op == Op::Param) != param {
            self.problem(
                Some(v),
                if param {
                    "a construct value that is not a parameter"
                } else {
                    "a parameter placed as a node"
                },
            );
        }
        match self.placed.get_mut(v.0 as usize) {
            Some(p) if *p => self.problem(Some(v), "is defined twice"),
            Some(p) => *p = true,
            None => {}
        }
        if let Some(s) = self.visible.get_mut(v.0 as usize) {
            *s = true;
        }
    }
    fn hide(&mut self, from: &[bool]) {
        // what a region defined is not visible after it: restore the visibility before it, keeping the slots
        for (v, &before) in self.visible.iter_mut().zip(from) {
            *v = before;
        }
    }
    fn op(&mut self, v: ValueId) {
        let Some(def) = self.f.value(v) else { return };
        for o in def.op.operands() {
            self.uses(Some(v), o);
        }
        if !def.ty.is_valid() {
            self.problem(Some(v), format!("type {} has no lanes count of 1 to 4", def.ty));
        }
        match derived_type(&def.op, |o| self.ty(o)) {
            Ok(Some(t)) if t != def.ty => self.problem(
                Some(v),
                format!("states {} where its operation gives {t}", def.ty),
            ),
            Ok(_) => {}
            Err(e) => self.problem(Some(v), e),
        }
        if let Err(e) = stated(&def.op, def.ty, self.f, self.interface) {
            self.problem(Some(v), e);
        }
        if matches!(
            def.op,
            Op::Sample {
                level: Level::Auto | Level::Bias(_),
                ..
            }
        ) && self.stage != Stage::Pixel
        {
            self.problem(Some(v), "an implicit-level sample outside a pixel program");
        }
        if matches!(def.op, Op::Unary(Unary::Derivative(..), _)) && self.stage != Stage::Pixel {
            self.problem(Some(v), "a derivative outside a pixel program");
        }
        let (stage, patch) = (self.stage, self.patch);
        let wrong = match &def.op {
            Op::LoadInputAt(..) if !matches!(stage, Stage::Geometry | Stage::Hull | Stage::Domain) => {
                Some("a control point or vertex input outside a geometry, hull or domain program")
            }
            Op::LoadOutputAt(..) if !patch => {
                Some("an output control point read outside a patch constant function")
            }
            Op::LoadPatch(..) if !(stage == Stage::Domain || patch) => {
                Some("a patch constant read outside a domain program or a patch constant function")
            }
            Op::Builtin(Builtin::OutputControlPoint) if stage != Stage::Hull || patch => {
                Some("the output control point outside a hull program's main function")
            }
            Op::Builtin(Builtin::PhaseInstance) if !patch => {
                Some("a phase instance outside a patch constant function")
            }
            Op::Builtin(Builtin::PrimitiveId)
                if !matches!(stage, Stage::Geometry | Stage::Hull | Stage::Domain) =>
            {
                Some("the primitive id builtin outside a geometry, hull or domain program")
            }
            Op::Builtin(Builtin::GsInstance) if stage != Stage::Geometry => {
                Some("the geometry instance outside a geometry program")
            }
            Op::Builtin(Builtin::DomainLocation) if stage != Stage::Domain => {
                Some("the domain location outside a domain program")
            }
            Op::Wave(Wave::QuadLane(..) | Wave::QuadSwap(..))
                if !matches!(stage, Stage::Pixel | Stage::Compute) =>
            {
                Some("a quad read outside a pixel or compute program")
            }
            _ => None,
        };
        if let Some(w) = wrong {
            self.problem(Some(v), w);
        }
    }
    fn node(&mut self, n: &Node) {
        match n {
            Node::Value(v) => {
                self.op(*v);
                self.define(*v, false);
            }
            Node::Demote if self.stage != Stage::Pixel => {
                self.problem(None, "a demote outside a pixel program");
            }
            Node::Demote => {}
            Node::StoreVar(var, value) => {
                self.uses(None, *value);
                self.writable(*var, *value);
                match (self.f.vars.get(var.0 as usize), self.ty(*value)) {
                    (Some(d), Some(t)) if d.ty == t => {}
                    (Some(d), Some(t)) => {
                        let m = format!("stores {t} into variable {} of {}", d.name, d.ty);
                        self.problem(Some(*value), m);
                    }
                    _ => self.problem(
                        Some(*value),
                        format!("store to variable {}, which does not exist", var.0),
                    ),
                }
            }
            Node::StoreElement(var, i, value, _) => {
                self.uses(None, *i);
                self.uses(None, *value);
                self.writable(*var, *value);
                match self.f.vars.get(var.0 as usize) {
                    Some(d) if d.len.is_none() => {
                        let m = format!("an element store to {}, which is not an array", d.name);
                        self.problem(Some(*value), m);
                    }
                    Some(d) if Some(d.ty) != self.ty(*value) => {
                        let m = format!(
                            "stores {:?} into an element of {} of {}",
                            self.ty(*value),
                            d.name,
                            d.ty
                        );
                        self.problem(Some(*value), m);
                    }
                    Some(_) => {}
                    None => self.problem(
                        Some(*value),
                        format!("store to variable {}, which does not exist", var.0),
                    ),
                }
                if let Some(t) = self.ty(*i)
                    && let Err(e) = index_type(t, "an element index")
                {
                    self.problem(Some(*i), e);
                }
            }
            Node::StoreBuffer {
                resource: r,
                address,
                value,
            } => {
                for v in n.values() {
                    self.uses(None, v);
                }
                let t = self.ty(*value);
                let mut wrong = Vec::new();
                match resource(self.interface, *r) {
                    Ok(Resource {
                        kind:
                            ResourceKind::Storage {
                                target: StorageTarget::Buffer(_),
                                ..
                            },
                        ..
                    }) => {}
                    Ok(res) => wrong.push(format!("a buffer store to {:?}", res.kind)),
                    Err(e) => wrong.push(e),
                }
                if let Some(i) = address.index.or(r.index)
                    && let Some(it) = self.ty(i)
                    && let Err(e) = index_type(it, "a buffer index")
                {
                    wrong.push(e);
                }
                if t.is_none_or(|t| t.scalar.bits() != 32 || t.scalar == Scalar::Bool) {
                    wrong.push(format!("a buffer store of {t:?}"));
                }
                for w in wrong {
                    self.problem(Some(*value), w);
                }
            }
            Node::StoreTexel { image, coord, value } => {
                for v in n.values() {
                    self.uses(None, v);
                }
                let want = match resource(self.interface, *image).map(|r| &r.kind) {
                    Ok(ResourceKind::Storage {
                        target: StorageTarget::Image { dim, arrayed, sample },
                        ..
                    }) => Ok((
                        dims(*dim).saturating_add(u8::from(*arrayed)),
                        sample_scalar(*sample),
                    )),
                    Ok(ResourceKind::Storage {
                        target: StorageTarget::Buffer(BufferKind::Typed(v)),
                        ..
                    }) => Ok((1, value_type(*v).scalar)),
                    Ok(k) => Err(format!("a texel store to {k:?}")),
                    Err(e) => Err(e),
                };
                match want {
                    Ok((lanes, scalar)) => {
                        if let Some(c) = self.ty(*coord)
                            && let Err(e) = int_coord(c, lanes, "a texel coordinate")
                        {
                            self.problem(Some(*coord), e);
                        }
                        if self.ty(*value) != Some(Type::vector(scalar, 4)) {
                            let m = format!(
                                "a texel store of {:?} where vec4<{}> is wanted",
                                self.ty(*value),
                                scalar.name()
                            );
                            self.problem(Some(*value), m);
                        }
                    }
                    Err(e) => self.problem(Some(*value), e),
                }
            }
            Node::StorePatch(slot, mask, value) => {
                self.uses(None, *value);
                if !self.patch {
                    self.problem(
                        Some(*value),
                        "a patch constant store outside a patch constant function",
                    );
                }
                match patch_constants(self.interface)
                    .get(*slot as usize)
                    .map(|o| masked_type(o.value, *mask))
                {
                    Some(Ok(t)) if Some(t) == self.ty(*value) => {}
                    Some(Ok(t)) => {
                        let m = format!(
                            "patch constant {slot} lanes {mask:#06b} of {t} take {:?}",
                            self.ty(*value)
                        );
                        self.problem(Some(*value), m);
                    }
                    Some(Err(e)) => self.problem(Some(*value), format!("patch constant {slot}: {e}")),
                    None => self.problem(
                        Some(*value),
                        format!("patch constant slot {slot} is not in the interface"),
                    ),
                }
            }
            // a storage fence alone orders one invocation's memory operations, which any stage may
            Node::Barrier(b) if (b.execution || b.workgroup) && !self.stage.has_thread_groups() => {
                self.problem(
                    None,
                    "a workgroup barrier outside a compute, mesh or amplification program",
                );
            }
            Node::Barrier(b) if *b == Barrier::default() => self.problem(None, "a barrier of nothing"),
            Node::Barrier(_) => {}
            Node::Emit(_) | Node::Cut(_) if self.stage != Stage::Geometry => {
                self.problem(None, "a vertex emitted or a strip cut outside a geometry program");
            }
            Node::Emit(_) | Node::Cut(_) => {}
            Node::DispatchMesh { .. } if self.stage != Stage::Amplification => {
                self.problem(None, "a mesh launch outside an amplification program");
            }
            Node::DispatchMesh { groups, payload } => {
                self.uses(None, *groups);
                if self.ty(*groups) != Some(Type::vector(Scalar::U32, 3)) {
                    self.problem(
                        Some(*groups),
                        "a mesh launch of other thread groups than a vec3<u32>",
                    );
                }
                let bytes = match self.interface.facts {
                    StageFacts::Amplification(a) => a.payload,
                    _ => 0,
                };
                match self.f.vars.get(payload.0 as usize) {
                    None => self.problem(
                        None,
                        format!("a mesh launch of variable {}, which does not exist", payload.0),
                    ),
                    Some(d) if d.space == crate::Space::Payload => {
                        self.problem(
                            None,
                            format!("a mesh launch of {}, a mesh program's task payload", d.name),
                        );
                    }
                    Some(d) if d.ty != Type::U32 || d.len != Some(bytes.div_ceil(4)) => {
                        let m = format!(
                            "a mesh launch of {}, not the {} u32 words of the {bytes}-byte payload the facts state",
                            d.name,
                            bytes.div_ceil(4)
                        );
                        self.problem(None, m);
                    }
                    Some(_) => {}
                }
            }
            Node::SetMeshCounts { .. } | Node::StoreVertex { .. } | Node::StoreIndices { .. }
                if self.stage != Stage::Mesh =>
            {
                self.problem(None, "a meshlet written outside a mesh program");
            }
            Node::SetMeshCounts { vertices, primitives } => {
                for (v, what) in [(vertices, "vertex"), (primitives, "primitive")] {
                    self.uses(None, *v);
                    if self.ty(*v) != Some(Type::U32) {
                        let m = format!("a mesh {what} count of {:?}", self.ty(*v));
                        self.problem(Some(*v), m);
                    }
                }
            }
            Node::StoreVertex {
                vertex,
                slot,
                mask,
                value,
            } => {
                self.uses(None, *vertex);
                self.uses(None, *value);
                if self.ty(*vertex) != Some(Type::U32) {
                    let m = format!("a mesh vertex numbered by {:?}", self.ty(*vertex));
                    self.problem(Some(*vertex), m);
                }
                match self
                    .interface
                    .outputs
                    .get(*slot as usize)
                    .map(|o| masked_type(o.value, *mask))
                {
                    Some(Ok(t)) if Some(t) == self.ty(*value) => {}
                    Some(Ok(t)) => {
                        let m = format!(
                            "vertex output {slot} lanes {mask:#06b} of {t} take {:?}",
                            self.ty(*value)
                        );
                        self.problem(Some(*value), m);
                    }
                    Some(Err(e)) => self.problem(Some(*value), format!("vertex output {slot}: {e}")),
                    None => self.problem(
                        Some(*value),
                        format!("vertex output slot {slot} is not in the interface"),
                    ),
                }
            }
            Node::StoreIndices { primitive, indices } => {
                self.uses(None, *primitive);
                self.uses(None, *indices);
                if self.ty(*primitive) != Some(Type::U32) {
                    let m = format!("a mesh primitive numbered by {:?}", self.ty(*primitive));
                    self.problem(Some(*primitive), m);
                }
                if let StageFacts::Mesh(m) = &self.interface.facts {
                    let lanes = u8::try_from(m.topology.indices()).unwrap_or(0);
                    if self.ty(*indices) != Some(Type::vector(Scalar::U32, lanes)) {
                        let m = format!(
                            "a {:?} primitive's {lanes} indices given as {:?}",
                            m.topology,
                            self.ty(*indices)
                        );
                        self.problem(Some(*indices), m);
                    }
                }
            }
            Node::Switch {
                label,
                selector,
                cases,
                default,
                results,
            } => {
                self.uses(None, *selector);
                if !matches!(self.ty(*selector), Some(Type::U32 | Type::I32)) {
                    self.problem(Some(*selector), "a switch selector that is not a 32-bit integer");
                }
                let mut seen: Vec<u32> = Vec::new();
                for c in cases {
                    if c.values.is_empty() {
                        self.problem(None, "a switch case with no values");
                    }
                    for v in &c.values {
                        if seen.contains(v) {
                            self.problem(None, format!("switch value {v} in two cases"));
                        }
                        seen.push(*v);
                    }
                }
                let before = self.visible.clone();
                self.labels.push((*label, results.clone(), None));
                for r in cases.iter().map(|c| &c.body).chain(core::iter::once(default)) {
                    self.region(r, Some(results));
                    self.hide(&before);
                }
                self.labels.pop();
                for r in results {
                    self.define(*r, true);
                }
            }
            Node::StoreOutput(slot, mask, value) => {
                self.uses(None, *value);
                match self
                    .interface
                    .outputs
                    .get(*slot as usize)
                    .map(|o| masked_type(o.value, *mask))
                {
                    Some(Ok(t)) if Some(t) == self.ty(*value) => {}
                    Some(Ok(t)) => {
                        let m = format!(
                            "output {slot} lanes {mask:#06b} of {t} take {:?}",
                            self.ty(*value)
                        );
                        self.problem(Some(*value), m);
                    }
                    Some(Err(e)) => self.problem(Some(*value), format!("output {slot}: {e}")),
                    None => self.problem(
                        Some(*value),
                        format!("output slot {slot} is not in the interface"),
                    ),
                }
            }
            Node::If {
                cond,
                accept,
                reject,
                results,
            } => {
                self.uses(None, *cond);
                if self.ty(*cond) != Some(Type::BOOL) {
                    self.problem(Some(*cond), "an if condition that is not a bool");
                }
                let before = self.visible.clone();
                self.region(accept, Some(results));
                self.hide(&before);
                self.region(reject, Some(results));
                self.hide(&before);
                for r in results {
                    self.define(*r, true);
                }
            }
            Node::Block { label, body, results } => {
                let before = self.visible.clone();
                self.labels.push((*label, results.clone(), None));
                self.region(body, Some(results));
                self.labels.pop();
                self.hide(&before);
                for r in results {
                    self.define(*r, true);
                }
            }
            Node::Loop {
                label,
                params,
                inits,
                body,
                results,
            } => {
                if params.len() != inits.len() {
                    self.problem(None, "a loop with as many initial values as parameters");
                }
                for (p, i) in params.iter().zip(inits) {
                    self.uses(Some(*p), *i);
                    if self.ty(*p) != self.ty(*i) {
                        self.problem(
                            Some(*p),
                            "a loop parameter of another type than its initial value",
                        );
                    }
                }
                let before = self.visible.clone();
                for p in params {
                    self.define(*p, true);
                }
                self.labels.push((*label, results.clone(), Some(params.clone())));
                self.region(body, Some(results));
                self.labels.pop();
                self.hide(&before);
                for r in results {
                    self.define(*r, true);
                }
            }
        }
    }
    fn args(&mut self, what: &str, given: &[ValueId], wanted: &[ValueId]) {
        for g in given {
            self.uses(None, *g);
        }
        let types = |c: &Self, vs: &[ValueId]| vs.iter().map(|v| c.ty(*v)).collect::<Vec<_>>();
        if types(self, given) != types(self, wanted) {
            self.problem(
                None,
                format!(
                    "{what} gives {} values that do not match the {} it fills",
                    given.len(),
                    wanted.len()
                ),
            );
        }
    }
    fn region(&mut self, r: &Region, results: Option<&Vec<ValueId>>) {
        for n in &r.nodes {
            self.node(n);
        }
        match &r.end {
            None => self.problem(None, "a region with no end"),
            Some(End::Yield(vs)) => {
                let wanted = results.cloned().unwrap_or_default();
                self.args("a yield", vs, &wanted);
            }
            Some(End::Br(l, vs)) => match self.labels.iter().rev().find(|(x, ..)| x == l).cloned() {
                Some((_, results, _)) => self.args("a break", vs, &results),
                None => self.problem(
                    None,
                    format!("a break to label {}, which no enclosing construct has", l.0),
                ),
            },
            Some(End::Continue(l, vs)) => match self.labels.iter().rev().find(|(x, ..)| x == l).cloned() {
                Some((_, _, Some(params))) => self.args("a continue", vs, &params),
                Some(_) => self.problem(None, format!("a continue to label {}, which is not a loop", l.0)),
                None => self.problem(
                    None,
                    format!("a continue to label {}, which no enclosing construct has", l.0),
                ),
            },
            Some(End::Kill) if self.stage != Stage::Pixel => {
                self.problem(None, "a kill outside a pixel program")
            }
            Some(End::Return | End::Kill | End::Unreachable) => {}
        }
    }
    fn cfg(&mut self, blocks: &[Block]) {
        let n = blocks.len();
        if n == 0 {
            self.problem(None, "a graph with no blocks");
            return;
        }
        let succ = |b: &Block| -> Vec<u32> { b.term.targets().iter().map(|t| t.block).collect() };
        let idom = dominators(blocks.iter().map(succ).collect());
        // a value is visible in a block when its own block dominates it (earlier in the same block)
        let mut home = vec![None; self.f.values.len()];
        for (i, b) in blocks.iter().enumerate() {
            for v in b.params.iter().chain(b.nodes.iter().filter_map(|n| match n {
                Node::Value(v) => Some(v),
                _ => None,
            })) {
                if let Some(h) = home.get_mut(v.0 as usize) {
                    *h = Some(i);
                }
            }
        }
        // every path from the entry to a block it does not reach passes every block (none does): each dominates it.
        // Its code is still checked, by every rule but this one
        let dominates = |a: usize, mut b: usize| {
            if b != 0 && idom.get(b).copied().flatten().is_none() {
                return true;
            }
            loop {
                if a == b {
                    return true;
                }
                match idom.get(b).copied().flatten() {
                    Some(p) if p != b => b = p,
                    _ => return false,
                }
            }
        };
        for (i, b) in blocks.iter().enumerate() {
            let mut local = Vec::new();
            let seen = |c: &mut Self, local: &Vec<ValueId>, at: Option<ValueId>, v: ValueId| {
                let ok = local.contains(&v)
                    || home
                        .get(v.0 as usize)
                        .copied()
                        .flatten()
                        .is_some_and(|h| h != i && dominates(h, i));
                if !ok {
                    c.problem(at, format!("uses %{}, which does not dominate it", v.0));
                }
            };
            for p in &b.params {
                self.define(*p, true);
                local.push(*p);
            }
            for node in &b.nodes {
                match node {
                    Node::Value(v) => {
                        if let Some(def) = self.f.value(*v) {
                            for o in def.op.operands() {
                                seen(self, &local, Some(*v), o);
                            }
                        }
                        // operand visibility was checked above; mark them so `op` checks only the rules
                        let saved = self.visible.clone();
                        self.visible.iter_mut().for_each(|s| *s = true);
                        self.op(*v);
                        self.visible = saved;
                        self.define(*v, false);
                        local.push(*v);
                    }
                    Node::If { .. } | Node::Block { .. } | Node::Loop { .. } | Node::Switch { .. } => {
                        self.problem(None, "a construct inside a graph block")
                    }
                    _ => {
                        for x in node.values() {
                            seen(self, &local, None, x);
                        }
                        let saved = self.visible.clone();
                        self.visible.iter_mut().for_each(|s| *s = true);
                        self.node(node);
                        self.visible = saved;
                    }
                }
            }
            match &b.term {
                Terminator::Branch { cond, .. } => {
                    seen(self, &local, None, *cond);
                    if self.ty(*cond) != Some(Type::BOOL) {
                        self.problem(Some(*cond), "a branch condition that is not a bool");
                    }
                }
                Terminator::Switch { selector, cases, .. } => {
                    seen(self, &local, None, *selector);
                    if !matches!(self.ty(*selector), Some(Type::U32 | Type::I32)) {
                        self.problem(Some(*selector), "a switch selector that is not a 32-bit integer");
                    }
                    let mut values: Vec<u32> = cases.iter().map(|(v, _)| *v).collect();
                    values.sort_unstable();
                    if values.windows(2).any(|w| w.first() == w.get(1)) {
                        self.problem(None, "a switch value with two targets");
                    }
                }
                Terminator::Kill if self.stage != Stage::Pixel => {
                    self.problem(None, "a kill outside a pixel program");
                }
                _ => {}
            }
            let targets = b.term.targets();
            for t in targets {
                match blocks.get(t.block as usize) {
                    None => self.problem(None, format!("a branch to block {}, of {n}", t.block)),
                    Some(to) => {
                        for a in &t.args {
                            seen(self, &local, None, *a);
                        }
                        let types = |vs: &[ValueId]| vs.iter().map(|v| self.ty(*v)).collect::<Vec<_>>();
                        if types(&t.args) != types(&to.params) {
                            self.problem(
                                None,
                                format!(
                                    "a branch to block {} whose arguments do not match its parameters",
                                    t.block
                                ),
                            );
                        }
                    }
                }
            }
        }
    }
}

/// Immediate dominators of a graph given by successor lists, block 0 the entry; `None` for unreachable blocks (and
/// the entry is its own).
pub fn dominators(succ: Vec<Vec<u32>>) -> Vec<Option<usize>> {
    let n = succ.len();
    // reverse post-order
    let mut order = Vec::new();
    let mut seen = vec![false; n];
    let mut stack = vec![(0usize, 0usize)];
    if let Some(s) = seen.get_mut(0) {
        *s = true;
    }
    while let Some((b, i)) = stack.pop() {
        let next = succ.get(b).and_then(|s| s.get(i)).map(|&x| x as usize);
        match next {
            Some(x) => {
                stack.push((b, i.saturating_add(1)));
                if x < n && !seen.get(x).copied().unwrap_or(true) {
                    if let Some(s) = seen.get_mut(x) {
                        *s = true;
                    }
                    stack.push((x, 0));
                }
            }
            None => order.push(b),
        }
    }
    order.reverse();
    let mut rpo = vec![usize::MAX; n];
    for (i, &b) in order.iter().enumerate() {
        if let Some(r) = rpo.get_mut(b) {
            *r = i;
        }
    }
    let mut preds = vec![Vec::new(); n];
    for (b, s) in succ.iter().enumerate() {
        for &x in s {
            if let Some(p) = preds.get_mut(x as usize) {
                p.push(b);
            }
        }
    }
    let mut idom: Vec<Option<usize>> = vec![None; n];
    if let Some(e) = idom.get_mut(0) {
        *e = Some(0);
    }
    let intersect = |idom: &Vec<Option<usize>>, mut a: usize, mut b: usize| {
        let r = |x: usize| rpo.get(x).copied().unwrap_or(usize::MAX);
        while a != b {
            while r(a) > r(b) {
                a = idom.get(a).copied().flatten().unwrap_or(0);
            }
            while r(b) > r(a) {
                b = idom.get(b).copied().flatten().unwrap_or(0);
            }
        }
        a
    };
    let mut changed = true;
    while changed {
        changed = false;
        for &b in order.iter().skip(1) {
            let mut new = None;
            for &p in preds.get(b).map(Vec::as_slice).unwrap_or(&[]) {
                if idom.get(p).copied().flatten().is_some() {
                    new = Some(match new {
                        None => p,
                        Some(q) => intersect(&idom, p, q),
                    });
                }
            }
            if new.is_some() && idom.get(b).copied().flatten() != new {
                if let Some(slot) = idom.get_mut(b) {
                    *slot = new;
                }
                changed = true;
            }
        }
    }
    idom
}

fn check_function(p: &Program, f: &Function, patch: bool, problems: &mut Vec<Problem>) {
    let mut c = Checker {
        f,
        interface: &p.interface,
        stage: p.stage,
        visible: vec![false; f.values.len()],
        placed: vec![false; f.values.len()],
        labels: Vec::new(),
        problems: Vec::new(),
        patch,
    };
    for v in &f.vars {
        if v.space == crate::Space::Workgroup && !p.stage.has_thread_groups() {
            c.problem(
                None,
                format!(
                    "workgroup variable {} outside a compute, mesh or amplification program",
                    v.name
                ),
            );
        }
        if v.space == crate::Space::Payload {
            let bytes = match p.interface.facts {
                StageFacts::Mesh(m) => Some(m.payload),
                _ => None,
            };
            match bytes {
                None => c.problem(None, format!("task payload {} outside a mesh program", v.name)),
                Some(b) if v.ty != Type::U32 || v.len != Some(b.div_ceil(4)) || b == 0 => c.problem(
                    None,
                    format!(
                        "task payload {} is not the {} u32 words of the {b}-byte payload the facts state",
                        v.name,
                        b.div_ceil(4)
                    ),
                ),
                Some(_) if !v.init.is_empty() => {
                    c.problem(None, format!("task payload {} has a start of its own", v.name));
                }
                Some(_) => {}
            }
        }
        if !v.init.is_empty() && v.init.len() != v.len.map_or(1, |n| n as usize) {
            c.problem(
                None,
                format!("variable {} starts with {} elements", v.name, v.init.len()),
            );
        }
        if v.len == Some(0) {
            c.problem(None, format!("array variable {} has no elements", v.name));
        }
    }
    match &f.body {
        Body::Regions(r) => {
            c.region(r, None);
            if !matches!(
                r.end,
                Some(End::Return | End::Kill | End::Unreachable | End::Yield(_))
            ) {
                c.problem(None, "the body ends in an exit to a construct it is not in");
            }
        }
        Body::Cfg(blocks) => c.cfg(blocks),
    }
    problems.extend(c.problems);
}

/// Check a program: every problem found.
pub fn validate(p: &Program) -> Result<(), Vec<Problem>> {
    let mut problems = Vec::new();
    if p.stage != p.interface.stage {
        problems.push(Problem {
            value: None,
            message: "the program's stage is not its interface's".into(),
        });
    }
    check_function(p, &p.entry, false, &mut problems);
    match (&p.patch, p.stage) {
        (Some(f), Stage::Hull) => check_function(p, f, true, &mut problems),
        (Some(_), _) => problems.push(Problem {
            value: None,
            message: "a patch constant function in a program that is not a hull program".into(),
        }),
        (None, Stage::Hull) => problems.push(Problem {
            value: None,
            message: "a hull program without its patch constant function".into(),
        }),
        (None, _) => {}
    }
    match (p.workgroup, p.stage.has_thread_groups()) {
        (Some(size), true) if size.contains(&0) => problems.push(Problem {
            value: None,
            message: "a thread group with no invocations along an axis".into(),
        }),
        (None, true) => problems.push(Problem {
            value: None,
            message: "a compute, mesh or amplification program without its thread group".into(),
        }),
        (Some(_), false) => problems.push(Problem {
            value: None,
            message: "a thread group in a program that is not a compute, mesh or amplification program"
                .into(),
        }),
        _ => {}
    }
    if problems.is_empty() {
        Ok(())
    } else {
        Err(problems)
    }
}