zyx 0.14.0

Zyx machine learning library
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
//! Intermediate representation that is close to assembly.
//! It is passed into different backends. Each backend
//! compiles IR into their own bytecode.

use super::{graph::Graph, node::ROp, scheduler::VOp, view::View};
use crate::{
    dtype::Constant,
    runtime::{
        node::{BOp, UOp},
        view::StridedDim,
    },
    shape::Axis,
    tensor::TensorId,
    DType,
};
use std::{
    collections::BTreeMap,
    fmt::{Display, Write},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum Var {
    // This is id into kernel.registers
    Id(u16),
    Const(Constant),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, bitcode::Encode, bitcode::Decode)]
pub(super) enum Scope {
    Global,
    Local,
    Register,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum IROp {
    // Loads variable from address to variable z at give offset.
    Load {
        z: Var,
        // Address is variable in addressables
        address: u16,
        // Offset is u32 var that needs to be added to address
        offset: Var,
    },
    // Stores variable from variable x to address as given offset.
    Store {
        // Address is variable in addressables
        address: u16,
        // Offset is u32 var that needs to be added to address
        offset: Var,
        x: Var,
    },
    // Assign value to register z
    Set {
        z: u16,
        value: Constant,
    },
    Unary {
        z: Var,
        x: Var,
        uop: UOp,
    },
    Binary {
        z: Var,
        x: Var,
        y: Var,
        bop: BOp,
    },
    // z = a * b + c
    MAdd {
        z: Var,
        a: Var,
        b: Var,
        c: Var,
    },
    // while id < len
    Loop {
        id: u16,
        len: usize,
    },
    EndLoop {
        id: u16,
        len: usize,
    },
    // TODO
    Barrier {
        scope: Scope,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum IRDType {
    #[cfg(feature = "half")]
    BF16(IRVec),
    #[cfg(feature = "half")]
    F16(IRVec),
    F32(IRVec),
    F64(IRVec),
    #[cfg(feature = "complex")]
    CF32(IRVec),
    #[cfg(feature = "complex")]
    CF64(IRVec),
    U8(IRVec),
    I8(IRVec),
    I16(IRVec),
    I32(IRVec),
    I64(IRVec),
    Bool,
    // For indexing
    U32,
}

// TODO add vectorization
#[allow(unused)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum IRVec {
    Scalar,
    V2,
    V4,
    V8,
    V16,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct IRKernel {
    // Index of var is it's Id
    // All addressable variables (those that use indexing for access)
    // These can be global args, local variables or variables in registers
    // scope, dtype, byte_size, read_only
    pub(super) addressables: Vec<(Scope, IRDType, usize, bool)>,
    // Registers with single value or vector stored in them
    // dtype
    pub(super) registers: Vec<IRDType>,
    pub(super) ops: Vec<IROp>,
}

impl IRVec {
    // TODO vectorization
    #[allow(unused)]
    pub(super) fn len(&self) -> usize {
        match self {
            IRVec::Scalar => 1,
            IRVec::V2 => 2,
            IRVec::V4 => 4,
            IRVec::V8 => 8,
            IRVec::V16 => 16,
        }
    }
}

impl IRDType {
    // TODO vectorization
    #[allow(unused)]
    pub(super) fn byte_size(&self) -> usize {
        match self {
            #[cfg(feature = "half")]
            IRDType::F16(v) => 2 * v.len(),
            #[cfg(feature = "half")]
            IRDType::BF16(v) => 2 * v.len(),
            IRDType::F32(v) => 4 * v.len(),
            IRDType::F64(v) => 8 * v.len(),
            #[cfg(feature = "complex")]
            IRDType::CF32(v) => 8 * v.len(),
            #[cfg(feature = "complex")]
            IRDType::CF64(v) => 16 * v.len(),
            IRDType::U8(v) => 1 * v.len(),
            IRDType::I8(v) => 1 * v.len(),
            IRDType::I16(v) => 2 * v.len(),
            IRDType::I32(v) => 4 * v.len(),
            IRDType::I64(v) => 8 * v.len(),
            IRDType::Bool => 1,
            IRDType::U32 => 4,
        }
    }

    pub(super) fn dtype(&self) -> DType {
        match self {
            #[cfg(feature = "half")]
            IRDType::BF16(_) => DType::BF16,
            #[cfg(feature = "half")]
            IRDType::F16(_) => DType::F16,
            IRDType::F32(_) => DType::F32,
            IRDType::F64(_) => DType::F64,
            #[cfg(feature = "complex")]
            IRDType::CF32(_) => DType::CF32,
            #[cfg(feature = "complex")]
            IRDType::CF64(_) => DType::CF64,
            IRDType::U8(_) => DType::U8,
            IRDType::I8(_) => DType::I8,
            IRDType::I16(_) => DType::I16,
            IRDType::I32(_) => DType::I32,
            IRDType::I64(_) => DType::I64,
            IRDType::Bool => DType::Bool,
            IRDType::U32 => panic!(),
        }
    }
}

impl From<DType> for IRDType {
    fn from(value: DType) -> Self {
        match value {
            #[cfg(feature = "half")]
            DType::BF16 => IRDType::BF16(IRVec::Scalar),
            #[cfg(feature = "half")]
            DType::F16 => IRDType::F16(IRVec::Scalar),
            DType::F32 => IRDType::F32(IRVec::Scalar),
            DType::F64 => IRDType::F64(IRVec::Scalar),
            #[cfg(feature = "complex")]
            DType::CF32 => IRDType::CF32(IRVec::Scalar),
            #[cfg(feature = "complex")]
            DType::CF64 => IRDType::CF64(IRVec::Scalar),
            DType::U8 => IRDType::U8(IRVec::Scalar),
            DType::I8 => IRDType::I8(IRVec::Scalar),
            DType::I16 => IRDType::I16(IRVec::Scalar),
            DType::I32 => IRDType::I32(IRVec::Scalar),
            DType::I64 => IRDType::I64(IRVec::Scalar),
            DType::Bool => IRDType::Bool,
        }
    }
}

impl std::fmt::Display for Scope {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Global => "g",
            Self::Local => "l",
            Self::Register => "r",
        })
    }
}

