cubecl_core/frontend/
debug.rs

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