cutile 0.0.1

cuTile Rust lets programmers safely author and execute tile kernels directly in Rust.
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
/*
 * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */
use cutile::{self, api::*, tensor::*, tile_kernel::*};
use cutile_compiler::compiler::utils::CompileOptions;
use cutile_compiler::compiler::{CUDATileFunctionCompiler, CUDATileModules};
use cutile_compiler::cuda_tile_runtime_utils::get_gpu_name;

mod common;

#[cutile::module]
mod control_flow_ops_module {

    use cutile::core::*;

    #[cutile::entry()]
    fn control_flow_test_kernel<const S: [i32; 1]>(
        output: &mut Tensor<f32, S>,
        dynamic_value: i32,
    ) {
        // Test what control flow operations are generated
        let mut sum: Tile<f32, S> = load_tile_mut(output);

        // Test for loop (should generate cuda_tile.for)
        for _i in 0i32..10i32 {
            sum = sum + sum;
        }

        // Test if/else (should generate cuda_tile.if with yield)
        if dynamic_value < 5i32 {
            sum = sum + sum;
        } else {
            sum = sum - sum;
        }

        output.store(sum);
    }

    #[cutile::entry()]
    fn break_test_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>) {
        // Test break statement in infinite loop
        let mut sum: Tile<f32, S> = load_tile_mut(output);
        let mut i: i32 = 0;
        loop {
            sum = sum + sum;
            i = i + 1i32;
            if i >= 2i32 {
                break;
            }
        }
        output.store(sum);
    }

    #[cutile::entry()]
    fn if_return_test_kernel<const S: [i32; 1]>(output: &mut Tensor<i64, S>, conditional: bool) {
        let mut val: Tile<i64, S> = output.load();
        let result: Tile<i64, S> = if conditional {
            val = val + val;
            constant(2, val.shape())
        } else {
            val = val + val + val;
            constant(3, val.shape())
        };
        val = val + result;
        output.store(val);
    }

    #[cutile::entry()]
    fn while_loop_test_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>) {
        // Test while loop
        let mut sum: Tile<f32, S> = load_tile_mut(output);
        let mut counter: i32 = 0i32;

        while counter < 10i32 {
            sum = sum + sum;
            counter = counter + 1i32;
        }

        output.store(sum);
    }

    #[cutile::entry()]
    fn infinite_loop_test_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>) {
        // Test infinite loop
        let mut sum: Tile<f32, S> = load_tile_mut(output);
        let mut counter: i32 = 0i32;

        // Simulate loop with while instead
        while counter < 10i32 {
            sum = sum + sum;
            counter = counter + 1i32;
        }

        output.store(sum);
    }

    #[cutile::entry()]
    fn step_by_test_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>) {
        let mut sum: Tile<f32, S> = load_tile_mut(output);
        for _i in (0i32..100i32).step_by(10) {
            sum = sum + sum;
        }
        output.store(sum);
    }

    #[cutile::entry()]
    fn assume_test_kernel<const S: [i32; 1]>(output: &mut Tensor<i64, S>) {
        // Test assume operation - provides optimization hints to compiler
        // Note: Using i64 tensor because bounded predicate only works with integers
        let tile: Tile<i64, S> = load_tile_mut(output);

        // Tell compiler to assume tile values are non-negative (bounded<0, ?>)
        // This can enable additional optimizations
        // Using Melih's const-generic assume_bounds_lower with lower bound of 0
        let assumed_tile: Tile<i64, S> = unsafe { assume_bounds_lower::<_, 0>(tile) };

        // Use the assumed tile
        let result: Tile<i64, S> = assumed_tile + constant(1i64, output.shape());

        output.store(result);
    }

    #[cutile::entry()]
    fn assume_non_negative_test_kernel<const S: [i32; 1]>(output: &mut Tensor<i64, S>) {
        let tile: Tile<i64, S> = load_tile_mut(output);

        // Assume values are non-negative (>= 0) using Melih's const-generic version
        let non_neg_tile: Tile<i64, S> = unsafe { assume_bounds_lower::<_, 0>(tile) };

        let result: Tile<i64, S> = non_neg_tile + constant(1i64, output.shape());
        output.store(result);
    }

    #[cutile::entry()]
    fn assume_div_by_test_kernel<const S: [i32; 1]>(output: &mut Tensor<i64, S>) {
        let tile: Tile<i64, S> = load_tile_mut(output);

        // Assume all values are divisible by 16 (using Melih's const generic version)
        let aligned_tile: Tile<i64, S> = unsafe { assume_div_by::<_, 16>(tile) };

        let result: Tile<i64, S> = aligned_tile + constant(1i64, output.shape());
        output.store(result);
    }

    #[cutile::entry()]
    fn assume_same_elements_test_kernel<const S: [i32; 2]>(output: &mut Tensor<i64, S>) {
        let tile: Tile<i64, S> = load_tile_mut(output);

        // Assume groups of size 2 along dim 0, size 4 along dim 1 have same elements
        // Using Melih's const-generic pattern instead of runtime parameters
        let same_tile: Tile<i64, S> = unsafe { assume_same_elements_2d::<_, 2, 4>(tile) };

        let result: Tile<i64, S> = same_tile + constant(1i64, output.shape());
        output.store(result);
    }

    /// Repro: for loop inside if doesn't propagate mutable variable.
    /// collect_mutated_variables_from_block doesn't recurse into
    /// nested control flow (for/while/if), so the if op doesn't
    /// yield acc, and post-if code uses the stale pre-if value.
    #[cutile::entry()]
    fn if_for_carry_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>, flag: i32) {
        let mut acc: Tile<f32, S> = constant(0.0f32, output.shape());
        if flag > 0i32 {
            for _i in 0i32..10i32 {
                let ones: Tile<f32, S> = constant(1.0f32, output.shape());
                acc = acc + ones;
            }
        } else {
            acc = acc;
        }
        output.store(acc);
    }

    /// Same repro but with const generic flag (closer to Yinuo's report).
    #[cutile::entry()]
    fn if_for_carry_const_kernel<const S: [i32; 1], const FLAG: i32, const N: i32>(
        output: &mut Tensor<f32, S>,
    ) {
        let mut acc: Tile<f32, S> = constant(0.0f32, output.shape());
        if FLAG > 0i32 {
            for _i in 0i32..N {
                let ones: Tile<f32, S> = constant(1.0f32, output.shape());
                acc = acc + ones;
            }
        } else {
            acc = acc;
        }
        output.store(acc);
    }

    /// if/else as a tile expression: `let result = if cond { a } else { b };`
    #[cutile::entry()]
    fn if_else_tile_expr_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>, flag: i32) {
        let ones: Tile<f32, S> = constant(1.0f32, output.shape());
        let twos: Tile<f32, S> = constant(2.0f32, output.shape());
        let result: Tile<f32, S> = if flag > 0i32 { ones } else { twos };
        output.store(result);
    }

    /// Nested mutation: for-in-if with runtime condition.
    /// The for loop mutates `acc` inside a dynamic if branch.
    #[cutile::entry()]
    fn nested_for_in_if_kernel<const S: [i32; 1]>(output: &mut Tensor<f32, S>, flag: i32) {
        let mut acc: Tile<f32, S> = constant(0.0f32, output.shape());
        if flag > 0i32 {
            for _i in 0i32..5i32 {
                let twos: Tile<f32, S> = constant(2.0f32, output.shape());
                acc = acc + twos;
            }
        } else {
            let ones: Tile<f32, S> = constant(1.0f32, output.shape());
            acc = acc + ones;
        }
        output.store(acc);
    }

    /// Nested mutation: for-in-if with const condition (const-folded path).
    #[cutile::entry()]
    fn nested_for_in_if_const_kernel<const S: [i32; 1], const FLAG: i32>(
        output: &mut Tensor<f32, S>,
    ) {
        let mut acc: Tile<f32, S> = constant(0.0f32, output.shape());
        if FLAG > 0i32 {
            for _i in 0i32..5i32 {
                let twos: Tile<f32, S> = constant(2.0f32, output.shape());
                acc = acc + twos;
            }
        } else {
            let ones: Tile<f32, S> = constant(1.0f32, output.shape());
            acc = acc + ones;
        }
        output.store(acc);
    }

    /// Deeply nested: if-in-for-in-if with const outer condition.
    /// Outer if is const-folded, for loop runs 4 times, inner if uses runtime flag.
    #[cutile::entry()]
    fn nested_if_for_if_kernel<const S: [i32; 1], const OUTER: i32>(
        output: &mut Tensor<f32, S>,
        inner_flag: i32,
    ) {
        let mut acc: Tile<f32, S> = constant(0.0f32, output.shape());
        if OUTER > 0i32 {
            for _i in 0i32..4i32 {
                if inner_flag > 0i32 {
                    let threes: Tile<f32, S> = constant(3.0f32, output.shape());
                    acc = acc + threes;
                } else {
                    acc = acc;
                }
            }
        } else {
            acc = acc;
        }
        output.store(acc);
    }
}