// Data structures will stay the same, but we will rewrite the implementation of to_ir function.
// The current version just does not work, particularly with padding. We also want to support vector datatypes,
// and local and register tiling. Neither of which currently works.
// Indexing also needs to be rewritten so that as much of it happens outside of the loops
// and so that it does work properly

// Returns IRKernel and order in which tensors are passed to it
pub(super) fn to_ir(kernel_ops: &[VOp], graph: &Graph) -> (IRKernel, Vec<TensorId>) {
    // What we need to calculate (outputs of this function)
    let mut addressables = Vec::new();
    let mut registers = Vec::new();
    let mut ops = Vec::new();
    let mut args = Vec::new();

    // Get reference counts for all tensors and axes
    let mut tensor_rcs: BTreeMap<TensorId, u32> = BTreeMap::new();
    let mut axes_rcs: BTreeMap<Axis, u32> = BTreeMap::new();
    for op in kernel_ops {
        match op {
            &VOp::Loop { axis, .. } => {
                axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
            }
            VOp::EndLoop => {}
            &VOp::Const { ref view, .. } => {
                // Constants are always valid, they do not need ref count
                for axis in view.used_axes() {
                    axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
                }
            }
            VOp::Load { zview, xview, .. } => {
                for axis in xview.used_axes() {
                    axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
                }
                for axis in zview.used_axes() {
                    axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
                }
            }
            VOp::Store {
                z,
                ref zview,
                ref xview,
                ..
            } => {
                for axis in xview.used_axes() {
                    axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
                }
                for axis in zview.used_axes() {
                    axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
                }
                tensor_rcs.entry(*z).and_modify(|rc| *rc += 1).or_insert(1);
            }
            VOp::Accumulator { view, .. } => {
                for axis in view.used_axes() {
                    axes_rcs.entry(axis).and_modify(|rc| *rc += 1).or_insert(1);
                }
            }
            &VOp::Move { x, .. } => {
                tensor_rcs.entry(x).and_modify(|rc| *rc += 1).or_insert(1);
            }
            // TODO should we put view into axes_rcs, or should we remove axes_rcs alltogether?
            &VOp::Unary { x, .. } => {
                tensor_rcs.entry(x).and_modify(|rc| *rc += 1).or_insert(1);
            }
            // TODO should we put view into axes_rcs, or should we remove axes_rcs alltogether?
            &VOp::Binary { x, y, .. } => {
                tensor_rcs.entry(x).and_modify(|rc| *rc += 1).or_insert(1);
                tensor_rcs.entry(y).and_modify(|rc| *rc += 1).or_insert(1);
            }
            VOp::Barrier { .. } => {}
        }
    }
    let tensor_rcs = tensor_rcs;

    // Allocate register space for axes.
    // This way axes also have the same id in registers.
    for (_, &rc) in &axes_rcs {
        registers.push((IRDType::U32, rc));
    }

    // Maps from tensors to registers
    let mut register_map: BTreeMap<TensorId, Var> = BTreeMap::new();
    // Map from addressables to registers
    let mut addressables_map: BTreeMap<(TensorId, Scope), u16> = BTreeMap::new();

    // Declare global arguments
    for op in kernel_ops {
        match op {
            &VOp::Load {
                x,
                xscope,
                ref xview,
                ..
            } => {
                if xscope == Scope::Global && !addressables_map.contains_key(&(x, xscope)) {
                    args.push(x);
                    let dtype = graph.dtype(x).ir_dtype();
                    addressables.push((xscope, dtype, xview.original_numel(), true));
                    let id = (addressables.len() - 1) as u16;
                    addressables_map.insert((x, xscope), id);
                }
            }
            &VOp::Store {
                z,
                zscope,
                ref zview,
                xscope,
                ..
            } => {
                if zscope == Scope::Global {
                    let dtype = graph.dtype(z).ir_dtype();
                    addressables_map
                        .entry((z, zscope))
                        .and_modify(|&mut id| {
                            assert_eq!(xscope, Scope::Register);
                            // set it to read-write
                            addressables[id as usize].3 = false;
                        })
                        .or_insert_with(|| {
                            args.push(z);
                            addressables.push((zscope, dtype, zview.original_numel(), false));
                            (addressables.len() - 1) as u16
                        });
                }
            }
            _ => {}
        }
    }

    // TODO Declare local variables
    for op in kernel_ops {
        match op {
            &VOp::Load {
                x,
                xscope,
                ref xview,
                ..
            } => {
                if xscope == Scope::Local && !addressables_map.contains_key(&(x, xscope)) {
                    let dtype = graph.dtype(x).ir_dtype();
                    addressables.push((xscope, dtype, xview.original_numel(), true));
                    let id = (addressables.len() - 1) as u16;
                    addressables_map.insert((x, xscope), id);
                }
            }
            &VOp::Store {
                zscope,
                ..
            } => {
                if zscope == Scope::Local {
                    todo!()
                }
            }
            _ => {}
        }
    }

    // Declare accumulators
    // TODO if we have multiple accumulators with the same size, we must reuse them
    for op in kernel_ops {
        if let &VOp::Accumulator { z, ref view, .. } = op {
            let dtype = graph.dtype(z).ir_dtype();
            addressables.push((Scope::Register, dtype, view.original_numel(), false));
            let id = (addressables.len() - 1) as u16;
            addressables_map.insert((z, Scope::Register), id);
        }
    }

    // Actual transpiling from Kernel to IRKernel
    let mut loops = Vec::new();
    for op in kernel_ops {
        //println!("{op}");
        match op {
            &VOp::Loop { axis, len } => {
                // Axis always maps to register ids
                let id = axis as u16;
                ops.push(IROp::Loop { id, len });
                loops.push((id, len));
            }
            VOp::EndLoop => {
                if let Some((id, len)) = loops.pop() {
                    ops.push(IROp::EndLoop { id, len });
                }
            }
            &VOp::Const { z, value, ref view } => {
                let var = if view.requires_conditional_padding() {
                    //println!("View: {view}");
                    let View::Padded(dims, padding) = view else {
                        panic!()
                    };
                    let padding_condition = get_empty_register(&mut registers, IRDType::Bool, 1);
                    ops.push(IROp::Set {
                        z: padding_condition,
                        value: Constant::Bool(true),
                    });
                    let padding_condition = Var::Id(padding_condition);
                    let mut pc = String::new();
                    for StridedDim { axis, .. } in dims {
                        if let Some((axes, (lp, rp))) = padding
                            .iter()
                            .find(|(axes, _)| axes.iter().max().unwrap() == axis)
                        {
                            if *lp > 0 || *rp > 0 {
                                let mut idx = String::new();
                                let mut st = 1;
                                let mut dim = 1;
                                for axis in axes.iter().rev() {
                                    idx = format!("i{axis}*{st}+{idx}");
                                    st *= dims[*axis].dim;
                                    dim *= dims[*axis].dim;
                                }
                                idx.pop();
                                if *lp > 0 {
                                    pc += &format!("{idx} < {lp} || ");
                                }
                                if *rp > 0 {
                                    pc += &format!("{idx} > {} || ", dim as isize - rp - 1);
                                }

                                let idx_id = get_empty_register(&mut registers, IRDType::U32, 1);
                                ops.push(IROp::Set {
                                    z: idx_id,
                                    value: Constant::U32(0),
                                });
                                let idx = Var::Id(idx_id);
                                let mut st = 1;
                                let mut dim = 1;
                                for axis in axes.iter().rev() {
                                    ops.push(IROp::MAdd {
                                        z: idx,
                                        a: Var::Id(*axis as u16),
                                        b: Var::Const(Constant::U32(st as u32)),
                                        c: idx,
                                    });
                                    st *= dims[*axis].dim;
                                    dim *= dims[*axis].dim;
                                }
                                if *lp > 0 {
                                    let temp = Var::Id(get_empty_register(
                                        &mut registers,
                                        IRDType::Bool,
                                        0,
                                    ));
                                    ops.push(IROp::Binary {
                                        z: temp,
                                        x: idx,
                                        y: Var::Const(Constant::U32(*lp as u32 - 1)),
                                        bop: BOp::Cmpgt,
                                    });
                                    ops.push(IROp::Binary {
                                        z: padding_condition,
                                        x: temp,
                                        y: padding_condition,
                                        bop: BOp::And,
                                    });
                                }
                                if *rp > 0 {
                                    let temp = Var::Id(get_empty_register(
                                        &mut registers,
                                        IRDType::Bool,
                                        0,
                                    ));
                                    ops.push(IROp::Binary {
                                        z: temp,
                                        x: idx,
                                        y: Var::Const(Constant::U32((dim as isize - *rp) as u32)),
                                        bop: BOp::Cmplt,
                                    });
                                    ops.push(IROp::Binary {
                                        z: padding_condition,
                                        x: temp,
                                        y: padding_condition,
                                        bop: BOp::And,
                                    });
                                }
                                registers[idx_id as usize].1 = 0;
                            }
                        }
                    }
                    //println!("Padding condition: {pc}");
                    // Nullify z if padding condition is false (if there is padding at that index)
                    let var = Var::Id(get_empty_register(
                        &mut registers,
                        value.dtype().ir_dtype(),
                        tensor_rcs[&z],
                    ));
                    ops.push(IROp::Binary {
                        z: var,
                        x: padding_condition,
                        y: Var::Const(value),
                        bop: BOp::Mul,
                    });
                    if let Var::Id(pc) = padding_condition {
                        registers[pc as usize].1 = 0;
                    }
                    var
                } else {
                    Var::Const(value)
                };
                register_map.insert(z, var);
            }
            &VOp::Load {
                z,
                zscope,
                ref zview,
                x,
                xscope,
                ref xview,
            } => match (zscope, xscope) {
                (Scope::Local, Scope::Global) => {
                    let dtype = graph.dtype(z).ir_dtype();
                    let id = get_empty_register(&mut registers, dtype, tensor_rcs[&z]);
                    let address = addressables_map[&(x, xscope)];
                    load_indexed(Var::Id(id), address, xview, &mut registers, &mut ops);
                    register_map.insert(z, Var::Id(id));
                    let address = addressables_map[&(z, zscope)];
                    store_indexed(address, Var::Id(id), zview, &mut registers, &mut ops);
                }
                (Scope::Register, Scope::Local) => {
                    let dtype = graph.dtype(z).ir_dtype();
                    let id = get_empty_register(&mut registers, dtype, tensor_rcs[&z]);
                    let address = addressables_map[&(x, xscope)];
                    load_indexed(Var::Id(id), address, xview, &mut registers, &mut ops);
                    register_map.insert(z, Var::Id(id));
                }
                (Scope::Register, Scope::Global) => {
                    let dtype = graph.dtype(z).ir_dtype();
                    let id = get_empty_register(&mut registers, dtype, tensor_rcs[&z]);
                    let address = addressables_map[&(x, xscope)];
                    load_indexed(Var::Id(id), address, xview, &mut registers, &mut ops);
                    register_map.insert(z, Var::Id(id));
                }
                _ => panic!("Invalid load scopes"),
            },
            &VOp::Store {
                z,
                zscope,
                ref zview,
                xscope,
                ref xview,
            } => match (zscope, xscope) {
                (Scope::Local, Scope::Register) => {
                    todo!()
                }
                (Scope::Global, Scope::Register) => {
                    let address = addressables_map[&(z, zscope)];
                    let dtype = graph.dtype(z).ir_dtype();
                    let x = if let Some(&address) = addressables_map.get(&(z, Scope::Register)) {
                        let var = Var::Id(get_empty_register(&mut registers, dtype, 0));
                        load_indexed(var, address, xview, &mut registers, &mut ops);
                        var
                    } else {
                        register_map[&z]
                    };
                    store_indexed(address, x, zview, &mut registers, &mut ops);
                }
                _ => panic!("Invalid store scopes"),
            },
            &VOp::Accumulator { z, rop, ref view } => {
                let dtype = graph.dtype(z).ir_dtype();
                store_indexed(
                    addressables_map[&(z, Scope::Register)],
                    Var::Const(match rop {
                        ROp::Sum => dtype.dtype().zero_constant(),
                        ROp::Max => dtype.dtype().min_constant(),
                    }),
                    view,
                    &mut registers,
                    &mut ops,
                );
            }
            &VOp::Move { z, x, .. } => {
                register_map.insert(z, register_map[&x]);
            }
            &VOp::Unary { z, x, uop, view: _ } => {
                // TODO unary can have view, but this will probably be only used for vectorization
                if let Var::Const(value) = register_map[&x] {
                    register_map.insert(z, Var::Const(value.unary(uop)));
                } else {
                    let dtype = graph.dtype(z).ir_dtype();
                    let id = get_empty_register(&mut registers, dtype, tensor_rcs[&z]);
                    let zvar = Var::Id(id);
                    register_map.insert(z, zvar);
                    ops.push(IROp::Unary {
                        z: zvar,
                        x: register_map[&x],
                        uop,
                    });
                    if let Var::Id(id) = register_map[&x] {
                        registers[id as usize].1 -= 1u32;
                    }
                }
            }
            &VOp::Binary {
                z,
                ref zview,
                x,
                ref xview,
                y,
                ref yview,
                bop,
            } => {
                // TODO binary can have view, for example if it is accumulator
                let dtype = graph.dtype(z).ir_dtype();
                let id = if let Some(&Var::Id(id)) = register_map.get(&z) {
                    id
                } else {
                    get_empty_register(&mut registers, dtype, tensor_rcs[&z])
                };
                let zvar = Var::Id(id);
                register_map.insert(z, zvar);

                let bin_op = IROp::Binary {
                    z: zvar,
                    x: if let Some(&address) = addressables_map.get(&(x, Scope::Register)) {
                        let var = Var::Id(get_empty_register(&mut registers, dtype, 0));
                        load_indexed(var, address, xview, &mut registers, &mut ops);
                        var
                    } else {
                        register_map[&x]
                    },
                    y: if let Some(&address) = addressables_map.get(&(y, Scope::Register)) {
                        let var = Var::Id(get_empty_register(&mut registers, dtype, 0));
                        load_indexed(var, address, yview, &mut registers, &mut ops);
                        var
                    } else {
                        register_map[&y]
                    },
                    bop,
                };
                ops.push(bin_op);

                if let Var::Id(id) = register_map[&x] {
                    registers[id as usize].1 -= 1u32;
                }
                if let Var::Id(id) = register_map[&y] {
                    registers[id as usize].1 -= 1u32;
                }

                if let Some(&address) = addressables_map.get(&(z, Scope::Register)) {
                    store_indexed(address, zvar, zview, &mut registers, &mut ops);
                }
            }
            &VOp::Barrier { scope } => {
                ops.push(IROp::Barrier { scope });
            }
        }
    }

    while let Some((id, len)) = loops.pop() {
        ops.push(IROp::EndLoop { id, len });
    }

    // TODO Optimize by deduplicating ops (namely indices) and moving them before loops

    //for op in &ops { println!("{op:?}"); }

    (
        IRKernel {
            addressables,
            registers: registers.into_iter().map(|(dtype, _)| dtype).collect(),
            ops,
        },
        args,
    )
}

