cubecl_core/frontend/container/line/base.rs
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
use std::num::NonZero;
use crate::{
ir::{ConstantScalarValue, Item},
prelude::{assign, CubeContext, ExpandElement},
unexpanded,
};
use crate::frontend::{
CubePrimitive, CubeType, ExpandElementBaseInit, ExpandElementTyped, IntoRuntime,
};
/// A contiguous list of elements that supports auto-vectorized operations.
#[derive(Clone, Copy, Eq)]
pub struct Line<P: CubePrimitive> {
// Comptime lines only support 1 element.
pub(crate) val: P,
}
/// Module that contains the implementation details of the new function.
mod new {
use super::*;
impl<P: CubePrimitive> Line<P> {
/// Create a new line of size 1 using the given value.
pub fn new(val: P) -> Self {
Self { val }
}
/// Expand function of [Self::new].
pub fn __expand_new(
_context: &mut CubeContext,
val: P::ExpandType,
) -> ExpandElementTyped<Self> {
let elem: ExpandElementTyped<P> = val;
elem.expand.into()
}
}
}
/// Module that contains the implementation details of the fill function.
mod fill {
use super::*;
impl<P: CubePrimitive + Into<ExpandElementTyped<P>>> Line<P> {
/// Fill the line with the given value.
///
/// If you want to fill the line with different values, consider using the index API
/// instead.
///
/// ```rust, ignore
/// let mut line = Line::<u32>::empty(2);
/// line[0] = 1;
/// line[1] = 2;
/// ```
#[allow(unused_variables)]
pub fn fill(mut self, value: P) -> Self {
self.val = value;
self
}
/// Expand function of [fill](Self::fill).
pub fn __expand_fill(
context: &mut CubeContext,
line: ExpandElementTyped<Self>,
value: ExpandElementTyped<P>,
) -> ExpandElementTyped<Self> {
line.__expand_fill_method(context, value)
}
}
impl<P: CubePrimitive> ExpandElementTyped<Line<P>> {
/// Expand method of [fill](Line::fill).
pub fn __expand_fill_method(
self,
context: &mut CubeContext,
value: ExpandElementTyped<P>,
) -> Self {
let length = self.expand.item().vectorization;
let output = context.create_local_binding(Item::vectorized(P::as_elem(), length));
assign::expand::<P>(context, value, output.clone().into());
output.into()
}
}
}
/// Module that contains the implementation details of the empty function.
mod empty {
use super::*;
impl<P: CubePrimitive + Into<ExpandElementTyped<P>>> Line<P> {
/// Create an empty line of the given size.
///
/// Note that a line can't change in size once it's fixed.
#[allow(unused_variables)]
pub fn empty(size: u32) -> Self {
unexpanded!()
}
/// Expand function of [empty](Self::empty).
pub fn __expand_empty(
context: &mut CubeContext,
length: ExpandElementTyped<u32>,
) -> ExpandElementTyped<Self> {
let length = match length.expand.as_const() {
Some(val) => match val {
ConstantScalarValue::Int(val, _) => NonZero::new(val)
.map(|val| val.get() as u8)
.map(|val| NonZero::new(val).unwrap()),
ConstantScalarValue::Float(val, _) => NonZero::new(val as i64)
.map(|val| val.get() as u8)
.map(|val| NonZero::new(val).unwrap()),
ConstantScalarValue::UInt(val) => NonZero::new(val as u8),
ConstantScalarValue::Bool(_) => None,
},
None => None,
};
context
.create_local_variable(Item::vectorized(Self::as_elem(), length))
.into()
}
}
}
/// Module that contains the implementation details of the size function.
mod size {
use super::*;
impl<P: CubePrimitive> Line<P> {
/// Get the number of individual elements a line contains.
///
/// The size is available at comptime and may be used in combination with the comptime
/// macro.
///
/// ```rust, ignore
/// // The if statement is going to be executed at comptime.
/// if comptime!(line.size() == 1) {
/// }
/// ```
pub fn size(&self) -> u32 {
unexpanded!()
}
/// Expand function of [size](Self::size).
pub fn __expand_size(context: &mut CubeContext, element: ExpandElementTyped<P>) -> u32 {
element.__expand_vectorization_factor_method(context)
}
}
impl<P: CubePrimitive> ExpandElementTyped<Line<P>> {
/// Comptime version of [size](Line::size).
pub fn size(&self) -> u32 {
self.expand
.item()
.vectorization
.unwrap_or(NonZero::new(1).unwrap())
.get() as u32
}
/// Expand method of [size](Line::size).
pub fn __expand_size_method(&self, _context: &mut CubeContext) -> u32 {
self.size()
}
}
}
impl<P: CubePrimitive> CubeType for Line<P> {
type ExpandType = ExpandElementTyped<Self>;
}
impl<P: CubePrimitive> ExpandElementBaseInit for Line<P> {
fn init_elem(context: &mut crate::prelude::CubeContext, elem: ExpandElement) -> ExpandElement {
P::init_elem(context, elem)
}
}
impl<P: CubePrimitive> IntoRuntime for Line<P> {
fn __expand_runtime_method(
self,
context: &mut crate::prelude::CubeContext,
) -> Self::ExpandType {
self.val.__expand_runtime_method(context).expand.into()
}
}
impl<P: CubePrimitive> CubePrimitive for Line<P> {
fn as_elem() -> crate::ir::Elem {
P::as_elem()
}
}