dsfb-gpu-debug-cuda 0.1.0

CUDA FFI bridge and kernel dispatch for dsfb-gpu-debug. Builds without nvcc unless the `cuda` feature is set.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
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
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
//! Safe Rust wrapper over the CUDA pipeline FFI.
//!
//! Two entry points are exposed:
//!
//! * [`build_gpu`] — the plain end-to-end pipeline call. Suitable for the
//!   CLI's `run-gpu` subcommand and for any caller that just wants a
//!   `CaseFile` back. Internally requests no CUDA-event timing.
//! * [`build_gpu_timed`] — the same call, but it also returns
//!   `PipelineTimingsFfi` describing per-stage microsecond timings.
//!   Used by `dsfb-gpu-debug bench --detail` to surface where each
//!   pipeline run is actually spending time.
//!
//! Both routes share the same Q16.16 kernels, the same locked launch
//! geometry, and the same `cuda/kernels.cu` host wrapper. The only
//! difference is whether the wrapper's `cudaEventRecord` calls fire. The
//! C++ side guards them behind a `want_timings = (timings_out != nullptr)`
//! flag, so the no-timing path has zero event overhead.
//!
//! Hash-chain construction reuses `dsfb_gpu_debug_core::casefile::
//! build_from_artifacts` so the CPU and GPU paths feed the same hashing
//! function with the same canonical bytes — which is exactly what makes
//! per-stage byte-equivalent comparison possible.

#![cfg(feature = "cuda")]

use core::ffi::c_int;
use std::vec;
use std::vec::Vec;

use dsfb_gpu_debug_core::candidate::{CandidateConfig, CandidateInterval};
use dsfb_gpu_debug_core::casefile::{build_from_artifacts_with_mode, CaseFile, EmissionMode};
use dsfb_gpu_debug_core::consensus::ConsensusCell;
use dsfb_gpu_debug_core::contract::Contract;
use dsfb_gpu_debug_core::detector::{DetectorCell, DetectorThresholds};
use dsfb_gpu_debug_core::event::{GpuTraceEventCompact, TraceEvent};
use dsfb_gpu_debug_core::motif::DetectorProfile;
use dsfb_gpu_debug_core::residual::{Baseline, ResidualCell};
use dsfb_gpu_debug_core::sign::SignCell;
use dsfb_gpu_debug_core::window::{compute_features, WindowFeature};

use crate::ffi::{
    dsfb_gpu_run_pipeline, dsfb_gpu_run_pipeline_batched, dsfb_gpu_run_pipeline_on_workspace,
    DetectorThresholdsFfi, PipelineTimingsFfi,
};
use crate::workspace::{
    BatchedGpuWorkspace, GpuWorkspace, GraphCaptureStatus,
    MAX_CANDIDATES_PER_ENTITY as WS_MAX_CANDIDATES_PER_ENTITY,
};
use crate::GpuError;

/// Per-entity capacity for the candidate output buffer.
///
/// At the v0 contract (128 windows per entity) the maximum possible
/// candidate count per entity is `floor(N / min_length_windows) = 128`,
/// but the canonical bank only ever admits a handful, so 16 is a
/// comfortable upper bound that fits in a single warp's worth of
/// register storage on the device. Re-exported from
/// `workspace::MAX_CANDIDATES_PER_ENTITY` so both modules agree on the
/// value without a circular dependency.
const MAX_CANDIDATES_PER_ENTITY: i32 = WS_MAX_CANDIDATES_PER_ENTITY;

/// Per-stage CUDA-event timings (microseconds), re-exported from the FFI
/// module so callers don't have to depend on `ffi` directly.
pub type PipelineTimings = PipelineTimingsFfi;

/// Run the GPU pipeline end-to-end and return the case file. The CUDA
/// path picks up at the residual stage; windowing runs on the CPU per
/// the v0 contract simplification.
///
/// # Errors
///
/// Returns `GpuError::CudaUnavailable` when the crate was built without
/// the `cuda` feature; `GpuError::KernelFailed(code)` for non-zero
/// `cudaError_t` from any stage of the kernel pipeline.
pub fn build_gpu(events: &[TraceEvent], contract: &Contract) -> Result<CaseFile, GpuError> {
    let (case, _) = build_gpu_inner(events, contract, false)?;
    Ok(case)
}

/// Run the GPU pipeline and return both the case file and per-stage
/// CUDA-event timings.
///
/// Equivalent to `build_gpu` except that the host wrapper records
/// `cudaEvent_t` markers around alloc / H2D / each kernel / D2H / free
/// and reports microsecond elapsed times for each. Useful for the
/// `dsfb-gpu-debug bench --detail` mode and for diagnosing where a
/// regression lives.
///
/// # Errors
///
/// Same as [`build_gpu`].
pub fn build_gpu_timed(
    events: &[TraceEvent],
    contract: &Contract,
) -> Result<(CaseFile, PipelineTimings), GpuError> {
    build_gpu_inner(events, contract, true)
}

fn build_gpu_inner(
    events: &[TraceEvent],
    contract: &Contract,
    want_timings: bool,
) -> Result<(CaseFile, PipelineTimings), GpuError> {
    // 1. CPU-side windowing (deferred from the GPU per spec v0). Same
    //    canonical bytes the CPU pipeline would produce; this is what
    //    keeps the `window_feature` hash byte-identical between
    //    backends.
    let features: Vec<WindowFeature> = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );

    // 2. Allocate output buffers sized for the contract grid.
    let total: usize = (contract.n_entities as usize) * (contract.n_windows as usize);
    let mut residuals: Vec<ResidualCell> = vec![ResidualCell::default(); total];
    let mut signs: Vec<SignCell> = vec![SignCell::default(); total];
    let mut detectors: Vec<DetectorCell> = vec![DetectorCell::default(); total];
    let mut consensus: Vec<ConsensusCell> = vec![ConsensusCell::default(); total];
    let candidate_capacity = (contract.n_entities as usize) * (MAX_CANDIDATES_PER_ENTITY as usize);
    let mut candidate_buf: Vec<CandidateInterval> =
        vec![CandidateInterval::default(); candidate_capacity];
    let mut candidate_count: Vec<i32> = vec![0i32; contract.n_entities as usize];

    let thresholds = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_config = CandidateConfig::CANONICAL;

    let mut timings = PipelineTimings::default();
    let timings_ptr: *mut PipelineTimings = if want_timings {
        std::ptr::from_mut::<PipelineTimings>(&mut timings)
    } else {
        std::ptr::null_mut()
    };

    // 3. Call into the FFI. The unsafe block is the only place where the
    //    pipeline transitions across the CUDA boundary, and it is the
    //    only `unsafe` in the entire CUDA crate. The pointers are
    //    derived from Rust Vec storage and remain valid for the
    //    duration of the call; sizes match the kernel's expectations
    //    exactly.
    let status: c_int = call_kernel(
        &features,
        &mut residuals,
        &mut signs,
        &mut detectors,
        &mut consensus,
        &mut candidate_buf,
        &mut candidate_count,
        contract,
        &thresholds,
        baseline,
        &candidate_config,
        timings_ptr,
    );

    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // 4. Flatten the per-entity candidate slots into a single canonical-
    //    order vector. Walking entities in order, then taking the first
    //    `candidate_count[e]` slots, yields the same `(entity, start)`
    //    ordering the CPU candidate stage produces.
    let mut candidates: Vec<CandidateInterval> = Vec::with_capacity(candidate_capacity);
    for entity_id in 0..(contract.n_entities as usize) {
        let count = candidate_count[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(candidate_buf[base + i]);
        }
    }

    let case = build_from_artifacts_with_mode(
        events,
        contract,
        "cuda",
        EmissionMode::Audit,
        &features,
        &residuals,
        &signs,
        &detectors,
        &consensus,
        &candidates,
    );
    Ok((case, timings))
}

/// Run the GPU pipeline on a pre-allocated [`GpuWorkspace`].
///
/// This is the hot path for repeated invocations: the device buffers and
/// the host Vec storage have already been allocated, so the only
/// per-call overhead is the H2D / kernel launch / D2H sequence. Suitable
/// for the bench harness's measured iterations and for any deployment
/// that processes many fixtures in a row.
///
/// # Errors
///
/// `GpuError::InvalidInput` if the workspace was built for a different
/// fixture shape than the contract describes; otherwise the same error
/// surface as [`build_gpu`].
pub fn build_gpu_on_workspace(
    workspace: &mut GpuWorkspace,
    events: &[TraceEvent],
    contract: &Contract,
) -> Result<CaseFile, GpuError> {
    let (case, _) =
        build_gpu_on_workspace_inner(workspace, events, contract, EmissionMode::Audit, false)?;
    Ok(case)
}

/// Run the GPU pipeline on a workspace and emit the case file in
/// [`EmissionMode::Throughput`]. Identical kernel execution to
/// [`build_gpu_on_workspace`]; the only difference is the host-side
/// per-stage hash byte form, which uses the compact little-endian
/// representation in throughput mode.
///
/// # Errors
///
/// Same as [`build_gpu_on_workspace`].
pub fn build_gpu_throughput_on_workspace(
    workspace: &mut GpuWorkspace,
    events: &[TraceEvent],
    contract: &Contract,
) -> Result<CaseFile, GpuError> {
    let (case, _) =
        build_gpu_on_workspace_inner(workspace, events, contract, EmissionMode::Throughput, false)?;
    Ok(case)
}

/// Same as [`build_gpu_on_workspace`] but also returns per-stage
/// CUDA-event timings. Used by the bench's `--detail` path when a
/// workspace is in play.
///
/// # Errors
///
/// Same as [`build_gpu_on_workspace`].
pub fn build_gpu_timed_on_workspace(
    workspace: &mut GpuWorkspace,
    events: &[TraceEvent],
    contract: &Contract,
) -> Result<(CaseFile, PipelineTimings), GpuError> {
    build_gpu_on_workspace_inner(workspace, events, contract, EmissionMode::Audit, true)
}

fn build_gpu_on_workspace_inner(
    workspace: &mut GpuWorkspace,
    events: &[TraceEvent],
    contract: &Contract,
    mode: EmissionMode,
    want_timings: bool,
) -> Result<(CaseFile, PipelineTimings), GpuError> {
    workspace.assert_compatible(contract)?;

    // CPU-side windowing (deferred from the GPU per v0 contract).
    let features: Vec<WindowFeature> = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );

    let thresholds = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_config = CandidateConfig::CANONICAL;

    let mut timings = PipelineTimings::default();
    let timings_ptr: *mut PipelineTimings = if want_timings {
        std::ptr::from_mut::<PipelineTimings>(&mut timings)
    } else {
        std::ptr::null_mut()
    };

    // Safety: the workspace pointers are alive for the duration of this
    // call because we hold `&mut workspace`. The host buffers come from
    // the workspace itself (also borrowed mutably). The kernel function
    // mirrors the layouts statically verified on the C++ side.
    #[allow(unsafe_code)]
    let status = unsafe {
        dsfb_gpu_run_pipeline_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            features.as_ptr(),
            contract.n_entities as i32,
            contract.n_windows as i32,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref::<DetectorThresholdsFfi>(&thresholds),
            candidate_config.min_detector_count as i32,
            candidate_config.min_residual_q_raw,
            candidate_config.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            workspace.residuals.as_mut_ptr(),
            workspace.signs.as_mut_ptr(),
            workspace.detectors.as_mut_ptr(),
            workspace.consensus.as_mut_ptr(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            timings_ptr,
        )
    };

    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // Flatten the per-entity candidate slots into a single canonical-
    // order vector. Walking entities in order, then taking the first
    // `candidate_count[e]` slots, yields the same `(entity, start)`
    // ordering the CPU candidate stage produces.
    let mut candidates: Vec<CandidateInterval> = Vec::with_capacity(workspace.candidate_buf.len());
    for entity_id in 0..(contract.n_entities as usize) {
        let count = workspace.candidate_count[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(workspace.candidate_buf[base + i]);
        }
    }

    let case = build_from_artifacts_with_mode(
        events,
        contract,
        "cuda",
        mode,
        &features,
        &workspace.residuals,
        &workspace.signs,
        &workspace.detectors,
        &workspace.consensus,
        &candidates,
    );
    Ok((case, timings))
}

