Skip to content

Commit 2737732

Browse files
authored
Rollup merge of rust-lang#68960 - eddyb:llvm-dbg-cleanup, r=nagisa
codegen: misc cleanups around debuginfo scopes and locations. See each commit message. Most of these seem to be leftovers from the transition to MIR codegen. r? @nagisa cc @bjorn3
2 parents 9f38e14 + bdb72e7 commit 2737732

File tree

7 files changed

+46
-129
lines changed

7 files changed

+46
-129
lines changed

src/librustc_codegen_llvm/debuginfo/create_scope_map.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,8 @@ fn make_mir_scope(
6666
if !has_variables.contains(scope) {
6767
// Do not create a DIScope if there are no variables
6868
// defined in this MIR Scope, to avoid debuginfo bloat.
69-
70-
// However, we don't skip creating a nested scope if
71-
// our parent is the root, because we might want to
72-
// put arguments in the root and not have shadowing.
73-
if parent_scope.scope_metadata.unwrap() != fn_metadata {
74-
debug_context.scopes[scope] = parent_scope;
75-
return;
76-
}
69+
debug_context.scopes[scope] = parent_scope;
70+
return;
7771
}
7872

7973
let loc = span_start(cx, scope_data.span);

src/librustc_codegen_llvm/debuginfo/mod.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;
55

66
use self::metadata::{file_metadata, type_metadata, TypeMap};
77
use self::namespace::mangled_name_of_instance;
8-
use self::source_loc::InternalDebugLocation::{self, UnknownLocation};
98
use self::type_names::compute_debuginfo_type_name;
109
use self::utils::{create_DIArray, is_node_local_to_unit, span_start, DIB};
1110

@@ -38,7 +37,7 @@ use std::ffi::CString;
3837
use rustc::ty::layout::{self, HasTyCtxt, LayoutOf, Size};
3938
use rustc_codegen_ssa::traits::*;
4039
use rustc_span::symbol::Symbol;
41-
use rustc_span::{self, BytePos, Pos, Span};
40+
use rustc_span::{self, BytePos, Span};
4241
use smallvec::SmallVec;
4342
use syntax::ast;
4443

@@ -52,7 +51,6 @@ mod utils;
5251
pub use self::create_scope_map::compute_mir_scopes;
5352
pub use self::metadata::create_global_var_metadata;
5453
pub use self::metadata::extend_scope_to_file;
55-
pub use self::source_loc::set_source_location;
5654

5755
#[allow(non_upper_case_globals)]
5856
const DW_TAG_auto_variable: c_uint = 0x100;
@@ -148,20 +146,18 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
148146
// names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
149147
fn dbg_var_addr(
150148
&mut self,
151-
dbg_context: &FunctionDebugContext<&'ll DIScope>,
152149
dbg_var: &'ll DIVariable,
153150
scope_metadata: &'ll DIScope,
154151
variable_alloca: Self::Value,
155152
direct_offset: Size,
156153
indirect_offsets: &[Size],
157154
span: Span,
158155
) {
159-
assert!(!dbg_context.source_locations_enabled);
160156
let cx = self.cx();
161157

162-
let loc = span_start(cx, span);
163-
164158
// Convert the direct and indirect offsets to address ops.
159+
// FIXME(eddyb) use `const`s instead of getting the values via FFI,
160+
// the values should match the ones in the DWARF standard anyway.
165161
let op_deref = || unsafe { llvm::LLVMRustDIBuilderCreateOpDeref() };
166162
let op_plus_uconst = || unsafe { llvm::LLVMRustDIBuilderCreateOpPlusUconst() };
167163
let mut addr_ops = SmallVec::<[_; 8]>::new();
@@ -178,37 +174,32 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
178174
}
179175
}
180176

181-
// FIXME(eddyb) maybe this information could be extracted from `var`,
177+
// FIXME(eddyb) maybe this information could be extracted from `dbg_var`,
182178
// to avoid having to pass it down in both places?
183-
source_loc::set_debug_location(
184-
self,
185-
InternalDebugLocation::new(scope_metadata, loc.line, loc.col.to_usize()),
186-
);
179+
// NB: `var` doesn't seem to know about the column, so that's a limitation.
180+
let dbg_loc = cx.create_debug_loc(scope_metadata, span);
187181
unsafe {
188-
let debug_loc = llvm::LLVMGetCurrentDebugLocation(self.llbuilder);
189182
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
190-
let instr = llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
183+
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
191184
DIB(cx),
192185
variable_alloca,
193186
dbg_var,
194187
addr_ops.as_ptr(),
195188
addr_ops.len() as c_uint,
196-
debug_loc,
189+
dbg_loc,
197190
self.llbb(),
198191
);
199-
200-
llvm::LLVMSetInstDebugLocation(self.llbuilder, instr);
201192
}
202-
source_loc::set_debug_location(self, UnknownLocation);
203193
}
204194

