fuel-dispatch 0.19.1

Dispatch infrastructure for fuel: KernelBindingTable, per-backend registration wrappers, CompiledNode + compile_node, ExecutionPlan + compile_plan + resolve_kernel, FusedKernelRegistry, PipelinedExecutor. The layer that takes a graph and runs it against registered backends. Extracted from fuel-memory 2026-05-31.
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
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Kernel reference + op parameters + binding table. Phase 7.5 B1+B5.
//!
//! [`KernelRef`] is the uniform function-pointer type that every
//! dispatch wrapper matches. Backend-specific typed kernels live in
//! their backend crates (e.g. `fuel_cpu_backend::byte_kernels`); the
//! *wrapper* functions here in fuel-storage bridge the dispatch-
//! erased `Storage` to those typed kernels by matching on
//! `BackendStorage::Cpu(...)` etc.
//!
//! [`OpParams`] is the typed extras bag — one variant per op family
//! that needs auxiliary data beyond inputs and outputs. Most
//! elementwise ops use `OpParams::None`; reductions carry their
//! reduce dims; conv2d carries kernel/stride/padding; etc.
//!
//! [`KernelBindingTable`] (B5) maps
//! `(OpKind, SmallVec<[DType; N]>, BackendId) -> KernelRef`. The dtype
//! list carries per-operand types — inputs in order, then outputs —
//! so mixed-precision ops (e.g. `Cast: src→dst`) and same-dtype ops
//! (e.g. `Add: [T, T, T]`) share the same key shape. Backends register
//! their dispatch wrappers via the `dispatch::register_*` functions;
//! op-builder methods consult the table at DAG construction time using
//! `Graph::target_backend(id)` as the BackendId key.
//!
//! ## Architecture (cycle-avoidance)
//!
//! - Backend crates (`fuel-cpu-backend`, …): typed kernels on their
//!   concrete storage types (`CpuStorageBytes`, …). No fuel-storage
//!   dependency.
//! - fuel-storage (this crate): dispatch wrappers that match
//!   `BackendStorage::Cpu(...)`, extract the typed storage, and
//!   call the backend's typed kernel. KernelBindingTable lives here.
//! - Backend crates depend on fuel-storage? No — only the wrappers
//!   do. fuel-storage already depends on backend crates for variant
//!   types; this round-trip closes naturally.

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;

use fuel_graph::QuantType;
use fuel_graph::registry::FusedOpId;
use fuel_ir::conv::{ParamsConv1D, ParamsConvTranspose1D};
use fuel_ir::dispatch::OpKind;
use fuel_ir::probe::BackendId;
use fuel_ir::{DType, Error, Layout, Result};
use smallvec::SmallVec;

/// Inline capacity for the per-operand dtype list in the binding-table
/// key. 8 covers every op currently in flight without spilling to heap:
/// PagedAttn (q + 2 caches + block_table + context_lens + alibi + out)
/// at 7 entries is the worst case in inference; mixed-precision matmul
/// is ≤ 4. Bumping later is one constant change.
pub type KernelDTypes = SmallVec<[DType; 8]>;

/// One entry of the coverage diagnostic: `(backend, op, operand dtypes)`.
///
/// Named rather than spelled out because the bare 3-tuple appears in a return
/// position where it was unreadable, and `#[allow(type_complexity)]` would
/// have asserted the tuple is fine as-is. It is not — the alias is the fix and
/// the lint was right.
///
/// NOT `BindingKey`: that name is already a distinct `pub enum` in this
/// module (`Static` / …). The compiler caught the collision; the lesson is
/// that a type alias introduced for a lint still needs a name check.
pub type CoverageBinding = (BackendId, OpKind, Vec<DType>);

/// Per-binding capability flags. Today carries one flag — `strided_input`
/// — that signals "this kernel walks input strides explicitly and so
/// can consume non-contiguous input layouts (including stride-0
/// broadcast axes) without auto-Contiguize materializing them first."
/// The executor's contiguize gate consults this to skip the materialize
/// step for capable kernels, so broadcast/transpose/slice layouts can
/// reach the kernel as metadata-only views.
///
/// Default is the conservative all-false; binding sites opt in via
/// [`KernelBindingTable::register_with_caps`]. Forward-extensible by
/// adding fields (no enum/bitflags churn).
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct KernelCaps {
    /// Kernel handles non-contiguous input layouts directly (consumes
    /// the strides side-channel from `KernelRef::layouts`). When true,
    /// the executor passes non-contiguous inputs through unchanged
    /// instead of running auto-Contiguize first. Inputs with non-zero
    /// `start_offset` still go through auto-Contiguize today (slicing
    /// the device buffer to honor offset is a separate concern).
    pub strided_input: bool,
    /// This kernel BAKES a specific broadcast pattern into its body — a
    /// `broadcast_stride0: required` operand it reads as stride-0 (hoisting
    /// `in[0]` / dropping the bcast-axis stride terms). It is therefore NOT
    /// a generic dense-safe kernel: a dense operand routed here would be
    /// read WRONG (element 0 repeatedly). The realize pick
    /// ([`KernelBindingTable::lookup_with_caps`]) SKIPS such a sibling so a dense operand
    /// is never routed into a broadcast-baked kernel — the exclusion is
    /// order-independent (it does not rest on the generic contract being
    /// registered first). Selecting it CORRECTLY, for an operand genuinely
    /// stride-0 on exactly the required axes, is the layout-aware follow-on
    /// (path 1b) that also teaches the contiguize gate; today the safe
    /// behavior is to exclude it, so a `required`-broadcast contract is a
    /// present-but-never-mis-selected sibling. Defaults `false` — every
    /// existing binding is unaffected (the pick stays first-registered).
    pub requires_broadcast: bool,
}

impl KernelCaps {
    /// All flags off. Equivalent to `Default::default()`; provided as a
    /// const for use in const-context registration tables.
    pub const fn empty() -> Self {
        Self {
            strided_input: false,
            requires_broadcast: false,
        }
    }

    /// Just `strided_input` on. Ergonomic for binary/unary registrations
    /// that opt in to the wrapper-side broadcast path.
    pub const fn strided_input() -> Self {
        Self {
            strided_input: true,
            requires_broadcast: false,
        }
    }
}

use fuel_memory::Storage;

/// Uniform function-pointer signature for per-backend op kernels.
///
/// Inputs are passed as a slice of `&Storage` to handle multi-input
/// ops (binary, ternary, custom). Outputs are passed as a slice of
/// `&mut Storage` to handle multi-output ops (topk, var_mean,
/// custom). Most ops use single-element slices.
///
/// `layouts` is a side-channel that parallels `inputs.append(outputs)`
/// — i.e. `layouts[0..inputs.len()]` are the input layouts in order,
/// followed by `layouts[inputs.len()..]` for the output layouts. This
/// is the load-bearing primitive for stride-aware kernels (broadcast,
/// transpose, gather-from-strided). Kernels that only support
/// contiguous-equal-shape inputs (today's default) can ignore
/// `layouts` because the executor's auto-Contiguize pass guarantees
/// every input is contiguous before this is called; the layout slice
/// then carries `Layout::contiguous(shape)` entries that are useful
/// for shape inference but redundant with `Storage.len_bytes / dtype`.
///
/// `OpParams` carries op-family-specific extras (reduce dims,
/// conv2d geometry, etc.). Most kernels match a specific variant;
/// mismatches are programming bugs that the dispatch resolver
/// must prevent. Pure layout/shape duplication that used to live in
/// `OpParams` (e.g. the old `Reduce::input_layout`) now flows through
/// `layouts` instead.
///
/// **Output Storage is pre-allocated** by the executor before the
/// kernel is called. Kernels write into the pre-allocated bytes;
/// they never allocate.
///
/// ## Multi-output kernels (Option C, Session 5)
///
/// Multi-output ops (e.g. SelectiveScan returning `(y, last_state)`)
/// emit ONE `KernelRef`. The contract:
///
/// - `outputs.len() == 1`. The single `Arc<RwLock<Storage>>` is the
///   producer's *bundled* Storage — its `bundle()` returns
///   `Some(&[OutputView; N])` describing each logical slot's
///   `dtype`/`shape`/`layout`/`byte_offset` inside the bundle's
///   underlying byte buffer.
/// - The kernel writes each logical output into its slot's byte
///   range by acquiring a single write lock on the output and
///   striding by the slot's `byte_offset`. The bundle metadata is
///   the authoritative per-slot spec — `outputs[0].read().unwrap()
///   .bundle().expect("bundled storage").get(slot_idx)` is the
///   canonical access path.
/// - Per-slot dtype tags do NOT travel through the kernel's
///   `KernelDTypes` key (which describes inputs + the bundle's
///   primary dtype only); the bundle metadata IS the per-slot
///   dispatch info.
/// - The bundle is pre-allocated by the executor via
///   `allocate_bundled_storage(device, &output_views_spec)` (see
///   `fuel_backend_contract::storage::allocate_bundled_storage`). Kernels
///   never allocate; they only fill bytes.
///
/// Consumers of multi-output producers are NOT multi-output kernels —
/// they're `Op::View` (zero-copy slot projection) or `Op::ViewOwned`
/// (independent slot buffer), which the executor handles directly
/// without invoking a kernel.
///
/// **Production-correct**: kernels return `Result`, never panic.
pub type KernelRef = fn(
    inputs: &[Arc<RwLock<Storage>>],
    outputs: &mut [Arc<RwLock<Storage>>],
    layouts: &[Layout],
    params: &OpParams,
) -> Result<()>;

/// How many rows a [`OpParams::Matmul`] computes — the data-determined-M
/// state for sparse-MoE / capacity-buffer matmuls. `m` in `OpParams::Matmul`
/// is always the row **capacity** (buffer/stride/size); this says how many
/// of those rows to actually compute (`<= m`), the rest left zeroed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MatmulM {
    /// Compute every one of the `m` rows — the universal static default.
    All,
    /// Compute exactly this many rows of the `m`-row capacity buffer. Set
    /// when the count is known at compile (an input-determined extent) or
    /// after a [`Self::Deferred`] is resolved at execute.
    Rows(usize),
    /// The row count is **data-determined** (a producer binds it mid-pass,
    /// e.g. `Op::NonZeroIndices`'s per-expert token count) — resolve at
    /// execute from `produced_syms`, then patch to [`Self::Rows`].
    Deferred(fuel_ir::DynScalar),
}

/// Typed extras bag passed to kernels via [`KernelRef`]. One variant
/// per op family that needs auxiliary parameters; `None` for
/// parameter-less ops (most elementwise unary and binary).
#[derive(Debug, Clone)]
pub enum OpParams {
    /// Op needs no auxiliary parameters. Used by elementwise
    /// unary (relu, neg, sqr, …), elementwise binary (add, mul,
    /// sub, div, …), shape-only ops (reshape, transpose), etc.
    None,

