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
use super::Compiler;
use crate::{
    ir::{
        Binding, CubeDim, Elem, Item, KernelDefinition, Location, ReadingStrategy, Scope, Variable,
        Vectorization, Visibility,
    },
    Runtime,
};

/// The kernel integrator allows you to create a [kernel definition](KernelDefinition) based on
/// [kernel expansion](KernelExpansion) and [kernel settings](KernelSettings).
#[derive(Clone)]
pub struct KernelIntegrator {
    expansion: KernelExpansion,
    input_bindings: Vec<Binding>,
    output_bindings: Vec<Binding>,
    named_bindings: Vec<(String, Binding)>,
}

/// The information necessary to compile a [kernel definition](KernelDefinition).
#[derive(Clone)]
pub struct KernelExpansion {
    pub inputs: Vec<InputInfo>,
    pub outputs: Vec<OutputInfo>,
    pub scope: Scope,
}

/// Simply indicate the output that can be replaced by the input.
#[derive(new, Default, Clone, Debug, Hash, PartialEq, Eq)]
pub struct InplaceMapping {
    /// Input position.
    pub pos_input: usize,
    /// Output position.
    pub pos_output: usize,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
enum VectorizationPartial {
    Input {
        pos: usize,
        vectorization: Vectorization,
    },
    Output {
        pos: usize,
        vectorization: Vectorization,
    },
}

#[derive(Default, Clone, Debug, Hash, PartialEq, Eq)]
pub struct KernelSettings {
    pub mappings: Vec<InplaceMapping>,
    vectorization_global: Option<Vectorization>,
    vectorization_partial: Vec<VectorizationPartial>,
    cube_dim: CubeDim,
    pub reading_strategy: Vec<(u16, ReadingStrategy)>,
}

impl core::fmt::Display for KernelSettings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // The goal of this implementation is to generate the shortest representation
        // that won't clash with any other compilation settings. This is crucial since we rely on
        // this representation to know when to compile a new version of a kernel.
        //
        // Each main section starts with a letter that can't be used by other main sections:
        //
        // * Mapping:          m
        //   * Input:  i
        //   * Output: o
        //
        // * Reading Strategy: r
        //   * Output layout: o
        //   * Plain:         p
        //
        // * Vectorization Global:    vg{factor}
        // * Vectorization Partial Input:    v{factor}i{pos}
        // * Vectorization Partial Output:    vo
        // * Cube Dim X: x
        // * Cube Dim Y: y
        // * Cube Dim Z: z
        f.write_str("m")?;
        for mapping in self.mappings.iter() {
            f.write_fmt(format_args!(
                "i{}o{}",
                mapping.pos_input, mapping.pos_output
            ))?;
        }

        f.write_str("r")?;

        for (input, strategy) in self.reading_strategy.iter() {
            match strategy {
                ReadingStrategy::OutputLayout => f.write_fmt(format_args!("i{}o", input)),
                ReadingStrategy::Plain => f.write_fmt(format_args!("i{}p", input)),
            }?;
        }

        match self.vectorization_global {
            Some(vectorization) => f.write_fmt(format_args!("vg{}", vectorization))?,
            None => f.write_str("vn")?,
        };

        for vectorization in self.vectorization_partial.iter() {
            match vectorization {
                VectorizationPartial::Input { pos, vectorization } => {
                    f.write_fmt(format_args!("v{vectorization}i{pos}"))?
                }
                VectorizationPartial::Output { pos, vectorization } => {
                    f.write_fmt(format_args!("v{vectorization}o{pos}"))?
                }
            };
        }

        f.write_fmt(format_args!(
            "x{}y{}z{}",
            self.cube_dim.x, self.cube_dim.y, self.cube_dim.x
        ))
    }
}

impl KernelSettings {
    /// Compile the shader with vectorization enabled for all inputs and outputs.
    #[allow(dead_code)]
    pub fn vectorize_global(mut self, vectorization: Vectorization) -> Self {
        self.vectorization_global = Some(vectorization);
        self
    }

    /// Compile the shader with vectorization enabled for an input.
    #[allow(dead_code)]
    pub fn vectorize_input(mut self, position: usize, vectorization: Vectorization) -> Self {
        // Not setting the vectorization factor when it's the default value reduces the kernel id
        // size.
        if vectorization == 1 {
            return self;
        }

        self.vectorization_partial
            .push(VectorizationPartial::Input {
                pos: position,
                vectorization,
            });
        self
    }

    /// Compile the shader with vectorization enabled for an output.
    #[allow(dead_code)]
    pub fn vectorize_output(mut self, position: usize, vectorization: Vectorization) -> Self {
        // Not setting the vectorization factor when it's the default value reduces the kernel id
        // size.
        if vectorization == 1 {
            return self;
        }

        self.vectorization_partial
            .push(VectorizationPartial::Output {
                pos: position,
                vectorization,
            });
        self
    }

