softgpu-core 0.8.0

Vendor-neutral SoftGPU runtime core (handles, agents, traces, profiles)
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
//! Process-global SoftGPU runtime session (vendor-neutral).
//!
//! Thread-safe via a single mutex. Init is reference-counted. Handles are
//! generation-bumping so destroy/recycle cannot revive stale IDs.

use crate::agent::{AgentInfoAttr, AgentKind, VirtualAgent};
use crate::aql::{
    parse_supported_packet, DispatchDescriptor, KernargClass, DIAGNOSTIC_COMPLETE_NO_EXECUTION,
    DIAGNOSTIC_REJECTED, KERNEL_SUCCESS,
};
use crate::error::{Error, ErrorCategory};
use crate::fidelity::FidelityLevel;
use crate::handle::{HandleKind, PackedHandle};
use crate::memory::{
    AllocError, MemorySpace, MemoryViewKind, PoolInfoValue, RegionInfoValue, SoftGpuAllocator,
    AMD_POOL_FLAGS_COARSE, AMD_POOL_FLAGS_FINE_KERNARG, AMD_POOL_LOCATION_CPU,
    AMD_POOL_LOCATION_GPU, AMD_SEGMENT_GLOBAL, REGION_FLAGS_COARSE, REGION_FLAGS_FINE_KERNARG,
    REGION_SEGMENT_GLOBAL, SOFTGPU_ALLOC_ALIGNMENT, SOFTGPU_ALLOC_GRANULE, SOFTGPU_MAX_ALLOC_BYTES,
    SOFTGPU_POOL_BYTES,
};
use crate::profile::DeviceProfile;
use crate::queue::{
    init_packet_buffer, is_power_of_two, HsaQueueAbi, PacketObservation, SoftGpuQueue,
    AQL_PACKET_BYTES, QUEUE_FEATURE_KERNEL_DISPATCH, QUEUE_TYPE_MULTI, SOFTGPU_QUEUES_MAX,
};
use crate::signal::{SignalCondition, SignalWaitOutcome, SoftGpuSignal};
use crate::trace::{SharedTrace, TraceEvent, TraceLog, TraceSink};
use softgpu_amd_isa::{run_code_1d, IsaMemory, WaveSize, TINY_ADD_TEXT};
use std::alloc::{alloc_zeroed, dealloc, Layout};
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};

/// Errors returned by the vendor-neutral runtime (mapped to HSA at the edge).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RuntimeError {
    NotInitialized,
    RefcountOverflow,
    InvalidArgument,
    InvalidAgent,
    InvalidRegion,
    InvalidPool,
    InvalidSignal,
    InvalidQueue,
    InvalidQueueCreation,
    InvalidAllocation,
    OutOfResources,
    UnsupportedAttribute,
    Internal(String),
}

impl RuntimeError {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::NotInitialized => "not_initialized",
            Self::RefcountOverflow => "refcount_overflow",
            Self::InvalidArgument => "invalid_argument",
            Self::InvalidAgent => "invalid_agent",
            Self::InvalidRegion => "invalid_region",
            Self::InvalidPool => "invalid_pool",
            Self::InvalidSignal => "invalid_signal",
            Self::InvalidQueue => "invalid_queue",
            Self::InvalidQueueCreation => "invalid_queue_creation",
            Self::InvalidAllocation => "invalid_allocation",
            Self::OutOfResources => "out_of_resources",
            Self::UnsupportedAttribute => "unsupported_attribute",
            Self::Internal(_) => "internal",
        }
    }
}

struct AgentSlot {
    generation: u32,
    live: bool,
    agent: Option<VirtualAgent>,
}

struct SpaceSlot {
    generation: u32,
    live: bool,
    space: Option<MemorySpace>,
}

struct SignalSlot {
    generation: u32,
    live: bool,
    signal: Option<std::sync::Arc<SoftGpuSignal>>,
}

struct QueueSlot {
    #[allow(dead_code)]
    generation: u32,
    live: bool,
    queue: Option<SoftGpuQueue>,
}

/// SoftGPU-registered ISA kernel image (Phase 11 / v0.8 executable load).
#[derive(Debug, Clone)]
pub struct RegisteredIsaKernel {
    pub name: String,
    pub code: Vec<u8>,
    pub kernarg_segment_size: u32,
    pub group_segment_size: u32,
    pub private_segment_size: u32,
}

/// SoftGPU runtime state.
pub struct Runtime {
    refcount: u32,
    profile: DeviceProfile,
    agents: Vec<AgentSlot>,
    regions: Vec<SpaceSlot>,
    pools: Vec<SpaceSlot>,
    signals: Vec<SignalSlot>,
    queues: Vec<QueueSlot>,
    allocator: SoftGpuAllocator,
    next_generation: u32,
    next_queue_id: u64,
    /// Captured validated dispatches for offline replay (owned bytes).
    dispatch_captures: Vec<DispatchDescriptor>,
    /// kernel_object handle → SoftGPU ISA image (Phase 11 / v0.8).
    isa_kernels: HashMap<u64, RegisteredIsaKernel>,
    next_kernel_object: u64,
    /// SoftGPU code-object readers (v0.8).
    code_object_readers: HashMap<u64, crate::executable::CodeObjectReader>,
    next_reader_id: u64,
    /// SoftGPU executables (v0.8).
    executables: HashMap<u64, crate::executable::SoftGpuExecutable>,
    next_executable_id: u64,
    /// executable_symbol handle → (executable_id, symbol_index).
    executable_symbols: HashMap<u64, (u64, usize)>,
    next_symbol_id: u64,
    trace: SharedTrace,
}

impl Runtime {
    pub fn new(profile: DeviceProfile) -> Result<Self, Error> {
        profile.validate()?;
        Ok(Self {
            refcount: 0,
            profile,
            agents: Vec::new(),
            regions: Vec::new(),
            pools: Vec::new(),
            signals: Vec::new(),
            queues: Vec::new(),
            allocator: SoftGpuAllocator::new(SOFTGPU_POOL_BYTES),
            next_generation: 1,
            next_queue_id: 1,
            dispatch_captures: Vec::new(),
            isa_kernels: HashMap::new(),
            next_kernel_object: 1,
            code_object_readers: HashMap::new(),
            next_reader_id: 1,
            executables: HashMap::new(),
            next_executable_id: 1,
            executable_symbols: HashMap::new(),
            next_symbol_id: 1,
            trace: SharedTrace::new(512),
        })
    }

    /// Register a SoftGPU ISA kernel; returns `kernel_object` id for AQL packets.
    pub fn register_isa_kernel(
        &mut self,
        name: impl Into<String>,
        code: Vec<u8>,
    ) -> Result<u64, RuntimeError> {
        self.register_isa_kernel_ex(name, code, 0, 0, 0)
    }

    /// Register ISA kernel with segment sizes (HSA symbol_get_info).
    pub fn register_isa_kernel_ex(
        &mut self,
        name: impl Into<String>,
        code: Vec<u8>,
        kernarg_segment_size: u32,
        group_segment_size: u32,
        private_segment_size: u32,
    ) -> Result<u64, RuntimeError> {
        if code.is_empty() {
            return Err(RuntimeError::InvalidArgument);
        }
        let id = self.next_kernel_object;
        self.next_kernel_object = self.next_kernel_object.saturating_add(1);
        self.isa_kernels.insert(
            id,
            RegisteredIsaKernel {
                name: name.into(),
                code,
                kernarg_segment_size,
                group_segment_size,
                private_segment_size,
            },
        );
        Ok(id)
    }