fn store_indexed(
    address: u16,
    x: Var,
    view: &View,
    registers: &mut Vec<(IRDType, u32)>,
    ops: &mut Vec<IROp>,
) {
    let numel = view.original_numel();
    match view {
        View::None => {
            ops.push(IROp::Store {
                address,
                x,
                offset: Var::Const(Constant::U32(0)),
            });
        }
        View::Strided(dims) => {
            let offset = get_empty_register(registers, IRDType::U32, 0);
            ops.push(IROp::Set {
                z: offset,
                value: Constant::U32(0),
            });
            for StridedDim { axis, stride, .. } in dims {
                if *stride != 0 && *stride != numel {
                    ops.push(IROp::MAdd {
                        z: Var::Id(offset),
                        a: Var::Id(*axis as u16),
                        b: Var::Const(Constant::U32(*stride as u32)),
                        c: Var::Id(offset),
                    });
                }
            }
            ops.push(IROp::Store {
                address,
                x,
                offset: Var::Id(offset),
            });
        }
        View::Padded(_, _) => todo!(),
    }
}

fn load_indexed(
    z: Var,
    address: u16,
    view: &View,
    registers: &mut Vec<(IRDType, u32)>,
    ops: &mut Vec<IROp>,
) {
    let numel = view.original_numel();
    match view {
        View::None => {
            ops.push(IROp::Load {
                z,
                address,
                offset: Var::Const(Constant::U32(0)),
            });
        }
        View::Strided(dims) => {
            let offset = get_empty_register(registers, IRDType::U32, 0);
            ops.push(IROp::Set {
                z: offset,
                value: Constant::U32(0),
            });
            for StridedDim { axis, stride, .. } in dims {
                if *stride != 0 && *stride != numel {
                    ops.push(IROp::MAdd {
                        z: Var::Id(offset),
                        a: Var::Id(*axis as u16),
                        b: Var::Const(Constant::U32(*stride as u32)),
                        c: Var::Id(offset),
                    });
                }
            }
            ops.push(IROp::Load {
                z,
                address,
                offset: Var::Id(offset),
            });
        }
        View::Padded(dims, padding) => {
            //println!("Loading indexed into {z:?} from p{address} with {view}");
            let offset = get_empty_register(registers, IRDType::U32, 1);
            ops.push(IROp::Set {
                z: offset,
                value: Constant::U32(0),
            });
            let offset = Var::Id(offset);
            for StridedDim { axis, stride, .. } in dims {
                if let Some((_, (lp, _))) = padding
                    .iter()
                    .find(|(axes, _)| axes.iter().max().unwrap() == axis)
                {
                    let t = if *lp > 0 {
                        let t = Var::Id(get_empty_register(registers, IRDType::U32, 0));
                        ops.push(IROp::Binary {
                            z: t,
                            x: Var::Id(*axis as u16),
                            y: Var::Const(Constant::U32(*lp as u32)),
                            bop: BOp::Sub,
                        });
                        t
                    } else if *lp < 0 {
                        let lp = -lp;
                        let t = Var::Id(get_empty_register(registers, IRDType::U32, 0));
                        ops.push(IROp::Binary {
                            z: t,
                            x: Var::Id(*axis as u16),
                            y: Var::Const(Constant::U32(lp as u32)),
                            bop: BOp::Add,
                        });
                        t
                    } else {
                        Var::Id(*axis as u16)
                    };
                    if *stride != 0 {
                        //&& *stride != numel {
                        ops.push(IROp::MAdd {
                            z: offset,
                            a: t,
                            b: Var::Const(Constant::U32(*stride as u32)),
                            c: offset,
                        });
                    }
                } else {
                    if *stride != 0 {
                        //&& *stride != numel {
                        ops.push(IROp::MAdd {
                            z: offset,
                            a: Var::Id(*axis as u16),
                            b: Var::Const(Constant::U32(*stride as u32)),
                            c: offset,
                        });
                    }
                }
            }
            let padding_condition = get_empty_register(registers, IRDType::Bool, 1);
            ops.push(IROp::Set {
                z: padding_condition,
                value: Constant::Bool(true),
            });
            let padding_condition = Var::Id(padding_condition);
            for StridedDim { axis, .. } in dims {
                if let Some((axes, (lp, rp))) = padding
                    .iter()
                    .find(|(axes, _)| axes.iter().max().unwrap() == axis)
                {
                    let idx_id = get_empty_register(registers, IRDType::U32, 1);
                    ops.push(IROp::Set {
                        z: idx_id,
                        value: Constant::U32(0),
                    });
                    let idx = Var::Id(idx_id);
                    let mut st = 1;
                    let mut dim = 1;
                    for axis in axes.iter().rev() {
                        ops.push(IROp::MAdd {
                            z: idx,
                            a: Var::Id(*axis as u16),
                            b: Var::Const(Constant::U32(st as u32)),
                            c: idx,
                        });
                        st *= dims[*axis].dim;
                        dim *= dims[*axis].dim;
                    }
                    if *lp > 0 {
                        let temp = Var::Id(get_empty_register(registers, IRDType::Bool, 0));
                        ops.push(IROp::Binary {
                            z: temp,
                            x: idx,
                            y: Var::Const(Constant::U32(*lp as u32 - 1)),
                            bop: BOp::Cmpgt,
                        });
                        ops.push(IROp::Binary {
                            z: padding_condition,
                            x: temp,
                            y: padding_condition,
                            bop: BOp::And,
                        });
                    }
                    if *rp > 0 {
                        let temp = Var::Id(get_empty_register(registers, IRDType::Bool, 0));
                        ops.push(IROp::Binary {
                            z: temp,
                            x: idx,
                            y: Var::Const(Constant::U32((dim as isize - *rp) as u32)),
                            bop: BOp::Cmplt,
                        });
                        ops.push(IROp::Binary {
                            z: padding_condition,
                            x: temp,
                            y: padding_condition,
                            bop: BOp::And,
                        });
                    }
                    registers[idx_id as usize].1 = 0;
                }
            }
            ops.push(IROp::Binary {
                z: offset,
                x: padding_condition,
                y: offset,
                bop: BOp::Mul,
            });
            ops.push(IROp::Load { z, address, offset });
            if let Var::Id(offset) = offset {
                registers[offset as usize].1 = 0;
            }
            // Nullify z if padding condition is false (if there is padding at that index)
            ops.push(IROp::Binary {
                z,
                x: padding_condition,
                y: z,
                bop: BOp::Mul,
            });
            if let Var::Id(pc) = padding_condition {
                registers[pc as usize].1 = 0;
            }
        }
    }
}

