oxicuda-ptx 0.1.2

OxiCUDA PTX - PTX code generation DSL and IR for GPU kernel development
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
//! Dead code elimination for PTX instruction sequences.
//!
//! This module implements a fixed-point dead code elimination (DCE) pass.
//! An instruction is *dead* if it defines a register that is never used by
//! any subsequent instruction **and** the instruction has no side effects.

use std::collections::HashSet;

use crate::ir::{Instruction, Operand, Register, WmmaOp};

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Remove instructions whose results are never used.
///
/// Performs iterative dead code elimination until a fixed point is reached:
/// each pass removes instructions that define registers not consumed by any
/// other instruction, provided the instruction has no side effects.
///
/// # Arguments
///
/// * `instructions` - The original instruction sequence.
///
/// # Returns
///
/// A tuple of `(optimized_instructions, eliminated_count)`.
pub fn eliminate_dead_code(instructions: &[Instruction]) -> (Vec<Instruction>, usize) {
    let mut current: Vec<Instruction> = instructions.to_vec();
    let mut total_eliminated: usize = 0;

    loop {
        let (next, eliminated) = dce_pass(&current);
        if eliminated == 0 {
            break;
        }
        total_eliminated += eliminated;
        current = next;
    }

    (current, total_eliminated)
}

// ---------------------------------------------------------------------------
// Internal pass
// ---------------------------------------------------------------------------

/// Single pass of dead code elimination.
///
/// Returns `(surviving_instructions, number_eliminated)`.
fn dce_pass(instructions: &[Instruction]) -> (Vec<Instruction>, usize) {
    // Phase 1: Collect the set of all registers that are *used* by any instruction.
    let mut used_regs: HashSet<String> = HashSet::new();
    for inst in instructions {
        for reg in uses(inst) {
            used_regs.insert(reg.name.clone());
        }
    }

    // Phase 2: Mark each instruction as live or dead.
    let mut result = Vec::with_capacity(instructions.len());
    let mut eliminated: usize = 0;

    for inst in instructions {
        if has_side_effects(inst) {
            // Side-effecting instructions are always kept.
            result.push(inst.clone());
            continue;
        }

        let defined = defs(inst);
        if defined.is_empty() {
            // Instructions that define nothing and have no side effects
            // are kept (e.g., they shouldn't exist, but be conservative).
            result.push(inst.clone());
            continue;
        }

        // The instruction is dead if *none* of its defined registers are used.
        let any_def_used = defined.iter().any(|r| used_regs.contains(&r.name));

        if any_def_used {
            result.push(inst.clone());
        } else {
            eliminated += 1;
        }
    }

    (result, eliminated)
}

// ---------------------------------------------------------------------------
// Side-effect classification
// ---------------------------------------------------------------------------

