Skip to main content

cubecl_ir/
marker.rs

1use core::fmt::Display;
2
3use enumset::{EnumSet, EnumSetType};
4
5use crate::{Instruction, Operation, TypeHash};
6
7use crate::{OperationCode, OperationReflect};
8
9use super::Value;
10
11/// Operations that don't change the semantics of the kernel. In other words, operations that do not
12/// perform any computation, if they run at all. i.e. `println`, comments and debug symbols.
13///
14/// Can be safely removed or ignored without changing the kernel result.
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[derive(Debug, Clone, TypeHash, PartialEq, Eq, Hash, OperationCode)]
17#[operation(opcode_name = MarkerOpCode)]
18pub enum Marker {
19    /// Frees a shared memory, allowing reuse in later blocks.
20    Free(Value),
21    #[operation(pure)]
22    DummyRead(Value),
23}
24
25impl OperationReflect for Marker {
26    type OpCode = MarkerOpCode;
27
28    fn op_code(&self) -> Self::OpCode {
29        self.__match_opcode()
30    }
31
32    fn sanitize_args(&mut self, _: &crate::Scope) {}
33}
34
35impl Display for Marker {
36    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37        match self {
38            Marker::Free(val) => write!(f, "free({val})"),
39            Marker::DummyRead(value) => write!(f, "mark_read({value})"),
40        }
41    }
42}
43
44impl From<Marker> for Instruction {
45    fn from(value: Marker) -> Self {
46        Instruction::no_out(Operation::Marker(value))
47    }
48}
49
50/// Unchecked optimizations for float operations. May cause precision differences, or undefined
51/// behaviour if the relevant conditions are not followed.
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53#[derive(Debug, Hash, TypeHash, EnumSetType)]
54pub enum FastMath {
55    /// Assume values are never `NaN`. If they are, the result is considered undefined behaviour.
56    NotNaN,
57    /// Assume values are never `Inf`/`-Inf`. If they are, the result is considered undefined
58    /// behaviour.
59    NotInf,
60    /// Ignore sign on zero values.
61    UnsignedZero,
62    /// Allow swapping float division with a reciprocal, even if that swap would change precision.
63    AllowReciprocal,
64    /// Allow contracting float operations into fewer operations, even if the precision could
65    /// change.
66    AllowContraction,
67    /// Allow reassociation for float operations, even if the precision could change.
68    AllowReassociation,
69    /// Allow all mathematical transformations for float operations, including contraction and
70    /// reassociation, even if the precision could change.
71    AllowTransform,
72    /// Allow using lower precision intrinsics
73    ReducedPrecision,
74}
75
76impl FastMath {
77    pub const fn all() -> EnumSet<FastMath> {
78        EnumSet::all()
79    }
80}