    /// Reduction (sum, max, mean, …) along specific dims. The input
    /// tensor's [`Layout`] flows through the new `layouts` side-channel
    /// (`layouts[0]`) on `KernelRef`, so this variant carries only the
    /// op-specific extras: which dims to reduce and the keepdim flag.
    ///
    /// `dims` is the sorted list of dims to reduce; `keepdim`
    /// controls whether reduced dims are retained as size-1 in
    /// the output (today fuel-graph never asks for keepdim, but
    /// the field is reserved for the future).
    Reduce { dims: Vec<usize>, keepdim: bool },

    /// Matrix multiplication. Carries the dimensions explicitly
    /// because [`Storage`](fuel_memory::Storage) only holds bytes + dtype;
    /// the kernel needs the batch shape and `(m, n, k)` to walk
    /// inputs and outputs.
    ///
    /// Shape contract: lhs `[..lhs_batch.., m, k]` @
    /// rhs `[..rhs_batch.., k, n]` → out `[..lhs_batch.., m, n]`.
    /// Per-axis the batch dims must either match or follow GQA-style
    /// divisibility (`lhs_dim > rhs_dim && lhs_dim % rhs_dim == 0`)
    /// — the kernel maps each lhs batch slot to the corresponding
    /// rhs slot via `rhs_axis_idx = lhs_axis_idx / n_rep_axis`.
    ///
    /// Equal-batch case: `lhs_batch_dims == rhs_batch_dims`. Rank-2
    /// case: both batch vectors are empty. Both work uniformly.
    ///
    /// Inputs are guaranteed contiguous by the executor's
    /// auto-Contiguize pass; transpose flags don't appear here
    /// because `Op::Transpose` is its own metadata-only op in
    /// fuel-graph.
    Matmul {
        lhs_batch_dims: Vec<usize>,
        rhs_batch_dims: Vec<usize>,
        /// The output/LHS row **capacity** (drives size checks + strides).
        m: usize,
        n: usize,
        k: usize,
        /// How many of the `m` rows to actually compute (data-determined-M
        /// sparse-MoE support). `MatmulM::All` for every ordinary matmul.
        m_compute: MatmulM,
    },

    /// 1D convolution geometry (forward path).
    Conv1D(ParamsConv1D),

    /// 2D convolution geometry (forward path). Carries the tuple-shaped
    /// stride/padding that fuel-graph's `Op::Conv2D` uses (asymmetric
    /// supported), plus the input/weight/output shapes the kernel
    /// needs to walk the multi-index. Storage holds only bytes +
    /// dtype, so spatial shapes flow through OpParams.
    ///
    /// Inputs: `x` shape `[N, Cin, Hin, Win]`, `weight` shape
    /// `[Cout, Cin/groups, Kh, Kw]`, optional `bias` shape `[Cout]`.
    /// Output: `[N, Cout, Hout, Wout]`.
    Conv2D {
        x_shape: [usize; 4],
        w_shape: [usize; 4],
        out_shape: [usize; 4],
        stride: (usize, usize),
        padding: (usize, usize),
        dilation: (usize, usize),
        groups: usize,
    },

    /// 1D transposed-convolution geometry.
    ConvTranspose1D(ParamsConvTranspose1D),

    /// 2D transposed-convolution geometry. Mirrors `OpParams::Conv2D`
    /// in shape (inline fields, asymmetric stride/padding/dilation,
    /// `groups`), with the additional `output_padding` parameter that
    /// transposed conv needs to disambiguate output spatial size.
    ///
    /// Inputs: `x` shape `[N, Cin, Hin, Win]`, `weight` shape
    /// `[Cin, Cout/groups, Kh, Kw]` (note transposed channel order
    /// vs Conv2D), optional `bias` shape `[Cout]`.
    /// Output: `[N, Cout, Hout, Wout]`.
    ConvTranspose2D {
        x_shape: [usize; 4],
        w_shape: [usize; 4],
        out_shape: [usize; 4],
        stride: (usize, usize),
        padding: (usize, usize),
        output_padding: (usize, usize),
        dilation: (usize, usize),
        groups: usize,
    },

    /// Sum-reduce a tensor to a smaller broadcast-compatible target
    /// shape. The kernel left-pads `output_shape` with 1s to match
    /// `input_shape`'s rank, then for each axis: if the padded output
    /// dim equals the input dim that axis carries through; if it's 1
    /// the axis is summed away.
    ReduceSumTo {
        input_shape: Vec<usize>,
        output_shape: Vec<usize>,
    },

    /// Max-reduce a tensor to a smaller broadcast-compatible target
    /// shape — the max-symmetric counterpart of `ReduceSumTo`. Same
    /// axis-alignment rules; per-axis reduction is `max` instead of
    /// `+`.
    ReduceMaxTo {
        input_shape: Vec<usize>,
        output_shape: Vec<usize>,
    },

    /// Backward of `ReduceMaxTo`. Inputs: `(x, upstream)` where
    /// `x.shape == input_shape` and `upstream.shape == output_shape`.
    /// Output: `grad_x` of `input_shape`. The kernel recomputes the
    /// forward max via `input_shape → output_shape` axis alignment,
    /// builds a tie-count mask, and routes upstream back to argmax
    /// positions (fair-share on ties).
    ReduceMaxToBackward {
        input_shape: Vec<usize>,
        output_shape: Vec<usize>,
    },

    /// Multi-head scaled-dot-product attention shape + math params.
    /// `q` is `[B, Hq, Sq, D]`, `k` and `v` are `[B, Hkv, Sk, D]`
    /// (Hkv ≤ Hq, GQA-divisible). Optional 4th input `alibi_slopes`
    /// has shape `[Hq]` (presence is implicit in `inputs.len() == 4`).
    ///
    /// `sk` is the **physical** K/V length axis (the allocated extent —
    /// used for strides + byte-length checks). `k_len` is the
    /// **logical** attended length (`k_len <= sk`): a kernel attends only
    /// the first `k_len` rows of the K/V buffers and bottom-right-aligns
    /// the causal mask at offset `k_len - sq` (Phase D symbolic extents:
    /// flash over a fixed-capacity KV-cache with a runtime live prefix).
    /// For the static path the lowering sets `k_len == sk`, so a kernel
    /// that loops `0..k_len` with the `k_len - sq` causal offset is
    /// byte-identical to the old `0..sk` / `kj > qi` form.
    FlashAttn {
        b: usize,
        hq: usize,
        hkv: usize,
        sq: usize,
        sk: usize,
        d: usize,
        k_len: usize,
        softmax_scale: f32,
        causal: bool,
        window_size_left: Option<usize>,
        window_size_right: Option<usize>,
        softcap: Option<f32>,
    },

    /// Paged-cache attention. `q` is `[B, Hq, Sq, D]`; `k_cache` and
    /// `v_cache` are `[num_blocks, block_size, Hkv, D]`. The 4th input
    /// is `block_table: [B, max_blocks_per_seq]` (U32 — physical block
    /// index per logical position). The 5th is `context_lens: [B]`
    /// (U32 — true context length per sequence). Optional 6th is
    /// `alibi_slopes: [Hq]`.
    PagedAttn {
        b: usize,
        hq: usize,
        hkv: usize,
        sq: usize,
        d: usize,
        block_size: usize,
        max_blocks_per_seq: usize,
        num_blocks: usize,
        softmax_scale: f32,
        softcap: Option<f32>,
    },

    /// Slice along a single dim with explicit start/end/step. The
    /// dim is implicit in the input Layout's relabeling for
    /// multi-dim slice; this variant covers the simple case.
    Slice {
        dim: usize,
        start: usize,
        end: usize,
        step: usize,
    },

    // (Earlier `OpParams::Pad { padding: Vec<(usize, usize)>, fill_bytes: Vec<u8> }`
    // was a speculative multi-dim shape with no consumers. The single-
    // dim shape that Op::Pad actually emits lives at the bottom of this
    // enum; if multi-dim padding lands later, it can extend either
    // shape additively.)
    /// Cast input dtype → target dtype. The target lives on the
    /// output Storage's `dtype` field; this variant signals the
    /// op family without requiring a re-read.
    Cast,

    /// Affine transformation `y = mul * x + add`. Used by
    /// `Tensor::affine` / `scale_and_shift`.
    Affine { mul: f64, add: f64 },

    /// Element-wise clamp: `y = clamp(x, min, max)`.
    Clamp { min: f64, max: f64 },

    /// Element-wise integer power: `y = x.powi(exp)`.
    PowI { exp: i32 },

    /// Concatenate N inputs along one dim. The kernel needs the
    /// outer/inner element counts (product of dims before/after
    /// the concat dim) plus each input's size along the concat
    /// dim — that's all that distinguishes a concat from a
    /// sequence of slab-copies. Order matches `Node::inputs`.
    ///
    /// `axis` is the original concat dim in the output's shape;
    /// kernels that want to walk strided rank-N inputs need it
    /// to build the per-axis stride mask (the outer/inner factoring
    /// alone loses the position info).
    Concat {
        /// Product of output dims before the concat dim.
        outer_count: usize,
        /// Per-input size along the concat dim (length = N inputs).
        input_dim_sizes: Vec<usize>,
        /// Product of output dims after the concat dim.
        inner_count: usize,
        /// Original concat axis index in the output's rank-N shape.
        axis: usize,
    },

    /// Softmax along the last dim. The kernel walks
    /// `outer_count` rows of `last_dim` elements each.
    SoftmaxLastDim { outer_count: usize, last_dim: usize },

    /// Last-dim norm parameters shared by RMS-norm and LayerNorm
    /// (no affine flavor for both today). The OpKind selects which
    /// kernel reads this variant.
    NormLastDim {
        outer_count: usize,
        last_dim: usize,
        eps: f64,
    },

    /// Pick slices along a single dim using a rank-1 U32 index
    /// tensor. The kernel needs the outer/inner counts (dims
    /// before/after the selected axis), the source's selected-dim
    /// size for index bounds checking, and the index count.
    IndexSelect {
        outer_count: usize,
        source_dim_size: usize,
        n_indices: usize,
        inner_count: usize,
    },

    /// N-dimensional gather along `dim`. Source and output shapes
    /// agree on every dim except `dim`; the indices tensor (U32)
    /// has output_shape and supplies the source coord for `dim`
    /// at every output position.
    Gather {
        source_shape: Vec<usize>,
        output_shape: Vec<usize>,
        dim: usize,
    },

    /// Rotary position embedding parameters. The kernel walks
    /// `outer_count` × `seq` × `head_dim` elements; `cos`/`sin`
    /// have `[seq, head_dim]` shape and broadcast across the
    /// outer dims.
    Rope {
        outer_count: usize,
        seq: usize,
        head_dim: usize,
    },

