1use alloc::{rc::Rc, vec::Vec};
2use core::cell::RefCell;
3
4use hashbrown::HashMap;
5use portable_atomic::{AtomicU32, Ordering};
6
7use crate::BarrierLevel;
8
9use super::{Item, Matrix, Variable, VariableKind};
10
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30#[derive(Clone, Debug, Default, TypeHash)]
31pub struct Allocator {
32 #[cfg_attr(feature = "serde", serde(skip))]
33 local_mut_pool: Rc<RefCell<HashMap<Item, Vec<ExpandElement>>>>,
34 next_id: Rc<AtomicU32>,
35}
36
37impl PartialEq for Allocator {
38 fn eq(&self, other: &Self) -> bool {
39 Rc::ptr_eq(&self.local_mut_pool, &other.local_mut_pool)
40 && Rc::ptr_eq(&self.next_id, &other.next_id)
41 }
42}
43impl Eq for Allocator {}
44
45impl Allocator {
46 pub fn create_local(&self, item: Item) -> ExpandElement {
48 let id = self.new_local_index();
49 let local = VariableKind::LocalConst { id };
50 ExpandElement::Plain(Variable::new(local, item))
51 }
52
53 pub fn create_local_mut(&self, item: Item) -> ExpandElement {
57 if item.elem.is_atomic() {
58 self.create_local_restricted(item)
59 } else {
60 self.reuse_local_mut(item)
61 .unwrap_or_else(|| ExpandElement::Managed(self.add_local_mut(item)))
62 }
63 }
64
65 pub fn create_local_restricted(&self, item: Item) -> ExpandElement {
67 let id = self.new_local_index();
68 let local = VariableKind::LocalMut { id };
69 ExpandElement::Plain(Variable::new(local, item))
70 }
71
72 pub fn create_local_array(&self, item: Item, array_size: u32) -> ExpandElement {
73 let id = self.new_local_index();
74 let local_array = Variable::new(
75 VariableKind::LocalArray {
76 id,
77 length: array_size,
78 },
79 item,
80 );
81 ExpandElement::Plain(local_array)
82 }
83
84 pub fn create_matrix(&self, matrix: Matrix) -> ExpandElement {
86 let id = self.new_local_index();
87 let variable = Variable::new(
88 VariableKind::Matrix { id, mat: matrix },
89 Item::new(matrix.elem),
90 );
91 ExpandElement::Plain(variable)
92 }
93
94 pub fn create_pipeline(&self, item: Item, num_stages: u8) -> ExpandElement {
95 let id = self.new_local_index();
96 let variable = Variable::new(
97 VariableKind::Pipeline {
98 id,
99 item,
100 num_stages,
101 },
102 item,
103 );
104 ExpandElement::Plain(variable)
105 }
106
107 pub fn create_barrier(&self, item: Item, level: BarrierLevel) -> ExpandElement {
108 let id = self.new_local_index();
109 let variable = Variable::new(VariableKind::Barrier { id, item, level }, item);
110 ExpandElement::Plain(variable)
111 }
112
113 pub fn reuse_local_mut(&self, item: Item) -> Option<ExpandElement> {
115 self.local_mut_pool.borrow().get(&item).and_then(|vars| {
118 vars.iter()
119 .rev()
120 .find(|var| matches!(var, ExpandElement::Managed(v) if Rc::strong_count(v) == 1))
121 .cloned()
122 })
123 }
124
125 pub fn add_local_mut(&self, item: Item) -> Rc<Variable> {
127 let id = self.new_local_index();
128 let local = Variable::new(VariableKind::LocalMut { id }, item);
129 let var = Rc::new(local);
130 let expand = ExpandElement::Managed(var.clone());
131 let mut pool = self.local_mut_pool.borrow_mut();
132 let variables = pool.entry(item).or_default();
133 variables.push(expand);
134 var
135 }
136
137 pub fn new_local_index(&self) -> u32 {
138 self.next_id.fetch_add(1, Ordering::Release)
139 }
140
141 pub fn take_variables(&self) -> Vec<Variable> {
142 self.local_mut_pool
143 .borrow_mut()
144 .drain()
145 .flat_map(|it| it.1)
146 .map(|it| *it)
147 .collect()
148 }
149}
150
151use cubecl_macros_internal::TypeHash;
152pub use expand_element::*;
153
154mod expand_element {
155 use cubecl_common::{e2m1, e2m3, e3m2, e4m3, e5m2, flex32, tf32, ue8m0};
156 use half::{bf16, f16};
157
158 use super::*;
159
160 #[derive(Clone, Debug, TypeHash)]
162 pub enum ExpandElement {
163 Managed(Rc<Variable>),
165 Plain(Variable),
167 }
168
169 impl core::ops::Deref for ExpandElement {
170 type Target = Variable;
171
172 fn deref(&self) -> &Self::Target {
173 match self {
174 ExpandElement::Managed(var) => var.as_ref(),
175 ExpandElement::Plain(var) => var,
176 }
177 }
178 }
179
180 impl From<ExpandElement> for Variable {
181 fn from(value: ExpandElement) -> Self {
182 match value {
183 ExpandElement::Managed(var) => *var,
184 ExpandElement::Plain(var) => var,
185 }
186 }
187 }
188
189 impl ExpandElement {
190 pub fn can_mut(&self) -> bool {
192 match self {
193 ExpandElement::Managed(var) => {
194 if let VariableKind::LocalMut { .. } = var.as_ref().kind {
195 Rc::strong_count(var) <= 2
196 } else {
197 false
198 }
199 }
200 ExpandElement::Plain(_) => false,
201 }
202 }
203
204 pub fn consume(self) -> Variable {
206 *self
207 }
208 }
209
210 macro_rules! impl_into_expand_element {
211 ($type:ty) => {
212 impl From<$type> for ExpandElement {
213 fn from(value: $type) -> Self {
214 ExpandElement::Plain(Variable::from(value))
215 }
216 }
217 };
218 }
219
220 impl_into_expand_element!(u8);
221 impl_into_expand_element!(u16);
222 impl_into_expand_element!(u32);
223 impl_into_expand_element!(u64);
224 impl_into_expand_element!(usize);
225 impl_into_expand_element!(bool);
226 impl_into_expand_element!(e2m1);
227 impl_into_expand_element!(e2m3);
228 impl_into_expand_element!(e3m2);
229 impl_into_expand_element!(e4m3);
230 impl_into_expand_element!(e5m2);
231 impl_into_expand_element!(ue8m0);
232 impl_into_expand_element!(flex32);
233 impl_into_expand_element!(f16);
234 impl_into_expand_element!(bf16);
235 impl_into_expand_element!(tf32);
236 impl_into_expand_element!(f32);
237 impl_into_expand_element!(i8);
238 impl_into_expand_element!(i16);
239 impl_into_expand_element!(i32);
240 impl_into_expand_element!(i64);
241}