Skip to content

Commit c820b7c

Browse files
committed
Remove TyCtxt field from CodegenCx
1 parent b181f2b commit c820b7c

File tree

5 files changed

+73
-72
lines changed

5 files changed

+73
-72
lines changed

src/base.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,30 @@ struct CodegenedFunction<'tcx> {
2424
}
2525

2626
pub(crate) fn codegen_and_compile_fn<'tcx>(
27+
tcx: TyCtxt<'tcx>,
2728
cx: &mut crate::CodegenCx<'tcx>,
2829
cached_context: &mut Context,
2930
module: &mut dyn Module,
3031
instance: Instance<'tcx>,
3132
) {
32-
let tcx = cx.tcx;
3333
let _inst_guard =
3434
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
3535

3636
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
37-
let codegened_func = codegen_fn(cx, cached_func, module, instance);
37+
let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance);
3838

39-
compile_fn(cx, cached_context, module, codegened_func);
39+
compile_fn(tcx, cx, cached_context, module, codegened_func);
4040
}
4141

4242
fn codegen_fn<'tcx>(
43+
tcx: TyCtxt<'tcx>,
4344
cx: &mut crate::CodegenCx<'tcx>,
4445
cached_func: Function,
4546
module: &mut dyn Module,
4647
instance: Instance<'tcx>,
4748
) -> CodegenedFunction<'tcx> {
4849
debug_assert!(!instance.substs.needs_infer());
4950

50-
let tcx = cx.tcx;
51-
5251
let mir = tcx.instance_mir(instance.def);
5352
let _mir_guard = crate::PrintOnPanic(|| {
5453
let mut buf = Vec::new();
@@ -117,14 +116,16 @@ fn codegen_fn<'tcx>(
117116

118117
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
119118

120-
crate::pretty_clif::write_clif_file(
121-
tcx,
122-
symbol_name.name,
123-
"unopt",
124-
module.isa(),
125-
&func,
126-
&clif_comments,
127-
);
119+
if cx.should_write_ir {
120+
crate::pretty_clif::write_clif_file(
121+
tcx.output_filenames(()),
122+
symbol_name.name,
123+
"unopt",
124+
module.isa(),
125+
&func,
126+
&clif_comments,
127+
);
128+
}
128129

129130
// Verify function
130131
verify_func(tcx, &clif_comments, &func);
@@ -141,13 +142,12 @@ fn codegen_fn<'tcx>(
141142
}
142143

143144
fn compile_fn<'tcx>(
145+
tcx: TyCtxt<'tcx>,
144146
cx: &mut crate::CodegenCx<'tcx>,
145147
cached_context: &mut Context,
146148
module: &mut dyn Module,
147149
codegened_func: CodegenedFunction<'tcx>,
148150
) {
149-
let tcx = cx.tcx;
150-
151151
let clif_comments = codegened_func.clif_comments;
152152

153153
// Store function in context
@@ -194,26 +194,28 @@ fn compile_fn<'tcx>(
194194

195195
// Define function
196196
tcx.sess.time("define function", || {
197-
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
197+
context.want_disasm = cx.should_write_ir;
198198
module.define_function(codegened_func.func_id, context).unwrap();
199199
});
200200

201-
// Write optimized function to file for debugging
202-
crate::pretty_clif::write_clif_file(
203-
tcx,
204-
codegened_func.symbol_name.name,
205-
"opt",
206-
module.isa(),
207-
&context.func,
208-
&clif_comments,
209-
);
210-
211-
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
212-
crate::pretty_clif::write_ir_file(
213-
tcx,
214-
|| format!("{}.vcode", codegened_func.symbol_name.name),
215-
|file| file.write_all(disasm.as_bytes()),
216-
)
201+
if cx.should_write_ir {
202+
// Write optimized function to file for debugging
203+
crate::pretty_clif::write_clif_file(
204+
&cx.output_filenames,
205+
codegened_func.symbol_name.name,
206+
"opt",
207+
module.isa(),
208+
&context.func,
209+
&clif_comments,
210+
);
211+
212+
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
213+
crate::pretty_clif::write_ir_file(
214+
&cx.output_filenames,
215+
&format!("{}.vcode", codegened_func.symbol_name.name),
216+
|file| file.write_all(disasm.as_bytes()),
217+
)
218+
}
217219
}
218220

219221
// Define debuginfo for function