    /// Quantized matmul shape: `A [batch, m, k] @ dequant(W) [n, k] →
    /// out [batch, m, n]`. The weight tensor is a flat U32-typed
    /// byte stream representing `n * k / elements_per_block` blocks
    /// of `quant_type`. Activations are F32 today; output is F32.
    QMatMul {
        quant_type: QuantType,
        batch_count: usize,
        m: usize,
        n: usize,
        k: usize,
    },

    /// Index-add along a single dim with rank-1 U32 indices.
    /// Output is `base` with `src[..., i, ...]` accumulated into
    /// `base[..., indices[i], ...]` for every i ∈ 0..n_indices.
    IndexAdd {
        outer_count: usize,
        base_dim_size: usize,
        n_indices: usize,
        inner_count: usize,
    },

    /// N-dimensional scatter-add. Indices and src share the same
    /// shape; base may differ only along `dim`. The kernel walks
    /// every src/indices position, reads `indices[p]` for the
    /// destination's `dim` coord, and accumulates `src[p]` into
    /// `base` at that destination.
    ScatterAdd {
        base_shape: Vec<usize>,
        src_shape: Vec<usize>,
        dim: usize,
    },

    /// Flip the order of elements along one dim. The flat-3-axis
    /// view (outer × dim × inner) lets the kernel walk a tight loop
    /// without re-deriving the axis split per call. `axis` is the
    /// original dim index in the input's rank-N shape — needed by
    /// stride-aware kernels to build a per-axis flip mask.
    Flip {
        outer_count: usize,
        dim_size: usize,
        inner_count: usize,
        axis: usize,
    },

    /// Cyclic shift along one dim by `shift` positions (positive
    /// shifts move elements to higher indices, wrapping around).
    /// Same flat-3-axis view as `Flip`. `shift` is signed: negative
    /// shifts move elements to lower indices.
    Roll {
        outer_count: usize,
        dim_size: usize,
        inner_count: usize,
        shift: i64,
        axis: usize,
    },

    /// Running cumulative sum along one dim. Same flat-3-axis view
    /// as `Flip`/`Roll`. Output is always the same dtype as input;
    /// kernel needs typed addition so it's per-dtype (unlike Flip/Roll).
    CumSum {
        outer_count: usize,
        dim_size: usize,
        inner_count: usize,
        axis: usize,
    },

    /// Triangular mask parameters (used by both Triu and Tril — the
    /// op-kind picks the direction). `batch_count` is the product of
    /// leading dims; `rows`/`cols` are the last two dims. `diagonal`
    /// is signed (0 = main diagonal, positive shifts up, negative
    /// shifts down).
    Triangular {
        batch_count: usize,
        rows: usize,
        cols: usize,
        diagonal: i64,
    },

    /// LogSoftmax along the last dim. Walks `outer_count` rows of
    /// `last_dim` elements each. Per-dtype kernel (uses log/exp).
    LogSoftmaxLastDim { outer_count: usize, last_dim: usize },

    /// MaskedFill: per-element fill where mask is nonzero. The kernel
    /// reads the element count from the layout; `fill_bytes` is
    /// pre-encoded in the output's dtype (one element's worth).
    MaskedFill { fill_bytes: Vec<u8> },

    /// Backward helper for Pad. Carries the input shape so the kernel
    /// can size its scatter-add buffer; the output shape is implicit
    /// from the input shape + padding.
    PadBackward {
        in_shape: Vec<usize>,
        out_shape: Vec<usize>,
        padding: Vec<(usize, usize)>,
        mode_tag: u8,
    },

    /// Multi-dim Pad: per-axis (before, after) plus a `mode_tag`
    /// (0=Constant, 1=Reflect, 2=Replicate) and pre-encoded
    /// `fill_bytes` for Constant fill. Dtype-agnostic at the byte
    /// level: the kernel just copies bytes per the input/output shapes,
    /// which is why fill is bytes (already encoded in the output's
    /// dtype) rather than `f64` (which would force the kernel to
    /// know its dtype).
    ///
    /// `in_shape.len() == out_shape.len() == padding.len()`, and
    /// `out_shape[i] == in_shape[i] + padding[i].0 + padding[i].1`.
    /// `fill_bytes.len() == dtype_size_in_bytes` (one element's worth).
    Pad {
        in_shape: Vec<usize>,
        out_shape: Vec<usize>,
        padding: Vec<(usize, usize)>,
        mode_tag: u8,
        fill_bytes: Vec<u8>,
    },

    /// In-place scatter write parameters. The destination shape +
    /// source shape together determine the kernel walk; `ranges`
    /// gives the per-axis (start, end) slab inside the destination.
    /// `dest_shape[i] >= ranges[i].1`, and source shape is implicitly
    /// `ranges[i].1 - ranges[i].0` along axis `i`.
    ///
    /// Phase E.3.2: backs `Op::WriteSlice` for persistent KV-cache
    /// writes.
    ///
    /// `deferred_dyn_offset` is the **data-determined dynamic-shapes** seam
    /// (increment 2b). Normally `None` — `ranges` is already resolved at
    /// compile (static, or an *input-determined* `dyn_offset` bound in the
    /// per-pass `SymEnv`). It is `Some((axis, sym))` only when the offset is
    /// *data-determined* — its `SymId` is UNBOUND at compile because a
    /// producer (e.g. `Op::NonZeroIndices`) computes it mid-pass. The
    /// `WorkItemKind::WriteSlice` executor arm resolves it from
    /// `produced_syms` and overrides `ranges[axis]` to `(base, base+width)`
    /// just before the kernel runs; the kernel wrappers ignore this field.
    WriteSlice {
        dest_shape: Vec<usize>,
        ranges: Vec<(usize, usize)>,
        deferred_dyn_offset: Option<(usize, fuel_ir::DynScalar)>,
    },

    /// In-place ring-buffer scatter write parameters. Like
    /// `WriteSlice` except `axis` wraps modulo `modulus`; the
    /// dynamic start on that axis is read from an extra input (rank-0
    /// U32). On the rotating axis, `ranges[axis].0` is ignored
    /// (dynamic) and `ranges[axis].1 - ranges[axis].0` is the slab
    /// width (must equal the source dim on that axis, and must not
    /// exceed `modulus`).
    ///
    /// Phase C: backs `Op::WriteSliceRotating` for sliding-window
    /// KV caches.
    WriteSliceRotating {
        dest_shape: Vec<usize>,
        axis: usize,
        modulus: usize,
        ranges: Vec<(usize, usize)>,
    },

    /// In-place scatter write whose start on `axis` is read from a
    /// **device-resident** rank-0 `I64` input (`inputs[2]`) at kernel
    /// launch — the CUDA-graph-capturable KV-cache append. Like
    /// `WriteSlice` except `ranges[axis].0` is a placeholder overridden
    /// device-side by the offset; no modulo wrap. The CUDA wrapper
    /// passes the offset buffer's device pointer straight through as
    /// `dyn_start_dev` (NO D2H — that would break capture); the CPU
    /// wrapper reads it host-side and delegates to `write_slice_cpu`.
    /// Bounds (`offset + width <= dest_shape[axis]`) are the caller's
    /// contract; the kernel does not clamp.
    ///
    /// CapturedRun (2026-07): backs `Op::WriteSliceDoff` for
    /// `DecodeSession` decode-step capture/replay.
    WriteSliceDoff {
        dest_shape: Vec<usize>,
        axis: usize,
        ranges: Vec<(usize, usize)>,
    },

    /// FusedSoftmaxCrossEntropy execution parameters. Inputs:
    /// `logits [n_rows, vocab]` (F32, flattened from the original
    /// `[..., V]` shape) and `targets [n_rows]` (I64). The kernel
    /// walks row-by-row, computing stable log-softmax + NLL +
    /// `ignore_index` masking + the requested reduction in a single
    /// pass, allocating only an `[n_rows]` per-row accumulator (and
    /// a scalar for Mean/Sum). Output is F32 scalar `[]` for
    /// Mean/Sum, F32 `[n_rows]` for None.
    ///
    /// `n_rows` and `vocab` are derived at translate time from the
    /// logits layout; the kernel uses them to iterate without
    /// re-parsing shapes.
    FusedSoftmaxCrossEntropy {
        n_rows: usize,
        vocab: usize,
        reduction: fuel_graph::registry::Reduction,
        ignore_index: i64,
    },

    /// CausalConv1d execution parameters. Inputs:
    /// `x [batch, channels, seq_in]` (pre-padded by caller with
    /// `kernel - 1` left zeros), `weight [channels, 1, kernel]`,
    /// `bias [channels]`. Output `[batch, channels, seq_out]` where
    /// `seq_out = seq_in - (kernel - 1)`. All tensors share dtype.
    ///
    /// Carries both `seq_in` and `seq_out` so the kernel walks the
    /// time axis without re-deriving from shapes. `use_silu` toggles
    /// the fused SiLU activation on the output store (matches
    /// baracuda's `causal_conv1d_*_run` signature flag).
    CausalConv1d {
        batch: usize,
        channels: usize,
        seq_in: usize,
        seq_out: usize,
        kernel: usize,
        use_silu: bool,
    },

    /// SelectiveScan execution parameters. 5 required inputs:
    /// `u [batch, seqlen, dim]`, `delta [batch, seqlen, dim]`,
    /// `a [dim, dstate]`, `b [batch, seqlen, dstate]`,
    /// `c [batch, seqlen, dstate]`. Output `y [batch, seqlen, dim]`.
    /// All tensors share dtype.
    ///
    /// `delta_softplus` toggles applying softplus(delta) before use
    /// (matches baracuda's `selective_scan_*_run` flag).
    SelectiveScan {
        batch: usize,
        seqlen: usize,
        dim: usize,
        dstate: usize,
        delta_softplus: bool,
    },

    /// SsdChunkScan execution parameters. 5 inputs:
    /// `x [batch, seqlen, heads, head_dim]`,
    /// `dt [batch, seqlen, heads]`, `a [heads]`,
    /// `b [batch, seqlen, heads, state_dim]`,
    /// `c [batch, seqlen, heads, state_dim]`. Output `y` matches `x`'s
    /// shape. All tensors share dtype.
    ///
    /// `chunk_size` is the SSD block size (GPU-parallelism knob).
    /// On CPU the kernel runs a sequential scan regardless;
    /// validation requires `chunk_size > 0` and
    /// `seqlen % chunk_size == 0`.
    SsdChunkScan {
        batch: usize,
        seqlen: usize,
        heads: usize,
        head_dim: usize,
        state_dim: usize,
        chunk_size: usize,
    },

    /// Nf4Matmul execution parameters. 3 inputs:
    /// `activations [batch, m, k]` (rank ≥ 2; leading dims flattened
    /// into `batch`), `w_packed [n, k/2]` U8, `absmax [n, k/block_size]`
    /// F32. Output `[batch, m, n]` matching activations' dtype.
    ///
    /// `block_size` is the per-output-row, per-block scale granularity
    /// (typically 64 in bitsandbytes). `k` must be even (w_packed
    /// layout requirement) and a multiple of `block_size`.
    Nf4Matmul {
        batch: usize,
        m: usize,
        n: usize,
        k: usize,
        block_size: usize,
    },