205-
fn set_source_location(
206-
&mut self,
207-
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
208-
scope: &'ll DIScope,
209-
span: Span,
210-
) {
211-
set_source_location(debug_context, &self, scope, span)
195+
fn set_source_location(&mut self, scope: &'ll DIScope, span: Span) {
196+
debug!("set_source_location: {}", self.sess().source_map().span_to_string(span));
197+
198+
let dbg_loc = self.cx().create_debug_loc(scope, span);
199+
200+
unsafe {
201+
llvm::LLVMSetCurrentDebugLocation(self.llbuilder, dbg_loc);
202+
}
212203
}
213204
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
214205
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
@@ -342,7 +333,6 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
342333
};
343334
let mut fn_debug_context = FunctionDebugContext {
344335
scopes: IndexVec::from_elem(null_scope, &mir.source_scopes),
345-
source_locations_enabled: false,
346336
defining_crate: def_id.krate,
347337
};
348338

Original file line numberDiff line numberDiff line change
@@ -1,79 +1,35 @@
1-
use self::InternalDebugLocation::*;
2-
31
use super::metadata::UNKNOWN_COLUMN_NUMBER;
42
use super::utils::{debug_context, span_start};
5-
use rustc_codegen_ssa::mir::debuginfo::FunctionDebugContext;
63

7-
use crate::builder::Builder;
8-
use crate::llvm;
4+
use crate::common::CodegenCx;
95
use crate::llvm::debuginfo::DIScope;
10-
use log::debug;
6+
use crate::llvm::{self, Value};
117
use rustc_codegen_ssa::traits::*;
128

139
use libc::c_uint;
1410
use rustc_span::{Pos, Span};
1511