/// Check if an instruction has side effects and therefore cannot be eliminated
/// even when its result register is unused.
///
/// Side-effecting instructions include memory stores, control flow, barriers,
/// fences, TMA operations, async copies, and meta-instructions (comments, raw).
const fn has_side_effects(inst: &Instruction) -> bool {
    match inst {
        // Memory stores, async copy, control flow, synchronization, fences,
        // TMA load, atomic operations, meta-instructions — all side-effecting.
        Instruction::Store { .. }
        | Instruction::CpAsync { .. }
        | Instruction::CpAsyncCommit
        | Instruction::CpAsyncWait { .. }
        | Instruction::Branch { .. }
        | Instruction::Label(_)
        | Instruction::Return
        | Instruction::BarSync { .. }
        | Instruction::BarArrive { .. }
        | Instruction::FenceAcqRel { .. }
        | Instruction::TmaLoad { .. }
        | Instruction::Atom { .. }
        | Instruction::AtomCas { .. }
        | Instruction::Red { .. }
        | Instruction::SurfStore { .. }
        | Instruction::Stmatrix { .. }
        | Instruction::Setmaxnreg { .. }
        | Instruction::Griddepcontrol { .. }
        | Instruction::FenceProxy { .. }
        | Instruction::MbarrierInit { .. }
        | Instruction::MbarrierArrive { .. }
        | Instruction::MbarrierWait { .. }
        | Instruction::Tcgen05Mma { .. }
        | Instruction::BarrierCluster
        | Instruction::FenceCluster
        | Instruction::CpAsyncBulk { .. }
        | Instruction::Comment(_)
        | Instruction::Raw(_) => true,

        // WMMA store operations write to memory
        Instruction::Wmma { op, .. } => matches!(op, WmmaOp::StoreD),

        // Pure computation instructions: no side effects
        Instruction::Add { .. }
        | Instruction::Sub { .. }
        | Instruction::Mul { .. }
        | Instruction::Mad { .. }
        | Instruction::Fma { .. }
        | Instruction::MadLo { .. }
        | Instruction::MadHi { .. }
        | Instruction::MadWide { .. }
        | Instruction::Neg { .. }
        | Instruction::Abs { .. }
        | Instruction::Min { .. }
        | Instruction::Max { .. }
        | Instruction::Brev { .. }
        | Instruction::Clz { .. }
        | Instruction::Popc { .. }
        | Instruction::Bfind { .. }
        | Instruction::Bfe { .. }
        | Instruction::Bfi { .. }
        | Instruction::Rcp { .. }
        | Instruction::Rsqrt { .. }
        | Instruction::Sqrt { .. }
        | Instruction::Ex2 { .. }
        | Instruction::Lg2 { .. }
        | Instruction::Sin { .. }
        | Instruction::Cos { .. }
        | Instruction::Shl { .. }
        | Instruction::Shr { .. }
        | Instruction::Div { .. }
        | Instruction::Rem { .. }
        | Instruction::And { .. }
        | Instruction::Or { .. }
        | Instruction::Xor { .. }
        | Instruction::SetP { .. }
        | Instruction::Load { .. }
        | Instruction::Cvt { .. }
        | Instruction::Mma { .. }
        | Instruction::Wgmma { .. }
        | Instruction::MovSpecial { .. }
        | Instruction::LoadParam { .. }
        | Instruction::Dp4a { .. }
        | Instruction::Dp2a { .. }
        | Instruction::Tex1d { .. }
        | Instruction::Tex2d { .. }
        | Instruction::Tex3d { .. }
        | Instruction::SurfLoad { .. }
        | Instruction::Redux { .. }
        | Instruction::ElectSync { .. }
        // Pragma is a directive hint — no side effects on execution
        | Instruction::Pragma(_)
        // ldmatrix: warp-cooperative load — result registers are output side, no mem-side effect
        | Instruction::Ldmatrix { .. } => false,
    }
}

// ---------------------------------------------------------------------------
// Register extraction helpers (mirrored from register_pressure)
// ---------------------------------------------------------------------------

