cubecl-core 0.11.0-pre.1

CubeCL core create
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
use alloc::vec;
use core::ops::{Deref, DerefMut};

use alloc::boxed::Box;

use crate::{self as cubecl, unexpanded};
use cubecl::prelude::*;
use cubecl_ir::{
    AggregateKind, Branch, ElemType, FloatKind, Instruction, MetadataKind, Operation, RangeLoop,
    SliceMetadata, Value, VectorSize,
};

pub type SliceExpand<T> = NativeExpand<[T]>;

#[derive(Clone, Copy)]
pub struct ReadOnly;
#[derive(Clone, Copy)]
pub struct ReadWrite;

pub trait SliceVisibility: Clone + Copy + Send + Sync + 'static {}

impl SliceVisibility for ReadOnly {}

impl SliceVisibility for ReadWrite {}

impl<E: CubePrimitive> SliceExpand<E> {
    pub fn __extract_list(&self, scope: &Scope) -> Value {
        let Type::Aggregate(AggregateKind::Ptr { inner_ty, .. }) = self.expand.ty else {
            unreachable!("Should be slice aggregate")
        };
        scope.extract_field(self.expand, *inner_ty, SliceMetadata::LIST)
    }

    pub fn __extract_offset(&self, scope: &Scope) -> NativeExpand<usize> {
        let ty = usize::__expand_as_type(scope);
        let field = scope.extract_field(self.expand, ty, SliceMetadata::OFFSET);
        field.into()
    }

    pub fn __extract_length(&self, scope: &Scope) -> NativeExpand<usize> {
        let ty = usize::__expand_as_type(scope);
        let field = scope.extract_field(self.expand, ty, SliceMetadata::LENGTH);
        field.into()
    }
}

pub trait SliceVectorExt<E: Scalar, N: Size> {
    fn with_vector_size<N2: Size>(&self) -> &[Vector<E, N2>] {
        unexpanded!()
    }
    fn with_vector_size_mut<N2: Size>(&mut self) -> &mut [Vector<E, N2>] {
        unexpanded!()
    }
    fn __expand_with_vector_size<'infer, N2: Size>(
        scope: &Scope,
        this: &'infer SliceExpand<Vector<E, N>>,
    ) -> &'infer SliceExpand<Vector<E, N2>> {
        this.__expand_with_vector_size_method(scope)
    }
    fn __expand_with_vector_size_mut<'infer, N2: Size>(
        scope: &Scope,
        this: &'infer mut SliceExpand<Vector<E, N>>,
    ) -> &'infer mut SliceExpand<Vector<E, N2>> {
        this.__expand_with_vector_size_mut_method(scope)
    }
}

impl<E: Scalar, N: Size> SliceVectorExt<E, N> for [Vector<E, N>] {}
impl<E: Scalar, N: Size> SliceExpand<Vector<E, N>> {
    pub fn __expand_with_vector_size_method<'infer, N2: Size>(
        &'infer self,
        scope: &Scope,
    ) -> &'infer SliceExpand<Vector<E, N2>> {
        let slice = self.with_vector_size_inner::<N2>(scope);
        scope.create_kernel_ref(slice)
    }

    pub fn __expand_with_vector_size_mut_method<'infer, N2: Size>(
        &'infer mut self,
        scope: &Scope,
    ) -> &'infer mut SliceExpand<Vector<E, N2>> {
        let slice = self.with_vector_size_inner::<N2>(scope);
        scope.create_kernel_ref(slice)
    }
}

impl<E: Scalar, N: Size> SliceExpand<Vector<E, N>> {
    fn with_vector_size_inner<N2: Size>(&self, scope: &Scope) -> SliceExpand<Vector<E, N2>> {
        let vector_size = N2::__expand_value(scope);
        let list = self.__extract_list(scope);
        let item = list.value_type();

        let length = self.__extract_length(scope);
        let offset = self.__extract_offset(scope);

        let current = list.ty.vector_size();

        if vector_size == item.vector_size() {
            return self.expand.into();
        }

        let mut new_ptr = list;
        new_ptr.ty = new_ptr.ty.with_vector_size(vector_size);

        if current < vector_size {
            let ratio = vector_size / current;
            let offset = offset.__expand_div_method(scope, ratio.into());
            let length = length.__expand_div_method(scope, ratio.into());
            from_raw_parts(scope, new_ptr, offset, length)
        } else {
            let ratio = current / vector_size;
            let offset = offset.__expand_mul_method(scope, ratio.into());
            let length = length.__expand_mul_method(scope, ratio.into());
            from_raw_parts(scope, new_ptr, offset, length)
        }
    }
}

