mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Remove TyCtxt field from CodegenCx
This commit is contained in:
parent
b181f2b376
commit
c820b7cd60
64
src/base.rs
64
src/base.rs
@ -24,22 +24,23 @@ struct CodegenedFunction<'tcx> {
|
||||
}
|
||||
|
||||
pub(crate) fn codegen_and_compile_fn<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
cx: &mut crate::CodegenCx<'tcx>,
|
||||
cached_context: &mut Context,
|
||||
module: &mut dyn Module,
|
||||
instance: Instance<'tcx>,
|
||||
) {
|
||||
let tcx = cx.tcx;
|
||||
let _inst_guard =
|
||||
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
|
||||
|
||||
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(tcx, cx, cached_context, module, codegened_func);
|
||||
}
|
||||
|
||||
fn codegen_fn<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
cx: &mut crate::CodegenCx<'tcx>,
|
||||
cached_func: Function,
|
||||
module: &mut dyn Module,
|
||||
@ -47,8 +48,6 @@ fn codegen_fn<'tcx>(
|
||||
) -> CodegenedFunction<'tcx> {
|
||||
debug_assert!(!instance.substs.needs_infer());
|
||||
|
||||
let tcx = cx.tcx;
|
||||
|
||||
let mir = tcx.instance_mir(instance.def);
|
||||
let _mir_guard = crate::PrintOnPanic(|| {
|
||||
let mut buf = Vec::new();
|
||||
@ -117,14 +116,16 @@ fn codegen_fn<'tcx>(
|
||||
|
||||
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
|
||||
|
||||
crate::pretty_clif::write_clif_file(
|
||||
tcx,
|
||||
symbol_name.name,
|
||||
"unopt",
|
||||
module.isa(),
|
||||
&func,
|
||||
&clif_comments,
|
||||
);
|
||||
if cx.should_write_ir {
|
||||
crate::pretty_clif::write_clif_file(
|
||||
tcx.output_filenames(()),
|
||||
symbol_name.name,
|
||||
"unopt",
|
||||
module.isa(),
|
||||
&func,
|
||||
&clif_comments,
|
||||
);
|
||||
}
|
||||
|
||||
// Verify function
|
||||
verify_func(tcx, &clif_comments, &func);
|
||||
@ -141,13 +142,12 @@ fn codegen_fn<'tcx>(
|
||||
}
|
||||
|
||||
fn compile_fn<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
cx: &mut crate::CodegenCx<'tcx>,
|
||||
cached_context: &mut Context,
|
||||
module: &mut dyn Module,
|
||||
codegened_func: CodegenedFunction<'tcx>,
|
||||
) {
|
||||
let tcx = cx.tcx;
|
||||
|
||||
let clif_comments = codegened_func.clif_comments;
|
||||
|
||||
// Store function in context
|
||||
@ -194,26 +194,28 @@ fn compile_fn<'tcx>(
|
||||
|
||||
// Define function
|
||||
tcx.sess.time("define function", || {
|
||||
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();
|
||||
});
|
||||
|
||||
// Write optimized function to file for debugging
|
||||
crate::pretty_clif::write_clif_file(
|
||||
tcx,
|
||||
codegened_func.symbol_name.name,
|
||||
"opt",
|
||||
module.isa(),
|
||||
&context.func,
|
||||
&clif_comments,
|
||||
);
|
||||
if cx.should_write_ir {
|
||||
// Write optimized function to file for debugging
|
||||
crate::pretty_clif::write_clif_file(
|
||||
&cx.output_filenames,
|
||||
codegened_func.symbol_name.name,
|
||||
"opt",
|
||||
module.isa(),
|
||||
&context.func,
|
||||
&clif_comments,
|
||||
);
|
||||
|
||||
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
|
||||
crate::pretty_clif::write_ir_file(
|
||||
tcx,
|
||||
|| format!("{}.vcode", codegened_func.symbol_name.name),
|
||||
|file| file.write_all(disasm.as_bytes()),
|
||||
)
|
||||
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
|
||||
crate::pretty_clif::write_ir_file(
|
||||
&cx.output_filenames,
|
||||
&format!("{}.vcode", codegened_func.symbol_name.name),
|
||||
|file| file.write_all(disasm.as_bytes()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Define debuginfo for function
|
||||
|
@ -256,8 +256,9 @@ fn module_codegen(
|
||||
for (mono_item, _) in mono_items {
|
||||
match mono_item {
|
||||
MonoItem::Fn(inst) => {
|
||||
cx.tcx.sess.time("codegen fn", || {
|
||||
tcx.sess.time("codegen fn", || {
|
||||
crate::base::codegen_and_compile_fn(
|
||||
tcx,
|
||||
&mut cx,
|
||||
&mut cached_context,
|
||||
&mut module,
|
||||
|
@ -129,8 +129,9 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
||||
MonoItem::Fn(inst) => match backend_config.codegen_mode {
|
||||
CodegenMode::Aot => unreachable!(),
|
||||
CodegenMode::Jit => {
|
||||
cx.tcx.sess.time("codegen fn", || {
|
||||
tcx.sess.time("codegen fn", || {
|
||||
crate::base::codegen_and_compile_fn(
|
||||
tcx,
|
||||
&mut cx,
|
||||
&mut cached_context,
|
||||
&mut jit_module,
|
||||
@ -139,7 +140,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
||||
});
|
||||
}
|
||||
CodegenMode::JitLazy => {
|
||||
codegen_shim(&mut cx, &mut cached_context, &mut jit_module, inst)
|
||||
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
|
||||
}
|
||||
},
|
||||
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", || {
|
||||
crate::base::codegen_and_compile_fn(
|
||||
tcx,
|
||||
&mut cx,
|
||||
&mut Context::new(),
|
||||
jit_module,
|
||||
@ -350,13 +352,11 @@ fn load_imported_symbols_for_jit(
|
||||
}
|
||||
|
||||
fn codegen_shim<'tcx>(
|
||||
cx: &mut CodegenCx<'tcx>,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
cached_context: &mut Context,
|
||||
module: &mut JITModule,
|
||||
inst: Instance<'tcx>,
|
||||
) {
|
||||
let tcx = cx.tcx;
|
||||
|
||||
let pointer_type = module.target_config().pointer_type();
|
||||
|
||||
let name = tcx.symbol_name(inst).name;
|
||||
|
@ -26,6 +26,7 @@ extern crate rustc_driver;
|
||||
|
||||
use std::any::Any;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::sync::Arc;
|
||||
|
||||
use rustc_codegen_ssa::traits::CodegenBackend;
|
||||
use rustc_codegen_ssa::CodegenResults;
|
||||
@ -121,7 +122,8 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
||||
/// 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).
|
||||
struct CodegenCx<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
output_filenames: Arc<OutputFilenames>,
|
||||
should_write_ir: bool,
|
||||
global_asm: String,
|
||||
inline_asm_index: Cell<usize>,
|
||||
debug_context: Option<DebugContext<'tcx>>,
|
||||
@ -147,7 +149,8 @@ impl<'tcx> CodegenCx<'tcx> {
|
||||
None
|
||||
};
|
||||
CodegenCx {
|
||||
tcx,
|
||||
output_filenames: tcx.output_filenames(()).clone(),
|
||||
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
|
||||
global_asm: String::new(),
|
||||
inline_asm_index: Cell::new(0),
|
||||
debug_context,
|
||||
|
@ -62,7 +62,7 @@ use cranelift_codegen::{
|
||||
};
|
||||
|
||||
use rustc_middle::ty::layout::FnAbiOf;
|
||||
use rustc_session::config::OutputType;
|
||||
use rustc_session::config::{OutputFilenames, OutputType};
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
@ -205,15 +205,11 @@ pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool {
|
||||
}
|
||||
|
||||
pub(crate) fn write_ir_file(
|
||||
tcx: TyCtxt<'_>,
|
||||
name: impl FnOnce() -> String,
|
||||
output_filenames: &OutputFilenames,
|
||||
name: &str,
|
||||
write: impl FnOnce(&mut dyn Write) -> std::io::Result<()>,
|
||||
) {
|
||||
if !should_write_ir(tcx) {
|
||||
return;
|
||||
}
|
||||
|
||||
let clif_output_dir = tcx.output_filenames(()).with_extension("clif");
|
||||
let clif_output_dir = output_filenames.with_extension("clif");
|
||||
|
||||
match std::fs::create_dir(&clif_output_dir) {
|
||||
Ok(()) => {}
|
||||
@ -221,16 +217,20 @@ pub(crate) fn write_ir_file(
|
||||
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));
|
||||
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>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
pub(crate) fn write_clif_file(
|
||||
output_filenames: &OutputFilenames,
|
||||
symbol_name: &str,
|
||||
postfix: &str,
|
||||
isa: &dyn cranelift_codegen::isa::TargetIsa,
|
||||
@ -238,27 +238,22 @@ pub(crate) fn write_clif_file<'tcx>(
|
||||
mut clif_comments: &CommentWriter,
|
||||
) {
|
||||
// FIXME work around filename too long errors
|
||||
write_ir_file(
|
||||
tcx,
|
||||
|| format!("{}.{}.clif", symbol_name, postfix),
|
||||
|file| {
|
||||
let mut clif = String::new();
|
||||
cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func)
|
||||
.unwrap();
|
||||
write_ir_file(output_filenames, &format!("{}.{}.clif", symbol_name, postfix), |file| {
|
||||
let mut clif = String::new();
|
||||
cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func).unwrap();
|
||||
|
||||
for flag in isa.flags().iter() {
|
||||
writeln!(file, "set {}", flag)?;
|
||||
}
|
||||
write!(file, "target {}", isa.triple().architecture.to_string())?;
|
||||
for isa_flag in isa.isa_flags().iter() {
|
||||
write!(file, " {}", isa_flag)?;
|
||||
}
|
||||
writeln!(file, "\n")?;
|
||||
writeln!(file)?;
|
||||
file.write_all(clif.as_bytes())?;
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
for flag in isa.flags().iter() {
|
||||
writeln!(file, "set {}", flag)?;
|
||||
}
|
||||
write!(file, "target {}", isa.triple().architecture.to_string())?;
|
||||
for isa_flag in isa.isa_flags().iter() {
|
||||
write!(file, " {}", isa_flag)?;
|
||||
}
|
||||
writeln!(file, "\n")?;
|
||||
writeln!(file)?;
|
||||
file.write_all(clif.as_bytes())?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
impl fmt::Debug for FunctionCx<'_, '_, '_> {
|
||||
|
Loading…
Reference in New Issue
Block a user