    /// Fetch the vectorization for the provided input position.
    pub fn vectorization_input(&self, position: usize) -> Vectorization {
        if let Some(vec) = self.vectorization_global {
            return vec;
        }

        for partial in self.vectorization_partial.iter() {
            if let VectorizationPartial::Input { pos, vectorization } = partial {
                if *pos == position {
                    return *vectorization;
                }
            }
        }

        1
    }

    /// Fetch the vectorization for the provided output position.
    pub fn vectorization_output(&self, position: usize) -> Vectorization {
        if let Some(vec) = self.vectorization_global {
            return vec;
        }

        for partial in self.vectorization_partial.iter() {
            if let VectorizationPartial::Output { pos, vectorization } = partial {
                if *pos == position {
                    return *vectorization;
                }
            }
        }

        1
    }

    /// Compile the shader with inplace enabled by the given [mapping](InplaceMapping).
    ///
    /// Notes:
    ///
    /// You should favor using `dynamic_settings` when using fusion, since the mapping is going to
    /// be created from the runtime information.
    pub fn inplace(mut self, mappings: Vec<InplaceMapping>) -> Self {
        self.mappings = mappings;
        self
    }

    /// Set cube dimension.
    #[allow(dead_code)]
    pub fn cube_dim(mut self, cube_dim: CubeDim) -> Self {
        self.cube_dim = cube_dim;
        self
    }
}

#[allow(dead_code)]
fn is_contiguous(strides: &[usize]) -> bool {
    let mut current = 0;

    for stride in strides.iter().rev() {
        if current > *stride {
            return false;
        }
        current = *stride;
    }

    true
}

/// Information related to an input.
#[derive(Clone, Debug)]
pub enum InputInfo {
    Array { item: Item, visibility: Visibility },
    Scalar { elem: Elem, size: usize },
}

impl InputInfo {
    /// The item type of the input.
    #[allow(dead_code)]
    pub fn item(&self) -> Item {
        match self {
            InputInfo::Array {
                item,
                visibility: _,
            } => *item,
            InputInfo::Scalar { elem, size: _ } => Item::new(*elem),
        }
    }
}

impl OutputInfo {
    /// The item type of the input.
    #[allow(dead_code)]
    pub fn item(&self) -> Item {
        match self {
            OutputInfo::ArrayWrite {
                item,
                local: _,
                position: _,
            } => *item,
            OutputInfo::InputArrayWrite {
                item,
                input: _,
                local: _,
                position: _,
            } => *item,
            OutputInfo::Array { item } => *item,
        }
    }
}

/// Information related to an output.
#[derive(Clone, Debug)]
pub enum OutputInfo {
    /// Write the local variable to a new array.
    ///
    /// This will create a new binding in the [kernel definition](KernelDefinition).
    ArrayWrite {
        item: Item,
        local: u16,
        position: Variable,
    },
    /// Write the local variable to an existing input binding.
    InputArrayWrite {
        item: Item,
        input: u16,
        local: u16,
        position: Variable,
    },
    /// Simply register the output, but don't automatically add a write to it.
    ///
    /// Useful when a procedure writes to the output using operations.
    Array { item: Item },
}

impl OutputInfo {
    #[allow(dead_code)]
    pub fn elem_size<R: Runtime>(&self) -> usize {
        let elem = match self {
            OutputInfo::ArrayWrite {
                item,
                local: _,
                position: _,
            } => bool_elem(item.elem()),
            OutputInfo::InputArrayWrite {
                item,
                input: _,
                local: _,
                position: _,
            } => bool_elem(item.elem()),
            OutputInfo::Array { item } => bool_elem(item.elem()),
        };
        <R::Compiler as Compiler>::elem_size(elem)
    }
}

impl KernelIntegrator {
    /// Starts a new compilation.
    pub fn new(info: KernelExpansion) -> Self {
        Self {
            expansion: info,
            input_bindings: Default::default(),
            output_bindings: Default::default(),
            named_bindings: Default::default(),
        }
    }

    /// Performs the compilation with the provided [settings](KernelSettings).
    pub fn integrate(mut self, mut settings: KernelSettings) -> KernelDefinition {
        if let Some(vectorization) = settings.vectorization_global {
            self.expansion.scope.vectorize(vectorization);
        }

        self.register_inputs(&settings);
        self.register_outputs(&mut settings);

        let inputs = self.input_bindings;
        let outputs = self.output_bindings;
        let mut named = Vec::with_capacity(2);

        named.push((
            "info".to_string(),
            Binding {
                item: Item::new(Elem::UInt),
                visibility: Visibility::Read,
                location: Location::Storage,
                size: None, // We avoid putting the length here since it will force a new kernel
                            // for each tensor rank.
            },
        ));

        for (name, binding) in self.named_bindings.into_iter() {
            named.push((name, binding));
        }

        KernelDefinition {
            inputs,
            outputs,
            named,
            cube_dim: settings.cube_dim,
            body: self.expansion.scope,
        }
    }