pub trait SliceExt<E: CubePrimitive> {
    /// Returns the same slice, but with the type reinterpreted as `Vector`.
    /// Preserves existing vector size of the primitive.
    fn as_vectorized(&self) -> &[Vector<E::Scalar, E::Size>] {
        unexpanded!()
    }

    /// Returns the same slice, but with the type reinterpreted as `Vector`.
    /// Preserves existing vector size of the primitive.
    fn as_vectorized_mut(&mut self) -> &mut [Vector<E::Scalar, E::Size>] {
        unexpanded!()
    }

    /// Downcast the slice to the given type and panic if the type isn't the same.
    ///
    /// This function should only be used to satisfy the Rust type system, when two generic
    /// types are supposed to be the same.
    fn downcast<T: CubePrimitive>(&self) -> &[T] {
        unexpanded!()
    }

    /// Downcast the slice to the given type and panic if the type isn't the same.
    ///
    /// This function should only be used to satisfy the Rust type system, when two generic
    /// types are supposed to be the same.
    fn downcast_mut<T: CubePrimitive>(&mut self) -> &mut [T] {
        unexpanded!()
    }

    /// Unsafely downcast the slice to the given type and panic if the type isn't the same.
    ///
    /// # Safety
    /// This function converts unsafely, and should only be used for temporary storage with a dummy
    /// type (i.e. `ReinterpretSlice`)
    unsafe fn downcast_unchecked<T: CubePrimitive>(&self) -> &[T] {
        unexpanded!()
    }

    /// Unsafely downcast the slice to the given type and panic if the type isn't the same.
    ///
    /// # Safety
    /// This function converts unsafely, and should only be used for temporary storage with a dummy
    /// type (i.e. `ReinterpretSlice`)
    unsafe fn downcast_mut_unchecked<T: CubePrimitive>(&mut self) -> &mut [T] {
        unexpanded!()
    }

    /// Unsafely cast an immutable slice to a mutable one.
    ///
    /// # Safety
    /// This is safe in practice, but breaks semantics. Should only be used if absolutely necessary.
    /// May cause problems if an immutable input is reinterpreted as mutable.
    #[allow(clippy::mut_from_ref)]
    unsafe fn as_mut_unchecked(&self) -> &mut [E] {
        unexpanded!()
    }

    /// Convert the slice to a start pointer, without any bounds checks.
    ///
    /// # Safety
    /// See [`get_unchecked`]([E]::get_unchecked)
    unsafe fn as_ptr_unchecked(&self) -> *const E {
        unexpanded!()
    }

    /// Convert the slice to a mutable start pointer, without any bounds checks.
    ///
    /// # Safety
    /// See [`get_unchecked_mut`]([E]::get_unchecked_mut)
    unsafe fn as_mut_ptr_unchecked(&mut self) -> *mut E {
        unexpanded!()
    }

    /// Convert to an owned boxed slice. This is very unsafe as it completely erases the lifetime.
    /// Only use it for global kernel inputs, which have a static lifetime.
    ///
    /// # Safety
    /// Erases the lifetime. Only use when an owned representation is absolutely needed.
    unsafe fn as_boxed_unchecked(&self) -> Box<[E]> {
        unexpanded!()
    }

    fn __expand_as_vectorized<'infer>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer SliceExpand<Vector<E::Scalar, E::Size>>;

