2020-09-23 08:13:49 -05:00
|
|
|
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
|
|
|
|
//! standalone executable.
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
use std::fs::File;
|
2020-07-09 09:55:38 -05:00
|
|
|
use std::path::PathBuf;
|
2022-08-24 11:40:58 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread::JoinHandle;
|
2020-07-09 09:55:38 -05:00
|
|
|
|
2023-10-09 03:52:46 -05:00
|
|
|
use cranelift_object::{ObjectBuilder, ObjectModule};
|
2021-12-20 11:56:35 -06:00
|
|
|
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
|
2023-09-16 09:36:23 -05:00
|
|
|
use rustc_codegen_ssa::base::determine_cgu_reuse;
|
2020-08-28 05:10:48 -05:00
|
|
|
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
|
2022-08-24 11:40:58 -05:00
|
|
|
use rustc_data_structures::profiling::SelfProfilerRef;
|
2020-08-28 05:10:48 -05:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2021-09-24 11:15:36 -05:00
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-05-09 07:14:45 -05:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2020-12-27 03:30:38 -06:00
|
|
|
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
|
2020-03-12 05:48:17 -05:00
|
|
|
use rustc_session::cgu_reuse_tracker::CguReuse;
|
2022-08-24 11:40:58 -05:00
|
|
|
use rustc_session::config::{DebugInfo, OutputFilenames, OutputType};
|
2021-09-19 06:56:58 -05:00
|
|
|
use rustc_session::Session;
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
|
|
|
|
use crate::global_asm::GlobalAsmConfig;
|
2021-03-05 12:12:59 -06:00
|
|
|
use crate::{prelude::*, BackendConfig};
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
struct ModuleCodegenResult {
|
|
|
|
module_regular: CompiledModule,
|
|
|
|
module_global_asm: Option<CompiledModule>,
|
|
|
|
existing_work_product: Option<(WorkProductId, WorkProduct)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum OngoingModuleCodegen {
|
|
|
|
Sync(Result<ModuleCodegenResult, String>),
|
|
|
|
Async(JoinHandle<Result<ModuleCodegenResult, String>>),
|
|
|
|
}
|
2020-03-12 05:58:59 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
impl<HCX> HashStable<HCX> for OngoingModuleCodegen {
|
2020-03-12 05:58:59 -05:00
|
|
|
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
pub(crate) struct OngoingCodegen {
|
|
|
|
modules: Vec<OngoingModuleCodegen>,
|
|
|
|
allocator_module: Option<CompiledModule>,
|
|
|
|
metadata_module: Option<CompiledModule>,
|
|
|
|
metadata: EncodedMetadata,
|
|
|
|
crate_info: CrateInfo,
|
|
|
|
concurrency_limiter: ConcurrencyLimiter,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OngoingCodegen {
|
|
|
|
pub(crate) fn join(
|
|
|
|
self,
|
|
|
|
sess: &Session,
|
|
|
|
backend_config: &BackendConfig,
|
2023-04-07 14:56:33 -05:00
|
|
|
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
|
|
|
|
let mut work_products = FxIndexMap::default();
|
2022-08-24 11:40:58 -05:00
|
|
|
let mut modules = vec![];
|
|
|
|
|
|
|
|
for module_codegen in self.modules {
|
|
|
|
let module_codegen_result = match module_codegen {
|
|
|
|
OngoingModuleCodegen::Sync(module_codegen_result) => module_codegen_result,
|
|
|
|
OngoingModuleCodegen::Async(join_handle) => match join_handle.join() {
|
|
|
|
Ok(module_codegen_result) => module_codegen_result,
|
|
|
|
Err(panic) => std::panic::resume_unwind(panic),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let module_codegen_result = match module_codegen_result {
|
|
|
|
Ok(module_codegen_result) => module_codegen_result,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-19 22:26:58 -05:00
|
|
|
Err(err) => sess.fatal(err),
|
2022-08-24 11:40:58 -05:00
|
|
|
};
|
|
|
|
let ModuleCodegenResult { module_regular, module_global_asm, existing_work_product } =
|
|
|
|
module_codegen_result;
|
|
|
|
|
|
|
|
if let Some((work_product_id, work_product)) = existing_work_product {
|
|
|
|
work_products.insert(work_product_id, work_product);
|
|
|
|
} else {
|
|
|
|
let work_product = if backend_config.disable_incr_cache {
|
|
|
|
None
|
|
|
|
} else if let Some(module_global_asm) = &module_global_asm {
|
|
|
|
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
|
|
|
|
sess,
|
|
|
|
&module_regular.name,
|
|
|
|
&[
|
|
|
|
("o", &module_regular.object.as_ref().unwrap()),
|
|
|
|
("asm.o", &module_global_asm.object.as_ref().unwrap()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
|
|
|
|
sess,
|
|
|
|
&module_regular.name,
|
|
|
|
&[("o", &module_regular.object.as_ref().unwrap())],
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if let Some((work_product_id, work_product)) = work_product {
|
|
|
|
work_products.insert(work_product_id, work_product);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modules.push(module_regular);
|
|
|
|
if let Some(module_global_asm) = module_global_asm {
|
|
|
|
modules.push(module_global_asm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
self.concurrency_limiter.finished();
|
2022-08-24 11:40:58 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
sess.abort_if_errors();
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
(
|
|
|
|
CodegenResults {
|
|
|
|
modules,
|
|
|
|
allocator_module: self.allocator_module,
|
|
|
|
metadata_module: self.metadata_module,
|
|
|
|
metadata: self.metadata,
|
|
|
|
crate_info: self.crate_info,
|
|
|
|
},
|
|
|
|
work_products,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) -> ObjectModule {
|
|
|
|
let isa = crate::build_isa(sess, backend_config);
|
|
|
|
|
2021-09-19 06:56:58 -05:00
|
|
|
let mut builder =
|
|
|
|
ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap();
|
|
|
|
// Unlike cg_llvm, cg_clif defaults to disabling -Zfunction-sections. For cg_llvm binary size
|
|
|
|
// is important, while cg_clif cares more about compilation times. Enabling -Zfunction-sections
|
|
|
|
// can easily double the amount of time necessary to perform linking.
|
2022-07-06 07:44:47 -05:00
|
|
|
builder.per_function_section(sess.opts.unstable_opts.function_sections.unwrap_or(false));
|
2021-09-19 06:56:58 -05:00
|
|
|
ObjectModule::new(builder)
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
fn emit_cgu(
|
|
|
|
output_filenames: &OutputFilenames,
|
|
|
|
prof: &SelfProfilerRef,
|
2020-03-12 05:58:59 -05:00
|
|
|
name: String,
|
2020-10-01 03:38:23 -05:00
|
|
|
module: ObjectModule,
|
2022-08-24 11:40:58 -05:00
|
|
|
debug: Option<DebugContext>,
|
2021-04-30 07:49:58 -05:00
|
|
|
unwind_context: UnwindContext,
|
2022-08-24 11:40:58 -05:00
|
|
|
global_asm_object_file: Option<PathBuf>,
|
|
|
|
) -> Result<ModuleCodegenResult, String> {
|
2020-03-12 05:58:59 -05:00
|
|
|
let mut product = module.finish();
|
|
|
|
|
|
|
|
if let Some(mut debug) = debug {
|
|
|
|
debug.emit(&mut product);
|
|
|
|
}
|
|
|
|
|
2020-05-01 12:21:29 -05:00
|
|
|
unwind_context.emit(&mut product);
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
let module_regular =
|
|
|
|
emit_module(output_filenames, prof, product.object, ModuleKind::Regular, name.clone())?;
|
|
|
|
|
|
|
|
Ok(ModuleCodegenResult {
|
|
|
|
module_regular,
|
|
|
|
module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
|
|
|
|
name: format!("{name}.asm"),
|
|
|
|
kind: ModuleKind::Regular,
|
|
|
|
object: Some(global_asm_object_file),
|
|
|
|
dwarf_object: None,
|
|
|
|
bytecode: None,
|
|
|
|
}),
|
|
|
|
existing_work_product: None,
|
|
|
|
})
|
|
|
|
}
|
2022-02-23 04:49:34 -06:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
fn emit_module(
|
|
|
|
output_filenames: &OutputFilenames,
|
|
|
|
prof: &SelfProfilerRef,
|
2023-01-24 11:56:42 -06:00
|
|
|
mut object: cranelift_object::object::write::Object<'_>,
|
2022-08-24 11:40:58 -05:00
|
|
|
kind: ModuleKind,
|
|
|
|
name: String,
|
|
|
|
) -> Result<CompiledModule, String> {
|
2023-01-24 11:56:42 -06:00
|
|
|
if object.format() == cranelift_object::object::BinaryFormat::Elf {
|
|
|
|
let comment_section = object.add_section(
|
|
|
|
Vec::new(),
|
|
|
|
b".comment".to_vec(),
|
|
|
|
cranelift_object::object::SectionKind::OtherString,
|
|
|
|
);
|
|
|
|
let mut producer = vec![0];
|
|
|
|
producer.extend(crate::debuginfo::producer().as_bytes());
|
|
|
|
producer.push(0);
|
|
|
|
object.set_section_data(comment_section, producer, 1);
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
let tmp_file = output_filenames.temp_path(OutputType::Object, Some(&name));
|
|
|
|
let mut file = match File::create(&tmp_file) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => return Err(format!("error creating object file: {}", err)),
|
|
|
|
};
|
2022-02-23 04:49:34 -06:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
if let Err(err) = object.write_stream(&mut file) {
|
|
|
|
return Err(format!("error writing object file: {}", err));
|
2020-08-08 09:14:11 -05:00
|
|
|
}
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
prof.artifact_size("object_file", &*name, file.metadata().unwrap().len());
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
Ok(CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None })
|
2020-03-12 05:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reuse_workproduct_for_cgu(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-04-05 06:48:26 -05:00
|
|
|
cgu: &CodegenUnit<'_>,
|
2022-08-24 11:40:58 -05:00
|
|
|
) -> Result<ModuleCodegenResult, String> {
|
2022-05-13 05:32:03 -05:00
|
|
|
let work_product = cgu.previous_work_product(tcx);
|
2022-08-24 11:40:58 -05:00
|
|
|
let obj_out_regular =
|
|
|
|
tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
|
|
|
|
let source_file_regular = rustc_incremental::in_incr_comp_dir_sess(
|
2022-07-04 08:38:42 -05:00
|
|
|
&tcx.sess,
|
|
|
|
&work_product.saved_files.get("o").expect("no saved object file in work product"),
|
|
|
|
);
|
2022-08-24 11:40:58 -05:00
|
|
|
|
|
|
|
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_regular, &obj_out_regular) {
|
|
|
|
return Err(format!(
|
2022-05-15 06:31:28 -05:00
|
|
|
"unable to copy {} to {}: {}",
|
2022-08-24 11:40:58 -05:00
|
|
|
source_file_regular.display(),
|
|
|
|
obj_out_regular.display(),
|
2022-05-15 06:31:28 -05:00
|
|
|
err
|
|
|
|
));
|
2020-03-12 05:58:59 -05:00
|
|
|
}
|
2022-08-24 11:40:58 -05:00
|
|
|
let obj_out_global_asm =
|
|
|
|
crate::global_asm::add_file_stem_postfix(obj_out_regular.clone(), ".asm");
|
|
|
|
let has_global_asm = if let Some(asm_o) = work_product.saved_files.get("asm.o") {
|
|
|
|
let source_file_global_asm = rustc_incremental::in_incr_comp_dir_sess(&tcx.sess, asm_o);
|
|
|
|
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_global_asm, &obj_out_global_asm)
|
|
|
|
{
|
|
|
|
return Err(format!(
|
|
|
|
"unable to copy {} to {}: {}",
|
|
|
|
source_file_regular.display(),
|
|
|
|
obj_out_regular.display(),
|
|
|
|
err
|
|
|
|
));
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
Ok(ModuleCodegenResult {
|
|
|
|
module_regular: CompiledModule {
|
|
|
|
name: cgu.name().to_string(),
|
|
|
|
kind: ModuleKind::Regular,
|
|
|
|
object: Some(obj_out_regular),
|
|
|
|
dwarf_object: None,
|
|
|
|
bytecode: None,
|
|
|
|
},
|
2023-02-15 05:43:41 -06:00
|
|
|
module_global_asm: has_global_asm.then(|| CompiledModule {
|
|
|
|
name: cgu.name().to_string(),
|
|
|
|
kind: ModuleKind::Regular,
|
|
|
|
object: Some(obj_out_global_asm),
|
|
|
|
dwarf_object: None,
|
|
|
|
bytecode: None,
|
|
|
|
}),
|
2022-08-24 11:40:58 -05:00
|
|
|
existing_work_product: Some((cgu.work_product_id(), work_product)),
|
|
|
|
})
|
2020-03-12 05:58:59 -05:00
|
|
|
}
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2021-03-05 12:12:59 -06:00
|
|
|
fn module_codegen(
|
|
|
|
tcx: TyCtxt<'_>,
|
2022-08-24 11:40:58 -05:00
|
|
|
(backend_config, global_asm_config, cgu_name, token): (
|
|
|
|
BackendConfig,
|
|
|
|
Arc<GlobalAsmConfig>,
|
|
|
|
rustc_span::Symbol,
|
|
|
|
ConcurrencyLimiterToken,
|
|
|
|
),
|
|
|
|
) -> OngoingModuleCodegen {
|
2023-02-09 05:38:16 -06:00
|
|
|
let (cgu_name, mut cx, mut module, codegened_functions) =
|
2023-09-10 06:15:46 -05:00
|
|
|
tcx.prof.generic_activity_with_arg("codegen cgu", cgu_name.as_str()).run(|| {
|
2023-02-09 05:38:16 -06:00
|
|
|
let cgu = tcx.codegen_unit(cgu_name);
|
|
|
|
let mono_items = cgu.items_in_deterministic_order(tcx);
|
|
|
|
|
|
|
|
let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string());
|
|
|
|
|
|
|
|
let mut cx = crate::CodegenCx::new(
|
|
|
|
tcx,
|
|
|
|
backend_config.clone(),
|
|
|
|
module.isa(),
|
|
|
|
tcx.sess.opts.debuginfo != DebugInfo::None,
|
|
|
|
cgu_name,
|
|
|
|
);
|
|
|
|
super::predefine_mono_items(tcx, &mut module, &mono_items);
|
|
|
|
let mut codegened_functions = vec![];
|
|
|
|
for (mono_item, _) in mono_items {
|
|
|
|
match mono_item {
|
|
|
|
MonoItem::Fn(inst) => {
|
2022-08-24 11:40:58 -05:00
|
|
|
let codegened_function = crate::base::codegen_fn(
|
|
|
|
tcx,
|
|
|
|
&mut cx,
|
|
|
|
Function::new(),
|
|
|
|
&mut module,
|
|
|
|
inst,
|
|
|
|
);
|
|
|
|
codegened_functions.push(codegened_function);
|
2023-02-09 05:38:16 -06:00
|
|
|
}
|
|
|
|
MonoItem::Static(def_id) => {
|
|
|
|
crate::constant::codegen_static(tcx, &mut module, def_id)
|
|
|
|
}
|
|
|
|
MonoItem::GlobalAsm(item_id) => {
|
|
|
|
crate::global_asm::codegen_global_asm_item(
|
|
|
|
tcx,
|
|
|
|
&mut cx.global_asm,
|
|
|
|
item_id,
|
|
|
|
);
|
|
|
|
}
|
2020-12-27 03:30:38 -06:00
|
|
|
}
|
|
|
|
}
|
2023-02-09 05:38:16 -06:00
|
|
|
crate::main_shim::maybe_create_entry_wrapper(
|
|
|
|
tcx,
|
|
|
|
&mut module,
|
|
|
|
&mut cx.unwind_context,
|
|
|
|
false,
|
|
|
|
cgu.is_primary(),
|
|
|
|
);
|
2022-08-24 11:40:58 -05:00
|
|
|
|
2023-02-09 05:38:16 -06:00
|
|
|
let cgu_name = cgu.name().as_str().to_owned();
|
2022-08-24 11:40:58 -05:00
|
|
|
|
2023-02-09 05:38:16 -06:00
|
|
|
(cgu_name, cx, module, codegened_functions)
|
|
|
|
});
|
2020-07-09 09:55:38 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
OngoingModuleCodegen::Async(std::thread::spawn(move || {
|
2023-09-10 06:15:46 -05:00
|
|
|
cx.profiler.clone().generic_activity_with_arg("compile functions", &*cgu_name).run(|| {
|
|
|
|
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
|
|
|
|
cx.profiler.clone(),
|
|
|
|
)));
|
|
|
|
|
|
|
|
let mut cached_context = Context::new();
|
|
|
|
for codegened_func in codegened_functions {
|
|
|
|
crate::base::compile_fn(&mut cx, &mut cached_context, &mut module, codegened_func);
|
|
|
|
}
|
|
|
|
});
|
2020-07-09 09:55:38 -05:00
|
|
|
|
2023-09-10 06:15:46 -05:00
|
|
|
let global_asm_object_file =
|
|
|
|
cx.profiler.generic_activity_with_arg("compile assembly", &*cgu_name).run(|| {
|
2022-08-24 11:40:58 -05:00
|
|
|
crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
|
|
|
|
})?;
|
|
|
|
|
2023-09-10 06:15:46 -05:00
|
|
|
let codegen_result =
|
|
|
|
cx.profiler.generic_activity_with_arg("write object file", &*cgu_name).run(|| {
|
2023-02-09 05:38:16 -06:00
|
|
|
emit_cgu(
|
|
|
|
&global_asm_config.output_filenames,
|
|
|
|
&cx.profiler,
|
|
|
|
cgu_name,
|
|
|
|
module,
|
|
|
|
cx.debug_context,
|
|
|
|
cx.unwind_context,
|
|
|
|
global_asm_object_file,
|
|
|
|
)
|
|
|
|
});
|
2022-08-24 11:40:58 -05:00
|
|
|
std::mem::drop(token);
|
|
|
|
codegen_result
|
|
|
|
}))
|
2020-03-12 05:58:59 -05:00
|
|
|
}
|
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
pub(crate) fn run_aot(
|
2020-03-12 05:58:59 -05:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-03-05 12:12:59 -06:00
|
|
|
backend_config: BackendConfig,
|
2020-03-12 05:58:59 -05:00
|
|
|
metadata: EncodedMetadata,
|
|
|
|
need_metadata_module: bool,
|
2022-08-24 11:40:58 -05:00
|
|
|
) -> Box<OngoingCodegen> {
|
2020-04-18 14:13:09 -05:00
|
|
|
let cgus = if tcx.sess.opts.output_types.should_codegen() {
|
2021-05-11 07:39:04 -05:00
|
|
|
tcx.collect_and_partition_mono_items(()).1
|
2020-04-18 14:13:09 -05:00
|
|
|
} else {
|
|
|
|
// If only `--emit metadata` is used, we shouldn't perform any codegen.
|
|
|
|
// Also `tcx.collect_and_partition_mono_items` may panic in that case.
|
|
|
|
&[]
|
|
|
|
};
|
2020-03-12 05:48:17 -05:00
|
|
|
|
|
|
|
if tcx.dep_graph.is_fully_enabled() {
|
2023-03-15 09:41:48 -05:00
|
|
|
for cgu in cgus {
|
2020-05-09 07:14:45 -05:00
|
|
|
tcx.ensure().codegen_unit(cgu.name());
|
2020-03-12 05:48:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
|
|
|
|
|
|
|
|
let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
|
|
|
|
|
2023-02-09 05:38:16 -06:00
|
|
|
let modules = tcx.sess.time("codegen mono items", || {
|
2020-08-28 05:10:48 -05:00
|
|
|
cgus.iter()
|
|
|
|
.map(|cgu| {
|
2022-08-24 11:40:58 -05:00
|
|
|
let cgu_reuse = if backend_config.disable_incr_cache {
|
|
|
|
CguReuse::No
|
|
|
|
} else {
|
|
|
|
determine_cgu_reuse(tcx, cgu)
|
|
|
|
};
|
2021-12-14 21:39:23 -06:00
|
|
|
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
|
2020-08-28 05:10:48 -05:00
|
|
|
|
|
|
|
match cgu_reuse {
|
2022-08-24 11:40:58 -05:00
|
|
|
CguReuse::No => {
|
|
|
|
let dep_node = cgu.codegen_dep_node(tcx);
|
|
|
|
tcx.dep_graph
|
|
|
|
.with_task(
|
|
|
|
dep_node,
|
|
|
|
tcx,
|
|
|
|
(
|
|
|
|
backend_config.clone(),
|
|
|
|
global_asm_config.clone(),
|
|
|
|
cgu.name(),
|
2023-04-29 07:00:43 -05:00
|
|
|
concurrency_limiter.acquire(tcx.sess.diagnostic()),
|
2022-08-24 11:40:58 -05:00
|
|
|
),
|
|
|
|
module_codegen,
|
|
|
|
Some(rustc_middle::dep_graph::hash_result),
|
|
|
|
)
|
|
|
|
.0
|
|
|
|
}
|
|
|
|
CguReuse::PreLto => unreachable!(),
|
|
|
|
CguReuse::PostLto => {
|
|
|
|
concurrency_limiter.job_already_done();
|
2023-03-15 09:41:48 -05:00
|
|
|
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
|
2020-08-28 05:10:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2020-03-12 05:48:17 -05:00
|
|
|
});
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
|
2021-12-30 07:53:41 -06:00
|
|
|
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
|
2020-08-28 05:10:48 -05:00
|
|
|
let created_alloc_shim =
|
|
|
|
crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
|
2020-03-12 05:48:17 -05:00
|
|
|
|
|
|
|
let allocator_module = if created_alloc_shim {
|
2022-08-24 11:40:58 -05:00
|
|
|
let mut product = allocator_module.finish();
|
|
|
|
allocator_unwind_context.emit(&mut product);
|
|
|
|
|
|
|
|
match emit_module(
|
|
|
|
tcx.output_filenames(()),
|
|
|
|
&tcx.sess.prof,
|
|
|
|
product.object,
|
2020-03-12 05:48:17 -05:00
|
|
|
ModuleKind::Allocator,
|
2022-08-24 11:40:58 -05:00
|
|
|
"allocator_shim".to_owned(),
|
|
|
|
) {
|
|
|
|
Ok(allocator_module) => Some(allocator_module),
|
|
|
|
Err(err) => tcx.sess.fatal(err),
|
2020-03-12 05:48:17 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let metadata_module = if need_metadata_module {
|
|
|
|
let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
|
2020-03-31 06:20:19 -05:00
|
|
|
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
|
2020-03-12 05:48:17 -05:00
|
|
|
|
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
|
|
|
|
let metadata_cgu_name = cgu_name_builder
|
|
|
|
.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
|
|
|
|
.as_str()
|
|
|
|
.to_string();
|
|
|
|
|
2021-05-27 06:08:14 -05:00
|
|
|
let tmp_file =
|
|
|
|
tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2021-12-20 11:56:35 -06:00
|
|
|
let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
|
|
|
|
let obj = create_compressed_metadata_file(tcx.sess, &metadata, &symbol_name);
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2020-08-08 09:14:11 -05:00
|
|
|
if let Err(err) = std::fs::write(&tmp_file, obj) {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-19 22:26:58 -05:00
|
|
|
tcx.sess.fatal(format!("error writing metadata object file: {}", err));
|
2020-08-08 09:14:11 -05:00
|
|
|
}
|
2020-03-12 05:48:17 -05:00
|
|
|
|
|
|
|
(metadata_cgu_name, tmp_file)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(CompiledModule {
|
|
|
|
name: metadata_cgu_name,
|
|
|
|
kind: ModuleKind::Metadata,
|
|
|
|
object: Some(tmp_file),
|
2020-11-08 08:01:23 -06:00
|
|
|
dwarf_object: None,
|
2020-03-12 05:48:17 -05:00
|
|
|
bytecode: None,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-07-07 04:14:20 -05:00
|
|
|
// FIXME handle `-Ctarget-cpu=native`
|
2022-04-03 11:42:39 -05:00
|
|
|
let target_cpu = match tcx.sess.opts.cg.target_cpu {
|
|
|
|
Some(ref name) => name,
|
|
|
|
None => tcx.sess.target.cpu.as_ref(),
|
|
|
|
}
|
|
|
|
.to_owned();
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
Box::new(OngoingCodegen {
|
|
|
|
modules,
|
|
|
|
allocator_module,
|
|
|
|
metadata_module,
|
|
|
|
metadata,
|
|
|
|
crate_info: CrateInfo::new(tcx, target_cpu),
|
|
|
|
concurrency_limiter,
|
|
|
|
})
|
2020-07-09 09:55:38 -05:00
|
|
|
}
|