/// Extract registers defined (written to) by an instruction.
fn defs(inst: &Instruction) -> Vec<&Register> {
    match inst {
        Instruction::Add { dst, .. }
        | Instruction::Sub { dst, .. }
        | Instruction::Mul { dst, .. }
        | Instruction::Mad { dst, .. }
        | Instruction::MadLo { dst, .. }
        | Instruction::MadHi { dst, .. }
        | Instruction::MadWide { dst, .. }
        | Instruction::Fma { dst, .. }
        | Instruction::Neg { dst, .. }
        | Instruction::Abs { dst, .. }
        | Instruction::Min { dst, .. }
        | Instruction::Max { dst, .. }
        | Instruction::Brev { dst, .. }
        | Instruction::Clz { dst, .. }
        | Instruction::Popc { dst, .. }
        | Instruction::Bfind { dst, .. }
        | Instruction::Bfe { dst, .. }
        | Instruction::Bfi { dst, .. }
        | Instruction::Rcp { dst, .. }
        | Instruction::Rsqrt { dst, .. }
        | Instruction::Sqrt { dst, .. }
        | Instruction::Ex2 { dst, .. }
        | Instruction::Lg2 { dst, .. }
        | Instruction::Sin { dst, .. }
        | Instruction::Cos { dst, .. }
        | Instruction::Shl { dst, .. }
        | Instruction::Shr { dst, .. }
        | Instruction::Div { dst, .. }
        | Instruction::Rem { dst, .. }
        | Instruction::And { dst, .. }
        | Instruction::Or { dst, .. }
        | Instruction::Xor { dst, .. }
        | Instruction::SetP { dst, .. }
        | Instruction::Load { dst, .. }
        | Instruction::Cvt { dst, .. }
        | Instruction::MovSpecial { dst, .. }
        | Instruction::LoadParam { dst, .. }
        | Instruction::Atom { dst, .. }
        | Instruction::AtomCas { dst, .. }
        | Instruction::Dp4a { dst, .. }
        | Instruction::Dp2a { dst, .. }
        | Instruction::Tex1d { dst, .. }
        | Instruction::Tex2d { dst, .. }
        | Instruction::Tex3d { dst, .. }
        | Instruction::SurfLoad { dst, .. }
        | Instruction::Redux { dst, .. }
        | Instruction::ElectSync { dst, .. } => vec![dst],

        Instruction::Ldmatrix { dst_regs, .. } => dst_regs.iter().collect(),

        Instruction::Store { .. }
        | Instruction::CpAsync { .. }
        | Instruction::CpAsyncCommit
        | Instruction::CpAsyncWait { .. }
        | Instruction::Branch { .. }
        | Instruction::Label(_)
        | Instruction::Return
        | Instruction::BarSync { .. }
        | Instruction::BarArrive { .. }
        | Instruction::FenceAcqRel { .. }
        | Instruction::TmaLoad { .. }
        | Instruction::Red { .. }
        | Instruction::SurfStore { .. }
        | Instruction::Stmatrix { .. }
        | Instruction::Setmaxnreg { .. }
        | Instruction::Griddepcontrol { .. }
        | Instruction::FenceProxy { .. }
        | Instruction::MbarrierInit { .. }
        | Instruction::MbarrierArrive { .. }
        | Instruction::MbarrierWait { .. }
        | Instruction::Tcgen05Mma { .. }
        | Instruction::BarrierCluster
        | Instruction::FenceCluster
        | Instruction::CpAsyncBulk { .. }
        | Instruction::Comment(_)
        | Instruction::Raw(_)
        | Instruction::Pragma(_) => vec![],

        Instruction::Wmma { op, fragments, .. } => match op {
            WmmaOp::LoadA | WmmaOp::LoadB | WmmaOp::Mma => fragments.iter().collect(),
            WmmaOp::StoreD => vec![],
        },
        Instruction::Mma { d_regs, .. } | Instruction::Wgmma { d_regs, .. } => {
            d_regs.iter().collect()
        }
    }
}

