Skip to content

Commit

Permalink
rustc_codegen_llvm: expose DILocation to rustc_codegen_ssa.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Oct 21, 2020
1 parent 88d874d commit 7374995
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 162 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/builder.rs
Expand Up @@ -56,6 +56,7 @@ impl BackendTypes for Builder<'_, 'll, 'tcx> {
type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;

type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
type DILocation = <CodegenCx<'ll, 'tcx> as BackendTypes>::DILocation;
type DIVariable = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIVariable;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/common.rs
Expand Up @@ -88,6 +88,7 @@ impl BackendTypes for CodegenCx<'ll, 'tcx> {
type Funclet = Funclet<'ll>;

type DIScope = &'ll llvm::debuginfo::DIScope;
type DILocation = &'ll llvm::debuginfo::DILocation;
type DIVariable = &'ll llvm::debuginfo::DIVariable;
}

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs
Expand Up @@ -49,7 +49,7 @@ fn make_mir_scope(
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
scope: SourceScope,
) {
if debug_context.scopes[scope].is_valid() {
if debug_context.scopes[scope].dbg_scope.is_some() {
return;
}

Expand All @@ -61,7 +61,7 @@ fn make_mir_scope(
// The root is the function itself.
let loc = cx.lookup_debug_loc(mir.span.lo());
debug_context.scopes[scope] = DebugScope {
scope_metadata: Some(fn_dbg_scope),
dbg_scope: Some(fn_dbg_scope),
file_start_pos: loc.file.start_pos,
file_end_pos: loc.file.end_pos,
};
Expand All @@ -78,17 +78,17 @@ fn make_mir_scope(
let loc = cx.lookup_debug_loc(scope_data.span.lo());
let file_metadata = file_metadata(cx, &loc.file);

let scope_metadata = unsafe {
let dbg_scope = unsafe {
Some(llvm::LLVMRustDIBuilderCreateLexicalBlock(
DIB(cx),
parent_scope.scope_metadata.unwrap(),
parent_scope.dbg_scope.unwrap(),
file_metadata,
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
loc.col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
))
};
debug_context.scopes[scope] = DebugScope {
scope_metadata,
dbg_scope,
file_start_pos: loc.file.start_pos,
file_end_pos: loc.file.end_pos,
};
Expand Down
94 changes: 70 additions & 24 deletions compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Expand Up @@ -3,7 +3,8 @@ mod doc;

use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;

use self::metadata::{file_metadata, type_metadata, TypeMap, UNKNOWN_LINE_NUMBER};
use self::metadata::{file_metadata, type_metadata, TypeMap};
use self::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER};
use self::namespace::mangled_name_of_instance;
use self::type_names::compute_debuginfo_type_name;
use self::utils::{create_DIArray, is_node_local_to_unit, DIB};
Expand All @@ -13,14 +14,16 @@ use crate::builder::Builder;
use crate::common::CodegenCx;
use crate::llvm;
use crate::llvm::debuginfo::{
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DISPFlags, DIScope, DIType, DIVariable,
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
DIVariable,
};
use crate::value::Value;

use rustc_codegen_ssa::debuginfo::type_names;
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind};
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
use rustc_hir::def_id::{DefId, DefIdMap, LOCAL_CRATE};
use rustc_index::vec::IndexVec;
use rustc_middle::mir;
Expand All @@ -29,7 +32,7 @@ use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
use rustc_middle::ty::{self, Instance, ParamEnv, Ty, TypeFoldable};
use rustc_session::config::{self, DebugInfo};
use rustc_span::symbol::Symbol;
use rustc_span::{self, BytePos, Span};
use rustc_span::{self, BytePos, Pos, SourceFile, SourceFileAndLine, Span};
use rustc_target::abi::{LayoutOf, Primitive, Size};

use libc::c_uint;
Expand All @@ -41,7 +44,6 @@ mod create_scope_map;
pub mod gdb;
pub mod metadata;
mod namespace;
mod source_loc;
mod utils;

pub use self::create_scope_map::compute_mir_scopes;
Expand Down Expand Up @@ -141,14 +143,11 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
fn dbg_var_addr(
&mut self,
dbg_var: &'ll DIVariable,
scope_metadata: &'ll DIScope,
dbg_loc: &'ll DILocation,
variable_alloca: Self::Value,
direct_offset: Size,
indirect_offsets: &[Size],
span: Span,
) {
let cx = self.cx();

// Convert the direct and indirect offsets to address ops.
// FIXME(eddyb) use `const`s instead of getting the values via FFI,
// the values should match the ones in the DWARF standard anyway.
Expand All @@ -168,14 +167,10 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
}
}

// FIXME(eddyb) maybe this information could be extracted from `dbg_var`,
// to avoid having to pass it down in both places?
// NB: `var` doesn't seem to know about the column, so that's a limitation.
let dbg_loc = cx.create_debug_loc(scope_metadata, span);
unsafe {
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
DIB(cx),
DIB(self.cx()),
variable_alloca,
dbg_var,
addr_ops.as_ptr(),
Expand All @@ -186,16 +181,13 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
}
}

fn set_source_location(&mut self, scope: &'ll DIScope, span: Span) {
debug!("set_source_location: {}", self.sess().source_map().span_to_string(span));

let dbg_loc = self.cx().create_debug_loc(scope, span);

fn set_dbg_loc(&mut self, dbg_loc: &'ll DILocation) {
unsafe {
let dbg_loc_as_llval = llvm::LLVMRustMetadataAsValue(self.cx().llcx, dbg_loc);
llvm::LLVMSetCurrentDebugLocation(self.llbuilder, dbg_loc_as_llval);
}
}

fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
}
Expand Down Expand Up @@ -224,6 +216,49 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
}
}

