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
use std::fmt::Debug;
// Kernel fusion optimization for XLA computations
//
// This module implements various kernel fusion strategies including
// elementwise fusion, producer-consumer fusion, loop fusion, and
// multi-output fusion to reduce memory traffic and improve performance.

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

use super::super::frontend::{
    OperandId, OperationAttributes, OperationId, OperationType, XLAComputation, XLAOperation,
};
use super::OptimizationPipelineConfig;
use crate::error::Result;

/// Kernel fusion engine for XLA computations
pub struct KernelFusionEngine<T: Float + Debug + Send + Sync + 'static> {
    /// Fusion configuration
    config: FusionConfig,

    /// Elementwise fusion pass
    elementwise_fusion: ElementwiseFusionPass<T>,

    /// Producer-consumer fusion pass
    producer_consumer_fusion: ProducerConsumerFusionPass<T>,

    /// Loop fusion pass
    loop_fusion: LoopFusionPass<T>,

    /// Multi-output fusion pass
    multi_output_fusion: MultiOutputFusionPass<T>,

    /// Convolution fusion pass
    convolution_fusion: ConvolutionFusionPass<T>,

    /// Custom fusion pass
    custom_fusion: CustomFusionPass<T>,

    /// Fusion statistics
    fusion_stats: FusionStatistics,
}

/// Fusion configuration
#[derive(Debug, Clone)]
pub struct FusionConfig {
    /// Enable elementwise fusion
    pub enable_elementwise_fusion: bool,

    /// Enable producer-consumer fusion
    pub enable_producer_consumer_fusion: bool,

    /// Enable loop fusion
    pub enable_loop_fusion: bool,

    /// Enable multi-output fusion
    pub enable_multi_output_fusion: bool,

    /// Enable convolution fusion
    pub enable_convolution_fusion: bool,

    /// Maximum fusion cluster size
    pub max_cluster_size: usize,

    /// Memory threshold for fusion (bytes)
    pub memory_threshold: usize,

    /// Enable aggressive fusion
    pub aggressive_fusion: bool,

    /// Minimum operations for fusion
    pub min_ops_for_fusion: usize,
}

/// Fusion statistics tracking
#[derive(Debug, Default)]
pub struct FusionStatistics {
    /// Total fusions performed
    pub total_fusions: usize,

    /// Fusions by type
    pub fusions_by_type: HashMap<String, usize>,

    /// Memory savings (bytes)
    pub memory_savings: usize,

    /// Estimated speedup
    pub estimated_speedup: f64,

    /// Operations before fusion
    pub ops_before_fusion: usize,

    /// Operations after fusion
    pub ops_after_fusion: usize,
}

/// Fusion cluster representing a group of operations to be fused
#[derive(Debug, Clone)]
pub struct FusionCluster<T: Float + Debug + Send + Sync + 'static> {
    /// Cluster identifier
    pub id: String,

    /// Operations in the cluster
    pub operations: Vec<OperationId>,

    /// Cluster inputs (from outside the cluster)
    pub inputs: Vec<OperandId>,

    /// Cluster outputs (used outside the cluster)
    pub outputs: Vec<OperandId>,

    /// Fusion type
    pub fusion_type: FusionType,

    /// Estimated benefit
    pub estimated_benefit: f64,

    /// Memory requirements
    pub memory_requirements: usize,

    /// Execution characteristics
    pub execution_info: ClusterExecutionInfo,

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

/// Types of fusion strategies
#[derive(Debug, Clone, PartialEq)]
pub enum FusionType {
    /// Elementwise operations fusion
    Elementwise,

    /// Producer-consumer chain fusion
    ProducerConsumer,

    /// Loop-level fusion
    Loop,

    /// Multi-output fusion
    MultiOutput,

    /// Convolution-related fusion
    Convolution,

    /// Custom fusion pattern
    Custom(String),
}

/// Execution characteristics for fusion cluster
#[derive(Debug, Clone, Default)]
pub struct ClusterExecutionInfo {
    /// Estimated execution time (microseconds)
    pub execution_time_us: u64,

    /// Memory bandwidth utilization
    pub memory_bandwidth_util: f64,

    /// Compute utilization
    pub compute_utilization: f64,