use control_flow_ops_module::{
    _module_asts, break_test_kernel, if_else_tile_expr_kernel, if_for_carry_const_kernel,
    if_for_carry_kernel, if_return_test_kernel, nested_for_in_if_const_kernel,
    nested_for_in_if_kernel, nested_if_for_if_kernel,
};

#[test]
fn compile_control_flow_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "control_flow_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== CONTROL FLOW TEST MLIR ===\n{}", module_op_str);

        let has_for = module_op_str.contains(" for ");
        let has_if = module_op_str.contains("if %");
        let has_continue = module_op_str.contains("continue ");
        let has_return = module_op_str.contains("return");

        println!("\n=== Control Flow Operations Found ===");
        println!("for:      {}", if has_for { "" } else { "" });
        println!("if:       {}", if has_if { "" } else { "" });
        println!("continue: {}", if has_continue { "" } else { "" });
        println!("return:   {}", if has_return { "" } else { "" });

        assert!(has_for, "Expected 'for' loop in MLIR");
        assert!(has_if, "Expected 'if' in MLIR");
        assert!(has_continue, "Expected 'continue' as loop terminator");
        assert!(has_return, "Expected 'return' at function end");

        println!("\n✓ All control flow operations generated (for, if, continue, return)!");
    });
}

#[test]
fn compile_if_result_test() -> () {
    common::with_test_stack(|| {
        let arg: Tensor<i64> = ones(&[16]).sync().expect("Failed.");
        // If true, double and add 2.
        let (result, _) = if_return_test_kernel(arg.partition([4]), true)
            .sync()
            .expect("Failed.");
        let result: Vec<i64> = result.unpartition().to_host_vec().sync().expect("Failed.");
        assert!(result.iter().all(|x| *x == 4));

        // If false, triple and add 3.
        let arg: Tensor<i64> = ones(&[16]).sync().expect("Failed.");
        let (result, _) = if_return_test_kernel(arg.partition([4]), false)
            .sync()
            .expect("Failed.");
        let result: Vec<i64> = result.unpartition().to_host_vec().sync().expect("Failed.");
        assert!(result.iter().all(|x| *x == 6));
    });
}