    fn __expand_as_vectorized_mut<'infer>(
        scope: &Scope,
        this: &'infer mut SliceExpand<E>,
    ) -> &'infer mut SliceExpand<Vector<E::Scalar, E::Size>>;

    fn __expand_downcast<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer SliceExpand<T>;

    fn __expand_downcast_mut<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer mut SliceExpand<E>,
    ) -> &'infer mut SliceExpand<T>;

    fn __expand_downcast_unchecked<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer SliceExpand<T>;

    fn __expand_downcast_mut_unchecked<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer mut SliceExpand<E>,
    ) -> &'infer mut SliceExpand<T>;

    fn __expand_as_ptr(scope: &Scope, this: &SliceExpand<E>) -> *const NativeExpand<E>;

    fn __expand_as_mut_ptr(scope: &Scope, this: &mut SliceExpand<E>) -> *mut NativeExpand<E>;

    #[doc(hidden)]
    unsafe fn __expand_as_ptr_unchecked(
        scope: &Scope,
        this: &SliceExpand<E>,
    ) -> *const NativeExpand<E>;

    #[doc(hidden)]
    unsafe fn __expand_as_mut_ptr_unchecked(
        scope: &Scope,
        this: &mut SliceExpand<E>,
    ) -> *mut NativeExpand<E>;

    #[allow(clippy::mut_from_ref)]
    #[doc(hidden)]
    unsafe fn __expand_as_mut_unchecked<'infer>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer mut SliceExpand<E>;

    #[doc(hidden)]
    unsafe fn __expand_as_boxed_unchecked(
        scope: &Scope,
        this: &SliceExpand<E>,
    ) -> NativeExpand<Box<[E]>>;
}

impl<E: CubePrimitive> SliceExt<E> for [E] {
    fn __expand_as_vectorized<'infer>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer SliceExpand<Vector<E::Scalar, E::Size>> {
        this.__expand_as_vectorized_method(scope)
    }

    fn __expand_as_vectorized_mut<'infer>(
        scope: &Scope,
        this: &'infer mut SliceExpand<E>,
    ) -> &'infer mut SliceExpand<Vector<E::Scalar, E::Size>> {
        this.__expand_as_vectorized_mut_method(scope)
    }

    fn __expand_downcast<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer SliceExpand<T> {
        this.__expand_downcast_method::<T>(scope)
    }

    fn __expand_downcast_mut<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer mut SliceExpand<E>,
    ) -> &'infer mut SliceExpand<T> {
        this.__expand_downcast_mut_method::<T>(scope)
    }

    fn __expand_downcast_unchecked<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer SliceExpand<T> {
        this.__expand_downcast_unchecked_method::<T>(scope)
    }

    fn __expand_downcast_mut_unchecked<'infer, T: CubePrimitive>(
        scope: &Scope,
        this: &'infer mut SliceExpand<E>,
    ) -> &'infer mut SliceExpand<T> {
        this.__expand_downcast_mut_unchecked_method::<T>(scope)
    }

    fn __expand_as_ptr(scope: &Scope, this: &SliceExpand<E>) -> *const NativeExpand<E> {
        this.__expand_as_ptr_method(scope)
    }

    fn __expand_as_mut_ptr(scope: &Scope, this: &mut SliceExpand<E>) -> *mut NativeExpand<E> {
        this.__expand_as_mut_ptr_method(scope)
    }

    unsafe fn __expand_as_ptr_unchecked(
        scope: &Scope,
        this: &SliceExpand<E>,
    ) -> *const NativeExpand<E> {
        unsafe { this.__expand_as_ptr_unchecked_method(scope) }
    }

    unsafe fn __expand_as_mut_ptr_unchecked(
        scope: &Scope,
        this: &mut SliceExpand<E>,
    ) -> *mut NativeExpand<E> {
        unsafe { this.__expand_as_mut_ptr_unchecked_method(scope) }
    }

    unsafe fn __expand_as_mut_unchecked<'infer>(
        scope: &Scope,
        this: &'infer SliceExpand<E>,
    ) -> &'infer mut SliceExpand<E> {
        this.__expand_as_mut_unchecked_method(scope)
    }

    unsafe fn __expand_as_boxed_unchecked(
        scope: &Scope,
        this: &SliceExpand<E>,
    ) -> NativeExpand<Box<[E]>> {
        unsafe { this.__expand_as_boxed_unchecked_method(scope) }
    }
}

impl<E: CubePrimitive> SliceExpand<E> {
    pub fn __expand_as_vectorized_method(
        &self,
        _: &Scope,
    ) -> &SliceExpand<Vector<E::Scalar, E::Size>> {
        unsafe { self.as_type_ref_unchecked() }
    }

    pub fn __expand_as_vectorized_mut_method(
        &mut self,
        _: &Scope,
    ) -> &mut SliceExpand<Vector<E::Scalar, E::Size>> {
        unsafe { self.as_type_mut_unchecked() }
    }

