cubecl-opt 0.11.0-pre.1

Compiler optimizations for CubeCL
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
use alloc::collections::vec_deque::VecDeque;
use cubecl_ir::{AddressSpace, Id, Value, ValueKind};
use hashbrown::{HashMap, HashSet};
use petgraph::graph::NodeIndex;

use crate::{Function, GlobalState, analyses::post_order::PostOrder};

use super::Analysis;

type LivePredicate = fn(&Function, &Value) -> Option<Id>;

pub struct Liveness {
    live_vars: HashMap<NodeIndex, HashSet<Id>>,
}

#[derive(Debug, Clone)]
struct BlockSets {
    generated: HashSet<Id>,
    kill: HashSet<Id>,
}

struct State {
    worklist: VecDeque<NodeIndex>,
    block_sets: HashMap<NodeIndex, BlockSets>,
}

impl Analysis for Liveness {
    fn init(func: &mut Function, state: &GlobalState) -> Self {
        Self {
            live_vars: compute_liveness(func, state, Function::local_variable_id),
        }
    }
}

impl Liveness {
    pub fn empty(func: &Function) -> Self {
        let live_vars = func
            .node_ids()
            .iter()
            .map(|it| (*it, HashSet::new()))
            .collect();
        Self { live_vars }
    }

    pub fn at_block(&self, block: NodeIndex) -> &HashSet<Id> {
        &self.live_vars[&block]
    }

    pub fn is_dead(&self, node: NodeIndex, var: Id) -> bool {
        !self.at_block(node).contains(&var)
    }
}

/// Block level liveness over all non-atomic local memories, including non-destructurable ones such
/// as arrays.
///
/// [`Liveness`] only tracks destructurable locals (those that get promoted to SSA registers and
/// phi nodes). Backends that keep local memory as actual allocations and need to free it when it
/// dies (e.g. the CPU backend) must also track non-destructurable locals, which this analysis does.
pub struct MemoryLiveness {
    live_vars: HashMap<NodeIndex, HashSet<Id>>,
}

impl Analysis for MemoryLiveness {
    fn init(func: &mut Function, state: &GlobalState) -> Self {
        Self {
            live_vars: compute_liveness(func, state, Function::local_memory_id),
        }
    }
}

impl MemoryLiveness {
    pub fn empty(func: &Function) -> Self {
        let live_vars = func
            .node_ids()
            .iter()
            .map(|it| (*it, HashSet::new()))
            .collect();
        Self { live_vars }
    }

    pub fn at_block(&self, block: NodeIndex) -> &HashSet<Id> {
        &self.live_vars[&block]
    }

    pub fn is_dead(&self, node: NodeIndex, var: Id) -> bool {
        !self.at_block(node).contains(&var)
    }
}

/// Do a conservative block level liveness analysis, tracking the variables selected by `pred`.
fn compute_liveness(
    func: &mut Function,
    global_state: &GlobalState,
    pred: LivePredicate,
) -> HashMap<NodeIndex, HashSet<Id>> {
    let mut live_vars: HashMap<NodeIndex, HashSet<Id>> = func
        .node_ids()
        .iter()
        .map(|it| (*it, HashSet::new()))
        .collect();
    let mut state = State {
        worklist: VecDeque::from(func.analysis::<PostOrder>(global_state).forward()),
        block_sets: HashMap::new(),
    };
    while let Some(block) = state.worklist.pop_front() {
        analyze_block(func, global_state, block, &mut state, &mut live_vars, pred);
    }
    live_vars
}

fn analyze_block(
    func: &mut Function,
    global_state: &GlobalState,
    block: NodeIndex,
    state: &mut State,
    live_vars: &mut HashMap<NodeIndex, HashSet<Id>>,
    pred: LivePredicate,
) {
    let BlockSets { generated, kill } = block_sets(func, global_state, block, state, pred);

    let mut block_live = generated.clone();

    for successor in func.successors(block) {
        let successor = &live_vars[&successor];
        block_live.extend(successor.difference(kill));
    }

    if block_live != live_vars[&block] {
        state.worklist.extend(func.predecessors(block));
        live_vars.insert(block, block_live);
    }
}

fn block_sets<'a>(
    func: &mut Function,
    global_state: &GlobalState,
    block: NodeIndex,
    state: &'a mut State,
    pred: LivePredicate,
) -> &'a BlockSets {
    let block_sets = state.block_sets.entry(block);
    block_sets.or_insert_with(|| calculate_block_sets(func, global_state, block, pred))
}