#[test]
fn execute_break_test() -> () {
    common::with_test_stack(|| {
        // break_test_kernel loads output, doubles it twice (loop runs 2 iterations then breaks),
        // and stores the result. Starting from 1.0, we expect 1.0 * 2 * 2 = 4.0.
        let arg: Tensor<f32> = ones(&[16]).sync().expect("Failed.");
        let (result,) = break_test_kernel(arg.partition([4]))
            .sync()
            .expect("Failed.");
        let result: Vec<f32> = result.unpartition().to_host_vec().sync().expect("Failed.");
        assert!(
            result.iter().all(|x| *x == 4.0),
            "Expected all elements to be 4.0, got: {:?}",
            result
        );
    });
}

#[test]
fn compile_break_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "break_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== BREAK TEST MLIR ===\n{}", module_op_str);

        let has_loop = module_op_str.contains("cuda_tile.loop");
        let has_break = module_op_str.contains("break");

        assert!(has_loop, "Expected loop operation in IR");
        assert!(has_break, "Expected break operation in IR");

        println!("\n✓ break statement compiled successfully");
    });
}

#[test]
fn compile_while_loop_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "while_loop_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== WHILE LOOP TEST MLIR ===\n{}", module_op_str);

        let has_loop = module_op_str.contains("cuda_tile.loop") || module_op_str.contains(" loop ");
        let has_break = module_op_str.contains("break ");

        assert!(has_loop, "Expected cuda_tile.loop operation in MLIR");
        assert!(has_break, "Expected break operation for while loop exit");

        println!("\n✓ while loop compiled to cuda_tile.loop with break!");
    });
}