/// S-PERF.13 — bulk pack of `TraceEvent[]` into
/// `GpuTraceEventCompact[]`. Replaces the prior scalar
/// `for ... zip()` loop in the D64 `_timed` dispatchers'
/// input-staging slot (renamed `features_us` →
/// `host_input_staging_us`; see the
/// `D64ThroughputHostStageTimings` doc + the
/// S-PERF.13-PREFLIGHT receipt at
/// `reports/s_perf_13_preflight_d64_feature_path_audit.txt`).
///
/// **Byte-identical-output contract (panel-locked S-PERF.13
/// N8 + P6)**: every event packed by this function matches
/// the scalar `GpuTraceEventCompact::from_trace_event`
/// output byte-for-byte. Enforced by (a) the AVX2 path's
/// explicit field arithmetic mirroring the const fn's
/// bit-mask + branchless-error-bit logic verbatim, (b) the
/// scalar fallback's direct call into the const fn, and
/// (c) the `s_perf_13_pack_equivalence_tests` module which
/// proves equivalence across n = {0, 1, 7, 8, 9, 64, 257,
/// 4096, 65_537}. Determinism: no atomics, no FP, no
/// order-dependent reduction.
///
/// **Approach A — Phase 2 (AVX2 + non-temporal stores)**:
/// when AVX2 is statically enabled (`cfg(target_feature =
/// "avx2")`) the helper delegates to
/// `pack_events_to_pinned_avx2`, which packs **two events
/// per 256-bit non-temporal store** (`_mm256_stream_si256`).
/// The non-temporal store bypasses L1/L2 so the pinned-
/// shadow writes do not pollute the cache during the bulk
/// pack; `_mm_sfence` retires the streaming stores before
/// the FFI dispatch's H2D copy reads the pinned shadow.
///
/// **Scalar fallback** (`pack_events_to_pinned_scalar`)
/// preserves Phase 1's chunk-and-unroll shape; runs on
/// non-AVX2 builds (any non-x86_64 target, or x86_64
/// builds without `+avx2` in the target feature set) and
/// serves as the byte-identical reference the unit tests
/// compare the AVX2 path against.
///
/// **Memory-bound floor**: at the canonical 256×4096 K=1
/// fixture the per-dispatch traffic is 4.2M × (48 B read +
/// 16 B write) ≈ 267 MB. At DDR4-3200 ~51 GB/s peak the
/// theoretical floor is ~5.2 ms. The pre-S-PERF.13 scalar
/// iterator measured ~6.3 ms (1.2× over floor). The Phase
/// 2 win comes from cache-bypass (non-temporal stores
/// eliminate L1/L2 eviction during the pack) and explicit
/// pointer arithmetic (no iterator-chain overhead).
#[inline(always)]
#[allow(clippy::inline_always)]
fn pack_events_to_pinned_simd(events: &[TraceEvent], pinned: &mut [GpuTraceEventCompact]) {
    #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
    {
        // SAFETY: `pack_events_to_pinned_avx2` reads at most
        // `events.len()` items from `events` and writes at
        // most `min(events.len(), pinned.len())` items to
        // `pinned`; bounds enforced inside the helper.
        // `_mm256_stream_si256` requires 32 B alignment; the
        // helper checks the dst pointer alignment at runtime
        // and falls back to `_mm256_storeu_si256` (unaligned)
        // when not 32 B aligned (e.g. Vec<T> in tests).
        #[allow(unsafe_code)]
        unsafe {
            pack_events_to_pinned_avx2(events, pinned);
        }
    }
    #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))]
    pack_events_to_pinned_scalar(events, pinned);
}

/// S-PERF.13 scalar fallback. Phase 1 chunk-and-unroll
/// preserved for non-AVX2 hosts and as the byte-identical
/// reference the unit tests compare the AVX2 path against.
/// `#[allow(dead_code)]` because on AVX2 builds the
/// dispatcher routes through `pack_events_to_pinned_avx2`
/// and this fallback is unreachable; the
/// `s_perf_13_pack_equivalence_tests::pack_events_to_pinned_scalar`
/// shadow keeps the byte-identical reference exercised in
/// tests regardless of build target features.
#[inline(always)]
#[allow(dead_code)]
fn pack_events_to_pinned_scalar(events: &[TraceEvent], pinned: &mut [GpuTraceEventCompact]) {
    const CHUNK: usize = 8;
    let n = events.len().min(pinned.len());
    let full = (n / CHUNK) * CHUNK;
    let src_iter = events[..full].chunks_exact(CHUNK);
    let dst_iter = pinned[..full].chunks_exact_mut(CHUNK);
    for (src, dst) in src_iter.zip(dst_iter) {
        dst[0] = GpuTraceEventCompact::from_trace_event(&src[0]);
        dst[1] = GpuTraceEventCompact::from_trace_event(&src[1]);
        dst[2] = GpuTraceEventCompact::from_trace_event(&src[2]);
        dst[3] = GpuTraceEventCompact::from_trace_event(&src[3]);
        dst[4] = GpuTraceEventCompact::from_trace_event(&src[4]);
        dst[5] = GpuTraceEventCompact::from_trace_event(&src[5]);
        dst[6] = GpuTraceEventCompact::from_trace_event(&src[6]);
        dst[7] = GpuTraceEventCompact::from_trace_event(&src[7]);
    }
    for (dst, src) in pinned[full..n].iter_mut().zip(events[full..n].iter()) {
        *dst = GpuTraceEventCompact::from_trace_event(src);
    }
}

/// S-PERF.13 Phase 2 — AVX2 pack with adaptive store kind.
/// Processes events in pairs (2 × 16 B = 32 B = one 256-bit
/// store per pair). Uses `_mm256_stream_si256` (non-temporal,
/// bypasses L1/L2 caches) when the destination is 32-byte
/// aligned; otherwise falls back to `_mm256_storeu_si256`
/// (unaligned, works on any alignment but does not bypass
/// the cache). The production caller is the D64 `_timed`
/// dispatcher whose `events_pinned` buffer is allocated via
/// `cudaMallocHost`'s page-aligned allocator, so the
/// non-temporal store hits its fast path; the unaligned
/// fallback exists for the unit tests (which use
/// `Vec<GpuTraceEventCompact>` whose allocator only
/// guarantees the type's 8-byte alignment).
///
/// `_mm_sfence` retires non-temporal stores if any were
/// issued, before return, so the FFI dispatch that follows
/// reads coherent memory.
///
/// # Safety
///
/// Caller MUST guarantee:
///   - `events.as_ptr().add(i)` is valid for `i < events.len()`
///     (Rust slice invariant);
///   - `pinned.as_mut_ptr().add(i)` is valid for `i <
///     pinned.len()` (Rust slice invariant).
///
/// The pair-stride of 32 bytes is satisfied for any
/// `GpuTraceEventCompact[]` slice because the type's size
/// is 16 bytes (verified by the layout printout in the
/// S-PERF.13-PREFLIGHT receipt).
#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
#[target_feature(enable = "avx2")]
#[inline]
#[allow(unsafe_code)]
unsafe fn pack_events_to_pinned_avx2(events: &[TraceEvent], pinned: &mut [GpuTraceEventCompact]) {
    use core::arch::x86_64::{
        __m256i, _mm256_set_epi32, _mm256_storeu_si256, _mm256_stream_si256, _mm_sfence,
    };

    let n = events.len().min(pinned.len());
    let src_base = events.as_ptr();
    let dst_base = pinned.as_mut_ptr();

    // 32-byte alignment check: streaming stores require it;
    // unaligned stores accept anything. cudaMallocHost gives
    // us page-aligned memory (4 KiB) so the production path
    // hits the fast non-temporal store. Vec<T> in tests gives
    // us 8-byte alignment so the fallback runs there.
    let dst_addr = dst_base as usize;
    let use_stream = (dst_addr % 32) == 0;

    let pairs = n / 2;
    for pair_idx in 0..pairs {
        let i = pair_idx * 2;
        // SAFETY: i+1 < n, both within slice bounds.
        let e0 = src_base.add(i);
        let e1 = src_base.add(i + 1);
        // The cast widens alignment from `GpuTraceEventCompact`
        // (8 B) to `__m256i` (32 B). The 32 B alignment is NOT
        // statically guaranteed; the runtime `use_stream` check
        // below picks the unaligned-store intrinsic when the
        // dst pointer is not 32 B aligned, so the cast itself is
        // sound as long as the stream-store path is gated on the
        // alignment check.
        #[allow(clippy::cast_ptr_alignment)]
        let d_pair = dst_base.add(i).cast::<__m256i>();

        // Event 0 fields (branchless error bit).
        let ts0 = (*e0).ts_ns;
        let entity0 = (*e0).entity_id;
        let lat0 = (*e0).latency_us;
        let err0_bit: u32 = u32::from((*e0).error_code != 0) << 31;
        let ee0: u32 = (entity0 & GpuTraceEventCompact::ENTITY_MASK) | err0_bit;

        // Event 1 fields.
        let ts1 = (*e1).ts_ns;
        let entity1 = (*e1).entity_id;
        let lat1 = (*e1).latency_us;
        let err1_bit: u32 = u32::from((*e1).error_code != 0) << 31;
        let ee1: u32 = (entity1 & GpuTraceEventCompact::ENTITY_MASK) | err1_bit;

        // Pack into a 256-bit register. `_mm256_set_epi32`
        // takes 8 i32s in HIGH-to-LOW order. Layout
        // (low → high bytes within the 32-byte store):
        //   [ts0_lo, ts0_hi, ee0, lat0, ts1_lo, ts1_hi, ee1, lat1]
        // Each event occupies 16 bytes (matches
        // GpuTraceEventCompact's repr(C) layout).
        #[allow(clippy::cast_possible_wrap)]
        let v: __m256i = _mm256_set_epi32(
            lat1 as i32,
            ee1 as i32,
            (ts1 >> 32) as i32,
            (ts1 & 0xFFFF_FFFF) as i32,
            lat0 as i32,
            ee0 as i32,
            (ts0 >> 32) as i32,
            (ts0 & 0xFFFF_FFFF) as i32,
        );
        if use_stream {
            _mm256_stream_si256(d_pair, v);
        } else {
            _mm256_storeu_si256(d_pair, v);
        }
    }

    // Tail: handle last event if n is odd. Falls through to
    // the const fn.
    if n & 1 != 0 {
        let i = n - 1;
        let src_ref = &*src_base.add(i);
        *dst_base.add(i) = GpuTraceEventCompact::from_trace_event(src_ref);
    }

    // Retire non-temporal stores so subsequent reads (the
    // FFI dispatch's H2D copy) see coherent memory. Cheap
    // when no non-temporal stores were issued.
    if use_stream {
        _mm_sfence();
    }
}

#[allow(clippy::too_many_arguments)]
fn call_kernel(
    features: &[WindowFeature],
    residuals: &mut [ResidualCell],
    signs: &mut [SignCell],
    detectors: &mut [DetectorCell],
    consensus: &mut [ConsensusCell],
    candidate_buf: &mut [CandidateInterval],
    candidate_count: &mut [i32],
    contract: &Contract,
    thresholds: &DetectorThresholdsFfi,
    baseline: Baseline,
    candidate_config: &CandidateConfig,
    timings_out: *mut PipelineTimingsFfi,
) -> c_int {
    // Why this `unsafe`: this is the FFI boundary. We're calling a foreign
    // function whose signature is mirrored verbatim in
    // `cuda/kernels.cu::dsfb_gpu_run_pipeline`. All host pointers come
    // from Rust slices we own; the slices outlive the call. Struct
    // layouts are statically verified on the C++ side via
    // `static_assert`. `timings_out` is nullable; the C++ side checks
    // for null before recording events.
    #[allow(unsafe_code)]
    unsafe {
        dsfb_gpu_run_pipeline(
            features.as_ptr(),
            contract.n_entities as i32,
            contract.n_windows as i32,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref::<DetectorThresholdsFfi>(thresholds),
            candidate_config.min_detector_count as i32,
            candidate_config.min_residual_q_raw,
            candidate_config.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            residuals.as_mut_ptr(),
            signs.as_mut_ptr(),
            detectors.as_mut_ptr(),
            consensus.as_mut_ptr(),
            candidate_buf.as_mut_ptr(),
            candidate_count.as_mut_ptr(),
            timings_out,
        )
    }
}