/// A source code location used to generate debug information.
// FIXME(eddyb) rename this to better indicate it's a duplicate of
// `rustc_span::Loc` rather than `DILocation`, perhaps by making
// `lookup_char_pos` return the right information instead.
pub struct DebugLoc {
/// Information about the original source file.
pub file: Lrc<SourceFile>,
/// The (1-based) line number.
pub line: Option<u32>,
/// The (1-based) column number.
pub col: Option<u32>,
}

impl CodegenCx<'ll, '_> {
/// Looks up debug source information about a `BytePos`.
// FIXME(eddyb) rename this to better indicate it's a duplicate of
// `lookup_char_pos` rather than `dbg_loc`, perhaps by making
// `lookup_char_pos` return the right information instead.
pub fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc {
let (file, line, col) = match self.sess().source_map().lookup_line(pos) {
Ok(SourceFileAndLine { sf: file, line }) => {
let line_pos = file.line_begin_pos(pos);

// Use 1-based indexing.
let line = (line + 1) as u32;
let col = (pos - line_pos).to_u32() + 1;

(file, Some(line), Some(col))
}
Err(file) => (file, None, None),
};

// For MSVC, omit the column number.
// Otherwise, emit it. This mimics clang behaviour.
// See discussion in https://github.com/rust-lang/rust/issues/42921
if self.sess().target.options.is_like_msvc {
DebugLoc { file, line, col: None }
} else {
DebugLoc { file, line, col }
}
}
}

impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
fn create_function_debug_context(
&self,
Expand All @@ -237,12 +272,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}

// Initialize fn debug context (including scopes).
// FIXME(eddyb) figure out a way to not need `Option` for `scope_metadata`.
let empty_scope = DebugScope {
scope_metadata: None,
file_start_pos: BytePos(0),
file_end_pos: BytePos(0),
};
// FIXME(eddyb) figure out a way to not need `Option` for `dbg_scope`.
let empty_scope =
DebugScope { dbg_scope: None, file_start_pos: BytePos(0), file_end_pos: BytePos(0) };
let mut fn_debug_context =
FunctionDebugContext { scopes: IndexVec::from_elem(empty_scope, &mir.source_scopes) };

Expand Down Expand Up @@ -505,6 +537,20 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
}

fn dbg_loc(&self, scope: &'ll DIScope, span: Span) -> &'ll DILocation {
let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());

unsafe {
llvm::LLVMRustDIBuilderCreateDebugLocation(
utils::debug_context(self).llcontext,
line.unwrap_or(UNKNOWN_LINE_NUMBER),
col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
scope,
None,
)
}
}

fn create_vtable_metadata(&self, ty: Ty<'tcx>, vtable: Self::Value) {
metadata::create_vtable_metadata(self, ty, vtable)
}
Expand Down
61 changes: 0 additions & 61 deletions compiler/rustc_codegen_llvm/src/debuginfo/source_loc.rs

This file was deleted.

0 comments on commit 7374995

Please sign in to comment.