Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support debuginfo for custom MIR. #115540

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion compiler/rustc_mir_build/src/build/custom/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_index::IndexSlice;
use rustc_middle::{mir::*, thir::*, ty::Ty};
use rustc_middle::ty::{self, Ty};
use rustc_middle::{mir::*, thir::*};
use rustc_span::Span;

use super::{PResult, ParseCtxt, ParseError};
Expand Down Expand Up @@ -159,6 +160,14 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
);
self.parse_local_decls(local_decls.iter().copied())?;

let (debuginfo, rest) = parse_by_kind!(self, rest, _, "body with debuginfo",
ExprKind::Block { block } => {
let block = &self.thir[*block];
(&block.stmts, block.expr.unwrap())
},
);
self.parse_debuginfo(debuginfo.iter().copied())?;

let block_defs = parse_by_kind!(self, rest, _, "body with block defs",
ExprKind::Block { block } => &self.thir[*block].stmts,
);
Expand Down Expand Up @@ -195,6 +204,52 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
Ok(())
}

fn parse_debuginfo(&mut self, stmts: impl Iterator<Item = StmtId>) -> PResult<()> {
for stmt in stmts {
let stmt = &self.thir[stmt];
let expr = match stmt.kind {
StmtKind::Let { span, .. } => {
return Err(ParseError {
span,
item_description: format!("{:?}", stmt),
expected: "debuginfo".to_string(),
});
}
StmtKind::Expr { expr, .. } => expr,
};
let span = self.thir[expr].span;
let (name, operand) = parse_by_kind!(self, expr, _, "debuginfo",
@call("mir_debuginfo", args) => {
(args[0], args[1])
},
);
let name = parse_by_kind!(self, name, _, "debuginfo",
ExprKind::Literal { lit, neg: false } => lit,
);
let Some(name) = name.node.str() else {
return Err(ParseError {
span,
item_description: format!("{:?}", name),
expected: "string".to_string(),
});
};
let operand = self.parse_operand(operand)?;
let value = match operand {
Operand::Constant(c) => VarDebugInfoContents::Const(*c),
Operand::Copy(p) | Operand::Move(p) => VarDebugInfoContents::Place(p),
};
let dbginfo = VarDebugInfo {
name,
source_info: SourceInfo { span, scope: self.source_scope },
argument_index: None,
value,
};
self.body.var_debug_info.push(dbginfo);
}

Ok(())
}

fn parse_let_statement(&mut self, stmt_id: StmtId) -> PResult<(LocalVarId, Ty<'tcx>, Span)> {
let pattern = match &self.thir[stmt_id].kind {
StmtKind::Let { pattern, .. } => pattern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
)
}

