cubecl_ir/
non_semantic.rs

1use core::fmt::Display;
2
3use alloc::{borrow::Cow, string::String, vec::Vec};
4
5use crate::TypeHash;
6
7use crate::{OperationCode, OperationReflect, fmt_vararg};
8
9use super::Variable;
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 = NonSemanticOpCode)]
18pub enum NonSemantic {
19    /// Enter a new debug scope, this happens recursively when a cube function is called and
20    /// inlined into the kernel. Should push a new frame onto the debug call stack.
21    EnterDebugScope,
22    /// Exit a debug scope and resume execution at the previous stack frame.
23    ExitDebugScope,
24    /// Print a formatted string with arguments to the backend's debugging facilit
25    /// (i.e. validation layer). The syntax of the format string depends on the backend, but tends
26    /// to follow C++ convention.
27    Print {
28        format_string: String,
29        args: Vec<Variable>,
30    },
31    /// Insert a comment into the compiled source
32    Comment { content: String },
33}
34
35impl OperationReflect for NonSemantic {
36    type OpCode = NonSemanticOpCode;
37
38    fn op_code(&self) -> Self::OpCode {
39        self.__match_opcode()
40    }
41}
42
43impl Display for NonSemantic {
44    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45        match self {
46            NonSemantic::Print {
47                format_string,
48                args,
49            } => {
50                write!(f, "print({format_string}, {})", fmt_vararg(args))
51            }
52            NonSemantic::Comment { content } => write!(f, "//{content}"),
53            // Scopes don't have meaning to the user
54            _ => Ok(()),
55        }
56    }
57}
58
59/// A Rust source location, including the file, line and column
60#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
61#[derive(Debug, Clone, PartialEq, Eq, Hash, TypeHash)]
62pub struct SourceLoc {
63    pub line: u32,
64    pub column: u32,
65    pub source: CubeFnSource,
66}
67
68/// A cube function's source
69#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70#[derive(Debug, Clone, PartialEq, Eq, Hash, TypeHash)]
71pub struct CubeFnSource {
72    pub function_name: Cow<'static, str>,
73    pub file: Cow<'static, str>,
74    pub source_text: Cow<'static, str>,
75    pub line: u32,
76    pub column: u32,
77}