cubecl_core/frontend/
debug.rs

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