#[test]
fn compile_loop_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "infinite_loop_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== LOOP TEST MLIR ===\n{}", module_op_str);

        let has_loop = module_op_str.contains("cuda_tile.loop") || module_op_str.contains(" loop ");
        let has_break = module_op_str.contains("break ");

        assert!(has_loop, "Expected cuda_tile.loop operation in MLIR");
        assert!(has_break, "Expected break operation for loop exit");

        println!("\n✓ loop expression compiled to cuda_tile.loop with break!");
    });
}

#[test]
fn compile_step_by_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "step_by_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== STEP_BY TEST MLIR ===\n{}", module_op_str);

        assert!(
            module_op_str.contains(" for "),
            "Expected for loop in MLIR output"
        );
        assert!(
            module_op_str.contains(", step %"),
            "Expected step_by(10) to compile to a for-loop with step"
        );
    });
}

#[test]
fn compile_assume_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "assume_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== ASSUME MLIR ===\n{}", module_op_str);

        // Verify assume operation appears
        assert!(
            module_op_str.contains("assume"),
            "Expected assume operation in MLIR output"
        );

        // Verify it has the bounded predicate attribute (bounded<0, ?> = non-negative)
        assert!(
            module_op_str.contains("bounded"),
            "Expected bounded predicate on assume operation"
        );

        println!(
            "\n✓ assume operation verified (compiler optimization hint with bounded predicate)"
        );
    });
}

#[test]
fn compile_assume_non_negative_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "assume_non_negative_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== ASSUME_NON_NEGATIVE MLIR ===\n{}", module_op_str);

        assert!(
            module_op_str.contains("assume"),
            "Expected assume operation in MLIR output"
        );
        assert!(
            module_op_str.contains("bounded<0, ?>"),
            "Expected bounded<0, ?> predicate on assume operation"
        );

        println!("\n✓ assume_non_negative operation verified with bounded<0, ?>");
    });
}

#[test]
fn compile_assume_div_by_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "assume_div_by_test_kernel",
            &[128.to_string()],
            &[("output", &[1])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== ASSUME_DIV_BY MLIR ===\n{}", module_op_str);

        assert!(
            module_op_str.contains("assume"),
            "Expected assume operation in MLIR output"
        );
        assert!(
            module_op_str.contains("div_by<16>"),
            "Expected div_by<16> predicate on assume operation"
        );

        println!("\n✓ assume_div_by operation verified with div_by<16>");
    });
}

#[test]
fn compile_assume_same_elements_test() -> () {
    common::with_test_stack(|| {
        let modules =
            CUDATileModules::new(_module_asts()).expect("Failed to create CUDATileModules");
        let gpu_name = get_gpu_name(0);
        let compiler = CUDATileFunctionCompiler::new(
            &modules,
            "control_flow_ops_module",
            "assume_same_elements_test_kernel",
            &[4.to_string(), 8.to_string()],
            &[("output", &[2, 2])],
            &[],
            &[],
            None,
            gpu_name,
            &CompileOptions::default(),
        )
        .expect("Failed.");
        let module_op_str = compiler
            .compile()
            .expect("Failed.")
            .to_string();
        println!("\n=== ASSUME_SAME_ELEMENTS MLIR ===\n{}", module_op_str);

        assert!(
            module_op_str.contains("assume"),
            "Expected assume operation in MLIR output"
        );
        assert!(
            module_op_str.contains("same_elements<[2, 4]>"),
            "Expected same_elements<[2, 4]> predicate on assume operation"
        );

        println!("\n✓ assume_same_elements operation verified with same_elements<[2, 4]>");
    });
}