src/driver/aot.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ fn module_codegen(
256256
for (mono_item, _) in mono_items {
257257
match mono_item {
258258
MonoItem::Fn(inst) => {
259-
cx.tcx.sess.time("codegen fn", || {
259+
tcx.sess.time("codegen fn", || {
260260
crate::base::codegen_and_compile_fn(
261+
tcx,
261262
&mut cx,
262263
&mut cached_context,
263264
&mut module,

src/driver/jit.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
129129
MonoItem::Fn(inst) => match backend_config.codegen_mode {
130130
CodegenMode::Aot => unreachable!(),
131131
CodegenMode::Jit => {
132-
cx.tcx.sess.time("codegen fn", || {
132+
tcx.sess.time("codegen fn", || {
133133
crate::base::codegen_and_compile_fn(
134+
tcx,
134135
&mut cx,
135136
&mut cached_context,
136137
&mut jit_module,
@@ -139,7 +140,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
139140
});
140141
}
141142
CodegenMode::JitLazy => {
142-
codegen_shim(&mut cx, &mut cached_context, &mut jit_module, inst)
143+
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
143144
}
144145
},
145146
MonoItem::Static(def_id) => {
@@ -269,6 +270,7 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
269270
);
270271
tcx.sess.time("codegen fn", || {
271272
crate::base::codegen_and_compile_fn(
273+
tcx,
272274
&mut cx,
273275
&mut Context::new(),
274276
jit_module,
@@ -350,13 +352,11 @@ fn load_imported_symbols_for_jit(
350352
}
351353

352354
fn codegen_shim<'tcx>(
353-
cx: &mut CodegenCx<'tcx>,
355+
tcx: TyCtxt<'tcx>,
354356
cached_context: &mut Context,
355357
module: &mut JITModule,
356358
inst: Instance<'tcx>,
357359
) {
358-
let tcx = cx.tcx;
359-
360360
let pointer_type = module.target_config().pointer_type();
361361

362362
let name = tcx.symbol_name(inst).name;

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern crate rustc_driver;
2626

2727
use std::any::Any;
2828
use std::cell::{Cell, RefCell};
29+
use std::sync::Arc;
2930

3031
use rustc_codegen_ssa::traits::CodegenBackend;
3132
use rustc_codegen_ssa::CodegenResults;
@@ -121,7 +122,8 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
121122
/// The codegen context holds any information shared between the codegen of individual functions
122123
/// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
123124
struct CodegenCx<'tcx> {
124-
tcx: TyCtxt<'tcx>,
125+
output_filenames: Arc<OutputFilenames>,
126+
should_write_ir: bool,
125127
global_asm: String,
126128
inline_asm_index: Cell<usize>,
127129
debug_context: Option<DebugContext<'tcx>>,
@@ -147,7 +149,8 @@ impl<'tcx> CodegenCx<'tcx> {
147149
None
148150
};
149151
CodegenCx {
150-
tcx,
152+
output_filenames: tcx.output_filenames(()).clone(),
153+
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
151154
global_asm: String::new(),
152155
inline_asm_index: Cell::new(0),
153156
debug_context,

src/pretty_clif.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use cranelift_codegen::{
6262
};
6363

6464
use rustc_middle::ty::layout::FnAbiOf;
65-
use rustc_session::config::OutputType;
65+
use rustc_session::config::{OutputFilenames, OutputType};
6666

6767
use crate::prelude::*;
6868

@@ -205,60 +205,55 @@ pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool {
205205
}
206206

207207
pub(crate) fn write_ir_file(
208-
tcx: TyCtxt<'_>,
209-
name: impl FnOnce() -> String,
208+
output_filenames: &OutputFilenames,
209+
name: &str,
210210
write: impl FnOnce(&mut dyn Write) -> std::io::Result<()>,
211211
) {
212-
if !should_write_ir(tcx) {
213-
return;
214-
}
215-
216-
let clif_output_dir = tcx.output_filenames(()).with_extension("clif");
212+
let clif_output_dir = output_filenames.with_extension("clif");
217213

218214
match std::fs::create_dir(&clif_output_dir) {
219215
Ok(()) => {}
220216
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {}
221217
res @ Err(_) => res.unwrap(),
222218
}
223219

224-
let clif_file_name = clif_output_dir.join(name());
220+
let clif_file_name = clif_output_dir.join(name);
225221

226222
let res = std::fs::File::create(clif_file_name).and_then(|mut file| write(&mut file));
227223
if let Err(err) = res {
228-
tcx.sess.warn(&format!("error writing ir file: {}", err));
224+
// Using early_warn as no Session is available here
225+
rustc_session::early_warn(
226+
rustc_session::config::ErrorOutputType::default(),
227+
&format!("error writing ir file: {}", err),
228+
);
229229
}
230230
}
231231

232-
pub(crate) fn write_clif_file<'tcx>(
233-
tcx: TyCtxt<'tcx>,
232+
pub(crate) fn write_clif_file(
233+
output_filenames: &OutputFilenames,
234234
symbol_name: &str,
235235
postfix: &str,
236236
isa: &dyn cranelift_codegen::isa::TargetIsa,
237237
func: &cranelift_codegen::ir::Function,
238238
mut clif_comments: &CommentWriter,
239239
) {
240240
// FIXME work around filename too long errors
241-
write_ir_file(
242-
tcx,
243-
|| format!("{}.{}.clif", symbol_name, postfix),
244-
|file| {
245-
let mut clif = String::new();
246-
cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func)
247-
.unwrap();
241+
write_ir_file(output_filenames, &format!("{}.{}.clif", symbol_name, postfix), |file| {
242+
let mut clif = String::new();
243+
cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func).unwrap();
248244

249-
for flag in isa.flags().iter() {
250-
writeln!(file, "set {}", flag)?;
251-
}
252-
write!(file, "target {}", isa.triple().architecture.to_string())?;
253-
for isa_flag in isa.isa_flags().iter() {
254-
write!(file, " {}", isa_flag)?;
255-
}
256-
writeln!(file, "\n")?;
257-
writeln!(file)?;
258-
file.write_all(clif.as_bytes())?;
259-
Ok(())
260-
},
261-
);
245+
for flag in isa.flags().iter() {
246+
writeln!(file, "set {}", flag)?;
247+
}
248+
write!(file, "target {}", isa.triple().architecture.to_string())?;
249+
for isa_flag in isa.isa_flags().iter() {
250+
write!(file, " {}", isa_flag)?;
251+
}
252+
writeln!(file, "\n")?;
253+
writeln!(file)?;
254+
file.write_all(clif.as_bytes())?;
255+
Ok(())
256+
});
262257
}
263258

264259
impl fmt::Debug for FunctionCx<'_, '_, '_> {

0 commit comments

Comments
 (0)