/// Run the deterministic-inference pipeline on `n_catalogs`
/// independent fixtures in a single batched kernel launch (O.16,
/// Tier 2). Returns one `CaseFile` per catalog, in catalog-major
/// order.
///
/// All catalogs share the same `contract` (and therefore the same
/// canonical bank, detector registry, kernel sequence, and so on).
/// Per-catalog output bytes depend only on per-catalog input bytes;
/// corrupting one catalog's events only changes its own per-stage
/// hashes and case file.
///
/// The pipeline emits case files in [`EmissionMode::Throughput`]
/// because batching is exclusively a performance lever; the audit
/// path remains the single-catalog `build_gpu_on_workspace` route.
///
/// # Errors
///
/// `GpuError::InvalidInput` if `catalogs.len() != workspace.n_catalogs`
/// or any catalog's event-count does not match the contract grid.
/// `GpuError::KernelFailed(code)` for non-zero `cudaError_t` from the
/// kernel pipeline.
pub fn build_gpu_batched_throughput(
    workspace: &mut BatchedGpuWorkspace,
    catalogs: &[&[TraceEvent]],
    contract: &Contract,
) -> Result<Vec<CaseFile>, GpuError> {
    // 1. Shape checks.
    if catalogs.len() != workspace.n_catalogs as usize {
        return Err(GpuError::InvalidInput(
            "BatchedGpuWorkspace.n_catalogs does not match catalogs.len()",
        ));
    }
    if contract.n_entities != workspace.n_entities || contract.n_windows != workspace.n_windows {
        return Err(GpuError::InvalidInput(
            "BatchedGpuWorkspace dimensions do not match the supplied contract",
        ));
    }

    let n_catalogs = workspace.n_catalogs as usize;
    let per_catalog = workspace.per_catalog_cells();

    // 2. CPU-side windowing for each catalog. Writes feature cells into
    //    the workspace's catalog-major host buffer at offset
    //    `catalog_id * per_catalog`.
    let window_size_ns = u64::from(contract.window_size_ms) * 1_000_000;
    for (c_idx, &events) in catalogs.iter().enumerate() {
        let features_for_catalog = compute_features(
            events,
            contract.n_windows,
            contract.n_entities,
            window_size_ns,
        );
        let dst = &mut workspace.features[c_idx * per_catalog..(c_idx + 1) * per_catalog];
        dst.copy_from_slice(&features_for_catalog);
    }

    let thresholds = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_config = CandidateConfig::CANONICAL;

    // 3. Single batched FFI call. The unsafe block is the only place
    //    where we cross the CUDA boundary in the batched path.
    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        dsfb_gpu_run_pipeline_batched(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.features.as_ptr(),
            workspace.n_catalogs as i32,
            workspace.n_entities as i32,
            workspace.n_windows as i32,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref::<DetectorThresholdsFfi>(&thresholds),
            candidate_config.min_detector_count as i32,
            candidate_config.min_residual_q_raw,
            candidate_config.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            workspace.residuals.as_mut_ptr(),
            workspace.signs.as_mut_ptr(),
            workspace.detectors.as_mut_ptr(),
            workspace.consensus.as_mut_ptr(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            std::ptr::null_mut(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // 4. Per-catalog case-file assembly. The pipeline stages all live
    //    in catalog-major buffers; for each catalog we slice into its
    //    contiguous window and feed `build_from_artifacts_with_mode`
    //    exactly as the single-catalog path does.
    let per_candidate_slot = workspace.per_catalog_candidate_slots();
    let mut cases: Vec<CaseFile> = Vec::with_capacity(n_catalogs);
    for c_idx in 0..n_catalogs {
        let cell_range = c_idx * per_catalog..(c_idx + 1) * per_catalog;
        let count_range =
            c_idx * (workspace.n_entities as usize)..(c_idx + 1) * (workspace.n_entities as usize);
        let cand_range = c_idx * per_candidate_slot..(c_idx + 1) * per_candidate_slot;

        // Flatten the per-entity candidate slots for this catalog.
        let mut candidates: Vec<CandidateInterval> = Vec::new();
        let counts = &workspace.candidate_count[count_range];
        let slots = &workspace.candidate_buf[cand_range];
        for entity_id in 0..(workspace.n_entities as usize) {
            let count = counts[entity_id] as usize;
            let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
            for i in 0..count {
                candidates.push(slots[base + i]);
            }
        }

        let case = build_from_artifacts_with_mode(
            catalogs[c_idx],
            contract,
            "cuda",
            EmissionMode::Throughput,
            &workspace.features[cell_range.clone()],
            &workspace.residuals[cell_range.clone()],
            &workspace.signs[cell_range.clone()],
            &workspace.detectors[cell_range.clone()],
            &workspace.consensus[cell_range],
            &candidates,
        );
        cases.push(case);
    }
    Ok(cases)
}

/// Hash `bytes` with the `__device__` SHA-256 in `cuda/sha256.cuh`.
///
/// Used by `tests/device_sha256_self_test.rs` to verify byte-equality
/// with `dsfb_gpu_debug_core::hash::sha256` over three known-vector
/// inputs. The Tier 3B digest kernels use the same `__device__`
/// routine internally; if this self-test fails, the Tier 3B path is
/// not safe to enable. Single thread on device, so the wall time
/// is dominated by the one-off `cudaMalloc` / `cudaMemcpy` for the
/// input bytes.
///
/// # Errors
///
/// Returns `GpuError::KernelFailed(code)` for any non-zero
/// `cudaError_t` from the FFI.
pub fn sha256_device(bytes: &[u8]) -> Result<[u8; 32], GpuError> {
    let mut out = [0u8; 32];
    #[allow(unsafe_code)]
    let status = unsafe {
        crate::ffi::dsfb_gpu_sha256_self_test(bytes.as_ptr(), bytes.len() as u64, out.as_mut_ptr())
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }
    Ok(out)
}

/// Hash `bytes` with the streaming `__device__` SHA-256
/// (init/update/finalize) in `cuda/sha256.cuh`.
///
/// S-PERF.14b.1 Step 1 byte-stream equivalence helper. The
/// Rust acceptance test
/// `tests/s_perf_14b_1_streaming_sha_self_test.rs` cross-
/// validates `sha256_device_streaming(bytes)` against
/// `sha256_device(bytes)` (one-shot) over three known-vector
/// inputs (empty / 55 B / 64 KB) BEFORE the streaming helpers
/// are allowed to be consumed by the rewritten
/// `compact_densor_digest_v1_root_kernel_blockcoop`. If
/// streaming ≠ one-shot for any of the three vectors, the
/// streaming primitives are not safe to use in production —
/// the rewrite is blocked until the divergence is fixed.
///
/// # Errors
///
/// Returns `GpuError::KernelFailed(code)` for any non-zero
/// `cudaError_t` from the FFI.
pub fn sha256_device_streaming(bytes: &[u8]) -> Result<[u8; 32], GpuError> {
    let mut out = [0u8; 32];
    #[allow(unsafe_code)]
    let status = unsafe {
        crate::ffi::dsfb_gpu_sha256_streaming_self_test(
            bytes.as_ptr(),
            bytes.len() as u64,
            out.as_mut_ptr(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }
    Ok(out)
}

/// S-PERF.14b.1 v3 tile-size sweep wrapper: measures the mean
/// per-iteration wall (in nanoseconds) of the inactive Path 1b
/// v2 streaming root kernel at the given `tile_bytes`. The
/// kernel is launched `n_warmup` warm-up iterations + `n_timed`
/// timed iterations with cudaEvent_t bracketing; the mean per
/// timed iteration is returned.
///
/// Per the panel-locked backend-selection discipline (2026-05-18
/// post-v2-seal correction), Path 1b v2 is INACTIVE in
/// production until a tile size beats Path 1a's pinned 925.2 µs
/// per-call wall. This wrapper drives the sweep harness that
/// reports whether such a tile size exists.
///
/// # Errors
///
/// Returns `GpuError::KernelFailed(code)` for any non-zero
/// `cudaError_t` from the FFI (including
/// `cudaErrorInvalidValue` for zero-sized inputs).
pub fn compact_densor_root_streaming_sweep_time(
    n_chunks_per_catalog: u32,
    chunk_size: u32,
    stage_id: u32,
    n_catalogs: u32,
    tile_bytes: u32,
    n_warmup: i32,
    n_timed: i32,
) -> Result<u64, GpuError> {
    let mut mean_ns: u64 = 0;
    #[allow(unsafe_code)]
    let status = unsafe {
        crate::ffi::dsfb_gpu_compact_densor_root_streaming_sweep_time(
            n_chunks_per_catalog,
            chunk_size,
            stage_id,
            n_catalogs,
            tile_bytes,
            n_warmup,
            n_timed,
            &mut mean_ns,
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }
    Ok(mean_ns)
}

/// S-PERF.14b.1 v3 Path 1a baseline wrapper. Measures the
/// active production backend
/// (`compact_densor_digest_v1_root_kernel_blockcoop`) with
/// the same fixture-shape parameters the streaming sweep
/// uses, so the v3 sweep harness can compare per-stage walls
/// apples-to-apples.
///
/// # Errors
///
/// Returns `GpuError::KernelFailed(code)` for any non-zero
/// `cudaError_t` from the FFI.
pub fn compact_densor_root_path1a_sweep_time(
    n_chunks_per_catalog: u32,
    chunk_size: u32,
    stage_id: u32,
    n_catalogs: u32,
    n_warmup: i32,
    n_timed: i32,
) -> Result<u64, GpuError> {
    let mut mean_ns: u64 = 0;
    #[allow(unsafe_code)]
    let status = unsafe {
        crate::ffi::dsfb_gpu_compact_densor_root_path1a_sweep_time(
            n_chunks_per_catalog,
            chunk_size,
            stage_id,
            n_catalogs,
            n_warmup,
            n_timed,
            &mut mean_ns,
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }
    Ok(mean_ns)
}

/// Tier 3B (O.17) single-catalog dispatch: runs the pipeline on a
/// pre-existing workspace, then runs the four `__device__` SHA-256
/// digest kernels (residual / sign / detector / consensus) on the
/// device. The residual / sign / detector cell buffers are *not*
/// copied back to the host; their per-stage hash contributions to
/// the chain come from the precomputed device digests.
///
/// Determinism: the device digests are byte-equal to the host's
/// `hash_*_compact` digests by construction (pinned by
/// `device_sha256_self_test` and the cross-mode equivalence test).
/// The bank stage receives the consensus grid (copied back to host
/// because the axis-5 entity-locality gate requires it) and the
/// candidate intervals.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if `workspace` dimensions disagree
///   with `contract`.
/// * `GpuError::KernelFailed(code)` for any non-zero `cudaError_t`.
pub fn build_gpu_throughput_device_digests_on_workspace(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let mut stage_digests_host = [0u8; 4 * 32];

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_digests_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            features.as_ptr(),
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            // R.3b: consensus D2H stripped from the Tier 3B path. After
            // R.5 the bank's axis-5 evidence (entity_avg_q, grid_avg_q)
            // lives inside each CandidateInterval, so the consensus
            // grid is no longer needed host-side. Passing null tells
            // the C++ wrapper to skip the cudaMemcpy entirely.
            std::ptr::null_mut(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            stage_digests_host.as_mut_ptr(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // Flatten per-entity candidate slots to a contiguous Vec the bank
    // can iterate over.
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = workspace.candidate_count[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(workspace.candidate_buf[base + i]);
        }
    }

    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
            events,
            contract,
            "cuda",
            &features,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            // R.3b: bank no longer reads consensus after R.5 (axis-5
            // moved into CandidateInterval). Pass an empty slice so
            // any future regression that re-introduces consensus
            // iteration in the bank surfaces as an out-of-bounds
            // panic in the test sweep rather than silently passing.
            &[],
            &candidates,
        ),
    )
}

/// Tier 3B (O.17) batched dispatch: runs K catalogs through the
/// pipeline + parallel device digest kernels. K SHA-256 streams run
/// concurrently as K independent blocks on the device. Per-catalog
/// stage digests are copied back; residual / sign / detector cells
/// stay on the device for each catalog.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if `catalogs.len() != workspace.n_catalogs`
///   or workspace dimensions disagree with `contract`.
/// * `GpuError::KernelFailed(code)` for any non-zero `cudaError_t`.
#[allow(clippy::too_many_lines)]
pub fn build_gpu_batched_throughput_device_digests(
    workspace: &mut BatchedGpuWorkspace,
    catalogs: &[&[TraceEvent]],
    contract: &Contract,
) -> Result<Vec<CaseFile>, GpuError> {
    if catalogs.len() != workspace.n_catalogs as usize {
        return Err(GpuError::InvalidInput(
            "Catalog slice length does not match BatchedGpuWorkspace n_catalogs",
        ));
    }
    if contract.n_entities != workspace.n_entities || contract.n_windows != workspace.n_windows {
        return Err(GpuError::InvalidInput(
            "BatchedGpuWorkspace dimensions do not match the supplied contract",
        ));
    }

    let n_catalogs = catalogs.len();
    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let per_catalog = workspace.per_catalog_cells();
    let per_candidate_slot = workspace.per_catalog_candidate_slots();

    // Compose all catalogs' WindowFeature arrays into the workspace's
    // catalog-major host features buffer.
    for (c_idx, events) in catalogs.iter().enumerate() {
        let features = compute_features(
            events,
            contract.n_windows,
            contract.n_entities,
            u64::from(contract.window_size_ms) * 1_000_000,
        );
        let dst = &mut workspace.features[c_idx * per_catalog..(c_idx + 1) * per_catalog];
        dst.copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;
    let mut stage_digests_host = vec![0u8; 4 * 32 * n_catalogs];

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_batched_throughput_digests(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            workspace.features.as_ptr(),
            i32::try_from(n_catalogs).unwrap_or(i32::MAX),
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            // R.3b: consensus D2H stripped from the batched Tier 3B
            // path. After R.5 the bank's axis-5 evidence rides inside
            // each CandidateInterval; passing null tells the C++
            // wrapper to skip the per-catalog consensus cudaMemcpy.
            std::ptr::null_mut(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            stage_digests_host.as_mut_ptr(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // Per-catalog case-file assembly. Stage digests are stage-major:
    // [K residual][K sign][K detector][K consensus]. Slice them per
    // catalog and feed each to the Tier 3B casefile entry.
    let mut cases: Vec<CaseFile> = Vec::with_capacity(n_catalogs);
    for c_idx in 0..n_catalogs {
        let cell_range = c_idx * per_catalog..(c_idx + 1) * per_catalog;
        let count_range =
            c_idx * (workspace.n_entities as usize)..(c_idx + 1) * (workspace.n_entities as usize);
        let cand_range = c_idx * per_candidate_slot..(c_idx + 1) * per_candidate_slot;

        let mut candidates: Vec<CandidateInterval> = Vec::new();
        let counts = &workspace.candidate_count[count_range];
        let slots = &workspace.candidate_buf[cand_range];
        for entity_id in 0..(workspace.n_entities as usize) {
            let count = counts[entity_id] as usize;
            let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
            for i in 0..count {
                candidates.push(slots[base + i]);
            }
        }

        let mut residual_digest = [0u8; 32];
        let mut sign_digest = [0u8; 32];
        let mut detector_digest = [0u8; 32];
        let mut consensus_digest = [0u8; 32];
        // Stage-major digest layout: [K residual][K sign][K detector][K consensus].
        // Each stage block is `32 * n_catalogs` bytes; within a block we step by
        // `c_idx * 32` to reach this catalog's slot.
        let stage_block = 32 * n_catalogs;
        let r_base = c_idx * 32;
        let s_base = stage_block + c_idx * 32;
        let d_base = 2 * stage_block + c_idx * 32;
        let cn_base = 3 * stage_block + c_idx * 32;
        residual_digest.copy_from_slice(&stage_digests_host[r_base..r_base + 32]);
        sign_digest.copy_from_slice(&stage_digests_host[s_base..s_base + 32]);
        detector_digest.copy_from_slice(&stage_digests_host[d_base..d_base + 32]);
        consensus_digest.copy_from_slice(&stage_digests_host[cn_base..cn_base + 32]);

        let _ = cell_range; // R.3b: consensus slice no longer indexed.
        let case =
            dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
                catalogs[c_idx],
                contract,
                "cuda",
                &workspace.features[c_idx * per_catalog..(c_idx + 1) * per_catalog],
                residual_digest,
                sign_digest,
                detector_digest,
                consensus_digest,
                // R.3b: bank no longer reads consensus after R.5.
                &[],
                &candidates,
            );
        cases.push(case);
    }
    Ok(cases)
}

/// R.3a — Layer A (device evidence fabric) single-catalog dispatch.
///
/// What's different from `build_gpu_throughput_device_digests_on_workspace`:
/// returns a `CompactCaseSummary` (no admitted episodes, no
/// `final_case_file_hash`) instead of a `CaseFile`. Layer A's whole
/// reason for existing is to measure the evidence-fabric cost in
/// isolation — without the host bank stage. The Semantic Non-Bypass
/// Axiom holds by type: a caller of Layer A literally cannot obtain
/// admitted episodes from this function.
///
/// R.3a uses the same Tier 3B FFI under the hood; the difference is
/// purely host-side (skip the bank stage; emit a summary instead of a
/// case file). R.5 will refine the FFI to also stop copying the
/// consensus grid back when the bank stage is not needed. Until then,
/// the consensus D2H still happens — Layer A pays for it but does not
/// use the bytes.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if `workspace` dimensions disagree
///   with `contract`.
/// * `GpuError::KernelFailed(code)` for any non-zero `cudaError_t`.
pub fn build_gpu_layer_a_on_workspace(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<dsfb_gpu_debug_core::casefile::CompactCaseSummary, GpuError> {
    workspace.assert_compatible(contract)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let mut stage_digests_host = [0u8; 4 * 32];

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_digests_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            features.as_ptr(),
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            // R.3b: consensus D2H stripped from Layer A. After R.5 the
            // bank's axis-5 evidence rides inside each CandidateInterval
            // (entity_avg_q, grid_avg_q), so the consensus grid is no
            // longer needed host-side. Null tells the C++ wrapper to
            // skip the cudaMemcpy entirely — this is the largest single
            // D2H win Layer A pays for at scaled fixtures.
            std::ptr::null_mut(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            stage_digests_host.as_mut_ptr(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = workspace.candidate_count[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(workspace.candidate_buf[base + i]);
        }
    }

    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_compact_summary_from_device_digests(
            events,
            contract,
            "cuda",
            &features,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &candidates,
        ),
    )
}

/// R.3a — Layer A batched dispatch. Runs K independent catalogs
/// through the Tier 3B device-digests FFI and returns K
/// `CompactCaseSummary` records (one per catalog). No bank stage runs
/// for any catalog; this is the pure evidence-fabric throughput path.
///
/// Same host-side restructuring as the single-catalog Layer A: the
/// FFI is the existing batched Tier 3B path, but instead of folding
/// the resulting digests into K full case files, we fold them into K
/// compact summaries. The Semantic Non-Bypass Axiom holds at the type
/// level — the function cannot emit admitted episodes.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if `catalogs.len() != workspace.n_catalogs`
///   or workspace dimensions disagree with `contract`.
/// * `GpuError::KernelFailed(code)` for any non-zero `cudaError_t`.
#[allow(clippy::too_many_lines)]
pub fn build_gpu_layer_a_batched(
    workspace: &mut BatchedGpuWorkspace,
    catalogs: &[&[TraceEvent]],
    contract: &Contract,
) -> Result<Vec<dsfb_gpu_debug_core::casefile::CompactCaseSummary>, GpuError> {
    if catalogs.len() != workspace.n_catalogs as usize {
        return Err(GpuError::InvalidInput(
            "Catalog slice length does not match BatchedGpuWorkspace n_catalogs",
        ));
    }
    if contract.n_entities != workspace.n_entities || contract.n_windows != workspace.n_windows {
        return Err(GpuError::InvalidInput(
            "BatchedGpuWorkspace dimensions do not match the supplied contract",
        ));
    }

    let n_catalogs = catalogs.len();
    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let per_catalog = workspace.per_catalog_cells();
    let per_candidate_slot = workspace.per_catalog_candidate_slots();

    // Compose all catalogs' WindowFeature arrays into the workspace's
    // catalog-major host features buffer. Same pattern as the Tier 3B
    // batched case-file path.
    for (c_idx, events) in catalogs.iter().enumerate() {
        let features = compute_features(
            events,
            contract.n_windows,
            contract.n_entities,
            u64::from(contract.window_size_ms) * 1_000_000,
        );
        let dst = &mut workspace.features[c_idx * per_catalog..(c_idx + 1) * per_catalog];
        dst.copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;
    let mut stage_digests_host = vec![0u8; 4 * 32 * n_catalogs];

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_batched_throughput_digests(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            workspace.features.as_ptr(),
            i32::try_from(n_catalogs).unwrap_or(i32::MAX),
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            // R.3b: consensus D2H stripped from batched Layer A. Same
            // rationale as the single-catalog Layer A path above —
            // bank's axis-5 evidence rides in CandidateInterval after
            // R.5, so the per-catalog consensus cudaMemcpy is skipped.
            // At K=64 large fixture this drops 2 MB * 64 = 128 MB per
            // batched dispatch from the D2H bill.
            std::ptr::null_mut(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            stage_digests_host.as_mut_ptr(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let mut summaries: Vec<dsfb_gpu_debug_core::casefile::CompactCaseSummary> =
        Vec::with_capacity(n_catalogs);
    let stage_block = 32 * n_catalogs;
    for c_idx in 0..n_catalogs {
        let cell_range = c_idx * per_catalog..(c_idx + 1) * per_catalog;
        let count_range =
            c_idx * (workspace.n_entities as usize)..(c_idx + 1) * (workspace.n_entities as usize);
        let cand_range = c_idx * per_candidate_slot..(c_idx + 1) * per_candidate_slot;

        let mut candidates: Vec<CandidateInterval> = Vec::new();
        let counts = &workspace.candidate_count[count_range];
        let slots = &workspace.candidate_buf[cand_range];
        for entity_id in 0..(workspace.n_entities as usize) {
            let count = counts[entity_id] as usize;
            let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
            for i in 0..count {
                candidates.push(slots[base + i]);
            }
        }

        let mut residual_digest = [0u8; 32];
        let mut sign_digest = [0u8; 32];
        let mut detector_digest = [0u8; 32];
        let mut consensus_digest = [0u8; 32];
        let r_base = c_idx * 32;
        let s_base = stage_block + c_idx * 32;
        let d_base = 2 * stage_block + c_idx * 32;
        let cn_base = 3 * stage_block + c_idx * 32;
        residual_digest.copy_from_slice(&stage_digests_host[r_base..r_base + 32]);
        sign_digest.copy_from_slice(&stage_digests_host[s_base..s_base + 32]);
        detector_digest.copy_from_slice(&stage_digests_host[d_base..d_base + 32]);
        consensus_digest.copy_from_slice(&stage_digests_host[cn_base..cn_base + 32]);

        let summary = dsfb_gpu_debug_core::casefile::build_compact_summary_from_device_digests(
            catalogs[c_idx],
            contract,
            "cuda",
            &workspace.features[cell_range],
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &candidates,
        );
        summaries.push(summary);
    }
    Ok(summaries)
}

/// R.4 fused Throughput dispatch — same evidence chain and case-file
/// output as [`build_gpu_throughput_device_digests_on_workspace`], but
/// the residual and sign stages run as a Pre-Alpha EWMA kernel
/// followed by a cell-parallel fused R+S kernel. The fused path is
/// byte-preserving: the resulting `CaseFile.hashes` are bit-identical
/// to the un-fused reference on the same fixture and contract,
/// pinned by `fused_throughput_equivalence`.
///
/// At canonical 16×128 the parallelism win is small (sign was 16
/// threads, becomes 2 048). At scaled 256×4 096 the win is
/// substantial (sign was 256 threads, becomes 1 M+). The same R.5
/// candidate bytes, the same R.3b consensus-D2H strip, and the same
/// Audit mode hold.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if `workspace` dimensions disagree
///   with `contract`.
/// * `GpuError::KernelFailed(code)` for any non-zero `cudaError_t`.
pub fn build_gpu_fused_throughput_digests_on_workspace(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let mut stage_digests_host = [0u8; 4 * 32];

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_fused_throughput_digests_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            workspace.d_drifts(),
            features.as_ptr(),
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            // R.3b: consensus D2H stripped from the fused path too —
            // axis-5 rides inside CandidateInterval after R.5.
            std::ptr::null_mut(),
            workspace.candidate_buf.as_mut_ptr(),
            workspace.candidate_count.as_mut_ptr(),
            stage_digests_host.as_mut_ptr(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = workspace.candidate_count[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(workspace.candidate_buf[base + i]);
        }
    }

    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
            events,
            contract,
            "cuda",
            &features,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            // R.3b: bank no longer reads consensus after R.5.
            &[],
            &candidates,
        ),
    )
}

/// R.6b — opt-in pinned/async Throughput-digests dispatch.
///
/// Same evidence chain and case-file output as
/// [`build_gpu_throughput_device_digests_on_workspace`] (the
/// post-R.3b sync path), but the workspace's pinned host shadows
/// are used for H2D/D2H and every transfer + kernel launch goes
/// through the workspace's CUDA stream. A single
/// `cudaStreamSynchronize` at the end of the FFI call ensures the
/// caller observes a fully-drained workspace on return.
///
/// The workspace must be constructed with
/// [`GpuWorkspace::new_with_pinned_async`]; otherwise this entry
/// returns `GpuError::InvalidInput`. Single-stream by design — no
/// concurrent overlap with other dispatches and no double-buffering
/// in R.6b. CUDA Graph capture lands in R.6c.
///
/// Byte equivalence: a single CUDA stream executes its work in
/// program order, identical to default-stream behaviour. The case
/// file is bit-identical to the sync dispatch on the same fixture,
/// pinned by `r6b_pinned_async_equivalence`.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if `workspace` was not built with
///   pinned shadows + stream, or if dimensions disagree with
///   `contract`.
/// * `GpuError::KernelFailed(code)` for any non-zero `cudaError_t`
///   from a transfer, kernel launch, or `cudaStreamSynchronize`.
///
/// # Panics
///
/// The body uses `.expect()` on the pinned-shadow `Option`s after
/// the `has_pinned_async()` gate at the top of the function has
/// returned `true`. The expects are unreachable on any well-formed
/// workspace; they would only panic on a malformed workspace that
/// passes `has_pinned_async` but lacks one of the pinned shadows —
/// which the constructor invariant rules out.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;

    // Compute features into the workspace's pinned shadow. The
    // computed Vec is dropped immediately after copy; the pinned
    // buffer persists across dispatches as part of the workspace.
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    {
        let pinned = workspace
            .features_pinned
            .as_mut()
            .expect("has_pinned_async guarantees features_pinned is Some");
        pinned.as_mut_slice().copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let features_ptr = workspace
        .features_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_digests_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            features_ptr,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            // R.3b: consensus D2H stripped in this dispatch path
            // exactly as in the post-R.5 sync version.
            std::ptr::null_mut(),
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            // R.6d: opt-in constant-memory detector kernel when
            // the workspace successfully uploaded thresholds at
            // construction. Byte-equivalent fallback otherwise.
            c_int::from(workspace.has_const_thresholds()),
            // R.8: pass null — this entry does not collect per-stage
            // timings. The `_timed` variant below opts in.
            std::ptr::null_mut(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // Flatten per-entity candidate slots from the pinned shadows.
    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
            events,
            contract,
            "cuda",
            &features,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            // R.3b: bank no longer reads consensus after R.5.
            &[],
            &candidates,
        ),
    )
}

/// R.6c — Throughput-digests dispatch that prefers a captured
/// CUDA Graph when available and gracefully demotes to the R.6b
/// async path when graph capture is not. Byte-identical to the
/// R.6b reference under both branches: a captured launch only
/// replays the same kernel + memcpy topology recorded by R.6b's
/// FFI minus its terminal `cudaStreamSynchronize`.
///
/// The graph is **not** semantic. It only records the launch
/// plan against the workspace's pinned shadows and stream. The
/// CPU bank still admits episodes; the GPU still emits evidence.
/// The returned `GraphCaptureStatus` is recorded into the case
/// file's supplementary fields so the audit chain documents
/// which path produced the bytes.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if the workspace was not built
///   with `new_with_pinned_async`.
/// * `GpuError::KernelFailed` if the graph launch or the demoted
///   R.6b dispatch returns a non-zero CUDA error.
///
/// # Panics
///
/// Unreachable in practice: the `has_pinned_async` gate at the
/// top of the function guarantees the pinned shadow `expect`s
/// after a successful capture all succeed.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_graph_or_demote(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<(CaseFile, GraphCaptureStatus), GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }

    // Idempotent attempt to capture. If a graph already exists on
    // this workspace this returns `Captured` immediately; the
    // first call on a fresh workspace either succeeds or returns
    // `Demoted` with a cudaError reason.
    let status = workspace.try_capture_throughput_graph(contract)?;

    let plan_hash = match status {
        GraphCaptureStatus::Demoted { ref reason } => {
            // Fall back to the R.6b async path. Its case file is
            // byte-identical to the pageable/sync reference and
            // the graph_plan_hash supplementary fields stay None.
            let case = build_gpu_throughput_pinned_async_on_workspace(events, contract, workspace)?;
            return Ok((
                case,
                GraphCaptureStatus::Demoted {
                    reason: reason.clone(),
                },
            ));
        }
        GraphCaptureStatus::Captured { plan_hash } => plan_hash,
    };

    // Captured branch: stage features into the pinned shadow,
    // launch the captured graph on the workspace's stream, then
    // hash the host shadows into a CaseFile through the same
    // post-pipeline routine R.6b uses.
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    {
        let pinned = workspace
            .features_pinned
            .as_mut()
            .expect("has_pinned_async guarantees features_pinned is Some");
        pinned.as_mut_slice().copy_from_slice(&features);
    }

    let stream = workspace.stream_handle();
    let graph_exec = workspace.graph_exec();
    debug_assert!(graph_exec != 0, "Captured status implies graph_exec != 0");

    // Safety: graph_exec and stream are owned by `workspace` and
    // remain valid for the duration of this call. The FFI calls
    // `cudaGraphLaunch` then `cudaStreamSynchronize`, so on
    // return every pinned shadow is valid to read.
    #[allow(unsafe_code)]
    let launch_status: c_int =
        unsafe { crate::ffi::dsfb_gpu_launch_throughput_graph(graph_exec, stream) };
    if launch_status != 0 {
        return Err(GpuError::KernelFailed(launch_status));
    }

    // Flatten per-entity candidate slots from the pinned shadows.
    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("has_pinned_async guarantees candidate_count_pinned is Some")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("has_pinned_async guarantees candidates_pinned is Some")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("has_pinned_async guarantees stage_digests_pinned is Some")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    let case = dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
        events,
        contract,
        "cuda",
        &features,
        residual_digest,
        sign_digest,
        detector_digest,
        consensus_digest,
        // R.3b: bank no longer reads consensus after R.5.
        &[],
        &candidates,
    );
    Ok((case, GraphCaptureStatus::Captured { plan_hash }))
}

/// R.8 — host-side segment timings paired with the device per-stage
/// breakdown from `R8StageTimingsFfi`. Together they form the full
/// "where is time going" picture that R.7's 2.9× speedup at
/// 256x4096 K=64 doesn't surface on its own.
///
/// All fields are microseconds. `features_us` is the host wall time
/// of `compute_features`; `bank_and_finalize_us` is the host wall
/// time of `casefile::build_throughput_from_artifacts_and_device_digests`
/// which includes both the bank-admission loop and the case-file
/// hash-chain construction. Splitting those two further would
/// require core-crate plumbing that R.8 deliberately avoids per the
/// "no contract changes" rule in the plan.
#[derive(Copy, Clone, Debug, Default)]
pub struct R8HostStageTimings {
    /// Host wall time of the windowing / feature-aggregation step
    /// (`dsfb_gpu_debug_core::window::compute_features`).
    pub features_us: f32,
    /// Host wall time of the bank admission + case-file hash chain
    /// finalisation step
    /// (`casefile::build_throughput_from_artifacts_and_device_digests`).
    pub bank_and_finalize_us: f32,
}

/// R.8 — public alias for the FFI struct that carries cudaEvent-derived
/// per-stage microseconds back from the C++ wrapper. Re-exported under
/// the cleaner `R8StageTimings` name so external callers (the bench,
/// any future internal user) do not need to refer to the `Ffi` suffix.
/// All fields are documented on the underlying type in `ffi.rs`.
pub use crate::ffi::R8StageTimingsFfi as R8StageTimings;

/// R.8 — same byte-for-byte dispatch as
/// `build_gpu_throughput_pinned_async_on_workspace`, but the C++
/// wrapper records cudaEvent timings around each kernel + memcpy
/// and the Rust side times the host segments (`compute_features`,
/// bank admission + finalisation). Returns the case file plus
/// both timing structs so the bench can produce the per-stage
/// percent-of-time table required by R.8.
///
/// # Errors
///
/// Same as the non-timed entry; this routine only adds the
/// instrumentation. The case file's bytes are identical to the
/// non-timed path under the same inputs and the same workspace
/// const-thresholds state.
///
/// # Panics
///
/// Unreachable: the `has_pinned_async` gate keeps the
/// `expect()` calls on pinned shadows reachable only when the
/// pinned shadows are `Some`.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_timed(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<(CaseFile, R8StageTimings, R8HostStageTimings), GpuError> {
    use std::time::Instant;
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;

    // ---- host: feature generation timing -------------------------
    let t_features = Instant::now();
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    // Convert via u64 → f32 to dodge clippy's u128 → f32 precision
    // warning. The host-side feature-generation wall is well under
    // 1 second at every supported scale (~10 µs canonical, ~1 ms
    // scale-large) so the u64-narrowing cast is loss-free in
    // practice.
    #[allow(clippy::cast_precision_loss)]
    let features_us = (t_features.elapsed().as_nanos() as u64) as f32 / 1_000.0_f32;

    {
        let pinned = workspace
            .features_pinned
            .as_mut()
            .expect("has_pinned_async guarantees features_pinned is Some");
        pinned.as_mut_slice().copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let features_ptr = workspace
        .features_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();

    let mut device_timings = R8StageTimings::default();

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_digests_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            features_ptr,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            std::ptr::null_mut(),
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            c_int::from(workspace.has_const_thresholds()),
            std::ptr::from_mut(&mut device_timings),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    // ---- host: bank admission + case-file hash chain timing -------
    let t_bank = Instant::now();
    let case = dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
        events,
        contract,
        "cuda",
        &features,
        residual_digest,
        sign_digest,
        detector_digest,
        consensus_digest,
        &[],
        &candidates,
    );
    // Same u128 → u64 → f32 narrowing as `features_us` above; the
    // post-launch host work is also well under 1 second so the
    // cast is loss-free.
    #[allow(clippy::cast_precision_loss)]
    let bank_and_finalize_us = (t_bank.elapsed().as_nanos() as u64) as f32 / 1_000.0_f32;

    let host_timings = R8HostStageTimings {
        features_us,
        bank_and_finalize_us,
    };

    Ok((case, device_timings, host_timings))
}

/// R.8.5 — Throughput-mode tree-digest dispatch.
///
/// Same residual / sign / detector / consensus / candidate kernels
/// as `build_gpu_throughput_pinned_async_on_workspace`, but the 4
/// single-thread `*_digest_kernel_batched` kernels are replaced
/// with the deterministic block-parallel tree digest from R.8.5
/// (`cuda/kernels.cu::tree_digest_leaf_kernel` + `_root_kernel`).
///
/// Produces a case file whose 4 stage hashes are
/// INTENTIONALLY DIFFERENT from the serial-digest path's stage
/// hashes — the tree digest commits to (canonical chunked stage
/// bytes || domain separator) rather than (canonical stage
/// bytes) alone. The case file's supplementary metadata records
/// `digest_mode = tree_sha256_v1`, `chunk_size`, and
/// `chunk_count` per stage so replay catches a mode-mismatched
/// receipt at validation time.
///
/// K=1 only at v0. K>1 batched tree digest needs the batched FFI
/// extended with leaf-arena strides; deferred to a follow-up.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if the workspace was not built with
///   `new_with_pinned_async` (no pinned shadows + stream) or the
///   contract's dimensions disagree with the workspace's allocation.
/// * `GpuError::KernelFailed` if any kernel returns a non-zero
///   `cudaError_t`. The dispatch returns immediately on error; the
///   case file is not built in that case.
///
/// # Panics
///
/// Unreachable in practice: the `has_pinned_async` gate keeps the
/// pinned shadow `expect()` calls reachable only when the shadows
/// are `Some`.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_tree(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;

    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    {
        let pinned = workspace
            .features_pinned
            .as_mut()
            .expect("has_pinned_async guarantees features_pinned is Some");
        pinned.as_mut_slice().copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let features_ptr = workspace
        .features_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_tree_digests_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            features_ptr,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            c_int::from(workspace.has_const_thresholds()),
            tree_chunk_size,
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    // Flatten per-entity candidate slots from the pinned shadows.
    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    // Hand the device-computed stage digests to the shared
    // case-file builder. The builder hashes them in canonical order
    // into the chain just like the serial-digest path; only the
    // bytes feeding the chain differ.
    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_from_artifacts_and_device_digests(
            events,
            contract,
            "cuda",
            &features,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &[],
            &candidates,
        ),
    )
}

/// R.11 — Throughput-mode dispatch + compact verdict finalizer.
///
/// Same kernel path as
/// `build_gpu_throughput_pinned_async_on_workspace_tree` (R.8.5
/// tree digest on the GPU side), but the host post-launch step
/// uses
/// `dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests`
/// with caller-supplied `FixtureHashes`, skipping the ~250 MB of
/// scalar SHA-256 work that the non-compact builder did per
/// dispatch (re-hashing ~4 M events + ~1 M window features).
///
/// The case file emitted here is **byte-identical** to the one
/// `build_gpu_throughput_pinned_async_on_workspace_tree` would
/// have produced for the same fixture, because `FixtureHashes`
/// carries the same canonical hashes the non-compact path would
/// have re-computed. The compact builder does not weaken any
/// chain commitment — it only reuses precomputed values.
///
/// **Semantic Non-Bypass Axiom**: unchanged. Episodes are still
/// admitted via the bank module's private `BankAdmissionToken`
/// constructor; the compact path only changes WHERE the input
/// commitment bytes come from (precomputed vs. recomputed), not
/// who can mint an admitted episode.
///
/// # Errors
///
/// Same as the non-compact tree-digest dispatch.
///
/// # Panics
///
/// Unreachable in practice — the `has_pinned_async` and
/// `has_tree_digest` gates keep the pinned-shadow `expect()`
/// calls reachable only when the shadows are `Some`.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_tree_compact(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
    fixture: &dsfb_gpu_debug_core::casefile::FixtureHashes,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;

    // `compute_features` still has to run because the GPU pipeline
    // needs the populated `WindowFeature[]` to consume. The
    // FixtureHashes saves us from re-hashing those features, not
    // from generating them. At v0 fixture generation is a small
    // fraction of wall (1.6 % at 256x4096 K=1 per R.8); a future
    // optimisation could cache the features themselves on the
    // workspace, but that is out of R.11's scope.
    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    {
        let pinned = workspace
            .features_pinned
            .as_mut()
            .expect("has_pinned_async guarantees features_pinned is Some");
        pinned.as_mut_slice().copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let features_ptr = workspace
        .features_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_tree_digests_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors(),
            workspace.d_consensus(),
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            features_ptr,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            c_int::from(workspace.has_const_thresholds()),
            tree_chunk_size,
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    // The compact verdict builder skips the ~250 MB of host
    // SHA-256 work that the non-compact builder did. Same chain
    // semantics; the FixtureHashes contract guarantees the bytes
    // committed to are identical.
    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests(
            contract,
            "cuda",
            fixture,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &candidates,
        ),
    )
}

/// R.9.b.2 — GPU wide-mask detector dispatch (D64).
///
/// Computes features on the host, uploads them to the workspace's
/// pinned shadow, runs residual + drift/slew sign + the D64
/// wide-mask detector kernel on the GPU, and copies the resulting
/// `DetectorCellWide[]` back to the caller. Stops at the detector
/// stage so the parity test against the CPU reference is precise.
///
/// **Byte equivalence**: every output cell's `DetectorMask2048`
/// matches the CPU `dsfb_gpu_debug_core::detector::evaluate_wide(
/// DetectorProfile::D64, ...)` byte-for-byte. The R.9.b parity
/// test pins this invariant at the canonical fixture; the same
/// math runs at every scale.
///
/// **Memory budget honesty**: `DetectorCellWide` is 264 bytes per
/// cell. At canonical 16×128 that's 540 KB per catalog; at
/// 256×4096 it's ~270 MB. K > 1 batched wide dispatch is R.9.c+
/// work — for now the buffer is sized for K = 1 single-catalog,
/// and `ensure_wide_detector_buffer` returns
/// `GpuError::KernelFailed` if the device allocation refuses.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if the workspace was not built with
///   `new_with_pinned_async`.
/// * `GpuError::KernelFailed` if the wide-detector buffer allocation
///   refuses or any kernel returns a non-zero `cudaError_t`.
///
/// # Panics
///
/// Unreachable in practice — every pinned-shadow `expect()` after
/// the `has_pinned_async` gate.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn evaluate_detector_wide_d64_on_workspace(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
) -> Result<Vec<dsfb_gpu_debug_core::detector::DetectorCellWide>, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    workspace.ensure_wide_detector_buffer()?;
    // R.10a — axis-5 grid-sum precompute buffer used by the wide
    // candidate kernel's flush loop.
    workspace.ensure_axis5_grid_sum_buffer()?;
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer that
    // backs the cell-parallel `drift_slew_sign_kernel_cellpar`
    // split. Workspace-resident i32 [n_entities × n_windows]
    // per catalog (4 MB at canonical 256×4096). Replaces the
    // legacy monolithic `drift_slew_sign_kernel`'s register-
    // carried EWMA state.
    workspace.ensure_drift_buffer()?;
    // R.10b — compact-wide-detector-digest-v1 arena that feeds the
    // detector-stage tree digest. Wide cells stay on device; only
    // the bytes the digest hashes change shape (264 → 18 B/cell).
    workspace.ensure_detector_digest_compact_buffer()?;
    // R.10c — parallel-candidate-collapse scratch buffers
    // (fired flags + boundary tuples). Inputs to the new
    // candidate_fired/boundary/pack kernels that replaced the
    // entity-serial wide candidate-collapse kernel.
    workspace.ensure_candidate_parallel_buffers()?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let n_cells = contract.n_entities as usize * contract.n_windows as usize;

    let features = compute_features(
        events,
        contract.n_windows,
        contract.n_entities,
        u64::from(contract.window_size_ms) * 1_000_000,
    );
    {
        let pinned = workspace
            .features_pinned
            .as_mut()
            .expect("has_pinned_async guarantees features_pinned is Some");
        pinned.as_mut_slice().copy_from_slice(&features);
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;

    let features_ptr = workspace
        .features_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let stream = workspace.stream_handle();

    // Host output buffer for the wide cells. 270 MB at scale-large;
    // we allocate on heap because the size is unknown at compile time
    // and stack-allocating ~270 MB would blow the stack.
    let mut host_wide: Vec<dsfb_gpu_debug_core::detector::DetectorCellWide> =
        vec![dsfb_gpu_debug_core::detector::DetectorCellWide::default(); n_cells];

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_evaluate_detector_wide_d64_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            workspace.d_detectors_wide(),
            features_ptr,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            host_wide.as_mut_ptr(),
            stream,
            0, // R.9.b.2: const-thresholds flag reserved for future use
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    Ok(host_wide)
}

/// R.9.b.3 — full D64 Throughput dispatch (wide detector +
/// projected consensus + projected candidate + tree digest of
/// wide stage bytes + compact verdict finalizer).
///
/// Routes the same pinned/async stream as
/// `build_gpu_throughput_pinned_async_on_workspace_tree_compact`
/// but uses the wide-mask GPU kernels with on-device OR projection
/// to the canonical 16-motif basis. The bank ABI is unchanged —
/// `CandidateInterval::union_mask` arrives at the host with the
/// projected u16 mask folded into its u32 layout, exactly as the
/// D16 path produces it.
///
/// **Semantic Non-Bypass**: every admitted episode goes through
/// `bank_collapse` which mints the `BankAdmissionToken`. R.9.b.3
/// does not introduce a new admission path; it only changes what
/// detector evidence the bank sees.
///
/// **Case-file divergence from D16**: the chain's detector,
/// consensus, candidate, and episode hashes all differ from the
/// D16 path because the OR projection produces a richer mask
/// (⊇ canonical 16-mask). The contract's
/// `detector_registry_hash` should be pinned to
/// `DetectorProfile::D64.registry_hash()` by the caller so the
/// `detector_registry` chain link matches the active profile.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if the workspace lacks pinned
///   shadows or tree-digest scratch.
/// * `GpuError::KernelFailed` if any kernel returns a non-zero
///   `cudaError_t` or the wide-detector buffer allocation refuses.
///
/// # Panics
///
/// Unreachable — every `expect()` after the workspace gates.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_d64_tree_compact(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
    fixture: &dsfb_gpu_debug_core::casefile::FixtureHashes,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }
    workspace.ensure_wide_detector_buffer()?;
    // R.10a — axis-5 grid-sum precompute buffer used by the wide
    // candidate kernel's flush loop.
    workspace.ensure_axis5_grid_sum_buffer()?;
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer that
    // backs the cell-parallel `drift_slew_sign_kernel_cellpar`
    // split. Workspace-resident i32 [n_entities × n_windows]
    // per catalog (4 MB at canonical 256×4096). Replaces the
    // legacy monolithic `drift_slew_sign_kernel`'s register-
    // carried EWMA state.
    workspace.ensure_drift_buffer()?;
    // R.10b — compact-wide-detector-digest-v1 arena that feeds the
    // detector-stage tree digest. Wide cells stay on device; only
    // the bytes the digest hashes change shape (264 → 18 B/cell).
    workspace.ensure_detector_digest_compact_buffer()?;
    // R.10c — parallel-candidate-collapse scratch buffers
    // (fired flags + boundary tuples). Inputs to the new
    // candidate_fired/boundary/pack kernels that replaced the
    // entity-serial wide candidate-collapse kernel.
    workspace.ensure_candidate_parallel_buffers()?;
    // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch
    // (`d_candidate_run_buffer` + `d_candidate_run_count`). The
    // D64 _timed FFI swap replaces the legacy single-kernel
    // `candidate_boundary_kernel_wide` with the new
    // `candidate_boundary_precompute_kernel` + `candidate_boundary_cellpar_emit_kernel`
    // pair to break the 2.1 %-occupancy ceiling.
    workspace.ensure_candidate_run_buffer()?;
    // R.11b — events buffer + pinned host shadow sized for the
    // actual input event count. compute_features no longer runs on
    // the host; the kernel builds WindowFeature[] on-device from
    // the events H2D'd here.
    workspace.ensure_events_buffer(events.len() as u64)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let window_size_ns: u64 = u64::from(contract.window_size_ms) * 1_000_000;

    // R.11b — copy events into the pinned shadow. The dispatch's
    // FFI then triggers a single cudaMemcpyAsync H2D from this
    // page-locked source. The pinned shadow is sized exactly to
    // the workspace's current event capacity; copy the leading
    // `events.len()` entries.
    {
        let pinned = workspace
            .events_pinned
            .as_mut()
            .expect("ensure_events_buffer guarantees events_pinned is Some");
        let slice = pinned.as_mut_slice();
        // R.11c — pack each event into its 16-byte compact form as
        // we copy. The audit `TraceEvent[]` slice is untouched; the
        // pinned shadow holds the throughput-mode projection.
        for (dst, src) in slice.iter_mut().zip(events.iter()).take(events.len()) {
            *dst = GpuTraceEventCompact::from_trace_event(src);
        }
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let events_ptr = workspace
        .events_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();
    let d_detectors_wide = workspace.d_detectors_wide();
    // Cast workspace's i64 grid-sum buffer pointer. cudaMalloc
    // returns ≥256-byte aligned memory; the localised allow keeps
    // clippy happy without a global override.
    #[allow(clippy::cast_ptr_alignment)]
    let d_axis5_grid_sum = workspace.d_axis5_grid_sum().cast::<i64>();
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer.
    let d_drift_buffer = workspace.d_drift_buffer();
    // R.10b — compact-wide-detector-digest-v1 arena pointer + the
    // two u16 metadata values the pack kernel folds into each
    // 18-byte compact record.
    let d_detector_digest_compact = workspace.d_detector_digest_compact();
    let d_candidate_fired = workspace.d_candidate_fired();
    let d_candidate_boundaries = workspace.d_candidate_boundaries();
    // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch pointers
    // (workspace-resident; ~33 KB at canonical 256 × 4 096 K=1). Allocated
    // by `workspace.ensure_candidate_run_buffer()` above; passed through
    // the D64 _timed FFI so the new `candidate_boundary_precompute_kernel`
    // can write run boundaries here and the `candidate_boundary_cellpar_emit_kernel`
    // can publish them into the legacy `d_candidate_boundaries` slot table.
    let d_candidate_run_buffer = workspace.d_candidate_run_buffer();
    let d_candidate_run_count = workspace.d_candidate_run_count();
    let d_events = workspace.d_events();
    let n_events_u64 = events.len() as u64;
    // R.11b — `ticks_per_event_ns` follows the structured-fixture
    // derivation: `(n_windows × window_size_ns) / n_events`. Both
    // `synthesize` and `synthesize_scaled` produce events whose
    // `ts_ns = i × ticks_per_event_ns`. The kernel uses this stride
    // to find candidate event indices per cell.
    let ticks_per_event_ns: u64 = if n_events_u64 == 0 {
        1
    } else {
        (u64::from(contract.n_windows) * window_size_ns) / n_events_u64
    };
    let profile_id_i32 = DetectorProfile::D64.active_detector_count() as i32;
    let wide_mask_words_used_i32 = DetectorProfile::D64.mask_word_count() as i32;

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_d64_tree_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            d_drift_buffer,
            d_detectors_wide,
            workspace.d_consensus(),
            d_axis5_grid_sum,
            d_detector_digest_compact,
            d_candidate_fired,
            d_candidate_boundaries,
            // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch.
            d_candidate_run_buffer,
            d_candidate_run_count,
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            d_events,
            events_ptr,
            n_events_u64,
            ticks_per_event_ns,
            window_size_ns,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            profile_id_i32,
            wide_mask_words_used_i32,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            tree_chunk_size,
            0, // digest_mode_id = 0 (TreeSha256V1 — S-PERF.11 default path)
            std::ptr::null_mut(),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests(
            contract,
            "cuda",
            fixture,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &candidates,
        ),
    )
}

/// R.9.d.1 — full D128 Throughput dispatch.
///
/// Mirrors `build_gpu_throughput_pinned_async_on_workspace_d64_tree_
/// compact` but routes through the D128 kernels: 16 motifs × 8
/// threshold-scaled variants per cell. Bridge invariants from R.9.b
/// extended: D128.V0..V3 scales mirror D64.V0..V3 bit-for-bit, so
/// D128 OR-projection ⊇ D64 OR-projection ⊇ canonical D16. The
/// bank ABI is unchanged.
///
/// **Scope-locked**: this dispatch does NOT apply the R.10b
/// compact-wide-detector-digest pack. The detector tree-digest
/// hashes the full 264-byte `DetectorCellWide` stride. R.10b for
/// D128 is intentionally deferred to R.9.d.1-followup so the
/// post-D128 R.12b sweep can measure where the new bottleneck is
/// before optimising blind.
///
/// **Case-file divergence from D64**: the chain's
/// `detector_registry` link binds to
/// `DetectorProfile::D128.registry_hash()` if the caller pinned it
/// on the contract; the consensus / candidate / final hashes also
/// differ because the OR-projected mask sees more cells fire under
/// D128's wider variant set than under D64's.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if the workspace lacks pinned
///   shadows, tree-digest scratch, or the events buffer.
/// * `GpuError::KernelFailed` on any non-zero `cudaError_t` or
///   on wide-detector / events / axis-5 / candidate-parallel
///   buffer allocation refusal.
///
/// # Panics
///
/// Unreachable — every `expect()` runs after a workspace gate
/// proves the pinned shadow is `Some`.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_d128_tree_compact(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
    fixture: &dsfb_gpu_debug_core::casefile::FixtureHashes,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }
    workspace.ensure_wide_detector_buffer()?;
    workspace.ensure_axis5_grid_sum_buffer()?;
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer that
    // backs the cell-parallel `drift_slew_sign_kernel_cellpar`
    // split. Workspace-resident i32 [n_entities × n_windows]
    // per catalog (4 MB at canonical 256×4096). Replaces the
    // legacy monolithic `drift_slew_sign_kernel`'s register-
    // carried EWMA state.
    workspace.ensure_drift_buffer()?;
    workspace.ensure_candidate_parallel_buffers()?;
    workspace.ensure_events_buffer(events.len() as u64)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let window_size_ns: u64 = u64::from(contract.window_size_ms) * 1_000_000;

    // Pack events into the R.11c compact form and stage them in the
    // pinned host shadow before the FFI's H2D.
    {
        let pinned = workspace
            .events_pinned
            .as_mut()
            .expect("ensure_events_buffer guarantees events_pinned is Some");
        let slice = pinned.as_mut_slice();
        for (dst, src) in slice.iter_mut().zip(events.iter()).take(events.len()) {
            *dst = GpuTraceEventCompact::from_trace_event(src);
        }
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let events_ptr = workspace
        .events_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();
    let d_detectors_wide = workspace.d_detectors_wide();
    #[allow(clippy::cast_ptr_alignment)]
    let d_axis5_grid_sum = workspace.d_axis5_grid_sum().cast::<i64>();
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer.
    let _d_drift_buffer = workspace.d_drift_buffer(); // allocated for D64 workspace compatibility; D128 FFI does not consume it
    let d_candidate_fired = workspace.d_candidate_fired();
    let d_candidate_boundaries = workspace.d_candidate_boundaries();
    let d_events = workspace.d_events();
    let n_events_u64 = events.len() as u64;
    let ticks_per_event_ns: u64 = if n_events_u64 == 0 {
        1
    } else {
        (u64::from(contract.n_windows) * window_size_ns) / n_events_u64
    };

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_d128_tree_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            d_detectors_wide,
            workspace.d_consensus(),
            d_axis5_grid_sum,
            d_candidate_fired,
            d_candidate_boundaries,
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            d_events,
            events_ptr,
            n_events_u64,
            ticks_per_event_ns,
            window_size_ns,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            tree_chunk_size,
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests(
            contract,
            "cuda",
            fixture,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &candidates,
        ),
    )
}

/// R.9.d.2.1 — D205 wide-mask throughput dispatch.
///
/// Mirrors `build_gpu_throughput_pinned_async_on_workspace_d128_
/// tree_compact` kernel-for-kernel; the only difference is that
/// the three profile-specific kernels
/// (`detector_motif_kernel_wide_d205`,
/// `consensus_grid_kernel_wide_d205`,
/// `candidate_pack_kernel_wide_d205`) replace their D128
/// counterparts and use a 13-variant scaled-threshold table with
/// an active-bit gate `det_id < 205`.
///
/// **D205 is a scaling-ladder byte-equivalence proof, NOT a new
/// performance headline.** The D64 ≈55× full-pipeline campaign
/// reduction at the courthouse-factory workload remains the R.13
/// headline. R.9.d.2.1 closes the asymmetry left by R.9.d.2
/// (CPU D205 landed; GPU D205 deferred): every supported
/// profile now has a CPU + GPU byte-equivalent execution path.
///
/// Bridge invariants (panel-locked, pinned by acceptance tests):
/// the D205 GPU mask matches the D205 CPU mask cell-for-cell;
/// D205 V0-only projection equals canonical D16; D205 OR ⊇ D128
/// OR ⊇ D64 OR ⊇ canonical D16; high bits ≥ 205 are
/// deterministically zero; D205 popcount never exceeds 205.
///
/// **Scope-locked**: this dispatch does NOT apply the R.10b
/// compact-wide-detector-digest pack. The detector tree-digest
/// hashes the full 264-byte `DetectorCellWide` stride. R.10b for
/// D205 is intentionally deferred (it would be a separate
/// optimisation commit). The same workspace fields used by D128
/// are reused — `DetectorCellWide` is profile-independent at 264
/// bytes per cell.
///
/// **Case-file divergence from D128**: the chain's
/// `detector_registry` link binds to
/// `DetectorProfile::D205.registry_hash()` if the caller pinned
/// it on the contract; consensus / candidate / final hashes also
/// differ because D205's additional V8..V12 variants can set
/// firings the D128 mask did not, while keeping V0..V7
/// byte-identical.
///
/// # Errors
///
/// * `GpuError::InvalidInput` if the workspace lacks pinned
///   shadows, tree-digest scratch, or the events buffer.
/// * `GpuError::KernelFailed` on any non-zero `cudaError_t` or
///   on wide-detector / events / axis-5 / candidate-parallel
///   buffer allocation refusal.
///
/// # Panics
///
/// Unreachable — every `expect()` runs after a workspace gate
/// proves the pinned shadow is `Some`.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_d205_tree_compact(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
    fixture: &dsfb_gpu_debug_core::casefile::FixtureHashes,
) -> Result<CaseFile, GpuError> {
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }
    workspace.ensure_wide_detector_buffer()?;
    workspace.ensure_axis5_grid_sum_buffer()?;
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer that
    // backs the cell-parallel `drift_slew_sign_kernel_cellpar`
    // split. Workspace-resident i32 [n_entities × n_windows]
    // per catalog (4 MB at canonical 256×4096). Replaces the
    // legacy monolithic `drift_slew_sign_kernel`'s register-
    // carried EWMA state.
    workspace.ensure_drift_buffer()?;
    workspace.ensure_candidate_parallel_buffers()?;
    workspace.ensure_events_buffer(events.len() as u64)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let window_size_ns: u64 = u64::from(contract.window_size_ms) * 1_000_000;

    // Pack events into the R.11c compact form and stage them in the
    // pinned host shadow before the FFI's H2D.
    {
        let pinned = workspace
            .events_pinned
            .as_mut()
            .expect("ensure_events_buffer guarantees events_pinned is Some");
        let slice = pinned.as_mut_slice();
        for (dst, src) in slice.iter_mut().zip(events.iter()).take(events.len()) {
            *dst = GpuTraceEventCompact::from_trace_event(src);
        }
    }

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let events_ptr = workspace
        .events_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();
    let d_detectors_wide = workspace.d_detectors_wide();
    #[allow(clippy::cast_ptr_alignment)]
    let d_axis5_grid_sum = workspace.d_axis5_grid_sum().cast::<i64>();
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer.
    let _d_drift_buffer = workspace.d_drift_buffer(); // allocated for D64 workspace compatibility; D205 FFI does not consume it
    let d_candidate_fired = workspace.d_candidate_fired();
    let d_candidate_boundaries = workspace.d_candidate_boundaries();
    let d_events = workspace.d_events();
    let n_events_u64 = events.len() as u64;
    let ticks_per_event_ns: u64 = if n_events_u64 == 0 {
        1
    } else {
        (u64::from(contract.n_windows) * window_size_ns) / n_events_u64
    };

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_d205_tree_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            d_detectors_wide,
            workspace.d_consensus(),
            d_axis5_grid_sum,
            d_candidate_fired,
            d_candidate_boundaries,
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            d_events,
            events_ptr,
            n_events_u64,
            ticks_per_event_ns,
            window_size_ns,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            tree_chunk_size,
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    Ok(
        dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests(
            contract,
            "cuda",
            fixture,
            residual_digest,
            sign_digest,
            detector_digest,
            consensus_digest,
            &candidates,
        ),
    )
}