    /// A **runtime fused op's** live scalar args (the `extract:` values from
    /// `FusedOpParams::Runtime { scalars }`), in the region's pattern
    /// pre-order — the same canonical slot order the synthesized kernel's
    /// trailing `, float p0, float p1, …` launch parameters are declared in
    /// (the synthesizer's scalar ABI appends them after `long long n`, always
    /// `float`-typed regardless of the kernel's element dtype). Only produced
    /// by `compile_one`'s `is_runtime` arm; a param-free runtime op carries
    /// `OpParams::None` instead.
    JitScalars {
        /// Slot values, pattern pre-order. `f64` at rest (the graph-side
        /// carrier); the CUDA launch narrows each to `f32` per the ABI.
        scalars: Vec<f64>,
    },

    /// NonZeroIndices execution parameters. 1 input `x` (the value
    /// tensor); one bundled output `[indices [capacity] U32 ; count
    /// [1] U32]` where `capacity == x.elem_count()`. The kernel writes
    /// the ascending flat indices of `x`'s nonzero elements into slot 0
    /// (first `count` entries) and the runtime `count` into slot 1.
    ///
    /// `count_sym` is the [`fuel_ir::symbol::SymId`] the executor binds to the realized
    /// `count` in the per-pass `SymEnv` *after* this op runs — the
    /// data-determined dynamic-shape seam. The kernel itself ignores it.
    NonZeroIndices {
        capacity: usize,
        count_sym: fuel_ir::SymId,
    },
}

// =============================================================================
// Phase 7.5 B5 — kernel binding table
// =============================================================================

/// Maps `(OpKind, KernelDTypes, BackendId)` triples to dispatch wrapper
/// functions, where `KernelDTypes` lists per-operand dtypes (inputs in
/// order, then outputs). Built once at backend registration time
/// (typically a process-wide `OnceLock` though that's not enforced
/// here for testability), consulted at execute time via lookup.
///
/// Same-dtype ops register `[T, T, ..., T]` for the right operand
/// count; mixed-precision ops (Cast, future F32×BF16→F32 matmul)
/// register the exact combo. Variadic ops (Concat) register a
/// canonical short shape `[T, T]` (one input dtype + output dtype);
/// the lookup site for those ops collapses its dtypes vector to the
/// same shorthand.
///
/// Backends register their wrappers via the `dispatch::register_*`
/// functions in this crate (e.g.
/// [`crate::dispatch::register_cpu_kernels`]).
/// Cost-fn signature for primitive-op registrations stored in
/// [`KernelBindingTable`]. Mirrors the fused-op cost-fn shape but
/// takes [`OpParams`] (the binding-table param payload) instead of
/// `FusedOpParams`.
///
/// Implementations return a [`crate::fused::CostEstimate`] computed
/// statically from shapes + dtypes + op-specific params + backend
/// capabilities. The architecture's Layer-1 (FLOP-count + bandwidth)
/// cost model lives here; Layer-2 empirical refinement composes on
/// top via the telemetry framework, not by changing this signature.
pub type CostFn = fn(
    &[fuel_ir::Shape],
    &[DType],
    &OpParams,
    &fuel_ir::backend::BackendCapabilities,
) -> crate::fused::CostEstimate;

/// Sentinel "no cost claim" function — returns
/// [`crate::fused::CostEstimate::default`] (all-zero). The fill pass
/// (`fill_unset_cpu_cost`) recognizes this exact function pointer
/// to decide which entries get the OpKind-family default.
pub fn unknown_cost(
    _shapes: &[fuel_ir::Shape],
    _dtypes: &[DType],
    _params: &OpParams,
    _caps: &fuel_ir::backend::BackendCapabilities,
) -> crate::fused::CostEstimate {
    crate::fused::CostEstimate::default()
}

/// One concrete kernel registered against a `(OpKind, dtypes, backend)`
/// decision-point key. Phase 7.6 step 9a: a single key now carries
/// `SmallVec<[BindingEntry; 2]>` — multiple alternatives compete at
/// the same decision point (e.g. cuBLAS bf16 matmul + CUTLASS bf16
/// matmul at `(MatMul, [BF16, BF16, BF16], Cuda)`). The route picker
/// (step 9b) selects among them at plan time; today's single-impl
/// callers (`lookup`, `lookup_with_caps`, `lookup_precision`,
/// `lookup_cost`) return the first entry, preserving the pre-9a
/// behavior for sites that haven't migrated.
#[derive(Clone, Copy, Debug)]
pub struct BindingEntry {
    pub kernel: KernelRef,
    pub caps: KernelCaps,
    pub precision: crate::fused::PrecisionGuarantee,
    pub cost: CostFn,
    /// Diagnostic tag identifying the kernel's implementation source
    /// when multiple alternatives register at the same `(op, dtypes,
    /// backend)` key. Used to distinguish e.g. AOCL's matmul from
    /// MKL's matmul from the portable scalar matmul — all three live
    /// at `(MatMul, [F32×3], Cpu)` as siblings.
    ///
    /// `""` is the default for kernels that don't need to distinguish.
    /// Conventional values: `"portable-cpu"` for fuel-cpu-backend
    /// scalar/gemm kernels, `"aocl"` for AOCL-BLAS, `"mkl"` for oneMKL,
    /// `"cublas"` / `"cutlass"` for CUDA variants. The string is
    /// diagnostic-only — never used for dispatch routing (the kernel
    /// function pointer is the dispatch identifier).
    pub kernel_source: &'static str,
    /// Whether this kernel's FKC accept-contract is the **GENERIC**
    /// (fully-permissive strided) one — admissible anywhere because it
    /// imposes no structure tightness (FKC §4.12). This is the
    /// structural-**miss** fallback bit: when the winning contract at a
    /// dispatch cell is generic, a structure-specialized kernel would have
    /// fit but none is registered, and the Baracuda miss telemetry records
    /// the demand.
    ///
    /// Computed **once at registration** by the FKC importer from the
    /// kernel's retained `ResolvedLayout` set
    /// ([`crate::fkc::is_generic_contract`]) and stamped here, so
    /// the live dispatch pick site reads a precomputed bool rather than
    /// re-deriving genericity from the lossy single-bool [`KernelCaps`].
    ///
    /// **Hand-written registrations default `false`.** Every non-FKC
    /// `register*` path routes through
    /// [`KernelBindingTable::register_full_with_source`], which sets this
    /// `false`: genericity is a claim about being the FKC generic-fallback
    /// contract in Baracuda's structure-key specialization space, and a
    /// hand-written kernel makes no such claim. This holds even for a
    /// hand-written strided kernel (`register_with_caps(strided_input=true)`):
    /// strided-capability is not the same as being the generic fallback, and
    /// conflating them would fabricate a miss demand signal for cells that are
    /// not part of Baracuda's matrix. Only the FKC importer, which computed
    /// genericity from the declared contract, ever sets this `true`.
    pub is_generic: bool,
    /// Stable per-implementation-version hash — the fifth axis of the FKC
    /// kernel-identity basis tuple (FKC §4.11), pinning the exact kernel build
    /// so a persisted plan / telemetry record re-resolves to it. `0`
    /// ([`crate::fused::KernelRevisionHash::UNTRACKED`]) for hand-written
    /// registrations (which carry no revision); the FKC importer threads the
    /// contract-computed revision from `ResolvedPrimitive.revision` here. Read
    /// by the Baracuda dispatch-telemetry `ImplId` at the plan pick site so the
    /// `chosen`/`candidates` ids carry a real revision instead of a stamped `0`.
    pub kernel_revision_hash: u64,
    /// The contract's parsed cost-expression AST (FKC §4.4 / adoption plan
    /// §2.3), when the kernel came from an FKC-imported contract that
    /// declares one. `&'static` so the entry stays `Copy` (`CompiledCostExpr`
    /// derives `Debug`, which is all `&'static T` needs to stay `Copy` /
    /// `Clone` / `Debug` itself). `None` for every hand-written registration
    /// (`register`/`register_full*`) — those carry no declared cost AST, only
    /// a `CostFn` fn-pointer. Only the FKC importer, once it lowers a
    /// contract's cost block to a live `CostFn` (a later Group-2 task),
    /// stamps `Some(&CompiledCostExpr)` here.
    pub cost_expr: Option<&'static crate::fkc::CompiledCostExpr>,
}

/// The binding table's op key — generalized so **runtime-registered** fused
/// ops live in the SAME registry as static kernels (the 2026-07-08
/// decisions-log end-state; retires the transitional runtime-kernel sidecar).
///
/// `Static` is the compile-time `OpKind` family every existing registration
/// uses (a bare `OpKind` converts via `From`, so those call sites are
/// unchanged). `RuntimeFused` names a Tier-2 (JIT-synthesized / import-time)
/// fused op by its runtime `FusedOpId` (`>= RUNTIME_FUSED_BASE`) — the id IS
/// the op identity, so no fake `OpKind` ever enters the table.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BindingKey {
    /// A compile-time op family (every static kernel registration).
    Static(OpKind),
    /// A runtime-registered fused op, keyed by its runtime id.
    RuntimeFused(FusedOpId),
}

impl From<OpKind> for BindingKey {
    fn from(op: OpKind) -> Self {
        BindingKey::Static(op)
    }
}

impl BindingKey {
    /// The `Static` op kind, if this is a static-family key.
    pub fn static_op(self) -> Option<OpKind> {
        match self {
            BindingKey::Static(op) => Some(op),
            BindingKey::RuntimeFused(_) => None,
        }
    }
}

/// `Clone` is load-bearing, not a convenience: the process-wide table is
/// published as a copy-on-write snapshot (`RwLock<Arc<KernelBindingTable>>`
/// in `dispatch.rs`), so a writer clones the current table, mutates the
/// clone, and swaps the pointer. Readers therefore never hold a lock guard.
/// The clone is shallow-ish and cheap — [`BindingEntry`] is `Copy` and the
/// keys are `Copy`/`SmallVec`, so it is a `HashMap` bulk copy of POD, on a
/// write-rare path (backend init + runtime-fusion adoption).
#[derive(Default, Clone)]
pub struct KernelBindingTable {
    bindings: HashMap<(BindingKey, KernelDTypes, BackendId), SmallVec<[BindingEntry; 2]>>,
}

/// The outcome of [`KernelBindingTable::unique_runtime_fused`] — three
/// distinct facts, deliberately not collapsed into an `Option`.
#[derive(Debug)]
pub enum RuntimeFusedLookup<'a> {
    /// No runtime-fused binding for this `(fid, backend)`.
    NotBound,
    /// Exactly one dtype tuple is registered; this is its first alternative.
    Unique(&'a [DType], &'a BindingEntry),
    /// Several dtype tuples share this id. **No kernel is returned**, because
    /// any choice among them would be arbitrary — and one of them may have a
    /// different operand arity than the caller expects.
    Ambiguous { signatures: Vec<Vec<DType>> },
}

