-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Introduce debuginfo to statements in MIR #142771
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
Open
dianqk
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
dianqk:mir-stmt-debuginfo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,157
−620
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e0303e
mir-opt: Eliminate dead ref statements
dianqk 1717a09
codegen: Generate `dbg_value` for the ref statement
dianqk 916910c
simplifycfg: Preserve debuginfos when merging bbs
dianqk 8625fa4
mir-opt: Eliminate dead statements even if they are used by debuginfos
dianqk 9e585d4
mir-opt: Eliminate trivial unnecessary storage annotations
dianqk 10fb5e3
mir-opt: Postpone the `MatchBranchSimplification` to after the final DSE
dianqk 94ddfe6
mir: Simplify the handling of debuginfos
dianqk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -160,19 +160,23 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> { | |
&mut self, | ||
dbg_var: &'ll DIVariable, | ||
dbg_loc: &'ll DILocation, | ||
variable_alloca: Self::Value, | ||
is_declared: bool, | ||
val: Self::Value, | ||
direct_offset: Size, | ||
indirect_offsets: &[Size], | ||
fragment: Option<Range<Size>>, | ||
) { | ||
use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst}; | ||
use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst, DW_OP_stack_value}; | ||
|
||
// Convert the direct and indirect offsets and fragment byte range to address ops. | ||
let mut addr_ops = SmallVec::<[u64; 8]>::new(); | ||
|
||
if direct_offset.bytes() > 0 { | ||
addr_ops.push(DW_OP_plus_uconst); | ||
addr_ops.push(direct_offset.bytes() as u64); | ||
if !is_declared { | ||
addr_ops.push(DW_OP_stack_value); | ||
} | ||
} | ||
for &offset in indirect_offsets { | ||
addr_ops.push(DW_OP_deref); | ||
|
@@ -189,17 +193,30 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> { | |
addr_ops.push((fragment.end - fragment.start).bits() as u64); | ||
} | ||
|
||
unsafe { | ||
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`. | ||
llvm::LLVMRustDIBuilderInsertDeclareAtEnd( | ||
DIB(self.cx()), | ||
variable_alloca, | ||
dbg_var, | ||
addr_ops.as_ptr(), | ||
addr_ops.len() as c_uint, | ||
dbg_loc, | ||
self.llbb(), | ||
); | ||
if is_declared { | ||
unsafe { | ||
llvm::LLVMRustDIBuilderInsertDeclareAtEnd( | ||
DIB(self.cx()), | ||
val, | ||
dbg_var, | ||
addr_ops.as_ptr(), | ||
addr_ops.len() as c_uint, | ||
dbg_loc, | ||
self.llbb(), | ||
); | ||
} | ||
} else { | ||
unsafe { | ||
llvm::LLVMRustDIBuilderInsertDbgValueAtEnd( | ||
DIB(self.cx()), | ||
val, | ||
dbg_var, | ||
addr_ops.as_ptr(), | ||
addr_ops.len() as c_uint, | ||
dbg_loc, | ||
self.llbb(), | ||
); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to move all debuginfo to |
||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 +1,17 @@ | ||
use rustc_middle::mir::{self, NonDivergingIntrinsic}; | ||
use rustc_middle::span_bug; | ||
use rustc_middle::mir::{self, NonDivergingIntrinsic, StmtDebugInfo, StmtDebugInfos}; | ||
use rustc_middle::{bug, span_bug}; | ||
use tracing::instrument; | ||
|
||
use super::{FunctionCx, LocalRef}; | ||
use crate::common::TypeKind; | ||
use crate::mir::operand::OperandValue; | ||
use crate::mir::place::PlaceRef; | ||
use crate::traits::*; | ||
|
||
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | ||
#[instrument(level = "debug", skip(self, bx))] | ||
pub(crate) fn codegen_statement(&mut self, bx: &mut Bx, statement: &mir::Statement<'tcx>) { | ||
self.codegen_stmt_debuginfos(bx, &statement.debuginfos); | ||
self.set_debug_loc(bx, statement.source_info); | ||
match statement.kind { | ||
mir::StatementKind::Assign(box (ref place, ref rvalue)) => { | ||
|
@@ -101,4 +105,70 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| mir::StatementKind::Nop => {} | ||
} | ||
} | ||
|
||
pub(crate) fn codegen_stmt_debuginfo(&mut self, bx: &mut Bx, debuginfo: &StmtDebugInfo<'tcx>) { | ||
match debuginfo { | ||
StmtDebugInfo::AssignRef(dest, place) => { | ||
let assign_ref = if let Some(place) = place { | ||
let place_ref = match self.locals[place.local] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
LocalRef::Place(place_ref) | LocalRef::UnsizedPlace(place_ref) => { | ||
Some(place_ref) | ||
} | ||
LocalRef::Operand(operand_ref) => match operand_ref.val { | ||
OperandValue::Immediate(v) => { | ||
Some(PlaceRef::new_sized(v, operand_ref.layout)) | ||
} | ||
OperandValue::Ref(_) | ||
| OperandValue::Pair(_, _) | ||
| OperandValue::ZeroSized => None, | ||
}, | ||
LocalRef::PendingOperand => None, | ||
} | ||
.filter(|place_ref| { | ||
// Drop unsupported projections. | ||
// FIXME: Add a test case. | ||
place.projection.iter().all(|p| p.can_use_in_debuginfo()) && | ||
// Only pointers can calculate addresses. | ||
bx.type_kind(bx.val_ty(place_ref.val.llval)) == TypeKind::Pointer | ||
}); | ||
match (place_ref, place.is_indirect_first_projection()) { | ||
(Some(place_ref), false) => { | ||
Some((place_ref.val, place_ref.layout, place.projection.as_slice())) | ||
} | ||
(Some(place_ref), true) => { | ||
let projected_ty = | ||
place_ref.layout.ty.builtin_deref(true).unwrap_or_else(|| { | ||
bug!("deref of non-pointer {:?}", place_ref) | ||
}); | ||
let layout = bx.cx().layout_of(projected_ty); | ||
Some((place_ref.val, layout, &place.projection[1..])) | ||
} | ||
_ => None, | ||
} | ||
} else { | ||
None | ||
}; | ||
let (val, layout, projection) = assign_ref.unwrap_or_else(|| { | ||
// If the address cannot be computed, use poison to indicate that the value has been optimized out. | ||
let ty = self.monomorphize(self.mir.local_decls[*dest].ty); | ||
let layout = bx.cx().layout_of(ty); | ||
let to_backend_ty = bx.cx().immediate_backend_type(layout); | ||
let place_ref = | ||
PlaceRef::new_sized(bx.cx().const_poison(to_backend_ty), layout); | ||
(place_ref.val, layout, [].as_slice()) | ||
}); | ||
self.debug_new_value_to_local(bx, *dest, val, layout, projection); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn codegen_stmt_debuginfos( | ||
&mut self, | ||
bx: &mut Bx, | ||
debuginfos: &StmtDebugInfos<'tcx>, | ||
) { | ||
for debuginfo in debuginfos.iter() { | ||
self.codegen_stmt_debuginfo(bx, debuginfo); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not certain I understand how the codegen part works. Is the
is_declared
a property of the MIR local? Or can the same MIR local mix declared and undeclared debuginfo? When the local is declared, what doesval
represent? Should we have anenum LocalDbgValue { ByRef(Value), ByValue(Value) }
?