    /// Register the SoftGPU llvm-mc `tiny_add` kernel image.
    pub fn register_builtin_tiny_add(&mut self) -> Result<u64, RuntimeError> {
        self.register_isa_kernel_ex("tiny_add", TINY_ADD_TEXT.to_vec(), 16, 0, 0)
    }

    pub fn lookup_isa_kernel(&self, kernel_object: u64) -> Option<&RegisteredIsaKernel> {
        self.isa_kernels.get(&kernel_object)
    }

    pub fn code_object_reader_create_from_memory(
        &mut self,
        bytes: &[u8],
    ) -> Result<u64, RuntimeError> {
        if bytes.is_empty() {
            return Err(RuntimeError::InvalidArgument);
        }
        let id = self.next_reader_id;
        self.next_reader_id = self.next_reader_id.saturating_add(1);
        self.code_object_readers.insert(
            id,
            crate::executable::CodeObjectReader {
                bytes: bytes.to_vec(),
            },
        );
        Ok(id)
    }

    pub fn code_object_reader_destroy(&mut self, reader_id: u64) -> Result<(), RuntimeError> {
        if self.code_object_readers.remove(&reader_id).is_none() {
            return Err(RuntimeError::InvalidArgument);
        }
        Ok(())
    }

    pub fn executable_create(&mut self) -> Result<u64, RuntimeError> {
        let id = self.next_executable_id;
        self.next_executable_id = self.next_executable_id.saturating_add(1);
        self.executables.insert(
            id,
            crate::executable::SoftGpuExecutable {
                frozen: false,
                symbols: Vec::new(),
            },
        );
        Ok(id)
    }

