Skip to content

Commit

Permalink
Auto merge of #32803 - eddyb:mir-debuginfo, r=nikomatsakis
Browse files Browse the repository at this point in the history
Initial implementation of debuginfo in MIR trans.

Progress is made towards #31005, but several issues remain, such as #32790.
  • Loading branch information
bors committed Apr 13, 2016
2 parents a43eb4e + 373b6ec commit 7c27cce
Show file tree
Hide file tree
Showing 39 changed files with 560 additions and 635 deletions.
18 changes: 1 addition & 17 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,27 +868,11 @@ fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
"-g".to_owned(),
"--debuginfo".to_owned()
];
let mut new_options =
let new_options =
split_maybe_args(options).into_iter()
.filter(|x| !options_to_remove.contains(x))
.collect::<Vec<String>>();

let mut i = 0;
while i + 1 < new_options.len() {
if new_options[i] == "-Z" {
// FIXME #31005 MIR missing debuginfo currently.
if new_options[i + 1] == "orbit" {
// Remove "-Z" and "orbit".
new_options.remove(i);
new_options.remove(i);
continue;
}
// Always skip over -Z's argument.
i += 1;
}
i += 1;
}

Some(new_options.join(" "))
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc/mir/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ impl ScopeId {

#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct ScopeData {
pub span: Span,
pub parent_scope: Option<ScopeId>,
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,11 @@ macro_rules! make_mir_visitor {
fn super_scope_data(&mut self,
scope_data: & $($mutability)* ScopeData) {
let ScopeData {
ref $($mutability)* span,
ref $($mutability)* parent_scope,
} = *scope_data;

self.visit_span(span);
if let Some(ref $($mutability)* parent_scope) = *parent_scope {
self.visit_scope_id(parent_scope);
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ impl<'a,'tcx> Builder<'a,'tcx> {
debug!("push_scope({:?})", extent);
let parent_id = self.scopes.last().map(|s| s.id);
let id = ScopeId::new(self.scope_datas.len());
let tcx = self.hir.tcx();
self.scope_datas.push(ScopeData {
span: extent.span(&tcx.region_maps, &tcx.map).unwrap_or(DUMMY_SP),
parent_scope: parent_id,
});
self.scopes.push(Scope {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ fn write_mir_intro(tcx: &TyCtxt, nid: NodeId, mir: &Mir, w: &mut Write)
if var.mutability == Mutability::Mut {
write!(w, "mut ")?;
}
writeln!(w, "{:?}: {}; // {}", Lvalue::Var(i as u32), var.ty, var.name)?;
writeln!(w, "{:?}: {}; // {} in {}",
Lvalue::Var(i as u32),
var.ty,
var.name,
comment(tcx, var.scope, var.span))?;
}

// Compiler-introduced temporary types.
Expand Down
76 changes: 40 additions & 36 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ pub fn alloca(cx: Block, ty: Type, name: &str) -> ValueRef {
return llvm::LLVMGetUndef(ty.ptr_to().to_ref());
}
}
debuginfo::clear_source_location(cx.fcx);
DebugLoc::None.apply(cx.fcx);
Alloca(cx, ty, name)
}

Expand Down Expand Up @@ -1400,23 +1400,23 @@ impl<'blk, 'tcx> FunctionContext<'blk, 'tcx> {
pub fn new(ccx: &'blk CrateContext<'blk, 'tcx>,
llfndecl: ValueRef,
fn_ty: FnType,
def_id: Option<DefId>,
param_substs: &'tcx Substs<'tcx>,
definition: Option<(Instance<'tcx>, &ty::FnSig<'tcx>, Abi)>,
block_arena: &'blk TypedArena<common::BlockS<'blk, 'tcx>>)
-> FunctionContext<'blk, 'tcx> {
common::validate_substs(param_substs);
let (param_substs, def_id) = match definition {
Some((instance, _, _)) => {
common::validate_substs(instance.substs);
(instance.substs, Some(instance.def))
}
None => (ccx.tcx().mk_substs(Substs::empty()), None)
};

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

debug!("FunctionContext::new(path={}, def_id={:?}, param_substs={:?})",
inlined_id.map_or(String::new(), |id| ccx.tcx().node_path_str(id)),
def_id,
param_substs);

let debug_context = debuginfo::create_function_debug_context(ccx,
inlined_id.unwrap_or(ast::DUMMY_NODE_ID), param_substs, llfndecl);
debug!("FunctionContext::new({})",
definition.map_or(String::new(), |d| d.0.to_string()));

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

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

let debug_context = if let (false, Some(definition)) = (no_debug, definition) {
let (instance, sig, abi) = definition;
debuginfo::create_function_debug_context(ccx, instance, sig, abi, llfndecl)
} else {
debuginfo::empty_function_debug_context(ccx)
};

FunctionContext {
needs_ret_allocas: nested_returns && mir.is_none(),
mir: mir,
Expand Down Expand Up @@ -1731,7 +1739,7 @@ impl<'blk, 'tcx> FunctionContext<'blk, 'tcx> {

self.build_return_block(ret_cx, ret_debug_loc);

debuginfo::clear_source_location(self);
DebugLoc::None.apply(self);
self.cleanup();
}

Expand Down Expand Up @@ -1810,32 +1818,34 @@ pub fn trans_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
decl: &hir::FnDecl,
body: &hir::Block,
llfndecl: ValueRef,
param_substs: &'tcx Substs<'tcx>,
def_id: DefId,
instance: Instance<'tcx>,
inlined_id: ast::NodeId,
fn_ty: FnType,
sig: &ty::FnSig<'tcx>,
abi: Abi,
closure_env: closure::ClosureEnv) {
ccx.stats().n_closures.set(ccx.stats().n_closures.get() + 1);

if collector::collecting_debug_information(ccx) {
ccx.record_translation_item_as_generated(
TransItem::Fn(Instance::new(def_id, param_substs)));
ccx.record_translation_item_as_generated(TransItem::Fn(instance));
}

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

debug!("trans_closure(..., param_substs={:?})", param_substs);
debug!("trans_closure(..., {})", instance);

let fn_ty = FnType::new(ccx, abi, sig, &[]);

let (arena, fcx): (TypedArena<_>, FunctionContext);
arena = TypedArena::new();
fcx = FunctionContext::new(ccx, llfndecl, fn_ty, Some(def_id), param_substs, &arena);
fcx = FunctionContext::new(ccx, llfndecl, fn_ty, Some((instance, sig, abi)), &arena);

if fcx.mir.is_some() {
return mir::trans_mir(&fcx);
}

debuginfo::fill_scope_map_for_function(&fcx, decl, body, inlined_id);

// cleanup scope for the incoming arguments
let fn_cleanup_debug_loc = debuginfo::get_cleanup_debug_loc_for_ast_node(
ccx, inlined_id, body.span, true);
Expand Down Expand Up @@ -1890,10 +1900,8 @@ pub fn trans_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
}
}

let ret_debug_loc = DebugLoc::At(fn_cleanup_debug_loc.id, fn_cleanup_debug_loc.span);

// Insert the mandatory first few basic blocks before lltop.
fcx.finish(bcx, ret_debug_loc);
fcx.finish(bcx, fn_cleanup_debug_loc.debug_loc());
}

/// Creates an LLVM function corresponding to a source language function.
Expand All @@ -1906,25 +1914,23 @@ pub fn trans_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let _s = StatRecorder::new(ccx, ccx.tcx().node_path_str(id));
debug!("trans_fn(param_substs={:?})", param_substs);
let _icx = push_ctxt("trans_fn");
let fn_ty = ccx.tcx().node_id_to_type(id);
let fn_ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &fn_ty);
let sig = ccx.tcx().erase_late_bound_regions(fn_ty.fn_sig());
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
let abi = fn_ty.fn_abi();
let fn_ty = FnType::new(ccx, abi, &sig, &[]);
let def_id = if let Some(&def_id) = ccx.external_srcs().borrow().get(&id) {
def_id
} else {
ccx.tcx().map.local_def_id(id)
};
let fn_ty = ccx.tcx().lookup_item_type(def_id).ty;
let fn_ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &fn_ty);
let sig = ccx.tcx().erase_late_bound_regions(fn_ty.fn_sig());
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
let abi = fn_ty.fn_abi();
trans_closure(ccx,
decl,
body,
llfndecl,
param_substs,
def_id,
Instance::new(def_id, param_substs),
id,
fn_ty,
&sig,
abi,
closure::ClosureEnv::NotClosure);
}
Expand Down Expand Up @@ -2015,9 +2021,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,

let (arena, fcx): (TypedArena<_>, FunctionContext);
arena = TypedArena::new();
fcx = FunctionContext::new(ccx, llfndecl, fn_ty,
Some(ccx.tcx().map.local_def_id(ctor_id)),
param_substs, &arena);
fcx = FunctionContext::new(ccx, llfndecl, fn_ty, None, &arena);
let bcx = fcx.init(false, None);

assert!(!fcx.needs_ret_allocas);
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use middle::cstore::LOCAL_CRATE;
use rustc::hir::def_id::DefId;
use rustc::infer;
use rustc::ty::subst;
use rustc::ty::subst::{Substs};
use rustc::traits;
use rustc::hir::map as hir_map;
use abi::{Abi, FnType};
Expand Down Expand Up @@ -385,10 +384,9 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
let llfn = declare::define_internal_fn(ccx, &function_name, tuple_fn_ty);

//
let empty_substs = tcx.mk_substs(Substs::empty());
let (block_arena, fcx): (TypedArena<_>, FunctionContext);
block_arena = TypedArena::new();
fcx = FunctionContext::new(ccx, llfn, fn_ty, None, empty_substs, &block_arena);
fcx = FunctionContext::new(ccx, llfn, fn_ty, None, &block_arena);
let mut bcx = fcx.init(false, None);

let llargs = get_params(fcx.llfn);
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,14 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
output: sig.output,
variadic: false
};
let fn_ty = FnType::new(ccx, Abi::RustCall, &sig, &[]);

trans_closure(ccx,
decl,
body,
llfn,
param_substs,
closure_def_id,
Instance::new(closure_def_id, param_substs),
id,
fn_ty,
&sig,
Abi::RustCall,
ClosureEnv::Closure(closure_def_id, id));

Expand Down Expand Up @@ -387,7 +385,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(

let (block_arena, fcx): (TypedArena<_>, FunctionContext);
block_arena = TypedArena::new();
fcx = FunctionContext::new(ccx, lloncefn, fn_ty, None, substs.func_substs, &block_arena);
fcx = FunctionContext::new(ccx, lloncefn, fn_ty, None, &block_arena);
let mut bcx = fcx.init(false, None);


Expand Down
8 changes: 4 additions & 4 deletions src/librustc_trans/controlflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
if cv == 1 {
// if true { .. } [else { .. }]
bcx = trans_block(bcx, &thn, dest);
debuginfo::clear_source_location(bcx.fcx);
DebugLoc::None.apply(bcx.fcx);
} else {
if let Some(elexpr) = els {
bcx = expr::trans_into(bcx, &elexpr, dest);
debuginfo::clear_source_location(bcx.fcx);
DebugLoc::None.apply(bcx.fcx);
}
}

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

let cond_source_loc = cond.debug_loc();

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

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

next_bcx
}
Expand Down
Loading

0 comments on commit 7c27cce

Please sign in to comment.