cubecl_core/frontend/
indexation.rs

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
use super::{CubeType, ExpandElement, ExpandElementTyped};
use crate::{
    ir::{IntKind, Variable},
    unexpanded,
};

/// Fake indexation so we can rewrite indexes into scalars as calls to this fake function in the
/// non-expanded function
pub trait CubeIndex<T: Index> {
    type Output: CubeType;

    fn cube_idx(&self, _i: T) -> &Self::Output {
        unexpanded!()
    }
}

pub trait CubeIndexMut<T: Index>: CubeIndex<T> {
    fn cube_idx_mut(&mut self, _i: T) -> &mut Self::Output {
        unexpanded!()
    }
}

pub trait Index {
    fn value(self) -> Variable;
}

impl Index for i32 {
    fn value(self) -> Variable {
        Variable::ConstantScalar(crate::ir::ConstantScalarValue::Int(
            self as i64,
            IntKind::I32,
        ))
    }
}

impl Index for u32 {
    fn value(self) -> Variable {
        Variable::ConstantScalar(crate::ir::ConstantScalarValue::UInt(self as u64))
    }
}

impl Index for ExpandElement {
    fn value(self) -> Variable {
        *self
    }
}

impl Index for ExpandElementTyped<u32> {
    fn value(self) -> Variable {
        *self.expand
    }
}