/// Extract registers used (read from) by an instruction.
#[allow(clippy::too_many_lines)]
fn uses(inst: &Instruction) -> Vec<&Register> {
    match inst {
        Instruction::Add { a, b, .. }
        | Instruction::Sub { a, b, .. }
        | Instruction::Mul { a, b, .. }
        | Instruction::Min { a, b, .. }
        | Instruction::Max { a, b, .. }
        | Instruction::Div { a, b, .. }
        | Instruction::Rem { a, b, .. }
        | Instruction::And { a, b, .. }
        | Instruction::Or { a, b, .. }
        | Instruction::Xor { a, b, .. }
        | Instruction::SetP { a, b, .. }
        | Instruction::Shl {
            src: a, amount: b, ..
        }
        | Instruction::Shr {
            src: a, amount: b, ..
        } => {
            let mut regs = operand_regs(a);
            regs.extend(operand_regs(b));
            regs
        }

        Instruction::Mad { a, b, c, .. }
        | Instruction::MadLo { a, b, c, .. }
        | Instruction::MadHi { a, b, c, .. }
        | Instruction::MadWide { a, b, c, .. }
        | Instruction::Fma { a, b, c, .. }
        | Instruction::Dp4a { a, b, c, .. }
        | Instruction::Dp2a { a, b, c, .. } => {
            let mut regs = operand_regs(a);
            regs.extend(operand_regs(b));
            regs.extend(operand_regs(c));
            regs
        }

        Instruction::Neg { src, .. }
        | Instruction::Abs { src, .. }
        | Instruction::Brev { src, .. }
        | Instruction::Clz { src, .. }
        | Instruction::Popc { src, .. }
        | Instruction::Bfind { src, .. }
        | Instruction::Rcp { src, .. }
        | Instruction::Rsqrt { src, .. }
        | Instruction::Sqrt { src, .. }
        | Instruction::Ex2 { src, .. }
        | Instruction::Lg2 { src, .. }
        | Instruction::Sin { src, .. }
        | Instruction::Cos { src, .. }
        | Instruction::Cvt { src, .. }
        | Instruction::Redux { src, .. } => operand_regs(src),

        Instruction::Bfe {
            src, start, len, ..
        } => {
            let mut regs = operand_regs(src);
            regs.extend(operand_regs(start));
            regs.extend(operand_regs(len));
            regs
        }

        Instruction::Bfi {
            insert,
            base,
            start,
            len,
            ..
        } => {
            let mut regs = operand_regs(insert);
            regs.extend(operand_regs(base));
            regs.extend(operand_regs(start));
            regs.extend(operand_regs(len));
            regs
        }

        Instruction::Load { addr, .. } | Instruction::MbarrierArrive { addr } => operand_regs(addr),

        Instruction::Store { addr, src, .. } => {
            let mut regs = operand_regs(addr);
            regs.push(src);
            regs
        }

        Instruction::CpAsync {
            dst_shared,
            src_global,
            ..
        } => {
            let mut regs = operand_regs(dst_shared);
            regs.extend(operand_regs(src_global));
            regs
        }

        Instruction::CpAsyncCommit
        | Instruction::CpAsyncWait { .. }
        | Instruction::Label(_)
        | Instruction::Return
        | Instruction::BarSync { .. }
        | Instruction::BarArrive { .. }
        | Instruction::FenceAcqRel { .. }
        | Instruction::MovSpecial { .. }
        | Instruction::LoadParam { .. }
        | Instruction::ElectSync { .. }
        | Instruction::Setmaxnreg { .. }
        | Instruction::Griddepcontrol { .. }
        | Instruction::FenceProxy { .. }
        | Instruction::BarrierCluster
        | Instruction::FenceCluster
        | Instruction::Comment(_)
        | Instruction::Raw(_)
        | Instruction::Pragma(_) => vec![],

        Instruction::Branch { predicate, .. } => {
            if let Some((reg, _)) = predicate {
                vec![reg]
            } else {
                vec![]
            }
        }

        Instruction::Wmma {
            op,
            fragments,
            addr,
            stride,
            ..
        } => {
            let mut regs: Vec<&Register> = Vec::new();
            match op {
                WmmaOp::LoadA | WmmaOp::LoadB => {
                    if let Some(a) = addr {
                        regs.extend(operand_regs(a));
                    }
                    if let Some(s) = stride {
                        regs.extend(operand_regs(s));
                    }
                }
                WmmaOp::StoreD => {
                    regs.extend(fragments.iter());
                    if let Some(a) = addr {
                        regs.extend(operand_regs(a));
                    }
                    if let Some(s) = stride {
                        regs.extend(operand_regs(s));
                    }
                }
                WmmaOp::Mma => {
                    regs.extend(fragments.iter());
                }
            }
            regs
        }

        Instruction::Mma {
            a_regs,
            b_regs,
            c_regs,
            ..
        } => {
            let mut regs: Vec<&Register> = Vec::new();
            regs.extend(a_regs.iter());
            regs.extend(b_regs.iter());
            regs.extend(c_regs.iter());
            regs
        }

        Instruction::Wgmma { desc_a, desc_b, .. } => vec![desc_a, desc_b],

        Instruction::TmaLoad {
            dst_shared,
            desc,
            coords,
            barrier,
            ..
        } => {
            let mut regs = operand_regs(dst_shared);
            regs.push(desc);
            regs.extend(coords.iter());
            regs.push(barrier);
            regs
        }

        // Atomic: reads addr and src
        Instruction::Atom { addr, src, .. } | Instruction::Red { addr, src, .. } => {
            let mut regs = operand_regs(addr);
            regs.extend(operand_regs(src));
            regs
        }
        // AtomCas: reads addr, compare, and value
        Instruction::AtomCas {
            addr,
            compare,
            value,
            ..
        } => {
            let mut regs = operand_regs(addr);
            regs.extend(operand_regs(compare));
            regs.extend(operand_regs(value));
            regs
        }

        // Texture: coord registers are used
        Instruction::Tex1d { coord, .. } | Instruction::SurfLoad { coord, .. } => {
            operand_regs(coord)
        }
        Instruction::Tex2d {
            coord_x, coord_y, ..
        } => {
            let mut regs = operand_regs(coord_x);
            regs.extend(operand_regs(coord_y));
            regs
        }
        Instruction::Tex3d {
            coord_x,
            coord_y,
            coord_z,
            ..
        } => {
            let mut regs = operand_regs(coord_x);
            regs.extend(operand_regs(coord_y));
            regs.extend(operand_regs(coord_z));
            regs
        }
        Instruction::SurfStore { coord, src, .. } => {
            let mut regs = operand_regs(coord);
            regs.push(src);
            regs
        }

        // PTX 8.x instructions
        Instruction::Stmatrix { dst_addr, src, .. } => {
            let mut regs = operand_regs(dst_addr);
            regs.push(src);
            regs
        }
        Instruction::MbarrierInit { addr, count, .. } => {
            let mut regs = operand_regs(addr);
            regs.extend(operand_regs(count));
            regs
        }
        Instruction::MbarrierWait { addr, phase } => {
            let mut regs = operand_regs(addr);
            regs.extend(operand_regs(phase));
            regs
        }

        Instruction::Tcgen05Mma { a_desc, b_desc } => vec![a_desc, b_desc],

        Instruction::CpAsyncBulk {
            dst_smem,
            src_gmem,
            desc,
        } => vec![dst_smem, src_gmem, desc],

        Instruction::Ldmatrix { src_addr, .. } => operand_regs(src_addr),
    }
}