fn get_empty_register(registers: &mut Vec<(IRDType, u32)>, ir_dtype: IRDType, rc: u32) -> u16 {
    if let Some(id) = registers.iter().enumerate().find_map(|(id, &(d, c))| {
        if c == 0 && ir_dtype == d {
            Some(id as u16)
        } else {
            None
        }
    }) {
        registers[id as usize] = (ir_dtype, rc);
        id
    } else {
        registers.push((ir_dtype, rc));
        (registers.len() - 1) as u16
    }
}

impl IRKernel {
    pub(super) fn debug(&self) {
        for op in &self.ops {
            println!("{op:?}");
        }
    }
}

impl Display for IRVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IRVec::Scalar => f.write_str(""),
            IRVec::V2 => f.write_char('2'),
            IRVec::V4 => f.write_char('4'),
            IRVec::V8 => f.write_char('8'),
            IRVec::V16 => f.write_str("16"),
        }
    }
}

impl DType {
    pub(super) fn ir_dtype(&self) -> IRDType {
        match self {
            DType::F32 => IRDType::F32(IRVec::Scalar),
            DType::F64 => IRDType::F64(IRVec::Scalar),
            DType::U8 => IRDType::U8(IRVec::Scalar),
            DType::I8 => IRDType::I8(IRVec::Scalar),
            DType::I16 => IRDType::I16(IRVec::Scalar),
            DType::I32 => IRDType::I32(IRVec::Scalar),
            DType::I64 => IRDType::I64(IRVec::Scalar),
            DType::Bool => IRDType::Bool,
        }
    }
}