impl KernelBindingTable {
    pub fn new() -> Self {
        Self {
            bindings: HashMap::new(),
        }
    }

    /// Register a dispatch wrapper for `(op, dtypes, backend)` with the
    /// default (all-false) capabilities. `dtypes` is the per-operand
    /// dtype list — inputs in order, then outputs.
    ///
    /// Phase 7.6 step 9a: multiple distinct kernels may register
    /// against the same `(op, dtypes, backend)` key — they become
    /// sibling alternatives at one decision point. Registration is
    /// **append-only and never panics**; the **same** `KernelRef`
    /// function pointer registered twice is detected by
    /// [`Self::finalize`] (run once after bulk registration) and
    /// returned as a `Result`, not raised as an inline panic.
    ///
    /// PrecisionGuarantee defaults to
    /// [`PrecisionGuarantee::UNAUDITED`](crate::fused::PrecisionGuarantee::UNAUDITED).
    /// Step-7b convention: the always-built backend
    /// (fuel-cpu-backend) runs [`Self::fill_unset_cpu_precision`] at
    /// the end of its bulk registration pass to upgrade every UNAUDITED
    /// CPU entry to `PRIMITIVE_DETERMINISTIC_CPU` (bit-stable per
    /// hardware). Kernels with weaker guarantees should call
    /// [`Self::register_with_precision`] explicitly *before* the fill
    /// pass to opt out of the default.
    pub fn register(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
    ) {
        self.register_full(
            op,
            dtypes,
            backend,
            kernel,
            KernelCaps::empty(),
            crate::fused::PrecisionGuarantee::UNAUDITED,
            unknown_cost,
        );
    }

    /// Register a dispatch wrapper with explicit capability flags.
    /// Used by binding sites that opt into kernel-side broadcast or
    /// other non-default behavior — the executor consults the caps to
    /// decide whether to auto-Contiguize inputs before kernel call.
    pub fn register_with_caps(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
        caps: KernelCaps,
    ) {
        self.register_full(
            op,
            dtypes,
            backend,
            kernel,
            caps,
            crate::fused::PrecisionGuarantee::UNAUDITED,
            unknown_cost,
        );
    }

    /// Register with an explicit [`crate::fused::PrecisionGuarantee`].
    /// Use this when a kernel has a precision claim that differs from
    /// the bulk-fill default (e.g., a multi-threaded reduction with
    /// non-deterministic accumulation order — bit_stable_on_same_hardware
    /// must be false).
    pub fn register_with_precision(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
        precision: crate::fused::PrecisionGuarantee,
    ) {
        self.register_full(
            op,
            dtypes,
            backend,
            kernel,
            KernelCaps::empty(),
            precision,
            unknown_cost,
        );
    }

    /// Backwards-compatible alias for full-form registration without
    /// an explicit cost function. New code should prefer
    /// [`Self::register_full`].
    pub fn register_with_caps_and_precision(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
        caps: KernelCaps,
        precision: crate::fused::PrecisionGuarantee,
    ) {
        self.register_full(op, dtypes, backend, kernel, caps, precision, unknown_cost);
    }

    /// Phase 7.6 step 8: full-form register with caps + precision +
    /// cost. Cost defaults to [`unknown_cost`] in the other
    /// signatures; sites with non-default cost claims must use this
    /// form (and not be overwritten by the bulk
    /// [`Self::fill_unset_cpu_cost`] pass, which only touches
    /// entries whose cost is still `unknown_cost`).
    ///
    /// Phase 7.6 step 9a: appends to the alternative set for `(op,
    /// dtypes, backend)`. Distinct `KernelRef` function pointers
    /// compose as siblings. Append-only — a duplicate `KernelRef` is
    /// detected by [`Self::finalize`] after bulk registration, not by
    /// an inline panic (see [`Self::register`]).
    pub fn register_full(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
        caps: KernelCaps,
        precision: crate::fused::PrecisionGuarantee,
        cost: CostFn,
    ) {
        self.register_full_with_source(op, dtypes, backend, kernel, caps, precision, cost, "");
    }

    /// Phase: backend-extensions refactor (2026-06-07). Same as
    /// [`Self::register_full`] but tags the binding with a
    /// `kernel_source` identifier used to distinguish kernels that
    /// register at the same `(op, dtypes, backend)` key from
    /// different implementation sources (AOCL vs MKL vs portable
    /// CPU, etc.). The tag is diagnostic-only — the picker still
    /// distinguishes alternatives by function-pointer identity.
    pub fn register_full_with_source(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
        caps: KernelCaps,
        precision: crate::fused::PrecisionGuarantee,
        cost: CostFn,
        kernel_source: &'static str,
    ) {
        // Hand-written registrations make no FKC generic-fallback claim —
        // see [`BindingEntry::is_generic`]. The FKC importer uses
        // [`Self::register_full_with_source_generic`] to stamp the computed
        // bit. Hand-written kernels carry no revision, so the revision axis is
        // `UNTRACKED` (`0`); only the FKC importer threads a real revision.
        self.register_full_with_source_generic(
            op,
            dtypes,
            backend,
            kernel,
            caps,
            precision,
            cost,
            kernel_source,
            false,
            crate::fused::KernelRevisionHash::UNTRACKED.0,
            None,
        );
    }

    /// Full-form register with the FKC structural-fallback `is_generic` bit
    /// (see [`BindingEntry::is_generic`]). The FKC importer computes the bit
    /// once from a kernel's retained `ResolvedLayout` set
    /// ([`crate::fkc::is_generic_contract`]) and stamps it here;
    /// every hand-written path routes through
    /// [`Self::register_full_with_source`], which passes `false`.
    ///
    /// Otherwise identical to [`Self::register_full_with_source`]:
    /// append-only, never-panic; the same `KernelRef` on one key is caught by
    /// [`Self::finalize`], not an inline panic.
    #[allow(clippy::too_many_arguments)]
    pub fn register_full_with_source_generic(
        &mut self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
        kernel: KernelRef,
        caps: KernelCaps,
        precision: crate::fused::PrecisionGuarantee,
        cost: CostFn,
        kernel_source: &'static str,
        is_generic: bool,
        kernel_revision_hash: u64,
        cost_expr: Option<&'static crate::fkc::CompiledCostExpr>,
    ) {
        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
        let entry = BindingEntry {
            kernel,
            caps,
            precision,
            cost,
            kernel_source,
            is_generic,
            kernel_revision_hash,
            cost_expr,
        };
        // Append-only (never-panic): distinct `KernelRef`s compose as
        // sibling alternatives at one decision point; the *same*
        // function pointer registered twice is NOT rejected inline.
        // [`Self::finalize`] detects duplicates after bulk registration
        // and returns a `Result`, so the static init boundary can
        // fail-fast (`.expect()`) while a dynamic importer surfaces the
        // error — honoring never-panic on the import path.
        self.bindings.entry(key).or_default().push(entry);
    }

    /// Validate the fully-assembled table: within the alternative set
    /// for any one `(op, dtypes, backend)` key, each `KernelRef`
    /// function pointer must appear **at most once**. Distinct
    /// alternatives compose as siblings; the same pointer twice is a
    /// registration bug (a copy-paste in a hand-written table, or a
    /// contract importer wiring one kernel onto two keys that collapse).
    ///
    /// `register*` is append-only and never panics; this single pass —
    /// run once after bulk registration — surfaces a duplicate as a
    /// typed `Result`. The static init boundary
    /// ([`crate::dispatch::global_bindings`]) `.expect()`s it (fail-fast
    /// at startup for hand-written tables); the FKC contract importer
    /// will call it with `?`. This is the never-panic replacement for
    /// the former inline duplicate `panic!`.
    pub fn finalize(&self) -> Result<()> {
        for ((op, dtypes, backend), alts) in self.bindings.iter() {
            for i in 0..alts.len() {
                let ptr_i = alts[i].kernel as *const () as usize;
                for entry_j in &alts[i + 1..] {
                    if entry_j.kernel as *const () as usize == ptr_i {
                        return Err(Error::Msg(format!(
                            "KernelBindingTable: duplicate KernelRef registered \
                             for (op={op:?}, dtypes={dtypes:?}, backend={backend:?}). \
                             The same function pointer was registered twice; \
                             distinct alternatives at one decision point must be \
                             distinct `fn` items."
                        )));
                    }
                }
            }
        }
        Ok(())
    }

    /// Phase 7.6 step 7b: upgrade every UNAUDITED-precision CPU
    /// registration to the supplied `default`. Convention is to call
    /// this at the *end* of bulk registration, so any
    /// `register_with_precision(...)` calls that explicitly claimed a
    /// non-default value are preserved. The architecture-target
    /// shape would be precision-per-call-site, but for the ~335 CPU
    /// primitive registrations that all share the deterministic
    /// F32-accumulator property, a fill pass keeps the call sites
    /// concise without sacrificing the architectural commitment
    /// (every entry ends up with an explicit, non-UNAUDITED precision
    /// claim before lookup is ever exercised).
    ///
    /// Only entries with `backend == BackendId::Cpu` and the current
    /// `PrecisionGuarantee::UNAUDITED` sentinel are touched. Non-CPU
    /// backends register their own precision claims; non-UNAUDITED
    /// entries are preserved.
    ///
    /// Step 9a: applies to **every alternative** registered under a
    /// CPU key, not just the first — so a key with N siblings all
    /// starting at UNAUDITED ends with N entries upgraded to `default`.
    pub fn fill_unset_cpu_precision(&mut self, default: crate::fused::PrecisionGuarantee) {
        for ((_, _, backend), alts) in self.bindings.iter_mut() {
            if *backend != BackendId::Cpu {
                continue;
            }
            for entry in alts.iter_mut() {
                let p = &mut entry.precision;
                // Upgrade entries that claim nothing — EXCEPT an audited
                // "no static bound applies" conclusion.
                //
                // GAP-095: the old predicate was purely structural ("all four
                // value fields at defaults"), and the comment here asserted
                // that "a real weaker claim would set at least one of them."
                // That is false for `PrecisionGuarantee::none(reason)`, which
                // is field-identical to UNAUDITED and sets none of them. The
                // fill therefore overwrote an explicit audit conclusion with
                // `bit_stable_on_same_hardware: true` — asserting on the
                // kernel's behalf exactly what its audit had declined to
                // claim. FKC-4.8-0001 sanctions this fill for blocks left
                // absent or `audited: false`; it does not sanction that.
                //
                // Note what must KEEP being upgraded: a contract claim that
                // the V-FKC-9 ledger gate downgraded collapses to the
                // UNAUDITED sentinel exactly, so it stays eligible here. That
                // is load-bearing — every one of the CPU table's bit-stable
                // entries arrives through this path (GAP-077), so excluding
                // them would break the FKC-4.8-0001 coverage commitment
                // outright.
                if p.makes_no_static_claim() && !p.is_audited_no_bound() {
                    *p = default;
                }
            }
        }
    }

