Skip to content

Commit 94aac0a

Browse files
committed
Pass around BackendConfig
1 parent c5dff34 commit 94aac0a

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

src/driver/aot.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_session::config::{DebugInfo, OutputType};
1414

1515
use cranelift_object::{ObjectModule, ObjectProduct};
1616

17-
use crate::prelude::*;
17+
use crate::{prelude::*, BackendConfig};
1818

1919
use crate::backend::AddConstructor;
2020

@@ -117,7 +117,10 @@ fn reuse_workproduct_for_cgu(
117117
}
118118
}
119119

120-
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult {
120+
fn module_codegen(
121+
tcx: TyCtxt<'_>,
122+
(backend_config, cgu_name): (BackendConfig, rustc_span::Symbol),
123+
) -> ModuleCodegenResult {
121124
let cgu = tcx.codegen_unit(cgu_name);
122125
let mono_items = cgu.items_in_deterministic_order(tcx);
123126

@@ -148,9 +151,9 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
148151

149152
let mut cx = crate::CodegenCx::new(
150153
tcx,
154+
backend_config,
151155
module,
152156
tcx.sess.opts.debuginfo != DebugInfo::None,
153-
true,
154157
);
155158
super::predefine_mono_items(&mut cx, &mono_items);
156159
for (mono_item, (linkage, visibility)) in mono_items {
@@ -202,6 +205,7 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
202205

203206
pub(super) fn run_aot(
204207
tcx: TyCtxt<'_>,
208+
backend_config: BackendConfig,
205209
metadata: EncodedMetadata,
206210
need_metadata_module: bool,
207211
) -> Box<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)> {
@@ -242,7 +246,7 @@ pub(super) fn run_aot(
242246
let (ModuleCodegenResult(module, work_product), _) = tcx.dep_graph.with_task(
243247
dep_node,
244248
tcx,
245-
cgu.name(),
249+
(backend_config, cgu.name()),
246250
module_codegen,
247251
rustc_middle::dep_graph::hash_result,
248252
);

src/driver/jit.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use rustc_middle::mir::mono::MonoItem;
1010

1111
use cranelift_jit::{JITBuilder, JITModule};
1212

13-
use crate::prelude::*;
13+
use crate::{prelude::*, BackendConfig};
1414
use crate::{CodegenCx, CodegenMode};
1515

1616
thread_local! {
17+
pub static BACKEND_CONFIG: RefCell<Option<BackendConfig>> = RefCell::new(None);
1718
pub static CURRENT_MODULE: RefCell<Option<JITModule>> = RefCell::new(None);
1819
}
1920

20-
pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
21+
pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
2122
if !tcx.sess.opts.output_types.should_codegen() {
2223
tcx.sess.fatal("JIT mode doesn't work with `cargo check`.");
2324
}
@@ -46,7 +47,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
4647
crate::build_isa(tcx.sess),
4748
cranelift_module::default_libcall_names(),
4849
);
49-
jit_builder.hotswap(matches!(codegen_mode, CodegenMode::JitLazy));
50+
jit_builder.hotswap(matches!(backend_config.codegen_mode, CodegenMode::JitLazy));
5051
jit_builder.symbols(imported_symbols);
5152
let mut jit_module = JITModule::new(jit_builder);
5253
assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type());
@@ -74,14 +75,14 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
7475
.into_iter()
7576
.collect::<Vec<(_, (_, _))>>();
7677

77-
let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false);
78+
let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false);
7879

7980
super::time(tcx, "codegen mono items", || {
8081
super::predefine_mono_items(&mut cx, &mono_items);
8182
for (mono_item, (linkage, visibility)) in mono_items {
8283
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
8384
match mono_item {
84-
MonoItem::Fn(inst) => match codegen_mode {
85+
MonoItem::Fn(inst) => match backend_config.codegen_mode {
8586
CodegenMode::Aot => unreachable!(),
8687
CodegenMode::Jit => {
8788
cx.tcx.sess.time("codegen fn", || {
@@ -137,6 +138,12 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
137138
// useful as some dynamic linkers use it as a marker to jump over.
138139
argv.push(std::ptr::null());
139140

141+
BACKEND_CONFIG.with(|tls_backend_config| {
142+
assert!(tls_backend_config
143+
.borrow_mut()
144+
.replace(backend_config)
145+
.is_none())
146+
});
140147
CURRENT_MODULE
141148
.with(|current_module| assert!(current_module.borrow_mut().replace(jit_module).is_none()));
142149

@@ -154,7 +161,9 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8
154161
CURRENT_MODULE.with(|jit_module| {
155162
let mut jit_module = jit_module.borrow_mut();
156163
let jit_module = jit_module.as_mut().unwrap();
157-
let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false);
164+
let backend_config =
165+
BACKEND_CONFIG.with(|backend_config| backend_config.borrow().clone().unwrap());
166+
let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false);
158167

159168
let name = tcx.symbol_name(instance).name.to_string();
160169
let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), instance);

src/driver/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ pub(crate) fn codegen_crate(
1717
tcx: TyCtxt<'_>,
1818
metadata: EncodedMetadata,
1919
need_metadata_module: bool,
20-
config: crate::BackendConfig,
20+
backend_config: crate::BackendConfig,
2121
) -> Box<dyn Any> {
2222
tcx.sess.abort_if_errors();
2323

24-
match config.codegen_mode {
25-
CodegenMode::Aot => aot::run_aot(tcx, metadata, need_metadata_module),
24+
match backend_config.codegen_mode {
25+
CodegenMode::Aot => aot::run_aot(tcx, backend_config, metadata, need_metadata_module),
2626
CodegenMode::Jit | CodegenMode::JitLazy => {
2727
let is_executable = tcx
2828
.sess
@@ -33,7 +33,7 @@ pub(crate) fn codegen_crate(
3333
}
3434

3535
#[cfg(feature = "jit")]
36-
let _: ! = jit::run_jit(tcx, config.codegen_mode);
36+
let _: ! = jit::run_jit(tcx, backend_config);
3737

3838
#[cfg(not(feature = "jit"))]
3939
tcx.sess

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ struct CodegenCx<'tcx, M: Module> {
142142
}
143143

144144
impl<'tcx, M: Module> CodegenCx<'tcx, M> {
145-
fn new(tcx: TyCtxt<'tcx>, module: M, debug_info: bool, pic_eh_frame: bool) -> Self {
146-
let unwind_context = UnwindContext::new(tcx, module.isa(), pic_eh_frame);
145+
fn new(tcx: TyCtxt<'tcx>, backend_config: BackendConfig, module: M, debug_info: bool) -> Self {
146+
let unwind_context = UnwindContext::new(
147+
tcx,
148+
module.isa(),
149+
matches!(backend_config.codegen_mode, CodegenMode::Aot),
150+
);
147151
let debug_context = if debug_info {
148152
Some(DebugContext::new(tcx, module.isa()))
149153
} else {

0 commit comments

Comments
 (0)