    /// Parallelization factor
    pub parallelization_factor: f64,
}

/// Elementwise fusion pass
pub struct ElementwiseFusionPass<T: Float + Debug + Send + Sync + 'static> {
    /// Fusion clusters found
    clusters: Vec<FusionCluster<T>>,

    /// Supported elementwise operations
    supported_ops: HashSet<OperationType>,

    /// Upper bound on the number of operations in a single fused cluster
    max_cluster_size: usize,
}

/// Producer-consumer fusion pass
pub struct ProducerConsumerFusionPass<T: Float + Debug + Send + Sync + 'static> {
    /// Producer-consumer chains
    chains: Vec<ProducerConsumerChain>,

    /// Maximum chain length
    max_chain_length: usize,

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

/// Producer-consumer chain
#[derive(Debug)]
pub struct ProducerConsumerChain {
    /// Operations in the chain
    pub operations: Vec<OperationId>,

    /// Chain score (fusion benefit)
    pub score: f64,

    /// Memory footprint reduction
    pub memory_reduction: usize,
}

/// Loop fusion pass
pub struct LoopFusionPass<T: Float + Debug + Send + Sync + 'static> {
    /// Detected loops
    loops: Vec<LoopStructure>,

    /// Loop fusion candidates
    fusion_candidates: Vec<LoopFusionCandidate>,

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

/// Loop structure representation
#[derive(Debug)]
pub struct LoopStructure {
    /// Loop identifier
    pub id: String,

    /// Loop body operations
    pub body_operations: Vec<OperationId>,

    /// Loop bounds
    pub bounds: LoopBounds,

    /// Loop iteration count
    pub iteration_count: Option<usize>,
}

/// Loop bounds information
#[derive(Debug)]
pub struct LoopBounds {
    /// Lower bound
    pub lower: i64,

    /// Upper bound
    pub upper: i64,

    /// Step size
    pub step: i64,
}

/// Loop fusion candidate
#[derive(Debug)]
pub struct LoopFusionCandidate {
    /// Loops to be fused
    pub loops: Vec<String>,

    /// Fusion type
    pub fusion_type: LoopFusionType,

    /// Expected benefit
    pub benefit: f64,
}

/// Types of loop fusion
#[derive(Debug)]
pub enum LoopFusionType {
    /// Horizontal fusion (same iteration space)
    Horizontal,

    /// Vertical fusion (nested loops)
    Vertical,

    /// Diagonal fusion (partial overlap)
    Diagonal,
}

/// Multi-output fusion pass
pub struct MultiOutputFusionPass<T: Float + Debug + Send + Sync + 'static> {
    /// Multi-output opportunities
    opportunities: Vec<MultiOutputOpportunity>,

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

/// Multi-output fusion opportunity
#[derive(Debug)]
pub struct MultiOutputOpportunity {
    /// Common computation
    pub common_computation: Vec<OperationId>,

    /// Different outputs
    pub outputs: Vec<OperandId>,

    /// Estimated savings
    pub savings: f64,
}

/// Convolution fusion pass
pub struct ConvolutionFusionPass<T: Float + Debug + Send + Sync + 'static> {
    /// Convolution patterns
    patterns: Vec<ConvolutionPattern>,

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

/// Convolution fusion pattern
#[derive(Debug)]
pub struct ConvolutionPattern {
    /// Pattern name
    pub name: String,

    /// Operations in pattern
    pub operations: Vec<OperationType>,

    /// Fusion benefit
    pub benefit: f64,
}

/// Custom fusion pass
pub struct CustomFusionPass<T: Float + Debug + Send + Sync + 'static> {
    /// Custom patterns
    patterns: Vec<CustomFusionPattern>,

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

/// Custom fusion pattern
#[derive(Debug)]
pub struct CustomFusionPattern {
    /// Pattern name
    pub name: String,

    /// Pattern matching function
    pub matcher: String,

