trueno-explain 0.2.2

PTX/SIMD/wgpu visualization and tracing CLI for Trueno
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
use super::types::*;
use super::analyzer::*;
use super::coverage::*;

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

    #[test]
    fn test_shared_mem_u64_detection() {
        let ptx = r#"
.visible .entry test() {
    .reg .u64 %rd<5>;
    .reg .f32 %f<2>;
    .shared .b8 smem[4096];
    st.shared.f32 [%rd0], %f0;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::SharedMemU64Addressing));
    }

    #[test]
    fn test_shared_mem_u32_valid() {
        let ptx = r#"
.visible .entry test() {
    .reg .u32 %r<5>;
    .reg .f32 %f<2>;
    .shared .b8 smem[4096];
    st.shared.f32 [%r0], %f0;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::SharedMemU64Addressing));
    }

    #[test]
    fn test_missing_barrier_sync_strict() {
        let ptx = r#"
.visible .entry test() {
    .shared .b8 smem[1024];
    st.shared.f32 [%r0], %f0;
    ld.shared.f32 %f1, [%r1];
    ret;
}
"#;
        // Non-strict mode: no warning
        let normal_result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!normal_result.has_bug(&PtxBugClass::MissingBarrierSync));

        // Strict mode: warning
        let strict_result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(strict_result.has_bug(&PtxBugClass::MissingBarrierSync));
    }

    #[test]
    fn test_barrier_present_valid() {
        let ptx = r#"
.visible .entry test() {
    .shared .b8 smem[1024];
    st.shared.f32 [%r0], %f0;
    bar.sync 0;
    ld.shared.f32 %f1, [%r1];
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        // Should not have the broad "no bar.sync" warning
        let missing_barrier_bugs: Vec<_> = result.bugs_of_class(&PtxBugClass::MissingBarrierSync);
        // The specific st/ld pattern should not trigger since bar.sync is present
        assert!(missing_barrier_bugs
            .iter()
            .all(|b| !b.message.contains("ld.shared follows st.shared")));
    }

    #[test]
    fn test_loop_branch_to_end_detection() {
        let ptx = r#"
.visible .entry test() {
main_loop:
    // loop body
    bra main_loop_end;
main_loop_end:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::LoopBranchToEnd));
    }

    #[test]
    fn test_conditional_branch_not_flagged() {
        let ptx = r#"
.visible .entry test() {
loop_start:
    @%p0 bra loop_end;
    bra loop_start;
loop_end:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        // Conditional branch should NOT be flagged
        assert!(!result.has_bug(&PtxBugClass::LoopBranchToEnd));
    }

    #[test]
    fn test_register_spills_detection() {
        let ptx = r#"
.visible .entry test() {
    .local .align 4 .b8 __local_depot[32];
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::RegisterSpills));
    }

    #[test]
    fn test_missing_entry_point_detection() {
        let ptx = r#"
.version 8.0
.target sm_70
.reg .f32 %f<4>;
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::MissingEntryPoint));
    }

    #[test]
    fn test_valid_kernel_no_bugs() {
        let ptx = r#"
.version 8.0
.target sm_70
.visible .entry valid_kernel() {
    .reg .f32 %f<4>;
    .reg .u32 %r<4>;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.is_valid());
        assert!(!result.has_bugs());
    }

    #[test]
    fn test_bug_severity_classification() {
        assert_eq!(
            PtxBugClass::MissingBarrierSync.severity(),
            BugSeverity::Critical
        );
        assert_eq!(
            PtxBugClass::SharedMemU64Addressing.severity(),
            BugSeverity::Critical
        );
        assert_eq!(PtxBugClass::RegisterSpills.severity(), BugSeverity::High);
        assert_eq!(
            PtxBugClass::MissingEntryPoint.severity(),
            BugSeverity::FalsePositive
        );
    }

    #[test]
    fn test_bug_report_format() {
        let ptx = r#"
.visible .entry test() {
    .shared .b8 smem[1024];
    st.shared.f32 [%rd0], %f0;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        let report = result.format_report();

        assert!(report.contains("PTX BUG HUNTING REPORT"));
        assert!(report.contains("P0 CRITICAL BUGS:"));
        assert!(report.contains("SUMMARY"));
    }

    #[test]
    fn test_kernel_name_extraction() {
        let ptx = r#"
.visible .entry gemm_tiled() {
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert_eq!(result.kernel_name, Some("gemm_tiled".to_string()));
    }

    #[test]
    fn test_count_by_severity() {
        let report = PtxBugReport {
            kernel_name: Some("test".to_string()),
            bugs: vec![
                PtxBug {
                    class: PtxBugClass::MissingBarrierSync,
                    line: 1,
                    instruction: "test".to_string(),
                    message: "test".to_string(),
                    fix: None,
                },
                PtxBug {
                    class: PtxBugClass::RegisterSpills,
                    line: 2,
                    instruction: "test".to_string(),
                    message: "test".to_string(),
                    fix: None,
                },
            ],
            lines_analyzed: 10,
            strict_mode: true,
        };

        assert_eq!(report.count_by_severity(BugSeverity::Critical), 1);
        assert_eq!(report.count_by_severity(BugSeverity::High), 1);
        assert_eq!(report.count_by_severity(BugSeverity::Medium), 0);
    }

    /// F101: Detect `st.shared [%rd0]`
    #[test]
    fn f101_detect_shared_u64_addressing() {
        let ptx = "st.shared.f32 [%rd5], %f0;";
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::SharedMemU64Addressing));
    }

    /// F102: Detect missing `bar.sync`
    #[test]
    fn f102_detect_missing_barrier() {
        let ptx = r#"
.visible .entry test() {
    .shared .b8 smem[1024];
    st.shared.f32 [%r0], %f0;
    ld.shared.f32 %f1, [%r1];
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::MissingBarrierSync));
    }

    /// F103: Detect `bra loop_end` in loop
    #[test]
    fn f103_detect_loop_branch_end() {
        let ptx = r#"
.entry test() {
test_loop:
    bra test_loop_end;
test_loop_end:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::LoopBranchToEnd));
    }

    /// F104: Valid PTX passes
    #[test]
    fn f104_valid_ptx_passes() {
        let ptx = r#"
.version 8.0
.target sm_70
.visible .entry valid() {
    .reg .f32 %f<4>;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.is_valid());
    }

    /// F106: Missing `.entry` detected
    #[test]
    fn f106_missing_entry_detected() {
        let ptx = ".version 8.0
.target sm_70
.reg .f32 %f<4>;";
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::MissingEntryPoint));
    }

    /// Test RedundantMoves detection - consecutive mov chain
    #[test]
    fn test_redundant_moves_chain() {
        let ptx = r#"
.visible .entry test() {
    mov.u32 %r1, %r0;
    mov.u32 %r2, %r1;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::RedundantMoves));
    }

    /// Test RedundantMoves - no chain (valid)
    #[test]
    fn test_redundant_moves_no_chain() {
        let ptx = r#"
.visible .entry test() {
    mov.u32 %r1, %r0;
    add.u32 %r2, %r1, 1;
    mov.u32 %r3, %r2;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::RedundantMoves));
    }

    /// Test UnoptimizedMemoryPattern - multiple single loads
    #[test]
    fn test_unoptimized_memory_single_loads() {
        let ptx = r#"
.visible .entry test() {
    ld.global.f32 %f0, [%rd0];
    ld.global.f32 %f1, [%rd1];
    ld.global.f32 %f2, [%rd2];
    ld.global.f32 %f3, [%rd3];
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::UnoptimizedMemoryPattern));
    }

    /// Test UnoptimizedMemoryPattern - vector loads (valid)
    #[test]
    fn test_unoptimized_memory_vector_loads() {
        let ptx = r#"
.visible .entry test() {
    ld.global.v4.f32 {%f0, %f1, %f2, %f3}, [%rd0];
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::UnoptimizedMemoryPattern));
    }

    /// Test UnoptimizedMemoryPattern - few single loads (acceptable)
    #[test]
    fn test_unoptimized_memory_few_loads() {
        let ptx = r#"
.visible .entry test() {
    ld.global.f32 %f0, [%rd0];
    ld.global.f32 %f1, [%rd1];
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        // Only 2 single loads - below threshold of 4, should not flag
        assert!(!result.has_bug(&PtxBugClass::UnoptimizedMemoryPattern));
    }

    /// Test suspicious stride detection in strict mode
    #[test]
    fn test_unoptimized_memory_suspicious_stride() {
        let ptx = r#"
.visible .entry test() {
    mul.wide.u32 %rd0, %r0, 17;
    ld.global.f32 %f0, [%rd0];
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::UnoptimizedMemoryPattern));
    }

    /// Test normal strides are not flagged
    #[test]
    fn test_unoptimized_memory_normal_stride() {
        let ptx = r#"
.visible .entry test() {
    mul.wide.u32 %rd0, %r0, 4;
    ld.global.f32 %f0, [%rd0];
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        // Stride 4 is normal for f32
        assert!(!result.has_bug(&PtxBugClass::UnoptimizedMemoryPattern));
    }

    /// Test high register pressure detection
    #[test]
    fn test_high_register_pressure() {
        let ptx = r#"
.visible .entry test() {
    .reg .b32 %r<64>;
    .reg .b64 %rd<16>;
    .reg .f32 %f<32>;
    .reg .pred %p<4>;
    ret;
}
"#;
        // 64 + 16 + 32 + 4 = 116 registers > 64 threshold
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::HighRegisterPressure));
    }

    /// Test acceptable register pressure (no bug)
    #[test]
    fn test_normal_register_pressure() {
        let ptx = r#"
.visible .entry test() {
    .reg .b32 %r<16>;
    .reg .b64 %rd<8>;
    .reg .f32 %f<8>;
    .reg .pred %p<4>;
    ret;
}
"#;
        // 16 + 8 + 8 + 4 = 36 registers < 64 threshold
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::HighRegisterPressure));
    }

    /// Test predicate overflow detection
    #[test]
    fn test_predicate_overflow() {
        let ptx = r#"
.visible .entry test() {
    .reg .pred %p<12>;
    .reg .b32 %r<4>;
    ret;
}
"#;
        // 12 predicates > 8 limit
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::PredicateOverflow));
    }

    /// Test acceptable predicate count (no bug)
    #[test]
    fn test_normal_predicate_count() {
        let ptx = r#"
.visible .entry test() {
    .reg .pred %p<8>;
    .reg .b32 %r<4>;
    ret;
}
"#;
        // 8 predicates = limit, should not flag
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::PredicateOverflow));
    }

    /// Test placeholder code detection - "omitted"
    #[test]
    fn test_placeholder_code_omitted() {
        let ptx = r#"
.visible .entry test() {
    // ... loading logic omitted for brevity
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::PlaceholderCode));
    }

    /// Test placeholder code detection - "simplified"
    #[test]
    fn test_placeholder_code_simplified() {
        let ptx = r#"
.visible .entry test() {
    // Simplified: only first element
    st.global.f32 [%rd0], %f0;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::PlaceholderCode));
    }

    /// Test placeholder code detection - "placeholder"
    #[test]
    fn test_placeholder_code_explicit() {
        let ptx = r#"
.visible .entry test() {
    // This is placeholder code for now
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::PlaceholderCode));
    }

    /// Test no placeholder code (clean kernel)
    #[test]
    fn test_no_placeholder_code() {
        let ptx = r#"
.visible .entry test() {
    // Load input
    ld.global.f32 %f0, [%rd0];
    // Compute result
    mul.f32 %f1, %f0, %f0;
    // Store output
    st.global.f32 [%rd1], %f1;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::PlaceholderCode));
    }

    /// Test new bug class severities
    #[test]
    fn test_new_bug_severities() {
        assert_eq!(
            PtxBugClass::HighRegisterPressure.severity(),
            BugSeverity::High
        );
        assert_eq!(PtxBugClass::PredicateOverflow.severity(), BugSeverity::High);
        assert_eq!(PtxBugClass::PlaceholderCode.severity(), BugSeverity::High);
    }

    /// Test new bug class codes
    #[test]
    fn test_new_bug_codes() {
        assert_eq!(
            PtxBugClass::HighRegisterPressure.code(),
            "HIGH_REG_PRESSURE"
        );
        assert_eq!(PtxBugClass::PredicateOverflow.code(), "PRED_OVERFLOW");
        assert_eq!(PtxBugClass::PlaceholderCode.code(), "PLACEHOLDER_CODE");
    }

    // ========================================================================
    // WHITELIST TESTS
    // ========================================================================

    /// Test whitelist suppresses matching bug
    #[test]
    fn test_whitelist_suppresses_bug() {
        let ptx = r#"
.visible .entry q4k_gemm_ggml() {
    .reg .b32 %r<64>;
    .reg .b64 %rd<16>;
    .reg .f32 %f<32>;
    ret;
}
"#;
        // Without whitelist: should flag high register pressure
        let result_no_whitelist = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result_no_whitelist.has_bug(&PtxBugClass::HighRegisterPressure));

        // With quantized whitelist: q4k* should be suppressed
        let result_with_whitelist = PtxBugAnalyzer::with_quantized_whitelist().analyze(ptx);
        assert!(!result_with_whitelist.has_bug(&PtxBugClass::HighRegisterPressure));
    }

    /// Test whitelist with exact kernel name match
    #[test]
    fn test_whitelist_exact_match() {
        let ptx = r#"
.visible .entry special_kernel() {
    .reg .b32 %r<64>;
    .reg .b64 %rd<16>;
    .reg .f32 %f<32>;
    ret;
}
"#;
        // With exact match whitelist
        let analyzer = PtxBugAnalyzer::new().with_whitelist(
            "special_kernel",
            PtxBugClass::HighRegisterPressure,
            "Expected high regs",
        );
        let result = analyzer.analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::HighRegisterPressure));
    }

    /// Test whitelist doesn't suppress non-matching kernels
    #[test]
    fn test_whitelist_no_match() {
        let ptx = r#"
.visible .entry other_kernel() {
    .reg .b32 %r<64>;
    .reg .b64 %rd<16>;
    .reg .f32 %f<32>;
    ret;
}
"#;
        // q4k* whitelist should not match "other_kernel"
        let result = PtxBugAnalyzer::with_quantized_whitelist().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::HighRegisterPressure));
    }

    /// Test performance whitelist covers Tensor Core kernels
    #[test]
    fn test_performance_whitelist_tensor_core() {
        let ptx = r#"
.visible .entry gemm_tensor_core() {
    .reg .b32 %r<64>;
    .reg .b64 %rd<32>;
    .reg .f32 %f<64>;
    .reg .pred %p<12>;
    ret;
}
"#;
        // Without whitelist: should flag both issues
        let result_no_whitelist = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result_no_whitelist.has_bug(&PtxBugClass::HighRegisterPressure));
        assert!(result_no_whitelist.has_bug(&PtxBugClass::PredicateOverflow));

        // With performance whitelist: both should be suppressed
        let result_with_whitelist = PtxBugAnalyzer::with_performance_whitelist().analyze(ptx);
        assert!(!result_with_whitelist.has_bug(&PtxBugClass::HighRegisterPressure));
        assert!(!result_with_whitelist.has_bug(&PtxBugClass::PredicateOverflow));
    }

    /// Test performance whitelist covers attention kernels
    #[test]
    fn test_performance_whitelist_attention() {
        let ptx = r#"
.visible .entry flash_attention_tensor_core() {
    .reg .b32 %r<64>;
    .reg .b64 %rd<32>;
    .reg .f32 %f<48>;
    ret;
}
"#;
        // With performance whitelist: register pressure should be suppressed
        let result = PtxBugAnalyzer::with_performance_whitelist().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::HighRegisterPressure));
    }

    // ========================================================================
    // EMPTY LOOP BODY TESTS
    // ========================================================================

    /// Test empty loop body detection
    #[test]
    fn test_empty_loop_body_detected() {
        let ptx = r#"
.visible .entry test() {
empty_loop:
    // Just comments here
    bra empty_loop;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::EmptyLoopBody));
    }

    /// Test valid loop body not flagged
    #[test]
    fn test_valid_loop_body_not_flagged() {
        let ptx = r#"
.visible .entry test() {
    .reg .f32 %f<4>;
    .reg .u32 %r<4>;
compute_loop:
    add.f32 %f0, %f0, %f1;
    add.u32 %r0, %r0, 1;
    setp.lt.u32 %p0, %r0, %r1;
    @%p0 bra compute_loop;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::EmptyLoopBody));
    }

    /// Test loop with only conditional branch not flagged
    #[test]
    fn test_loop_with_exit_condition_not_flagged() {
        let ptx = r#"
.visible .entry test() {
    .reg .u32 %r<4>;
    .reg .pred %p<2>;
check_loop:
    setp.lt.u32 %p0, %r0, %r1;
    @%p0 bra check_loop;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        // Has setp which is computation
        assert!(!result.has_bug(&PtxBugClass::EmptyLoopBody));
    }

    // ========================================================================
    // MISSING BOUNDS CHECK TESTS
    // ========================================================================

    /// Test missing bounds check detection
    #[test]
    fn test_missing_bounds_check() {
        let ptx = r#"
.visible .entry test() {
    .reg .u64 %rd<4>;
    .reg .f32 %f<4>;
    mov.u32 %r0, %tid.x;
    ld.global.f32 %f0, [%rd0];
    st.global.f32 [%rd1], %f0;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::MissingBoundsCheck));
    }

    /// Test proper bounds check not flagged
    #[test]
    fn test_proper_bounds_check_not_flagged() {
        let ptx = r#"
.visible .entry test() {
    .reg .u64 %rd<4>;
    .reg .f32 %f<4>;
    .reg .u32 %r<4>;
    .reg .pred %p<2>;
    mov.u32 %r0, %tid.x;
    setp.lt.u32 %p0, %r0, %r1;
    @%p0 bra do_work;
    bra done;
do_work:
    ld.global.f32 %f0, [%rd0];
    st.global.f32 [%rd1], %f0;
done:
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::MissingBoundsCheck));
    }

    /// Test kernel without global memory not flagged
    #[test]
    fn test_no_global_mem_no_bounds_check_needed() {
        let ptx = r#"
.visible .entry test() {
    .reg .u32 %r<4>;
    mov.u32 %r0, %tid.x;
    add.u32 %r1, %r0, 1;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        // No global memory, so no bounds check needed
        assert!(!result.has_bug(&PtxBugClass::MissingBoundsCheck));
    }

    // ========================================================================
    // DEAD CODE TESTS
    // ========================================================================

    /// Test dead code after ret
    #[test]
    fn test_dead_code_after_ret() {
        let ptx = r#"
.visible .entry test() {
    .reg .f32 %f<4>;
    add.f32 %f0, %f1, %f2;
    ret;
    mul.f32 %f3, %f0, %f1;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::DeadCode));
    }

    /// Test dead code after unconditional branch
    #[test]
    fn test_dead_code_after_branch() {
        let ptx = r#"
.visible .entry test() {
    .reg .f32 %f<4>;
    bra skip;
    add.f32 %f0, %f1, %f2;
skip:
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::DeadCode));
    }

    /// Test reachable code not flagged (label after branch)
    #[test]
    fn test_reachable_code_not_flagged() {
        let ptx = r#"
.visible .entry test() {
    .reg .f32 %f<4>;
    .reg .pred %p<2>;
    @%p0 bra skip;
    add.f32 %f0, %f1, %f2;
skip:
    mul.f32 %f3, %f0, %f1;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        // Conditional branch, code after is reachable
        assert!(!result.has_bug(&PtxBugClass::DeadCode));
    }

    /// Test code after label is reachable
    #[test]
    fn test_code_after_label_reachable() {
        let ptx = r#"
.visible .entry test() {
    .reg .f32 %f<4>;
    bra middle;
middle:
    add.f32 %f0, %f1, %f2;
    ret;
}
"#;
        let result = PtxBugAnalyzer::new().analyze(ptx);
        // The add after middle: label is reachable via the branch
        assert!(!result.has_bug(&PtxBugClass::DeadCode));
    }

    // ========================================================================
    // NEW BUG CLASS SEVERITY/CODE TESTS
    // ========================================================================

    /// Test new extended bug class severities
    #[test]
    fn test_extended_bug_severities() {
        assert_eq!(PtxBugClass::EmptyLoopBody.severity(), BugSeverity::High);
        assert_eq!(
            PtxBugClass::MissingBoundsCheck.severity(),
            BugSeverity::High
        );
        assert_eq!(PtxBugClass::DeadCode.severity(), BugSeverity::Medium);
    }

    /// Test new extended bug class codes
    #[test]
    fn test_extended_bug_codes() {
        assert_eq!(PtxBugClass::EmptyLoopBody.code(), "EMPTY_LOOP");
        assert_eq!(PtxBugClass::MissingBoundsCheck.code(), "NO_BOUNDS_CHECK");
        assert_eq!(PtxBugClass::DeadCode.code(), "DEAD_CODE");
    }

    // ========================================================================
    // PARITY-114: EARLY EXIT BEFORE BARRIER TESTS
    // ========================================================================

    /// PARITY-114: Detect conditional early exit before barrier
    #[test]
    fn test_parity114_conditional_exit_before_barrier() {
        let ptx = r#"
.visible .entry kernel() {
    mov.u32 %r0, %tid.x;
    setp.lt.u32 %p0, %r0, 32;

loop_start:
    @!%p0 bra exit;
    ld.shared.f32 %f0, [%r0];
    bar.sync 0;
    st.shared.f32 [%r0], %f0;
    bra loop_start;

loop_start_end:
done:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));
        // Verify it's P0 Critical
        assert_eq!(
            PtxBugClass::EarlyExitBeforeBarrier.severity(),
            BugSeverity::Critical
        );
    }

    /// PARITY-114: Detect unconditional early exit before barrier
    #[test]
    fn test_parity114_unconditional_exit_before_barrier() {
        let ptx = r#"
.visible .entry kernel() {
loop_start:
    bra exit;
    bar.sync 0;
    bra loop_start;

loop_start_end:
done:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));
    }

    /// PARITY-114: Safe kernel with barrier before any possible exit
    #[test]
    fn test_parity114_safe_barrier_first() {
        let ptx = r#"
.visible .entry kernel() {
    mov.u32 %r0, %tid.x;
    setp.lt.u32 %p0, %r0, 32;

loop_start:
    ld.shared.f32 %f0, [%r0];
    bar.sync 0;
    st.shared.f32 [%r0], %f0;
    bra loop_start;

loop_start_end:
    @!%p0 bra exit;
    st.global.f32 [%r1], %f0;
exit:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));
    }

    /// PARITY-114: Exit after loop end is safe
    #[test]
    fn test_parity114_exit_after_loop_is_safe() {
        let ptx = r#"
.visible .entry kernel() {
k_tile_loop:
    bar.sync 0;
    ld.shared.f32 %f0, [%r0];
    bra k_tile_loop;

k_tile_end:
    @!%p0 bra exit;
    st.global.f32 [%r1], %f0;
done:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));
    }

    /// PARITY-114: Non-strict mode does not flag barrier issues
    #[test]
    fn test_parity114_non_strict_mode() {
        let ptx = r#"
.visible .entry kernel() {
loop_start:
    @!%p0 bra exit;
    bar.sync 0;
    bra loop_start;

loop_start_end:
done:
    ret;
}
"#;
        // Non-strict mode should NOT flag this
        let result = PtxBugAnalyzer::new().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));

        // Strict mode SHOULD flag this
        let strict_result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(strict_result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));
    }

    /// PARITY-114: Bug class properties
    #[test]
    fn test_parity114_bug_class_properties() {
        assert_eq!(
            PtxBugClass::EarlyExitBeforeBarrier.code(),
            "EARLY_EXIT_BARRIER"
        );
        assert_eq!(
            PtxBugClass::EarlyExitBeforeBarrier.severity(),
            BugSeverity::Critical
        );
    }

    /// PARITY-114: kv_loop pattern (attention kernels) - safe after fix
    #[test]
    fn test_parity114_attention_kv_loop_safe() {
        let ptx = r#"
.visible .entry flash_attention() {
kv_loop:
    bar.sync 0;
    wmma.mma.sync.aligned.row.col.m16n16k16.f32.f16.f16.f32;
    bra kv_loop;

kv_loop_end:
    @!%p_valid bra exit;
    st.global.f32 [%out], %f0;
done:
    ret;
}
"#;
        let result = PtxBugAnalyzer::strict().analyze(ptx);
        assert!(!result.has_bug(&PtxBugClass::EarlyExitBeforeBarrier));
    }
}