/// R.9.c-diagnostic — host-side timing partner for the D64
/// throughput tree-compact path. Mirrors `R8HostStageTimings` but
/// applies to the D64 throughput dispatch: feature generation on
/// the host before the FFI and bank admission + case-file
/// finalisation on the host after.
///
/// Together with `D64ThroughputStageTimings` this is the full
/// "where is the time going" picture for the D64 throughput path,
/// which the R.9.b.3 timing report identified as the next target
/// for analysis. Splitting bank vs case-file further would require
/// core-crate plumbing intentionally avoided in this diagnostic-only
/// commit.
#[derive(Copy, Clone, Debug, Default)]
pub struct D64ThroughputHostStageTimings {
    /// Host wall time of the input pack-to-pinned staging step
    /// (TraceEvent[] -> GpuTraceEventCompact[] copy into the
    /// workspace's pinned events shadow before the FFI dispatch).
    ///
    /// **Renamed at S-PERF.13** (panel-locked 2026-05-18,
    /// post-S-PERF.13-PREFLIGHT). Previously named `features_us`,
    /// which mislabelled the slot: R.11b already moved
    /// `window::compute_features` to device via
    /// `window_feature_kernel_structured`. This slot has never
    /// timed host feature math on the D64 `_timed` path; it has
    /// always measured input event materialisation into the
    /// pinned shadow. See
    /// `reports/s_perf_13_preflight_d64_feature_path_audit.txt`
    /// for the full audit verdict
    /// (`FeaturePathMixedHostStagingDeviceCompute`).
    pub host_input_staging_us: f32,
    /// Host wall time of
    /// `casefile::build_throughput_compact_verdict_from_device_digests`,
    /// which folds the 4 device digests + compact candidate
    /// descriptors + precomputed `FixtureHashes` into the 12-link
    /// chain. Includes the bank admission loop.
    pub bank_and_finalize_us: f32,
}