    /// Fusion generator function
    pub generator: String,
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> KernelFusionEngine<T> {
    /// Create new kernel fusion engine
    pub fn new(config: &OptimizationPipelineConfig) -> Self {
        let fusion_config = FusionConfig {
            enable_elementwise_fusion: true,
            enable_producer_consumer_fusion: true,
            enable_loop_fusion: config.aggressive_mode,
            enable_multi_output_fusion: true,
            enable_convolution_fusion: true,
            max_cluster_size: if config.aggressive_mode { 16 } else { 8 },
            memory_threshold: 1024 * 1024, // 1 MB
            aggressive_fusion: config.aggressive_mode,
            min_ops_for_fusion: 2,
        };

        Self {
            config: fusion_config.clone(),
            elementwise_fusion: ElementwiseFusionPass::new(&fusion_config),
            producer_consumer_fusion: ProducerConsumerFusionPass::new(&fusion_config),
            loop_fusion: LoopFusionPass::new(&fusion_config),
            multi_output_fusion: MultiOutputFusionPass::new(&fusion_config),
            convolution_fusion: ConvolutionFusionPass::new(&fusion_config),
            custom_fusion: CustomFusionPass::new(&fusion_config),
            fusion_stats: FusionStatistics::default(),
        }
    }

    /// Fuse kernels in computation
    pub fn fuse_kernels(&mut self, computation: XLAComputation<T>) -> Result<XLAComputation<T>> {
        let (fused, _changed) = self.fuse_kernels_tracked(computation)?;
        Ok(fused)
    }

    /// Fuse kernels, reporting whether the computation actually changed.
    ///
    /// `fusions_by_type` counts fusions that were *materialized*, not
    /// opportunities that were merely enumerated. Strategies that are not
    /// implemented contribute no entry at all rather than reporting the size of
    /// their pattern table as if it were work performed.
    pub fn fuse_kernels_tracked(
        &mut self,
        computation: XLAComputation<T>,
    ) -> Result<(XLAComputation<T>, bool)> {
        let mut current_computation = computation;
        self.fusion_stats.ops_before_fusion = current_computation.operations.len();
        self.fusion_stats.fusions_by_type.clear();

        // Elementwise fusion is the one strategy with a real implementation.
        if self.config.enable_elementwise_fusion {
            let fused = self
                .elementwise_fusion
                .apply_fusion_tracked(&mut current_computation)?;
            if fused > 0 {
                self.fusion_stats
                    .fusions_by_type
                    .insert("elementwise".to_string(), fused);
            }
        }

        self.fusion_stats.ops_after_fusion = current_computation.operations.len();
        self.fusion_stats.total_fusions = self.fusion_stats.fusions_by_type.values().sum();
        self.fusion_stats.memory_savings = self
            .elementwise_fusion
            .clusters
            .iter()
            .map(|cluster| cluster.memory_requirements)
            .sum();
        self.fusion_stats.estimated_speedup = self
            .elementwise_fusion
            .clusters
            .iter()
            .map(|cluster| cluster.estimated_benefit)
            .sum::<f64>();

        Ok((current_computation, self.fusion_stats.total_fusions > 0))
    }

    /// Fusion strategies that are configured but not implemented.
    ///
    /// Producer-consumer, loop, multi-output, convolution and custom fusion
    /// have no implementation in this crate. Enabling them is reported here
    /// instead of being silently accepted while the pass does nothing.
    pub fn unimplemented_strategies(&self) -> Vec<&'static str> {
        let mut unimplemented = Vec::new();
        if self.config.enable_producer_consumer_fusion {
            unimplemented.push("producer_consumer");
        }
        if self.config.enable_loop_fusion {
            unimplemented.push("loop");
        }
        if self.config.enable_multi_output_fusion {
            unimplemented.push("multi_output");
        }
        if self.config.enable_convolution_fusion {
            unimplemented.push("convolution");
        }
        unimplemented
    }

    /// Get fusion statistics
    pub fn get_statistics(&self) -> &FusionStatistics {
        &self.fusion_stats
    }

