diff --git a/src/back/lto.rs b/src/back/lto.rs index 5b6eec855503..4b31bfac5dd7 100644 --- a/src/back/lto.rs +++ b/src/back/lto.rs @@ -39,7 +39,7 @@ use tempfile::{tempdir, TempDir}; use crate::back::write::save_temp_bitcode; use crate::errors::{DynamicLinkingWithLTO, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib}; -use crate::{to_gcc_opt_level, GccCodegenBackend, GccContext}; +use crate::{to_gcc_opt_level, GccCodegenBackend, GccContext, SyncContext}; /// We keep track of the computed LTO cache keys from the previous /// session to determine which CGUs we can reuse. @@ -485,9 +485,9 @@ fn thin_lto( });*/ match module { - SerializedModule::Local(ref module_buffer) => { - let path = module_buffer.0.to_str().expect("path"); - let my_path = PathBuf::from(path); + SerializedModule::Local(_) => { + //let path = module_buffer.0.to_str().expect("path"); + //let my_path = PathBuf::from(path); //let exists = my_path.exists(); //println!("Path: {:?}: {}", path, exists); /*module.module_llvm.should_combine_object_files = true; @@ -644,7 +644,7 @@ pub unsafe fn optimize_thin_module( unimplemented!("from uncompressed file") } } - Arc::new(context) + Arc::new(SyncContext::new(context)) } }; let module = ModuleCodegen { @@ -718,7 +718,7 @@ pub unsafe fn optimize_thin_module( } pub struct ThinBuffer { - context: Arc>, + context: Arc, } // TODO: check if this makes sense to make ThinBuffer Send and Sync. @@ -726,7 +726,7 @@ unsafe impl Send for ThinBuffer {} unsafe impl Sync for ThinBuffer {} impl ThinBuffer { - pub fn new(context: &Arc>) -> Self { + pub(crate) fn new(context: &Arc) -> Self { Self { context: Arc::clone(context) } } } diff --git a/src/base.rs b/src/base.rs index b2cf6fe51df8..488757c44849 100644 --- a/src/base.rs +++ b/src/base.rs @@ -19,8 +19,8 @@ use rustc_target::spec::PanicStrategy; use crate::builder::Builder; use crate::context::CodegenCx; -use crate::GccContext; use crate::{gcc_util, new_context, LockedTargetInfo}; +use crate::{GccContext, SyncContext}; #[cfg(feature = "master")] pub fn visibility_to_gcc(linkage: Visibility) -> gccjit::Visibility { @@ -207,7 +207,7 @@ pub fn compile_codegen_unit( ModuleCodegen { name: cgu_name.to_string(), module_llvm: GccContext { - context: Arc::new(context), + context: Arc::new(SyncContext::new(context)), should_combine_object_files: false, temp_dir: None, }, diff --git a/src/lib.rs b/src/lib.rs index 3df48f96e8c4..ce781b3f3ff7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,6 +73,7 @@ mod type_of; use std::any::Any; use std::fmt::Debug; +use std::ops::Deref; #[cfg(not(feature = "master"))] use std::sync::atomic::AtomicBool; #[cfg(not(feature = "master"))] @@ -294,7 +295,7 @@ impl ExtraBackendMethods for GccCodegenBackend { alloc_error_handler_kind: AllocatorKind, ) -> Self::Module { let mut mods = GccContext { - context: Arc::new(new_context(tcx)), + context: Arc::new(SyncContext::new(new_context(tcx))), should_combine_object_files: false, temp_dir: None, }; @@ -325,15 +326,34 @@ impl ExtraBackendMethods for GccCodegenBackend { } pub struct GccContext { - context: Arc>, + context: Arc, should_combine_object_files: bool, // Temporary directory used by LTO. We keep it here so that it's not removed before linking. temp_dir: Option, } -unsafe impl Send for GccContext {} -// FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "-Zno-parallel-llvm". Try to disable it here. -unsafe impl Sync for GccContext {} +struct SyncContext { + context: Context<'static>, +} + +impl SyncContext { + fn new(context: Context<'static>) -> Self { + Self { context } + } +} + +impl Deref for SyncContext { + type Target = Context<'static>; + + fn deref(&self) -> &Self::Target { + &self.context + } +} + +unsafe impl Send for SyncContext {} +// FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "-Zno-parallel-llvm". +// TODO: disable it here by returing false in CodegenBackend::supports_parallel(). +unsafe impl Sync for SyncContext {} impl WriteBackendMethods for GccCodegenBackend { type Module = GccContext;