Skip to content

Commit

Permalink
fix rust-analyzer hint on control flow keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
shiinamiyuki committed Oct 16, 2023
1 parent 484772b commit d5bf1f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
18 changes: 17 additions & 1 deletion luisa_compute/src/lang/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Debug;

use crate::internal_prelude::*;

use super::{with_recorder, recording_started};
use super::{recording_started, with_recorder};

#[macro_export]
macro_rules! cpu_dbg {
Expand Down Expand Up @@ -163,4 +163,20 @@ macro_rules! lc_comment_lineno {
column!()
))
};
($msg:literal, $e:expr) => {
$crate::lang::debug::with_lineno(
$msg,
file!(),
line!(),
column!(),
|| $e,
)
};
}

pub fn with_lineno<T>(msg: &str, file: &str, line: u32, col: u32, f: impl FnOnce() -> T) -> T {
comment(&format!("`{}` begin at {}:{}:{}", msg, file, line, col));
let ret = f();
comment(&format!("`{}` end at {}:{}:{}", msg, file, line, col));
ret
}
2 changes: 1 addition & 1 deletion luisa_compute/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::lang::soa::{SoaBuffer, SoaBufferVar, SoaBufferView, SoaMetadata};
use crate::lang::types::SoaValue;
use ir::{
CallableModule, CallableModuleRef, Capture, CpuCustomOp, KernelModule, Module, ModuleFlags,
ModuleKind, ModulePools,
ModuleKind,
};

use crate::backend::Backend;
Expand Down
64 changes: 28 additions & 36 deletions luisa_compute_track/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl VisitMut for TraceVisitor {
fn visit_expr_mut(&mut self, node: &mut Expr) {
let flow_path = &self.flow_path;
let trait_path = &self.trait_path;
let debug_path = quote!(::luisa_compute::lang::debug);
let span = node.span();

match node {
Expand Down Expand Up @@ -98,45 +99,39 @@ impl VisitMut for TraceVisitor {
if let Expr::Let(_) = **cond {
} else if let Some((_, else_branch)) = else_branch {
*node = parse_quote_spanned! {span=>
{
::luisa_compute::lc_comment_lineno!("if stmt begin");
let __ret = <_ as #trait_path::SelectMaybeExpr<_>>::if_then_else(#cond, || #then_branch, || #else_branch);
::luisa_compute::lc_comment_lineno!("if stmt end");
__ret
}
#debug_path::with_lineno("if",
file!(),
line!(),
column!(),
|| <_ as #trait_path::SelectMaybeExpr<_>>::if_then_else(#cond, || #then_branch, || #else_branch))
}
} else {
*node = parse_quote_spanned! {span=>
{
::luisa_compute::lc_comment_lineno!("if stmt begin");
let __ret = <_ as #trait_path::ActivateMaybeExpr>::activate(#cond, || #then_branch);
::luisa_compute::lc_comment_lineno!("if stmt end");
__ret
}
#debug_path::with_lineno("if",
file!(),
line!(),
column!(),
|| <_ as #trait_path::ActivateMaybeExpr>::activate(#cond, || #then_branch))
}
}
}
Expr::While(expr) => {
let cond = &expr.cond;
let body = &expr.body;
*node = parse_quote_spanned! {span=>
{
::luisa_compute::lc_comment_lineno!("while stmt begin");
let __ret = <_ as #trait_path::LoopMaybeExpr>::while_loop(|| #cond, || #body);
::luisa_compute::lc_comment_lineno!("while stmt end");
__ret
}
#debug_path::with_lineno("while",
file!(),
line!(),
column!(), ||<_ as #trait_path::LoopMaybeExpr>::while_loop(|| #cond, || #body))
}
}
Expr::Loop(expr) => {
let body = &expr.body;
*node = parse_quote_spanned! {span=>
{
::luisa_compute::lc_comment_lineno!("loop stmt begin");
let __ret = #flow_path::loop_(|| #body);
::luisa_compute::lc_comment_lineno!("loop stmt end");
__ret
}
#debug_path::with_lineno("loop",
file!(),
line!(),
column!(), || #flow_path::loop_(|| #body))
}
}
Expr::ForLoop(expr) => {
Expand All @@ -149,20 +144,17 @@ impl VisitMut for TraceVisitor {
let unroll = attrs.iter().any(|attr| attr.path().is_ident("unroll"));
if unroll {
*node = parse_quote_spanned! {span=>
{
::luisa_compute::lc_comment_lineno!("for loop stmt begin");
let __ret = #range.for_each(|#pat| #body);
::luisa_compute::lc_comment_lineno!("for loop stmt end");
__ret
}
#debug_path::with_lineno("for range",
file!(),
line!(),
column!(), || #range.for_each(|#pat| #body))
}
} else {
*node = parse_quote_spanned! {span=> {
::luisa_compute::lc_comment_lineno!("for loop stmt begin");
let __ret = #flow_path::for_range(#range, |#pat| #body);
::luisa_compute::lc_comment_lineno!("for loop stmt end");
__ret
}
*node = parse_quote_spanned! {span=>
#debug_path::with_lineno("for range",
file!(),
line!(),
column!(), ||#flow_path::for_range(#range, |#pat| #body))
}
}
}
Expand Down

0 comments on commit d5bf1f0

Please sign in to comment.