use alloc::{string::String, vec::Vec};
use cubecl_ir::CubeFnSource;
use crate::ir::{NonSemantic, Scope, Variable};
use super::CubeDebug;
#[track_caller]
pub fn debug_call_expand<C>(
scope: &mut Scope,
line: u32,
col: u32,
call: impl FnOnce(&mut Scope) -> C,
) -> C {
let source_loc = scope.debug.source_loc.take();
scope.update_span(line, col);
scope.register(NonSemantic::EnterDebugScope);
let ret = call(scope);
scope.register(NonSemantic::ExitDebugScope);
scope.debug.source_loc = source_loc;
ret
}
#[track_caller]
pub fn spanned_expand<C>(
scope: &mut Scope,
line: u32,
col: u32,
call: impl FnOnce(&mut Scope) -> C,
) -> C {
scope.update_span(line, col);
call(scope)
}
#[track_caller]
pub fn debug_source_expand(
scope: &mut Scope,
name: &'static str,
file: &'static str,
source_text: &'static str,
line: u32,
column: u32,
) {
let file = file.replace("\\", "/");
scope.update_source(CubeFnSource {
function_name: name.into(),
file: file.into(),
source_text: source_text.into(),
line,
column,
});
}
#[track_caller]
pub fn debug_var_expand<E: CubeDebug>(scope: &mut Scope, name: &'static str, expand: E) -> E {
expand.set_debug_name(scope, name);
expand
}
pub fn printf_expand(scope: &mut Scope, format_string: impl Into<String>, args: Vec<Variable>) {
scope.register(NonSemantic::Print {
format_string: format_string.into(),
args,
});
}
#[macro_export]
macro_rules! debug_print {
($format:literal, $($args:expr),*) => {
{
let _ = $format;
$(let _ = $args;)*
}
};
($format:literal, $($args:expr,)*) => {
$crate::debug_print!($format, $($args),*);
};
}
#[macro_export]
macro_rules! debug_print_expand {
($scope:expr, $format:expr, $($args:expr),*) => {
{
let args = $crate::__private::vec![$(*$crate::ir::ManagedVariable::from($args)),*];
$crate::frontend::printf_expand($scope, $format, args);
}
};
($format:literal, $($args:expr,)*) => {
$crate::debug_print_expand!($format, $($args),*)
};
}
pub mod cube_comment {
use alloc::string::ToString;
use crate::ir::NonSemantic;
use cubecl_ir::Scope;
pub fn expand(scope: &mut Scope, content: &str) {
scope.register(NonSemantic::Comment {
content: content.to_string(),
});
}
}