fn calculate_block_sets(
    func: &mut Function,
    state: &GlobalState,
    block: NodeIndex,
    pred: LivePredicate,
) -> BlockSets {
    let mut generated = HashSet::new();
    let mut kill = HashSet::new();

    let ops = func[block].ops.clone();

    let control_flow = func[block].control_flow.clone();
    func.visit_control_flow(&mut control_flow.borrow_mut(), |func, val| {
        if let Some(id) = pred(func, val) {
            generated.insert(id);
        }
    });
    let mut ops = ops.borrow().clone();
    for op in ops.values_mut().rev() {
        // Reads must be tracked after writes
        func.visit_out(&mut op.out, |func, val| {
            if let Some(id) = pred(func, val) {
                kill.insert(id);
                generated.remove(&id);
            }
        });
        func.visit_operation(state, &mut op.operation, |func, val| {
            if let Some(id) = pred(func, val) {
                generated.insert(id);
            }
        });
    }

    BlockSets { generated, kill }
}

impl Function {
    /// Gets the `id` of the variable if it's a destructurable `Local` and not atomic, `None`
    /// otherwise.
    pub fn local_variable_id(&self, value: &Value) -> Option<Id> {
        match value.kind {
            ValueKind::Value { id } if self.destructurable_local_memories().contains_key(&id) => {
                Some(id)
            }
            _ => None,
        }
    }

    /// Gets the `id` of the variable if it's a `Local` memory and not atomic, `None` otherwise.
    ///
    /// Unlike [`Function::local_variable_id`], this includes non-destructurable locals such as
    /// arrays, so it can be used by backends that allocate and free those memories.
    pub fn local_memory_id(&self, value: &Value) -> Option<Id> {
        match value.kind {
            ValueKind::Value { id } => match self.memories.get(&id) {
                Some(mem)
                    if matches!(mem.address_space, AddressSpace::Local)
                        && !mem.value_ty.is_atomic() =>
                {
                    Some(id)
                }
                _ => None,
            },
            _ => None,
        }
    }
}

/// Shared memory liveness analysis and allocation
pub mod shared {
    use alloc::vec::Vec;
    use cubecl_ir::{AddressSpace, Marker, Operation, Type, Value, ValueKind};

    use crate::{MemoryBlock, Uniformity};

    use super::*;

    /// A specific allocation of shared memory at some `offset`
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct SmemAllocation {
        pub id: Id,
        /// The shared memory being allocated
        pub smem: MemoryBlock,
        /// The offset in the shared memory buffer
        pub offset: usize,
    }

    /// Shared liveness works the other way around from normal liveness, since shared memory lives
    /// forever by default. So any use (read or write) inserts it as live, while only `free` changes
    /// the state to dead.
    ///
    /// It also handles allocation of slices to each shared memory object, using the analyzed
    /// liveness. `allocations` contains a specific slice allocation for each shared memory, while
    /// ensuring no shared memories that exist at the same time can overlap.
    #[derive(Default, Clone)]
    pub struct SharedLiveness {
        live_vars: HashMap<NodeIndex, HashSet<Id>>,
        /// Map of all shared memories by their ID. Populated during the first pass with all
        /// accessed shared memories.
        pub shared_memories: HashMap<Id, MemoryBlock>,
        /// Map of allocations for each shared memory by its ID. Populated after the analysis, and
        /// should contain all memories from `shared_memories`.
        pub allocations: HashMap<Id, SmemAllocation>,
    }

    impl Analysis for SharedLiveness {
        fn init(func: &mut Function, state: &GlobalState) -> Self {
            let mut this = Self::empty(func);
            this.analyze_liveness(func, state);
            this.uniformize_liveness(func, state);
            this.allocate_slices(func);
            this
        }
    }

    impl SharedLiveness {
        pub fn empty(func: &Function) -> Self {
            let live_vars = func
                .node_ids()
                .iter()
                .map(|it| (*it, HashSet::new()))
                .collect();
            Self {
                live_vars,
                shared_memories: Default::default(),
                allocations: Default::default(),
            }
        }

        pub fn at_block(&self, block: NodeIndex) -> &HashSet<Id> {
            &self.live_vars[&block]
        }

        fn is_live(&self, node: NodeIndex, var: Id) -> bool {
            self.at_block(node).contains(&var)
        }

        /// Do a conservative block level liveness analysis
        fn analyze_liveness(&mut self, func: &mut Function, global_state: &GlobalState) {
            let mut state = State {
                worklist: VecDeque::from(func.analysis::<PostOrder>(global_state).reverse()),
                block_sets: HashMap::new(),
            };
            while let Some(block) = state.worklist.pop_front() {
                self.analyze_block(func, global_state, block, &mut state);
            }
        }

