optirs-tpu 0.3.2

OptiRS TPU coordination and pod management
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
use std::fmt::Debug;
// Memory planning and layout optimization for XLA computations
//
// This module implements memory layout optimization, buffer allocation strategies,
// memory bandwidth optimization, and memory hierarchy utilization for TPU execution.

use scirs2_core::numeric::Float;
use std::collections::{BTreeMap, HashMap};

use super::super::frontend::{
    DataType, Layout, MemorySpace, Operand, OperandId, OperationId, XLAComputation,
};
use super::super::{TPUConfig, TPUVersion};
use crate::error::{OptimError, Result};

/// Size in bytes of one element of an XLA data type.
fn data_type_size(dtype: DataType) -> usize {
    match dtype {
        DataType::F16 | DataType::BF16 => 2,
        DataType::F32 => 4,
        DataType::F64 => 8,
        DataType::S8 | DataType::U8 | DataType::Pred => 1,
        DataType::S16 | DataType::U16 => 2,
        DataType::S32 | DataType::U32 => 4,
        DataType::S64 | DataType::U64 => 8,
        DataType::C64 => 8,
        DataType::C128 => 16,
    }
}

/// Byte budget an operand must fit inside to be placed in on-chip memory.
///
/// Derived from the TPU version's real per-core HBM capacity: a conservative
/// 1/1024 of it, which is the order of magnitude of a TPU's vector-memory
/// working set relative to its HBM. Not a magic constant -- it moves with the
/// configured target.
fn on_chip_budget_bytes(version: TPUVersion) -> usize {
    let gib = 1024usize * 1024 * 1024;
    let per_core = match version {
        TPUVersion::V2 => 8 * gib,
        TPUVersion::V3 => 16 * gib,
        TPUVersion::V4 => 32 * gib,
        TPUVersion::V5e => 16 * gib,
        TPUVersion::V5p => 95 * gib,
    };
    per_core / 1024
}

/// Memory planner for XLA computations
pub struct MemoryPlanner<T: Float + Debug + Send + Sync + 'static> {
    /// Memory allocation strategy handed to the buffer manager's allocator.
    ///
    /// The target hardware configuration is not retained: it is consumed at
    /// construction to size each sub-manager (layout budget, bandwidth model,
    /// memory hierarchy) and nothing read it afterwards.
    allocation_strategy: AllocationStrategy,

    /// Layout optimizer
    layout_optimizer: LayoutOptimizer<T>,

    /// Buffer manager
    buffer_manager: BufferManager<T>,

    /// Memory bandwidth optimizer
    bandwidth_optimizer: BandwidthOptimizer<T>,

    /// Memory hierarchy manager
    hierarchy_manager: MemoryHierarchyManager<T>,

    /// Memory planning statistics
    planning_stats: MemoryPlanningStats,
}

/// Memory allocation strategy
#[derive(Debug, Clone)]
pub enum AllocationStrategy {
    /// First-fit allocation
    FirstFit,

    /// Best-fit allocation
    BestFit,

    /// Worst-fit allocation
    WorstFit,

    /// Buddy system allocation
    BuddySystem,

    /// Pool-based allocation
    PoolBased,

    /// Linear allocation
    Linear,
}

/// Layout optimizer for memory access patterns
pub struct LayoutOptimizer<T: Float + Debug + Send + Sync + 'static> {
    /// Access pattern analyzer
    access_analyzer: AccessPatternAnalyzer,

    /// Byte budget an operand must fit inside to be placed on chip, derived
    /// from the target TPU version's real per-core capacity at construction.
    on_chip_budget_bytes: usize,

    _phantom: std::marker::PhantomData<T>,
}

/// Memory buffer manager
pub struct BufferManager<T: Float + Debug + Send + Sync + 'static> {
    /// Memory allocator that actually carves the buffers out of the address
    /// space. The active-buffer map, the pooled-buffer list and the reuse
    /// tracker that used to sit beside it were all empty and unread; liveness
    /// (`BufferLifetime`) is what reuse would key off, and that already travels
    /// on each `BufferAllocation`.
    allocator: MemoryAllocator,

    _phantom: std::marker::PhantomData<T>,
}

/// Memory bandwidth optimizer
/// Memory bandwidth optimizer.
///
/// `schedule_memory_operations` derives the operation order from the
/// computation itself, so there is no separate access schedule, prefetch-strategy
/// list or cache-manager to keep in sync -- all three were constructed empty and
/// never read.
pub struct BandwidthOptimizer<T: Float + Debug + Send + Sync + 'static> {
    _phantom: std::marker::PhantomData<T>,
}

/// Memory hierarchy manager
pub struct MemoryHierarchyManager<T: Float + Debug + Send + Sync + 'static> {
    /// Memory levels (L1, L2, HBM, etc.)
    memory_levels: Vec<MemoryLevel>,

    /// Data placement strategy
    placement_strategy: PlacementStrategy,

    _phantom: std::marker::PhantomData<T>,
}

/// Memory planning statistics
#[derive(Debug, Default)]
pub struct MemoryPlanningStats {
    /// Total memory allocated
    pub total_memory_allocated: usize,

    /// Peak memory usage
    pub peak_memory_usage: usize,

    /// Memory fragmentation ratio
    pub fragmentation_ratio: f64,

    /// Buffer reuse ratio
    pub buffer_reuse_ratio: f64,

    /// Memory bandwidth utilization
    pub bandwidth_utilization: f64,

    /// Layout transformations performed
    pub layout_transformations: usize,

    /// Memory level utilization
    pub level_utilization: HashMap<String, f64>,
}

