cutile-compiler 0.3.0

Crate for compiling kernels authored in cuTile Rust to executable kernels.
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
/*
 * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Pure-Rust utility functions and types used by compiler2.

use crate::ast::SourceLocation;
use crate::error::{JITError, SpannedJITError};
use crate::syn_utils::get_ident_from_path_expr;
use half::{bf16, f16};
use proc_macro2::{TokenStream, TokenTree};
use quote::ToTokens;
use std::collections::{BTreeSet, HashSet};
use std::fmt::{Debug, LowerHex};
use std::hash::Hash;
use syn::{Expr, Pat, Stmt};

use super::_value::{CompilerContext, TileRustValue};

// ---------------------------------------------------------------------------
// Stack management constants
// ---------------------------------------------------------------------------

/// Minimum remaining stack space before growing (4 MiB).
///
/// Large kernels with nested control flow and inlined DSL helpers can enter
/// non-trivial work just below a `stacker::maybe_grow` boundary. Growing before
/// the final MiB avoids finite deep lowering paths exhausting the native stack.
pub(crate) const STACK_RED_ZONE: usize = 4 * 1024 * 1024;
/// Size of each new stack segment when growth is needed (10 MiB).
pub(crate) const STACK_GROW_SIZE: usize = 10 * 1024 * 1024;

/// Maximum rank of a mapped partition (`MappedPartitionMut` / `iter_indices`).
/// Matches the DSL's `variadic_struct(N = 6)` expansion ceiling.
pub(crate) const MAX_MAPPED_PARTITION_RANK: usize = 6;

/// Map-shape sentinel marking an owned axis (mirrors `cutile::tensor::OWNED`).
///
/// An owned axis is not traversed by the mapped work-item stream: each stream
/// item owns the axis's full extent (subtensor-per-CTA exclusivity), and
/// in-kernel loops traverse it with bounds-proven indices.
pub(crate) const OWNED_MAP_DIM: i32 = 0;

// ---------------------------------------------------------------------------
// AtomicMode
// ---------------------------------------------------------------------------

#[derive(Debug, Eq, PartialEq)]
/// Supported atomic read-modify-write modes.
pub enum AtomicMode {
    And = 0,
    Or = 1,
    Xor = 2,
    Add = 3,
    AddF = 4,
    Max = 5,
    Min = 6,
    UMax = 7,
    UMin = 8,
    XChg = 9,
}

// ---------------------------------------------------------------------------
// ElementTypePrefix
// ---------------------------------------------------------------------------

#[derive(Debug, Eq, PartialEq)]
/// Whether an element type is floating-point or integer.
pub enum ElementTypePrefix {
    Float,
    Integer,
}

impl ElementTypePrefix {
    /// Determines the prefix from a CUDA Tile element type string (e.g. `"f32"` -> `Float`).
    pub fn new(cuda_elem_ty_str: &str) -> Result<Self, JITError> {
        match super::_type::scalar_from_name(cuda_elem_ty_str) {
            Some(s) if s.is_float() => Ok(ElementTypePrefix::Float),
            Some(_) => Ok(ElementTypePrefix::Integer),
            None => SourceLocation::unknown()
                .jit_error_result(&format!("unsupported element type `{cuda_elem_ty_str}`")),
        }
    }
}

impl AtomicMode {
    /// Parses an atomic mode from the ZST type identifier (e.g. `AddF`),
    /// validating compatibility with the element type.
    pub fn new(mode: &str, elem_ty_prefix: ElementTypePrefix) -> Result<Self, JITError> {
        let result = match mode {
            "And" => AtomicMode::And,
            "Or" => AtomicMode::Or,
            "Xor" => AtomicMode::Xor,
            "Add" => AtomicMode::Add,
            "AddF" => AtomicMode::AddF,
            "Max" => AtomicMode::Max,
            "Min" => AtomicMode::Min,
            "Umax" => AtomicMode::UMax,
            "Umin" => AtomicMode::UMin,
            "Xchg" => AtomicMode::XChg,
            _ => return SourceLocation::unknown().jit_error_result(
                &format!("invalid atomic mode `{mode}`; valid modes are: And, Or, Xor, Add, AddF, Max, Min, Umax, Umin, Xchg"),
            ),
        };
        if elem_ty_prefix == ElementTypePrefix::Float {
            if ![AtomicMode::XChg, AtomicMode::AddF].contains(&result) {
                return SourceLocation::unknown().jit_error_result(&format!(
                    "float types only support `Xchg` and `AddF` atomic modes, got `{:?}`",
                    result
                ));
            }
        }
        Ok(result)
    }
}

// Re-export from bounds.rs (canonical location shared with old compiler).
pub use crate::bounds::{get_binary_op_from_op_str, get_tile_bop_from_rust_bop, TileBinaryOp};

// ---------------------------------------------------------------------------
// Constant hex encoding
// ---------------------------------------------------------------------------

fn format_hex<T: LowerHex>(val: T) -> String {
    format!("0x{:x}", val)
}

trait Float {
    fn to_hex(&self) -> String;
    fn zero() -> Self;
    fn one() -> Self;
    fn negative_infinity() -> Self;
    fn positive_infinity() -> Self;
    fn e() -> Self;
}

impl Float for f16 {
    fn to_hex(&self) -> String {
        format_hex(self.to_bits())
    }
    fn zero() -> f16 {
        f16::ZERO
    }
    fn one() -> f16 {
        f16::ONE
    }
    fn negative_infinity() -> f16 {
        f16::NEG_INFINITY
    }
    fn positive_infinity() -> f16 {
        f16::INFINITY
    }
    fn e() -> f16 {
        f16::E
    }
}

impl Float for bf16 {
    fn to_hex(&self) -> String {
        format_hex(self.to_bits())
    }
    fn zero() -> bf16 {
        bf16::ZERO
    }
    fn one() -> bf16 {
        bf16::ONE
    }
    fn negative_infinity() -> bf16 {
        bf16::NEG_INFINITY
    }
    fn positive_infinity() -> bf16 {
        bf16::INFINITY
    }
    fn e() -> bf16 {
        bf16::E
    }
}

impl Float for f32 {
    fn to_hex(&self) -> String {
        format_hex(self.to_bits())
    }
    fn zero() -> f32 {
        0.0f32
    }
    fn one() -> f32 {
        1.0f32
    }
    fn negative_infinity() -> f32 {
        f32::NEG_INFINITY
    }
    fn positive_infinity() -> f32 {
        f32::INFINITY
    }
    fn e() -> f32 {
        std::f32::consts::E
    }
}

impl Float for f64 {
    fn to_hex(&self) -> String {
        format_hex(self.to_bits())
    }
    fn zero() -> f64 {
        0.0f64
    }
    fn one() -> f64 {
        1.0f64
    }
    fn negative_infinity() -> f64 {
        f64::NEG_INFINITY
    }
    fn positive_infinity() -> f64 {
        f64::INFINITY
    }
    fn e() -> f64 {
        std::f64::consts::E
    }
}

trait Integer
where
    Self: LowerHex,
{
    fn to_hex(&self) -> String {
        format_hex(self)
    }
    fn zero() -> Self;
    fn one() -> Self;
    fn min() -> Self;
    fn max() -> Self;
}

impl Integer for i32 {
    fn zero() -> i32 {
        0i32
    }
    fn one() -> i32 {
        1i32
    }
    fn min() -> i32 {
        i32::MIN
    }
    fn max() -> i32 {
        i32::MAX
    }
}
impl Integer for i64 {
    fn zero() -> i64 {
        0i64
    }
    fn one() -> i64 {
        1i64
    }
    fn min() -> i64 {
        i64::MIN
    }
    fn max() -> i64 {
        i64::MAX
    }
}
impl Integer for u32 {
    fn zero() -> u32 {
        0u32
    }
    fn one() -> u32 {
        1u32
    }
    fn min() -> u32 {
        u32::MIN
    }
    fn max() -> u32 {
        u32::MAX
    }
}
impl Integer for u64 {
    fn zero() -> u64 {
        0u64
    }
    fn one() -> u64 {
        1u64
    }
    fn min() -> u64 {
        u64::MIN
    }
    fn max() -> u64 {
        u64::MAX
    }
}

fn get_float_const<T: Float>(const_str: &str) -> Result<String, JITError> {
    match const_str {
        "zero" => Ok(T::zero().to_hex()),
        "one" => Ok(T::one().to_hex()),
        "min" => Ok(T::negative_infinity().to_hex()),
        "max" => Ok(T::positive_infinity().to_hex()),
        "e" => Ok(T::e().to_hex()),
        _ => SourceLocation::unknown()
            .jit_error_result(&format!("Unsupported float constant type {}.", const_str)),
    }
}

fn get_integer_const<T: Integer>(const_str: &str) -> Result<String, JITError> {
    match const_str {
        "zero" => Ok(T::zero().to_hex()),
        "one" => Ok(T::one().to_hex()),
        "min" => Ok(T::min().to_hex()),
        "max" => Ok(T::max().to_hex()),
        _ => SourceLocation::unknown()
            .jit_error_result(&format!("Unsupported integer constant type {}.", const_str)),
    }
}

/// Returns the hex-encoded constant string for a typed constant name (e.g. `"zero"`, `"one"`).
pub fn get_const_hex(rust_element_type_str: &str, const_str: &str) -> Result<String, JITError> {
    match rust_element_type_str {
        "bf16" => get_float_const::<bf16>(const_str),
        "f16" => get_float_const::<f16>(const_str),
        "f32" => get_float_const::<f32>(const_str),
        "f64" => get_float_const::<f64>(const_str),
        "i32" => get_integer_const::<i32>(const_str),
        "i64" => get_integer_const::<i64>(const_str),
        "u32" => get_integer_const::<u32>(const_str),
        "u64" => get_integer_const::<u64>(const_str),
        _ => SourceLocation::unknown().jit_error_result(&format!(
            "Unsupported constant type {} {}.",
            rust_element_type_str, const_str
        )),
    }
}

// ---------------------------------------------------------------------------
// String literal / option arg extraction
// ---------------------------------------------------------------------------

/// Reads the last path-segment ident from a ZST type-as-value expression.
///
/// Examples: `atomic::AddF` -> `"AddF"`, `Enabled` -> `"Enabled"`.
/// Used to resolve attribute selectors that the DSL surfaces as ZST type
/// arguments (`mode: atomic::Mode`, `memory_ordering: ordering::Mode`, etc.).
pub fn extract_zst_type_name(expr: &syn::Expr, param_name: &str) -> Result<String, JITError> {
    use syn::Expr;
    match expr {
        Expr::Path(path) => Ok(path.path.segments.last().unwrap().ident.to_string()),
        _ => SourceLocation::unknown().jit_error_result(&format!(
            "`{param_name}` must be a unit-struct type-as-value path, got `{}`",
            expr.to_token_stream().to_string()
        )),
    }
}

pub fn padding_zst_value(expr: &syn::Expr) -> Option<String> {
    let syn::Expr::Path(path) = expr else {
        return None;
    };
    let ident = path.path.segments.last()?.ident.to_string();
    match ident.as_str() {
        "Zero" => Some("zero".to_string()),
        "NegZero" => Some("neg_zero".to_string()),
        "Nan" => Some("nan".to_string()),
        "PosInf" => Some("pos_inf".to_string()),
        "NegInf" => Some("neg_inf".to_string()),
        _ => None,
    }
}

pub fn zst_type_name(expr: &syn::Expr) -> Option<String> {
    let syn::Expr::Path(path) = expr else {
        return None;
    };
    Some(path.path.segments.last()?.ident.to_string())
}

/// Extracts a string literal value from an expression, handling both direct literals
/// and variables that were bound from string literals.
pub fn extract_string_literal(
    expr: &syn::Expr,
    param_name: &str,
    ctx: &CompilerContext,
) -> Result<String, JITError> {
    use syn::{Expr, ExprLit, Lit};

    match expr {
        Expr::Lit(ExprLit {
            lit: Lit::Str(s), ..
        }) => Ok(s.value()),
        Expr::Path(path_expr) => {
            let var_name = path_expr.path.segments.last().unwrap().ident.to_string();
            if let Some(val) = ctx.vars.get(&var_name) {
                if let Some(Expr::Lit(ExprLit {
                    lit: Lit::Str(s), ..
                })) = &val.string_literal
                {
                    return Ok(s.value());
                }
            }
            SourceLocation::unknown().jit_error_result(&format!(
                "`{param_name}` must be a string literal, but got variable `{var_name}`; \
                     ensure string literals are passed directly",
            ))
        }
        _ => SourceLocation::unknown().jit_error_result(&format!(
            "`{param_name}` must be a string literal, got `{}`",
            expr.to_token_stream().to_string()
        )),
    }
}

/// Helper to resolve compile-time optional argument.
/// Returns the inner expression if it is Some(expr), or None if it is None.
pub fn resolve_option_arg(expr: &syn::Expr, ctx: &CompilerContext) -> Option<syn::Expr> {
    use syn::Expr;
    if let Expr::Call(call) = expr {
        if let Expr::Path(path) = &*call.func {
            if path.path.segments.last().unwrap().ident == "Some" {
                return call.args.first().cloned();
            }
        }
    } else if let Expr::Path(path) = expr {
        if path.path.segments.len() == 1 && path.path.segments.last().unwrap().ident == "None" {
            return None;
        }
        let var_name = path.path.segments.last().unwrap().ident.to_string();
        if let Some(val) = ctx.vars.get(&var_name) {
            if let Some(variant) = &val.enum_variant {
                return match variant.as_str() {
                    "Some" => val.enum_payload.as_deref().cloned(),
                    "None" => None,
                    _ => None,
                };
            }
            if let Some(ast) = &val.string_literal {
                if let Expr::Call(call) = ast {
                    if let Expr::Path(path) = &*call.func {
                        if path.path.segments.last().unwrap().ident == "Some" {
                            return call.args.first().cloned();
                        }
                    }
                } else if let Expr::Path(path) = ast {
                    if path.path.segments.len() == 1
                        && path.path.segments.last().unwrap().ident == "None"
                    {
                        return None;
                    }
                }
            }
        }
    }
    None
}

// ---------------------------------------------------------------------------
// Variable mutation analysis
// ---------------------------------------------------------------------------

fn collect_pattern_bindings(pat: &Pat, names: &mut Vec<String>) -> Result<(), JITError> {
    match pat {
        Pat::Ident(ident) => {
            names.push(ident.ident.to_string());
            if let Some((_at, subpat)) = &ident.subpat {
                collect_pattern_bindings(subpat, names)?;
            }
            Ok(())
        }
        Pat::Type(pat_type) => collect_pattern_bindings(&pat_type.pat, names),
        Pat::Paren(paren) => collect_pattern_bindings(&paren.pat, names),
        Pat::Reference(reference) => collect_pattern_bindings(&reference.pat, names),
        Pat::Tuple(tuple) => {
            for elem in &tuple.elems {
                collect_pattern_bindings(elem, names)?;
            }
            Ok(())
        }
        Pat::Slice(slice) => {
            for elem in &slice.elems {
                collect_pattern_bindings(elem, names)?;
            }
            Ok(())
        }
        Pat::Struct(pat_struct) => {
            for field in &pat_struct.fields {
                collect_pattern_bindings(&field.pat, names)?;
            }
            Ok(())
        }
        Pat::TupleStruct(tuple_struct) => {
            for elem in &tuple_struct.elems {
                collect_pattern_bindings(elem, names)?;
            }
            Ok(())
        }
        Pat::Or(or_pat) => {
            for case in &or_pat.cases {
                collect_pattern_bindings(case, names)?;
            }
            Ok(())
        }
        Pat::Wild(_) | Pat::Rest(_) => Ok(()),
        _ => SourceLocation::unknown()
            .jit_error_result(&format!("Local pattern type not supported {:#?}", pat)),
    }
}

/// Collects the names of variables assigned (mutated) in a block that were defined outside it.
pub fn collect_mutated_variables_from_block(
    block: &syn::Block,
) -> Result<BTreeSet<String>, JITError> {
    let mut local_vars: HashSet<String> = HashSet::new();
    let mut result: BTreeSet<String> = BTreeSet::new();
    for (_i, statement) in block.stmts.iter().enumerate() {
        match statement {
            Stmt::Local(local) => {
                let mut var_names: Vec<String> = vec![];
                collect_pattern_bindings(&local.pat, &mut var_names)?;
                local_vars.extend(var_names);
            }
            Stmt::Expr(Expr::Assign(assign_expr), _) => {
                let var_name: String = match &*assign_expr.left {
                    Expr::Path(path_expr) => get_ident_from_path_expr(path_expr).to_string(),
                    _ => {
                        return SourceLocation::unknown().jit_error_result(&format!(
                            "LHS assign expression not implemented {:#?}.",
                            assign_expr.left
                        ));
                    }
                };
                if !local_vars.contains(&var_name) {
                    result.insert(var_name);
                }
            }
            // Recurse into control-flow expressions to find nested mutations.
            Stmt::Expr(Expr::ForLoop(for_expr), _) => {
                let inner = collect_mutated_variables(for_expr)?;
                for name in inner {
                    if !local_vars.contains(&name) {
                        result.insert(name);
                    }
                }
            }
            Stmt::Expr(Expr::While(while_expr), _) => {
                let inner = collect_mutated_variables_from_block(&while_expr.body)?;
                for name in inner {
                    if !local_vars.contains(&name) {
                        result.insert(name);
                    }
                }
            }
            Stmt::Expr(Expr::Loop(loop_expr), _) => {
                let inner = collect_mutated_variables_from_block(&loop_expr.body)?;
                for name in inner {
                    if !local_vars.contains(&name) {
                        result.insert(name);
                    }
                }
            }
            Stmt::Expr(expr, _) => {
                let inner = collect_mutated_variables_from_expr(expr)?;
                for name in inner {
                    if !local_vars.contains(&name) {
                        result.insert(name);
                    }
                }
            }
            _ => continue,
        }
    }
    Ok(result)
}

/// Collects mutated outer-scope variables from an expression.
pub fn collect_mutated_variables_from_expr(expr: &Expr) -> Result<BTreeSet<String>, JITError> {
    match expr {
        Expr::Assign(assign_expr) => {
            let var_name: String = match &*assign_expr.left {
                Expr::Path(path_expr) => get_ident_from_path_expr(path_expr).to_string(),
                _ => {
                    return SourceLocation::unknown().jit_error_result(&format!(
                        "LHS assign expression not implemented {:#?}.",
                        assign_expr.left
                    ));
                }
            };
            Ok(BTreeSet::from([var_name]))
        }
        Expr::Block(block_expr) => collect_mutated_variables_from_block(&block_expr.block),
        Expr::ForLoop(for_expr) => collect_mutated_variables(for_expr),
        Expr::While(while_expr) => collect_mutated_variables_from_block(&while_expr.body),
        Expr::Loop(loop_expr) => collect_mutated_variables_from_block(&loop_expr.body),
        Expr::If(if_expr) => {
            let mut result = collect_mutated_variables_from_block(&if_expr.then_branch)?;
            if let Some((_else, else_expr)) = &if_expr.else_branch {
                result.extend(collect_mutated_variables_from_expr(else_expr)?);
            }
            Ok(result)
        }
        _ => Ok(BTreeSet::new()),
    }
}

/// Collects mutated outer-scope variables from a for-loop body.
pub fn collect_mutated_variables(
    for_expr: &syn::ExprForLoop,
) -> Result<BTreeSet<String>, JITError> {
    let mut result = collect_mutated_variables_from_block(&for_expr.body)?;
    let mut loop_vars = Vec::new();
    collect_pattern_bindings(&for_expr.pat, &mut loop_vars)?;
    for loop_var in loop_vars {
        result.remove(&loop_var);
    }
    Ok(result)
}

/// Set a variable's ordering token directly. A no-op if the variable is absent
/// or carries no token field. Used to publish a loop's accumulated token to the
/// tensor and to views written in the loop body.
pub fn set_view_token(var: &str, token: cutile_ir::ir::Value, ctx: &mut CompilerContext) {
    let Some(value) = ctx.vars.get(var) else {
        return;
    };
    let mut new_value = value.clone();
    let Some(meta) = new_value.type_meta.as_mut() else {
        return;
    };
    let Some(field) = meta.fields.get_mut("token") else {
        return;
    };
    field.value = Some(token);
    ctx.vars.insert(var.to_string(), new_value);
}

/// Method names that write to a mutable partition/tensor view (advance its
/// ordering token). Used to find which resources a loop body stores to, so the
/// loop can thread and publish their tokens (token-ordering across the loop
/// scope boundary).
const STORE_METHOD_NAMES: &[&str] = &["store", "store_index"];

/// A `.store(...)` call found in a loop body: the receiver view and whether its
/// index varies with the loop variable (distinct per iteration → the writes are
/// disjoint and may fork; otherwise the same address is written each iteration
/// and the writes must be serialized).
pub struct StoreCall {
    pub receiver: String,
    pub index_distinct: bool,
}

/// Collects the `.store(...)` / `.store_index(...)` calls anywhere in `block`
/// (descending through nested control flow and `unsafe` blocks), with the
/// receiver view and whether the store's index references `loop_var`. When
/// `loop_var` is `None` (no simple loop variable), indices are treated as
/// non-distinct (conservative: serialize).
pub fn collect_store_calls(block: &syn::Block, loop_var: Option<&str>) -> Vec<StoreCall> {
    let mut out = Vec::new();
    for stmt in &block.stmts {
        collect_store_calls_stmt(stmt, loop_var, &mut out);
    }
    out
}

fn collect_store_calls_stmt(stmt: &Stmt, loop_var: Option<&str>, out: &mut Vec<StoreCall>) {
    match stmt {
        Stmt::Local(local) => {
            if let Some(init) = &local.init {
                collect_store_calls_expr(&init.expr, loop_var, out);
            }
        }
        Stmt::Expr(expr, _) => collect_store_calls_expr(expr, loop_var, out),
        _ => {}
    }
}

fn collect_store_calls_expr(expr: &Expr, loop_var: Option<&str>, out: &mut Vec<StoreCall>) {
    match expr {
        Expr::MethodCall(mc) => {
            if STORE_METHOD_NAMES.contains(&mc.method.to_string().as_str()) {
                if let Expr::Path(path) = &*mc.receiver {
                    if let Some(ident) = path.path.get_ident() {
                        // The index is the second argument: `store(tile, index)`.
                        let index_distinct = match (loop_var, mc.args.iter().nth(1)) {
                            (Some(lv), Some(index)) => expr_references_ident(index, lv),
                            _ => false,
                        };
                        out.push(StoreCall {
                            receiver: ident.to_string(),
                            index_distinct,
                        });
                    }
                }
            }
            collect_store_calls_expr(&mc.receiver, loop_var, out);
            for arg in &mc.args {
                collect_store_calls_expr(arg, loop_var, out);
            }
        }
        Expr::Block(b) => collect_store_calls_block(&b.block, loop_var, out),
        Expr::Unsafe(u) => collect_store_calls_block(&u.block, loop_var, out),
        Expr::ForLoop(f) => collect_store_calls_block(&f.body, loop_var, out),
        Expr::While(w) => collect_store_calls_block(&w.body, loop_var, out),
        Expr::Loop(l) => collect_store_calls_block(&l.body, loop_var, out),
        Expr::If(i) => {
            collect_store_calls_block(&i.then_branch, loop_var, out);
            if let Some((_, else_expr)) = &i.else_branch {
                collect_store_calls_expr(else_expr, loop_var, out);
            }
        }
        Expr::Call(c) => {
            for arg in &c.args {
                collect_store_calls_expr(arg, loop_var, out);
            }
        }
        _ => {}
    }
}

fn collect_store_calls_block(block: &syn::Block, loop_var: Option<&str>, out: &mut Vec<StoreCall>) {
    for stmt in &block.stmts {
        collect_store_calls_stmt(stmt, loop_var, out);
    }
}

/// Whether `expr` syntactically references the identifier `name` anywhere. Used
/// to decide whether a store's index varies with the loop variable (fork —
/// distinct per iteration) or not (serialize — a constant/repeated address must
/// be ordered).
pub fn expr_references_ident(expr: &Expr, name: &str) -> bool {
    let mut found = false;
    references_ident_expr(expr, name, &mut found);
    found
}

fn references_ident_expr(expr: &Expr, name: &str, found: &mut bool) {
    if *found {
        return;
    }
    if let Expr::Path(path) = expr {
        if path.path.is_ident(name) {
            *found = true;
            return;
        }
    }
    // Descend into the common sub-expression carriers for index expressions.
    match expr {
        Expr::MethodCall(mc) => {
            references_ident_expr(&mc.receiver, name, found);
            for arg in &mc.args {
                references_ident_expr(arg, name, found);
            }
        }
        Expr::Call(c) => {
            references_ident_expr(&c.func, name, found);
            for arg in &c.args {
                references_ident_expr(arg, name, found);
            }
        }
        Expr::Binary(b) => {
            references_ident_expr(&b.left, name, found);
            references_ident_expr(&b.right, name, found);
        }
        Expr::Unary(u) => references_ident_expr(&u.expr, name, found),
        Expr::Paren(p) => references_ident_expr(&p.expr, name, found),
        Expr::Group(g) => references_ident_expr(&g.expr, name, found),
        Expr::Reference(r) => references_ident_expr(&r.expr, name, found),
        Expr::Tuple(t) => {
            for e in &t.elems {
                references_ident_expr(e, name, found);
            }
        }
        Expr::Array(a) => {
            for e in &a.elems {
                references_ident_expr(e, name, found);
            }
        }
        Expr::Cast(c) => references_ident_expr(&c.expr, name, found),
        Expr::Index(i) => {
            references_ident_expr(&i.expr, name, found);
            references_ident_expr(&i.index, name, found);
        }
        Expr::Field(f) => references_ident_expr(&f.base, name, found),
        _ => {}
    }
}

/// Collects mutated outer-scope variables from a while-loop body.
pub fn collect_mutated_variables_while(
    while_expr: &syn::ExprWhile,
) -> Result<BTreeSet<String>, JITError> {
    collect_mutated_variables_from_block(&while_expr.body)
}

/// Collects mutated outer-scope variables from a loop body.
pub fn collect_mutated_variables_loop(
    loop_expr: &syn::ExprLoop,
) -> Result<BTreeSet<String>, JITError> {
    collect_mutated_variables_from_block(&loop_expr.body)
}

// ---------------------------------------------------------------------------
// Misc utilities
// ---------------------------------------------------------------------------

/// Removes duplicate elements from a vector while preserving order.
pub fn dedup<T: Hash + Eq + Clone>(v: &mut Vec<T>) {
    let mut set = HashSet::new();
    v.retain(|x| set.insert(x.clone()));
}

/// Parses a comma-separated token stream into a list of `syn::Expr`.
pub fn parse_list_of_expr(tokens: TokenStream) -> Result<Vec<Expr>, JITError> {
    let mut args: Vec<Expr> = vec![];
    let mut arg_expr: Vec<TokenTree> = vec![];
    for (_i, token) in tokens.clone().into_iter().enumerate() {
        match &token {
            TokenTree::Literal(_lit) => {
                arg_expr.push(token.clone());
            }
            TokenTree::Ident(_ident) => {
                arg_expr.push(token.clone());
            }
            TokenTree::Punct(punct) => {
                if punct.as_char() == ',' {
                    if arg_expr.len() > 0 {
                        let expr =
                            syn::parse2::<syn::Expr>(arg_expr.into_iter().collect()).unwrap();
                        args.push(expr);
                    }
                    arg_expr = vec![];
                } else {
                    arg_expr.push(token.clone());
                }
            }
            _ => {
                return SourceLocation::unknown().jit_error_result(&format!(
                    "unexpected token `{}` in expression list",
                    token.to_string()
                ));
            }
        }
    }
    if arg_expr.len() > 0 {
        let expr = syn::parse2::<syn::Expr>(arg_expr.into_iter().collect()).unwrap();
        args.push(expr);
    }
    Ok(args)
}

// ---------------------------------------------------------------------------
// Token / type_meta helpers
// ---------------------------------------------------------------------------

/// Updates the ordering token in a variable's type metadata.
pub fn update_token(
    var_arg: &Expr,
    new_token: cutile_ir::ir::Value,
    ctx: &mut CompilerContext,
) -> Result<Option<TileRustValue>, JITError> {
    use crate::syn_utils::get_ident_from_expr;
    let Some(var_arg_ident) = get_ident_from_expr(var_arg) else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "expected a variable name, got `{}`",
            var_arg.to_token_stream().to_string()
        ));
    };
    let var_name = var_arg_ident.to_string();
    let Some(old_value) = ctx.vars.get(var_name.as_str()) else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "undefined variable `{var_name}` when updating token"
        ));
    };
    let mut new_value = old_value.clone();
    let Some(new_type_meta) = &mut new_value.type_meta else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "variable `{var_name}` does not have associated type metadata (expected a view type)"
        ));
    };
    let Some(new_token_value) = new_type_meta.fields.get_mut("token") else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "variable `{var_name}` is missing a `token` field (expected a view with an ordering token)"
        ));
    };
    new_token_value.value = Some(new_token);
    Ok(ctx.vars.insert(var_name, new_value))
}

/// Propagate a resource's current ordering token up its borrow link to the root
/// tensor it views. A view roots at its tensor (`tensor_origin`), so advancing
/// the view's token must advance the tensor's: a *later* view of the same tensor
/// seeds its token from `get_tensor_token`, and this is what makes that later
/// view happen-after this one's writes (the epoch boundary of the token model).
///
/// This runs at scope boundaries (where the view and its root tensor are both in
/// scope), not at the store site — inside a method frame the root tensor is out
/// of scope, so the propagation is deferred to the boundary that reconciles the
/// view back to its caller. A no-op when the resource has no root, roots at
/// itself (a tensor), or the root is not a live variable here.
pub fn propagate_token_to_root(resource_var: &str, ctx: &mut CompilerContext) {
    let Some(resource) = ctx.vars.get(resource_var) else {
        return;
    };
    let Some(root) = resource.tensor_origin.clone() else {
        return;
    };
    if root == resource_var {
        return; // A tensor roots at itself; there is nothing above it to walk to.
    }
    let token = resource
        .type_meta
        .as_ref()
        .and_then(|meta| meta.fields.get("token"))
        .and_then(|field| field.value);
    let Some(token) = token else {
        return;
    };
    let Some(root_value) = ctx.vars.get(&root) else {
        return;
    };
    let mut new_root = root_value.clone();
    let Some(meta) = new_root.type_meta.as_mut() else {
        return;
    };
    let Some(token_field) = meta.fields.get_mut("token") else {
        return;
    };
    token_field.value = Some(token);
    ctx.vars.insert(root, new_root);
}

/// Retrieves the ordering token from a variable expression's type metadata.
pub fn get_token_from_expr(
    var_arg: &Expr,
    ctx: &mut CompilerContext,
) -> Result<TileRustValue, JITError> {
    use crate::syn_utils::get_ident_from_expr;
    let Some(var_arg_ident) = get_ident_from_expr(var_arg) else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "expected a variable name, got `{}`",
            var_arg.to_token_stream().to_string()
        ));
    };
    let var_name = var_arg_ident.to_string();
    get_token(var_name.as_str(), ctx)
}

/// Retrieves the ordering token from a named variable's type metadata.
pub fn get_token(var_name: &str, ctx: &mut CompilerContext) -> Result<TileRustValue, JITError> {
    let Some(value) = ctx.vars.get(var_name) else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "undefined variable `{var_name}` when reading token"
        ));
    };
    let Some(value_type_meta) = &value.type_meta else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "variable `{var_name}` does not have associated type metadata (expected a view type)"
        ));
    };
    let Some(value_token_value) = value_type_meta.fields.get("token") else {
        return SourceLocation::unknown().jit_error_result(&format!(
            "variable `{var_name}` is missing a `token` field (expected a view with an ordering token)"
        ));
    };
    Ok(value_token_value.clone())
}

/// Propagates type metadata changes from an inner block context to the outer block context.
pub fn update_outer_block_type_meta(
    inner_block_vars: &mut CompilerContext,
    outer_block_vars: &mut CompilerContext,
    field_name: String,
) -> () {
    let mut var_map = std::collections::HashMap::new();
    for var_name in outer_block_vars.var_keys() {
        var_map.insert(var_name.clone(), var_name.clone());
    }
    update_type_meta(inner_block_vars, outer_block_vars, &var_map, field_name);
}

/// Copies mutable type metadata fields from inner to outer context using a variable name mapping.
pub fn update_type_meta(
    inner_block_vars: &mut CompilerContext,
    outer_block_vars: &mut CompilerContext,
    outer2inner_vars: &std::collections::HashMap<String, String>,
    _field_name: String,
) -> () {
    use super::shared_types::Mutability;
    let outer_keys_ = outer_block_vars.var_keys();
    let outer_keys = outer_keys_
        .iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>();
    for outer_key in &outer_keys {
        let Some(outer_val) = outer_block_vars.vars.get(outer_key) else {
            continue;
        };
        if outer_val.mutability == Mutability::Mutable {
            if let Some(inner_key) = outer2inner_vars.get(outer_key) {
                if let Some(inner_val) = inner_block_vars.vars.get(inner_key) {
                    if inner_val.mutability == Mutability::Mutable {
                        let mut new_val = outer_val.clone();
                        new_val.type_meta = inner_val.type_meta.clone();
                        outer_block_vars.vars.insert(outer_key.clone(), new_val);
                    }
                }
            }
        }
    }
}