Merge pull request #1266 from bjorn3/parallel_comp_refactor2
Refactorings for enabling parallel compilation (part 2)
This commit is contained in:
commit
7dc8f38956
122
src/base.rs
122
src/base.rs
@ -7,48 +7,44 @@ use rustc_middle::ty::layout::FnAbiOf;
|
|||||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||||
use rustc_middle::ty::SymbolName;
|
use rustc_middle::ty::SymbolName;
|
||||||
|
|
||||||
use indexmap::IndexSet;
|
|
||||||
|
|
||||||
use crate::constant::ConstantCx;
|
use crate::constant::ConstantCx;
|
||||||
|
use crate::debuginfo::FunctionDebugContext;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::pretty_clif::CommentWriter;
|
use crate::pretty_clif::CommentWriter;
|
||||||
|
|
||||||
struct CodegenedFunction<'tcx> {
|
struct CodegenedFunction<'tcx> {
|
||||||
instance: Instance<'tcx>,
|
|
||||||
symbol_name: SymbolName<'tcx>,
|
symbol_name: SymbolName<'tcx>,
|
||||||
func_id: FuncId,
|
func_id: FuncId,
|
||||||
func: Function,
|
func: Function,
|
||||||
clif_comments: CommentWriter,
|
clif_comments: CommentWriter,
|
||||||
source_info_set: IndexSet<SourceInfo>,
|
func_debug_cx: Option<FunctionDebugContext>,
|
||||||
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn codegen_and_compile_fn<'tcx>(
|
pub(crate) fn codegen_and_compile_fn<'tcx>(
|
||||||
cx: &mut crate::CodegenCx<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
|
cx: &mut crate::CodegenCx,
|
||||||
cached_context: &mut Context,
|
cached_context: &mut Context,
|
||||||
module: &mut dyn Module,
|
module: &mut dyn Module,
|
||||||
instance: Instance<'tcx>,
|
instance: Instance<'tcx>,
|
||||||
) {
|
) {
|
||||||
let tcx = cx.tcx;
|
|
||||||
let _inst_guard =
|
let _inst_guard =
|
||||||
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
|
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
|
||||||
|
|
||||||
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
|
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
|
||||||
let codegened_func = codegen_fn(cx, cached_func, module, instance);
|
let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance);
|
||||||
|
|
||||||
compile_fn(cx, cached_context, module, codegened_func);
|
compile_fn(cx, cached_context, module, codegened_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn codegen_fn<'tcx>(
|
fn codegen_fn<'tcx>(
|
||||||
cx: &mut crate::CodegenCx<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
|
cx: &mut crate::CodegenCx,
|
||||||
cached_func: Function,
|
cached_func: Function,
|
||||||
module: &mut dyn Module,
|
module: &mut dyn Module,
|
||||||
instance: Instance<'tcx>,
|
instance: Instance<'tcx>,
|
||||||
) -> CodegenedFunction<'tcx> {
|
) -> CodegenedFunction<'tcx> {
|
||||||
debug_assert!(!instance.substs.needs_infer());
|
debug_assert!(!instance.substs.needs_infer());
|
||||||
|
|
||||||
let tcx = cx.tcx;
|
|
||||||
|
|
||||||
let mir = tcx.instance_mir(instance.def);
|
let mir = tcx.instance_mir(instance.def);
|
||||||
let _mir_guard = crate::PrintOnPanic(|| {
|
let _mir_guard = crate::PrintOnPanic(|| {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
@ -84,6 +80,12 @@ fn codegen_fn<'tcx>(
|
|||||||
let pointer_type = target_config.pointer_type();
|
let pointer_type = target_config.pointer_type();
|
||||||
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
|
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
|
||||||
|
|
||||||
|
let func_debug_cx = if let Some(debug_context) = &mut cx.debug_context {
|
||||||
|
Some(debug_context.define_function(tcx, symbol_name.name, mir.span))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let mut fx = FunctionCx {
|
let mut fx = FunctionCx {
|
||||||
cx,
|
cx,
|
||||||
module,
|
module,
|
||||||
@ -91,6 +93,7 @@ fn codegen_fn<'tcx>(
|
|||||||
target_config,
|
target_config,
|
||||||
pointer_type,
|
pointer_type,
|
||||||
constants_cx: ConstantCx::new(),
|
constants_cx: ConstantCx::new(),
|
||||||
|
func_debug_cx,
|
||||||
|
|
||||||
instance,
|
instance,
|
||||||
symbol_name,
|
symbol_name,
|
||||||
@ -103,52 +106,42 @@ fn codegen_fn<'tcx>(
|
|||||||
caller_location: None, // set by `codegen_fn_prelude`
|
caller_location: None, // set by `codegen_fn_prelude`
|
||||||
|
|
||||||
clif_comments,
|
clif_comments,
|
||||||
source_info_set: indexmap::IndexSet::new(),
|
last_source_file: None,
|
||||||
next_ssa_var: 0,
|
next_ssa_var: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block));
|
tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block));
|
||||||
|
|
||||||
// Recover all necessary data from fx, before accessing func will prevent future access to it.
|
// Recover all necessary data from fx, before accessing func will prevent future access to it.
|
||||||
let instance = fx.instance;
|
|
||||||
let clif_comments = fx.clif_comments;
|
let clif_comments = fx.clif_comments;
|
||||||
let source_info_set = fx.source_info_set;
|
let func_debug_cx = fx.func_debug_cx;
|
||||||
let local_map = fx.local_map;
|
|
||||||
|
|
||||||
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
|
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
|
||||||
|
|
||||||
crate::pretty_clif::write_clif_file(
|
if cx.should_write_ir {
|
||||||
tcx,
|
crate::pretty_clif::write_clif_file(
|
||||||
"unopt",
|
tcx.output_filenames(()),
|
||||||
module.isa(),
|
symbol_name.name,
|
||||||
instance,
|
"unopt",
|
||||||
&func,
|
module.isa(),
|
||||||
&clif_comments,
|
&func,
|
||||||
);
|
&clif_comments,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Verify function
|
// Verify function
|
||||||
verify_func(tcx, &clif_comments, &func);
|
verify_func(tcx, &clif_comments, &func);
|
||||||
|
|
||||||
CodegenedFunction {
|
CodegenedFunction { symbol_name, func_id, func, clif_comments, func_debug_cx }
|
||||||
instance,
|
|
||||||
symbol_name,
|
|
||||||
func_id,
|
|
||||||
func,
|
|
||||||
clif_comments,
|
|
||||||
source_info_set,
|
|
||||||
local_map,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_fn<'tcx>(
|
fn compile_fn<'tcx>(
|
||||||
cx: &mut crate::CodegenCx<'tcx>,
|
cx: &mut crate::CodegenCx,
|
||||||
cached_context: &mut Context,
|
cached_context: &mut Context,
|
||||||
module: &mut dyn Module,
|
module: &mut dyn Module,
|
||||||
codegened_func: CodegenedFunction<'tcx>,
|
codegened_func: CodegenedFunction<'tcx>,
|
||||||
) {
|
) {
|
||||||
let tcx = cx.tcx;
|
let clif_comments = codegened_func.clif_comments;
|
||||||
|
|
||||||
let mut clif_comments = codegened_func.clif_comments;
|
|
||||||
|
|
||||||
// Store function in context
|
// Store function in context
|
||||||
let context = cached_context;
|
let context = cached_context;
|
||||||
@ -165,17 +158,6 @@ fn compile_fn<'tcx>(
|
|||||||
// invalidate it when it would change.
|
// invalidate it when it would change.
|
||||||
context.domtree.clear();
|
context.domtree.clear();
|
||||||
|
|
||||||
// Perform rust specific optimizations
|
|
||||||
tcx.sess.time("optimize clif ir", || {
|
|
||||||
crate::optimize::optimize_function(
|
|
||||||
tcx,
|
|
||||||
module.isa(),
|
|
||||||
codegened_func.instance,
|
|
||||||
context,
|
|
||||||
&mut clif_comments,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
#[cfg(any())] // This is never true
|
#[cfg(any())] // This is never true
|
||||||
let _clif_guard = {
|
let _clif_guard = {
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
@ -204,43 +186,41 @@ fn compile_fn<'tcx>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Define function
|
// Define function
|
||||||
tcx.sess.time("define function", || {
|
cx.profiler.verbose_generic_activity("define function").run(|| {
|
||||||
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
|
context.want_disasm = cx.should_write_ir;
|
||||||
module.define_function(codegened_func.func_id, context).unwrap();
|
module.define_function(codegened_func.func_id, context).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Write optimized function to file for debugging
|
if cx.should_write_ir {
|
||||||
crate::pretty_clif::write_clif_file(
|
// Write optimized function to file for debugging
|
||||||
tcx,
|
crate::pretty_clif::write_clif_file(
|
||||||
"opt",
|
&cx.output_filenames,
|
||||||
module.isa(),
|
codegened_func.symbol_name.name,
|
||||||
codegened_func.instance,
|
"opt",
|
||||||
&context.func,
|
module.isa(),
|
||||||
&clif_comments,
|
&context.func,
|
||||||
);
|
&clif_comments,
|
||||||
|
);
|
||||||
|
|
||||||
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
|
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
|
||||||
crate::pretty_clif::write_ir_file(
|
crate::pretty_clif::write_ir_file(
|
||||||
tcx,
|
&cx.output_filenames,
|
||||||
|| format!("{}.vcode", tcx.symbol_name(codegened_func.instance).name),
|
&format!("{}.vcode", codegened_func.symbol_name.name),
|
||||||
|file| file.write_all(disasm.as_bytes()),
|
|file| file.write_all(disasm.as_bytes()),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define debuginfo for function
|
// Define debuginfo for function
|
||||||
let isa = module.isa();
|
let isa = module.isa();
|
||||||
let debug_context = &mut cx.debug_context;
|
let debug_context = &mut cx.debug_context;
|
||||||
let unwind_context = &mut cx.unwind_context;
|
let unwind_context = &mut cx.unwind_context;
|
||||||
tcx.sess.time("generate debug info", || {
|
cx.profiler.verbose_generic_activity("generate debug info").run(|| {
|
||||||
if let Some(debug_context) = debug_context {
|
if let Some(debug_context) = debug_context {
|
||||||
debug_context.define_function(
|
codegened_func.func_debug_cx.unwrap().finalize(
|
||||||
codegened_func.instance,
|
debug_context,
|
||||||
codegened_func.func_id,
|
codegened_func.func_id,
|
||||||
codegened_func.symbol_name.name,
|
|
||||||
isa,
|
|
||||||
context,
|
context,
|
||||||
&codegened_func.source_info_set,
|
|
||||||
codegened_func.local_map,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
unwind_context.add_function(codegened_func.func_id, &context, isa);
|
unwind_context.add_function(codegened_func.func_id, &context, isa);
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
use cranelift_codegen::isa::TargetFrontendConfig;
|
use cranelift_codegen::isa::TargetFrontendConfig;
|
||||||
|
use gimli::write::FileId;
|
||||||
|
|
||||||
|
use rustc_data_structures::sync::Lrc;
|
||||||
use rustc_index::vec::IndexVec;
|
use rustc_index::vec::IndexVec;
|
||||||
use rustc_middle::ty::layout::{
|
use rustc_middle::ty::layout::{
|
||||||
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
|
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::SymbolName;
|
use rustc_middle::ty::SymbolName;
|
||||||
|
use rustc_span::SourceFile;
|
||||||
use rustc_target::abi::call::FnAbi;
|
use rustc_target::abi::call::FnAbi;
|
||||||
use rustc_target::abi::{Integer, Primitive};
|
use rustc_target::abi::{Integer, Primitive};
|
||||||
use rustc_target::spec::{HasTargetSpec, Target};
|
use rustc_target::spec::{HasTargetSpec, Target};
|
||||||
|
|
||||||
use crate::constant::ConstantCx;
|
use crate::constant::ConstantCx;
|
||||||
|
use crate::debuginfo::FunctionDebugContext;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
|
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
|
||||||
@ -232,12 +237,13 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
|
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
|
||||||
pub(crate) cx: &'clif mut crate::CodegenCx<'tcx>,
|
pub(crate) cx: &'clif mut crate::CodegenCx,
|
||||||
pub(crate) module: &'m mut dyn Module,
|
pub(crate) module: &'m mut dyn Module,
|
||||||
pub(crate) tcx: TyCtxt<'tcx>,
|
pub(crate) tcx: TyCtxt<'tcx>,
|
||||||
pub(crate) target_config: TargetFrontendConfig, // Cached from module
|
pub(crate) target_config: TargetFrontendConfig, // Cached from module
|
||||||
pub(crate) pointer_type: Type, // Cached from module
|
pub(crate) pointer_type: Type, // Cached from module
|
||||||
pub(crate) constants_cx: ConstantCx,
|
pub(crate) constants_cx: ConstantCx,
|
||||||
|
pub(crate) func_debug_cx: Option<FunctionDebugContext>,
|
||||||
|
|
||||||
pub(crate) instance: Instance<'tcx>,
|
pub(crate) instance: Instance<'tcx>,
|
||||||
pub(crate) symbol_name: SymbolName<'tcx>,
|
pub(crate) symbol_name: SymbolName<'tcx>,
|
||||||
@ -252,7 +258,11 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
|
|||||||
pub(crate) caller_location: Option<CValue<'tcx>>,
|
pub(crate) caller_location: Option<CValue<'tcx>>,
|
||||||
|
|
||||||
pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
|
pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
|
||||||
pub(crate) source_info_set: indexmap::IndexSet<SourceInfo>,
|
|
||||||
|
/// Last accessed source file and it's debuginfo file id.
|
||||||
|
///
|
||||||
|
/// For optimization purposes only
|
||||||
|
pub(crate) last_source_file: Option<(Lrc<SourceFile>, FileId)>,
|
||||||
|
|
||||||
/// This should only be accessed by `CPlace::new_var`.
|
/// This should only be accessed by `CPlace::new_var`.
|
||||||
pub(crate) next_ssa_var: u32,
|
pub(crate) next_ssa_var: u32,
|
||||||
@ -336,8 +346,31 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
|
pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
|
||||||
let (index, _) = self.source_info_set.insert_full(source_info);
|
if let Some(debug_context) = &mut self.cx.debug_context {
|
||||||
self.bcx.set_srcloc(SourceLoc::new(index as u32));
|
let (file, line, column) =
|
||||||
|
DebugContext::get_span_loc(self.tcx, self.mir.span, source_info.span);
|
||||||
|
|
||||||
|
// add_source_file is very slow.
|
||||||
|
// Optimize for the common case of the current file not being changed.
|
||||||
|
let mut cached_file_id = None;
|
||||||
|
if let Some((ref last_source_file, last_file_id)) = self.last_source_file {
|
||||||
|
// If the allocations are not equal, the files may still be equal, but that
|
||||||
|
// doesn't matter, as this is just an optimization.
|
||||||
|
if rustc_data_structures::sync::Lrc::ptr_eq(last_source_file, &file) {
|
||||||
|
cached_file_id = Some(last_file_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let file_id = if let Some(file_id) = cached_file_id {
|
||||||
|
file_id
|
||||||
|
} else {
|
||||||
|
debug_context.add_source_file(&file)
|
||||||
|
};
|
||||||
|
|
||||||
|
let source_loc =
|
||||||
|
self.func_debug_cx.as_mut().unwrap().add_dbg_loc(file_id, line, column);
|
||||||
|
self.bcx.set_srcloc(source_loc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: must be kept in sync with get_caller_location from cg_ssa
|
// Note: must be kept in sync with get_caller_location from cg_ssa
|
||||||
|
@ -9,7 +9,7 @@ use gimli::{RunTimeEndian, SectionId};
|
|||||||
use super::object::WriteDebugInfo;
|
use super::object::WriteDebugInfo;
|
||||||
use super::DebugContext;
|
use super::DebugContext;
|
||||||
|
|
||||||
impl DebugContext<'_> {
|
impl DebugContext {
|
||||||
pub(crate) fn emit(&mut self, product: &mut ObjectProduct) {
|
pub(crate) fn emit(&mut self, product: &mut ObjectProduct) {
|
||||||
let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
|
let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
|
||||||
let root = self.dwarf.unit.root();
|
let root = self.dwarf.unit.root();
|
||||||
|
@ -3,8 +3,10 @@
|
|||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::path::{Component, Path};
|
use std::path::{Component, Path};
|
||||||
|
|
||||||
|
use crate::debuginfo::FunctionDebugContext;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
use rustc_data_structures::sync::Lrc;
|
||||||
use rustc_span::{
|
use rustc_span::{
|
||||||
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHash, SourceFileHashAlgorithm,
|
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHash, SourceFileHashAlgorithm,
|
||||||
};
|
};
|
||||||
@ -14,7 +16,6 @@ use cranelift_codegen::MachSrcLoc;
|
|||||||
|
|
||||||
use gimli::write::{
|
use gimli::write::{
|
||||||
Address, AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable,
|
Address, AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable,
|
||||||
UnitEntryId,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// OPTIMIZATION: It is cheaper to do this in one pass than using `.parent()` and `.file_name()`.
|
// OPTIMIZATION: It is cheaper to do this in one pass than using `.parent()` and `.file_name()`.
|
||||||
@ -47,9 +48,9 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) const MD5_LEN: usize = 16;
|
const MD5_LEN: usize = 16;
|
||||||
|
|
||||||
pub(crate) fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
|
fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
|
||||||
if hash.kind == SourceFileHashAlgorithm::Md5 {
|
if hash.kind == SourceFileHashAlgorithm::Md5 {
|
||||||
let mut buf = [0u8; MD5_LEN];
|
let mut buf = [0u8; MD5_LEN];
|
||||||
buf.copy_from_slice(hash.hash_bytes());
|
buf.copy_from_slice(hash.hash_bytes());
|
||||||
@ -59,160 +60,132 @@ pub(crate) fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn line_program_add_file(
|
impl DebugContext {
|
||||||
line_program: &mut LineProgram,
|
pub(crate) fn get_span_loc(
|
||||||
line_strings: &mut LineStringTable,
|
tcx: TyCtxt<'_>,
|
||||||
file: &SourceFile,
|
function_span: Span,
|
||||||
) -> FileId {
|
span: Span,
|
||||||
match &file.name {
|
) -> (Lrc<SourceFile>, u64, u64) {
|
||||||
FileName::Real(path) => {
|
// Based on https://github.com/rust-lang/rust/blob/e369d87b015a84653343032833d65d0545fd3f26/src/librustc_codegen_ssa/mir/mod.rs#L116-L131
|
||||||
let (dir_path, file_name) = split_path_dir_and_file(path.remapped_path_if_available());
|
// In order to have a good line stepping behavior in debugger, we overwrite debug
|
||||||
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
|
// locations of macro expansions with that of the outermost expansion site
|
||||||
let file_name = osstr_as_utf8_bytes(file_name);
|
// (unless the crate is being compiled with `-Z debug-macros`).
|
||||||
|
let span = if !span.from_expansion() || tcx.sess.opts.unstable_opts.debug_macros {
|
||||||
|
span
|
||||||
|
} else {
|
||||||
|
// Walk up the macro expansion chain until we reach a non-expanded span.
|
||||||
|
// We also stop at the function body level because no line stepping can occur
|
||||||
|
// at the level above that.
|
||||||
|
rustc_span::hygiene::walk_chain(span, function_span.ctxt())
|
||||||
|
};
|
||||||
|
|
||||||
let dir_id = if !dir_name.is_empty() {
|
match tcx.sess.source_map().lookup_line(span.lo()) {
|
||||||
let dir_name = LineString::new(dir_name, line_program.encoding(), line_strings);
|
Ok(SourceFileAndLine { sf: file, line }) => {
|
||||||
line_program.add_directory(dir_name)
|
let line_pos = file.line_begin_pos(span.lo());
|
||||||
} else {
|
|
||||||
line_program.default_directory()
|
|
||||||
};
|
|
||||||
let file_name = LineString::new(file_name, line_program.encoding(), line_strings);
|
|
||||||
|
|
||||||
let info = make_file_info(file.src_hash);
|
(
|
||||||
|
file,
|
||||||
line_program.file_has_md5 &= info.is_some();
|
u64::try_from(line).unwrap() + 1,
|
||||||
line_program.add_file(file_name, dir_id, info)
|
u64::from((span.lo() - line_pos).to_u32()) + 1,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Err(file) => (file, 0, 0),
|
||||||
}
|
}
|
||||||
// FIXME give more appropriate file names
|
}
|
||||||
filename => {
|
|
||||||
let dir_id = line_program.default_directory();
|
pub(crate) fn add_source_file(&mut self, source_file: &SourceFile) -> FileId {
|
||||||
let dummy_file_name = LineString::new(
|
let line_program: &mut LineProgram = &mut self.dwarf.unit.line_program;
|
||||||
filename.prefer_remapped().to_string().into_bytes(),
|
let line_strings: &mut LineStringTable = &mut self.dwarf.line_strings;
|
||||||
line_program.encoding(),
|
|
||||||
line_strings,
|
match &source_file.name {
|
||||||
);
|
FileName::Real(path) => {
|
||||||
line_program.add_file(dummy_file_name, dir_id, None)
|
let (dir_path, file_name) =
|
||||||
|
split_path_dir_and_file(path.remapped_path_if_available());
|
||||||
|
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
|
||||||
|
let file_name = osstr_as_utf8_bytes(file_name);
|
||||||
|
|
||||||
|
let dir_id = if !dir_name.is_empty() {
|
||||||
|
let dir_name = LineString::new(dir_name, line_program.encoding(), line_strings);
|
||||||
|
line_program.add_directory(dir_name)
|
||||||
|
} else {
|
||||||
|
line_program.default_directory()
|
||||||
|
};
|
||||||
|
let file_name = LineString::new(file_name, line_program.encoding(), line_strings);
|
||||||
|
|
||||||
|
let info = make_file_info(source_file.src_hash);
|
||||||
|
|
||||||
|
line_program.file_has_md5 &= info.is_some();
|
||||||
|
line_program.add_file(file_name, dir_id, info)
|
||||||
|
}
|
||||||
|
// FIXME give more appropriate file names
|
||||||
|
filename => {
|
||||||
|
let dir_id = line_program.default_directory();
|
||||||
|
let dummy_file_name = LineString::new(
|
||||||
|
filename.prefer_remapped().to_string().into_bytes(),
|
||||||
|
line_program.encoding(),
|
||||||
|
line_strings,
|
||||||
|
);
|
||||||
|
line_program.add_file(dummy_file_name, dir_id, None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> DebugContext<'tcx> {
|
impl FunctionDebugContext {
|
||||||
pub(super) fn emit_location(&mut self, entry_id: UnitEntryId, span: Span) {
|
pub(crate) fn add_dbg_loc(&mut self, file_id: FileId, line: u64, column: u64) -> SourceLoc {
|
||||||
let loc = self.tcx.sess.source_map().lookup_char_pos(span.lo());
|
let (index, _) = self.source_loc_set.insert_full((file_id, line, column));
|
||||||
|
SourceLoc::new(u32::try_from(index).unwrap())
|
||||||
let file_id = line_program_add_file(
|
|
||||||
&mut self.dwarf.unit.line_program,
|
|
||||||
&mut self.dwarf.line_strings,
|
|
||||||
&loc.file,
|
|
||||||
);
|
|
||||||
|
|
||||||
let entry = self.dwarf.unit.get_mut(entry_id);
|
|
||||||
|
|
||||||
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
|
|
||||||
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(loc.line as u64));
|
|
||||||
entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(loc.col.to_usize() as u64));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn create_debug_lines(
|
pub(super) fn create_debug_lines(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
debug_context: &mut DebugContext,
|
||||||
symbol: usize,
|
symbol: usize,
|
||||||
entry_id: UnitEntryId,
|
|
||||||
context: &Context,
|
context: &Context,
|
||||||
function_span: Span,
|
|
||||||
source_info_set: &indexmap::IndexSet<SourceInfo>,
|
|
||||||
) -> CodeOffset {
|
) -> CodeOffset {
|
||||||
let tcx = self.tcx;
|
let create_row_for_span =
|
||||||
let line_program = &mut self.dwarf.unit.line_program;
|
|debug_context: &mut DebugContext, source_loc: (FileId, u64, u64)| {
|
||||||
|
let (file_id, line, col) = source_loc;
|
||||||
|
|
||||||
let line_strings = &mut self.dwarf.line_strings;
|
debug_context.dwarf.unit.line_program.row().file = file_id;
|
||||||
let mut last_span = None;
|
debug_context.dwarf.unit.line_program.row().line = line;
|
||||||
let mut last_file = None;
|
debug_context.dwarf.unit.line_program.row().column = col;
|
||||||
let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| {
|
debug_context.dwarf.unit.line_program.generate_row();
|
||||||
if let Some(last_span) = last_span {
|
|
||||||
if span == last_span {
|
|
||||||
line_program.generate_row();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
last_span = Some(span);
|
|
||||||
|
|
||||||
// Based on https://github.com/rust-lang/rust/blob/e369d87b015a84653343032833d65d0545fd3f26/src/librustc_codegen_ssa/mir/mod.rs#L116-L131
|
|
||||||
// In order to have a good line stepping behavior in debugger, we overwrite debug
|
|
||||||
// locations of macro expansions with that of the outermost expansion site
|
|
||||||
// (unless the crate is being compiled with `-Z debug-macros`).
|
|
||||||
let span = if !span.from_expansion() || tcx.sess.opts.unstable_opts.debug_macros {
|
|
||||||
span
|
|
||||||
} else {
|
|
||||||
// Walk up the macro expansion chain until we reach a non-expanded span.
|
|
||||||
// We also stop at the function body level because no line stepping can occur
|
|
||||||
// at the level above that.
|
|
||||||
rustc_span::hygiene::walk_chain(span, function_span.ctxt())
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let (file, line, col) = match tcx.sess.source_map().lookup_line(span.lo()) {
|
debug_context
|
||||||
Ok(SourceFileAndLine { sf: file, line }) => {
|
.dwarf
|
||||||
let line_pos = file.line_begin_pos(span.lo());
|
.unit
|
||||||
|
.line_program
|
||||||
(
|
.begin_sequence(Some(Address::Symbol { symbol, addend: 0 }));
|
||||||
file,
|
|
||||||
u64::try_from(line).unwrap() + 1,
|
|
||||||
u64::from((span.lo() - line_pos).to_u32()) + 1,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Err(file) => (file, 0, 0),
|
|
||||||
};
|
|
||||||
|
|
||||||
// line_program_add_file is very slow.
|
|
||||||
// Optimize for the common case of the current file not being changed.
|
|
||||||
let current_file_changed = if let Some(last_file) = &last_file {
|
|
||||||
// If the allocations are not equal, then the files may still be equal, but that
|
|
||||||
// is not a problem, as this is just an optimization.
|
|
||||||
!rustc_data_structures::sync::Lrc::ptr_eq(last_file, &file)
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
};
|
|
||||||
if current_file_changed {
|
|
||||||
let file_id = line_program_add_file(line_program, line_strings, &file);
|
|
||||||
line_program.row().file = file_id;
|
|
||||||
last_file = Some(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
line_program.row().line = line;
|
|
||||||
line_program.row().column = col;
|
|
||||||
line_program.generate_row();
|
|
||||||
};
|
|
||||||
|
|
||||||
line_program.begin_sequence(Some(Address::Symbol { symbol, addend: 0 }));
|
|
||||||
|
|
||||||
let mut func_end = 0;
|
let mut func_end = 0;
|
||||||
|
|
||||||
let mcr = context.mach_compile_result.as_ref().unwrap();
|
let mcr = context.mach_compile_result.as_ref().unwrap();
|
||||||
for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() {
|
for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() {
|
||||||
line_program.row().address_offset = u64::from(start);
|
debug_context.dwarf.unit.line_program.row().address_offset = u64::from(start);
|
||||||
if !loc.is_default() {
|
if !loc.is_default() {
|
||||||
let source_info = *source_info_set.get_index(loc.bits() as usize).unwrap();
|
let source_loc = *self.source_loc_set.get_index(loc.bits() as usize).unwrap();
|
||||||
create_row_for_span(line_program, source_info.span);
|
create_row_for_span(debug_context, source_loc);
|
||||||
} else {
|
} else {
|
||||||
create_row_for_span(line_program, function_span);
|
create_row_for_span(debug_context, self.function_source_loc);
|
||||||
}
|
}
|
||||||
func_end = end;
|
func_end = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
line_program.end_sequence(u64::from(func_end));
|
debug_context.dwarf.unit.line_program.end_sequence(u64::from(func_end));
|
||||||
|
|
||||||
let func_end = mcr.buffer.total_size();
|
let func_end = mcr.buffer.total_size();
|
||||||
|
|
||||||
assert_ne!(func_end, 0);
|
assert_ne!(func_end, 0);
|
||||||
|
|
||||||
let entry = self.dwarf.unit.get_mut(entry_id);
|
let entry = debug_context.dwarf.unit.get_mut(self.entry_id);
|
||||||
entry.set(
|
entry.set(
|
||||||
gimli::DW_AT_low_pc,
|
gimli::DW_AT_low_pc,
|
||||||
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
|
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
|
||||||
);
|
);
|
||||||
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end)));
|
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end)));
|
||||||
|
|
||||||
self.emit_location(entry_id, function_span);
|
|
||||||
|
|
||||||
func_end
|
func_end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,35 +7,34 @@ mod unwind;
|
|||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use rustc_index::vec::IndexVec;
|
use cranelift_codegen::ir::Endianness;
|
||||||
|
|
||||||
use cranelift_codegen::entity::EntityRef;
|
|
||||||
use cranelift_codegen::ir::{Endianness, LabelValueLoc, ValueLabel};
|
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
use cranelift_codegen::isa::TargetIsa;
|
||||||
use cranelift_codegen::ValueLocRange;
|
|
||||||
|
|
||||||
use gimli::write::{
|
use gimli::write::{
|
||||||
Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
|
Address, AttributeValue, DwarfUnit, FileId, LineProgram, LineString, Range, RangeList,
|
||||||
LocationList, Range, RangeList, UnitEntryId,
|
UnitEntryId,
|
||||||
};
|
};
|
||||||
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, X86_64};
|
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian};
|
||||||
|
use indexmap::IndexSet;
|
||||||
|
|
||||||
pub(crate) use emit::{DebugReloc, DebugRelocName};
|
pub(crate) use emit::{DebugReloc, DebugRelocName};
|
||||||
pub(crate) use unwind::UnwindContext;
|
pub(crate) use unwind::UnwindContext;
|
||||||
|
|
||||||
pub(crate) struct DebugContext<'tcx> {
|
pub(crate) struct DebugContext {
|
||||||
tcx: TyCtxt<'tcx>,
|
|
||||||
|
|
||||||
endian: RunTimeEndian,
|
endian: RunTimeEndian,
|
||||||
|
|
||||||
dwarf: DwarfUnit,
|
dwarf: DwarfUnit,
|
||||||
unit_range_list: RangeList,
|
unit_range_list: RangeList,
|
||||||
|
|
||||||
types: FxHashMap<Ty<'tcx>, UnitEntryId>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> DebugContext<'tcx> {
|
pub(crate) struct FunctionDebugContext {
|
||||||
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
|
entry_id: UnitEntryId,
|
||||||
|
function_source_loc: (FileId, u64, u64),
|
||||||
|
source_loc_set: indexmap::IndexSet<(FileId, u64, u64)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DebugContext {
|
||||||
|
pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa) -> Self {
|
||||||
let encoding = Encoding {
|
let encoding = Encoding {
|
||||||
format: Format::Dwarf32,
|
format: Format::Dwarf32,
|
||||||
// FIXME this should be configurable
|
// FIXME this should be configurable
|
||||||
@ -101,127 +100,18 @@ impl<'tcx> DebugContext<'tcx> {
|
|||||||
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
|
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugContext {
|
DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
|
||||||
tcx,
|
|
||||||
|
|
||||||
endian,
|
|
||||||
|
|
||||||
dwarf,
|
|
||||||
unit_range_list: RangeList(Vec::new()),
|
|
||||||
|
|
||||||
types: FxHashMap::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
|
|
||||||
if let Some(type_id) = self.types.get(&ty) {
|
|
||||||
return *type_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
let new_entry = |dwarf: &mut DwarfUnit, tag| dwarf.unit.add(dwarf.unit.root(), tag);
|
|
||||||
|
|
||||||
let primitive = |dwarf: &mut DwarfUnit, ate| {
|
|
||||||
let type_id = new_entry(dwarf, gimli::DW_TAG_base_type);
|
|
||||||
let type_entry = dwarf.unit.get_mut(type_id);
|
|
||||||
type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(ate));
|
|
||||||
type_id
|
|
||||||
};
|
|
||||||
|
|
||||||
let name = format!("{}", ty);
|
|
||||||
let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
|
|
||||||
|
|
||||||
let type_id = match ty.kind() {
|
|
||||||
ty::Bool => primitive(&mut self.dwarf, gimli::DW_ATE_boolean),
|
|
||||||
ty::Char => primitive(&mut self.dwarf, gimli::DW_ATE_UTF),
|
|
||||||
ty::Uint(_) => primitive(&mut self.dwarf, gimli::DW_ATE_unsigned),
|
|
||||||
ty::Int(_) => primitive(&mut self.dwarf, gimli::DW_ATE_signed),
|
|
||||||
ty::Float(_) => primitive(&mut self.dwarf, gimli::DW_ATE_float),
|
|
||||||
ty::Ref(_, pointee_ty, _mutbl)
|
|
||||||
| ty::RawPtr(ty::TypeAndMut { ty: pointee_ty, mutbl: _mutbl }) => {
|
|
||||||
let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_pointer_type);
|
|
||||||
|
|
||||||
// Ensure that type is inserted before recursing to avoid duplicates
|
|
||||||
self.types.insert(ty, type_id);
|
|
||||||
|
|
||||||
let pointee = self.dwarf_ty(*pointee_ty);
|
|
||||||
|
|
||||||
let type_entry = self.dwarf.unit.get_mut(type_id);
|
|
||||||
|
|
||||||
//type_entry.set(gimli::DW_AT_mutable, AttributeValue::Flag(mutbl == rustc_hir::Mutability::Mut));
|
|
||||||
type_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(pointee));
|
|
||||||
|
|
||||||
type_id
|
|
||||||
}
|
|
||||||
ty::Adt(adt_def, _substs) if adt_def.is_struct() && !layout.is_unsized() => {
|
|
||||||
let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type);
|
|
||||||
|
|
||||||
// Ensure that type is inserted before recursing to avoid duplicates
|
|
||||||
self.types.insert(ty, type_id);
|
|
||||||
|
|
||||||
let variant = adt_def.non_enum_variant();
|
|
||||||
|
|
||||||
for (field_idx, field_def) in variant.fields.iter().enumerate() {
|
|
||||||
let field_offset = layout.fields.offset(field_idx);
|
|
||||||
let field_layout = layout.field(
|
|
||||||
&layout::LayoutCx { tcx: self.tcx, param_env: ParamEnv::reveal_all() },
|
|
||||||
field_idx,
|
|
||||||
);
|
|
||||||
|
|
||||||
let field_type = self.dwarf_ty(field_layout.ty);
|
|
||||||
|
|
||||||
let field_id = self.dwarf.unit.add(type_id, gimli::DW_TAG_member);
|
|
||||||
let field_entry = self.dwarf.unit.get_mut(field_id);
|
|
||||||
|
|
||||||
field_entry.set(
|
|
||||||
gimli::DW_AT_name,
|
|
||||||
AttributeValue::String(field_def.name.as_str().to_string().into_bytes()),
|
|
||||||
);
|
|
||||||
field_entry.set(
|
|
||||||
gimli::DW_AT_data_member_location,
|
|
||||||
AttributeValue::Udata(field_offset.bytes()),
|
|
||||||
);
|
|
||||||
field_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(field_type));
|
|
||||||
}
|
|
||||||
|
|
||||||
type_id
|
|
||||||
}
|
|
||||||
_ => new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type),
|
|
||||||
};
|
|
||||||
|
|
||||||
let type_entry = self.dwarf.unit.get_mut(type_id);
|
|
||||||
|
|
||||||
type_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
|
|
||||||
type_entry.set(gimli::DW_AT_byte_size, AttributeValue::Udata(layout.size.bytes()));
|
|
||||||
|
|
||||||
self.types.insert(ty, type_id);
|
|
||||||
|
|
||||||
type_id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn define_local(&mut self, scope: UnitEntryId, name: String, ty: Ty<'tcx>) -> UnitEntryId {
|
|
||||||
let dw_ty = self.dwarf_ty(ty);
|
|
||||||
|
|
||||||
let var_id = self.dwarf.unit.add(scope, gimli::DW_TAG_variable);
|
|
||||||
let var_entry = self.dwarf.unit.get_mut(var_id);
|
|
||||||
|
|
||||||
var_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes()));
|
|
||||||
var_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(dw_ty));
|
|
||||||
|
|
||||||
var_id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn define_function(
|
pub(crate) fn define_function(
|
||||||
&mut self,
|
&mut self,
|
||||||
instance: Instance<'tcx>,
|
tcx: TyCtxt<'_>,
|
||||||
func_id: FuncId,
|
|
||||||
name: &str,
|
name: &str,
|
||||||
isa: &dyn TargetIsa,
|
function_span: Span,
|
||||||
context: &Context,
|
) -> FunctionDebugContext {
|
||||||
source_info_set: &indexmap::IndexSet<SourceInfo>,
|
let (file, line, column) = DebugContext::get_span_loc(tcx, function_span, function_span);
|
||||||
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
|
|
||||||
) {
|
let file_id = self.add_source_file(&file);
|
||||||
let symbol = func_id.as_u32() as usize;
|
|
||||||
let mir = self.tcx.instance_mir(instance.def);
|
|
||||||
|
|
||||||
// FIXME: add to appropriate scope instead of root
|
// FIXME: add to appropriate scope instead of root
|
||||||
let scope = self.dwarf.unit.root();
|
let scope = self.dwarf.unit.root();
|
||||||
@ -233,14 +123,35 @@ impl<'tcx> DebugContext<'tcx> {
|
|||||||
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
|
entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id));
|
||||||
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id));
|
entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id));
|
||||||
|
|
||||||
let end = self.create_debug_lines(symbol, entry_id, context, mir.span, source_info_set);
|
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
|
||||||
|
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line));
|
||||||
|
entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(column));
|
||||||
|
|
||||||
self.unit_range_list.0.push(Range::StartLength {
|
FunctionDebugContext {
|
||||||
|
entry_id,
|
||||||
|
function_source_loc: (file_id, line, column),
|
||||||
|
source_loc_set: IndexSet::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FunctionDebugContext {
|
||||||
|
pub(crate) fn finalize(
|
||||||
|
mut self,
|
||||||
|
debug_context: &mut DebugContext,
|
||||||
|
func_id: FuncId,
|
||||||
|
context: &Context,
|
||||||
|
) {
|
||||||
|
let symbol = func_id.as_u32() as usize;
|
||||||
|
|
||||||
|
let end = self.create_debug_lines(debug_context, symbol, context);
|
||||||
|
|
||||||
|
debug_context.unit_range_list.0.push(Range::StartLength {
|
||||||
begin: Address::Symbol { symbol, addend: 0 },
|
begin: Address::Symbol { symbol, addend: 0 },
|
||||||
length: u64::from(end),
|
length: u64::from(end),
|
||||||
});
|
});
|
||||||
|
|
||||||
let func_entry = self.dwarf.unit.get_mut(entry_id);
|
let func_entry = debug_context.dwarf.unit.get_mut(self.entry_id);
|
||||||
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
|
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
|
||||||
func_entry.set(
|
func_entry.set(
|
||||||
gimli::DW_AT_low_pc,
|
gimli::DW_AT_low_pc,
|
||||||
@ -248,110 +159,5 @@ impl<'tcx> DebugContext<'tcx> {
|
|||||||
);
|
);
|
||||||
// Using Udata for DW_AT_high_pc requires at least DWARF4
|
// Using Udata for DW_AT_high_pc requires at least DWARF4
|
||||||
func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
|
func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
|
||||||
|
|
||||||
// FIXME make it more reliable and implement scopes before re-enabling this.
|
|
||||||
if false {
|
|
||||||
let value_labels_ranges = std::collections::HashMap::new(); // FIXME
|
|
||||||
|
|
||||||
for (local, _local_decl) in mir.local_decls.iter_enumerated() {
|
|
||||||
let ty = self.tcx.subst_and_normalize_erasing_regions(
|
|
||||||
instance.substs,
|
|
||||||
ty::ParamEnv::reveal_all(),
|
|
||||||
mir.local_decls[local].ty,
|
|
||||||
);
|
|
||||||
let var_id = self.define_local(entry_id, format!("{:?}", local), ty);
|
|
||||||
|
|
||||||
let location = place_location(
|
|
||||||
self,
|
|
||||||
isa,
|
|
||||||
symbol,
|
|
||||||
&local_map,
|
|
||||||
&value_labels_ranges,
|
|
||||||
Place { local, projection: ty::List::empty() },
|
|
||||||
);
|
|
||||||
|
|
||||||
let var_entry = self.dwarf.unit.get_mut(var_id);
|
|
||||||
var_entry.set(gimli::DW_AT_location, location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME create locals for all entries in mir.var_debug_info
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn place_location<'tcx>(
|
|
||||||
debug_context: &mut DebugContext<'tcx>,
|
|
||||||
isa: &dyn TargetIsa,
|
|
||||||
symbol: usize,
|
|
||||||
local_map: &IndexVec<mir::Local, CPlace<'tcx>>,
|
|
||||||
#[allow(rustc::default_hash_types)] value_labels_ranges: &std::collections::HashMap<
|
|
||||||
ValueLabel,
|
|
||||||
Vec<ValueLocRange>,
|
|
||||||
>,
|
|
||||||
place: Place<'tcx>,
|
|
||||||
) -> AttributeValue {
|
|
||||||
assert!(place.projection.is_empty()); // FIXME implement them
|
|
||||||
|
|
||||||
match local_map[place.local].inner() {
|
|
||||||
CPlaceInner::Var(_local, var) => {
|
|
||||||
let value_label = cranelift_codegen::ir::ValueLabel::new(var.index());
|
|
||||||
if let Some(value_loc_ranges) = value_labels_ranges.get(&value_label) {
|
|
||||||
let loc_list = LocationList(
|
|
||||||
value_loc_ranges
|
|
||||||
.iter()
|
|
||||||
.map(|value_loc_range| Location::StartEnd {
|
|
||||||
begin: Address::Symbol {
|
|
||||||
symbol,
|
|
||||||
addend: i64::from(value_loc_range.start),
|
|
||||||
},
|
|
||||||
end: Address::Symbol { symbol, addend: i64::from(value_loc_range.end) },
|
|
||||||
data: translate_loc(isa, value_loc_range.loc).unwrap(),
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
);
|
|
||||||
let loc_list_id = debug_context.dwarf.unit.locations.add(loc_list);
|
|
||||||
|
|
||||||
AttributeValue::LocationListRef(loc_list_id)
|
|
||||||
} else {
|
|
||||||
// FIXME set value labels for unused locals
|
|
||||||
|
|
||||||
AttributeValue::Exprloc(Expression::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CPlaceInner::VarPair(_, _, _) => {
|
|
||||||
// FIXME implement this
|
|
||||||
|
|
||||||
AttributeValue::Exprloc(Expression::new())
|
|
||||||
}
|
|
||||||
CPlaceInner::VarLane(_, _, _) => {
|
|
||||||
// FIXME implement this
|
|
||||||
|
|
||||||
AttributeValue::Exprloc(Expression::new())
|
|
||||||
}
|
|
||||||
CPlaceInner::Addr(_, _) => {
|
|
||||||
// FIXME implement this (used by arguments and returns)
|
|
||||||
|
|
||||||
AttributeValue::Exprloc(Expression::new())
|
|
||||||
|
|
||||||
// For PointerBase::Stack:
|
|
||||||
//AttributeValue::Exprloc(translate_loc(ValueLoc::Stack(*stack_slot)).unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adapted from https://github.com/CraneStation/wasmtime/blob/5a1845b4caf7a5dba8eda1fef05213a532ed4259/crates/debug/src/transform/expression.rs#L59-L137
|
|
||||||
fn translate_loc(isa: &dyn TargetIsa, loc: LabelValueLoc) -> Option<Expression> {
|
|
||||||
match loc {
|
|
||||||
LabelValueLoc::Reg(reg) => {
|
|
||||||
let machine_reg = isa.map_regalloc_reg_to_dwarf(reg).unwrap();
|
|
||||||
let mut expr = Expression::new();
|
|
||||||
expr.op_reg(gimli::Register(machine_reg));
|
|
||||||
Some(expr)
|
|
||||||
}
|
|
||||||
LabelValueLoc::SPOffset(offset) => {
|
|
||||||
let mut expr = Expression::new();
|
|
||||||
expr.op_breg(X86_64::RSP, offset);
|
|
||||||
Some(expr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,7 @@ fn emit_cgu(
|
|||||||
prof: &SelfProfilerRef,
|
prof: &SelfProfilerRef,
|
||||||
name: String,
|
name: String,
|
||||||
module: ObjectModule,
|
module: ObjectModule,
|
||||||
debug: Option<DebugContext<'_>>,
|
debug: Option<DebugContext>,
|
||||||
unwind_context: UnwindContext,
|
unwind_context: UnwindContext,
|
||||||
global_asm_object_file: Option<PathBuf>,
|
global_asm_object_file: Option<PathBuf>,
|
||||||
) -> Result<ModuleCodegenResult, String> {
|
) -> Result<ModuleCodegenResult, String> {
|
||||||
@ -256,8 +256,9 @@ fn module_codegen(
|
|||||||
for (mono_item, _) in mono_items {
|
for (mono_item, _) in mono_items {
|
||||||
match mono_item {
|
match mono_item {
|
||||||
MonoItem::Fn(inst) => {
|
MonoItem::Fn(inst) => {
|
||||||
cx.tcx.sess.time("codegen fn", || {
|
tcx.sess.time("codegen fn", || {
|
||||||
crate::base::codegen_and_compile_fn(
|
crate::base::codegen_and_compile_fn(
|
||||||
|
tcx,
|
||||||
&mut cx,
|
&mut cx,
|
||||||
&mut cached_context,
|
&mut cached_context,
|
||||||
&mut module,
|
&mut module,
|
||||||
@ -279,25 +280,20 @@ fn module_codegen(
|
|||||||
cgu.is_primary(),
|
cgu.is_primary(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let global_asm_object_file = match crate::global_asm::compile_global_asm(
|
let global_asm_object_file = crate::global_asm::compile_global_asm(
|
||||||
&global_asm_config,
|
&global_asm_config,
|
||||||
cgu.name().as_str(),
|
cgu.name().as_str(),
|
||||||
&cx.global_asm,
|
&cx.global_asm,
|
||||||
) {
|
)?;
|
||||||
Ok(global_asm_object_file) => global_asm_object_file,
|
|
||||||
Err(err) => tcx.sess.fatal(&err),
|
|
||||||
};
|
|
||||||
|
|
||||||
let debug_context = cx.debug_context;
|
|
||||||
let unwind_context = cx.unwind_context;
|
|
||||||
tcx.sess.time("write object file", || {
|
tcx.sess.time("write object file", || {
|
||||||
emit_cgu(
|
emit_cgu(
|
||||||
&global_asm_config.output_filenames,
|
&global_asm_config.output_filenames,
|
||||||
&tcx.sess.prof,
|
&cx.profiler,
|
||||||
cgu.name().as_str().to_string(),
|
cgu.name().as_str().to_string(),
|
||||||
module,
|
module,
|
||||||
debug_context,
|
cx.debug_context,
|
||||||
unwind_context,
|
cx.unwind_context,
|
||||||
global_asm_object_file,
|
global_asm_object_file,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -61,11 +61,11 @@ impl UnsafeMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_jit_module<'tcx>(
|
fn create_jit_module(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'_>,
|
||||||
backend_config: &BackendConfig,
|
backend_config: &BackendConfig,
|
||||||
hotswap: bool,
|
hotswap: bool,
|
||||||
) -> (JITModule, CodegenCx<'tcx>) {
|
) -> (JITModule, CodegenCx) {
|
||||||
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
|
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
|
||||||
let imported_symbols = load_imported_symbols_for_jit(tcx.sess, crate_info);
|
let imported_symbols = load_imported_symbols_for_jit(tcx.sess, crate_info);
|
||||||
|
|
||||||
@ -129,8 +129,9 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|||||||
MonoItem::Fn(inst) => match backend_config.codegen_mode {
|
MonoItem::Fn(inst) => match backend_config.codegen_mode {
|
||||||
CodegenMode::Aot => unreachable!(),
|
CodegenMode::Aot => unreachable!(),
|
||||||
CodegenMode::Jit => {
|
CodegenMode::Jit => {
|
||||||
cx.tcx.sess.time("codegen fn", || {
|
tcx.sess.time("codegen fn", || {
|
||||||
crate::base::codegen_and_compile_fn(
|
crate::base::codegen_and_compile_fn(
|
||||||
|
tcx,
|
||||||
&mut cx,
|
&mut cx,
|
||||||
&mut cached_context,
|
&mut cached_context,
|
||||||
&mut jit_module,
|
&mut jit_module,
|
||||||
@ -139,7 +140,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
CodegenMode::JitLazy => {
|
CodegenMode::JitLazy => {
|
||||||
codegen_shim(&mut cx, &mut cached_context, &mut jit_module, inst)
|
codegen_shim(tcx, &mut cx, &mut cached_context, &mut jit_module, inst)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
MonoItem::Static(def_id) => {
|
MonoItem::Static(def_id) => {
|
||||||
@ -269,6 +270,7 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
|
|||||||
);
|
);
|
||||||
tcx.sess.time("codegen fn", || {
|
tcx.sess.time("codegen fn", || {
|
||||||
crate::base::codegen_and_compile_fn(
|
crate::base::codegen_and_compile_fn(
|
||||||
|
tcx,
|
||||||
&mut cx,
|
&mut cx,
|
||||||
&mut Context::new(),
|
&mut Context::new(),
|
||||||
jit_module,
|
jit_module,
|
||||||
@ -350,13 +352,12 @@ fn load_imported_symbols_for_jit(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn codegen_shim<'tcx>(
|
fn codegen_shim<'tcx>(
|
||||||
cx: &mut CodegenCx<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
|
cx: &mut CodegenCx,
|
||||||
cached_context: &mut Context,
|
cached_context: &mut Context,
|
||||||
module: &mut JITModule,
|
module: &mut JITModule,
|
||||||
inst: Instance<'tcx>,
|
inst: Instance<'tcx>,
|
||||||
) {
|
) {
|
||||||
let tcx = cx.tcx;
|
|
||||||
|
|
||||||
let pointer_type = module.target_config().pointer_type();
|
let pointer_type = module.target_config().pointer_type();
|
||||||
|
|
||||||
let name = tcx.symbol_name(inst).name;
|
let name = tcx.symbol_name(inst).name;
|
||||||
@ -403,4 +404,5 @@ fn codegen_shim<'tcx>(
|
|||||||
trampoline_builder.ins().return_(&ret_vals);
|
trampoline_builder.ins().return_(&ret_vals);
|
||||||
|
|
||||||
module.define_function(func_id, context).unwrap();
|
module.define_function(func_id, context).unwrap();
|
||||||
|
cx.unwind_context.add_function(func_id, context, module.isa());
|
||||||
}
|
}
|
||||||
|
18
src/lib.rs
18
src/lib.rs
@ -26,9 +26,11 @@ extern crate rustc_driver;
|
|||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use rustc_codegen_ssa::traits::CodegenBackend;
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
||||||
use rustc_codegen_ssa::CodegenResults;
|
use rustc_codegen_ssa::CodegenResults;
|
||||||
|
use rustc_data_structures::profiling::SelfProfilerRef;
|
||||||
use rustc_errors::ErrorGuaranteed;
|
use rustc_errors::ErrorGuaranteed;
|
||||||
use rustc_metadata::EncodedMetadata;
|
use rustc_metadata::EncodedMetadata;
|
||||||
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
||||||
@ -120,18 +122,20 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
|||||||
|
|
||||||
/// The codegen context holds any information shared between the codegen of individual functions
|
/// The codegen context holds any information shared between the codegen of individual functions
|
||||||
/// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
|
/// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
|
||||||
struct CodegenCx<'tcx> {
|
struct CodegenCx {
|
||||||
tcx: TyCtxt<'tcx>,
|
profiler: SelfProfilerRef,
|
||||||
|
output_filenames: Arc<OutputFilenames>,
|
||||||
|
should_write_ir: bool,
|
||||||
global_asm: String,
|
global_asm: String,
|
||||||
inline_asm_index: Cell<usize>,
|
inline_asm_index: Cell<usize>,
|
||||||
debug_context: Option<DebugContext<'tcx>>,
|
debug_context: Option<DebugContext>,
|
||||||
unwind_context: UnwindContext,
|
unwind_context: UnwindContext,
|
||||||
cgu_name: Symbol,
|
cgu_name: Symbol,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> CodegenCx<'tcx> {
|
impl CodegenCx {
|
||||||
fn new(
|
fn new(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'_>,
|
||||||
backend_config: BackendConfig,
|
backend_config: BackendConfig,
|
||||||
isa: &dyn TargetIsa,
|
isa: &dyn TargetIsa,
|
||||||
debug_info: bool,
|
debug_info: bool,
|
||||||
@ -147,7 +151,9 @@ impl<'tcx> CodegenCx<'tcx> {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
CodegenCx {
|
CodegenCx {
|
||||||
tcx,
|
profiler: tcx.prof.clone(),
|
||||||
|
output_filenames: tcx.output_filenames(()).clone(),
|
||||||
|
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
|
||||||
global_asm: String::new(),
|
global_asm: String::new(),
|
||||||
inline_asm_index: Cell::new(0),
|
inline_asm_index: Cell::new(0),
|
||||||
debug_context,
|
debug_context,
|
||||||
|
@ -1,20 +1,3 @@
|
|||||||
//! Various optimizations specific to cg_clif
|
//! Various optimizations specific to cg_clif
|
||||||
|
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
|
||||||
|
|
||||||
use crate::prelude::*;
|
|
||||||
|
|
||||||
pub(crate) mod peephole;
|
pub(crate) mod peephole;
|
||||||
|
|
||||||
pub(crate) fn optimize_function<'tcx>(
|
|
||||||
tcx: TyCtxt<'tcx>,
|
|
||||||
isa: &dyn TargetIsa,
|
|
||||||
instance: Instance<'tcx>,
|
|
||||||
ctx: &mut Context,
|
|
||||||
clif_comments: &mut crate::pretty_clif::CommentWriter,
|
|
||||||
) {
|
|
||||||
// FIXME classify optimizations over opt levels once we have more
|
|
||||||
|
|
||||||
crate::pretty_clif::write_clif_file(tcx, "preopt", isa, instance, &ctx.func, &*clif_comments);
|
|
||||||
crate::base::verify_func(tcx, &*clif_comments, &ctx.func);
|
|
||||||
}
|
|
||||||
|
@ -62,7 +62,7 @@ use cranelift_codegen::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use rustc_middle::ty::layout::FnAbiOf;
|
use rustc_middle::ty::layout::FnAbiOf;
|
||||||
use rustc_session::config::OutputType;
|
use rustc_session::config::{OutputFilenames, OutputType};
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
@ -205,15 +205,11 @@ pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn write_ir_file(
|
pub(crate) fn write_ir_file(
|
||||||
tcx: TyCtxt<'_>,
|
output_filenames: &OutputFilenames,
|
||||||
name: impl FnOnce() -> String,
|
name: &str,
|
||||||
write: impl FnOnce(&mut dyn Write) -> std::io::Result<()>,
|
write: impl FnOnce(&mut dyn Write) -> std::io::Result<()>,
|
||||||
) {
|
) {
|
||||||
if !should_write_ir(tcx) {
|
let clif_output_dir = output_filenames.with_extension("clif");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let clif_output_dir = tcx.output_filenames(()).with_extension("clif");
|
|
||||||
|
|
||||||
match std::fs::create_dir(&clif_output_dir) {
|
match std::fs::create_dir(&clif_output_dir) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
@ -221,44 +217,43 @@ pub(crate) fn write_ir_file(
|
|||||||
res @ Err(_) => res.unwrap(),
|
res @ Err(_) => res.unwrap(),
|
||||||
}
|
}
|
||||||
|
|
||||||
let clif_file_name = clif_output_dir.join(name());
|
let clif_file_name = clif_output_dir.join(name);
|
||||||
|
|
||||||
let res = std::fs::File::create(clif_file_name).and_then(|mut file| write(&mut file));
|
let res = std::fs::File::create(clif_file_name).and_then(|mut file| write(&mut file));
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
tcx.sess.warn(&format!("error writing ir file: {}", err));
|
// Using early_warn as no Session is available here
|
||||||
|
rustc_session::early_warn(
|
||||||
|
rustc_session::config::ErrorOutputType::default(),
|
||||||
|
&format!("error writing ir file: {}", err),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn write_clif_file<'tcx>(
|
pub(crate) fn write_clif_file(
|
||||||
tcx: TyCtxt<'tcx>,
|
output_filenames: &OutputFilenames,
|
||||||
|
symbol_name: &str,
|
||||||
postfix: &str,
|
postfix: &str,
|
||||||
isa: &dyn cranelift_codegen::isa::TargetIsa,
|
isa: &dyn cranelift_codegen::isa::TargetIsa,
|
||||||
instance: Instance<'tcx>,
|
|
||||||
func: &cranelift_codegen::ir::Function,
|
func: &cranelift_codegen::ir::Function,
|
||||||
mut clif_comments: &CommentWriter,
|
mut clif_comments: &CommentWriter,
|
||||||
) {
|
) {
|
||||||
// FIXME work around filename too long errors
|
// FIXME work around filename too long errors
|
||||||
write_ir_file(
|
write_ir_file(output_filenames, &format!("{}.{}.clif", symbol_name, postfix), |file| {
|
||||||
tcx,
|
let mut clif = String::new();
|
||||||
|| format!("{}.{}.clif", tcx.symbol_name(instance).name, postfix),
|
cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func).unwrap();
|
||||||
|file| {
|
|
||||||
let mut clif = String::new();
|
|
||||||
cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
for flag in isa.flags().iter() {
|
for flag in isa.flags().iter() {
|
||||||
writeln!(file, "set {}", flag)?;
|
writeln!(file, "set {}", flag)?;
|
||||||
}
|
}
|
||||||
write!(file, "target {}", isa.triple().architecture.to_string())?;
|
write!(file, "target {}", isa.triple().architecture.to_string())?;
|
||||||
for isa_flag in isa.isa_flags().iter() {
|
for isa_flag in isa.isa_flags().iter() {
|
||||||
write!(file, " {}", isa_flag)?;
|
write!(file, " {}", isa_flag)?;
|
||||||
}
|
}
|
||||||
writeln!(file, "\n")?;
|
writeln!(file, "\n")?;
|
||||||
writeln!(file)?;
|
writeln!(file)?;
|
||||||
file.write_all(clif.as_bytes())?;
|
file.write_all(clif.as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for FunctionCx<'_, '_, '_> {
|
impl fmt::Debug for FunctionCx<'_, '_, '_> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user