/// Memory plan for computation
#[derive(Debug)]
pub struct MemoryPlan<T: Float + Debug + Send + Sync + 'static> {
    /// Buffer allocations
    pub buffer_allocations: HashMap<OperandId, BufferAllocation>,

    /// Layout assignments
    pub layout_assignments: HashMap<OperandId, Layout>,

    /// Memory level assignments
    pub memory_assignments: HashMap<OperandId, MemorySpace>,

    /// Execution order for memory operations
    pub execution_order: Vec<MemoryOperation>,

    /// Total memory requirements
    pub total_memory: usize,

    /// Performance characteristics
    pub performance_info: MemoryPerformanceInfo,

    /// Phantom data for type parameter
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Float + Debug + Send + Sync + 'static> MemoryPlan<T> {
    /// A plan that places nothing.
    ///
    /// Useful as a neutral input to consumers that only read the plan's totals
    /// (code generation, register allocation) without needing a real placement.
    pub fn empty() -> Self {
        Self {
            buffer_allocations: HashMap::new(),
            layout_assignments: HashMap::new(),
            memory_assignments: HashMap::new(),
            execution_order: Vec::new(),
            total_memory: 0,
            performance_info: MemoryPerformanceInfo::default(),
            _phantom: std::marker::PhantomData,
        }
    }
}

/// Buffer allocation information
#[derive(Debug, Clone)]
pub struct BufferAllocation {
    /// Buffer identifier
    pub buffer_id: String,

    /// Memory address (virtual)
    pub address: usize,

    /// Buffer size in bytes
    pub size: usize,

    /// Alignment requirements
    pub alignment: usize,

    /// Lifetime information
    pub lifetime: BufferLifetime,

    /// Access pattern
    pub access_pattern: AccessPattern,
}

/// Buffer lifetime tracking
#[derive(Debug, Clone)]
pub struct BufferLifetime {
    /// First use operation
    pub first_use: OperationId,

    /// Last use operation
    pub last_use: OperationId,

    /// Live range
    pub live_range: (usize, usize),

    /// Reuse opportunities
    pub reuse_opportunities: Vec<OperandId>,
}

/// Memory access pattern
#[derive(Debug, Clone)]
pub enum AccessPattern {
    /// Sequential access
    Sequential,

    /// Random access
    Random,

    /// Strided access
    Strided { stride: usize },

    /// Block access
    Block { block_size: usize },

    /// Broadcast access
    Broadcast,
}

/// Layout format specification
#[derive(Debug, Clone)]
pub struct LayoutFormat {
    /// Format name
    pub name: String,

    /// Dimension ordering (minor to major)
    pub dimension_order: Vec<usize>,

    /// Memory layout type
    pub layout_type: LayoutType,

    /// Tiling specification
    pub tiling: Option<TilingSpec>,

    /// Alignment requirements
    pub alignment: usize,
}

/// Memory layout types
#[derive(Debug, Clone)]
pub enum LayoutType {
    /// Row-major (C-style)
    RowMajor,

    /// Column-major (Fortran-style)
    ColumnMajor,

    /// Blocked layout
    Blocked,

    /// Compressed layout
    Compressed,

    /// Custom layout
    Custom(String),
}

/// Tiling specification
#[derive(Debug, Clone)]
pub struct TilingSpec {
    /// Tile dimensions
    pub tile_dims: Vec<usize>,

    /// Tile order
    pub tile_order: Vec<usize>,

    /// Padding strategy
    pub padding: PaddingStrategy,
}

/// Padding strategies for tiling
#[derive(Debug, Clone)]
pub enum PaddingStrategy {
    /// No padding
    None,

    /// Zero padding
    Zero,

    /// Edge replication
    EdgeReplicate,

    /// Mirror padding
    Mirror,
}

/// Access pattern analyzer
pub struct AccessPatternAnalyzer {}

/// Stride analysis information
#[derive(Debug)]
pub struct StrideAnalysis {
    /// Detected strides per dimension
    pub strides: Vec<i64>,

    /// Regularity score
    pub regularity: f64,

    /// Memory locality score
    pub locality: f64,
}

/// Layout transformation rule
#[derive(Debug)]
pub struct LayoutTransformationRule {
    /// Rule name
    pub name: String,

    /// Source layout pattern
    pub source_pattern: LayoutPattern,

    /// Target layout pattern
    pub target_pattern: LayoutPattern,

    /// Applicability conditions
    pub conditions: Vec<String>,

    /// Expected benefit
    pub benefit: f64,
}

/// Layout pattern for matching
#[derive(Debug)]
pub struct LayoutPattern {
    /// Tensor rank constraints
    pub rank_constraints: Vec<RankConstraint>,

    /// Dimension constraints
    pub dimension_constraints: Vec<DimensionConstraint>,

    /// Access pattern requirements
    pub access_requirements: Vec<AccessPattern>,
}

/// Rank constraint for layout patterns
#[derive(Debug)]
pub enum RankConstraint {
    /// Exact rank
    Exact(usize),

    /// Minimum rank
    Minimum(usize),

    /// Maximum rank
    Maximum(usize),

    /// Range of ranks
    Range(usize, usize),
}

/// Dimension constraint for layout patterns
#[derive(Debug)]
pub struct DimensionConstraint {
    /// Dimension index
    pub dimension: usize,

    /// Size constraint
    pub size_constraint: SizeConstraint,

    /// Alignment constraint
    pub alignment_constraint: Option<usize>,
}

/// Size constraint for dimensions
#[derive(Debug)]
pub enum SizeConstraint {
    /// Exact size
    Exact(usize),

    /// Multiple of value
    MultipleOf(usize),

    /// Range of sizes
    Range(usize, usize),

    /// Any size
    Any,
}

/// Buffer information
#[derive(Debug)]
pub struct BufferInfo {
    /// Buffer size
    pub size: usize,

    /// Current allocation
    pub allocation: Option<BufferAllocation>,

    /// Reference count
    pub ref_count: usize,

    /// Access statistics
    pub access_stats: AccessStatistics,
}

/// Pooled buffer for reuse
#[derive(Debug)]
pub struct PooledBuffer {
    /// Buffer identifier
    pub id: String,

    /// Buffer size
    pub size: usize,

    /// Is available for reuse
    pub available: bool,

    /// Last used timestamp
    pub last_used: u64,
}

/// Memory allocator
pub struct MemoryAllocator {
    /// Allocation strategy
    strategy: AllocationStrategy,