/// R.9.c-diagnostic — public alias for the FFI struct that carries
/// cudaEvent-derived per-stage microseconds back from the C++
/// wrapper. Re-exported under the cleaner
/// `D64ThroughputStageTimings` name so external callers don't see
/// the `Ffi` suffix. Field docs live on the underlying type.
pub use crate::ffi::D64ThroughputStageTimingsFfi as D64ThroughputStageTimings;

/// R.9.c-diagnostic — same dispatch as
/// `build_gpu_throughput_pinned_async_on_workspace_d64_tree_compact`,
/// but the C++ wrapper records cudaEvents around each kernel and
/// memcpy, and the Rust side times the host segments. Returns the
/// case file plus both timing structs.
///
/// **No byte-changes**: the case-file output is identical to the
/// non-timed entry; the events ride along on the captured stream
/// and the elapsed-time math runs after `cudaStreamSynchronize`,
/// outside the byte path. This is the load-bearing property that
/// keeps the R.9.b.3 reproducibility invariants intact.
///
/// # Errors
///
/// Same as the non-timed entry; this routine only adds the
/// instrumentation.
///
/// # Panics
///
/// Unreachable: the `has_pinned_async` gate keeps the
/// `expect()` calls on pinned shadows reachable only when the
/// pinned shadows are `Some`.
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_d64_tree_compact_timed(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
    fixture: &dsfb_gpu_debug_core::casefile::FixtureHashes,
) -> Result<
    (
        CaseFile,
        D64ThroughputStageTimings,
        D64ThroughputHostStageTimings,
    ),
    GpuError,