    pub fn __expand_downcast_method<T: CubePrimitive>(&self, scope: &Scope) -> &SliceExpand<T> {
        if T::__expand_as_type(scope) != E::__expand_as_type(scope) && !is_tf32::<E, T>(scope) {
            let elems = [
                T::__expand_as_type(scope).elem_type(),
                E::__expand_as_type(scope).elem_type(),
            ];
            let is_flex32_cast = elems.contains(&ElemType::Float(FloatKind::F32))
                && elems.contains(&ElemType::Float(FloatKind::Flex32));

            if !is_flex32_cast {
                panic!(
                    "Downcast should only be used to satisfy the Rust type system.
Expected types to be the same, got [{}, {}]",
                    elems[0], elems[1]
                )
            }
        }

        self.__expand_downcast_unchecked_method(scope)
    }

    pub fn __expand_downcast_mut_method<T: CubePrimitive>(
        &mut self,
        scope: &Scope,
    ) -> &mut SliceExpand<T> {
        if T::__expand_as_type(scope) != E::__expand_as_type(scope) && !is_tf32::<E, T>(scope) {
            let elems = [
                T::__expand_as_type(scope).elem_type(),
                E::__expand_as_type(scope).elem_type(),
            ];
            let is_flex32_cast = elems.contains(&ElemType::Float(FloatKind::F32))
                && elems.contains(&ElemType::Float(FloatKind::Flex32));

            if !is_flex32_cast {
                panic!(
                    "Downcast should only be used to satisfy the Rust type system.
Expected types to be the same, got [{}, {}]",
                    elems[0], elems[1]
                )
            }
        }

        self.__expand_downcast_mut_unchecked_method(scope)
    }

    #[doc(hidden)]
    pub fn __expand_downcast_unchecked_method<T: CubePrimitive>(
        &self,
        _: &Scope,
    ) -> &SliceExpand<T> {
        unsafe { self.as_type_ref_unchecked() }
    }

    #[doc(hidden)]
    pub fn __expand_downcast_mut_unchecked_method<T: CubePrimitive>(
        &mut self,
        _: &Scope,
    ) -> &mut SliceExpand<T> {
        unsafe { self.as_type_mut_unchecked() }
    }

    pub fn __expand_as_ptr_method(&self, scope: &Scope) -> *const NativeExpand<E> {
        self.__expand_index_method(scope, NativeExpand::<usize>::from_lit(scope, 0))
    }

    pub fn __expand_as_mut_ptr_method(&mut self, scope: &Scope) -> *mut NativeExpand<E> {
        self.__expand_index_mut_method(scope, NativeExpand::<usize>::from_lit(scope, 0))
    }

    #[doc(hidden)]
    pub unsafe fn __expand_as_ptr_unchecked_method(&self, scope: &Scope) -> *const NativeExpand<E> {
        unsafe {
            self.__expand_get_unchecked_method(scope, NativeExpand::<usize>::from_lit(scope, 0))
        }
    }

    #[doc(hidden)]
    pub unsafe fn __expand_as_mut_ptr_unchecked_method(
        &mut self,
        scope: &Scope,
    ) -> *mut NativeExpand<E> {
        unsafe {
            self.__expand_get_unchecked_mut_method(scope, NativeExpand::<usize>::from_lit(scope, 0))
        }
    }

    pub fn __expand_as_mut_unchecked_method(&self, scope: &Scope) -> &mut SliceExpand<E> {
        scope.create_kernel_ref(self.expand.into())
    }

    #[doc(hidden)]
    pub unsafe fn __expand_as_boxed_unchecked_method(&self, _: &Scope) -> NativeExpand<Box<[E]>> {
        self.expand.into()
    }
}

pub fn from_raw_parts<E: CubePrimitive>(
    scope: &Scope,
    list: Value,
    offset: NativeExpand<usize>,
    length: NativeExpand<usize>,
) -> SliceExpand<E> {
    let ty = Type::Aggregate(AggregateKind::ptr(list.ty, MetadataKind::Slice));
    let out = scope.create_value(ty);
    scope.register(Instruction::new(
        Operation::ConstructAggregate(vec![list, offset.expand, length.expand]),
        out,
    ));
    out.into()
}

