Skip to content

Commit 7c27cce

Browse files
committed
Auto merge of #32803 - eddyb:mir-debuginfo, r=nikomatsakis
Initial implementation of debuginfo in MIR trans. Progress is made towards #31005, but several issues remain, such as #32790.
2 parents a43eb4e + 373b6ec commit 7c27cce

39 files changed

+560
-635
lines changed

src/compiletest/runtest.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -868,27 +868,11 @@ fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
868868
"-g".to_owned(),
869869
"--debuginfo".to_owned()
870870
];
871-
let mut new_options =
871+
let new_options =
872872
split_maybe_args(options).into_iter()
873873
.filter(|x| !options_to_remove.contains(x))
874874
.collect::<Vec<String>>();
875875

876-
let mut i = 0;
877-
while i + 1 < new_options.len() {
878-
if new_options[i] == "-Z" {
879-
// FIXME #31005 MIR missing debuginfo currently.
880-
if new_options[i + 1] == "orbit" {
881-
// Remove "-Z" and "orbit".
882-
new_options.remove(i);
883-
new_options.remove(i);
884-
continue;
885-
}
886-
// Always skip over -Z's argument.
887-
i += 1;
888-
}
889-
i += 1;
890-
}
891-
892876
Some(new_options.join(" "))
893877
}
894878