> {
    use std::time::Instant;
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }
    workspace.ensure_wide_detector_buffer()?;
    // R.10a — axis-5 grid-sum precompute buffer used by the wide
    // candidate kernel's flush loop.
    workspace.ensure_axis5_grid_sum_buffer()?;
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer that
    // backs the cell-parallel `drift_slew_sign_kernel_cellpar`
    // split. Workspace-resident i32 [n_entities × n_windows]
    // per catalog (4 MB at canonical 256×4096). Replaces the
    // legacy monolithic `drift_slew_sign_kernel`'s register-
    // carried EWMA state.
    workspace.ensure_drift_buffer()?;
    // R.10b — compact-wide-detector-digest-v1 arena that feeds the
    // detector-stage tree digest. Wide cells stay on device; only
    // the bytes the digest hashes change shape (264 → 18 B/cell).
    workspace.ensure_detector_digest_compact_buffer()?;
    // R.10c — parallel-candidate-collapse scratch buffers
    // (fired flags + boundary tuples). Inputs to the new
    // candidate_fired/boundary/pack kernels that replaced the
    // entity-serial wide candidate-collapse kernel.
    workspace.ensure_candidate_parallel_buffers()?;
    // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch.
    workspace.ensure_candidate_run_buffer()?;
    // R.11b — events buffer + pinned host shadow sized for the
    // actual input event count. compute_features no longer runs on
    // the host; the kernel builds WindowFeature[] on-device.
    workspace.ensure_events_buffer(events.len() as u64)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let window_size_ns: u64 = u64::from(contract.window_size_ms) * 1_000_000;

    // ---- host: input pack-to-pinned staging timing ---------------
    //
    // **S-PERF.13 (panel-locked 2026-05-18) — this slot measures
    // INPUT STAGING, NOT host compute_features.** R.11b moved
    // feature math to device under `window_feature_kernel_structured`
    // (launched inside the FFI path at cuda/kernels.cu around line
    // 5026-5028). What remains here is the
    // `TraceEvent` (48 B) -> `GpuTraceEventCompact` (16 B)
    // projection into the pinned events shadow before the FFI
    // dispatch. The historical `features_us` field name was renamed
    // to `host_input_staging_us` at S-PERF.13 seal time after the
    // S-PERF.13-PREFLIGHT audit verdict
    // (`FeaturePathMixedHostStagingDeviceCompute`); see
    // `reports/s_perf_13_preflight_d64_feature_path_audit.txt` for
    // the full evidence chain.
    //
    // The pack loop below (`for (dst, src) in slice.iter_mut().zip(...)`)
    // is the SIMD-acceleration target for S-PERF.13: at the canonical
    // 256x4096 K=1 fixture (~4.2M events x 16 B = ~67 MB) the manual
    // iterator chain emits scalar stores that inhibit
    // autovectorization. The byte-identical packed-shadow output
    // contract is what permits SIMD/chunk-unroll without perturbing
    // any downstream hash or episode count.
    let t_input_staging = Instant::now();
    {
        let pinned = workspace
            .events_pinned
            .as_mut()
            .expect("ensure_events_buffer guarantees events_pinned is Some");
        let slice = pinned.as_mut_slice();
        // S-PERF.13 — bulk SIMD-friendly pack. Replaces the prior
        // scalar `for ... zip()` loop. The helper packs in chunks
        // of 8 with hand-unrolled stores; LLVM can prove the
        // chunk length and emit AVX2 vector stores on x86_64
        // Haswell+. R.11c projection semantics are unchanged
        // (every chunk-element is packed by the same
        // `GpuTraceEventCompact::from_trace_event` const fn);
        // pinned-shadow output is byte-identical to the scalar
        // path — panel-locked S-PERF.13 N8 + P6 contract.
        pack_events_to_pinned_simd(events, slice);
    }
    #[allow(clippy::cast_precision_loss)]
    let host_input_staging_us = (t_input_staging.elapsed().as_nanos() as u64) as f32 / 1_000.0_f32;

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let events_ptr = workspace
        .events_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();
    let d_detectors_wide = workspace.d_detectors_wide();
    #[allow(clippy::cast_ptr_alignment)]
    let d_axis5_grid_sum = workspace.d_axis5_grid_sum().cast::<i64>();
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer.
    let d_drift_buffer = workspace.d_drift_buffer();
    let d_detector_digest_compact = workspace.d_detector_digest_compact();
    let d_candidate_fired = workspace.d_candidate_fired();
    let d_candidate_boundaries = workspace.d_candidate_boundaries();
    // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch pointers
    // (workspace-resident; ~33 KB at canonical 256 × 4 096 K=1). Allocated
    // by `workspace.ensure_candidate_run_buffer()` above; passed through
    // the D64 _timed FFI so the new `candidate_boundary_precompute_kernel`
    // can write run boundaries here and the `candidate_boundary_cellpar_emit_kernel`
    // can publish them into the legacy `d_candidate_boundaries` slot table.
    let d_candidate_run_buffer = workspace.d_candidate_run_buffer();
    let d_candidate_run_count = workspace.d_candidate_run_count();
    let d_events = workspace.d_events();
    let n_events_u64 = events.len() as u64;
    let ticks_per_event_ns: u64 = if n_events_u64 == 0 {
        1
    } else {
        (u64::from(contract.n_windows) * window_size_ns) / n_events_u64
    };
    let profile_id_i32 = DetectorProfile::D64.active_detector_count() as i32;
    let wide_mask_words_used_i32 = DetectorProfile::D64.mask_word_count() as i32;

    let mut device_timings = D64ThroughputStageTimings::default();

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_d64_tree_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            d_drift_buffer,
            d_detectors_wide,
            workspace.d_consensus(),
            d_axis5_grid_sum,
            d_detector_digest_compact,
            d_candidate_fired,
            d_candidate_boundaries,
            // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch.
            d_candidate_run_buffer,
            d_candidate_run_count,
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            d_events,
            events_ptr,
            n_events_u64,
            ticks_per_event_ns,
            window_size_ns,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            profile_id_i32,
            wide_mask_words_used_i32,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            tree_chunk_size,
            0, // digest_mode_id = 0 (TreeSha256V1 — S-PERF.11 default path)
            std::ptr::from_mut(&mut device_timings),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    // ---- host: bank admission + case-file finalize timing --------
    let t_bank = Instant::now();
    let case = dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests(
        contract,
        "cuda",
        fixture,
        residual_digest,
        sign_digest,
        detector_digest,
        consensus_digest,
        &candidates,
    );
    #[allow(clippy::cast_precision_loss)]
    let bank_and_finalize_us = (t_bank.elapsed().as_nanos() as u64) as f32 / 1_000.0_f32;

    let host_timings = D64ThroughputHostStageTimings {
        host_input_staging_us,
        bank_and_finalize_us,
    };

    Ok((case, device_timings, host_timings))
}