16-
/// Sets the current debug location at the beginning of the span.
17-
///
18-
/// Maps to a call to llvm::LLVMSetCurrentDebugLocation(...).
19-
pub fn set_source_location<D>(
20-
debug_context: &FunctionDebugContext<D>,
21-
bx: &Builder<'_, 'll, '_>,
22-
scope: &'ll DIScope,
23-
span: Span,
24-
) {
25-
let dbg_loc = if debug_context.source_locations_enabled {
26-
debug!("set_source_location: {}", bx.sess().source_map().span_to_string(span));
27-
let loc = span_start(bx.cx(), span);
28-
InternalDebugLocation::new(scope, loc.line, loc.col.to_usize())
29-
} else {
30-
UnknownLocation
31-
};
32-
set_debug_location(bx, dbg_loc);
33-
}
34-
35-
#[derive(Copy, Clone, PartialEq)]
36-
pub enum InternalDebugLocation<'ll> {
37-
KnownLocation { scope: &'ll DIScope, line: usize, col: usize },
38-
UnknownLocation,
39-
}
40-
41-
impl InternalDebugLocation<'ll> {
42-
pub fn new(scope: &'ll DIScope, line: usize, col: usize) -> Self {
43-
KnownLocation { scope, line, col }
44-
}
45-
}
46-
47-
pub fn set_debug_location(bx: &Builder<'_, 'll, '_>, debug_location: InternalDebugLocation<'ll>) {
48-
let metadata_node = match debug_location {
49-
KnownLocation { scope, line, col } => {
50-
// For MSVC, set the column number to zero.
51-
// Otherwise, emit it. This mimics clang behaviour.
52-
// See discussion in https://github.com/rust-lang/rust/issues/42921
53-
let col_used = if bx.sess().target.target.options.is_like_msvc {
54-
UNKNOWN_COLUMN_NUMBER
55-
} else {
56-
col as c_uint
57-
};
58-
debug!("setting debug location to {} {}", line, col);
59-
60-
unsafe {
61-
Some(llvm::LLVMRustDIBuilderCreateDebugLocation(
62-
debug_context(bx.cx()).llcontext,
63-
line as c_uint,
64-
col_used,
65-
scope,
66-
None,
67-
))
68-
}
12+
impl CodegenCx<'ll, '_> {
13+
pub fn create_debug_loc(&self, scope: &'ll DIScope, span: Span) -> &'ll Value {
14+
let loc = span_start(self, span);
15+
16+
// For MSVC, set the column number to zero.
17+
// Otherwise, emit it. This mimics clang behaviour.
18+
// See discussion in https://github.com/rust-lang/rust/issues/42921
19+
let col_used = if self.sess().target.target.options.is_like_msvc {
20+
UNKNOWN_COLUMN_NUMBER
21+
} else {
22+
loc.col.to_usize() as c_uint
23+
};
24+
25+
unsafe {
26+
llvm::LLVMRustDIBuilderCreateDebugLocation(
27+
debug_context(self).llcontext,
28+
loc.line as c_uint,
29+
col_used,
30+
scope,
31+
None,
32+
)
6933
}
70-
UnknownLocation => {
71-
debug!("clearing debug location ");
72-
None
73-
}
74-
};
75-
76-
unsafe {
77-
llvm::LLVMSetCurrentDebugLocation(bx.llbuilder, metadata_node);
7834
}
7935
}

src/librustc_codegen_llvm/llvm/ffi.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -909,9 +909,7 @@ extern "C" {
909909
pub fn LLVMDisposeBuilder(Builder: &'a mut Builder<'a>);
910910

911911
// Metadata
912-
pub fn LLVMSetCurrentDebugLocation(Builder: &Builder<'a>, L: Option<&'a Value>);
913-
pub fn LLVMGetCurrentDebugLocation(Builder: &Builder<'a>) -> &'a Value;
914-
pub fn LLVMSetInstDebugLocation(Builder: &Builder<'a>, Inst: &'a Value);
912+
pub fn LLVMSetCurrentDebugLocation(Builder: &Builder<'a>, L: &'a Value);
915913

916914
// Terminators
917915
pub fn LLVMBuildRetVoid(B: &Builder<'a>) -> &'a Value;

src/librustc_codegen_ssa/mir/debuginfo.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use super::{FunctionCx, LocalRef};
1414

1515
pub struct FunctionDebugContext<D> {
1616
pub scopes: IndexVec<mir::SourceScope, DebugScope<D>>,
17-
pub source_locations_enabled: bool,
1817
pub defining_crate: CrateNum,
1918
}
2019

@@ -53,11 +52,10 @@ impl<D> DebugScope<D> {
5352
}
5453

5554
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
56-
pub fn set_debug_loc(&mut self, bx: &mut Bx, source_info: mir::SourceInfo) {
55+
pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
5756
let (scope, span) = self.debug_loc(source_info);
58-
if let Some(debug_context) = &mut self.debug_context {
59-
// FIXME(eddyb) get rid of this unwrap somehow.
60-
bx.set_source_location(debug_context, scope.unwrap(), span);
57+
if let Some(scope) = scope {
58+
bx.set_source_location(scope, span);
6159
}
6260
}
6361

@@ -210,11 +208,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
210208
return;
211209
}
212210

213-
let debug_context = match &self.debug_context {
214-
Some(debug_context) => debug_context,
215-
None => return,
216-
};
217-
218211
// FIXME(eddyb) add debuginfo for unsized places too.
219212
let base = match local_ref {
220213
LocalRef::Place(place) => place,
@@ -264,7 +257,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
264257
if let Some(scope) = scope {
265258
if let Some(dbg_var) = var.dbg_var {
266259
bx.dbg_var_addr(
267-
debug_context,
268260
dbg_var,
269261
scope,
270262
base.llval,

src/librustc_codegen_ssa/mir/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,6 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
230230
bx.br(fx.blocks[mir::START_BLOCK]);
231231
}
232232

233-
// Up until here, IR instructions for this function have explicitly not been annotated with
234-
// source code location, so we don't step into call setup code. From here on, source location
235-
// emitting should be enabled.
236-
if let Some(debug_context) = &mut fx.debug_context {
237-
debug_context.source_locations_enabled = true;
238-
}
239-
240233
let rpo = traversal::reverse_postorder(&mir_body);
241234
let mut visited = BitSet::new_empty(mir_body.basic_blocks().len());
242235

src/librustc_codegen_ssa/traits/debuginfo.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
4949
// names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
5050
fn dbg_var_addr(
5151
&mut self,
52-
dbg_context: &FunctionDebugContext<Self::DIScope>,
5352
dbg_var: Self::DIVariable,
5453
scope_metadata: Self::DIScope,
5554
variable_alloca: Self::Value,
@@ -58,12 +57,7 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
5857
indirect_offsets: &[Size],
5958
span: Span,
6059
);
61-
fn set_source_location(
62-
&mut self,
63-
debug_context: &mut FunctionDebugContext<Self::DIScope>,
64-
scope: Self::DIScope,
65-
span: Span,
66-
);
60+
fn set_source_location(&mut self, scope: Self::DIScope, span: Span);
6761
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self);
6862
fn set_var_name(&mut self, value: Self::Value, name: &str);
6963
}

0 commit comments

Comments
 (0)