    /// Phase 7.6 step 8: upgrade every still-`unknown_cost` CPU
    /// registration to the cost function returned by `dispatcher` for
    /// its OpKind. The dispatcher's contract: given an `OpKind`,
    /// return the appropriate cost-family function — typically
    /// [`crate::cost::default_cost_for_op_kind`] in production code,
    /// but tests may pass their own dispatcher to exercise specific
    /// cost shapes.
    ///
    /// Convention is to call this at the *end* of bulk registration,
    /// so any `register_full(...)` calls that explicitly claimed a
    /// non-default cost are preserved. The function-pointer equality
    /// check identifies UNKNOWN entries — any caller that wants a
    /// "weaker" claim with non-zero values must use a distinct
    /// function pointer (which won't compare equal to `unknown_cost`
    /// and thus won't be overwritten).
    ///
    /// Step 9a: applies to **every alternative** with a CPU backend —
    /// each sibling entry at a key starting at `unknown_cost` is
    /// upgraded independently.
    pub fn fill_unset_cpu_cost(&mut self, dispatcher: fn(OpKind) -> CostFn) {
        self.fill_unset_cost_for_backend(BackendId::Cpu, dispatcher);
    }

    /// Per-backend variant of [`Self::fill_unset_cpu_cost`]. Walks
    /// every alternative whose backend matches `backend` and replaces
    /// `unknown_cost` entries with the dispatcher's choice for the
    /// op. Used by [`crate::vulkan_dispatch::register_vulkan_kernels`]
    /// to bulk-fill cost functions after per-kernel precision
    /// registration; future MKL/AOCL/Metal backends register through
    /// this same helper.
    ///
    /// Backend-specific cost adjustments (e.g. higher
    /// `kernel_overhead_ns` for Vulkan command-buffer submission vs
    /// CPU's nested-loop call overhead) are a Layer-2 refinement —
    /// today's `default_cost_for_op_kind` returns the same CPU-flavored
    /// functions regardless of backend, which over-estimates Vulkan
    /// throughput on small tensors. Either pass a Vulkan-flavored
    /// dispatcher to this method, or rely on the empirical calibration
    /// framework's per-(op, dtype, backend) refinement.
    pub fn fill_unset_cost_for_backend(
        &mut self,
        backend: BackendId,
        dispatcher: fn(OpKind) -> CostFn,
    ) {
        let sentinel = unknown_cost as *const () as usize;
        for ((key, _, this_backend), alts) in self.bindings.iter_mut() {
            if *this_backend != backend {
                continue;
            }
            // Runtime-fused entries keep the sentinel: their cost derives from
            // the op's recipe (keyed by the runtime id), not an OpKind table.
            let BindingKey::Static(op) = key else {
                continue;
            };
            for entry in alts.iter_mut() {
                if (entry.cost as usize) == sentinel {
                    entry.cost = dispatcher(*op);
                }
            }
        }
    }

    /// Look up the [`crate::fused::PrecisionGuarantee`] for a
    /// registered `(op, dtypes, backend)` triple. Returns the **first
    /// alternative**'s precision; multi-impl callers wanting all
    /// alternatives use [`Self::lookup_alternatives`]. Returns
    /// `PrecisionGuarantee::UNAUDITED` if the binding is missing —
    /// callers (notably the step-7b lint) treat that as "no claim"
    /// rather than panicking.
    pub fn lookup_precision(
        &self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
    ) -> crate::fused::PrecisionGuarantee {
        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
        self.bindings
            .get(&key)
            .and_then(|alts| alts.first())
            .map(|e| e.precision)
            .unwrap_or(crate::fused::PrecisionGuarantee::UNAUDITED)
    }

    /// Look up the [`CostFn`] for a registered `(op, dtypes, backend)`
    /// triple. Returns the **first alternative**'s cost; multi-impl
    /// callers wanting all alternatives use
    /// [`Self::lookup_alternatives`]. Returns [`unknown_cost`] for
    /// missing bindings — the step-8 lint treats that as "no cost
    /// claim."
    pub fn lookup_cost(
        &self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
    ) -> CostFn {
        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
        self.bindings
            .get(&key)
            .and_then(|alts| alts.first())
            .map(|e| e.cost)
            .unwrap_or(unknown_cost)
    }

    /// Iterate `(op, dtypes, backend, precision)` over every
    /// registered alternative — one tuple per `BindingEntry`, not per
    /// key. Used by the step-7b coverage lint, which groups by
    /// OpKind and checks the bit-stable commitment per group; the
    /// grouping handles N-alternatives-per-key naturally.
    pub fn iter_precision(
        &self,
    ) -> impl Iterator<
        Item = (
            OpKind,
            &[DType],
            BackendId,
            crate::fused::PrecisionGuarantee,
        ),
    > {
        self.bindings
            .iter()
            .filter_map(|((key, dtypes, backend), alts)| {
                // Static-kernel audit view: runtime-fused entries are outside the
                // per-OpKind commitments these iterators exist to check.
                key.static_op().map(|op| (op, dtypes, backend, alts))
            })
            .flat_map(|(op, dtypes, backend, alts)| {
                alts.iter()
                    .map(move |e| (op, dtypes.as_slice(), *backend, e.precision))
            })
    }

    /// Iterate `(op, dtypes, backend, &entry)` over every registered
    /// alternative — one tuple per `BindingEntry`, mirroring
    /// [`Self::iter_precision`] but yielding the FULL entry (by reference)
    /// instead of projecting out just the `precision` field. Used by the
    /// FKC empirical verification harness (Task 4.4+), which needs the
    /// whole `BindingEntry` (the `kernel` fn pointer to invoke, `caps`,
    /// `kernel_revision_hash` for ledger keys, ...), not just one field.
    /// Runtime-fused/JIT entries are filtered out (`static_op()` returns
    /// `None` for them) — same "static-kernel audit view" as
    /// `iter_precision`/`iter_cost`.
    pub fn iter_entries(
        &self,
    ) -> impl Iterator<Item = (OpKind, &[DType], BackendId, &BindingEntry)> {
        self.bindings
            .iter()
            .filter_map(|((key, dtypes, backend), alts)| {
                key.static_op().map(|op| (op, dtypes, backend, alts))
            })
            .flat_map(|(op, dtypes, backend, alts)| {
                alts.iter()
                    .map(move |e| (op, dtypes.as_slice(), *backend, e))
            })
    }

    /// Iterate `(op, dtypes, backend, cost_fn)` over every registered
    /// alternative — one tuple per `BindingEntry`. Used by the step-8
    /// coverage lint to check the "every primitive op has a
    /// non-default cost function" commitment per OpKind.
    pub fn iter_cost(&self) -> impl Iterator<Item = (OpKind, &[DType], BackendId, CostFn)> {
        self.bindings
            .iter()
            .filter_map(|((key, dtypes, backend), alts)| {
                key.static_op().map(|op| (op, dtypes, backend, alts))
            })
            .flat_map(|(op, dtypes, backend, alts)| {
                alts.iter()
                    .map(move |e| (op, dtypes.as_slice(), *backend, e.cost))
            })
    }

    /// Iterate every registered `(op, dtypes, backend)` decision-point
    /// key — one item per key, regardless of how many alternatives
    /// share it. Consumed by `SystemTopology::build_at` to derive
    /// which backends have kernels registered + which (op, dtype)
    /// pairs each backend covers.
    pub fn iter_keys(&self) -> impl Iterator<Item = (OpKind, &[DType], BackendId)> {
        self.bindings.keys().filter_map(|(key, dtypes, backend)| {
            key.static_op().map(|op| (op, dtypes.as_slice(), *backend))
        })
    }

    /// Whether ANY runtime-fused entry is bound for `(fid, backend)`,
    /// regardless of dtype tuple — the coarse capability-gate query (the
    /// optimizer's fuse/don't-fuse predicate is id-level; the dtype-precise
    /// check is the dispatch-time [`Self::lookup_with_caps`] itself).
    pub fn has_runtime_fused(&self, fid: FusedOpId, backend: BackendId) -> bool {
        self.bindings
            .keys()
            .any(|(k, _, b)| *b == backend && matches!(k, BindingKey::RuntimeFused(f) if *f == fid))
    }