impl<E: CubePrimitive> SliceExpand<E> {
    /// Get the length of the slice.
    pub fn __expand_len_method(&self, scope: &Scope) -> NativeExpand<usize> {
        self.__extract_length(scope)
    }
    /// Returns true if the slice is empty.
    pub fn is_empty(&self, scope: &Scope) -> NativeExpand<bool> {
        self.__extract_length(scope)
            .__expand_eq_method(scope, &0usize.into_expand(scope))
    }
}

impl<E: CubePrimitive> CubeType for [E] {
    type ExpandType = SliceExpand<E>;
}

impl<E: CubePrimitive> CubeType for Box<[E]> {
    type ExpandType = NativeExpand<Box<[E]>>;
}

impl<E> Deref for NativeExpand<Box<[E]>> {
    type Target = NativeExpand<[E]>;

    fn deref(&self) -> &Self::Target {
        unsafe { self.as_type_ref_unchecked() }
    }
}

impl<E> DerefMut for NativeExpand<Box<[E]>> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { self.as_type_mut_unchecked() }
    }
}

macro_rules! impl_expand_traits {
    ($generic: ident, $ty: ty) => {
        impl<$generic: CubePrimitive> AsMutExpand for $ty {
            fn __expand_ref_mut_method(&mut self, _: &Scope) -> &mut Self {
                self
            }
        }

        impl<$generic: CubePrimitive> IntoMut for $ty {
            fn into_mut(self, _scope: &Scope) -> Self {
                self
            }
        }
    };
}

impl_expand_traits!(E, SliceExpand<E>);
impl_expand_traits!(E, NativeExpand<Box<[E]>>);

impl<'a, E: CubePrimitive> From<&'a NativeExpand<Box<[E]>>> for &'a NativeExpand<[E]> {
    fn from(value: &'a NativeExpand<Box<[E]>>) -> Self {
        unsafe { value.as_type_ref_unchecked() }
    }
}

impl<'a, E: CubePrimitive> From<&'a mut NativeExpand<Box<[E]>>> for &'a mut NativeExpand<[E]> {
    fn from(value: &'a mut NativeExpand<Box<[E]>>) -> Self {
        unsafe { value.as_type_mut_unchecked() }
    }
}

impl<E: CubePrimitive> SizedContainer<usize> for [E] {
    fn len(&self) -> usize {
        unexpanded!()
    }
}

impl<E: CubePrimitive> SizedContainerExpand<usize> for SliceExpand<E> {
    fn __expand_len_method(&self, scope: &Scope) -> NativeExpand<usize> {
        self.__expand_len_method(scope)
    }
}

impl<E: CubePrimitive> Iterable for SliceExpand<E> {
    type Item = E::ExpandType;

    fn expand(self, scope: &Scope, mut body: impl FnMut(&Scope, Self::Item)) {
        let index_ty = usize::__expand_as_type(scope);
        let len = self.__extract_length(scope).expand;

        let child = scope.child();
        let i = scope.create_local_mut(index_ty);

        let index = NativeExpand::new(i);
        let item = self
            .__expand_index_method(&child, index)
            .__expand_deref_method(&child);
        body(&child, item);

        scope.register(Branch::RangeLoop(Box::new(RangeLoop {
            i,
            start: 0usize.into(),
            end: len,
            step: None,
            inclusive: false,
            scope: child,
        })));
    }

    fn expand_unroll(self, _scope: &Scope, _body: impl FnMut(&Scope, Self::Item)) {
        unimplemented!("Can't unroll slice iterator")
    }
}

impl<'a, E: CubePrimitive> Iterable for &'a SliceExpand<E> {
    type Item = &'a E::ExpandType;

    fn expand(self, scope: &Scope, mut body: impl FnMut(&Scope, Self::Item)) {
        let index_ty = usize::__expand_as_type(scope);
        let len = self.__extract_length(scope).expand;

        let child = scope.child();
        let i = scope.create_local_mut(index_ty);

        let index = NativeExpand::new(i);
        let item = self.__expand_index_method(&child, index);
        body(&child, item);

        scope.register(Branch::RangeLoop(Box::new(RangeLoop {
            i,
            start: 0usize.into(),
            end: len,
            step: None,
            inclusive: false,
            scope: child,
        })));
    }

