Skip to content

Commit c2f0b3a

Browse files
committed
Move copy to incr comp cache to codegen join phase
The copy depends on Session, which is only available on the main thread. As such the copy can't be done on future codegen threads.
1 parent 6206c4e commit c2f0b3a

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

src/driver/aot.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{prelude::*, BackendConfig};
2323
struct ModuleCodegenResult {
2424
module_regular: CompiledModule,
2525
module_global_asm: Option<CompiledModule>,
26-
work_product: Option<(WorkProductId, WorkProduct)>,
26+
existing_work_product: Option<(WorkProductId, WorkProduct)>,
2727
}
2828

2929
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
@@ -41,16 +41,44 @@ pub(crate) struct OngoingCodegen {
4141
}
4242

4343
impl OngoingCodegen {
44-
pub(crate) fn join(self) -> (CodegenResults, FxHashMap<WorkProductId, WorkProduct>) {
44+
pub(crate) fn join(
45+
self,
46+
sess: &Session,
47+
backend_config: &BackendConfig,
48+
) -> (CodegenResults, FxHashMap<WorkProductId, WorkProduct>) {
4549
let mut work_products = FxHashMap::default();
4650
let mut modules = vec![];
4751

4852
for module_codegen_result in self.modules {
49-
let ModuleCodegenResult { module_regular, module_global_asm, work_product } =
53+
let ModuleCodegenResult { module_regular, module_global_asm, existing_work_product } =
5054
module_codegen_result;
51-
if let Some((work_product_id, work_product)) = work_product {
55+
56+
if let Some((work_product_id, work_product)) = existing_work_product {
5257
work_products.insert(work_product_id, work_product);
58+
} else {
59+
let work_product = if backend_config.disable_incr_cache {
60+
None
61+
} else if let Some(module_global_asm) = &module_global_asm {
62+
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
63+
sess,
64+
&module_regular.name,
65+
&[
66+
("o", &module_regular.object.as_ref().unwrap()),
67+
("asm.o", &module_global_asm.object.as_ref().unwrap()),
68+
],
69+
)
70+
} else {
71+
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
72+
sess,
73+
&module_regular.name,
74+
&[("o", &module_regular.object.as_ref().unwrap())],
75+
)
76+
};
77+
if let Some((work_product_id, work_product)) = work_product {
78+
work_products.insert(work_product_id, work_product);
79+
}
5380
}
81+
5482
modules.push(module_regular);
5583
if let Some(module_global_asm) = module_global_asm {
5684
modules.push(module_global_asm);
@@ -84,7 +112,6 @@ fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) ->
84112

85113
fn emit_cgu(
86114
tcx: TyCtxt<'_>,
87-
backend_config: &BackendConfig,
88115
name: String,
89116
module: ObjectModule,
90117
debug: Option<DebugContext<'_>>,
@@ -101,22 +128,6 @@ fn emit_cgu(
101128

102129
let module_regular = emit_module(tcx, product.object, ModuleKind::Regular, name.clone());
103130

104-
let work_product = if backend_config.disable_incr_cache {
105-
None
106-
} else if let Some(global_asm_object_file) = &global_asm_object_file {
107-
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
108-
tcx.sess,
109-
&name,
110-
&[("o", &module_regular.object.as_ref().unwrap()), ("asm.o", global_asm_object_file)],
111-
)
112-
} else {
113-
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
114-
tcx.sess,
115-
&name,
116-
&[("o", &module_regular.object.as_ref().unwrap())],
117-
)
118-
};
119-
120131
ModuleCodegenResult {
121132
module_regular,
122133
module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
@@ -126,7 +137,7 @@ fn emit_cgu(
126137
dwarf_object: None,
127138
bytecode: None,
128139
}),
129-
work_product,
140+
existing_work_product: None,
130141
}
131142
}
132143

@@ -205,7 +216,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
205216
} else {
206217
None
207218
},
208-
work_product: Some((cgu.work_product_id(), work_product)),
219+
existing_work_product: Some((cgu.work_product_id(), work_product)),
209220
}
210221
}
211222

@@ -271,7 +282,6 @@ fn module_codegen(
271282
let codegen_result = tcx.sess.time("write object file", || {
272283
emit_cgu(
273284
tcx,
274-
&backend_config,
275285
cgu.name().as_str().to_string(),
276286
module,
277287
debug_context,

src/lib.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern crate rustc_target;
2525
extern crate rustc_driver;
2626

2727
use std::any::Any;
28-
use std::cell::Cell;
28+
use std::cell::{Cell, RefCell};
2929

3030
use rustc_codegen_ssa::traits::CodegenBackend;
3131
use rustc_codegen_ssa::CodegenResults;
@@ -158,7 +158,7 @@ impl<'tcx> CodegenCx<'tcx> {
158158
}
159159

160160
pub struct CraneliftCodegenBackend {
161-
pub config: Option<BackendConfig>,
161+
pub config: RefCell<Option<BackendConfig>>,
162162
}
163163

164164
impl CodegenBackend for CraneliftCodegenBackend {
@@ -168,6 +168,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
168168
Lto::No | Lto::ThinLocal => {}
169169
Lto::Thin | Lto::Fat => sess.warn("LTO is not supported. You may get a linker error."),
170170
}
171+
172+
let mut config = self.config.borrow_mut();
173+
if config.is_none() {
174+
let new_config = BackendConfig::from_opts(&sess.opts.cg.llvm_args)
175+
.unwrap_or_else(|err| sess.fatal(&err));
176+
*config = Some(new_config);
177+
}
171178
}
172179

173180
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
@@ -185,15 +192,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
185192
need_metadata_module: bool,
186193
) -> Box<dyn Any> {
187194
tcx.sess.abort_if_errors();
188-
let config = if let Some(config) = self.config.clone() {
189-
config
190-
} else {
191-
if !tcx.sess.unstable_options() && !tcx.sess.opts.cg.llvm_args.is_empty() {
192-
tcx.sess.fatal("`-Z unstable-options` must be passed to allow configuring cg_clif");
193-
}
194-
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
195-
.unwrap_or_else(|err| tcx.sess.fatal(&err))
196-
};
195+
let config = self.config.borrow().clone().unwrap();
197196
match config.codegen_mode {
198197
CodegenMode::Aot => driver::aot::run_aot(tcx, config, metadata, need_metadata_module),
199198
CodegenMode::Jit | CodegenMode::JitLazy => {
@@ -209,10 +208,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
209208
fn join_codegen(
210209
&self,
211210
ongoing_codegen: Box<dyn Any>,
212-
_sess: &Session,
211+
sess: &Session,
213212
_outputs: &OutputFilenames,
214213
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
215-
Ok(ongoing_codegen.downcast::<driver::aot::OngoingCodegen>().unwrap().join())
214+
Ok(ongoing_codegen
215+
.downcast::<driver::aot::OngoingCodegen>()
216+
.unwrap()
217+
.join(sess, self.config.borrow().as_ref().unwrap()))
216218
}
217219

218220
fn link(
@@ -309,5 +311,5 @@ fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Box<dyn isa::Tar
309311
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
310312
#[no_mangle]
311313
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
312-
Box::new(CraneliftCodegenBackend { config: None })
314+
Box::new(CraneliftCodegenBackend { config: RefCell::new(None) })
313315
}

0 commit comments

Comments
 (0)