    /// Resolve `(fid, backend)` to a runtime-fused binding, **refusing to pick
    /// when the answer is ambiguous**.
    ///
    /// A [`FusedOpId`] names a RECIPE, not a signature: `register_runtime_fused`
    /// deduplicates on the region's base-map hash, so one id can legitimately
    /// carry several dtype tuples — `[F32, F32]` and `[F32, F32, F32]` are
    /// different arities under the same id. The pre-GAP-213 `first_runtime_fused` scanned
    /// for `(fid, backend)` and **binds `dtypes` without testing it**, so it
    /// answers such a question by `HashMap::iter()` order: a coin flip per
    /// process, and capable of returning a kernel of the wrong operand arity.
    ///
    /// The three outcomes are kept as three values rather than folded into an
    /// `Option`, because *not bound* and *ambiguous* are different facts with
    /// different fixes, and an `Option` would make them indistinguishable to the
    /// caller — the same collapse that let GAP-001 read as a version regression
    /// for eleven days.
    ///
    /// **Scope: this is the diagnostic/test resolution path.** Production
    /// dispatch never came through here — it uses [`Self::lookup_with_caps`],
    /// which takes the dtype tuple and does an exact keyed `get`, so it can
    /// neither be ambiguous nor return a wrong arity. See GAP-213.
    pub fn unique_runtime_fused(
        &self,
        fid: FusedOpId,
        backend: BackendId,
    ) -> RuntimeFusedLookup<'_> {
        let mut matches = self.bindings.iter().filter(|((k, _, b), _)| {
            *b == backend && matches!(k, BindingKey::RuntimeFused(f) if *f == fid)
        });
        let Some(((_, dtypes, _), alts)) = matches.next() else {
            return RuntimeFusedLookup::NotBound;
        };
        if matches.next().is_some() {
            let mut signatures: Vec<Vec<DType>> = self
                .bindings
                .iter()
                .filter(|((k, _, b), _)| {
                    *b == backend && matches!(k, BindingKey::RuntimeFused(f) if *f == fid)
                })
                .map(|((_, d, _), _)| d.to_vec())
                .collect();
            // HashMap order is per-process, so the report would otherwise vary run to
            // run. `DType` is not `Ord`, so key on arity then the debug spelling —
            // a presentation order only; nothing depends on which comes first.
            signatures.sort_by_key(|d| (d.len(), format!("{d:?}")));
            return RuntimeFusedLookup::Ambiguous { signatures };
        }
        match alts.first() {
            Some(e) => RuntimeFusedLookup::Unique(dtypes.as_slice(), e),
            None => RuntimeFusedLookup::NotBound,
        }
    }

    /// **TEST-ONLY.** Drop every runtime-fused entry (static entries retained).
    /// The runtime-fused reset story: the metadata sidecar's Vec length is the
    /// id allocator, so clearing it MUST clear these bindings in the same
    /// breath or a reused id resolves a stale kernel
    /// (`runtime_fused_kernels::clear_runtime_fused_for_tests` does both).
    #[doc(hidden)]
    pub fn remove_runtime_fused_for_tests(&mut self) {
        self.bindings
            .retain(|(k, _, _), _| matches!(k, BindingKey::Static(_)));
    }

    /// Look up the wrapper for `(op, dtypes, backend)`. Returns the
    /// **first alternative**'s [`KernelRef`]; for capability-aware
    /// lookup use [`Self::lookup_with_caps`]; for the full alternative
    /// set use [`Self::lookup_alternatives`].
    pub fn lookup(
        &self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
    ) -> Result<KernelRef> {
        self.lookup_with_caps(op, dtypes, backend).map(|(k, _)| k)
    }

    /// Capability-aware lookup. Returns the first **dense-safe** alternative
    /// — the first sibling whose caps do NOT set
    /// [`KernelCaps::requires_broadcast`] — paired with its registered
    /// [`KernelCaps`]. A baked-broadcast sibling (`broadcast_stride0:
    /// required`) is SKIPPED, so a dense operand is never routed into a
    /// kernel that reads `in[0]`; the exclusion is order-independent (path
    /// 1a). For every existing binding — none set `requires_broadcast` —
    /// this is byte-identical to returning the first-registered alternative.
    /// Surfaces [`Error::NoBackendForOp`] on a missing binding OR when every
    /// sibling is broadcast-baked (production-correct: a surfaced miss, never
    /// a wrong read, no panic).
    pub fn lookup_with_caps(
        &self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
    ) -> Result<(KernelRef, KernelCaps)> {
        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
        self.bindings
            .get(&key)
            .and_then(|alts| alts.iter().find(|e| !e.caps.requires_broadcast))
            .map(|e| (e.kernel, e.caps))
            .ok_or_else(|| {
                let (available_backends, supported_combinations) = self.coverage_diagnostic();
                Error::NoBackendForOp {
                    op: key.0.static_op().unwrap_or(OpKind::RuntimeFused),
                    dtypes: dtypes.to_vec(),
                    available_backends,
                    supported_combinations,
                }
                .bt()
            })
    }

    /// Build the truthful `(available_backends, supported_combinations)`
    /// diagnostic pair for a [`Error::NoBackendForOp`] miss: the DISTINCT
    /// registered backends, and the full static `(backend, op, dtypes)` coverage
    /// (the useful "what exists" context). Shared by
    /// [`lookup_with_caps`](Self::lookup_with_caps) AND the plan-time miss
    /// (`plan::missing_binding_error`) so BOTH surface the same real context. The
    /// plan-time path formerly hardcoded `[]` here, which falsely reads as "no
    /// backend is registered" and misdirects diagnosis — a mixed-precision
    /// `(op, dtypes)` gap is not an empty registry (observed in the wild:
    /// `[F32, BF16, F32]` matmul reported as "available backends: []").
    pub fn coverage_diagnostic(&self) -> (Vec<BackendId>, Vec<CoverageBinding>) {
        let available_backends: Vec<BackendId> = self
            .bindings
            .keys()
            .map(|(_, _, b)| *b)
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        let supported_combinations: Vec<(BackendId, OpKind, Vec<DType>)> = self
            .bindings
            .keys()
            .filter_map(|(k, d, b)| k.static_op().map(|o| (*b, o, d.to_vec())))
            .collect();
        (available_backends, supported_combinations)
    }

    /// Does **this specific backend** serve `(op, dtypes)` with a usable
    /// (non-broadcast-baked) binding?
    ///
    /// Mirrors [`lookup_with_caps`](Self::lookup_with_caps)'s success condition
    /// exactly — same key, same `!requires_broadcast` admission — but skips
    /// constructing the miss diagnostic, so it is cheap enough to call in a
    /// per-node loop (`coverage_diagnostic` walks the entire table).
    ///
    /// Ask this once per *candidate* backend. There is deliberately no
    /// "does any backend anywhere serve it?" sibling: a predicate that ignores
    /// the key's backend component cannot answer a dispatch question, because a
    /// node executes on one backend rather than on the union of them. Such a
    /// sibling did exist (`any_backend_supports`) and it caused a real defect —
    /// the dtype-reconciliation pass consulted it, so a backend a node could
    /// never run on suppressed the `Op::Cast` that node needed and planning
    /// dead-ended at `NoBackendForOp`. See `crate::plan::candidate_backends_for`.
    pub fn backend_supports(
        &self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
    ) -> bool {
        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
        self.bindings
            .get(&key)
            .is_some_and(|alts| alts.iter().any(|e| !e.caps.requires_broadcast))
    }

    /// Phase 7.6 step 9a: return the full set of registered
    /// alternatives at the `(op, dtypes, backend)` decision point.
    /// Order is registration order — append-on-register is the step-9a
    /// contract. The empty slice means "no kernel registered at this
    /// decision point."
    ///
    /// The route picker (step 9b) consumes this to rank alternatives
    /// by cost + precision at plan time. Single-impl callers continue
    /// to use [`Self::lookup_with_caps`] (which returns the first
    /// alternative — the route picker's job is the longer-term home
    /// for selection logic).
    pub fn lookup_alternatives(
        &self,
        op: impl Into<BindingKey>,
        dtypes: &[DType],
        backend: BackendId,
    ) -> &[BindingEntry] {
        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
        self.bindings
            .get(&key)
            .map(|alts| alts.as_slice())
            .unwrap_or(&[])
    }

    /// Total number of registered alternatives across all keys (not
    /// just unique keys). Step 9a: a key with N siblings counts N.
    pub fn len(&self) -> usize {
        self.bindings.values().map(|alts| alts.len()).sum()
    }

    /// Empty binding table.
    pub fn is_empty(&self) -> bool {
        self.bindings.is_empty()
    }

    /// Total number of unique `(op, dtypes, backend)` keys. Step 9a:
    /// distinct from [`Self::len`], which counts alternatives.
    pub fn key_count(&self) -> usize {
        self.bindings.len()
    }
}