src/librustc/mir/repr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ impl ScopeId {
698698

699699
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
700700
pub struct ScopeData {
701+
pub span: Span,
701702
pub parent_scope: Option<ScopeId>,
702703
}
703704

src/librustc/mir/visit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,11 @@ macro_rules! make_mir_visitor {
298298
fn super_scope_data(&mut self,
299299
scope_data: & $($mutability)* ScopeData) {
300300
let ScopeData {
301+
ref $($mutability)* span,
301302
ref $($mutability)* parent_scope,
302303
} = *scope_data;
303304

305+
self.visit_span(span);
304306
if let Some(ref $($mutability)* parent_scope) = *parent_scope {
305307
self.visit_scope_id(parent_scope);
306308
}

src/librustc_mir/build/scope.rs

+2
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ impl<'a,'tcx> Builder<'a,'tcx> {
255255
debug!("push_scope({:?})", extent);
256256
let parent_id = self.scopes.last().map(|s| s.id);
257257
let id = ScopeId::new(self.scope_datas.len());
258+
let tcx = self.hir.tcx();
258259
self.scope_datas.push(ScopeData {
260+
span: extent.span(&tcx.region_maps, &tcx.map).unwrap_or(DUMMY_SP),
259261
parent_scope: parent_id,
260262
});
261263
self.scopes.push(Scope {

src/librustc_mir/pretty.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ fn write_mir_intro(tcx: &TyCtxt, nid: NodeId, mir: &Mir, w: &mut Write)
242242
if var.mutability == Mutability::Mut {
243243
write!(w, "mut ")?;
244244
}
245-
writeln!(w, "{:?}: {}; // {}", Lvalue::Var(i as u32), var.ty, var.name)?;
245+
writeln!(w, "{:?}: {}; // {} in {}",
246+
Lvalue::Var(i as u32),
247+
var.ty,
248+
var.name,
249+
comment(tcx, var.scope, var.span))?;
246250
}
247251

248252
// Compiler-introduced temporary types.

src/librustc_trans/base.rs

+40-36
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ pub fn alloca(cx: Block, ty: Type, name: &str) -> ValueRef {
12761276
return llvm::LLVMGetUndef(ty.ptr_to().to_ref());
12771277
}
12781278
}
1279-
debuginfo::clear_source_location(cx.fcx);
1279+
DebugLoc::None.apply(cx.fcx);
12801280
Alloca(cx, ty, name)
12811281
}
12821282

@@ -1400,23 +1400,23 @@ impl<'blk, 'tcx> FunctionContext<'blk, 'tcx> {
14001400
pub fn new(ccx: &'blk CrateContext<'blk, 'tcx>,
14011401
llfndecl: ValueRef,
14021402
fn_ty: FnType,
1403-
def_id: Option<DefId>,
1404-
param_substs: &'tcx Substs<'tcx>,
1403+
definition: Option<(Instance<'tcx>, &ty::FnSig<'tcx>, Abi)>,
14051404
block_arena: &'blk TypedArena<common::BlockS<'blk, 'tcx>>)
14061405
-> FunctionContext<'blk, 'tcx> {
1407-
common::validate_substs(param_substs);
1406+
let (param_substs, def_id) = match definition {
1407+
Some((instance, _, _)) => {
1408+
common::validate_substs(instance.substs);
1409+
(instance.substs, Some(instance.def))
1410+
}
1411+
None => (ccx.tcx().mk_substs(Substs::empty()), None)
1412+
};
14081413

14091414
let inlined_did = def_id.and_then(|def_id| inline::get_local_instance(ccx, def_id));
14101415
let inlined_id = inlined_did.and_then(|id| ccx.tcx().map.as_local_node_id(id));
14111416
let local_id = def_id.and_then(|id| ccx.tcx().map.as_local_node_id(id));
14121417

1413-
debug!("FunctionContext::new(path={}, def_id={:?}, param_substs={:?})",
1414-
inlined_id.map_or(String::new(), |id| ccx.tcx().node_path_str(id)),
1415-
def_id,
1416-
param_substs);
1417-
1418-
let debug_context = debuginfo::create_function_debug_context(ccx,
1419-
inlined_id.unwrap_or(ast::DUMMY_NODE_ID), param_substs, llfndecl);
1418+
debug!("FunctionContext::new({})",
1419+
definition.map_or(String::new(), |d| d.0.to_string()));
14201420

14211421
let cfg = inlined_id.map(|id| build_cfg(ccx.tcx(), id));
14221422
let nested_returns = if let Some((blk_id, Some(ref cfg))) = cfg {
@@ -1428,10 +1428,11 @@ impl<'blk, 'tcx> FunctionContext<'blk, 'tcx> {
14281428
let check_attrs = |attrs: &[ast::Attribute]| {
14291429
let default_to_mir = ccx.sess().opts.debugging_opts.orbit;
14301430
let invert = if default_to_mir { "rustc_no_mir" } else { "rustc_mir" };
1431-
default_to_mir ^ attrs.iter().any(|item| item.check_name(invert))
1431+
(default_to_mir ^ attrs.iter().any(|item| item.check_name(invert)),
1432+
attrs.iter().any(|item| item.check_name("no_debug")))
14321433
};
14331434

1434-
let use_mir = if let Some(id) = local_id {
1435+
let (use_mir, no_debug) = if let Some(id) = local_id {
14351436
check_attrs(ccx.tcx().map.attrs(id))
14361437
} else if let Some(def_id) = def_id {
14371438
check_attrs(&ccx.sess().cstore.item_attrs(def_id))
@@ -1445,6 +1446,13 @@ impl<'blk, 'tcx> FunctionContext<'blk, 'tcx> {
14451446
None
14461447
};
14471448

1449+
let debug_context = if let (false, Some(definition)) = (no_debug, definition) {
1450+
let (instance, sig, abi) = definition;
1451+
debuginfo::create_function_debug_context(ccx, instance, sig, abi, llfndecl)
1452+
} else {
1453+
debuginfo::empty_function_debug_context(ccx)
1454+
};
1455+
14481456
FunctionContext {
14491457
needs_ret_allocas: nested_returns && mir.is_none(),
14501458
mir: mir,
@@ -1731,7 +1739,7 @@ impl<'blk, 'tcx> FunctionContext<'blk, 'tcx> {
17311739

17321740
self.build_return_block(ret_cx, ret_debug_loc);
17331741

1734-
debuginfo::clear_source_location(self);
1742+
DebugLoc::None.apply(self);
17351743
self.cleanup();
17361744
}
17371745

@@ -1810,32 +1818,34 @@ pub fn trans_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
18101818
decl: &hir::FnDecl,
18111819
body: &hir::Block,
18121820
llfndecl: ValueRef,
1813-
param_substs: &'tcx Substs<'tcx>,
1814-
def_id: DefId,
1821+
instance: Instance<'tcx>,
18151822
inlined_id: ast::NodeId,
1816-
fn_ty: FnType,
1823+
sig: &ty::FnSig<'tcx>,
18171824
abi: Abi,
18181825
closure_env: closure::ClosureEnv) {
18191826
ccx.stats().n_closures.set(ccx.stats().n_closures.get() + 1);
18201827

18211828
if collector::collecting_debug_information(ccx) {
1822-
ccx.record_translation_item_as_generated(
1823-
TransItem::Fn(Instance::new(def_id, param_substs)));
1829+
ccx.record_translation_item_as_generated(TransItem::Fn(instance));
18241830
}
18251831

18261832
let _icx = push_ctxt("trans_closure");
18271833
attributes::emit_uwtable(llfndecl, true);
18281834

1829-
debug!("trans_closure(..., param_substs={:?})", param_substs);
1835+
debug!("trans_closure(..., {})", instance);
1836+
1837+
let fn_ty = FnType::new(ccx, abi, sig, &[]);
18301838

18311839
let (arena, fcx): (TypedArena<_>, FunctionContext);
18321840
arena = TypedArena::new();
1833-
fcx = FunctionContext::new(ccx, llfndecl, fn_ty, Some(def_id), param_substs, &arena);
1841+
fcx = FunctionContext::new(ccx, llfndecl, fn_ty, Some((instance, sig, abi)), &arena);
18341842

18351843
if fcx.mir.is_some() {
18361844
return mir::trans_mir(&fcx);
18371845
}
18381846

1847+
debuginfo::fill_scope_map_for_function(&fcx, decl, body, inlined_id);
1848+
18391849
// cleanup scope for the incoming arguments
18401850
let fn_cleanup_debug_loc = debuginfo::get_cleanup_debug_loc_for_ast_node(
18411851
ccx, inlined_id, body.span, true);
@@ -1890,10 +1900,8 @@ pub fn trans_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
18901900
}
18911901
}
18921902

1893-
let ret_debug_loc = DebugLoc::At(fn_cleanup_debug_loc.id, fn_cleanup_debug_loc.span);
1894-
18951903
// Insert the mandatory first few basic blocks before lltop.
1896-
fcx.finish(bcx, ret_debug_loc);
1904+
fcx.finish(bcx, fn_cleanup_debug_loc.debug_loc());
18971905
}
18981906

18991907
/// Creates an LLVM function corresponding to a source language function.
@@ -1906,25 +1914,23 @@ pub fn trans_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
19061914
let _s = StatRecorder::new(ccx, ccx.tcx().node_path_str(id));
19071915
debug!("trans_fn(param_substs={:?})", param_substs);
19081916
let _icx = push_ctxt("trans_fn");
1909-
let fn_ty = ccx.tcx().node_id_to_type(id);
1910-
let fn_ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &fn_ty);
1911-
let sig = ccx.tcx().erase_late_bound_regions(fn_ty.fn_sig());
1912-
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
1913-
let abi = fn_ty.fn_abi();
1914-
let fn_ty = FnType::new(ccx, abi, &sig, &[]);
19151917
let def_id = if let Some(&def_id) = ccx.external_srcs().borrow().get(&id) {
19161918
def_id
19171919
} else {
19181920
ccx.tcx().map.local_def_id(id)
19191921
};
1922+
let fn_ty = ccx.tcx().lookup_item_type(def_id).ty;
1923+
let fn_ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &fn_ty);
1924+
let sig = ccx.tcx().erase_late_bound_regions(fn_ty.fn_sig());
1925+
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
1926+
let abi = fn_ty.fn_abi();
19201927
trans_closure(ccx,
19211928
decl,
19221929
body,
19231930
llfndecl,
1924-
param_substs,
1925-
def_id,
1931+
Instance::new(def_id, param_substs),
19261932
id,
1927-
fn_ty,
1933+
&sig,
19281934
abi,
19291935
closure::ClosureEnv::NotClosure);
19301936
}
@@ -2015,9 +2021,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
20152021

20162022
let (arena, fcx): (TypedArena<_>, FunctionContext);
20172023
arena = TypedArena::new();
2018-
fcx = FunctionContext::new(ccx, llfndecl, fn_ty,
2019-
Some(ccx.tcx().map.local_def_id(ctor_id)),
2020-
param_substs, &arena);
2024+
fcx = FunctionContext::new(ccx, llfndecl, fn_ty, None, &arena);
20212025
let bcx = fcx.init(false, None);
20222026

20232027
assert!(!fcx.needs_ret_allocas);

src/librustc_trans/callee.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use middle::cstore::LOCAL_CRATE;
2424
use rustc::hir::def_id::DefId;
2525
use rustc::infer;
2626
use rustc::ty::subst;
27-
use rustc::ty::subst::{Substs};
2827
use rustc::traits;
2928
use rustc::hir::map as hir_map;
3029
use abi::{Abi, FnType};
@@ -385,10 +384,9 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
385384
let llfn = declare::define_internal_fn(ccx, &function_name, tuple_fn_ty);
386385

387386
//
388-
let empty_substs = tcx.mk_substs(Substs::empty());
389387
let (block_arena, fcx): (TypedArena<_>, FunctionContext);
390388
block_arena = TypedArena::new();
391-
fcx = FunctionContext::new(ccx, llfn, fn_ty, None, empty_substs, &block_arena);
389+
fcx = FunctionContext::new(ccx, llfn, fn_ty, None, &block_arena);
392390
let mut bcx = fcx.init(false, None);
393391

394392
let llargs = get_params(fcx.llfn);

src/librustc_trans/closure.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,14 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
234234
output: sig.output,
235235
variadic: false
236236
};
237-
let fn_ty = FnType::new(ccx, Abi::RustCall, &sig, &[]);
238237

239238
trans_closure(ccx,
240239
decl,
241240
body,
242241
llfn,
243-
param_substs,
244-
closure_def_id,
242+
Instance::new(closure_def_id, param_substs),
245243
id,
246-
fn_ty,
244+
&sig,
247245
Abi::RustCall,
248246
ClosureEnv::Closure(closure_def_id, id));
249247

@@ -387,7 +385,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
387385

388386
let (block_arena, fcx): (TypedArena<_>, FunctionContext);
389387
block_arena = TypedArena::new();
390-
fcx = FunctionContext::new(ccx, lloncefn, fn_ty, None, substs.func_substs, &block_arena);
388+
fcx = FunctionContext::new(ccx, lloncefn, fn_ty, None, &block_arena);
391389
let mut bcx = fcx.init(false, None);
392390

393391

src/librustc_trans/controlflow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
167167
if cv == 1 {
168168
// if true { .. } [else { .. }]
169169
bcx = trans_block(bcx, &thn, dest);
170-
debuginfo::clear_source_location(bcx.fcx);
170+
DebugLoc::None.apply(bcx.fcx);
171171
} else {
172172
if let Some(elexpr) = els {
173173
bcx = expr::trans_into(bcx, &elexpr, dest);
174-
debuginfo::clear_source_location(bcx.fcx);
174+
DebugLoc::None.apply(bcx.fcx);
175175
}
176176
}
177177

@@ -181,7 +181,7 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
181181
let name = format!("then-block-{}-", thn.id);
182182
let then_bcx_in = bcx.fcx.new_id_block(&name[..], thn.id);
183183
let then_bcx_out = trans_block(then_bcx_in, &thn, dest);
184-
debuginfo::clear_source_location(bcx.fcx);
184+
DebugLoc::None.apply(bcx.fcx);
185185

186186
let cond_source_loc = cond.debug_loc();
187187

@@ -204,7 +204,7 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
204204

205205
// Clear the source location because it is still set to whatever has been translated
206206
// right before.
207-
debuginfo::clear_source_location(next_bcx.fcx);
207+
DebugLoc::None.apply(next_bcx.fcx);
208208

209209
next_bcx
210210
}

0 commit comments

Comments
 (0)