Skip to content

Commit 8dab8de

Browse files
committed
Get rid of CodegenCx
1 parent 9360f7c commit 8dab8de

File tree

8 files changed

+63
-67
lines changed

8 files changed

+63
-67
lines changed

src/base.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::adjustment::PointerCoercion;
1313
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
1414
use rustc_middle::ty::print::with_no_trimmed_paths;
1515
use rustc_session::config::OutputFilenames;
16+
use rustc_span::Symbol;
1617

1718
use crate::constant::ConstantCx;
1819
use crate::debuginfo::{FunctionDebugContext, TypeDebugContext};
@@ -31,7 +32,8 @@ pub(crate) struct CodegenedFunction {
3132

3233
pub(crate) fn codegen_fn<'tcx>(
3334
tcx: TyCtxt<'tcx>,
34-
cx: &mut crate::CodegenCx,
35+
cgu_name: Symbol,
36+
mut debug_context: Option<&mut DebugContext>,
3537
type_dbg: &mut TypeDebugContext<'tcx>,
3638
cached_func: Function,
3739
module: &mut dyn Module,
@@ -63,7 +65,7 @@ pub(crate) fn codegen_fn<'tcx>(
6365
func.clear();
6466
func.name = UserFuncName::user(0, func_id.as_u32());
6567
func.signature = sig;
66-
if cx.debug_context.is_some() {
68+
if debug_context.is_some() {
6769
func.collect_debug_info();
6870
}
6971

@@ -79,9 +81,10 @@ pub(crate) fn codegen_fn<'tcx>(
7981
// Make FunctionCx
8082
let target_config = module.target_config();
8183
let pointer_type = target_config.pointer_type();
84+
assert_eq!(pointer_ty(tcx), pointer_type);
8285
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance, fn_abi);
8386

84-
let func_debug_cx = if let Some(debug_context) = &mut cx.debug_context {
87+
let func_debug_cx = if let Some(debug_context) = debug_context.as_deref_mut() {
8588
Some(debug_context.define_function(tcx, type_dbg, instance, fn_abi, &symbol_name, mir.span))
8689
} else {
8790
None
@@ -91,14 +94,15 @@ pub(crate) fn codegen_fn<'tcx>(
9194
bcx.declare_var(exception_slot, pointer_type);
9295

9396
let mut fx = FunctionCx {
94-
cx,
9597
module,
98+
debug_context,
9699
tcx,
97100
target_config,
98101
pointer_type,
99102
constants_cx: ConstantCx::new(),
100103
func_debug_cx,
101104

105+
cgu_name,
102106
instance,
103107
symbol_name,
104108
mir,
@@ -128,7 +132,7 @@ pub(crate) fn codegen_fn<'tcx>(
128132

129133
fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
130134

131-
if cx.should_write_ir {
135+
if crate::pretty_clif::should_write_ir(tcx.sess) {
132136
crate::pretty_clif::write_clif_file(
133137
tcx.output_filenames(()),
134138
&symbol_name,
@@ -146,11 +150,12 @@ pub(crate) fn codegen_fn<'tcx>(
146150
}
147151

148152
pub(crate) fn compile_fn(
149-
cx: &mut crate::CodegenCx,
150153
profiler: &SelfProfilerRef,
151154
output_filenames: &OutputFilenames,
155+
should_write_ir: bool,
152156
cached_context: &mut Context,
153157
module: &mut dyn Module,
158+
debug_context: Option<&mut DebugContext>,
154159
global_asm: &mut String,
155160
codegened_func: CodegenedFunction,
156161
) {
@@ -195,7 +200,7 @@ pub(crate) fn compile_fn(
195200

196201
// Define function
197202
profiler.generic_activity("define function").run(|| {
198-
context.want_disasm = cx.should_write_ir;
203+
context.want_disasm = should_write_ir;
199204
match module.define_function(codegened_func.func_id, context) {
200205
Ok(()) => {}
201206
Err(ModuleError::Compilation(CodegenError::ImplLimitExceeded)) => {
@@ -225,7 +230,7 @@ pub(crate) fn compile_fn(
225230
}
226231
});
227232

228-
if cx.should_write_ir {
233+
if should_write_ir {
229234
// Write optimized function to file for debugging
230235
crate::pretty_clif::write_clif_file(
231236
output_filenames,
@@ -246,7 +251,6 @@ pub(crate) fn compile_fn(
246251
}
247252

248253
// Define debuginfo for function
249-
let debug_context = &mut cx.debug_context;
250254
profiler.generic_activity("generate debug info").run(|| {
251255
if let Some(debug_context) = debug_context {
252256
codegened_func.func_debug_cx.unwrap().finalize(

src/common.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::ty::TypeFoldable;
66
use rustc_middle::ty::layout::{
77
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
88
};
9+
use rustc_span::Symbol;
910
use rustc_span::source_map::Spanned;
1011
use rustc_target::callconv::FnAbi;
1112
use rustc_target::spec::{HasTargetSpec, Target};
@@ -268,14 +269,15 @@ pub(crate) fn create_wrapper_function(
268269
}
269270

270271
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
271-
pub(crate) cx: &'clif mut crate::CodegenCx,
272272
pub(crate) module: &'m mut dyn Module,
273+
pub(crate) debug_context: Option<&'clif mut DebugContext>,
273274
pub(crate) tcx: TyCtxt<'tcx>,
274275
pub(crate) target_config: TargetFrontendConfig, // Cached from module
275276
pub(crate) pointer_type: Type, // Cached from module
276277
pub(crate) constants_cx: ConstantCx,
277278
pub(crate) func_debug_cx: Option<FunctionDebugContext>,
278279

280+
pub(crate) cgu_name: Symbol,
279281
pub(crate) instance: Instance<'tcx>,
280282
pub(crate) symbol_name: String,
281283
pub(crate) mir: &'tcx Body<'tcx>,
@@ -407,7 +409,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
407409
}
408410

409411
pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
410-
if let Some(debug_context) = &mut self.cx.debug_context {
412+
if let Some(debug_context) = &mut self.debug_context {
411413
let (file_id, line, column) =
412414
debug_context.get_span_loc(self.tcx, self.mir.span, source_info.span);
413415

src/debuginfo/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_codegen_ssa::debuginfo::type_names;
2020
use rustc_hir::def::DefKind;
2121
use rustc_hir::def_id::DefIdMap;
2222
use rustc_session::Session;
23+
use rustc_session::config::DebugInfo;
2324
use rustc_span::{FileNameDisplayPreference, SourceFileHash, StableSourceFileId};
2425
use rustc_target::callconv::FnAbi;
2526

@@ -53,7 +54,19 @@ pub(crate) struct FunctionDebugContext {
5354
}
5455

5556
impl DebugContext {
56-
pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, cgu_name: &str) -> Self {
57+
pub(crate) fn new(
58+
tcx: TyCtxt<'_>,
59+
isa: &dyn TargetIsa,
60+
force_disable_debuginfo: bool,
61+
cgu_name: &str,
62+
) -> Option<Self> {
63+
if tcx.sess.opts.debuginfo == DebugInfo::None
64+
|| force_disable_debuginfo
65+
|| tcx.sess.target.options.is_like_windows
66+
{
67+
return None;
68+
}
69+
5770
let encoding = Encoding {
5871
format: Format::Dwarf32,
5972
// FIXME this should be configurable
@@ -146,7 +159,7 @@ impl DebugContext {
146159
AttributeValue::Udata(isa.frontend_config().pointer_bytes().into()),
147160
);
148161

149-
DebugContext {
162+
Some(DebugContext {
150163
endian,
151164
dwarf,
152165
unit_range_list: RangeList(Vec::new()),
@@ -155,7 +168,7 @@ impl DebugContext {
155168
namespace_map: DefIdMap::default(),
156169
array_size_type,
157170
filename_display_preference,
158-
}
171+
})
159172
}
160173

161174
fn item_namespace(&mut self, tcx: TyCtxt<'_>, def_id: DefId) -> UnitEntryId {

src/driver/aot.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ use rustc_middle::mir::mono::{
2525
CodegenUnit, Linkage as RLinkage, MonoItem, MonoItemData, Visibility,
2626
};
2727
use rustc_session::Session;
28-
use rustc_session::config::{DebugInfo, OutFileName, OutputFilenames, OutputType};
28+
use rustc_session::config::{OutFileName, OutputFilenames, OutputType};
2929

30-
use crate::CodegenCx;
3130
use crate::base::CodegenedFunction;
3231
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
3332
use crate::debuginfo::TypeDebugContext;
@@ -512,18 +511,13 @@ fn codegen_cgu_content(
512511
tcx: TyCtxt<'_>,
513512
module: &mut dyn Module,
514513
cgu_name: rustc_span::Symbol,
515-
) -> (CodegenCx, Vec<CodegenedFunction>, String) {
514+
) -> (Option<DebugContext>, Vec<CodegenedFunction>, String) {
516515
let _timer = tcx.prof.generic_activity_with_arg("codegen cgu", cgu_name.as_str());
517516

518517
let cgu = tcx.codegen_unit(cgu_name);
519518
let mono_items = cgu.items_in_deterministic_order(tcx);
520519

521-
let mut cx = crate::CodegenCx::new(
522-
tcx,
523-
module.isa(),
524-
tcx.sess.opts.debuginfo != DebugInfo::None,
525-
cgu_name,
526-
);
520+
let mut debug_context = DebugContext::new(tcx, module.isa(), false, cgu_name.as_str());
527521
let mut global_asm = String::new();
528522
let mut type_dbg = TypeDebugContext::default();
529523
super::predefine_mono_items(tcx, module, &mono_items);
@@ -550,7 +544,8 @@ fn codegen_cgu_content(
550544
}
551545
let codegened_function = crate::base::codegen_fn(
552546
tcx,
553-
&mut cx,
547+
cgu_name,
548+
debug_context.as_mut(),
554549
&mut type_dbg,
555550
Function::new(),
556551
module,
@@ -560,7 +555,7 @@ fn codegen_cgu_content(
560555
}
561556
MonoItem::Static(def_id) => {
562557
let data_id = crate::constant::codegen_static(tcx, module, def_id);
563-
if let Some(debug_context) = &mut cx.debug_context {
558+
if let Some(debug_context) = debug_context.as_mut() {
564559
debug_context.define_static(tcx, &mut type_dbg, def_id, data_id);
565560
}
566561
}
@@ -574,7 +569,7 @@ fn codegen_cgu_content(
574569
}
575570
crate::main_shim::maybe_create_entry_wrapper(tcx, module, false, cgu.is_primary());
576571

577-
(cx, codegened_functions, global_asm)
572+
(debug_context, codegened_functions, global_asm)
578573
}
579574

580575
fn module_codegen(
@@ -587,7 +582,7 @@ fn module_codegen(
587582
) -> OngoingModuleCodegen {
588583
let mut module = make_module(tcx.sess, cgu_name.as_str().to_string());
589584

590-
let (mut cx, codegened_functions, mut global_asm) =
585+
let (mut debug_context, codegened_functions, mut global_asm) =
591586
codegen_cgu_content(tcx, &mut module, cgu_name);
592587

593588
let cgu_name = cgu_name.as_str().to_owned();
@@ -597,6 +592,7 @@ fn module_codegen(
597592
let profiler = tcx.prof.clone();
598593
let invocation_temp = tcx.sess.invocation_temp.clone();
599594
let output_filenames = tcx.output_filenames(()).clone();
595+
let should_write_ir = crate::pretty_clif::should_write_ir(tcx.sess);
600596

601597
OngoingModuleCodegen::Async(std::thread::spawn(move || {
602598
profiler.clone().generic_activity_with_arg("compile functions", &*cgu_name).run(|| {
@@ -607,11 +603,12 @@ fn module_codegen(
607603
let mut cached_context = Context::new();
608604
for codegened_func in codegened_functions {
609605
crate::base::compile_fn(
610-
&mut cx,
611606
&profiler,
612607
&output_filenames,
608+
should_write_ir,
613609
&mut cached_context,
614610
&mut module,
611+
debug_context.as_mut(),
615612
&mut global_asm,
616613
codegened_func,
617614
);
@@ -636,7 +633,7 @@ fn module_codegen(
636633
&profiler,
637634
cgu_name,
638635
module,
639-
cx.debug_context,
636+
debug_context,
640637
global_asm_object_file,
641638
&producer,
642639
)

src/driver/jit.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ use rustc_session::Session;
1212
use rustc_session::config::OutputFilenames;
1313
use rustc_span::sym;
1414

15-
use crate::CodegenCx;
1615
use crate::debuginfo::TypeDebugContext;
1716
use crate::prelude::*;
1817
use crate::unwind_module::UnwindModule;
1918

20-
fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, CodegenCx) {
19+
fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, Option<DebugContext>) {
2120
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
2221

2322
let isa = crate::build_isa(tcx.sess, true);
@@ -26,7 +25,7 @@ fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, CodegenCx) {
2625
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
2726
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
2827

29-
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);
28+
let cx = DebugContext::new(tcx, jit_module.isa(), false, "dummy_cgu_name");
3029

3130
crate::allocator::codegen(tcx, &mut jit_module);
3231

@@ -43,7 +42,8 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
4342
}
4443

4544
let output_filenames = tcx.output_filenames(());
46-
let (mut jit_module, mut cx) = create_jit_module(tcx);
45+
let should_write_ir = crate::pretty_clif::should_write_ir(tcx.sess);
46+
let (mut jit_module, mut debug_context) = create_jit_module(tcx);
4747
let mut cached_context = Context::new();
4848

4949
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
@@ -63,7 +63,8 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
6363
codegen_and_compile_fn(
6464
tcx,
6565
&output_filenames,
66-
&mut cx,
66+
should_write_ir,
67+
debug_context.as_mut(),
6768
&mut cached_context,
6869
&mut jit_module,
6970
inst,
@@ -122,7 +123,8 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
122123
fn codegen_and_compile_fn<'tcx>(
123124
tcx: TyCtxt<'tcx>,
124125
output_filenames: &OutputFilenames,
125-
cx: &mut crate::CodegenCx,
126+
should_write_ir: bool,
127+
mut debug_context: Option<&mut DebugContext>,
126128
cached_context: &mut Context,
127129
module: &mut dyn Module,
128130
instance: Instance<'tcx>,
@@ -143,7 +145,8 @@ fn codegen_and_compile_fn<'tcx>(
143145
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
144146
let codegened_func = crate::base::codegen_fn(
145147
tcx,
146-
cx,
148+
sym::dummy_cgu_name,
149+
debug_context.as_deref_mut(),
147150
&mut TypeDebugContext::default(),
148151
cached_func,
149152
module,
@@ -152,11 +155,12 @@ fn codegen_and_compile_fn<'tcx>(
152155

153156
let mut global_asm = String::new();
154157
crate::base::compile_fn(
155-
cx,
156158
&tcx.prof,
157159
output_filenames,
160+
should_write_ir,
158161
cached_context,
159162
module,
163+
debug_context.as_deref_mut(),
160164
&mut global_asm,
161165
codegened_func,
162166
);

src/inline_asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
125125
let wrapper_name = format!(
126126
"{}__inline_asm_{}_wrapper_n{}",
127127
fx.symbol_name,
128-
fx.cx.cgu_name.as_str().replace('.', "__").replace('-', "_"),
128+
fx.cgu_name.as_str().replace('.', "__").replace('-', "_"),
129129
fx.inline_asm_index,
130130
);
131131
fx.inline_asm_index += 1;
@@ -189,7 +189,7 @@ pub(crate) fn codegen_inline_asm_inner<'tcx>(
189189
let asm_name = format!(
190190
"{}__inline_asm_{}_n{}",
191191
fx.symbol_name,
192-
fx.cx.cgu_name.as_str().replace('.', "__").replace('-', "_"),
192+
fx.cgu_name.as_str().replace('.', "__").replace('-', "_"),
193193
fx.inline_asm_index,
194194
);
195195
fx.inline_asm_index += 1;

0 commit comments

Comments
 (0)