impl std::fmt::Debug for KernelBindingTable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("KernelBindingTable")
            .field("keys", &self.bindings.len())
            .field("total_alternatives", &self.len())
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuel_ir::DType;

    /// Smoke: KernelRef can be constructed and stored.
    #[test]
    fn kernel_ref_stores_function_pointer() {
        fn dummy_kernel(
            _inputs: &[Arc<RwLock<Storage>>],
            _outputs: &mut [Arc<RwLock<Storage>>],
            _layouts: &[Layout],
            _params: &OpParams,
        ) -> Result<()> {
            Ok(())
        }
        let k: KernelRef = dummy_kernel;
        // Function pointer is Copy + Clone.
        let _k2 = k;
        let _k3: KernelRef = k;
    }

    /// Smoke: OpParams variants construct cleanly and Debug-format.
    #[test]
    fn op_params_variants_construct() {
        let _ = OpParams::None;
        let _ = OpParams::Reduce {
            dims: vec![0, 1],
            keepdim: false,
        };
        let _ = OpParams::Matmul {
            lhs_batch_dims: vec![],
            rhs_batch_dims: vec![],
            m: 4,
            n: 8,
            k: 16,
            m_compute: MatmulM::All,
        };
        let _ = OpParams::Slice {
            dim: 0,
            start: 0,
            end: 10,
            step: 1,
        };
        let _ = OpParams::Cast;
        let _ = OpParams::Affine { mul: 2.0, add: 1.0 };
        // Debug format compiles.
        let _ = format!("{:?}", OpParams::None);
    }

    /// Smoke: a hand-constructed kernel that allocates the output
    /// Storage outside this crate would be:
    ///   1. allocate output via fuel_memory::alloc_cpu_zeroed
    ///   2. wrap inputs as &[Arc<RwLock<Storage>>]
    ///   3. call the kernel
    ///
    /// Phase B5 lands the first such migration; B1 just type-checks
    /// the surface.
    #[test]
    fn kernel_ref_can_be_called() {
        fn copy_kernel(
            inputs: &[Arc<RwLock<Storage>>],
            outputs: &mut [Arc<RwLock<Storage>>],
            _layouts: &[Layout],
            _params: &OpParams,
        ) -> Result<()> {
            // Simplest real kernel: copy bytes from inputs[0] to outputs[0].
            let in_arc = &inputs[0];
            let out_arc = &outputs[0];
            let in_guard = in_arc.read().unwrap();
            let mut out_guard = out_arc.write().unwrap();
            let in_bytes = match &in_guard.inner {
                fuel_memory::BackendStorage::Cpu(s) => s.bytes(),
                #[allow(unreachable_patterns)]
                _ => return Ok(()),
            };
            if let fuel_memory::BackendStorage::Cpu(s) = &mut out_guard.inner {
                s.bytes_mut().copy_from_slice(in_bytes);
            }
            Ok(())
        }

        let input = fuel_memory::from_slice_cpu(&[1.0_f32, 2.0, 3.0, 4.0]);
        let output = fuel_memory::alloc_cpu_zeroed(DType::F32, 4).unwrap();
        let inputs = vec![Arc::new(RwLock::new(input))];
        let mut outputs = vec![Arc::new(RwLock::new(output))];

        let k: KernelRef = copy_kernel;
        k(&inputs, &mut outputs, &[], &OpParams::None).unwrap();

        // Output bytes match input.
        let out_guard = outputs[0].read().unwrap();
        if let fuel_memory::BackendStorage::Cpu(s) = &out_guard.inner {
            let typed: &[f32] = s.as_slice().unwrap();
            assert_eq!(typed, &[1.0, 2.0, 3.0, 4.0]);
        }
    }

    fn ok_kernel(
        _inputs: &[Arc<RwLock<Storage>>],
        _outputs: &mut [Arc<RwLock<Storage>>],
        _layouts: &[Layout],
        _params: &OpParams,
    ) -> Result<()> {
        Ok(())
    }

    fn ok_kernel_alt(
        _inputs: &[Arc<RwLock<Storage>>],
        _outputs: &mut [Arc<RwLock<Storage>>],
        _layouts: &[Layout],
        _params: &OpParams,
    ) -> Result<()> {
        Ok(())
    }

    /// Phase 7.6 step 9a: two distinct `KernelRef`s register against
    /// the same `(op, dtypes, backend)` key as sibling alternatives.
    /// `lookup_with_caps` returns the first; `lookup_alternatives`
    /// returns both in registration order.
    #[test]
    fn step_9a_two_alternatives_append_then_first_wins_on_legacy_lookup() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::BF16, DType::BF16, DType::BF16];
        table.register(OpKind::MatMul, &dts, BackendId::Cuda, ok_kernel);
        table.register(OpKind::MatMul, &dts, BackendId::Cuda, ok_kernel_alt);

        // Legacy single-impl lookup returns the first-registered.
        let (k1, _caps) = table
            .lookup_with_caps(OpKind::MatMul, &dts, BackendId::Cuda)
            .unwrap();
        assert_eq!(k1 as *const () as usize, ok_kernel as *const () as usize);

        // Multi-impl lookup returns both in registration order.
        let alts = table.lookup_alternatives(OpKind::MatMul, &dts, BackendId::Cuda);
        assert_eq!(alts.len(), 2);
        assert_eq!(
            alts[0].kernel as *const () as usize,
            ok_kernel as *const () as usize
        );
        assert_eq!(
            alts[1].kernel as *const () as usize,
            ok_kernel_alt as *const () as usize
        );

        // Step-9a accounting helpers stay consistent.
        assert_eq!(table.key_count(), 1, "one decision point");
        assert_eq!(table.len(), 2, "two alternatives total");
    }

    /// FKC gap-closure Task 2.1: hand-written registrations (the
    /// `register`/`register_full*` family that never touches the FKC
    /// importer) carry no declared cost-expression AST — `cost_expr`
    /// defaults to `None`. Only the FKC importer (a later Group-2 task)
    /// stamps a real `Some(&CompiledCostExpr)`.
    #[test]
    fn cost_expr_field_defaults_none_for_hand_written_registration() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
        let alts = table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu);
        assert_eq!(alts.len(), 1);
        assert!(
            alts[0].cost_expr.is_none(),
            "hand-written registrations carry no declared cost AST"
        );
    }

    /// Task 4.4: `iter_entries` mirrors `iter_precision`/`iter_cost` —
    /// yields one `(op, dtypes, backend, &entry)` tuple per registered
    /// alternative (both siblings at a shared key show up), and filters
    /// out `RuntimeFused` keys just like the precision/cost audit
    /// iterators (a "static-kernel audit view").
    #[test]
    fn iter_entries_yields_full_entries_and_filters_runtime_fused() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel_alt);
        // A runtime-fused key — must be excluded from `iter_entries`.
        table.register(
            BindingKey::RuntimeFused(FusedOpId(FusedOpId::RUNTIME_FUSED_BASE)),
            &dts,
            BackendId::Cpu,
            ok_kernel,
        );

        let entries: Vec<_> = table.iter_entries().collect();
        assert_eq!(
            entries.len(),
            2,
            "both AddElementwise alternatives, RuntimeFused excluded"
        );
        for (op, dtypes, backend, entry) in &entries {
            assert_eq!(*op, OpKind::AddElementwise);
            assert_eq!(*dtypes, dts.as_slice());
            assert_eq!(*backend, BackendId::Cpu);
            // Full entry is reachable, not just one projected field.
            let _ = entry.caps;
            let _ = entry.kernel_revision_hash;
        }
        let fns: Vec<usize> = entries
            .iter()
            .map(|(_, _, _, e)| e.kernel as *const () as usize)
            .collect();
        assert!(fns.contains(&(ok_kernel as *const () as usize)));
        assert!(fns.contains(&(ok_kernel_alt as *const () as usize)));
    }

    /// Never-panic (option B): registering the same `KernelRef` twice
    /// against one key is append-only — it does NOT panic at
    /// registration. `finalize()` detects the duplicate and returns
    /// `Err`. (Replaces the former `#[should_panic]` guard.)
    #[test]
    /// FKC-CLAUSE: FKC-10.10-0001 — the boundary itself: registration stays
    /// infallible and `finalize()` RETURNS the duplicate as an error rather than
    /// panicking. This is why the clause does not require a `Result` signature on
    /// `register_full_with_source`.
    fn step_9a_duplicate_kernel_ref_detected_by_finalize_not_panic() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel);
        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel); // dup — must NOT panic
        let err = table
            .finalize()
            .expect_err("finalize must report the duplicate KernelRef");
        assert!(
            format!("{err}").contains("duplicate KernelRef"),
            "unexpected error: {err}"
        );
    }

    /// Distinct sibling kernels under one key finalize cleanly.
    #[test]
    fn finalize_ok_for_distinct_sibling_kernels() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel);
        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel_alt);
        table
            .finalize()
            .expect("distinct sibling kernels must finalize OK");
    }

    /// Path 1a: a `broadcast_stride0: required` (baked-broadcast) kernel
    /// reads its operand as stride-0; a dense operand routed there
    /// miscomputes (reads `in[0]` repeatedly). The realize pick MUST skip
    /// such a sibling when a generic one exists, and — the load-bearing
    /// part — that exclusion must NOT rest on the generic being registered
    /// first. Register the broadcast-baked one FIRST (adversarial: `first()`
    /// would return it) and assert the pick returns the GENERIC sibling.
    #[test]
    fn lookup_with_caps_skips_broadcast_baked_sibling_regardless_of_order() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        // Baked-broadcast sibling registered FIRST.
        table.register_with_caps(
            OpKind::AddElementwise,
            &dts,
            BackendId::Cpu,
            ok_kernel,
            KernelCaps {
                strided_input: true,
                requires_broadcast: true,
            },
        );
        // Generic dense-safe sibling registered SECOND.
        table.register_with_caps(
            OpKind::AddElementwise,
            &dts,
            BackendId::Cpu,
            ok_kernel_alt,
            KernelCaps {
                strided_input: true,
                requires_broadcast: false,
            },
        );

        let (picked, caps) = table
            .lookup_with_caps(OpKind::AddElementwise, &dts, BackendId::Cpu)
            .expect("a dense-safe sibling exists");
        assert!(
            !caps.requires_broadcast,
            "the realize pick must never be a broadcast-baked kernel",
        );
        assert_eq!(
            picked as *const () as usize, ok_kernel_alt as *const () as usize,
            "lookup_with_caps must skip the broadcast-baked sibling and return the generic \
             one regardless of registration order",
        );
        // Both siblings coexist — the exclusion is at the pick, not at
        // registration (the baked-broadcast contract is still PRESENT).
        let alts = table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu);
        assert_eq!(
            alts.len(),
            2,
            "both siblings registered; neither is dropped"
        );
    }

    /// The corner that keeps the exclusion honest: if EVERY sibling is
    /// broadcast-baked (no dense-safe fallback), the pick is a surfaced
    /// miss (`NoBackendForOp`), NOT a silent wrong dense read.
    #[test]
    fn lookup_with_caps_misses_when_only_broadcast_baked_siblings_exist() {
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        table.register_with_caps(
            OpKind::AddElementwise,
            &dts,
            BackendId::Cpu,
            ok_kernel,
            KernelCaps {
                strided_input: true,
                requires_broadcast: true,
            },
        );
        assert!(
            table
                .lookup_with_caps(OpKind::AddElementwise, &dts, BackendId::Cpu)
                .is_err(),
            "no dense-safe sibling ⇒ honest miss, never a wrong read",
        );
    }

    /// Phase 7.6 step 9a: `fill_unset_cpu_precision` upgrades every
    /// alternative under a CPU key, not just the first.
    #[test]
    fn step_9a_fill_passes_touch_every_alternative() {
        use crate::fused::PrecisionGuarantee;
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];
        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel_alt);

        // Both start at UNAUDITED.
        for e in table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu) {
            assert!(!e.precision.bit_stable_on_same_hardware);
        }

        let bit_stable = PrecisionGuarantee {
            bit_stable_on_same_hardware: true,
            max_ulp: Some(0),
            max_relative: None,
            max_absolute: None,
            notes: "test default",
        };
        table.fill_unset_cpu_precision(bit_stable);

        // Both got upgraded.
        let alts = table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu);
        assert_eq!(alts.len(), 2);
        for e in alts {
            assert!(e.precision.bit_stable_on_same_hardware);
            assert_eq!(e.precision.max_ulp, Some(0));
        }
    }

    /// GAP-095: the CPU bulk fill must NOT overwrite an audited
    /// "no static bound applies" conclusion.
    ///
    /// `PrecisionGuarantee::none(reason)` and `PrecisionGuarantee::UNAUDITED`
    /// carry identical *values* — all four claim fields at their defaults —
    /// and differ only in `notes`. The fill's predicate was purely
    /// structural, so it conflated them and rewrote an explicit audit
    /// conclusion into `PRIMITIVE_DETERMINISTIC_CPU`: it asserted
    /// `bit_stable_on_same_hardware: true` on a kernel whose audit had
    /// specifically declined to claim it.
    ///
    /// FKC-4.8-0001 sanctions the fill for precision blocks left *absent* or
    /// `audited: false`. It does not sanction overwriting an `audited: true`
    /// + `none(reason)` conclusion.
    ///
    /// **Both directions asserted.** A test that only checked that
    /// `none(reason)` survives would also pass with the fill disabled
    /// outright, so the UNAUDITED sibling must still be upgraded in the same
    /// run — otherwise the test proves nothing about discrimination.
    #[test]
    fn gap_095_fill_preserves_audited_none_but_still_upgrades_unaudited() {
        use crate::fused::PrecisionGuarantee;
        use fuel_ir::probe::BackendId;
        let mut table = KernelBindingTable::new();
        let dts = [DType::F32, DType::F32, DType::F32];

        const REASON: &str = "audited: accumulation order is scheduler-dependent; \
                              no static bound applies";
        // The audited conclusion — must survive untouched.
        table.register_with_precision(
            OpKind::MatMul,
            &dts,
            BackendId::Cpu,
            ok_kernel,
            PrecisionGuarantee::none(REASON),
        );
        // The never-audited placeholder — must still be upgraded.
        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);

        table.fill_unset_cpu_precision(PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU);

        let audited = table
            .lookup_alternatives(OpKind::MatMul, &dts, BackendId::Cpu)
            .first()
            .expect("audited-none entry registered")
            .precision;
        assert!(
            !audited.bit_stable_on_same_hardware,
            "the fill asserted bit-stability on a kernel whose audit \
             explicitly concluded that no static bound applies",
        );
        assert_eq!(
            audited.notes, REASON,
            "the audit reason was overwritten — the reasoning that lived at \
             the registration site is gone",
        );

        // Opposite direction: the fill must still do its job.
        let placeholder = table
            .lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu)
            .first()
            .expect("unaudited entry registered")
            .precision;
        assert!(
            placeholder.bit_stable_on_same_hardware,
            "control FAILED: the UNAUDITED placeholder was not upgraded, so \
             this test would pass with the fill disabled entirely and proves \
             nothing about the discrimination it is meant to check",
        );
    }
}