cubecl_core/frontend/
debug.rs1use crate::ir::{NonSemantic, Variable};
2
3use super::CubeContext;
4
5#[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#[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#[track_caller]
49pub fn debug_source_expand(context: &mut CubeContext, name: &str, file: &str, line: u32, col: u32) {
50 if context.debug_enabled {
51 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
62pub 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#[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#[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}