        /// Extend divergent liveness to the preceding uniform block. Shared memory is always
        /// uniformly declared, so it must be allocated before the branch.
        fn uniformize_liveness(&mut self, func: &mut Function, global_state: &GlobalState) {
            let mut state = State {
                worklist: VecDeque::from(func.analysis::<PostOrder>(global_state).forward()),
                block_sets: HashMap::new(),
            };
            while let Some(block) = state.worklist.pop_front() {
                self.uniformize_block(func, global_state, block, &mut state);
            }
        }

        /// Allocate slices while ensuring no concurrent shared memory slices overlap.
        /// See also [`allocate_slice`]
        fn allocate_slices(&mut self, func: &mut Function) {
            for block in func.node_ids() {
                for live_smem in self.at_block(block).clone() {
                    if !self.allocations.contains_key(&live_smem) {
                        let smem = self.shared_memories[&live_smem];
                        let offset = self.allocate_slice(block, smem.size(), smem.alignment);
                        self.allocations.insert(
                            live_smem,
                            SmemAllocation {
                                id: live_smem,
                                smem,
                                offset,
                            },
                        );
                    }
                }
            }
        }

        /// Finds a valid offset for a specific slice, taking into account ranges that are already
        /// in use.
        ///
        /// Essentially the same as the global memory pool, looking for a free slice first, then
        /// extending the pool if there isn't one. Note that this linear algorithm isn't optimal
        /// for offline allocations where we know all allocations beforehand, but should be good
        /// enough for our current purposes. It may produce larger-than-required allocations in
        /// some cases. Optimal allocation would require a far more complex algorithm.
        fn allocate_slice(&mut self, block: NodeIndex, size: usize, align: usize) -> usize {
            let live_slices = self.live_slices(block);
            if live_slices.is_empty() {
                return 0;
            }

            for i in 0..live_slices.len() - 1 {
                let slice_0 = &live_slices[i];
                let slice_1 = &live_slices[i + 1];
                let end_0 = (slice_0.offset + slice_0.smem.size()).next_multiple_of(align);
                let gap = slice_1.offset.saturating_sub(end_0);
                if gap >= size {
                    return end_0;
                }
            }
            let last_slice = &live_slices[live_slices.len() - 1];
            (last_slice.offset + last_slice.smem.size()).next_multiple_of(align)
        }

        /// List of allocations that are currently live
        fn live_slices(&mut self, block: NodeIndex) -> Vec<SmemAllocation> {
            let mut live_slices = self
                .allocations
                .iter()
                .filter(|(k, _)| self.is_live(block, **k))
                .map(|it| *it.1)
                .collect::<Vec<_>>();
            live_slices.sort_by_key(|it| it.offset);
            live_slices
        }

        fn analyze_block(
            &mut self,
            func: &mut Function,
            global_state: &GlobalState,
            block: NodeIndex,
            state: &mut State,
        ) {
            let BlockSets { generated, kill } = self.block_sets(func, global_state, block, state);

            let mut live_vars = generated.clone();

            for predecessor in func.predecessors(block) {
                let predecessor = &self.live_vars[&predecessor];
                live_vars.extend(predecessor.difference(kill));
            }

            if live_vars != self.live_vars[&block] {
                state.worklist.extend(func.successors(block));
                self.live_vars.insert(block, live_vars);
            }
        }

        fn uniformize_block(
            &mut self,
            func: &mut Function,
            global_state: &GlobalState,
            block: NodeIndex,
            state: &mut State,
        ) {
            let mut live_vars = self.live_vars[&block].clone();
            let uniformity = func.analysis::<Uniformity>(global_state);

            for successor in func.successors(block) {
                if !uniformity.is_block_uniform(successor) {
                    let successor = &self.live_vars[&successor];
                    live_vars.extend(successor);
                }
            }

            if live_vars != self.live_vars[&block] {
                state.worklist.extend(func.predecessors(block));
                self.live_vars.insert(block, live_vars);
            }
        }