    pub fn executable_destroy(&mut self, exec_id: u64) -> Result<(), RuntimeError> {
        let Some(exec) = self.executables.remove(&exec_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        self.executable_symbols
            .retain(|_, (eid, _)| *eid != exec_id);
        for sym in exec.symbols {
            self.isa_kernels.remove(&sym.kernel_object);
        }
        Ok(())
    }

    pub fn executable_load_agent_code_object(
        &mut self,
        exec_id: u64,
        reader_id: u64,
    ) -> Result<(), RuntimeError> {
        let frozen = self
            .executables
            .get(&exec_id)
            .map(|e| e.frozen)
            .ok_or(RuntimeError::InvalidArgument)?;
        if frozen {
            return Err(RuntimeError::InvalidArgument);
        }
        let bytes = self
            .code_object_readers
            .get(&reader_id)
            .map(|r| r.bytes.clone())
            .ok_or(RuntimeError::InvalidArgument)?;
        let kernels = crate::executable::parse_agent_kernels(&bytes)
            .map_err(|_| RuntimeError::InvalidArgument)?;
        let mut symbols = Vec::new();
        for k in kernels {
            let ko = self.register_isa_kernel_ex(
                k.name.clone(),
                k.text,
                k.kernarg_segment_size,
                k.group_segment_size,
                k.private_segment_size,
            )?;
            symbols.push(crate::executable::ExecutableSymbol {
                name: k.name,
                symbol: k.symbol,
                kernel_object: ko,
                kernarg_segment_size: k.kernarg_segment_size,
                kernarg_segment_align: k.kernarg_segment_align.max(1),
                group_segment_size: k.group_segment_size,
                private_segment_size: k.private_segment_size,
                launch_abi: k.launch_abi,
            });
        }
        let exec = self
            .executables
            .get_mut(&exec_id)
            .ok_or(RuntimeError::InvalidArgument)?;
        exec.symbols.extend(symbols);
        Ok(())
    }

    pub fn executable_freeze(&mut self, exec_id: u64) -> Result<(), RuntimeError> {
        let Some(exec) = self.executables.get_mut(&exec_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        if exec.symbols.is_empty() {
            return Err(RuntimeError::InvalidArgument);
        }
        exec.frozen = true;
        Ok(())
    }

    pub fn executable_get_symbol_by_name(
        &mut self,
        exec_id: u64,
        name: &str,
    ) -> Result<u64, RuntimeError> {
        let Some(exec) = self.executables.get(&exec_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        if !exec.frozen {
            return Err(RuntimeError::InvalidArgument);
        }
        let idx_map = crate::executable::index_symbols(&exec.symbols);
        let Some(&sym_idx) = idx_map.get(name) else {
            return Err(RuntimeError::InvalidArgument);
        };
        let id = self.next_symbol_id;
        self.next_symbol_id = self.next_symbol_id.saturating_add(1);
        self.executable_symbols.insert(id, (exec_id, sym_idx));
        Ok(id)
    }

    pub fn executable_symbol_kernel_object(&self, symbol_id: u64) -> Result<u64, RuntimeError> {
        let Some(&(exec_id, sym_idx)) = self.executable_symbols.get(&symbol_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        let Some(exec) = self.executables.get(&exec_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        let Some(sym) = exec.symbols.get(sym_idx) else {
            return Err(RuntimeError::InvalidArgument);
        };
        Ok(sym.kernel_object)
    }

    pub fn executable_symbol_info(
        &self,
        symbol_id: u64,
    ) -> Result<&crate::executable::ExecutableSymbol, RuntimeError> {
        let Some(&(exec_id, sym_idx)) = self.executable_symbols.get(&symbol_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        let Some(exec) = self.executables.get(&exec_id) else {
            return Err(RuntimeError::InvalidArgument);
        };
        exec.symbols
            .get(sym_idx)
            .ok_or(RuntimeError::InvalidArgument)
    }

    pub fn executable_is_frozen(&self, exec_id: u64) -> Result<bool, RuntimeError> {
        self.executables
            .get(&exec_id)
            .map(|e| e.frozen)
            .ok_or(RuntimeError::InvalidArgument)
    }

    /// Owned packet captures suitable for offline `aql::replay_dispatch`.
    pub fn dispatch_captures(&self) -> &[DispatchDescriptor] {
        &self.dispatch_captures
    }

    pub fn profile(&self) -> &DeviceProfile {
        &self.profile
    }

    pub fn trace_snapshot(&self) -> Vec<TraceEvent> {
        self.trace.snapshot()
    }

    pub fn init(&mut self) -> Result<(), RuntimeError> {
        if self.refcount == u32::MAX {
            return Err(RuntimeError::RefcountOverflow);
        }
        if self.refcount == 0 {
            self.bootstrap();
            let seq = self.trace.with_log(TraceLog::next_seq);
            self.trace.with_log(|log| {
                log.record(TraceEvent::RuntimeInit {
                    seq,
                    refcount: 1,
                    profile_id: self.profile.profile_id.clone(),
                    profile_revision: self.profile.profile_revision.clone(),
                    fidelity: TraceLog::fidelity_label(self.profile.max_fidelity),
                });
            });
        }
        self.refcount += 1;
        Ok(())
    }

    pub fn shut_down(&mut self) -> Result<(), RuntimeError> {
        if self.refcount == 0 {
            return Err(RuntimeError::NotInitialized);
        }
        self.refcount -= 1;
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::RuntimeShutdown {
                seq,
                refcount: self.refcount,
            });
        });
        if self.refcount == 0 {
            self.teardown();
        }
        Ok(())
    }

    pub fn is_initialized(&self) -> bool {
        self.refcount > 0
    }

    fn bootstrap(&mut self) {
        self.teardown();
        let generation = self.alloc_generation();
        let index = 0u32;
        let handle = PackedHandle::pack(HandleKind::Agent, generation, index);
        let agent = VirtualAgent::gpu_from_profile(handle, &self.profile);
        self.agents.push(AgentSlot {
            generation,
            live: true,
            agent: Some(agent),
        });
        self.bootstrap_memory(handle);
    }

    fn bootstrap_memory(&mut self, agent_handle: PackedHandle) {
        let specs = [
            (
                HandleKind::Region,
                MemoryViewKind::RegionFineKernarg,
                REGION_SEGMENT_GLOBAL,
                REGION_FLAGS_FINE_KERNARG,
                None,
            ),
            (
                HandleKind::Region,
                MemoryViewKind::RegionCoarse,
                REGION_SEGMENT_GLOBAL,
                REGION_FLAGS_COARSE,
                None,
            ),
            (
                HandleKind::MemoryPool,
                MemoryViewKind::PoolFineHost,
                AMD_SEGMENT_GLOBAL,
                AMD_POOL_FLAGS_FINE_KERNARG,
                Some(AMD_POOL_LOCATION_CPU),
            ),
            (
                HandleKind::MemoryPool,
                MemoryViewKind::PoolCoarseDevice,
                AMD_SEGMENT_GLOBAL,
                AMD_POOL_FLAGS_COARSE,
                Some(AMD_POOL_LOCATION_GPU),
            ),
        ];
        for (kind, view, segment, flags, location) in specs {
            let generation = self.alloc_generation();
            let table = if kind == HandleKind::Region {
                &mut self.regions
            } else {
                &mut self.pools
            };
            let index = table.len() as u32;
            let handle = PackedHandle::pack(kind, generation, index);
            let space = MemorySpace {
                handle,
                kind: view,
                agent_handle,
                segment,
                global_flags: flags,
                size_bytes: SOFTGPU_POOL_BYTES,
                alloc_max_size: SOFTGPU_MAX_ALLOC_BYTES,
                runtime_alloc_allowed: true,
                granule: SOFTGPU_ALLOC_GRANULE,
                alignment: SOFTGPU_ALLOC_ALIGNMENT,
                location,
            };
            table.push(SpaceSlot {
                generation,
                live: true,
                space: Some(space),
            });
        }
    }

    fn alloc_generation(&mut self) -> u32 {
        let generation = self.next_generation;
        self.next_generation = if generation >= 0x00FF_FFFF {
            1
        } else {
            generation + 1
        };
        generation
    }

    fn teardown(&mut self) {
        // Cancel waiters before dropping signal/queue tables.
        for slot in &self.signals {
            if let Some(sig) = slot.signal.as_ref() {
                sig.cancel();
            }
        }
        let mut doorbells = Vec::new();
        while let Some(slot) = self.queues.pop() {
            if let Some(q) = slot.queue {
                doorbells.push(q.doorbell);
                self.destroy_queue_resources(q);
            }
        }
        for doorbell in doorbells {
            let _ = self.signal_destroy_doorbell(doorbell);
        }
        self.signals.clear();
        self.allocator.clear_all();
        self.regions.clear();
        self.pools.clear();
        let live_count = self.agents.iter().filter(|s| s.live).count();
        for _ in 0..live_count {
            let _ = self.alloc_generation();
        }
        self.agents.clear();
        self.dispatch_captures.clear();
    }

    fn destroy_queue_resources(&mut self, queue: SoftGpuQueue) {
        if !queue.packet_buffer.is_null() {
            let layout = Layout::from_size_align(queue.packet_bytes, AQL_PACKET_BYTES)
                .unwrap_or_else(|_| Layout::from_size_align(AQL_PACKET_BYTES, 8).unwrap());
            // SAFETY: buffer allocated by SoftGPU for this queue.
            unsafe { dealloc(queue.packet_buffer, layout) };
        }
        // Doorbell signal is destroyed with the queue ownership table entry separately.
        let _ = queue;
    }

    fn resolve_agent(&self, handle: PackedHandle) -> Result<&VirtualAgent, RuntimeError> {
        if handle.is_invalid() || handle.kind() != Some(HandleKind::Agent) {
            return Err(RuntimeError::InvalidAgent);
        }
        let idx = handle.index() as usize;
        let slot = self.agents.get(idx).ok_or(RuntimeError::InvalidAgent)?;
        if !slot.live || slot.generation != handle.generation() {
            return Err(RuntimeError::InvalidAgent);
        }
        slot.agent.as_ref().ok_or(RuntimeError::InvalidAgent)
    }

    fn resolve_space(
        &self,
        handle: PackedHandle,
        kind: HandleKind,
    ) -> Result<&MemorySpace, RuntimeError> {
        let make_err = || {
            if kind == HandleKind::Region {
                RuntimeError::InvalidRegion
            } else {
                RuntimeError::InvalidPool
            }
        };
        if handle.is_invalid() || handle.kind() != Some(kind) {
            return Err(make_err());
        }
        let table = if kind == HandleKind::Region {
            &self.regions
        } else {
            &self.pools
        };
        let slot = table.get(handle.index() as usize).ok_or_else(make_err)?;
        if !slot.live || slot.generation != handle.generation() {
            return Err(make_err());
        }
        slot.space.as_ref().ok_or_else(make_err)
    }

    fn resolve_signal(
        &self,
        handle: PackedHandle,
    ) -> Result<std::sync::Arc<SoftGpuSignal>, RuntimeError> {
        if handle.is_invalid() || handle.kind() != Some(HandleKind::Signal) {
            return Err(RuntimeError::InvalidSignal);
        }
        let slot = self
            .signals
            .get(handle.index() as usize)
            .ok_or(RuntimeError::InvalidSignal)?;
        if !slot.live || slot.generation != handle.generation() {
            return Err(RuntimeError::InvalidSignal);
        }
        slot.signal
            .as_ref()
            .cloned()
            .ok_or(RuntimeError::InvalidSignal)
    }

    fn resolve_queue_by_abi(&self, abi: *const HsaQueueAbi) -> Result<&SoftGpuQueue, RuntimeError> {
        if abi.is_null() {
            return Err(RuntimeError::InvalidQueue);
        }
        for slot in &self.queues {
            if !slot.live {
                continue;
            }
            if let Some(q) = slot.queue.as_ref() {
                if std::ptr::eq(q.abi_ptr(), abi) {
                    return Ok(q);
                }
            }
        }
        Err(RuntimeError::InvalidQueue)
    }

    pub fn iterate_agents<F>(&self, mut callback: F) -> Result<(), RuntimeError>
    where
        F: FnMut(&VirtualAgent) -> Result<(), RuntimeError>,
    {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::AgentIterateBegin {
                seq,
                agent_count: self.agents.iter().filter(|s| s.live).count(),
            });
        });
        for slot in &self.agents {
            if !slot.live {
                continue;
            }
            let agent = slot.agent.as_ref().ok_or(RuntimeError::Internal(
                "live agent slot missing agent".into(),
            ))?;
            let seq = self.trace.with_log(TraceLog::next_seq);
            self.trace.with_log(|log| {
                log.record(TraceEvent::AgentIterateVisit {
                    seq,
                    agent_handle: agent.handle.raw(),
                    kind: agent.kind.as_str().to_string(),
                });
            });
            callback(agent)?;
        }
        Ok(())
    }