    fn expand_unroll(self, _scope: &Scope, _body: impl FnMut(&Scope, Self::Item)) {
        unimplemented!("Can't unroll slice iterator")
    }
}

impl<'a, E: CubePrimitive> Iterable for &'a mut SliceExpand<E> {
    type Item = &'a mut E::ExpandType;

    fn expand(self, scope: &Scope, mut body: impl FnMut(&Scope, Self::Item)) {
        let index_ty = usize::__expand_as_type(scope);
        let len = self.__extract_length(scope).expand;

        let child = scope.child();
        let i = scope.create_local_mut(index_ty);

        let index = NativeExpand::new(i);
        let item = self.__expand_index_mut_method(&child, index);
        body(&child, item);

        scope.register(Branch::RangeLoop(Box::new(RangeLoop {
            i,
            start: 0usize.into(),
            end: len,
            step: None,
            inclusive: false,
            scope: child,
        })));
    }

    fn expand_unroll(self, _scope: &Scope, _body: impl FnMut(&Scope, Self::Item)) {
        unimplemented!("Can't unroll slice iterator")
    }
}

impl<E: CubePrimitive> SliceExpand<E> {
    #[doc(hidden)]
    pub unsafe fn __expand_get_unchecked_method(
        &self,
        scope: &Scope,
        index: NativeExpand<usize>,
    ) -> &NativeExpand<E> {
        read_offset::expand::<E>(scope, self, index, false)
    }

    #[doc(hidden)]
    pub unsafe fn __expand_get_unchecked_mut_method(
        &mut self,
        scope: &Scope,
        index: NativeExpand<usize>,
    ) -> &mut NativeExpand<E> {
        write_offset::expand::<E>(scope, self, index, false)
    }
}

impl<E: CubePrimitive> IndexExpand<NativeExpand<usize>> for SliceExpand<E> {
    type Output = E::ExpandType;

    fn __expand_index_method(&self, scope: &Scope, index: NativeExpand<usize>) -> &Self::Output {
        read_offset::expand::<E>(scope, self, index, true)
    }
}

impl<E: CubePrimitive> IndexMutExpand<NativeExpand<usize>> for SliceExpand<E> {
    fn __expand_index_mut_method(
        &mut self,
        scope: &Scope,
        index: NativeExpand<usize>,
    ) -> &mut Self::Output {
        write_offset::expand::<E>(scope, self, index, true)
    }
}

impl_slice_ranges!(SliceExpand<E>);

impl<E: CubePrimitive> List<E> for [E] {}
impl<E: CubePrimitive> ListExpand<E> for SliceExpand<E> {
    fn __expand_len_method(&self, scope: &Scope) -> NativeExpand<usize> {
        self.__expand_len_method(scope)
    }
}

impl<E: CubePrimitive> Vectorized for Box<[E]> {}
impl<E: CubePrimitive> Vectorized for [E] {}
impl<E: CubePrimitive> VectorizedExpand for SliceExpand<E> {
    fn vector_size(&self) -> VectorSize {
        self.expand.vector_size()
    }
}
impl<E: CubePrimitive> VectorizedExpand for NativeExpand<Box<[E]>> {
    fn vector_size(&self) -> VectorSize {
        self.expand.vector_size()
    }
}

mod read_offset {
    use super::*;

    pub fn expand<'a, E: CubePrimitive>(
        scope: &Scope,
        slice: &SliceExpand<E>,
        index: NativeExpand<usize>,
        checked: bool,
    ) -> &'a <E as cubecl::prelude::CubeType>::ExpandType {
        let list = slice.__extract_list(scope);
        let offset = slice.__extract_offset(scope);
        let index = offset.__expand_add_method(scope, index);

        expand_index_native(scope, list, index, checked)
    }
}

mod write_offset {
    use super::*;

    pub fn expand<'a, E: CubePrimitive>(
        scope: &Scope,
        slice: &SliceExpand<E>,
        index: <usize as CubeType>::ExpandType,
        checked: bool,
    ) -> &'a mut E::ExpandType {
        let list = slice.__extract_list(scope);
        let offset = slice.__extract_offset(scope);
        let index = offset.__expand_add_method(scope, index);

        expand_index_mut_native(scope, list, index, checked)
    }
}