fn parse_operand(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
pub fn parse_operand(&self, expr_id: ExprId) -> PResult<Operand<'tcx>> {
parse_by_kind!(self, expr_id, expr, "operand",
@call("mir_move", args) => self.parse_place(args[0]).map(Operand::Move),
@call("mir_static", args) => self.parse_static(args[0]),
Expand Down
39 changes: 27 additions & 12 deletions library/core/src/intrinsics/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
//!
//! Typical usage will look like this:
//!
//! ```rust
#![cfg_attr(bootstrap, doc = "```rust,ignore")]
#![cfg_attr(not(bootstrap), doc = "```rust")]
//! #![feature(core_intrinsics, custom_mir)]
//! #![allow(internal_features)]
//!
Expand Down Expand Up @@ -62,7 +63,8 @@
//!
//! # Examples
//!
//! ```rust
#![cfg_attr(bootstrap, doc = "```rust,ignore")]
#![cfg_attr(not(bootstrap), doc = "```rust")]
//! #![feature(core_intrinsics, custom_mir)]
//! #![allow(internal_features)]
//!
Expand Down Expand Up @@ -317,7 +319,8 @@ define!(
///
/// # Examples
///
/// ```rust
#[cfg_attr(bootstrap, doc = "```rust,ignore")]
#[cfg_attr(not(bootstrap), doc = "```rust")]
/// #![allow(internal_features)]
/// #![feature(custom_mir, core_intrinsics)]
///
Expand Down Expand Up @@ -361,6 +364,11 @@ define!(
#[doc(hidden)]
fn __internal_make_place<T>(place: T) -> *mut T
);
define!(
"mir_debuginfo",
#[doc(hidden)]
fn __debuginfo<T>(name: &'static str, s: T)
);

/// Macro for generating custom MIR.
///
Expand All @@ -371,6 +379,7 @@ pub macro mir {
(
$(type RET = $ret_ty:ty ;)?
$(let $local_decl:ident $(: $local_decl_ty:ty)? ;)*
$(debug $dbg_name:ident => $dbg_data:expr ;)*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if some doc comment somewhere explained this syntax.


{
$($entry:tt)*
Expand All @@ -394,26 +403,32 @@ pub macro mir {
$(
let $local_decl $(: $local_decl_ty)? ;
)*

::core::intrinsics::mir::__internal_extract_let!($($entry)*);
$(
::core::intrinsics::mir::__internal_extract_let!($($block)*);
)*

{
// Finally, the contents of the basic blocks
::core::intrinsics::mir::__internal_remove_let!({
{}
{ $($entry)* }
});
// Now debuginfo
$(
__debuginfo(stringify!($dbg_name), $dbg_data);
)*

{
// Finally, the contents of the basic blocks
::core::intrinsics::mir::__internal_remove_let!({
{}
{ $($block)* }
{ $($entry)* }
});
)*
$(
::core::intrinsics::mir::__internal_remove_let!({
{}
{ $($block)* }
});
)*

RET
RET
}
}
}
}}
Expand Down
11 changes: 11 additions & 0 deletions tests/mir-opt/building/custom/debuginfo.numbered.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// MIR for `numbered` after built

fn numbered(_1: (u32, i32)) -> () {
debug first => (_1.0: u32);
debug second => (_1.0: u32);
let mut _0: ();

bb0: {
return;
}
}
10 changes: 10 additions & 0 deletions tests/mir-opt/building/custom/debuginfo.pointee.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// MIR for `pointee` after built

fn pointee(_1: &mut Option<i32>) -> () {
debug foo => (((*_1) as variant#1).0: i32);
let mut _0: ();

bb0: {
return;
}
}
71 changes: 71 additions & 0 deletions tests/mir-opt/building/custom/debuginfo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#![feature(custom_mir, core_intrinsics)]

extern crate core;
use core::intrinsics::mir::*;

// EMIT_MIR debuginfo.pointee.built.after.mir
#[custom_mir(dialect = "built")]
fn pointee(opt: &mut Option<i32>) {
mir!(
debug foo => Field::<i32>(Variant(*opt, 1), 0);
{
Return()
}
)
}

// EMIT_MIR debuginfo.numbered.built.after.mir
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn numbered(i: (u32, i32)) {
mir!(
debug first => i.0;
debug second => i.0;
{
Return()
}
)
}

struct S { x: f32 }

// EMIT_MIR debuginfo.structured.built.after.mir
#[custom_mir(dialect = "analysis", phase = "post-cleanup")]
fn structured(i: S) {
mir!(
debug x => i.x;
{
Return()
}
)
}

// EMIT_MIR debuginfo.variant.built.after.mir
#[custom_mir(dialect = "built")]
fn variant(opt: Option<i32>) {
mir!(
debug inner => Field::<i32>(Variant(opt, 1), 0);
{
Return()
}
)
}

// EMIT_MIR debuginfo.variant_deref.built.after.mir
#[custom_mir(dialect = "built")]
fn variant_deref(opt: Option<&i32>) {
mir!(
debug pointer => Field::<&i32>(Variant(opt, 1), 0);
debug deref => *Field::<&i32>(Variant(opt, 1), 0);
{
Return()
}
)
}

fn main() {
numbered((5, 6));
structured(S { x: 5. });
variant(Some(5));
variant_deref(Some(&5));
pointee(&mut Some(5));
}
10 changes: 10 additions & 0 deletions tests/mir-opt/building/custom/debuginfo.structured.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// MIR for `structured` after built

fn structured(_1: S) -> () {
debug x => (_1.0: f32);
let mut _0: ();

bb0: {
return;
}
}
10 changes: 10 additions & 0 deletions tests/mir-opt/building/custom/debuginfo.variant.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// MIR for `variant` after built

fn variant(_1: Option<i32>) -> () {
debug inner => ((_1 as variant#1).0: i32);
let mut _0: ();

bb0: {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// MIR for `variant_deref` after built

fn variant_deref(_1: Option<&i32>) -> () {
debug pointer => ((_1 as variant#1).0: &i32);
debug deref => (*((_1 as variant#1).0: &i32));
let mut _0: ();

bb0: {
return;
}
}