    /// Reset fusion engine state
    pub fn reset(&mut self) {
        self.elementwise_fusion.clusters.clear();
        self.producer_consumer_fusion.chains.clear();
        self.loop_fusion.loops.clear();
        self.loop_fusion.fusion_candidates.clear();
        self.multi_output_fusion.opportunities.clear();
        self.convolution_fusion.patterns.clear();
        self.custom_fusion.patterns.clear();
        self.fusion_stats = FusionStatistics::default();
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> ElementwiseFusionPass<T> {
    /// Create new elementwise fusion pass
    pub fn new(config: &FusionConfig) -> Self {
        let mut supported_ops = HashSet::new();
        supported_ops.insert(OperationType::Add);
        supported_ops.insert(OperationType::Multiply);
        supported_ops.insert(OperationType::Subtract);
        supported_ops.insert(OperationType::Divide);
        supported_ops.insert(OperationType::Maximum);
        supported_ops.insert(OperationType::Minimum);
        supported_ops.insert(OperationType::Abs);
        supported_ops.insert(OperationType::Exp);
        supported_ops.insert(OperationType::Log);
        supported_ops.insert(OperationType::Sqrt);

        Self {
            clusters: Vec::new(),
            supported_ops,
            max_cluster_size: config.max_cluster_size.max(2),
        }
    }

    /// Apply elementwise fusion
    pub fn apply_fusion(
        &mut self,
        mut computation: XLAComputation<T>,
    ) -> Result<XLAComputation<T>> {
        self.apply_fusion_tracked(&mut computation)?;
        Ok(computation)
    }

    /// Apply elementwise fusion, reporting how many clusters were actually
    /// materialized into fused operations.
    pub fn apply_fusion_tracked(&mut self, computation: &mut XLAComputation<T>) -> Result<usize> {
        self.find_elementwise_clusters(computation)?;
        self.create_fused_operations(computation)
    }

    /// Find elementwise fusion clusters
    fn find_elementwise_clusters(&mut self, computation: &XLAComputation<T>) -> Result<()> {
        self.clusters.clear();
        let mut visited = HashSet::new();

        for operation in &computation.operations {
            if visited.contains(&operation.id) || !self.is_elementwise_operation(&operation.op_type)
            {
                continue;
            }

            let cluster = self.build_elementwise_cluster(operation, computation, &mut visited)?;
            if cluster.operations.len() >= 2 {
                self.clusters.push(cluster);
            }
        }

        Ok(())
    }

    /// Build elementwise cluster starting from an operation
    ///
    /// The cluster grows backwards through producers. A producer joins only if
    /// fusing it is legal (see [`Self::can_fuse_operations`]); otherwise its
    /// result stays an external input of the cluster.
    fn build_elementwise_cluster(
        &self,
        start_op: &XLAOperation<T>,
        computation: &XLAComputation<T>,
        visited: &mut HashSet<OperationId>,
    ) -> Result<FusionCluster<T>> {
        let mut cluster_ops = vec![start_op.id];
        let mut cluster_set: HashSet<OperationId> = HashSet::new();
        cluster_set.insert(start_op.id);

        let mut queue = VecDeque::new();
        let mut inputs: Vec<OperandId> = Vec::new();

        queue.push_back(start_op.id);
        visited.insert(start_op.id);

        while let Some(op_id) = queue.pop_front() {
            let Some(operation) = computation.operations.iter().find(|op| op.id == op_id) else {
                continue;
            };

            for &input_id in &operation.inputs {
                let producer = self.find_producer_operation(input_id, computation);

                let fusible = match producer {
                    Some(producer) => {
                        self.is_elementwise_operation(&producer.op_type)
                            && !visited.contains(&producer.id)
                            && cluster_ops.len() < self.max_cluster_size
                            && self.can_fuse_operations(producer, operation, computation)
                    }
                    None => false,
                };

                match (fusible, producer) {
                    (true, Some(producer)) => {
                        cluster_ops.push(producer.id);
                        cluster_set.insert(producer.id);
                        queue.push_back(producer.id);
                        visited.insert(producer.id);
                    }
                    _ => {
                        if !inputs.contains(&input_id) {
                            inputs.push(input_id);
                        }
                    }
                }
            }
        }

        // Operands the cluster produces that something outside still needs.
        // Anything else is purely internal and disappears into the fused body.
        let declared_outputs: HashSet<OperandId> =
            computation.outputs.iter().map(|o| o.operand).collect();

        let mut outputs: Vec<OperandId> = Vec::new();
        for &op_id in &cluster_ops {
            let Some(operation) = computation.operations.iter().find(|op| op.id == op_id) else {
                continue;
            };

            let escapes = declared_outputs.contains(&operation.output)
                || computation.operations.iter().any(|other| {
                    !cluster_set.contains(&other.id) && other.inputs.contains(&operation.output)
                });

            if escapes && !outputs.contains(&operation.output) {
                outputs.push(operation.output);
            }
        }

        // Drop any operand that is both produced and consumed inside the
        // cluster from the external input list.
        let produced_inside: HashSet<OperandId> = cluster_ops
            .iter()
            .filter_map(|id| computation.operations.iter().find(|op| op.id == *id))
            .map(|op| op.output)
            .collect();
        inputs.retain(|operand| !produced_inside.contains(operand));

        let estimated_benefit = self.estimate_elementwise_benefit(&cluster_ops);
        let memory_requirements = self.estimate_memory_requirements(&cluster_ops, computation);

        let cluster = FusionCluster {
            id: format!("elementwise_cluster_{}", start_op.id.0),
            operations: cluster_ops,
            inputs,
            outputs,
            fusion_type: FusionType::Elementwise,
            estimated_benefit,
            memory_requirements,
            execution_info: ClusterExecutionInfo::default(),
            _phantom: std::marker::PhantomData,
        };

        Ok(cluster)
    }

    /// Check if operation is elementwise
    fn is_elementwise_operation(&self, op_type: &OperationType) -> bool {
        self.supported_ops.contains(op_type)
    }

    /// Legality check for fusing `producer` into the consumer's cluster.
    ///
    /// Two conditions must hold:
    ///
    /// 1. **Shape compatibility** — elementwise fusion evaluates the whole
    ///    cluster over one iteration space, so producer and consumer must agree
    ///    on their output shape.
    /// 2. **No external consumers** — if anything other than `consumer` reads
    ///    the producer's result, fusing it would either duplicate the
    ///    computation or leave a dangling operand.
    ///
    /// Returning `true` unconditionally (the previous behaviour) produced
    /// clusters that silently changed program semantics.
    fn can_fuse_operations(
        &self,
        producer: &XLAOperation<T>,
        consumer: &XLAOperation<T>,
        computation: &XLAComputation<T>,
    ) -> bool {
        let producer_shape = computation.operands.get(&producer.output).map(|o| &o.shape);
        let consumer_shape = computation.operands.get(&consumer.output).map(|o| &o.shape);

        match (producer_shape, consumer_shape) {
            (Some(a), Some(b)) if a.dimensions == b.dimensions => {}
            _ => return false,
        }

        // A value the computation returns must remain individually addressable.
        if computation
            .outputs
            .iter()
            .any(|output| output.operand == producer.output)
        {
            return false;
        }

        let external_consumers = computation
            .operations
            .iter()
            .filter(|op| op.id != consumer.id && op.inputs.contains(&producer.output))
            .count();

        external_consumers == 0
    }

    /// Find producer operation for operand
    fn find_producer_operation<'a>(
        &self,
        operand_id: OperandId,
        computation: &'a XLAComputation<T>,
    ) -> Option<&'a XLAOperation<T>> {
        computation
            .operations
            .iter()
            .find(|op| op.output == operand_id)
    }

