cubecl-core 0.10.0-pre.3

CubeCL core create
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
use alloc::{vec, vec::Vec};
use cubecl_ir::{
    Allocator, Arithmetic, BinaryOperator, Branch, CoopMma, CopyMemoryBulkOperator,
    IndexAssignOperator, IndexOperator, Instruction, ManagedVariable, MatrixLayout, Metadata,
    Operation, OperationReflect, Operator, Processor, ScopeProcessing, Type, Variable,
    VariableKind, VectorSize,
};
use hashbrown::HashMap;

/// The action that should be performed on an instruction, returned by ``IrTransformer::maybe_transform``
pub enum TransformAction {
    /// The transformer doesn't apply to this instruction
    Ignore,
    /// Replace this instruction with one or more other instructions
    Replace(Vec<Instruction>),
}

#[derive(new, Debug)]
pub struct UnrollProcessor {
    max_vector_size: VectorSize,
}

struct Mappings(HashMap<Variable, Vec<ManagedVariable>>);

impl Mappings {
    fn get(
        &mut self,
        alloc: &Allocator,
        var: Variable,
        unroll_factor: usize,
        vector_size: VectorSize,
    ) -> Vec<Variable> {
        self.0
            .entry(var)
            .or_insert_with(|| create_unrolled(alloc, &var, vector_size, unroll_factor))
            .iter()
            .map(|it| **it)
            .collect()
    }
}