        fn block_sets<'a>(
            &mut self,
            func: &mut Function,
            global_state: &GlobalState,
            block: NodeIndex,
            state: &'a mut State,
        ) -> &'a BlockSets {
            let block_sets = state.block_sets.entry(block);
            block_sets.or_insert_with(|| self.calculate_block_sets(func, global_state, block))
        }

        /// Any use makes a shared memory live (`generated`), while `free` kills it (`kill`).
        /// Also collects all shared memories into a map.
        fn calculate_block_sets(
            &mut self,
            func: &mut Function,
            state: &GlobalState,
            block: NodeIndex,
        ) -> BlockSets {
            let mut generated = HashSet::new();
            let mut kill = HashSet::new();

            let ops = func[block].ops.clone();

            for op in ops.borrow_mut().values_mut() {
                func.visit_out(&mut op.out, |func, var| {
                    if let Some((id, smem)) = shared_memory(func, var) {
                        generated.insert(id);
                        self.shared_memories.insert(id, smem);
                    }
                });
                func.visit_operation(state, &mut op.operation, |func, var| {
                    if let Some((id, smem)) = shared_memory(func, var) {
                        generated.insert(id);
                        self.shared_memories.insert(id, smem);
                    }
                });

                if let Operation::Marker(Marker::Free(Value {
                    ty: Type::Pointer(_, AddressSpace::Shared),
                    kind: ValueKind::Value { id, .. },
                    ..
                })) = &op.operation
                {
                    kill.insert(*id);
                    generated.remove(id);
                }
            }

            BlockSets { generated, kill }
        }
    }

    fn shared_memory(func: &Function, var: &Value) -> Option<(Id, MemoryBlock)> {
        match var.kind {
            ValueKind::Value { id } => {
                if let Some(mem) = func.memories.get(&id)
                    && matches!(mem.address_space, AddressSpace::Shared)
                {
                    Some((id, *mem))
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

mod captures {
    use cubecl_ir::Value;

    use super::*;

    pub struct Captures {
        live_vars: HashMap<NodeIndex, HashSet<Value>>,
    }

    #[derive(Clone)]
    struct BlockSets {
        generated: HashSet<Value>,
        kill: HashSet<Value>,
    }

    struct State {
        worklist: VecDeque<NodeIndex>,
        block_sets: HashMap<NodeIndex, BlockSets>,
    }

    impl Analysis for Captures {
        fn init(func: &mut Function, state: &GlobalState) -> Self {
            let mut this = Self::empty(func);
            this.analyze_liveness(func, state);
            this
        }
    }

    impl Captures {
        pub fn empty(func: &Function) -> Self {
            let live_vars = func
                .node_ids()
                .iter()
                .map(|it| (*it, HashSet::new()))
                .collect();
            Self { live_vars }
        }

        pub fn at_block(&self, block: NodeIndex) -> &HashSet<Value> {
            &self.live_vars[&block]
        }

        /// Do a conservative block level liveness analysis
        pub fn analyze_liveness(&mut self, func: &mut Function, global_state: &GlobalState) {
            let mut state = State {
                worklist: VecDeque::from(func.analysis::<PostOrder>(global_state).forward()),
                block_sets: HashMap::new(),
            };
            while let Some(block) = state.worklist.pop_front() {
                self.analyze_block(func, global_state, block, &mut state);
            }
        }

        fn analyze_block(
            &mut self,
            func: &mut Function,
            global_state: &GlobalState,
            block: NodeIndex,
            state: &mut State,
        ) {
            let BlockSets { generated, kill } = block_sets(func, global_state, block, state);

            let mut live_vars = generated.clone();

            for successor in func.successors(block) {
                let successor = &self.live_vars[&successor];
                live_vars.extend(successor.difference(kill));
            }

            if live_vars != self.live_vars[&block] {
                state.worklist.extend(func.predecessors(block));
                self.live_vars.insert(block, live_vars);
            }
        }
    }

    fn block_sets<'a>(
        func: &mut Function,
        global_state: &GlobalState,
        block: NodeIndex,
        state: &'a mut State,
    ) -> &'a BlockSets {
        let block_sets = state.block_sets.entry(block);
        block_sets.or_insert_with(|| calculate_block_sets(func, global_state, block))
    }

    fn calculate_block_sets(
        func: &mut Function,
        state: &GlobalState,
        block: NodeIndex,
    ) -> BlockSets {
        let mut generated = HashSet::new();
        let mut kill = HashSet::new();

        let ops = func[block].ops.clone();

        let control_flow = func[block].control_flow.clone();
        func.visit_control_flow(&mut control_flow.borrow_mut(), |_, var| {
            generated.insert(*var);
        });
        for inst in ops.borrow_mut().values_mut().rev() {
            // Reads must be tracked after writes
            func.visit_out(&mut inst.out, |_, var| {
                kill.insert(*var);
                generated.remove(var);
            });
            func.visit_operation(state, &mut inst.operation, |_, var| {
                generated.insert(*var);
            });
        }

        BlockSets { generated, kill }
    }
}

pub use captures::Captures;