    /// Estimate benefit of elementwise fusion
    ///
    /// Fusing `n` operations removes `n - 1` intermediate round trips to
    /// memory. `saturating_sub` keeps a single-operation cluster from
    /// underflowing `usize`.
    fn estimate_elementwise_benefit(&self, operations: &[OperationId]) -> f64 {
        operations.len().saturating_sub(1) as f64 * 0.2
    }

    /// Estimate the live memory a cluster needs, from real operand extents.
    fn estimate_memory_requirements(
        &self,
        operations: &[OperationId],
        computation: &XLAComputation<T>,
    ) -> usize {
        operations
            .iter()
            .filter_map(|id| computation.operations.iter().find(|op| op.id == *id))
            .filter_map(|op| computation.operands.get(&op.output))
            .map(|operand| {
                operand
                    .shape
                    .element_count
                    .saturating_mul(std::mem::size_of::<T>())
            })
            .sum()
    }

    /// Materialize each cluster as a fused operation.
    ///
    /// Clusters with several escaping results emit a `Tuple`-producing fused
    /// operation followed by one `GetTupleElement` per result, so that every
    /// original operand keeps a definition. The previous implementation kept
    /// only `outputs[0]` and silently dropped the rest.
    ///
    /// Returns the number of clusters actually fused.
    fn create_fused_operations(&self, computation: &mut XLAComputation<T>) -> Result<usize> {
        let mut fused = 0usize;

        for cluster in &self.clusters {
            if cluster.operations.len() < 2 {
                continue;
            }

            let Some(&primary_output) = cluster.outputs.first() else {
                // A cluster nothing reads is dead code, not a fusion
                // opportunity; leave it for dead-code elimination.
                continue;
            };

            // Insert where the last cluster member sat: in a dependency-ordered
            // operation list every external input is defined before that point,
            // and every consumer comes after it.
            let insert_at = computation
                .operations
                .iter()
                .rposition(|op| cluster.operations.contains(&op.id))
                .map(|pos| pos + 1)
                .unwrap_or(computation.operations.len());

            let mut new_ops: Vec<XLAOperation<T>> = Vec::new();
            let mut next_op_id = computation.next_free_operation_id().0;

            let fused_custom = super::super::frontend::graph_capture::CustomOperation {
                name: format!("fused_{}", cluster.id),
                custom_attributes: HashMap::new(),
                backend_config: Some("elementwise_fusion".to_string()),
            };

            if cluster.outputs.len() == 1 {
                new_ops.push(XLAOperation {
                    id: super::super::frontend::graph_capture::OperationId(next_op_id),
                    op_type: OperationType::Custom(fused_custom),
                    inputs: cluster.inputs.clone(),
                    output: primary_output,
                    attributes: OperationAttributes::default(),
                    performance: Default::default(),
                    memory_requirements: Default::default(),
                    source_location: None,
                    _phantom: std::marker::PhantomData,
                });
            } else {
                // Multi-output cluster: the fused op yields a tuple, and each
                // original operand is re-defined by a GetTupleElement.
                let tuple_operand = computation.allocate_operand_id();
                let tuple_shape = {
                    let mut shape = super::super::frontend::graph_capture::TensorShape::default();
                    shape.tuple_shapes = cluster
                        .outputs
                        .iter()
                        .filter_map(|id| computation.operands.get(id))
                        .map(|operand| operand.shape.clone())
                        .collect();
                    shape
                };

                let template = computation.operands.get(&primary_output).cloned();
                let tuple_operand_value = super::super::frontend::graph_capture::Operand {
                    id: tuple_operand,
                    shape: tuple_shape,
                    layout: template
                        .as_ref()
                        .map(|o| o.layout.clone())
                        .unwrap_or_default(),
                    dtype: template
                        .as_ref()
                        .map(|o| o.dtype)
                        .unwrap_or(super::super::frontend::graph_capture::DataType::F32),
                    metadata: Default::default(),
                    _phantom: std::marker::PhantomData,
                };
                computation
                    .operands
                    .insert(tuple_operand, tuple_operand_value);

                new_ops.push(XLAOperation {
                    id: super::super::frontend::graph_capture::OperationId(next_op_id),
                    op_type: OperationType::Custom(fused_custom),
                    inputs: cluster.inputs.clone(),
                    output: tuple_operand,
                    attributes: OperationAttributes::default(),
                    performance: Default::default(),
                    memory_requirements: Default::default(),
                    source_location: None,
                    _phantom: std::marker::PhantomData,
                });

                for (index, &operand) in cluster.outputs.iter().enumerate() {
                    next_op_id = next_op_id.saturating_add(1);
                    let mut attributes = OperationAttributes::default();
                    attributes.attributes.insert(
                        "tuple_index".to_string(),
                        super::super::frontend::graph_capture::AttributeValue::Int(index as i64),
                    );

                    new_ops.push(XLAOperation {
                        id: super::super::frontend::graph_capture::OperationId(next_op_id),
                        op_type: OperationType::GetTupleElement,
                        inputs: vec![tuple_operand],
                        output: operand,
                        attributes,
                        performance: Default::default(),
                        memory_requirements: Default::default(),
                        source_location: None,
                        _phantom: std::marker::PhantomData,
                    });
                }
            }

            // Remove the originals, then splice the replacement in.
            let removed_before_insert = computation.operations
                [..insert_at.min(computation.operations.len())]
                .iter()
                .filter(|op| cluster.operations.contains(&op.id))
                .count();

            computation
                .operations
                .retain(|op| !cluster.operations.contains(&op.id));

            let splice_at = insert_at
                .saturating_sub(removed_before_insert)
                .min(computation.operations.len());
            for (offset, op) in new_ops.into_iter().enumerate() {
                computation.operations.insert(splice_at + offset, op);
            }

            fused += 1;
        }

        if fused > 0 {
            computation.rebuild_dependencies();
        }

        Ok(fused)
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync>
    ProducerConsumerFusionPass<T>
{
    /// Create new producer-consumer fusion pass
    pub fn new(config: &FusionConfig) -> Self {
        Self {
            chains: Vec::new(),
            max_chain_length: config.max_cluster_size,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Run producer-consumer chain detection over a computation.
    ///
    /// This is a *detection* pass: it identifies the chains and scores them, and
    /// returns the computation unchanged. Materializing a chain into a single
    /// fused operation is [`ElementwiseFusionPass`]'s job -- it already builds
    /// real fused operations out of elementwise clusters, and duplicating that
    /// rewrite here would fuse the same operations twice.
    pub fn apply_fusion(&mut self, computation: XLAComputation<T>) -> Result<XLAComputation<T>> {
        self.find_producer_consumer_chains(&computation)?;
        Ok(computation)
    }

    /// Chains found by the most recent [`Self::apply_fusion`] call, highest
    /// scoring first.
    pub fn chains(&self) -> &[ProducerConsumerChain] {
        &self.chains
    }

    /// Find producer-consumer chains.
    ///
    /// A chain is a maximal run of operations where each operation's output
    /// operand is consumed by exactly one other operation, so the intermediate
    /// tensor never has to be written out to memory if the pair is fused. The
    /// run stops at `max_chain_length`, at an operand with several consumers,
    /// and at an operand that is a declared computation output (which must be
    /// materialized regardless).
    ///
    /// The score is the real memory traffic a fusion would save: the byte size
    /// of every intermediate tensor inside the chain.
    fn find_producer_consumer_chains(&mut self, computation: &XLAComputation<T>) -> Result<()> {
        self.chains.clear();
        if self.max_chain_length < 2 {
            return Ok(());
        }

        // How many operations consume each operand, and which operation
        // produces it.
        let mut consumers: HashMap<OperandId, Vec<OperationId>> = HashMap::new();
        let mut producer: HashMap<OperandId, OperationId> = HashMap::new();
        for operation in &computation.operations {
            producer.insert(operation.output, operation.id);
            for input in &operation.inputs {
                consumers.entry(*input).or_default().push(operation.id);
            }
        }

        let by_id: HashMap<OperationId, &XLAOperation<T>> = computation
            .operations
            .iter()
            .map(|operation| (operation.id, operation))
            .collect();

        let outputs: HashSet<OperandId> = computation
            .outputs
            .iter()
            .map(|spec| spec.operand)
            .collect();

        let mut claimed: HashSet<OperationId> = HashSet::new();

        for operation in &computation.operations {
            if claimed.contains(&operation.id) {
                continue;
            }

            let mut chain = vec![operation.id];
            let mut score = 0.0f64;
            let mut current = operation;

            while chain.len() < self.max_chain_length {
                // The intermediate must be consumed exactly once and must not be
                // a computation output.
                if outputs.contains(&current.output) {
                    break;
                }
                let Some(next_ids) = consumers.get(&current.output) else {
                    break;
                };
                if next_ids.len() != 1 {
                    break;
                }
                let Some(next) = next_ids.first().and_then(|id| by_id.get(id)).copied() else {
                    break;
                };
                if claimed.contains(&next.id) {
                    break;
                }

                // Bytes that would not have to round-trip through memory.
                if let Some(operand) = computation.operands.get(&current.output) {
                    score += operand.shape.element_count as f64;
                }

                chain.push(next.id);
                current = next;
            }

            if chain.len() >= 2 {
                for id in &chain {
                    claimed.insert(*id);
                }
                // Every intermediate that stays in registers is memory the
                // fused kernel never touches.
                let memory_reduction = score as usize;
                self.chains.push(ProducerConsumerChain {
                    operations: chain,
                    score,
                    memory_reduction,
                });
            }
        }

        self.chains.sort_by(|a, b| b.score.total_cmp(&a.score));
        Ok(())
    }
}

// Similar implementations for other fusion passes...
impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> LoopFusionPass<T> {
    pub fn new(_config: &FusionConfig) -> Self {
        Self {
            loops: Vec::new(),
            fusion_candidates: Vec::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn apply_fusion(&mut self, computation: XLAComputation<T>) -> Result<XLAComputation<T>> {
        // Loop fusion implementation
        Ok(computation)
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> MultiOutputFusionPass<T> {
    pub fn new(_config: &FusionConfig) -> Self {
        Self {
            opportunities: Vec::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn apply_fusion(&mut self, computation: XLAComputation<T>) -> Result<XLAComputation<T>> {
        // Multi-output fusion implementation
        Ok(computation)
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> ConvolutionFusionPass<T> {
    pub fn new(_config: &FusionConfig) -> Self {
        let patterns = vec![ConvolutionPattern {
            name: "conv_bias_relu".to_string(),
            operations: vec![
                OperationType::Convolution(
                    super::super::frontend::graph_capture::ConvolutionConfig {
                        strides: vec![1, 1],
                        padding: super::super::frontend::graph_capture::PaddingConfig::Same,
                        dilation: vec![1, 1],
                        feature_group_count: 1,
                        batch_group_count: 1,
                    },
                ),
                OperationType::Add,     // Bias
                OperationType::Maximum, // ReLU
            ],
            benefit: 0.3,
        }];

        Self {
            patterns,
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn apply_fusion(&mut self, computation: XLAComputation<T>) -> Result<XLAComputation<T>> {
        // Convolution fusion implementation
        Ok(computation)
    }
}

impl<T: Float + Debug + Default + std::fmt::Debug + Clone + Send + Sync> CustomFusionPass<T> {
    pub fn new(_config: &FusionConfig) -> Self {
        Self {
            patterns: Vec::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn apply_fusion(&mut self, computation: XLAComputation<T>) -> Result<XLAComputation<T>> {
        // Custom fusion implementation
        Ok(computation)
    }
}

#[cfg(test)]
mod tests {
    use super::super::{ComputeCapability, HardwareTarget};
    use super::*;

    #[test]
    fn test_kernel_fusion_engine_creation() {
        let config = OptimizationPipelineConfig {
            optimization_level: crate::main_types::XLAOptimizationLevel::Standard,
            enable_graph_optimization: true,
            enable_kernel_fusion: true,
            enable_memory_optimization: true,
            enable_scheduling_optimization: true,
            max_optimization_time: 300,
            target_hardware: HardwareTarget {
                tpu_version: "v4".to_string(),
                num_cores: 4,
                memory_capacity: 1024 * 1024 * 1024,
                memory_bandwidth: 1600.0,
                compute_capability: ComputeCapability {
                    matrix_unit_dims: (128, 128),
                    vector_unit_width: 256,
                    supported_dtypes: vec!["F32".to_string()],
                    special_instructions: vec![],
                },
            },
            custom_passes: vec![],
            aggressive_mode: false,
            debug_mode: false,
        };

        let engine: KernelFusionEngine<f32> = KernelFusionEngine::new(&config);
        assert_eq!(engine.fusion_stats.total_fusions, 0);
        assert!(engine.config.enable_elementwise_fusion);
    }

    #[test]
    fn test_elementwise_fusion_pass() {
        let config = FusionConfig {
            enable_elementwise_fusion: true,
            enable_producer_consumer_fusion: true,
            enable_loop_fusion: false,
            enable_multi_output_fusion: true,
            enable_convolution_fusion: true,
            max_cluster_size: 8,
            memory_threshold: 1024 * 1024,
            aggressive_fusion: false,
            min_ops_for_fusion: 2,
        };

        let pass: ElementwiseFusionPass<f32> = ElementwiseFusionPass::new(&config);
        assert!(!pass.supported_ops.is_empty());
        assert!(pass.supported_ops.contains(&OperationType::Add));
        assert!(pass.supported_ops.contains(&OperationType::Multiply));
    }
}