impl UnrollProcessor {
    fn maybe_transform(
        &self,
        alloc: &Allocator,
        inst: &Instruction,
        mappings: &mut Mappings,
    ) -> TransformAction {
        if matches!(inst.operation, Operation::Marker(_)) {
            return TransformAction::Ignore;
        }

        if inst.operation.args().is_none() {
            // Detect unhandled ops that can't be reflected
            match &inst.operation {
                Operation::CoopMma(op) => match op {
                    // Stride is in scalar elems
                    CoopMma::Load {
                        value,
                        stride,
                        offset,
                        layout,
                    } if value.vector_size() > self.max_vector_size => {
                        return TransformAction::Replace(self.transform_cmma_load(
                            alloc,
                            inst.out(),
                            value,
                            stride,
                            offset,
                            layout,
                        ));
                    }
                    CoopMma::Store {
                        mat,
                        stride,
                        offset,
                        layout,
                    } if inst.out().vector_size() > self.max_vector_size => {
                        return TransformAction::Replace(self.transform_cmma_store(
                            alloc,
                            inst.out(),
                            mat,
                            stride,
                            offset,
                            layout,
                        ));
                    }
                    _ => return TransformAction::Ignore,
                },
                Operation::Branch(_) | Operation::NonSemantic(_) | Operation::Marker(_) => {
                    return TransformAction::Ignore;
                }
                _ => {
                    panic!("Need special handling for unrolling non-reflectable operations")
                }
            }
        }

        let args = inst.operation.args().unwrap_or_default();
        if (inst.out.is_some() && inst.ty().vector_size() > self.max_vector_size)
            || args
                .iter()
                .any(|arg| arg.vector_size() > self.max_vector_size)
        {
            let vector_size = max_vector_size(&inst.out, &args);
            let unroll_factor = vector_size / self.max_vector_size;

            match &inst.operation {
                Operation::Operator(Operator::CopyMemoryBulk(op)) => TransformAction::Replace(
                    self.transform_memcpy(alloc, op, inst.out(), unroll_factor),
                ),
                Operation::Operator(Operator::CopyMemory(op)) => {
                    TransformAction::Replace(self.transform_memcpy(
                        alloc,
                        &CopyMemoryBulkOperator {
                            out_index: op.out_index,
                            input: op.input,
                            in_index: op.in_index,
                            len: 1,
                            offset_input: 0.into(),
                            offset_out: 0.into(),
                        },
                        inst.out(),
                        unroll_factor,
                    ))
                }
                Operation::Operator(Operator::Index(op)) if op.list.is_array() => {
                    TransformAction::Replace(self.transform_array_index(
                        alloc,
                        inst.out(),
                        op,
                        Operator::Index,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::UncheckedIndex(op)) if op.list.is_array() => {
                    TransformAction::Replace(self.transform_array_index(
                        alloc,
                        inst.out(),
                        op,
                        Operator::UncheckedIndex,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::Index(op)) => {
                    TransformAction::Replace(self.transform_composite_index(
                        alloc,
                        inst.out(),
                        op,
                        Operator::Index,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::UncheckedIndex(op)) => {
                    TransformAction::Replace(self.transform_composite_index(
                        alloc,
                        inst.out(),
                        op,
                        Operator::UncheckedIndex,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::IndexAssign(op)) if inst.out().is_array() => {
                    TransformAction::Replace(self.transform_array_index_assign(
                        alloc,
                        inst.out(),
                        op,
                        Operator::IndexAssign,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::UncheckedIndexAssign(op))
                    if inst.out().is_array() =>
                {
                    TransformAction::Replace(self.transform_array_index_assign(
                        alloc,
                        inst.out(),
                        op,
                        Operator::UncheckedIndexAssign,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::IndexAssign(op)) => {
                    TransformAction::Replace(self.transform_composite_index_assign(
                        alloc,
                        inst.out(),
                        op,
                        Operator::IndexAssign,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Operator(Operator::UncheckedIndexAssign(op)) => {
                    TransformAction::Replace(self.transform_composite_index_assign(
                        alloc,
                        inst.out(),
                        op,
                        Operator::UncheckedIndexAssign,
                        unroll_factor,
                        mappings,
                    ))
                }
                Operation::Metadata(op) => {
                    TransformAction::Replace(self.transform_metadata(inst.out(), op, args))
                }
                _ => TransformAction::Replace(self.transform_basic(
                    alloc,
                    inst,
                    args,
                    unroll_factor,
                    mappings,
                )),
            }
        } else {
            TransformAction::Ignore
        }
    }

    /// Transform CMMA load offset and array
    fn transform_cmma_load(
        &self,
        alloc: &Allocator,
        out: Variable,
        value: &Variable,
        stride: &Variable,
        offset: &Variable,
        layout: &Option<MatrixLayout>,
    ) -> Vec<Instruction> {
        let vector_size = value.vector_size();
        let unroll_factor = vector_size / self.max_vector_size;

        let value = unroll_array(*value, self.max_vector_size, unroll_factor);
        let (mul, offset) = mul_index(alloc, *offset, unroll_factor);
        let load = Instruction::new(
            Operation::CoopMma(CoopMma::Load {
                value,
                stride: *stride,
                offset: *offset,
                layout: *layout,
            }),
            out,
        );
        vec![mul, load]
    }

    /// Transform CMMA store offset and array
    fn transform_cmma_store(
        &self,
        alloc: &Allocator,
        out: Variable,
        mat: &Variable,
        stride: &Variable,
        offset: &Variable,
        layout: &MatrixLayout,
    ) -> Vec<Instruction> {
        let vector_size = out.vector_size();
        let unroll_factor = vector_size / self.max_vector_size;

        let out = unroll_array(out, self.max_vector_size, unroll_factor);
        let (mul, offset) = mul_index(alloc, *offset, unroll_factor);
        let store = Instruction::new(
            Operation::CoopMma(CoopMma::Store {
                mat: *mat,
                stride: *stride,
                offset: *offset,
                layout: *layout,
            }),
            out,
        );
        vec![mul, store]
    }

    /// Transforms memcpy into one with higher length and adjusted indices/offsets
    fn transform_memcpy(
        &self,
        alloc: &Allocator,
        op: &CopyMemoryBulkOperator,
        out: Variable,
        unroll_factor: usize,
    ) -> Vec<Instruction> {
        let (mul1, in_index) = mul_index(alloc, op.in_index, unroll_factor);
        let (mul2, offset_input) = mul_index(alloc, op.offset_input, unroll_factor);
        let (mul3, out_index) = mul_index(alloc, op.out_index, unroll_factor);
        let (mul4, offset_out) = mul_index(alloc, op.offset_out, unroll_factor);

        let input = unroll_array(op.input, self.max_vector_size, unroll_factor);
        let out = unroll_array(out, self.max_vector_size, unroll_factor);

        vec![
            mul1,
            mul2,
            mul3,
            mul4,
            Instruction::new(
                Operator::CopyMemoryBulk(CopyMemoryBulkOperator {
                    input,
                    in_index: *in_index,
                    out_index: *out_index,
                    len: op.len * unroll_factor,
                    offset_input: *offset_input,
                    offset_out: *offset_out,
                }),
                out,
            ),
        ]
    }

    /// Transforms indexing into multiple index operations, each offset by 1 from the base. The base
    /// is also multiplied by the unroll factor to compensate for the lower actual vectorization.
    fn transform_array_index(
        &self,
        alloc: &Allocator,
        out: Variable,
        op: &IndexOperator,
        operator: impl Fn(IndexOperator) -> Operator,
        unroll_factor: usize,
        mappings: &mut Mappings,
    ) -> Vec<Instruction> {
        let (mul, start_idx) = mul_index(alloc, op.index, unroll_factor);
        let mut indices = (0..unroll_factor).map(|i| add_index(alloc, *start_idx, i));

        let list = unroll_array(op.list, self.max_vector_size, unroll_factor);

        let out = mappings.get(alloc, out, unroll_factor, self.max_vector_size);
        let mut instructions = vec![mul];
        instructions.extend((0..unroll_factor).flat_map(|i| {
            let (add, idx) = indices.next().unwrap();
            let index = Instruction::new(
                operator(IndexOperator {
                    list,
                    index: *idx,
                    vector_size: 0,
                    unroll_factor,
                }),
                out[i],
            );
            [add, index]
        }));

        instructions
    }

    /// Transforms index assign into multiple index assign operations, each offset by 1 from the base.
    /// The base is also multiplied by the unroll factor to compensate for the lower actual vectorization.
    fn transform_array_index_assign(
        &self,
        alloc: &Allocator,
        out: Variable,
        op: &IndexAssignOperator,
        operator: impl Fn(IndexAssignOperator) -> Operator,
        unroll_factor: usize,
        mappings: &mut Mappings,
    ) -> Vec<Instruction> {
        let (mul, start_idx) = mul_index(alloc, op.index, unroll_factor);
        let mut indices = (0..unroll_factor).map(|i| add_index(alloc, *start_idx, i));

        let out = unroll_array(out, self.max_vector_size, unroll_factor);

        let value = mappings.get(alloc, op.value, unroll_factor, self.max_vector_size);

        let mut instructions = vec![mul];
        instructions.extend((0..unroll_factor).flat_map(|i| {
            let (add, idx) = indices.next().unwrap();
            let index = Instruction::new(
                operator(IndexAssignOperator {
                    index: *idx,
                    vector_size: 0,
                    value: value[i],
                    unroll_factor,
                }),
                out,
            );

            [add, index]
        }));

        instructions
    }

    /// Transforms a composite index (i.e. `Vector`) that always returns a scalar. Translates the index
    /// to a local index and an unroll index, then indexes the proper variable. Note that this requires
    /// the index to be constant - it needs to be decomposed at compile time, otherwise it wouldn't
    /// work.
    fn transform_composite_index(
        &self,
        alloc: &Allocator,
        out: Variable,
        op: &IndexOperator,
        operator: impl Fn(IndexOperator) -> Operator,
        unroll_factor: usize,
        mappings: &mut Mappings,
    ) -> Vec<Instruction> {
        let index = op
            .index
            .as_const()
            .expect("Can't unroll non-constant vector index")
            .as_usize();

        let unroll_idx = index / self.max_vector_size;
        let sub_idx = index % self.max_vector_size;

        let value = mappings.get(alloc, op.list, unroll_factor, self.max_vector_size);

        vec![Instruction::new(
            operator(IndexOperator {
                list: value[unroll_idx],
                index: sub_idx.into(),
                vector_size: 1,
                unroll_factor,
            }),
            out,
        )]
    }

    /// Transforms a composite index assign (i.e. `Vector`) that always takes a scalar. Translates the index
    /// to a local index and an unroll index, then indexes the proper variable. Note that this requires
    /// the index to be constant - it needs to be decomposed at compile time, otherwise it wouldn't
    /// work.
    fn transform_composite_index_assign(
        &self,
        alloc: &Allocator,
        out: Variable,
        op: &IndexAssignOperator,
        operator: impl Fn(IndexAssignOperator) -> Operator,
        unroll_factor: usize,
        mappings: &mut Mappings,
    ) -> Vec<Instruction> {
        let index = op
            .index
            .as_const()
            .expect("Can't unroll non-constant vector index")
            .as_usize();

        let unroll_idx = index / self.max_vector_size;
        let sub_idx = index % self.max_vector_size;

        let out = mappings.get(alloc, out, unroll_factor, self.max_vector_size);

        vec![Instruction::new(
            operator(IndexAssignOperator {
                index: sub_idx.into(),
                vector_size: 1,
                value: op.value,
                unroll_factor,
            }),
            out[unroll_idx],
        )]
    }

    /// Transforms metadata by just replacing the type of the buffer. The values are already
    /// properly calculated on the CPU.
    fn transform_metadata(
        &self,
        out: Variable,
        op: &Metadata,
        args: Vec<Variable>,
    ) -> Vec<Instruction> {
        let op_code = op.op_code();
        let args = args
            .into_iter()
            .map(|mut var| {
                if var.vector_size() > self.max_vector_size {
                    var.ty = var.ty.with_vector_size(self.max_vector_size);
                }
                var
            })
            .collect::<Vec<_>>();
        let operation = Metadata::from_code_and_args(op_code, &args).unwrap();
        vec![Instruction::new(operation, out)]
    }

    /// Transforms generic instructions, i.e. comparison, arithmetic. Unrolls each vectorized variable
    /// to `unroll_factor` replacements, and executes the operation `unroll_factor` times.
    fn transform_basic(
        &self,
        alloc: &Allocator,
        inst: &Instruction,
        args: Vec<Variable>,
        unroll_factor: usize,
        mappings: &mut Mappings,
    ) -> Vec<Instruction> {
        let op_code = inst.operation.op_code();
        let out = inst
            .out
            .map(|out| mappings.get(alloc, out, unroll_factor, self.max_vector_size));
        let args = args
            .into_iter()
            .map(|arg| {
                if arg.vector_size() > 1 {
                    mappings.get(alloc, arg, unroll_factor, self.max_vector_size)
                } else {
                    // Preserve scalars
                    vec![arg]
                }
            })
            .collect::<Vec<_>>();

        (0..unroll_factor)
            .map(|i| {
                let out = out.as_ref().map(|out| out[i]);
                let args = args
                    .iter()
                    .map(|arg| if arg.len() == 1 { arg[0] } else { arg[i] })
                    .collect::<Vec<_>>();
                let operation = Operation::from_code_and_args(op_code, &args)
                    .expect("Failed to reconstruct operation");
                Instruction {
                    out,
                    source_loc: inst.source_loc.clone(),
                    modes: inst.modes,
                    operation,
                }
            })
            .collect()
    }

    fn transform_instructions(
        &self,
        allocator: &Allocator,
        instructions: Vec<Instruction>,
        mappings: &mut Mappings,
    ) -> Vec<Instruction> {
        let mut new_instructions = Vec::with_capacity(instructions.len());

        for mut instruction in instructions {
            if let Operation::Branch(branch) = &mut instruction.operation {
                match branch {
                    Branch::If(op) => {
                        op.scope.instructions = self.transform_instructions(
                            allocator,
                            op.scope.instructions.drain(..).collect(),
                            mappings,
                        );
                    }
                    Branch::IfElse(op) => {
                        op.scope_if.instructions = self.transform_instructions(
                            allocator,
                            op.scope_if.instructions.drain(..).collect(),
                            mappings,
                        );
                        op.scope_else.instructions = self.transform_instructions(
                            allocator,
                            op.scope_else.instructions.drain(..).collect(),
                            mappings,
                        );
                    }
                    Branch::Switch(op) => {
                        for (_, case) in &mut op.cases {
                            case.instructions = self.transform_instructions(
                                allocator,
                                case.instructions.drain(..).collect(),
                                mappings,
                            );
                        }
                        op.scope_default.instructions = self.transform_instructions(
                            allocator,
                            op.scope_default.instructions.drain(..).collect(),
                            mappings,
                        );
                    }
                    Branch::RangeLoop(op) => {
                        op.scope.instructions = self.transform_instructions(
                            allocator,
                            op.scope.instructions.drain(..).collect(),
                            mappings,
                        );
                    }
                    Branch::Loop(op) => {
                        op.scope.instructions = self.transform_instructions(
                            allocator,
                            op.scope.instructions.drain(..).collect(),
                            mappings,
                        );
                    }
                    _ => {}
                }
            }
            match self.maybe_transform(allocator, &instruction, mappings) {
                TransformAction::Ignore => {
                    new_instructions.push(instruction);
                }
                TransformAction::Replace(replacement) => {
                    new_instructions.extend(replacement);
                }
            }
        }

        new_instructions
    }
}

impl Processor for UnrollProcessor {
    fn transform(&self, processing: ScopeProcessing, allocator: Allocator) -> ScopeProcessing {
        let mut mappings = Mappings(Default::default());

        let instructions =
            self.transform_instructions(&allocator, processing.instructions, &mut mappings);

        ScopeProcessing {
            variables: processing.variables,
            instructions,
            typemap: processing.typemap.clone(),
        }
    }
}

fn max_vector_size(out: &Option<Variable>, args: &[Variable]) -> VectorSize {
    let vector_size = args.iter().map(|it| it.vector_size()).max().unwrap();
    vector_size.max(out.map(|out| out.vector_size()).unwrap_or(1))
}

fn create_unrolled(
    allocator: &Allocator,
    var: &Variable,
    max_vector_size: VectorSize,
    unroll_factor: usize,
) -> Vec<ManagedVariable> {
    // Preserve scalars
    if var.vector_size() == 1 {
        return vec![ManagedVariable::Plain(*var); unroll_factor];
    }

    let item = Type::new(var.storage_type()).with_vector_size(max_vector_size);
    (0..unroll_factor)
        .map(|_| match var.kind {
            VariableKind::LocalMut { .. } | VariableKind::Versioned { .. } => {
                allocator.create_local_mut(item)
            }
            VariableKind::Shared { .. } => {
                let id = allocator.new_local_index();
                let shared = VariableKind::Shared { id };
                ManagedVariable::Plain(Variable::new(shared, item))
            }
            VariableKind::LocalConst { .. } => allocator.create_local(item),
            other => panic!("Out must be local, found {other:?}"),
        })
        .collect()
}

fn add_index(alloc: &Allocator, idx: Variable, i: usize) -> (Instruction, ManagedVariable) {
    let add_idx = alloc.create_local(idx.ty);
    let add = Instruction::new(
        Arithmetic::Add(BinaryOperator {
            lhs: idx,
            rhs: i.into(),
        }),
        *add_idx,
    );
    (add, add_idx)
}

fn mul_index(
    alloc: &Allocator,
    idx: Variable,
    unroll_factor: usize,
) -> (Instruction, ManagedVariable) {
    let mul_idx = alloc.create_local(idx.ty);
    let mul = Instruction::new(
        Arithmetic::Mul(BinaryOperator {
            lhs: idx,
            rhs: unroll_factor.into(),
        }),
        *mul_idx,
    );
    (mul, mul_idx)
}

fn unroll_array(mut var: Variable, max_vector_size: VectorSize, factor: usize) -> Variable {
    var.ty = var.ty.with_vector_size(max_vector_size);

    match &mut var.kind {
        VariableKind::LocalArray { unroll_factor, .. }
        | VariableKind::ConstantArray { unroll_factor, .. }
        | VariableKind::SharedArray { unroll_factor, .. } => {
            *unroll_factor = factor;
        }
        _ => {}
    }

    var
}