Skip to main content

cubecl_core/frontend/operation/
assignation.rs

1use core::ops::{
2    Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
3};
4
5use cubecl_ir::{ExpandValue, Scope};
6
7use crate::unexpanded;
8use crate::{
9    frontend::{Array, Tensor},
10    prelude::*,
11};
12
13type ArrayExpand<E> = NativeExpand<Array<E>>;
14
15pub mod assign {
16    use cubecl_ir::{dialect::memory::StoreOp, interfaces::TypedExt, pliron::r#type::Typed};
17    use pliron::value::Value;
18
19    use crate::prelude::NativeExpand;
20
21    use super::*;
22
23    /// Expand the assign operation.
24    ///
25    /// If you want to assign to a manually initialized const variable, look into
26    /// [`expand_no_check()`].
27    pub fn expand<C: CubeType>(
28        scope: &Scope,
29        input: NativeExpand<C>,
30        output: &mut NativeExpand<C>,
31    ) {
32        if output.value(scope).is_immutable(scope.ctx()) {
33            panic!("Can't assign a value to a const variable. Try to use `RuntimeCell`.");
34        }
35
36        expand_no_check(scope, input, output);
37    }
38    /// Expand the assign operation without any check.
39    ///
40    /// You can't assign to a const variable with this [`expand()`].
41    pub fn expand_no_check<C: CubeType>(
42        scope: &Scope,
43        input: NativeExpand<C>,
44        output: &mut NativeExpand<C>,
45    ) {
46        let output = output.expand;
47        let input = input.expand;
48
49        expand_element(scope, input, output);
50    }
51
52    pub fn expand_element(scope: &Scope, input: ExpandValue, output: ExpandValue) {
53        let input = input.read_value(scope);
54        let output = output.value(scope);
55
56        let input = broadcast_input(scope, input, output).unwrap_or(input);
57
58        // value -> ptr = store
59        let store = StoreOp::new(scope.ctx_mut(), output, input);
60        scope.register(&store);
61    }
62
63    fn broadcast_input(scope: &Scope, input: Value, output: Value) -> Option<Value> {
64        let out_vec = output.try_get_vector_size(scope.ctx())?;
65        let in_vec = input.try_get_vector_size(scope.ctx())?;
66        if out_vec > 1 && in_vec == 1 {
67            let out_ty = output.get_type(scope.ctx()).unwrap_ptr(scope.ctx());
68            Some(cast_value(scope, input, out_ty))
69        } else {
70            None
71        }
72    }
73}
74
75pub mod index_mut {
76    use super::*;
77
78    macro_rules! impl_index {
79        ($type: ty, $expand: ty) => {
80            impl<E: CubePrimitive> IndexMut<usize> for $type {
81                fn index_mut(&mut self, _idx: usize) -> &mut Self::Output {
82                    unexpanded!()
83                }
84            }
85
86            impl<E: CubePrimitive> IndexMut<Range<usize>> for $type {
87                fn index_mut(&mut self, _idx: Range<usize>) -> &mut Self::Output {
88                    unexpanded!()
89                }
90            }
91
92            impl<E: CubePrimitive> IndexMut<RangeFrom<usize>> for $type {
93                fn index_mut(&mut self, _idx: RangeFrom<usize>) -> &mut Self::Output {
94                    unexpanded!()
95                }
96            }
97
98            impl<E: CubePrimitive> IndexMut<RangeFull> for $type {
99                fn index_mut(&mut self, _idx: RangeFull) -> &mut Self::Output {
100                    unexpanded!()
101                }
102            }
103
104            impl<E: CubePrimitive> IndexMut<RangeInclusive<usize>> for $type {
105                fn index_mut(&mut self, _idx: RangeInclusive<usize>) -> &mut Self::Output {
106                    unexpanded!()
107                }
108            }
109
110            impl<E: CubePrimitive> IndexMut<RangeTo<usize>> for $type {
111                fn index_mut(&mut self, _idx: RangeTo<usize>) -> &mut Self::Output {
112                    unexpanded!()
113                }
114            }
115
116            impl<E: CubePrimitive> IndexMut<RangeToInclusive<usize>> for $type {
117                fn index_mut(&mut self, _idx: RangeToInclusive<usize>) -> &mut Self::Output {
118                    unexpanded!()
119                }
120            }
121
122            impl<E: CubePrimitive> IndexMutExpand<NativeExpand<usize>> for $expand {
123                fn __expand_index_mut_method(
124                    &mut self,
125                    scope: &Scope,
126                    index: NativeExpand<usize>,
127                ) -> &mut E::ExpandType {
128                    self.__expand_as_mut_slice_method(scope)
129                        .__expand_index_mut_method(scope, index)
130                }
131            }
132        };
133    }
134
135    impl_index!(Array<E>, NativeExpand<Array<E>>);
136    impl_index!(Tensor<E>, TensorExpand<E>);
137    impl_index!(Shared<[E]>, NativeExpand<Shared<[E]>>);
138
139    impl_slice_ranges!(ArrayExpand<E>);
140    impl_slice_ranges!(TensorExpand<E>);
141    impl_slice_ranges!(NativeExpand<Shared<[E]>>);
142}
143
144pub mod index {
145    use super::*;
146
147    macro_rules! impl_index {
148        ($type: ty, $expand: ty) => {
149            impl<E: CubePrimitive> Index<usize> for $type {
150                type Output = E;
151
152                fn index(&self, _idx: usize) -> &Self::Output {
153                    unexpanded!()
154                }
155            }
156
157            impl<E: CubePrimitive> Index<Range<usize>> for $type {
158                type Output = [E];
159
160                fn index(&self, _idx: Range<usize>) -> &Self::Output {
161                    unexpanded!()
162                }
163            }
164
165            impl<E: CubePrimitive> Index<RangeFrom<usize>> for $type {
166                type Output = [E];
167
168                fn index(&self, _idx: RangeFrom<usize>) -> &Self::Output {
169                    unexpanded!()
170                }
171            }
172
173            impl<E: CubePrimitive> Index<RangeFull> for $type {
174                type Output = [E];
175
176                fn index(&self, _idx: RangeFull) -> &Self::Output {
177                    unexpanded!()
178                }
179            }
180
181            impl<E: CubePrimitive> Index<RangeInclusive<usize>> for $type {
182                type Output = [E];
183
184                fn index(&self, _idx: RangeInclusive<usize>) -> &Self::Output {
185                    unexpanded!()
186                }
187            }
188
189            impl<E: CubePrimitive> Index<RangeTo<usize>> for $type {
190                type Output = [E];
191
192                fn index(&self, _idx: RangeTo<usize>) -> &Self::Output {
193                    unexpanded!()
194                }
195            }
196
197            impl<E: CubePrimitive> Index<RangeToInclusive<usize>> for $type {
198                type Output = [E];
199
200                fn index(&self, _idx: RangeToInclusive<usize>) -> &Self::Output {
201                    unexpanded!()
202                }
203            }
204
205            impl<E: CubePrimitive> IndexExpand<NativeExpand<usize>> for $expand {
206                type Output = NativeExpand<E>;
207
208                fn __expand_index_method(
209                    &self,
210                    scope: &Scope,
211                    index: NativeExpand<usize>,
212                ) -> &Self::Output {
213                    self.__expand_as_slice_method(scope)
214                        .__expand_index_method(scope, index)
215                }
216            }
217        };
218    }
219
220    impl_index!(Array<E>, NativeExpand<Array<E>>);
221    impl_index!(Tensor<E>, TensorExpand<E>);
222    impl_index!(Shared<[E]>, NativeExpand<Shared<[E]>>);
223}