#[test]
fn if_for_carry_propagates_mutation() {
    // Repro: for loop inside if should propagate mutable variable updates.
    // flag=1 means the if body runs: acc += 1.0 ten times → acc = 10.0.
    // Bug: acc stays 0.0 because the if op doesn't yield the for's output.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        if_for_carry_kernel((&mut output).partition([128]), 1i32)
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 10.0).abs() < 1e-3,
            "Expected 10.0 (for loop ran 10 times), got {}",
            host[0]
        );
    });
}

#[test]
fn if_for_carry_const_propagates_mutation() {
    // Same as above but with const generic FLAG and N.
    // FLAG=1, N=10 → acc = 10.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        if_for_carry_const_kernel((&mut output).partition([128]))
            .generics(vec![
                "128".to_string(), // S
                "1".to_string(),   // FLAG
                "10".to_string(),  // N
            ])
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 10.0).abs() < 1e-3,
            "Expected 10.0 (const FLAG=1, N=10), got {}",
            host[0]
        );
    });
}

#[test]
fn if_else_tile_expr_returns_value() {
    // if/else as an expression: `let result = if flag > 0 { ones } else { twos };`
    // flag=1 → result = 1.0, flag=0 → result = 2.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        if_else_tile_expr_kernel((&mut output).partition([128]), 1i32)
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 1.0).abs() < 1e-3,
            "flag=1: expected 1.0, got {}",
            host[0]
        );
    });
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        if_else_tile_expr_kernel((&mut output).partition([128]), 0i32)
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 2.0).abs() < 1e-3,
            "flag=0: expected 2.0, got {}",
            host[0]
        );
    });
}

// ---- Nested mutation tests ------------------------------------------------

#[test]
fn nested_for_in_if_dynamic() {
    // Runtime flag=1: for loop runs 5 times, acc += 2.0 each → 10.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_for_in_if_kernel((&mut output).partition([128]), 1i32)
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 10.0).abs() < 1e-3,
            "flag=1: expected 10.0, got {}",
            host[0]
        );
    });
    // Runtime flag=0: else branch, acc += 1.0 → 1.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_for_in_if_kernel((&mut output).partition([128]), 0i32)
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 1.0).abs() < 1e-3,
            "flag=0: expected 1.0, got {}",
            host[0]
        );
    });
}

#[test]
fn nested_for_in_if_const_folded() {
    // Const FLAG=1: then branch const-folded in, for runs 5 times → 10.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_for_in_if_const_kernel((&mut output).partition([128]))
            .generics(vec!["128".into(), "1".into()])
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 10.0).abs() < 1e-3,
            "FLAG=1: expected 10.0, got {}",
            host[0]
        );
    });
    // Const FLAG=0: else branch const-folded in, acc += 1.0 → 1.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_for_in_if_const_kernel((&mut output).partition([128]))
            .generics(vec!["128".into(), "0".into()])
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 1.0).abs() < 1e-3,
            "FLAG=0: expected 1.0, got {}",
            host[0]
        );
    });
}

#[test]
fn nested_if_for_if_deep() {
    // Const OUTER=1, runtime inner_flag=1: for runs 4x, inner if adds 3.0 → 12.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_if_for_if_kernel((&mut output).partition([128]), 1i32)
            .generics(vec!["128".into(), "1".into()])
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0] - 12.0).abs() < 1e-3,
            "OUTER=1, inner=1: expected 12.0, got {}",
            host[0]
        );
    });
    // Const OUTER=1, runtime inner_flag=0: for runs 4x, inner else is no-op → 0.0.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_if_for_if_kernel((&mut output).partition([128]), 0i32)
            .generics(vec!["128".into(), "1".into()])
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0]).abs() < 1e-3,
            "OUTER=1, inner=0: expected 0.0, got {}",
            host[0]
        );
    });
    // Const OUTER=0: outer else is no-op → 0.0 regardless of inner_flag.
    common::with_test_stack(|| {
        let mut output = cutile::api::zeros::<f32>(&[128]).sync().expect("alloc");
        nested_if_for_if_kernel((&mut output).partition([128]), 1i32)
            .generics(vec!["128".into(), "0".into()])
            .sync()
            .expect("kernel");
        let host: Vec<f32> = output.dup().to_host_vec().sync().expect("to_host");
        assert!(
            (host[0]).abs() < 1e-3,
            "OUTER=0: expected 0.0, got {}",
            host[0]
        );
    });
}