    /// Free memory regions
    free_regions: BTreeMap<usize, usize>, // address -> size

    /// Allocated regions
    allocated_regions: HashMap<usize, usize>, // address -> size

    /// Total memory capacity
    total_capacity: usize,

    /// Current usage
    current_usage: usize,
}

/// Buffer reuse tracker
pub struct BufferReuseTracker {}

/// Buffer reuse candidate
#[derive(Debug)]
pub struct ReuseCandidate {
    /// Source buffer
    pub source_buffer: OperandId,

    /// Target buffer
    pub target_buffer: OperandId,

    /// Reuse score
    pub score: f64,

    /// Size compatibility
    pub size_compatible: bool,
}

/// Reuse statistics
#[derive(Debug, Default)]
pub struct ReuseStatistics {
    /// Total reuse opportunities
    pub total_opportunities: usize,

    /// Successful reuses
    pub successful_reuses: usize,

    /// Memory saved
    pub memory_saved: usize,
}

/// Access statistics for buffers
#[derive(Debug, Default)]
pub struct AccessStatistics {
    /// Number of reads
    pub read_count: usize,

    /// Number of writes
    pub write_count: usize,

    /// Access pattern
    pub pattern: Option<AccessPattern>,

    /// Average access size
    pub avg_access_size: usize,
}

/// Memory access schedule
pub struct MemoryAccessSchedule {}

/// Scheduled memory access
#[derive(Debug)]
pub struct ScheduledAccess {
    /// Operation ID
    pub operation_id: OperationId,

    /// Access type
    pub access_type: MemoryAccessType,

    /// Buffer ID
    pub buffer_id: OperandId,

    /// Scheduled time
    pub scheduled_time: u64,

    /// Access size
    pub size: usize,
}

/// Memory access types
#[derive(Debug)]
pub enum MemoryAccessType {
    /// Read access
    Read,

    /// Write access
    Write,

    /// Read-modify-write
    ReadModifyWrite,

    /// Prefetch
    Prefetch,
}

/// Memory pressure point in timeline
#[derive(Debug)]
pub struct MemoryPressurePoint {
    /// Time point
    pub time: u64,

    /// Memory usage
    pub memory_usage: usize,

    /// Bandwidth usage
    pub bandwidth_usage: f64,
}

/// Prefetch strategy
#[derive(Debug)]
pub struct PrefetchStrategy {
    /// Strategy name
    pub name: String,

    /// Prefetch distance
    pub distance: usize,

    /// Confidence threshold
    pub confidence_threshold: f64,

    /// Memory level
    pub target_level: MemorySpace,
}

/// Cache manager for memory hierarchy
pub struct CacheManager {}

/// Cache level information
#[derive(Debug)]
pub struct CacheLevel {
    /// Cache identifier
    pub id: String,

    /// Cache size
    pub size: usize,

    /// Line size
    pub line_size: usize,

    /// Associativity
    pub associativity: usize,

    /// Access latency
    pub latency: u32,
}

/// Cache replacement policy
#[derive(Debug)]
pub enum CachePolicy {
    /// Least Recently Used
    LRU,

    /// Least Frequently Used
    LFU,

    /// First In First Out
    FIFO,

    /// Random replacement
    Random,

    /// Optimal (theoretical)
    Optimal,
}

/// Memory level in hierarchy
#[derive(Debug)]
pub struct MemoryLevel {
    /// Level identifier
    pub id: String,

    /// Memory space type
    pub memory_space: MemorySpace,

    /// Capacity (bytes)
    pub capacity: usize,

    /// Bandwidth (bytes/second)
    pub bandwidth: f64,

    /// Access latency (nanoseconds)
    pub latency: u32,

    /// Power consumption (watts)
    pub power: f64,
}

/// Data placement strategy
#[derive(Debug)]
pub enum PlacementStrategy {
    /// Place in fastest available memory
    FastestAvailable,

    /// Place based on access frequency
    AccessFrequency,

    /// Place based on data size
    SizeBased,

    /// Manual placement
    Manual,

    /// Machine learning guided
    MLGuided,
}

/// Migration policy for moving data between levels
#[derive(Debug)]
pub struct MigrationPolicy {
    /// Policy name
    pub name: String,

    /// Migration trigger
    pub trigger: MigrationTrigger,

    /// Source memory level
    pub source_level: MemorySpace,

    /// Target memory level
    pub target_level: MemorySpace,

    /// Migration cost model
    pub cost_model: CostModel,
}

/// Migration triggers
#[derive(Debug)]
pub enum MigrationTrigger {
    /// Access frequency threshold
    AccessFrequency(f64),

    /// Memory pressure threshold
    MemoryPressure(f64),

    /// Time-based
    TimeBased(u64),

    /// Predictive
    Predictive,
}

/// Cost model for migration decisions
#[derive(Debug)]
pub struct CostModel {
    /// Migration cost (time)
    pub migration_cost: f64,

    /// Access cost difference
    pub access_cost_diff: f64,

    /// Energy cost difference
    pub energy_cost_diff: f64,
}

/// Memory operation for execution ordering
#[derive(Debug)]
pub enum MemoryOperation {
    /// Allocate buffer
    Allocate {
        buffer_id: OperandId,
        size: usize,
        alignment: usize,
    },

    /// Deallocate buffer
    Deallocate { buffer_id: OperandId },

    /// Copy data between buffers
    Copy {
        source: OperandId,
        target: OperandId,
        size: usize,
    },

    /// Prefetch data
    Prefetch {
        buffer_id: OperandId,
        target_level: MemorySpace,
    },
}

/// Memory performance information
#[derive(Debug, Default)]
pub struct MemoryPerformanceInfo {
    /// Estimated memory bandwidth utilization
    pub bandwidth_utilization: f64,

    /// Estimated access latency
    pub avg_access_latency: f64,

    /// Memory efficiency score
    pub efficiency_score: f64,