    pub fn agent_get_info(
        &self,
        handle: PackedHandle,
        attr: AgentInfoAttr,
    ) -> Result<AgentInfoValue, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let agent = match self.resolve_agent(handle) {
            Ok(a) => a,
            Err(err) => {
                self.trace_get_info(handle, attr, err.as_str());
                return Err(err);
            }
        };
        let value = match attr {
            AgentInfoAttr::Name => AgentInfoValue::Name(agent.name.clone()),
            AgentInfoAttr::VendorName => AgentInfoValue::VendorName(agent.vendor_name.clone()),
            AgentInfoAttr::Feature => AgentInfoValue::Feature(agent.feature_mask),
            AgentInfoAttr::Device => AgentInfoValue::Device(agent.kind),
            AgentInfoAttr::VersionMajor => AgentInfoValue::U16(1),
            AgentInfoAttr::VersionMinor => AgentInfoValue::U16(2),
            AgentInfoAttr::QueuesMax => AgentInfoValue::U32(agent.queues_max),
            AgentInfoAttr::QueueMinSize => AgentInfoValue::U32(agent.queue_min_size),
            AgentInfoAttr::QueueMaxSize => AgentInfoValue::U32(agent.queue_max_size),
            AgentInfoAttr::QueueType => AgentInfoValue::U32(agent.queue_type),
        };
        self.trace_get_info(handle, attr, "ok");
        Ok(value)
    }

    fn trace_get_info(&self, handle: PackedHandle, attr: AgentInfoAttr, outcome: &str) {
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::AgentGetInfo {
                seq,
                agent_handle: handle.raw(),
                attribute: attr.as_str().to_string(),
                outcome: outcome.to_string(),
            });
        });
    }

    pub fn iterate_regions<F>(
        &self,
        agent: PackedHandle,
        mut callback: F,
    ) -> Result<(), RuntimeError>
    where
        F: FnMut(&MemorySpace) -> Result<(), RuntimeError>,
    {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let _ = self.resolve_agent(agent)?;
        for slot in &self.regions {
            if !slot.live {
                continue;
            }
            let space = slot
                .space
                .as_ref()
                .ok_or(RuntimeError::Internal("live region missing".into()))?;
            if space.agent_handle != agent {
                continue;
            }
            callback(space)?;
        }
        Ok(())
    }

    pub fn iterate_pools<F>(&self, agent: PackedHandle, mut callback: F) -> Result<(), RuntimeError>
    where
        F: FnMut(&MemorySpace) -> Result<(), RuntimeError>,
    {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let _ = self.resolve_agent(agent)?;
        for slot in &self.pools {
            if !slot.live {
                continue;
            }
            let space = slot
                .space
                .as_ref()
                .ok_or(RuntimeError::Internal("live pool missing".into()))?;
            if space.agent_handle != agent {
                continue;
            }
            callback(space)?;
        }
        Ok(())
    }

    pub fn region_get_info(
        &self,
        handle: PackedHandle,
        attr: RegionInfoAttr,
    ) -> Result<RegionInfoValue, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let space = self.resolve_space(handle, HandleKind::Region)?;
        Ok(match attr {
            RegionInfoAttr::Segment => RegionInfoValue::Segment(space.segment),
            RegionInfoAttr::GlobalFlags => RegionInfoValue::GlobalFlags(space.global_flags),
            RegionInfoAttr::Size => RegionInfoValue::Size(space.size_bytes),
            RegionInfoAttr::AllocMaxSize => RegionInfoValue::AllocMaxSize(space.alloc_max_size),
            RegionInfoAttr::RuntimeAllocAllowed => {
                RegionInfoValue::RuntimeAllocAllowed(space.runtime_alloc_allowed)
            }
            RegionInfoAttr::Granule => RegionInfoValue::Granule(space.granule),
            RegionInfoAttr::Alignment => RegionInfoValue::Alignment(space.alignment),
        })
    }

    pub fn pool_get_info(
        &self,
        handle: PackedHandle,
        attr: PoolInfoAttr,
    ) -> Result<PoolInfoValue, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let space = self.resolve_space(handle, HandleKind::MemoryPool)?;
        Ok(match attr {
            PoolInfoAttr::Segment => PoolInfoValue::Segment(space.segment),
            PoolInfoAttr::GlobalFlags => PoolInfoValue::GlobalFlags(space.global_flags),
            PoolInfoAttr::Size => PoolInfoValue::Size(space.size_bytes),
            PoolInfoAttr::RuntimeAllocAllowed => {
                PoolInfoValue::RuntimeAllocAllowed(space.runtime_alloc_allowed)
            }
            PoolInfoAttr::Granule => PoolInfoValue::Granule(space.granule),
            PoolInfoAttr::Alignment => PoolInfoValue::Alignment(space.alignment),
            PoolInfoAttr::AccessibleByAll => PoolInfoValue::AccessibleByAll(true),
            PoolInfoAttr::AllocMaxSize => PoolInfoValue::AllocMaxSize(space.alloc_max_size),
            PoolInfoAttr::Location => {
                PoolInfoValue::Location(space.location.unwrap_or(AMD_POOL_LOCATION_CPU))
            }
            PoolInfoAttr::RecGranule => PoolInfoValue::RecGranule(space.granule),
        })
    }

    pub fn memory_allocate(
        &mut self,
        space_handle: PackedHandle,
        size: usize,
    ) -> Result<*mut u8, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let kind = space_handle.kind().ok_or(RuntimeError::InvalidArgument)?;
        if kind != HandleKind::Region && kind != HandleKind::MemoryPool {
            return Err(RuntimeError::InvalidArgument);
        }
        let space = self.resolve_space(space_handle, kind)?.clone();
        if !space.runtime_alloc_allowed {
            return Err(RuntimeError::InvalidAllocation);
        }
        let result = self.allocator.allocate(
            space.handle,
            size,
            space.granule,
            space.alignment,
            space.alloc_max_size,
        );
        let (ptr, outcome) = match &result {
            Ok(p) => (*p as u64, "ok"),
            Err(AllocError::InvalidArgument) => (0, "invalid_argument"),
            Err(AllocError::InvalidAllocation) => (0, "invalid_allocation"),
            Err(AllocError::OutOfResources) => (0, "out_of_resources"),
        };
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::MemoryAllocate {
                seq,
                space_handle: space_handle.raw(),
                size,
                ptr,
                outcome: outcome.into(),
            });
        });
        result.map_err(|e| match e {
            AllocError::InvalidArgument => RuntimeError::InvalidArgument,
            AllocError::InvalidAllocation => RuntimeError::InvalidAllocation,
            AllocError::OutOfResources => RuntimeError::OutOfResources,
        })
    }

    pub fn memory_free(&mut self, ptr: *mut u8) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let result = self.allocator.free(ptr);
        let outcome = match &result {
            Ok(_) => "ok",
            Err(_) => "invalid_argument",
        };
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::MemoryFree {
                seq,
                ptr: ptr as u64,
                outcome: outcome.into(),
            });
        });
        result
            .map(|_| ())
            .map_err(|_| RuntimeError::InvalidArgument)
    }

    pub fn allocation_lookup(
        &self,
        ptr: *const u8,
    ) -> Result<crate::memory::AllocationMeta, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        self.allocator
            .lookup(ptr)
            .ok_or(RuntimeError::InvalidArgument)
    }

    /// # Safety
    ///
    /// `dst` and `src` must be valid for `size` bytes; SoftGPU does not
    /// validate OS-mapped pointers beyond SoftGPU allocations.
    pub unsafe fn memory_copy(
        &self,
        dst: *mut u8,
        src: *const u8,
        size: usize,
    ) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        if dst.is_null() || src.is_null() {
            return Err(RuntimeError::InvalidArgument);
        }
        // SAFETY: caller guarantees valid non-overlapping regions of `size`.
        unsafe {
            std::ptr::copy_nonoverlapping(src, dst, size);
        }
        Ok(())
    }

    pub fn agents_allow_access(
        &self,
        agents: &[PackedHandle],
        _ptr: *const u8,
    ) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        for agent in agents {
            let _ = self.resolve_agent(*agent)?;
        }
        // SoftGPU host memory is already CPU-visible; allow is a no-op success.
        Ok(())
    }

    pub fn agent_memory_pool_access(
        &self,
        agent: PackedHandle,
        pool: PackedHandle,
    ) -> Result<u32, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let _ = self.resolve_agent(agent)?;
        let _ = self.resolve_space(pool, HandleKind::MemoryPool)?;
        // HSA_AMD_MEMORY_POOL_ACCESS_ALLOWED_BY_DEFAULT
        Ok(1)
    }

    pub fn signal_create(&mut self, initial: i64) -> Result<PackedHandle, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let generation = self.alloc_generation();
        let index = self.signals.len() as u32;
        let handle = PackedHandle::pack(HandleKind::Signal, generation, index);
        self.signals.push(SignalSlot {
            generation,
            live: true,
            signal: Some(std::sync::Arc::new(SoftGpuSignal::new(handle, initial))),
        });
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::SignalCreate {
                seq,
                signal_handle: handle.raw(),
                initial,
            });
        });
        Ok(handle)
    }

    pub fn signal_destroy(&mut self, handle: PackedHandle) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        if handle.is_invalid() || handle.kind() != Some(HandleKind::Signal) {
            return Err(RuntimeError::InvalidSignal);
        }
        let idx = handle.index() as usize;
        let slot = self
            .signals
            .get_mut(idx)
            .ok_or(RuntimeError::InvalidSignal)?;
        if !slot.live || slot.generation != handle.generation() {
            return Err(RuntimeError::InvalidSignal);
        }
        if slot.signal.as_ref().map(|s| s.is_doorbell).unwrap_or(false) {
            // Doorbell owned by a live queue — reject independent destroy.
            return Err(RuntimeError::InvalidArgument);
        }
        if let Some(sig) = slot.signal.as_ref() {
            sig.cancel();
        }
        slot.live = false;
        slot.signal = None;
        let _ = self.alloc_generation();
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::SignalDestroy {
                seq,
                signal_handle: handle.raw(),
            });
        });
        Ok(())
    }

    pub fn signal_load(&self, handle: PackedHandle) -> Result<i64, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        Ok(self.resolve_signal(handle)?.load())
    }

    pub fn signal_store(&mut self, handle: PackedHandle, value: i64) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let sig = self.resolve_signal(handle)?;
        if sig.is_cancelled() {
            return Err(RuntimeError::InvalidSignal);
        }
        let is_doorbell = sig.is_doorbell;
        let queue_id = sig.queue_id;
        sig.store(value);
        if is_doorbell {
            if let Some(qid) = queue_id {
                let mut pending: Vec<PacketObservation> = Vec::new();
                let mut observe_err: Option<String> = None;
                for slot in &self.queues {
                    if let Some(q) = slot.queue.as_ref() {
                        if q.id == qid {
                            q.note_doorbell_store();
                            let seq = self.trace.with_log(TraceLog::next_seq);
                            self.trace.with_log(|log| {
                                log.record(TraceEvent::QueueDoorbell {
                                    seq,
                                    queue_id: qid,
                                    value,
                                });
                            });
                            match q.observe_packets() {
                                Ok(obs) => pending = obs,
                                Err(err) => observe_err = Some(format!("{err:?}")),
                            }
                            break;
                        }
                    }
                }
                if let Some(detail) = observe_err {
                    let seq = self.trace.with_log(TraceLog::next_seq);
                    self.trace.with_log(|log| {
                        log.record(TraceEvent::PacketValidateFailed {
                            seq,
                            queue_id: qid,
                            detail,
                        });
                    });
                }
                for p in pending {
                    self.process_observed_packet(qid, &p);
                }
            }
        }
        Ok(())
    }

    fn classify_kernarg(&self, addr: u64) -> KernargClass {
        if addr == 0 {
            return KernargClass::Null;
        }
        match self.allocator.lookup(addr as *const u8) {
            Some(meta) => KernargClass::SoftGpu {
                alloc_id: Some(meta.alloc_id),
                addr,
            },
            None => KernargClass::ForeignOpaque { addr },
        }
    }

    fn kernarg_class_label(k: &KernargClass) -> String {
        match k {
            KernargClass::Null => "null".into(),
            KernargClass::SoftGpu { alloc_id, .. } => {
                format!("softgpu:{}", alloc_id.unwrap_or(0))
            }
            KernargClass::ForeignOpaque { .. } => "foreign_opaque".into(),
        }
    }

    /// Store a completion signal value without doorbell observe side effects.
    fn signal_store_plain(&self, handle: PackedHandle, value: i64) -> Result<(), RuntimeError> {
        let sig = self.resolve_signal(handle)?;
        if sig.is_cancelled() || sig.is_doorbell {
            return Err(RuntimeError::InvalidSignal);
        }
        sig.store(value);
        Ok(())
    }

    fn process_observed_packet(&mut self, queue_id: u64, obs: &PacketObservation) {
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::PacketObserved {
                seq,
                queue_id,
                packet_index: obs.packet_index,
                packet_type: obs.packet_type,
            });
        });

        let parsed = parse_supported_packet(&obs.bytes, obs.packet_index, |addr| {
            self.classify_kernarg(addr)
        });

        match parsed {
            Ok(desc) => {
                let seq = self.trace.with_log(TraceLog::next_seq);
                self.trace.with_log(|log| {
                    log.record(TraceEvent::DispatchValidated {
                        seq,
                        queue_id,
                        packet_index: desc.packet_index,
                        packet_type: desc.packet_type.as_u16(),
                        dimensions: desc.dimensions,
                        workgroup_size: desc.workgroup_size,
                        grid_size: desc.grid_size,
                        private_segment_size: desc.private_segment_size,
                        group_segment_size: desc.group_segment_size,
                        kernel_object: desc.kernel_object,
                        kernarg_class: Self::kernarg_class_label(&desc.kernarg),
                        completion_signal: desc.completion_signal,
                    });
                });
                self.try_dispatch_or_diagnose(queue_id, &desc);
                self.dispatch_captures.push(desc);
            }
            Err(err) => {
                let seq = self.trace.with_log(TraceLog::next_seq);
                self.trace.with_log(|log| {
                    log.record(TraceEvent::DispatchRejected {
                        seq,
                        queue_id,
                        packet_index: obs.packet_index,
                        packet_type: obs.packet_type,
                        detail: format!("{err:?}"),
                        contract: DIAGNOSTIC_REJECTED.into(),
                    });
                });
                self.apply_diagnostic_reject(queue_id, obs.packet_index);
            }
        }
    }

    fn try_dispatch_or_diagnose(&mut self, queue_id: u64, desc: &DispatchDescriptor) {
        if desc.packet_type != crate::aql::PacketType::KernelDispatch {
            self.apply_diagnostic_complete(queue_id, desc);
            return;
        }
        let Some(kernel) = self.isa_kernels.get(&desc.kernel_object).cloned() else {
            self.apply_diagnostic_complete(queue_id, desc);
            return;
        };
        let KernargClass::SoftGpu { addr, .. } = desc.kernarg else {
            self.apply_diagnostic_complete(queue_id, desc);
            return;
        };
        let grid_x = desc.grid_size[0];
        if grid_x == 0 {
            self.apply_diagnostic_complete(queue_id, desc);
            return;
        }

        struct AllocMem<'a>(&'a mut SoftGpuAllocator);
        impl IsaMemory for AllocMem<'_> {
            fn load_u32(&self, addr: u64) -> softgpu_amd_isa::Result<u32> {
                let mut buf = [0u8; 4];
                self.0.read_bytes_at(addr, &mut buf).map_err(|_| {
                    softgpu_amd_isa::IsaError::new(
                        softgpu_amd_isa::IsaErrorKind::Trap,
                        format!("SoftGPU alloc load_u32 OOB/unknown addr=0x{addr:x}"),
                    )
                })?;
                Ok(u32::from_le_bytes(buf))
            }
            fn store_u32(&mut self, addr: u64, value: u32) -> softgpu_amd_isa::Result<()> {
                self.0
                    .write_bytes_at(addr, &value.to_le_bytes())
                    .map_err(|_| {
                        softgpu_amd_isa::IsaError::new(
                            softgpu_amd_isa::IsaErrorKind::Trap,
                            format!("SoftGPU alloc store_u32 OOB/unknown addr=0x{addr:x}"),
                        )
                    })
            }
            fn load_u64(&self, addr: u64) -> softgpu_amd_isa::Result<u64> {
                let mut buf = [0u8; 8];
                self.0.read_bytes_at(addr, &mut buf).map_err(|_| {
                    softgpu_amd_isa::IsaError::new(
                        softgpu_amd_isa::IsaErrorKind::Trap,
                        format!("SoftGPU alloc load_u64 OOB/unknown addr=0x{addr:x}"),
                    )
                })?;
                Ok(u64::from_le_bytes(buf))
            }
            fn store_u64(&mut self, addr: u64, value: u64) -> softgpu_amd_isa::Result<()> {
                self.0
                    .write_bytes_at(addr, &value.to_le_bytes())
                    .map_err(|_| {
                        softgpu_amd_isa::IsaError::new(
                            softgpu_amd_isa::IsaErrorKind::Trap,
                            format!("SoftGPU alloc store_u64 OOB/unknown addr=0x{addr:x}"),
                        )
                    })
            }
        }

        let mut mem = AllocMem(&mut self.allocator);
        match run_code_1d(&kernel.code, &mut mem, addr, grid_x, WaveSize::Wave32) {
            Ok(_) => self.apply_kernel_success(queue_id, desc, &kernel.name),
            Err(e) => {
                let seq = self.trace.with_log(TraceLog::next_seq);
                self.trace.with_log(|log| {
                    log.record(TraceEvent::DispatchRejected {
                        seq,
                        queue_id,
                        packet_index: desc.packet_index,
                        packet_type: desc.packet_type.as_u16(),
                        detail: e.to_string(),
                        contract: DIAGNOSTIC_REJECTED.into(),
                    });
                });
                self.apply_diagnostic_reject(queue_id, desc.packet_index);
            }
        }
    }

    fn apply_kernel_success(
        &mut self,
        queue_id: u64,
        desc: &DispatchDescriptor,
        kernel_name: &str,
    ) {
        if desc.completion_signal != 0 {
            let handle = PackedHandle::from_raw(desc.completion_signal);
            let _ = self.signal_store_plain(handle, 0);
        }
        self.advance_packet_processor(queue_id, desc.packet_index);
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::DiagnosticComplete {
                seq,
                queue_id,
                packet_index: desc.packet_index,
                completion_signal: desc.completion_signal,
                contract: KERNEL_SUCCESS.into(),
                note: format!("softgpu_isa_kernel:{kernel_name}"),
            });
        });
    }
    fn apply_diagnostic_complete(&mut self, queue_id: u64, desc: &DispatchDescriptor) {
        // HSA completion convention: producer waits for signal == 0.
        // SoftGPU stores 0 only as the experimental no-execution contract.
        if desc.completion_signal != 0 {
            let handle = PackedHandle::from_raw(desc.completion_signal);
            let _ = self.signal_store_plain(handle, 0);
        }
        self.advance_packet_processor(queue_id, desc.packet_index);
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::DiagnosticComplete {
                seq,
                queue_id,
                packet_index: desc.packet_index,
                completion_signal: desc.completion_signal,
                contract: DIAGNOSTIC_COMPLETE_NO_EXECUTION.into(),
                note: "not_kernel_success".into(),
            });
        });
    }

    fn apply_diagnostic_reject(&mut self, queue_id: u64, packet_index: u64) {
        // Reject does not store completion as success; still advance protocol.
        self.advance_packet_processor(queue_id, packet_index);
    }

    fn advance_packet_processor(&mut self, queue_id: u64, packet_index: u64) {
        for slot in &self.queues {
            if let Some(q) = slot.queue.as_ref() {
                if q.id == queue_id {
                    q.invalidate_packet_slot(packet_index);
                    let next = packet_index.saturating_add(1);
                    // Monotonic processor progress; never move read_index backwards.
                    if next > q.read_index() {
                        q.store_read_index(next);
                    }
                    break;
                }
            }
        }
    }

    /// Clone the signal Arc for waiting **without** holding the runtime lock.
    pub fn signal_arc(
        &self,
        handle: PackedHandle,
    ) -> Result<std::sync::Arc<SoftGpuSignal>, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        self.resolve_signal(handle)
    }

    pub fn signal_wait(
        &self,
        handle: PackedHandle,
        condition: u32,
        compare: i64,
        timeout_hint_ns: u64,
        wait_state_hint: u32,
    ) -> Result<SignalWaitOutcome, RuntimeError> {
        let sig = self.signal_arc(handle)?;
        let cond = SignalCondition::from_u32(condition).ok_or(RuntimeError::InvalidArgument)?;
        Ok(sig.wait(cond, compare, timeout_hint_ns, wait_state_hint))
    }

    /// SoftGPU packet observation without doorbell (test / tooling).
    pub fn queue_observe(
        &self,
        abi: *const HsaQueueAbi,
    ) -> Result<Vec<crate::queue::PacketObservation>, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let q = self.resolve_queue_by_abi(abi)?;
        q.observe_packets()
            .map_err(|e| RuntimeError::Internal(format!("{e:?}")))
    }

    pub fn queue_create(
        &mut self,
        agent: PackedHandle,
        size: u32,
        type_: u32,
    ) -> Result<*mut HsaQueueAbi, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let agent_ref = self.resolve_agent(agent)?;
        if size == 0 || !is_power_of_two(size) {
            return Err(RuntimeError::InvalidArgument);
        }
        if size < agent_ref.queue_min_size || size > agent_ref.queue_max_size {
            return Err(RuntimeError::InvalidArgument);
        }
        if type_ != QUEUE_TYPE_MULTI && type_ != 1 {
            // SoftGPU supports single and multi; cooperative unsupported.
            return Err(RuntimeError::InvalidQueueCreation);
        }
        let live_queues = self.queues.iter().filter(|s| s.live).count() as u32;
        if live_queues >= SOFTGPU_QUEUES_MAX {
            return Err(RuntimeError::OutOfResources);
        }

        let queue_id = self.next_queue_id;
        self.next_queue_id = self.next_queue_id.saturating_add(1);

        let generation = self.alloc_generation();
        let index = self.signals.len() as u32;
        let doorbell = PackedHandle::pack(HandleKind::Signal, generation, index);
        self.signals.push(SignalSlot {
            generation,
            live: true,
            signal: Some(std::sync::Arc::new(SoftGpuSignal::new_doorbell(
                doorbell, 0, queue_id,
            ))),
        });
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::SignalCreate {
                seq,
                signal_handle: doorbell.raw(),
                initial: 0,
            });
        });

        let packet_bytes = (size as usize).saturating_mul(AQL_PACKET_BYTES);
        let layout = Layout::from_size_align(packet_bytes, AQL_PACKET_BYTES)
            .map_err(|_| RuntimeError::OutOfResources)?;
        // SAFETY: layout validated.
        let packet_ptr = unsafe { alloc_zeroed(layout) };
        if packet_ptr.is_null() {
            let _ = self.signal_destroy_doorbell(doorbell);
            return Err(RuntimeError::OutOfResources);
        }
        // SAFETY: freshly allocated buffer of packet_bytes.
        unsafe {
            init_packet_buffer(std::slice::from_raw_parts_mut(packet_ptr, packet_bytes));
        }

        let generation = self.alloc_generation();
        let index = self.queues.len() as u32;
        let handle = PackedHandle::pack(HandleKind::Queue, generation, index);

        let abi = Box::new(HsaQueueAbi {
            type_,
            features: QUEUE_FEATURE_KERNEL_DISPATCH,
            base_address: packet_ptr,
            doorbell_signal: doorbell.raw(),
            size,
            reserved1: 0,
            id: queue_id,
        });

        let queue = SoftGpuQueue::new(
            handle,
            agent,
            queue_id,
            abi,
            packet_ptr,
            packet_bytes,
            doorbell,
        );
        let abi_ptr = queue.abi_ptr();
        self.queues.push(QueueSlot {
            generation,
            live: true,
            queue: Some(queue),
        });

        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::QueueCreate {
                seq,
                queue_id,
                size,
                agent_handle: agent.raw(),
            });
        });
        Ok(abi_ptr)
    }

    fn signal_destroy_doorbell(&mut self, handle: PackedHandle) -> Result<(), RuntimeError> {
        let idx = handle.index() as usize;
        let slot = self
            .signals
            .get_mut(idx)
            .ok_or(RuntimeError::InvalidSignal)?;
        if !slot.live || slot.generation != handle.generation() {
            return Err(RuntimeError::InvalidSignal);
        }
        if let Some(sig) = slot.signal.as_ref() {
            sig.cancel();
        }
        slot.live = false;
        slot.signal = None;
        let _ = self.alloc_generation();
        Ok(())
    }

    pub fn queue_destroy(&mut self, abi: *mut HsaQueueAbi) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        if abi.is_null() {
            return Err(RuntimeError::InvalidQueue);
        }
        let mut found = None;
        for (i, slot) in self.queues.iter().enumerate() {
            if !slot.live {
                continue;
            }
            if let Some(q) = slot.queue.as_ref() {
                if q.abi_ptr() == abi {
                    found = Some(i);
                    break;
                }
            }
        }
        let idx = found.ok_or(RuntimeError::InvalidQueue)?;
        let slot = &mut self.queues[idx];
        let Some(mut queue) = slot.queue.take() else {
            return Err(RuntimeError::InvalidQueue);
        };
        slot.live = false;
        let queue_id = queue.id;
        let doorbell = queue.doorbell;
        if !queue.packet_buffer.is_null() {
            let layout = Layout::from_size_align(queue.packet_bytes, AQL_PACKET_BYTES)
                .unwrap_or_else(|_| Layout::from_size_align(AQL_PACKET_BYTES, 8).unwrap());
            unsafe { dealloc(queue.packet_buffer, layout) };
            queue.packet_buffer = std::ptr::null_mut();
        }
        let _ = self.signal_destroy_doorbell(doorbell);
        let _ = self.alloc_generation();
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::QueueDestroy { seq, queue_id });
        });
        Ok(())
    }

    pub fn queue_load_write_index(&self, abi: *const HsaQueueAbi) -> Result<u64, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        Ok(self.resolve_queue_by_abi(abi)?.write_index())
    }

    pub fn queue_load_read_index(&self, abi: *const HsaQueueAbi) -> Result<u64, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        Ok(self.resolve_queue_by_abi(abi)?.read_index())
    }

    pub fn queue_store_write_index(
        &self,
        abi: *const HsaQueueAbi,
        value: u64,
    ) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let q = self.resolve_queue_by_abi(abi)?;
        q.store_write_index(value);
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::QueueIndexStore {
                seq,
                queue_id: q.id,
                which: "write".into(),
                value,
            });
        });
        Ok(())
    }

    pub fn queue_store_read_index(
        &self,
        abi: *const HsaQueueAbi,
        value: u64,
    ) -> Result<(), RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        let q = self.resolve_queue_by_abi(abi)?;
        q.store_read_index(value);
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::QueueIndexStore {
                seq,
                queue_id: q.id,
                which: "read".into(),
                value,
            });
        });
        Ok(())
    }

    pub fn queue_doorbell_count(&self, abi: *const HsaQueueAbi) -> Result<u64, RuntimeError> {
        if !self.is_initialized() {
            return Err(RuntimeError::NotInitialized);
        }
        Ok(self.resolve_queue_by_abi(abi)?.doorbell_store_count())
    }

    pub fn record_unsupported(&self, api: &str, detail: &str) {
        let seq = self.trace.with_log(TraceLog::next_seq);
        self.trace.with_log(|log| {
            log.record(TraceEvent::Unsupported {
                seq,
                api: api.to_string(),
                detail: detail.to_string(),
            });
        });
    }

    pub fn max_fidelity(&self) -> FidelityLevel {
        self.profile.max_fidelity
    }
}