/// Extract register references from an operand.
fn operand_regs(op: &Operand) -> Vec<&Register> {
    match op {
        Operand::Register(reg) => vec![reg],
        Operand::Address { base, .. } => vec![base],
        Operand::Immediate(_) | Operand::Symbol(_) => vec![],
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{
        CacheQualifier, FenceScope, ImmValue, Instruction, MemorySpace, MulMode, Operand, PtxType,
        Register, SpecialReg, VectorWidth, WmmaOp,
    };

    fn reg(name: &str, ty: PtxType) -> Register {
        Register {
            name: name.to_string(),
            ty,
        }
    }

    fn reg_op(name: &str, ty: PtxType) -> Operand {
        Operand::Register(reg(name, ty))
    }

    fn imm_u32(val: u32) -> Operand {
        Operand::Immediate(ImmValue::U32(val))
    }

    /// Unused register definition should be removed.
    #[test]
    fn test_unused_register_removed() {
        let instructions = vec![
            Instruction::Add {
                ty: PtxType::F32,
                dst: reg("%f0", PtxType::F32),
                a: imm_u32(1),
                b: imm_u32(2),
            },
            // %f0 is never used
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 1);
        assert!(result.is_empty());
    }

    /// Used register definition should be kept.
    #[test]
    fn test_used_register_kept() {
        let instructions = vec![
            Instruction::MovSpecial {
                dst: reg("%r0", PtxType::U32),
                special: SpecialReg::TidX,
            },
            Instruction::Store {
                space: MemorySpace::Global,
                qualifier: CacheQualifier::None,
                vec: VectorWidth::V1,
                ty: PtxType::U32,
                addr: Operand::Address {
                    base: reg("%rd0", PtxType::U64),
                    offset: None,
                },
                src: reg("%r0", PtxType::U32),
            },
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 2);
    }

    /// Store instructions are never removed (side effect).
    #[test]
    fn test_stores_never_removed() {
        let instructions = vec![Instruction::Store {
            space: MemorySpace::Global,
            qualifier: CacheQualifier::None,
            vec: VectorWidth::V1,
            ty: PtxType::F32,
            addr: Operand::Address {
                base: reg("%rd0", PtxType::U64),
                offset: None,
            },
            src: reg("%f0", PtxType::F32),
        }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// Branches are never removed (control flow side effect).
    #[test]
    fn test_branches_never_removed() {
        let instructions = vec![
            Instruction::Branch {
                target: "loop".to_string(),
                predicate: None,
            },
            Instruction::Label("loop".to_string()),
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 2);
    }

    /// Barrier is never removed (synchronization side effect).
    #[test]
    fn test_barrier_never_removed() {
        let instructions = vec![Instruction::BarSync { id: 0 }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// `BarArrive` is never removed.
    #[test]
    fn test_bar_arrive_never_removed() {
        let instructions = vec![Instruction::BarArrive { id: 0, count: 32 }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// `FenceAcqRel` is never removed.
    #[test]
    fn test_fence_never_removed() {
        let instructions = vec![Instruction::FenceAcqRel {
            scope: FenceScope::Gpu,
        }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// Return is never removed.
    #[test]
    fn test_return_never_removed() {
        let instructions = vec![Instruction::Return];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// Comment is never removed.
    #[test]
    fn test_comment_never_removed() {
        let instructions = vec![Instruction::Comment("keep me".to_string())];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// Raw PTX is never removed.
    #[test]
    fn test_raw_never_removed() {
        let instructions = vec![Instruction::Raw("nop;".to_string())];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// Chain of dead instructions: A defines X, B uses X to define Y, Y unused.
    /// Both should be eliminated (fixed-point iteration).
    #[test]
    fn test_chain_of_dead_instructions() {
        let instructions = vec![
            // %f0 = 1 + 2 (dead because only used by next instruction)
            Instruction::Add {
                ty: PtxType::F32,
                dst: reg("%f0", PtxType::F32),
                a: imm_u32(1),
                b: imm_u32(2),
            },
            // %f1 = %f0 + 3 (dead because %f1 is never used)
            Instruction::Add {
                ty: PtxType::F32,
                dst: reg("%f1", PtxType::F32),
                a: reg_op("%f0", PtxType::F32),
                b: imm_u32(3),
            },
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        // First pass: %f1 unused → remove second instruction → eliminated=1
        // Second pass: %f0 now unused → remove first instruction → eliminated=1
        // Total: 2
        assert_eq!(eliminated, 2);
        assert!(result.is_empty());
    }

    /// Fixed-point: three-level chain of dead code.
    #[test]
    fn test_three_level_dead_chain() {
        let instructions = vec![
            Instruction::Add {
                ty: PtxType::U32,
                dst: reg("%r0", PtxType::U32),
                a: imm_u32(1),
                b: imm_u32(2),
            },
            Instruction::Mul {
                ty: PtxType::U32,
                mode: MulMode::Lo,
                dst: reg("%r1", PtxType::U32),
                a: reg_op("%r0", PtxType::U32),
                b: imm_u32(3),
            },
            Instruction::Sub {
                ty: PtxType::U32,
                dst: reg("%r2", PtxType::U32),
                a: reg_op("%r1", PtxType::U32),
                b: imm_u32(4),
            },
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 3);
        assert!(result.is_empty());
    }

    /// Function with no dead code returns identical instructions.
    #[test]
    fn test_no_dead_code_unchanged() {
        let instructions = vec![
            Instruction::MovSpecial {
                dst: reg("%r0", PtxType::U32),
                special: SpecialReg::TidX,
            },
            Instruction::Add {
                ty: PtxType::U32,
                dst: reg("%r1", PtxType::U32),
                a: reg_op("%r0", PtxType::U32),
                b: imm_u32(1),
            },
            Instruction::Store {
                space: MemorySpace::Global,
                qualifier: CacheQualifier::None,
                vec: VectorWidth::V1,
                ty: PtxType::U32,
                addr: Operand::Address {
                    base: reg("%rd0", PtxType::U64),
                    offset: None,
                },
                src: reg("%r1", PtxType::U32),
            },
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 3);
    }

    /// `CpAsync` is never removed (DMA side effect).
    #[test]
    fn test_cp_async_never_removed() {
        let instructions = vec![
            Instruction::CpAsync {
                bytes: 16,
                dst_shared: Operand::Address {
                    base: reg("%rd0", PtxType::U64),
                    offset: None,
                },
                src_global: Operand::Address {
                    base: reg("%rd1", PtxType::U64),
                    offset: None,
                },
            },
            Instruction::CpAsyncCommit,
            Instruction::CpAsyncWait { n: 0 },
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 3);
    }

    /// `TmaLoad` is never removed.
    #[test]
    fn test_tma_load_never_removed() {
        let instructions = vec![Instruction::TmaLoad {
            dst_shared: Operand::Address {
                base: reg("%rd0", PtxType::U64),
                offset: None,
            },
            desc: reg("%rd1", PtxType::U64),
            coords: vec![reg("%r0", PtxType::U32)],
            barrier: reg("%rd2", PtxType::U64),
        }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// Mixed live and dead instructions.
    #[test]
    fn test_mixed_live_and_dead() {
        let instructions = vec![
            // Live chain: tid → add → store
            Instruction::MovSpecial {
                dst: reg("%r0", PtxType::U32),
                special: SpecialReg::TidX,
            },
            Instruction::Add {
                ty: PtxType::U32,
                dst: reg("%r1", PtxType::U32),
                a: reg_op("%r0", PtxType::U32),
                b: imm_u32(1),
            },
            // Dead: %r2 is never used
            Instruction::Mul {
                ty: PtxType::U32,
                mode: MulMode::Lo,
                dst: reg("%r2", PtxType::U32),
                a: reg_op("%r0", PtxType::U32),
                b: imm_u32(2),
            },
            Instruction::Store {
                space: MemorySpace::Global,
                qualifier: CacheQualifier::None,
                vec: VectorWidth::V1,
                ty: PtxType::U32,
                addr: Operand::Address {
                    base: reg("%rd0", PtxType::U64),
                    offset: None,
                },
                src: reg("%r1", PtxType::U32),
            },
        ];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 1);
        assert_eq!(result.len(), 3);
    }

    /// Empty input returns empty output with zero eliminated.
    #[test]
    fn test_empty_instructions() {
        let (result, eliminated) = eliminate_dead_code(&[]);
        assert_eq!(eliminated, 0);
        assert!(result.is_empty());
    }

    /// Load instruction without subsequent use is dead (loads have no side
    /// effect on other memory; they only write to the destination register).
    #[test]
    fn test_dead_load_removed() {
        let instructions = vec![Instruction::Load {
            space: MemorySpace::Global,
            qualifier: CacheQualifier::None,
            vec: VectorWidth::V1,
            ty: PtxType::F32,
            dst: reg("%f0", PtxType::F32),
            addr: Operand::Address {
                base: reg("%rd0", PtxType::U64),
                offset: None,
            },
        }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 1);
        assert!(result.is_empty());
    }

    /// `Wmma` `StoreD` is a side effect and must not be removed.
    #[test]
    fn test_wmma_store_never_removed() {
        use crate::ir::{WmmaLayout, WmmaShape};

        let instructions = vec![Instruction::Wmma {
            op: WmmaOp::StoreD,
            shape: WmmaShape::M16N16K16,
            layout: WmmaLayout::RowMajor,
            ty: PtxType::F16,
            fragments: vec![reg("%f0", PtxType::F16), reg("%f1", PtxType::F16)],
            addr: Some(Operand::Address {
                base: reg("%rd0", PtxType::U64),
                offset: None,
            }),
            stride: None,
        }];
        let (result, eliminated) = eliminate_dead_code(&instructions);
        assert_eq!(eliminated, 0);
        assert_eq!(result.len(), 1);
    }

    /// `has_side_effects` correctly classifies all instruction categories.
    #[test]
    fn test_side_effects_classification() {
        // Pure computation → no side effects
        let add = Instruction::Add {
            ty: PtxType::F32,
            dst: reg("%f0", PtxType::F32),
            a: imm_u32(0),
            b: imm_u32(0),
        };
        assert!(!has_side_effects(&add));

        // Store → side effect
        let store = Instruction::Store {
            space: MemorySpace::Global,
            qualifier: CacheQualifier::None,
            vec: VectorWidth::V1,
            ty: PtxType::F32,
            addr: Operand::Address {
                base: reg("%rd0", PtxType::U64),
                offset: None,
            },
            src: reg("%f0", PtxType::F32),
        };
        assert!(has_side_effects(&store));

        // Branch → side effect
        let branch = Instruction::Branch {
            target: "L1".to_string(),
            predicate: None,
        };
        assert!(has_side_effects(&branch));

        // Label → side effect
        let label = Instruction::Label("L1".to_string());
        assert!(has_side_effects(&label));

        // BarSync → side effect
        let bar = Instruction::BarSync { id: 0 };
        assert!(has_side_effects(&bar));

        // MovSpecial → no side effect (can be DCE'd if unused)
        let mov = Instruction::MovSpecial {
            dst: reg("%r0", PtxType::U32),
            special: SpecialReg::TidX,
        };
        assert!(!has_side_effects(&mov));
    }

    // -------------------------------------------------------------------------
    // Additional DCE quality-gate tests
    // -------------------------------------------------------------------------

    /// An unreachable chain of pure computations after a branch is dead:
    /// the defined registers are never consumed, so DCE eliminates them.
    /// Note: the Branch and Label are kept (side effects); the pure
    /// computations whose results feed nowhere are eliminated.
    #[test]
    fn test_dce_removes_unreachable_block() {
        let instructions = vec![
            // Unconditional branch — side effect, kept
            Instruction::Branch {
                target: "after_dead".to_string(),
                predicate: None,
            },
            // The following pure computations are never consumed (the branch
            // causes them to be skipped at runtime, and their outputs are
            // never used anywhere in this list).
            Instruction::Add {
                ty: PtxType::F32,
                dst: reg("%f_dead0", PtxType::F32),
                a: imm_u32(1),
                b: imm_u32(2),
            },
            Instruction::Mul {
                ty: PtxType::F32,
                mode: MulMode::Lo,
                dst: reg("%f_dead1", PtxType::F32),
                a: reg_op("%f_dead0", PtxType::F32),
                b: imm_u32(3),
            },
            // Label — side effect, kept
            Instruction::Label("after_dead".to_string()),
            Instruction::Return,
        ];

        let (result, eliminated) = eliminate_dead_code(&instructions);

        // Branch, Label, Return are always kept (side effects = 3)
        // Add and Mul are dead (their outputs go nowhere) = 2 eliminated
        // Fixed-point: pass1 eliminates Mul (uses %f_dead0 which is defined but
        // then %f_dead0 has no other consumer after Mul is gone), pass2 eliminates Add
        assert_eq!(
            eliminated, 2,
            "DCE must eliminate both unreachable pure-computation instructions"
        );
        // Branch, Label, Return survive
        assert_eq!(
            result.len(),
            3,
            "Branch, Label and Return must be preserved"
        );
    }

    /// Reachable blocks (pure computations whose results feed a store) must NOT
    /// be removed by DCE.
    #[test]
    fn test_dce_keeps_reachable_blocks() {
        let instructions = vec![
            Instruction::MovSpecial {
                dst: reg("%r0", PtxType::U32),
                special: SpecialReg::TidX,
            },
            Instruction::Add {
                ty: PtxType::U32,
                dst: reg("%r1", PtxType::U32),
                a: reg_op("%r0", PtxType::U32),
                b: imm_u32(10),
            },
            Instruction::Mul {
                ty: PtxType::U32,
                mode: MulMode::Lo,
                dst: reg("%r2", PtxType::U32),
                a: reg_op("%r1", PtxType::U32),
                b: imm_u32(4),
            },
            // Store consumes %r2 — the whole chain is live
            Instruction::Store {
                space: MemorySpace::Global,
                qualifier: CacheQualifier::None,
                vec: VectorWidth::V1,
                ty: PtxType::U32,
                addr: Operand::Address {
                    base: reg("%rd0", PtxType::U64),
                    offset: None,
                },
                src: reg("%r2", PtxType::U32),
            },
        ];

        let (result, eliminated) = eliminate_dead_code(&instructions);

        assert_eq!(
            eliminated, 0,
            "no instruction should be eliminated from a fully-live chain"
        );
        assert_eq!(
            result.len(),
            instructions.len(),
            "all instructions must survive DCE"
        );
    }

    /// DCE must be idempotent: running the pass twice on the same input must
    /// produce the same result as running it once.
    #[test]
    fn test_dce_idempotent() {
        let instructions = vec![
            // Live chain
            Instruction::MovSpecial {
                dst: reg("%r0", PtxType::U32),
                special: SpecialReg::TidX,
            },
            // Dead computation
            Instruction::Add {
                ty: PtxType::F32,
                dst: reg("%f_unused", PtxType::F32),
                a: imm_u32(7),
                b: imm_u32(8),
            },
            Instruction::Store {
                space: MemorySpace::Global,
                qualifier: CacheQualifier::None,
                vec: VectorWidth::V1,
                ty: PtxType::U32,
                addr: Operand::Address {
                    base: reg("%rd0", PtxType::U64),
                    offset: None,
                },
                src: reg("%r0", PtxType::U32),
            },
        ];

        let (first_result, first_eliminated) = eliminate_dead_code(&instructions);
        // DCE already runs to fixed-point internally, so a second call changes nothing
        let (second_result, second_eliminated) = eliminate_dead_code(&first_result);

        assert_eq!(
            second_eliminated, 0,
            "second DCE pass must not eliminate anything additional (idempotent)"
        );
        assert_eq!(
            first_result.len(),
            second_result.len(),
            "result length must be the same on both passes"
        );
        assert_eq!(
            first_eliminated, 1,
            "first pass must eliminate the unused Add instruction"
        );
    }
}