/// S-PERF.12 — D64 Throughput pipeline using the
/// **CompactDensorDigestV1** throughput-mode digest. Mirrors
/// [`build_gpu_throughput_pinned_async_on_workspace_d64_tree_compact_timed`]
/// in every respect except the digest mode: the four per-stage
/// tree digests are produced by
/// [`cuda::compact_densor_digest_v1_leaf_kernel`] +
/// [`cuda::compact_densor_digest_v1_root_kernel`] which hash a
/// deterministic XOR-fold-by-4 compact projection of each
/// chunk rather than the raw chunk bytes. Per-stage root
/// digests under this mode are NOT byte-identical to
/// `TreeSha256V1` — the canonical domain header
/// (`DSFB_STAGE_COMPACT_DENSOR_V1`, 28 bytes) and the per-leaf
/// SHA inputs are both different — and S-PERF.10's
/// `digest_mode_non_aliasing_law` covers this: each declared
/// throughput-digest mode owns its own root-byte projection.
///
/// Same court semantics as the TreeSha256V1 path:
///
/// * Same candidate descriptors (the bank stage runs on
///   `CandidateInterval` byte slices, not on digest bytes).
/// * R.12b episode counts 13 / 89 / 1917 preserved.
/// * Audit mode (SerialSha256) is untouched.
///
/// # Errors
///
/// Same as the tree-compact-timed entry: returns
/// [`GpuError::InvalidInput`] if the workspace lacks pinned/
/// async + tree-digest scratch arenas, and [`GpuError::KernelFailed`]
/// on any CUDA error.
///
/// # Panics
///
/// Unreachable: same `expect()` discipline as
/// [`build_gpu_throughput_pinned_async_on_workspace_d64_tree_compact_timed`].
#[allow(clippy::expect_used, clippy::too_many_lines)]
pub fn build_gpu_throughput_pinned_async_on_workspace_d64_compact_densor_compact_timed(
    events: &[TraceEvent],
    contract: &Contract,
    workspace: &mut GpuWorkspace,
    fixture: &dsfb_gpu_debug_core::casefile::FixtureHashes,
) -> Result<
    (
        CaseFile,
        D64ThroughputStageTimings,
        D64ThroughputHostStageTimings,
    ),
    GpuError,