/// Typed agent info payload before ABI packing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AgentInfoValue {
    Name(String),
    VendorName(String),
    Feature(u32),
    Device(AgentKind),
    U16(u16),
    U32(u32),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegionInfoAttr {
    Segment,
    GlobalFlags,
    Size,
    AllocMaxSize,
    RuntimeAllocAllowed,
    Granule,
    Alignment,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PoolInfoAttr {
    Segment,
    GlobalFlags,
    Size,
    RuntimeAllocAllowed,
    Granule,
    Alignment,
    AccessibleByAll,
    AllocMaxSize,
    Location,
    RecGranule,
}

/// Process-global runtime holder.
static GLOBAL: OnceLock<Mutex<Option<Runtime>>> = OnceLock::new();

fn global() -> &'static Mutex<Option<Runtime>> {
    GLOBAL.get_or_init(|| Mutex::new(None))
}

/// Install the process runtime from a profile (idempotent replace only when down).
pub fn install_runtime(profile: DeviceProfile) -> Result<(), Error> {
    let mut guard = global()
        .lock()
        .map_err(|_| Error::new(ErrorCategory::Internal, "runtime mutex poisoned"))?;
    if let Some(rt) = guard.as_ref() {
        if rt.is_initialized() {
            return Err(Error::new(
                ErrorCategory::Internal,
                "cannot replace SoftGPU runtime while initialized",
            ));
        }
    }
    *guard = Some(Runtime::new(profile)?);
    Ok(())
}

