-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bad842e
commit 255fa72
Showing
4 changed files
with
43 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,57 @@ | ||
use crate::internal_prelude::*; | ||
|
||
use super::types::core::Primitive; | ||
use std::ffi::CString; | ||
|
||
pub struct DebugFormatter { | ||
use crate::internal_prelude::*; | ||
/// TODO: support custom print format | ||
pub struct DevicePrintFormatter { | ||
pub(crate) fmt: String, | ||
pub(crate) args: Vec<NodeRef>, | ||
} | ||
impl DebugFormatter { | ||
impl DevicePrintFormatter { | ||
pub fn new() -> Self { | ||
Self { | ||
fmt: String::new(), | ||
args: Vec::new(), | ||
} | ||
} | ||
pub fn push_str(&mut self, s: &str) { | ||
assert!( | ||
s.find("{}").is_none(), | ||
"DebugFormatter::push_str cannot contain {{}}" | ||
); | ||
self.fmt.push_str(s); | ||
} | ||
pub fn push_arg(&mut self, node: NodeRef) { | ||
assert!( | ||
node.type_().is_primitive(), | ||
"DebugFormatter::push_arg must be primitive" | ||
); | ||
self.fmt.push_str("{}"); | ||
self.args.push(node); | ||
pub fn push_arg(&mut self, node: SafeNodeRef) { | ||
self.args.push(node.get()); | ||
} | ||
} | ||
pub trait DebugPrintValue: Value { | ||
fn fmt_args(e: Expr<Self>, fmt: &mut DebugFormatter); | ||
} | ||
|
||
impl<T: DebugPrintValue + Primitive> DebugPrintValue for T { | ||
fn fmt_args(e: Expr<Self>, fmt: &mut DebugFormatter) { | ||
fmt.push_arg(e.node().get()); | ||
pub fn print(self) { | ||
let Self { fmt, args } = self; | ||
__current_scope(|b| { | ||
b.print( | ||
CBoxedSlice::new(CString::new(fmt).unwrap().into_bytes_with_nul()), | ||
&args, | ||
); | ||
}) | ||
} | ||
} | ||
|
||
pub trait DebugPrint { | ||
fn fmt_args(&self, fmt: &mut DebugFormatter); | ||
pub trait DevicePrint { | ||
fn fmt(&self, fmt: &mut DevicePrintFormatter); | ||
} | ||
|
||
impl<T: DebugPrintValue> DebugPrint for Expr<T> { | ||
fn fmt_args(&self, fmt: &mut DebugFormatter) { | ||
T::fmt_args(*self, fmt); | ||
impl<T: Value> DevicePrint for Expr<T> { | ||
fn fmt(&self, fmt: &mut DevicePrintFormatter) { | ||
fmt.push_arg(self.node()); | ||
} | ||
} | ||
impl<T: DebugPrintValue> DebugPrint for Var<T> { | ||
fn fmt_args(&self, fmt: &mut DebugFormatter) { | ||
T::fmt_args(self.load(), fmt); | ||
impl<T: Value> DevicePrint for Var<T> { | ||
fn fmt(&self, fmt: &mut DevicePrintFormatter) { | ||
fmt.push_arg(self.node()); | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! device_log { | ||
($fmt:literal, $($arg:expr),*) => {{ | ||
let mut fmt = $crate::lang::print::DevicePrintFormatter::new(); | ||
fmt.push_str($fmt); | ||
$( | ||
$crate::lang::print::DevicePrint::fmt(&$arg, &mut fmt); | ||
)* | ||
fmt.print(); | ||
}}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule LuisaCompute
updated
21 files