> {
    use std::time::Instant;
    workspace.assert_compatible(contract)?;
    if !workspace.has_pinned_async() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace was not built with pinned shadows + stream; \
             call GpuWorkspace::new_with_pinned_async(contract)",
        ));
    }
    if !workspace.has_tree_digest() {
        return Err(GpuError::InvalidInput(
            "GpuWorkspace tree-digest scratch is not allocated; \
             call GpuWorkspace::new_with_pinned_async(contract) on a fresh workspace",
        ));
    }
    workspace.ensure_wide_detector_buffer()?;
    workspace.ensure_axis5_grid_sum_buffer()?;
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer that
    // backs the cell-parallel `drift_slew_sign_kernel_cellpar`
    // split. Workspace-resident i32 [n_entities × n_windows]
    // per catalog (4 MB at canonical 256×4096). Replaces the
    // legacy monolithic `drift_slew_sign_kernel`'s register-
    // carried EWMA state.
    workspace.ensure_drift_buffer()?;
    workspace.ensure_detector_digest_compact_buffer()?;
    workspace.ensure_candidate_parallel_buffers()?;
    // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch.
    workspace.ensure_candidate_run_buffer()?;
    workspace.ensure_events_buffer(events.len() as u64)?;

    let n_entities = contract.n_entities as i32;
    let n_windows = contract.n_windows as i32;
    let window_size_ns: u64 = u64::from(contract.window_size_ms) * 1_000_000;

    // ---- host: input pack-to-pinned staging timing ---------------
    //
    // **S-PERF.13 (panel-locked 2026-05-18) — this slot measures
    // INPUT STAGING, NOT host compute_features.** R.11b moved
    // feature math to device under `window_feature_kernel_structured`
    // (launched inside the FFI path). The pack loop below is the
    // SIMD-acceleration target for S-PERF.13; the byte-identical
    // packed-shadow output contract permits SIMD/chunk-unroll
    // without perturbing any downstream hash or episode count.
    // See the tree-compact-timed sibling above + the
    // S-PERF.13-PREFLIGHT receipt for the full audit chain.
    let t_input_staging = Instant::now();
    {
        let pinned = workspace
            .events_pinned
            .as_mut()
            .expect("ensure_events_buffer guarantees events_pinned is Some");
        let slice = pinned.as_mut_slice();
        // S-PERF.13 — bulk SIMD-friendly pack (see the
        // tree-compact-timed sibling above for the full doc + the
        // panel-locked byte-identical-output contract).
        pack_events_to_pinned_simd(events, slice);
    }
    #[allow(clippy::cast_precision_loss)]
    let host_input_staging_us = (t_input_staging.elapsed().as_nanos() as u64) as f32 / 1_000.0_f32;

    let thresholds_ffi = DetectorThresholdsFfi::from(&DetectorThresholds::CANONICAL);
    let baseline = Baseline::CANONICAL;
    let candidate_cfg = CandidateConfig::CANONICAL;

    let events_ptr = workspace
        .events_pinned
        .as_ref()
        .expect("guarded above")
        .as_ptr();
    let candidates_ptr = workspace
        .candidates_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let count_ptr = workspace
        .candidate_count_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let digests_ptr = workspace
        .stage_digests_pinned
        .as_mut()
        .expect("guarded above")
        .as_mut_ptr();
    let stream = workspace.stream_handle();
    let tree_chunk_size = workspace.tree_chunk_size();
    let tree_leaves_stride = workspace.tree_leaves_stride_bytes();
    let tree_scratch_stride = workspace.tree_scratch_stride_bytes();
    let d_tree_leaves = workspace.d_tree_leaves();
    let d_tree_scratch = workspace.d_tree_scratch();
    let d_detectors_wide = workspace.d_detectors_wide();
    #[allow(clippy::cast_ptr_alignment)]
    let d_axis5_grid_sum = workspace.d_axis5_grid_sum().cast::<i64>();
    // S-PERF.14 — Pre-Alpha drift EWMA precompute buffer.
    let d_drift_buffer = workspace.d_drift_buffer();
    let d_detector_digest_compact = workspace.d_detector_digest_compact();
    let d_candidate_fired = workspace.d_candidate_fired();
    let d_candidate_boundaries = workspace.d_candidate_boundaries();
    // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch pointers
    // (workspace-resident; ~33 KB at canonical 256 × 4 096 K=1). Allocated
    // by `workspace.ensure_candidate_run_buffer()` above; passed through
    // the D64 _timed FFI so the new `candidate_boundary_precompute_kernel`
    // can write run boundaries here and the `candidate_boundary_cellpar_emit_kernel`
    // can publish them into the legacy `d_candidate_boundaries` slot table.
    let d_candidate_run_buffer = workspace.d_candidate_run_buffer();
    let d_candidate_run_count = workspace.d_candidate_run_count();
    let d_events = workspace.d_events();
    let n_events_u64 = events.len() as u64;
    let ticks_per_event_ns: u64 = if n_events_u64 == 0 {
        1
    } else {
        (u64::from(contract.n_windows) * window_size_ns) / n_events_u64
    };
    let profile_id_i32 = DetectorProfile::D64.active_detector_count() as i32;
    let wide_mask_words_used_i32 = DetectorProfile::D64.mask_word_count() as i32;

    let mut device_timings = D64ThroughputStageTimings::default();

    #[allow(unsafe_code)]
    let status: c_int = unsafe {
        crate::ffi::dsfb_gpu_run_pipeline_throughput_d64_tree_async_on_workspace(
            workspace.d_features(),
            workspace.d_residuals(),
            workspace.d_signs(),
            d_drift_buffer,
            d_detectors_wide,
            workspace.d_consensus(),
            d_axis5_grid_sum,
            d_detector_digest_compact,
            d_candidate_fired,
            d_candidate_boundaries,
            // S-PERF.14c — Pre-Alpha + cellpar split intermediate scratch.
            d_candidate_run_buffer,
            d_candidate_run_count,
            workspace.d_candidates(),
            workspace.d_candidate_count(),
            workspace.d_stage_digests(),
            d_tree_leaves,
            tree_leaves_stride,
            d_tree_scratch,
            tree_scratch_stride,
            d_events,
            events_ptr,
            n_events_u64,
            ticks_per_event_ns,
            window_size_ns,
            n_entities,
            n_windows,
            contract.ewma_alpha_q16_raw,
            baseline.latency_us,
            baseline.error_rate_q16_raw,
            std::ptr::from_ref(&thresholds_ffi),
            candidate_cfg.min_detector_count as i32,
            candidate_cfg.min_residual_q_raw,
            candidate_cfg.min_length_windows as i32,
            MAX_CANDIDATES_PER_ENTITY,
            profile_id_i32,
            wide_mask_words_used_i32,
            candidates_ptr,
            count_ptr,
            digests_ptr,
            stream,
            tree_chunk_size,
            1, // digest_mode_id = 1 (CompactDensorDigestV1 — S-PERF.12 path)
            std::ptr::from_mut(&mut device_timings),
        )
    };
    if status != 0 {
        return Err(GpuError::KernelFailed(status));
    }

    let count_slice = workspace
        .candidate_count_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let slots = workspace
        .candidates_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut candidates: Vec<CandidateInterval> = Vec::new();
    for entity_id in 0..(workspace.n_entities as usize) {
        let count = count_slice[entity_id] as usize;
        let base = entity_id * MAX_CANDIDATES_PER_ENTITY as usize;
        for i in 0..count {
            candidates.push(slots[base + i]);
        }
    }

    let stage_digests_host = workspace
        .stage_digests_pinned
        .as_ref()
        .expect("guarded above")
        .as_slice();
    let mut residual_digest = [0u8; 32];
    let mut sign_digest = [0u8; 32];
    let mut detector_digest = [0u8; 32];
    let mut consensus_digest = [0u8; 32];
    residual_digest.copy_from_slice(&stage_digests_host[0..32]);
    sign_digest.copy_from_slice(&stage_digests_host[32..64]);
    detector_digest.copy_from_slice(&stage_digests_host[64..96]);
    consensus_digest.copy_from_slice(&stage_digests_host[96..128]);

    let t_bank = Instant::now();
    // S-PERF.12 — the CaseFile builder is unchanged: it folds
    // the four per-stage digests into the throughput-mode case
    // file under the existing TreeSha256V1 chain in the case
    // file's metadata. The CompactDensorDigestV1 mode identity
    // is recorded ONLY in the S-PERF.12 corpus receipt (not in
    // the case file), so all prior case-file hashes stay
    // byte-identical. The four stage digests under
    // CompactDensorDigestV1 differ from TreeSha256V1 → the
    // case-file hashes also differ, which is expected and
    // panel-acknowledged via `digest_mode_non_aliasing_law`.
    let case = dsfb_gpu_debug_core::casefile::build_throughput_compact_verdict_from_device_digests(
        contract,
        "cuda",
        fixture,
        residual_digest,
        sign_digest,
        detector_digest,
        consensus_digest,
        &candidates,
    );
    #[allow(clippy::cast_precision_loss)]
    let bank_and_finalize_us = (t_bank.elapsed().as_nanos() as u64) as f32 / 1_000.0_f32;

    let host_timings = D64ThroughputHostStageTimings {
        host_input_staging_us,
        bank_and_finalize_us,
    };

    Ok((case, device_timings, host_timings))
}

#[cfg(test)]
mod s_perf_13_pack_equivalence_tests {
    //! S-PERF.13 — CPU-side equivalence tests for the
    //! [`pack_events_to_pinned_simd`] helper. These tests pin
    //! the panel-locked N8 + P6 contract: the bulk SIMD pack
    //! MUST produce byte-identical output to the scalar
    //! `for ... zip()` baseline for any input length. No GPU,
    //! no FFI, no clock dependence — pure host-side
    //! determinism check that runs under every workspace gate
    //! including the pre-commit hook.
    use super::*;
    use dsfb_gpu_debug_core::event::TraceEvent;

    /// Reference scalar pack — the pre-S-PERF.13 baseline.
    fn pack_events_to_pinned_scalar(events: &[TraceEvent], pinned: &mut [GpuTraceEventCompact]) {
        for (dst, src) in pinned.iter_mut().zip(events.iter()).take(events.len()) {
            *dst = GpuTraceEventCompact::from_trace_event(src);
        }
    }

    fn synthetic_events(n: usize) -> Vec<TraceEvent> {
        let mut events = Vec::with_capacity(n);
        let mut state: u64 = 0xD5FB_D5FB_D5FB_D5FB;
        for i in 0..n {
            state = state
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            let ts_ns = (i as u64) * 1000 + (state >> 16);
            let entity_id = (i as u32) % 1024;
            let latency_us = ((state >> 32) as u32) % 100_000;
            let error_code: u16 = u16::from((state & (1 << 7)) != 0);
            // TraceEvent::new takes all 10 fields positionally;
            // GpuTraceEventCompact::from_trace_event reads only
            // (ts_ns, entity_id, error_code, latency_us), so
            // the auxiliary fields (route_id / span_id /
            // parent_span_id / status_code / event_kind / flags)
            // are filled with deterministic-but-irrelevant values.
            events.push(TraceEvent::new(
                ts_ns,
                entity_id,
                (i as u32) % 16,
                state,
                state.wrapping_shr(8),
                latency_us,
                200,
                error_code,
                0,
                0,
            ));
        }
        events
    }

    /// Panel-locked N8 + P6 contract — byte-equivalence
    /// across every length covering chunk boundaries
    /// (empty, tail-only, exact-chunk, multi-chunk + tail,
    /// canonical-scale and large-scale fixtures).
    #[test]
    fn packed_bytes_byte_identical_to_old_path() {
        for &n in &[0_usize, 1, 7, 8, 9, 64, 257, 4096, 65_537] {
            let events = synthetic_events(n);
            let mut scalar_out = vec![GpuTraceEventCompact::default(); n];
            let mut simd_out = vec![GpuTraceEventCompact::default(); n];
            pack_events_to_pinned_scalar(&events, &mut scalar_out);
            pack_events_to_pinned_simd(&events, &mut simd_out);
            assert_eq!(
                scalar_out, simd_out,
                "SIMD pack diverged from scalar baseline at n_events={n}; \
                 panel-locked S-PERF.13 N8 + P6 contract violated"
            );
        }
    }

    /// Defense-in-depth: helper must not write past
    /// `events.len()` even when the pinned buffer is larger.
    #[test]
    fn pack_does_not_write_past_events_len() {
        let events = synthetic_events(17);
        let mut pinned = vec![GpuTraceEventCompact::default(); 64];
        pack_events_to_pinned_simd(&events, &mut pinned);
        let mut expected_head = vec![GpuTraceEventCompact::default(); 17];
        pack_events_to_pinned_scalar(&events, &mut expected_head);
        assert_eq!(&pinned[..17], &expected_head[..]);
        for (i, slot) in pinned.iter().enumerate().skip(17) {
            assert_eq!(
                *slot,
                GpuTraceEventCompact::default(),
                "pinned shadow slot {i} written past events.len()"
            );
        }
    }

    /// Mutation sensitivity: changing any single event's
    /// bytes MUST surface as a different packed-shadow byte.
    #[test]
    fn changing_any_event_byte_changes_packed_output() {
        let n = 32;
        let baseline_events = synthetic_events(n);
        let mut baseline = vec![GpuTraceEventCompact::default(); n];
        pack_events_to_pinned_simd(&baseline_events, &mut baseline);

        for mutate_idx in 0..n {
            let mut mutated_events = baseline_events.clone();
            mutated_events[mutate_idx].latency_us =
                mutated_events[mutate_idx].latency_us.wrapping_add(1);
            let mut mutated = vec![GpuTraceEventCompact::default(); n];
            pack_events_to_pinned_simd(&mutated_events, &mut mutated);
            assert_ne!(
                baseline, mutated,
                "mutating event index {mutate_idx} produced byte-identical packed output; \
                 SIMD pack may be skipping a chunk boundary"
            );
        }
    }
}