    fn register_inputs(&mut self, settings: &KernelSettings) {
        for (id, strategy) in settings.reading_strategy.iter() {
            self.expansion.scope.update_read(*id, *strategy);
        }

        for input in self.expansion.inputs.drain(..) {
            match input {
                InputInfo::Array { item, visibility } => {
                    let item = if let Some(vectorization) = settings.vectorization_global {
                        item.vectorize(vectorization)
                    } else {
                        item
                    };

                    self.input_bindings.push(Binding {
                        item: bool_item(item),
                        visibility,
                        location: Location::Storage,
                        size: None,
                    });
                }
                InputInfo::Scalar { elem, size } => {
                    let elem = bool_elem(elem);

                    self.named_bindings.push((
                        format!("scalars_{}", elem),
                        Binding {
                            item: Item::new(elem),
                            visibility: Visibility::Read,
                            location: Location::Storage,
                            size: Some(size),
                        },
                    ));
                }
            }
        }
    }

    fn register_outputs(&mut self, settings: &mut KernelSettings) {
        let mut index = 0;

        if !settings.mappings.is_empty() {
            let mut mappings = Vec::new();
            core::mem::swap(&mut settings.mappings, &mut mappings);

            for mapping in mappings {
                self.register_inplace_mapping(mapping);
            }
        }

        for array in self.expansion.outputs.drain(..) {
            match array {
                OutputInfo::ArrayWrite {
                    item,
                    local,
                    position,
                } => {
                    let item = if let Some(vectorization) = settings.vectorization_global {
                        item.vectorize(vectorization)
                    } else {
                        item
                    };
                    let item_adapted = bool_item(item);

                    self.output_bindings.push(Binding {
                        item: item_adapted,
                        visibility: Visibility::ReadWrite,
                        location: Location::Storage,
                        size: None,
                    });
                    self.expansion.scope.write_global(
                        Variable::Local {
                            id: local,
                            item,
                            depth: self.expansion.scope.depth,
                        },
                        Variable::GlobalOutputArray {
                            id: index,
                            item: item_adapted,
                        },
                        position,
                    );
                    index += 1;
                }
                OutputInfo::InputArrayWrite {
                    item,
                    input,
                    local,
                    position,
                } => {
                    let item = if let Some(vectorization) = settings.vectorization_global {
                        item.vectorize(vectorization)
                    } else {
                        item
                    };

                    self.expansion.scope.write_global(
                        Variable::Local {
                            id: local,
                            item,
                            depth: self.expansion.scope.depth,
                        },
                        Variable::GlobalInputArray {
                            id: input,
                            item: bool_item(item),
                        },
                        position,
                    );
                }
                OutputInfo::Array { item } => {
                    let item = if let Some(vectorization) = settings.vectorization_global {
                        item.vectorize(vectorization)
                    } else {
                        item
                    };
                    let elem_adapted = bool_item(item);

                    self.output_bindings.push(Binding {
                        item: elem_adapted,
                        visibility: Visibility::ReadWrite,
                        location: Location::Storage,
                        size: None,
                    });

                    index += 1;
                }
            }
        }
    }

    fn register_inplace_mapping(&mut self, mapping: InplaceMapping) {
        let output = match self.expansion.outputs.get_mut(mapping.pos_output) {
            Some(output) => output,
            None => {
                if let Some(binding) = self.input_bindings.get_mut(mapping.pos_input) {
                    // Update input visibility.
                    binding.visibility = Visibility::ReadWrite;
                }

                // The mapping is handled differently, normally by cube itself.
                return;
            }
        };

        let (item, local, position) = match output {
            OutputInfo::ArrayWrite { item, local, position } => (item, local, position),
            OutputInfo::InputArrayWrite {
                item: _,
                input,
                local: _,
                position: _,
            } => {
                assert_eq!(
                    *input, mapping.pos_input as u16,
                    "Can't use different inputs for the same output."
                );
                return;
            }
            OutputInfo::Array { item: _ } => panic!("Can't register an inplace operation for an array that isn't using a defined writing strategy."),
        };

        let item = match self.input_bindings.get_mut(mapping.pos_input) {
            Some(binding) => {
                // Update input visibility.
                binding.visibility = Visibility::ReadWrite;
                // Inputs modified inplace should be read without any specified layout.
                self.expansion
                    .scope
                    .update_read(mapping.pos_input as u16, ReadingStrategy::Plain);

                // Use the same item as the input.
                //
                // The output can be different (i.e inplace boolean operations on float bindings).
                binding.item
            }
            None => *item,
        };

        // Update the output.
        *output = OutputInfo::InputArrayWrite {
            item,
            input: mapping.pos_input as u16,
            local: *local,
            position: *position,
        };
    }
}

fn bool_item(ty: Item) -> Item {
    Item {
        elem: bool_elem(ty.elem),
        vectorization: ty.vectorization,
    }
}

pub fn bool_elem(elem: Elem) -> Elem {
    match elem {
        // U32 are used for bool tensors
        Elem::Bool => Elem::UInt,
        _ => elem,
    }
}