Skip to main content

cubecl_core/frontend/
debug.rs

1use alloc::{string::String, vec::Vec};
2use cubecl_ir::{dialect::general::PrintfOp, pliron::value::Value};
3
4use crate::ir::Scope;
5
6use super::CubeDebug;
7
8/// Calls a function and inserts debug symbols if debug is enabled.
9#[track_caller]
10pub fn debug_call_expand<C>(
11    scope: &Scope,
12    _line: u32,
13    _col: u32,
14    call: impl FnOnce(&Scope) -> C,
15) -> C {
16    // Save source_loc before the call so it can be restored once the call returns
17    // let source_loc = scope.debug.source_loc.take();
18    // scope.update_span(line, col);
19    // scope.register(NonSemantic::EnterDebugScope);
20    let ret = call(scope);
21    // scope.register(NonSemantic::ExitDebugScope);
22    // *scope.debug.source_loc.borrow_mut() = source_loc;
23    ret
24}
25
26/// Adds source instruction if debug is enabled
27#[track_caller]
28pub fn debug_source_expand(
29    _scope: &Scope,
30    _name: &'static str,
31    _file: &'static str,
32    _source_text: &'static str,
33    _line: u32,
34    _column: u32,
35) {
36    // let file = file.replace("\\", "/");
37    // scope.update_source(CubeFnSource {
38    //     function_name: name.into(),
39    //     file: file.into(),
40    //     source_text: source_text.into(),
41    //     line,
42    //     column,
43    // });
44}
45
46/// Registers name for an expand if possible
47#[track_caller]
48pub fn debug_var_expand<E: CubeDebug>(scope: &Scope, name: &'static str, expand: E) -> E {
49    expand.set_debug_name(scope, name);
50    expand
51}
52
53/// Prints a formatted message using the print debug layer in Vulkan, or `printf` in CUDA.
54pub fn printf_expand(scope: &Scope, format_string: impl Into<String>, args: Vec<Value>) {
55    scope.register(&PrintfOp::new(scope.ctx_mut(), format_string.into(), args));
56}
57
58/// Print a formatted message using the target's debug print facilities. The format string is target
59/// specific, but Vulkan and CUDA both use the C++ conventions. WGSL isn't currently supported.
60#[macro_export]
61macro_rules! debug_print {
62    ($format:literal, $($args:expr),*) => {
63        {
64            let _ = $format;
65            $(let _ = $args;)*
66        }
67    };
68    ($format:literal, $($args:expr,)*) => {
69        $crate::debug_print!($format, $($args),*);
70    };
71}
72
73/// Print a formatted message using the target's debug print facilities. The format string is target
74/// specific, but Vulkan and CUDA both use the C++ conventions. WGSL isn't currently supported.
75#[macro_export]
76macro_rules! __expand_debug_print {
77    ($scope:expr, $format:expr, $($args:expr),*) => {
78        {
79            let args = $crate::__private::vec![$($crate::ir::ExpandValue::from($args).read_value($scope)),*];
80            $crate::frontend::printf_expand($scope, $format, args);
81        }
82    };
83    ($format:literal, $($args:expr,)*) => {
84        $crate::__expand_debug_print!($format, $($args),*)
85    };
86}
87
88pub mod cube_comment {
89    use alloc::string::ToString;
90
91    use cubecl_ir::{Scope, dialect::general::CommentOp};
92
93    pub fn expand(scope: &Scope, content: &str) {
94        scope.register(&CommentOp::new(scope.ctx_mut(), content.to_string()));
95    }
96}