/// Ensure a default runtime exists.
pub fn ensure_default_runtime() -> Result<(), Error> {
    let mut guard = global()
        .lock()
        .map_err(|_| Error::new(ErrorCategory::Internal, "runtime mutex poisoned"))?;
    if guard.is_none() {
        let profile = if let Ok(path) = std::env::var("SOFTGPU_PROFILE") {
            DeviceProfile::load_path(path)?
        } else {
            DeviceProfile::parse_bytes(DEFAULT_GENERIC_PROFILE.as_bytes())?
        };
        *guard = Some(Runtime::new(profile)?);
    }
    Ok(())
}

const DEFAULT_GENERIC_PROFILE: &str = include_str!("../embedded/softgpu-generic-v0.json");

/// Access the global runtime under the process mutex.
pub fn with_runtime<R>(f: impl FnOnce(&mut Runtime) -> R) -> Result<R, RuntimeError> {
    let mut guard = global()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let rt = guard.as_mut().ok_or(RuntimeError::NotInitialized)?;
    Ok(f(rt))
}

/// Like `with_runtime` but auto-installs the default profile if missing.
pub fn with_runtime_autostart<R>(f: impl FnOnce(&mut Runtime) -> R) -> Result<R, RuntimeError> {
    ensure_default_runtime().map_err(|e| RuntimeError::Internal(e.to_string()))?;
    with_runtime(f)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::AGENT_FEATURE_KERNEL_DISPATCH;
    use crate::profile::{ProfileField, ProfileIdentity, ResourceLimits};
    use crate::queue::{
        QUEUE_TYPE_MULTI, SOFTGPU_QUEUES_MAX, SOFTGPU_QUEUE_MAX_SIZE, SOFTGPU_QUEUE_MIN_SIZE,
    };
    use std::sync::Barrier;
    use std::thread;

    fn test_profile() -> DeviceProfile {
        DeviceProfile {
            schema_version: 1,
            profile_id: "test-gpu".into(),
            profile_revision: "0".into(),
            identity: ProfileIdentity {
                vendor: "SoftGPU".into(),
                product_name: "Test GPU".into(),
                architecture_family: "softgpu-abstract".into(),
                llvm_target: ProfileField::unknown(),
            },
            max_fidelity: FidelityLevel::Abi,
            conformance_allowed: false,
            resource_limits: ResourceLimits::default(),
            analytical_performance: Default::default(),
            quirks: vec![],
        }
    }

    #[test]
    fn init_enumerates_one_gpu_agent() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let mut count = 0;
        rt.iterate_agents(|agent| {
            count += 1;
            assert_eq!(agent.kind, AgentKind::Gpu);
            assert_eq!(agent.feature_mask, AGENT_FEATURE_KERNEL_DISPATCH);
            Ok(())
        })
        .unwrap();
        assert_eq!(count, 1);
        let name = rt
            .agent_get_info(
                rt.agents[0].agent.as_ref().unwrap().handle,
                AgentInfoAttr::Name,
            )
            .unwrap();
        assert_eq!(name, AgentInfoValue::Name("Test GPU".into()));
        rt.shut_down().unwrap();
    }

    #[test]
    fn path_c_regions_and_pools_allocate() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let agent = rt.agents[0].agent.as_ref().unwrap().handle;
        let mut region = None;
        rt.iterate_regions(agent, |space| {
            if space.kind == MemoryViewKind::RegionFineKernarg {
                region = Some(space.handle);
            }
            Ok(())
        })
        .unwrap();
        let region = region.expect("fine region");
        let ptr = rt.memory_allocate(region, 128).unwrap();
        assert!(!ptr.is_null());
        rt.memory_free(ptr).unwrap();

        let mut pool = None;
        rt.iterate_pools(agent, |space| {
            if space.kind == MemoryViewKind::PoolFineHost {
                pool = Some(space.handle);
            }
            Ok(())
        })
        .unwrap();
        let pool = pool.expect("fine pool");
        let ptr = rt.memory_allocate(pool, 256).unwrap();
        rt.memory_free(ptr).unwrap();
        rt.shut_down().unwrap();
    }

    #[test]
    fn signal_and_queue_observe_doorbell() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let agent = rt.agents[0].agent.as_ref().unwrap().handle;
        let q = rt
            .queue_create(agent, SOFTGPU_QUEUE_MIN_SIZE, QUEUE_TYPE_MULTI)
            .unwrap();
        assert!(!q.is_null());
        let doorbell = unsafe { (*q).doorbell_signal };
        rt.signal_store(PackedHandle::from_raw(doorbell), 1)
            .unwrap();
        assert_eq!(rt.queue_doorbell_count(q).unwrap(), 1);
        rt.queue_store_write_index(q, 1).unwrap();
        assert_eq!(rt.queue_load_write_index(q).unwrap(), 1);
        rt.queue_destroy(q).unwrap();
        rt.shut_down().unwrap();
    }

    #[test]
    fn stale_handle_after_shutdown_fails() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let handle = rt.agents[0].agent.as_ref().unwrap().handle;
        rt.shut_down().unwrap();
        let err = rt.agent_get_info(handle, AgentInfoAttr::Name).unwrap_err();
        assert_eq!(err, RuntimeError::NotInitialized);
        rt.init().unwrap();
        let err = rt.agent_get_info(handle, AgentInfoAttr::Name).unwrap_err();
        assert_eq!(err, RuntimeError::InvalidAgent);
        rt.shut_down().unwrap();
    }

    #[test]
    fn forged_handle_rejected() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let forged = PackedHandle::from_raw(0xDEAD_BEEF_DEAD_BEEF);
        assert_eq!(
            rt.agent_get_info(forged, AgentInfoAttr::Device)
                .unwrap_err(),
            RuntimeError::InvalidAgent
        );
        rt.shut_down().unwrap();
    }

    #[test]
    fn concurrent_init_and_iterate() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let shared = Mutex::new(rt);
        let barrier = Barrier::new(4);
        thread::scope(|scope| {
            for _ in 0..4 {
                scope.spawn(|| {
                    barrier.wait();
                    let guard = shared.lock().unwrap();
                    guard
                        .iterate_agents(|agent| {
                            assert_eq!(agent.kind, AgentKind::Gpu);
                            Ok(())
                        })
                        .unwrap();
                });
            }
        });
        shared.lock().unwrap().shut_down().unwrap();
    }

    #[test]
    fn traces_include_profile_and_fidelity() {
        let mut rt = Runtime::new(test_profile()).unwrap();
        rt.init().unwrap();
        let events = rt.trace_snapshot();
        assert!(events.iter().any(|e| matches!(
            e,
            TraceEvent::RuntimeInit {
                fidelity,
                profile_id,
                ..
            } if fidelity == "abi" && profile_id == "test-gpu"
        )));
        rt.shut_down().unwrap();
    }

    #[test]
    fn softgpu_queue_limits_are_software_defaults() {
        assert!(SOFTGPU_QUEUE_MIN_SIZE.is_power_of_two());
        assert!(SOFTGPU_QUEUE_MAX_SIZE.is_power_of_two());
        const { assert!(SOFTGPU_QUEUES_MAX > 0) };
    }
}