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

Preserve argument indexes when inlining MIR #109466

Merged
merged 1 commit into from
Apr 13, 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
10 changes: 4 additions & 6 deletions compiler/rustc_codegen_ssa/src/mir/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let (var_ty, var_kind) = match var.value {
mir::VarDebugInfoContents::Place(place) => {
let var_ty = self.monomorphized_place_ty(place.as_ref());
let var_kind = if self.mir.local_kind(place.local) == mir::LocalKind::Arg
let var_kind = if let Some(arg_index) = var.argument_index
&& place.projection.is_empty()
&& var.source_info.scope == mir::OUTERMOST_SOURCE_SCOPE
{
let arg_index = place.local.index() - 1;
let arg_index = arg_index as usize;
if target_is_msvc {
// ScalarPair parameters are spilled to the stack so they need to
// be marked as a `LocalVariable` for MSVC debuggers to visualize
Expand All @@ -455,13 +454,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
if let Abi::ScalarPair(_, _) = var_ty_layout.abi {
VariableKind::LocalVariable
} else {
VariableKind::ArgumentVariable(arg_index + 1)
VariableKind::ArgumentVariable(arg_index)
}
} else {
// FIXME(eddyb) shouldn't `ArgumentVariable` indices be
// offset in closures to account for the hidden environment?
// Also, is this `+ 1` needed at all?
VariableKind::ArgumentVariable(arg_index + 1)
VariableKind::ArgumentVariable(arg_index)
}
} else {
VariableKind::LocalVariable
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,11 @@ pub struct VarDebugInfo<'tcx> {

/// Where the data for this user variable is to be found.
pub value: VarDebugInfoContents<'tcx>,

/// When present, indicates what argument number this variable is in the function that it
/// originated from (starting from 1). Note, if MIR inlining is enabled, then this is the
/// argument number in the original function before it was inlined.
pub argument_index: Option<u16>,
}

///////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ macro_rules! make_mir_visitor {
name: _,
source_info,
value,
argument_index: _,
} = var_debug_info;

self.visit_source_info(source_info);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
name,
source_info: debug_source_info,
value: VarDebugInfoContents::Place(for_arm_body.into()),
argument_index: None,
});
let locals = if has_guard.0 {
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
Expand All @@ -2260,6 +2261,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
name,
source_info: debug_source_info,
value: VarDebugInfoContents::Place(ref_for_guard.into()),
argument_index: None,
});
LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
} else {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
name,
source_info: SourceInfo::outermost(captured_place.var_ident.span),
value: VarDebugInfoContents::Place(use_place),
argument_index: None,
});

let capture = Capture { captured_place, use_place, mutability };
Expand All @@ -827,7 +828,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr: &Expr<'tcx>,
) -> BlockAnd<()> {
// Allocate locals for the function arguments
for param in arguments.iter() {
for (argument_index, param) in arguments.iter().enumerate() {
let source_info =
SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
let arg_local =
Expand All @@ -839,6 +840,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
name,
source_info,
value: VarDebugInfoContents::Place(arg_local.into()),
argument_index: Some(argument_index as u16 + 1),
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,13 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
body.arg_count = 2; // self, resume arg
body.spread_arg = None;

// The original arguments to the function are no longer arguments, mark them as such.
// Otherwise they'll conflict with our new arguments, which although they don't have
// argument_index set, will get emitted as unnamed arguments.
for var in &mut body.var_debug_info {
var.argument_index = None;
}

body.generator.as_mut().unwrap().yield_ty = None;
body.generator.as_mut().unwrap().generator_layout = Some(layout);

Expand Down
20 changes: 20 additions & 0 deletions tests/codegen/inline-function-args-debug-info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This test checks that debug information includes function argument indexes even if the function
// gets inlined by MIR inlining. Without function argument indexes, `info args` in gdb won't show
// arguments and their values for the current function.

// compile-flags: -Zinline-mir=yes -Cdebuginfo=2 --edition=2021

#![crate_type = "lib"]

pub fn outer_function(x: usize, y: usize) -> usize {
inner_function(x, y) + 1
}

#[inline]
fn inner_function(aaaa: usize, bbbb: usize) -> usize {
// CHECK: !DILocalVariable(name: "aaaa", arg: 1
// CHECK-SAME: line: 14
// CHECK: !DILocalVariable(name: "bbbb", arg: 2
// CHECK-SAME: line: 14
aaaa + bbbb
}
25 changes: 25 additions & 0 deletions tests/ui/async-await/task-context-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Checks that we don't get conflicting arguments in our debug info with a particular async function
// structure.

// edition:2021
// compile-flags: -Cdebuginfo=2
// build-pass

#![crate_type = "lib"]

use std::future::Future;

// The compiler produces a closure as part of this function. That closure initially takes an
// argument _task_context. Later, when the MIR for that closure is transformed into a generator
// state machine, _task_context is demoted to not be an argument, but just part of an unnamed
// argument. If we emit debug info saying that both _task_context and the unnamed argument are both
// argument number 2, then LLVM will fail with "conflicting debug info for argument". See
// https://github.com/rust-lang/rust/pull/109466#issuecomment-1500879195 for details.
async fn recv_unit() {
std::future::ready(()).await;
}

pub fn poll_recv() {
// This box is necessary in order to reproduce the problem.
let _: Box<dyn Future<Output = ()>> = Box::new(recv_unit());
}