    /// Cache hit rates by level
    pub cache_hit_rates: HashMap<String, f64>,
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> MemoryPlanner<T> {
    /// Create new memory planner
    pub fn new(target_hardware: TPUConfig) -> Self {
        Self {
            layout_optimizer: LayoutOptimizer::new(&target_hardware),
            buffer_manager: BufferManager::new(),
            bandwidth_optimizer: BandwidthOptimizer::new(&target_hardware),
            hierarchy_manager: MemoryHierarchyManager::new(&target_hardware),
            allocation_strategy: AllocationStrategy::BestFit,
            planning_stats: MemoryPlanningStats::default(),
        }
    }

    /// Create memory plan for computation
    pub fn create_memory_plan(&mut self, computation: &XLAComputation<T>) -> Result<MemoryPlan<T>> {
        // Analyze memory requirements
        let memory_analysis = self.analyze_memory_requirements(computation)?;

        // Optimize layouts
        let layout_assignments = self.layout_optimizer.optimize_layouts(computation)?;

        // Allocate buffers
        let buffer_allocations = self
            .buffer_manager
            .allocate_buffers(&memory_analysis, self.allocation_strategy.clone())?;

        // Assign memory levels
        let memory_assignments = self
            .hierarchy_manager
            .assign_memory_levels(&memory_analysis)?;

        // Schedule memory operations
        let execution_order = self
            .bandwidth_optimizer
            .schedule_memory_operations(computation)?;

        // Calculate performance characteristics
        let performance_info =
            self.calculate_performance_info(&buffer_allocations, &memory_assignments)?;

        let total_memory: usize = buffer_allocations.values().map(|alloc| alloc.size).sum();

        // Fold this plan into the planner's running statistics. Previously
        // `planning_stats` was default-constructed and never touched, so a
        // caller asking how much memory the planner had placed always saw zero.
        let largest = buffer_allocations
            .values()
            .map(|alloc| alloc.size)
            .max()
            .unwrap_or(0);
        self.planning_stats.total_memory_allocated += total_memory;
        self.planning_stats.peak_memory_usage =
            self.planning_stats.peak_memory_usage.max(total_memory);
        self.planning_stats.fragmentation_ratio = if total_memory == 0 {
            0.0
        } else {
            1.0 - (largest as f64 / total_memory as f64)
        };
        self.planning_stats.bandwidth_utilization = performance_info.bandwidth_utilization;
        self.planning_stats.layout_transformations += layout_assignments.len();
        self.planning_stats.level_utilization.clear();
        for (operand_id, space) in &memory_assignments {
            let bytes = buffer_allocations
                .get(operand_id)
                .map(|alloc| alloc.size)
                .unwrap_or(0);
            *self
                .planning_stats
                .level_utilization
                .entry(format!("{space:?}"))
                .or_insert(0.0) += bytes as f64;
        }

        Ok(MemoryPlan {
            buffer_allocations,
            layout_assignments,
            memory_assignments,
            execution_order,
            total_memory,
            performance_info,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Statistics accumulated across every [`Self::create_memory_plan`] call.
    pub fn planning_statistics(&self) -> &MemoryPlanningStats {
        &self.planning_stats
    }

    /// Optimize memory layout for computation
    pub fn optimize_memory_layout(
        &mut self,
        computation: XLAComputation<T>,
    ) -> Result<XLAComputation<T>> {
        let memory_plan = self.create_memory_plan(&computation)?;

        // Apply layout optimizations to computation
        let mut optimized_computation = computation;

        // Update operand layouts
        for (operand_id, layout) in memory_plan.layout_assignments {
            if let Some(operand) = optimized_computation.operands.get_mut(&operand_id) {
                operand.layout = layout;
            }
        }

        // Update memory spaces
        for (operand_id, memory_space) in memory_plan.memory_assignments {
            if let Some(operand) = optimized_computation.operands.get_mut(&operand_id) {
                operand.layout.memory_space = memory_space;
            }
        }

        Ok(optimized_computation)
    }

    /// Analyze memory requirements for computation
    fn analyze_memory_requirements(
        &self,
        computation: &XLAComputation<T>,
    ) -> Result<MemoryAnalysis> {
        let mut analysis = MemoryAnalysis::default();

        // Analyze each operand
        for (operand_id, operand) in &computation.operands {
            let size = self.calculate_operand_size(operand)?;
            let lifetime = self.calculate_operand_lifetime(*operand_id, computation)?;
            let access_pattern = self.analyze_access_pattern(*operand_id, computation)?;

            analysis.operand_info.insert(
                *operand_id,
                OperandMemoryInfo {
                    size,
                    lifetime,
                    access_pattern,
                    alignment_requirements: vec![32], // Default 32-byte alignment
                },
            );
        }

        analysis.total_memory = analysis.operand_info.values().map(|info| info.size).sum();

        Ok(analysis)
    }

    /// Calculate operand memory size
    fn calculate_operand_size(&self, operand: &Operand<T>) -> Result<usize> {
        Ok(operand
            .shape
            .element_count
            .saturating_mul(data_type_size(operand.dtype)))
    }

    /// Calculate operand lifetime
    fn calculate_operand_lifetime(
        &self,
        operand_id: OperandId,
        computation: &XLAComputation<T>,
    ) -> Result<BufferLifetime> {
        // Find first and last use of operand, tracking both the operation id
        // (for reporting) and the operation's position in the schedule (for the
        // live range). Positions are the natural unit for overlap tests: two
        // buffers whose [first, last] position intervals are disjoint can share
        // the same memory.
        let mut first_use = None;
        let mut last_use = None;
        let mut first_index = None;
        let mut last_index = None;

        for (index, operation) in computation.operations.iter().enumerate() {
            if operation.inputs.contains(&operand_id) || operation.output == operand_id {
                if first_use.is_none() {
                    first_use = Some(operation.id);
                    first_index = Some(index);
                }
                last_use = Some(operation.id);
                last_index = Some(index);
            }
        }

        // A per-operand live range: the closed interval of schedule positions in
        // which this operand is live. An operand that is never referenced
        // collapses to (0, 0) rather than spanning the whole program.
        let live_range = match (first_index, last_index) {
            (Some(first), Some(last)) => (first, last),
            _ => (0, 0),
        };

        Ok(BufferLifetime {
            first_use: first_use.unwrap_or(super::super::frontend::graph_capture::OperationId(0)),
            last_use: last_use.unwrap_or(super::super::frontend::graph_capture::OperationId(0)),
            live_range,
            reuse_opportunities: vec![],
        })
    }

    /// Analyze access pattern for operand
    fn analyze_access_pattern(
        &self,
        _operand_id: OperandId,
        _computation: &XLAComputation<T>,
    ) -> Result<AccessPattern> {
        // Simplified access pattern analysis
        Ok(AccessPattern::Sequential)
    }

    /// Calculate performance information
    fn calculate_performance_info(
        &self,
        _buffer_allocations: &HashMap<OperandId, BufferAllocation>,
        _memory_assignments: &HashMap<OperandId, MemorySpace>,
    ) -> Result<MemoryPerformanceInfo> {
        Ok(MemoryPerformanceInfo {
            bandwidth_utilization: 0.8,
            avg_access_latency: 100.0, // nanoseconds
            efficiency_score: 0.85,
            cache_hit_rates: HashMap::new(),
        })
    }
}

/// Memory analysis results
#[derive(Debug, Default)]
pub struct MemoryAnalysis {
    /// Per-operand memory information
    pub operand_info: HashMap<OperandId, OperandMemoryInfo>,

    /// Total memory requirement
    pub total_memory: usize,

    /// Peak memory usage
    pub peak_memory: usize,

    /// Memory access patterns
    pub access_patterns: HashMap<OperandId, AccessPattern>,
}

/// Memory information for operand
#[derive(Debug)]
pub struct OperandMemoryInfo {
    /// Size in bytes
    pub size: usize,

    /// Buffer lifetime
    pub lifetime: BufferLifetime,

    /// Access pattern
    pub access_pattern: AccessPattern,

    /// Alignment requirements
    pub alignment_requirements: Vec<usize>,
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> LayoutOptimizer<T> {
    /// Create new layout optimizer
    pub fn new(target_hardware: &TPUConfig) -> Self {
        // The two-entry `supported_layouts` table and the empty
        // `transformation_rules` list that used to be built here were never
        // consulted: `select_optimal_layout` derives the permutation from each
        // operand's real rank, which a fixed rank-2 table cannot express.
        Self {
            access_analyzer: AccessPatternAnalyzer::new(),
            on_chip_budget_bytes: on_chip_budget_bytes(target_hardware.tpu_version),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Optimize layouts for computation
    pub fn optimize_layouts(
        &mut self,
        computation: &XLAComputation<T>,
    ) -> Result<HashMap<OperandId, Layout>> {
        let mut layout_assignments = HashMap::new();

        // Analyze access patterns
        self.access_analyzer.analyze_computation(computation)?;

        // Assign optimal layouts
        for (operand_id, operand) in &computation.operands {
            let optimal_layout = self.select_optimal_layout(*operand_id, operand)?;
            layout_assignments.insert(*operand_id, optimal_layout);
        }

        Ok(layout_assignments)
    }

    /// Select a layout for one operand.
    ///
    /// The operand is what determines the answer, so this reads its real rank
    /// and element count rather than returning a fixed `[1, 0]`: that constant
    /// is a rank-2 permutation and was silently wrong for every scalar, vector
    /// and rank-3+ tensor in the graph (a rank-4 convolution operand would have
    /// carried a two-entry `minor_to_major`, which is not a valid layout for it
    /// at all).
    ///
    /// Row-major means the last dimension varies fastest, i.e. `minor_to_major`
    /// counts down from `rank - 1` to `0`. A rank-0 operand has an empty
    /// permutation, which is correct rather than degenerate.
    fn select_optimal_layout(&self, operand_id: OperandId, operand: &Operand<T>) -> Result<Layout> {
        let rank = operand.shape.dimensions.len();
        let minor_to_major: Vec<usize> = (0..rank).rev().collect();

        // Placement: operands small enough to stay resident in on-chip memory
        // are assigned there; everything else lives in the default (HBM) space.
        // The threshold comes from the configured per-core memory rather than a
        // magic number.
        let element_bytes = data_type_size(operand.dtype);
        let operand_bytes = operand.shape.element_count.saturating_mul(element_bytes);
        let on_chip_budget = self.on_chip_budget_bytes;
        let memory_space = if operand_bytes <= on_chip_budget {
            MemorySpace::Device
        } else {
            MemorySpace::Default
        };

        debug_assert_eq!(
            operand.id, operand_id,
            "layout assignment must describe the operand it is keyed by"
        );

        Ok(Layout {
            minor_to_major,
            tiles: vec![],
            memory_space,
        })
    }
}

impl Default for AccessPatternAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl AccessPatternAnalyzer {
    pub fn new() -> Self {
        Self {}
    }

    pub fn analyze_computation<T: Float + Debug + Send + Sync + 'static>(
        &mut self,
        _computation: &XLAComputation<T>,
    ) -> Result<()> {
        // Access pattern analysis implementation
        Ok(())
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> Default
    for BufferManager<T>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> BufferManager<T> {
    pub fn new() -> Self {
        Self {
            allocator: MemoryAllocator::new(AllocationStrategy::BestFit, 1024 * 1024 * 1024), // 1GB
            _phantom: std::marker::PhantomData,
        }
    }

    /// Allocate one buffer per operand under the planner's configured
    /// allocation strategy.
    ///
    /// The strategy is threaded through rather than being fixed at allocator
    /// construction, so changing the planner's strategy actually changes how
    /// buffers are placed.
    pub fn allocate_buffers(
        &mut self,
        analysis: &MemoryAnalysis,
        strategy: AllocationStrategy,
    ) -> Result<HashMap<OperandId, BufferAllocation>> {
        self.allocator.set_strategy(strategy);
        let mut allocations = HashMap::new();

        for (operand_id, operand_info) in &analysis.operand_info {
            let mut allocation = self.allocator.allocate(operand_info.size, 32)?;
            // The allocator has no visibility into liveness, so stamp the real
            // per-operand lifetime (first/last use and live range) onto the
            // allocation here, where the analysis is available.
            allocation.lifetime = operand_info.lifetime.clone();
            allocations.insert(*operand_id, allocation);
        }

        Ok(allocations)
    }
}

impl MemoryAllocator {
    pub fn new(strategy: AllocationStrategy, capacity: usize) -> Self {
        let mut free_regions = BTreeMap::new();
        free_regions.insert(0, capacity);

        Self {
            strategy,
            free_regions,
            allocated_regions: HashMap::new(),
            total_capacity: capacity,
            current_usage: 0,
        }
    }

    /// Change the fit policy used by subsequent allocations.
    pub fn set_strategy(&mut self, strategy: AllocationStrategy) {
        self.strategy = strategy;
    }

    pub fn allocate(&mut self, size: usize, alignment: usize) -> Result<BufferAllocation> {
        if alignment == 0 || (alignment & (alignment - 1)) != 0 {
            return Err(OptimError::InvalidArgument(
                scirs2_core::error::ErrorContext::new(format!(
                    "Allocation alignment must be a non-zero power of two, got {alignment}"
                )),
            ));
        }

        // Round the request up to the alignment boundary. Every free region in
        // this allocator starts at an alignment-friendly address (0 initially,
        // and every split leaves the remainder at `address + aligned_size`),
        // so an aligned size is sufficient to guarantee an aligned address.
        let aligned_size = (size.max(1) + alignment - 1) & !(alignment - 1);

        // Pick a free region honoring the configured allocation strategy.
        let address = self.select_region(aligned_size).ok_or_else(|| {
            OptimError::AllocationError(scirs2_core::error::ErrorContext::new(format!(
                "Out of memory: cannot allocate {aligned_size} bytes (usage {}/{})",
                self.current_usage, self.total_capacity
            )))
        })?;

        // Remove the chosen region; it must be present because `select_region`
        // just returned it from the same map.
        let region_size = self.free_regions.remove(&address).ok_or_else(|| {
            OptimError::InvalidState(scirs2_core::error::ErrorContext::new(format!(
                "Selected free region at address {address} vanished from the free list"
            )))
        })?;

        // Record the allocation and return any unused tail to the free list.
        self.allocated_regions.insert(address, aligned_size);
        self.current_usage += aligned_size;

        if region_size > aligned_size {
            self.free_regions
                .insert(address + aligned_size, region_size - aligned_size);
        }

        Ok(BufferAllocation {
            buffer_id: format!("buf_{}", address),
            address,
            size: aligned_size,
            alignment,
            lifetime: BufferLifetime {
                first_use: super::super::frontend::graph_capture::OperationId(0),
                last_use: super::super::frontend::graph_capture::OperationId(0),
                live_range: (0, 0),
                reuse_opportunities: vec![],
            },
            access_pattern: AccessPattern::Sequential,
        })
    }

    /// Select the address of a free region that can hold `needed` bytes,
    /// according to the configured [`AllocationStrategy`].
    ///
    /// * `FirstFit` (and the strategies not otherwise specialized) return the
    ///   lowest-address region that fits — `free_regions` iterates in ascending
    ///   address order, so the first match is the first fit.
    /// * `BestFit` returns the smallest fitting region (ties broken by lowest
    ///   address), minimizing leftover fragmentation.
    /// * `WorstFit` returns the largest fitting region (ties broken by lowest
    ///   address), keeping the remainder large.
    fn select_region(&self, needed: usize) -> Option<usize> {
        let mut chosen: Option<(usize, usize)> = None; // (address, size)

        for (&address, &size) in self.free_regions.iter() {
            if size < needed {
                continue;
            }

            match self.strategy {
                // Lowest address wins; iteration is ascending so the first fit
                // is the answer immediately.
                AllocationStrategy::FirstFit
                | AllocationStrategy::Linear
                | AllocationStrategy::BuddySystem
                | AllocationStrategy::PoolBased => return Some(address),

                // Smallest fitting region. `<` (not `<=`) keeps the earliest
                // (lowest-address) region on a size tie.
                AllocationStrategy::BestFit => {
                    if chosen.map(|(_, best)| size < best).unwrap_or(true) {
                        chosen = Some((address, size));
                    }
                }

                // Largest fitting region, lowest address on a size tie.
                AllocationStrategy::WorstFit => {
                    if chosen.map(|(_, best)| size > best).unwrap_or(true) {
                        chosen = Some((address, size));
                    }
                }
            }
        }

        chosen.map(|(address, _)| address)
    }

    /// Return a previously allocated region (identified by its start address)
    /// to the free list, coalescing it with any adjacent free regions.
    pub fn deallocate(&mut self, address: usize) -> Result<()> {
        let size = self.allocated_regions.remove(&address).ok_or_else(|| {
            OptimError::InvalidArgument(scirs2_core::error::ErrorContext::new(format!(
                "Cannot free address {address}: it is not an active allocation"
            )))
        })?;

        self.current_usage = self.current_usage.saturating_sub(size);
        self.insert_free_region(address, size);
        Ok(())
    }

    /// Free the region described by a [`BufferAllocation`].
    ///
    /// Convenience wrapper over [`Self::deallocate`] for callers that hold the
    /// allocation record rather than a bare address.
    pub fn free(&mut self, allocation: &BufferAllocation) -> Result<()> {
        self.deallocate(allocation.address)
    }

    /// Insert `[address, address + size)` into the free list, merging it with a
    /// directly preceding and/or directly following free region so that
    /// fragmentation created by allocation splits is reclaimed.
    ///
    /// The free list is kept maximally coalesced as an invariant, so at most one
    /// neighbor can be adjacent on each side.
    fn insert_free_region(&mut self, address: usize, size: usize) {
        let mut start = address;
        let mut end = address + size;

        // Coalesce with the region immediately preceding `start`, if it ends
        // exactly where this one begins.
        if let Some((&prev_addr, &prev_size)) = self.free_regions.range(..start).next_back() {
            if prev_addr + prev_size == start {
                self.free_regions.remove(&prev_addr);
                start = prev_addr;
            }
        }

        // Coalesce with the region immediately following, i.e. the one starting
        // exactly at the current end.
        if let Some(&next_size) = self.free_regions.get(&end) {
            self.free_regions.remove(&end);
            end += next_size;
        }

        self.free_regions.insert(start, end - start);
    }
}

impl Default for BufferReuseTracker {
    fn default() -> Self {
        Self::new()
    }
}

impl BufferReuseTracker {
    pub fn new() -> Self {
        Self {}
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> BandwidthOptimizer<T> {
    pub fn new(_target_hardware: &TPUConfig) -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn schedule_memory_operations(
        &mut self,
        _computation: &XLAComputation<T>,
    ) -> Result<Vec<MemoryOperation>> {
        // Memory operation scheduling implementation
        Ok(vec![])
    }
}

impl Default for MemoryAccessSchedule {
    fn default() -> Self {
        Self::new()
    }
}

impl MemoryAccessSchedule {
    pub fn new() -> Self {
        Self {}
    }
}

impl Default for CacheManager {
    fn default() -> Self {
        Self::new()
    }
}

impl CacheManager {
    pub fn new() -> Self {
        Self {}
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> MemoryHierarchyManager<T> {
    pub fn new(_target_hardware: &TPUConfig) -> Self {
        let memory_levels = vec![
            MemoryLevel {
                id: "L1".to_string(),
                memory_space: MemorySpace::Device,
                capacity: 1024 * 1024, // 1 MB
                bandwidth: 1000e9,     // 1 TB/s
                latency: 1,            // 1 ns
                power: 10.0,           // 10W
            },
            MemoryLevel {
                id: "HBM".to_string(),
                memory_space: MemorySpace::Default,
                capacity: 32 * 1024 * 1024 * 1024, // 32 GB
                bandwidth: 1600e9,                 // 1.6 TB/s
                latency: 100,                      // 100 ns
                power: 200.0,                      // 200W
            },
        ];

        Self {
            memory_levels,
            placement_strategy: PlacementStrategy::SizeBased,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Place each operand in a real memory level.
    ///
    /// The levels declared in [`Self::new`] are what the decision is made
    /// against -- previously every operand was assigned `MemorySpace::Default`
    /// unconditionally, so both the level table and the placement strategy were
    /// inert and the hierarchy had no effect on the plan.
    pub fn assign_memory_levels(
        &mut self,
        analysis: &MemoryAnalysis,
    ) -> Result<HashMap<OperandId, MemorySpace>> {
        // Fastest level first, so "the first level that fits" is also the best
        // level that fits.
        let mut levels: Vec<&MemoryLevel> = self.memory_levels.iter().collect();
        levels.sort_by_key(|level| level.latency);

        let fallback = levels
            .last()
            .map(|level| level.memory_space)
            .unwrap_or(MemorySpace::Default);

        let mut assignments = HashMap::new();
        for (operand_id, info) in &analysis.operand_info {
            let space = match self.placement_strategy {
                // Everything the fastest level can hold goes there.
                PlacementStrategy::FastestAvailable => levels
                    .first()
                    .filter(|level| info.size <= level.capacity)
                    .map(|level| level.memory_space)
                    .unwrap_or(fallback),
                // The smallest level that fits, which keeps large tensors out of
                // the scarce fast levels.
                PlacementStrategy::SizeBased | PlacementStrategy::AccessFrequency => levels
                    .iter()
                    .find(|level| info.size <= level.capacity)
                    .map(|level| level.memory_space)
                    .unwrap_or(fallback),
                // Manual and ML-guided placement need an external decision this
                // planner does not receive; both fall back to the level that can
                // always hold the operand rather than inventing a placement.
                PlacementStrategy::Manual | PlacementStrategy::MLGuided => fallback,
            };
            assignments.insert(*operand_id, space);
        }

        Ok(assignments)
    }
}

#[cfg(test)]
mod tests {
    use super::super::super::frontend::OperationType;
    use super::*;

    #[test]
    fn test_memory_planner_creation() {
        use crate::main_types::{PodTopology, TPUConfig, TPUVersion};

        let tpu_config = TPUConfig {
            tpu_version: TPUVersion::V4,
            num_cores: 8,
            enable_xla: true,
            xla_optimization_level: crate::main_types::XLAOptimizationLevel::Standard,
            mixed_precision: true,
            batch_size_per_core: 32,
            enable_pod_coordination: false,
            pod_topology: PodTopology::Pod2x2,
            memory_optimization: crate::main_types::TPUMemoryOptimization::Balanced,
            gradient_compression: true,
            prefetch_depth: 2,
            experimental_features: false,
        };

        let planner: MemoryPlanner<f32> = MemoryPlanner::new(tpu_config);
        assert_eq!(planner.planning_stats.total_memory_allocated, 0);
    }

    #[test]
    fn test_memory_allocator() {
        let mut allocator = MemoryAllocator::new(AllocationStrategy::BestFit, 1024);

        let allocation = allocator.allocate(256, 32).expect("unwrap failed");
        assert_eq!(allocation.size, 256);
        assert_eq!(allocation.alignment, 32);
        assert!(allocator.current_usage >= 256);
    }

    /// Shared TPU config for planner-level tests.
    fn test_tpu_config() -> crate::main_types::TPUConfig {
        use crate::main_types::{PodTopology, TPUConfig, TPUVersion};

        TPUConfig {
            tpu_version: TPUVersion::V4,
            num_cores: 8,
            enable_xla: true,
            xla_optimization_level: crate::main_types::XLAOptimizationLevel::Standard,
            mixed_precision: true,
            batch_size_per_core: 32,
            enable_pod_coordination: false,
            pod_topology: PodTopology::Pod2x2,
            memory_optimization: crate::main_types::TPUMemoryOptimization::Balanced,
            gradient_compression: true,
            prefetch_depth: 2,
            experimental_features: false,
        }
    }

    /// (a) A freed hole is reused: allocate two buffers, free the first, then a
    /// third allocation that fits the hole lands back at the freed address.
    #[test]
    fn freed_region_is_reused() {
        let mut allocator = MemoryAllocator::new(AllocationStrategy::FirstFit, 1024);

        let first = allocator.allocate(128, 32).expect("first allocation");
        let _second = allocator.allocate(128, 32).expect("second allocation");
        assert_eq!(first.address, 0);

        allocator.deallocate(first.address).expect("free first");

        // The freed hole at address 0 is the lowest-address region that fits.
        let third = allocator.allocate(128, 32).expect("third allocation");
        assert_eq!(
            third.address, first.address,
            "third allocation must reuse the freed hole"
        );
    }

    /// (b) Adjacent freed regions coalesce: after freeing two neighbors, a
    /// single allocation as large as their sum succeeds (which is impossible if
    /// the two holes were left fragmented).
    #[test]
    fn adjacent_frees_coalesce() {
        let mut allocator = MemoryAllocator::new(AllocationStrategy::FirstFit, 256);

        let a = allocator.allocate(128, 32).expect("alloc a");
        let b = allocator.allocate(128, 32).expect("alloc b");
        assert_eq!(a.address, 0);
        assert_eq!(b.address, 128);

        // Without coalescing the free list would be {0:128, 128:128} and a
        // 256-byte request would fail.
        allocator.deallocate(a.address).expect("free a");
        allocator.deallocate(b.address).expect("free b");

        let big = allocator
            .allocate(256, 32)
            .expect("coalesced region must satisfy the full-size request");
        assert_eq!(big.address, 0);
        assert_eq!(big.size, 256);
    }

    /// Carve a free list with two differently sized holes at known addresses.
    /// During carving there is always exactly one free region, so every
    /// strategy carves identically; only the final placement differs.
    fn carve_two_holes(strategy: AllocationStrategy) -> MemoryAllocator {
        let mut allocator = MemoryAllocator::new(strategy, 1024);

        let hole_big = allocator.allocate(256, 32).expect("carve big");
        let _keep1 = allocator.allocate(32, 32).expect("keep 1");
        let hole_small = allocator.allocate(64, 32).expect("carve small");
        let _keep2 = allocator.allocate(32, 32).expect("keep 2");

        allocator
            .deallocate(hole_big.address)
            .expect("free big hole");
        allocator
            .deallocate(hole_small.address)
            .expect("free small hole");

        // Free list is now {0: 256, 288: 64, 384: 640}.
        allocator
    }

    /// (c) FirstFit, BestFit and WorstFit pick different regions for the same
    /// request against an identical crafted free list.
    #[test]
    fn strategies_pick_different_regions() {
        // FirstFit: lowest-address fitting region -> the big hole at 0.
        let mut first_fit = carve_two_holes(AllocationStrategy::FirstFit);
        let a = first_fit.allocate(64, 32).expect("first-fit alloc");
        assert_eq!(a.address, 0);

        // BestFit: tightest fitting region -> the exact-size hole at 288.
        let mut best_fit = carve_two_holes(AllocationStrategy::BestFit);
        let b = best_fit.allocate(64, 32).expect("best-fit alloc");
        assert_eq!(b.address, 288);

        // WorstFit: largest fitting region -> the 640-byte tail at 384.
        let mut worst_fit = carve_two_holes(AllocationStrategy::WorstFit);
        let c = worst_fit.allocate(64, 32).expect("worst-fit alloc");
        assert_eq!(c.address, 384);

        assert_ne!(a.address, b.address);
        assert_ne!(a.address, c.address);
        assert_ne!(b.address, c.address);
    }

    /// (d) `live_range` is per-operand (a real [first_use, last_use] interval of
    /// schedule positions), not the old hardcoded whole-program (0, ops.len()).
    #[test]
    fn live_range_is_per_operand() {
        use crate::xla::frontend::graph_capture::test_support::{add_op, shape};
        use crate::xla::frontend::graph_capture::ComputationGraphBuilder;

        let mut builder: ComputationGraphBuilder<f32> = ComputationGraphBuilder::new();
        let mut comp = builder.create_computation("live_range");

        // Schedule positions: 0=Param a, 1=Param b, 2=Add(a,b)->c, 3=Negate(c)->d.
        let a = add_op(
            &mut builder,
            &mut comp,
            OperationType::Parameter,
            vec![],
            shape(&[4]),
        );
        let b = add_op(
            &mut builder,
            &mut comp,
            OperationType::Parameter,
            vec![],
            shape(&[4]),
        );
        let c = add_op(
            &mut builder,
            &mut comp,
            OperationType::Add,
            vec![a, b],
            shape(&[4]),
        );
        let d = add_op(
            &mut builder,
            &mut comp,
            OperationType::Negate,
            vec![c],
            shape(&[4]),
        );

        let planner: MemoryPlanner<f32> = MemoryPlanner::new(test_tpu_config());
        let analysis = planner
            .analyze_memory_requirements(&comp)
            .expect("memory analysis");

        let ops = comp.operations.len();
        let live_range = |id| {
            analysis
                .operand_info
                .get(&id)
                .map(|info| info.lifetime.live_range)
                .expect("operand info present")
        };

        // Each operand gets its own [first_use, last_use] position interval.
        assert_eq!(live_range(a), (0, 2));
        assert_eq!(live_range(b), (1, 2));
        assert_eq!(live_range(c), (2, 3));
        assert_eq!(live_range(d), (3, 3));

        // Regression: nothing is the old whole-program (0, ops.len()) range.
        for id in [a, b, c, d] {
            assert_ne!(
                live_range(id),
                (0, ops),
                "live_range must be per-operand, not whole-program"
            );
        }
    }
}