cubecl_ir/
allocator.rs

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/// An allocator for local variables of a kernel.
12///
13/// A local variable is unique to a unit. That is, each unit have their own copy of a local variable.
14/// There are three types of local variables based on their capabilities.
15///     - An immutable local variable is obtained by calling [Allocator::create_local].
16///     - A mutable local variable is obtained by calling [Allocator::create_local_mut]. The allocator will reuse
17///       previously defined mutable variables if possible.
18///     - A restricted mutable local variable is obtained by calling [Allocator::create_local_restricted]. This a is
19///       mutable variable that cannot be reused. This is mostly used for loop indices.
20///
21/// # Performance tips
22///
23/// In order, prefer immutable local variables, then mutable, then restricted.
24///
25/// To enable many compiler optimizations, it is preferred to use the [static single-assignment] strategy for immutable variables.
26/// That is, each variable must be declared and used exactly once.
27///
28/// [static single-assignment](https://en.wikipedia.org/wiki/Static_single-assignment_form)
29#[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    /// Create a new immutable local variable of type specified by `item`.
47    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    /// Create a new mutable local variable of type specified by `item`.
54    /// Try to reuse a previously defined but unused mutable variable if possible.
55    /// Else, this define a new variable.
56    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    /// Create a new mutable restricted local variable of type specified by `item`.
66    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    /// Create a matrix variable
85    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    // Try to return a reusable mutable variable for the given `item` or `None` otherwise.
114    pub fn reuse_local_mut(&self, item: Item) -> Option<ExpandElement> {
115        // Among the candidates, take a variable if it's only referenced by the pool.
116        // Arbitrarily takes the first it finds in reversed order.
117        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    /// Add a new variable to the pool with type specified by `item` for the given `scope`.
126    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    /// Reference to a JIT variable
161    #[derive(Clone, Debug, TypeHash)]
162    pub enum ExpandElement {
163        /// Variable kept in the variable pool.
164        Managed(Rc<Variable>),
165        /// Variable not kept in the variable pool.
166        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        /// If the element can be mutated inplace, potentially reusing the register.
191        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        /// Explicitly consume the element, freeing it for reuse if no other copies exist.
205        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}