]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/driver/aot.rs
Auto merge of #106938 - GuillaumeGomez:normalize-projection-field-ty, r=oli-obk
[rust.git] / compiler / rustc_codegen_cranelift / src / driver / aot.rs
1 //! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
2 //! standalone executable.
3
4 use std::fs::File;
5 use std::path::PathBuf;
6 use std::sync::Arc;
7 use std::thread::JoinHandle;
8
9 use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
10 use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
11 use rustc_data_structures::profiling::SelfProfilerRef;
12 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13 use rustc_metadata::EncodedMetadata;
14 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
15 use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
16 use rustc_session::cgu_reuse_tracker::CguReuse;
17 use rustc_session::config::{DebugInfo, OutputFilenames, OutputType};
18 use rustc_session::Session;
19
20 use cranelift_object::{ObjectBuilder, ObjectModule};
21
22 use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
23 use crate::global_asm::GlobalAsmConfig;
24 use crate::{prelude::*, BackendConfig};
25
26 struct ModuleCodegenResult {
27     module_regular: CompiledModule,
28     module_global_asm: Option<CompiledModule>,
29     existing_work_product: Option<(WorkProductId, WorkProduct)>,
30 }
31
32 enum OngoingModuleCodegen {
33     Sync(Result<ModuleCodegenResult, String>),
34     Async(JoinHandle<Result<ModuleCodegenResult, String>>),
35 }
36
37 impl<HCX> HashStable<HCX> for OngoingModuleCodegen {
38     fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
39         // do nothing
40     }
41 }
42
43 pub(crate) struct OngoingCodegen {
44     modules: Vec<OngoingModuleCodegen>,
45     allocator_module: Option<CompiledModule>,
46     metadata_module: Option<CompiledModule>,
47     metadata: EncodedMetadata,
48     crate_info: CrateInfo,
49     concurrency_limiter: ConcurrencyLimiter,
50 }
51
52 impl OngoingCodegen {
53     pub(crate) fn join(
54         self,
55         sess: &Session,
56         backend_config: &BackendConfig,
57     ) -> (CodegenResults, FxHashMap<WorkProductId, WorkProduct>) {
58         let mut work_products = FxHashMap::default();
59         let mut modules = vec![];
60
61         for module_codegen in self.modules {
62             let module_codegen_result = match module_codegen {
63                 OngoingModuleCodegen::Sync(module_codegen_result) => module_codegen_result,
64                 OngoingModuleCodegen::Async(join_handle) => match join_handle.join() {
65                     Ok(module_codegen_result) => module_codegen_result,
66                     Err(panic) => std::panic::resume_unwind(panic),
67                 },
68             };
69
70             let module_codegen_result = match module_codegen_result {
71                 Ok(module_codegen_result) => module_codegen_result,
72                 Err(err) => sess.fatal(&err),
73             };
74             let ModuleCodegenResult { module_regular, module_global_asm, existing_work_product } =
75                 module_codegen_result;
76
77             if let Some((work_product_id, work_product)) = existing_work_product {
78                 work_products.insert(work_product_id, work_product);
79             } else {
80                 let work_product = if backend_config.disable_incr_cache {
81                     None
82                 } else if let Some(module_global_asm) = &module_global_asm {
83                     rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
84                         sess,
85                         &module_regular.name,
86                         &[
87                             ("o", &module_regular.object.as_ref().unwrap()),
88                             ("asm.o", &module_global_asm.object.as_ref().unwrap()),
89                         ],
90                     )
91                 } else {
92                     rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
93                         sess,
94                         &module_regular.name,
95                         &[("o", &module_regular.object.as_ref().unwrap())],
96                     )
97                 };
98                 if let Some((work_product_id, work_product)) = work_product {
99                     work_products.insert(work_product_id, work_product);
100                 }
101             }
102
103             modules.push(module_regular);
104             if let Some(module_global_asm) = module_global_asm {
105                 modules.push(module_global_asm);
106             }
107         }
108
109         self.concurrency_limiter.finished();
110
111         sess.abort_if_errors();
112
113         (
114             CodegenResults {
115                 modules,
116                 allocator_module: self.allocator_module,
117                 metadata_module: self.metadata_module,
118                 metadata: self.metadata,
119                 crate_info: self.crate_info,
120             },
121             work_products,
122         )
123     }
124 }
125
126 fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) -> ObjectModule {
127     let isa = crate::build_isa(sess, backend_config);
128
129     let mut builder =
130         ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap();
131     // Unlike cg_llvm, cg_clif defaults to disabling -Zfunction-sections. For cg_llvm binary size
132     // is important, while cg_clif cares more about compilation times. Enabling -Zfunction-sections
133     // can easily double the amount of time necessary to perform linking.
134     builder.per_function_section(sess.opts.unstable_opts.function_sections.unwrap_or(false));
135     ObjectModule::new(builder)
136 }
137
138 fn emit_cgu(
139     output_filenames: &OutputFilenames,
140     prof: &SelfProfilerRef,
141     name: String,
142     module: ObjectModule,
143     debug: Option<DebugContext>,
144     unwind_context: UnwindContext,
145     global_asm_object_file: Option<PathBuf>,
146 ) -> Result<ModuleCodegenResult, String> {
147     let mut product = module.finish();
148
149     if let Some(mut debug) = debug {
150         debug.emit(&mut product);
151     }
152
153     unwind_context.emit(&mut product);
154
155     let module_regular =
156         emit_module(output_filenames, prof, product.object, ModuleKind::Regular, name.clone())?;
157
158     Ok(ModuleCodegenResult {
159         module_regular,
160         module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
161             name: format!("{name}.asm"),
162             kind: ModuleKind::Regular,
163             object: Some(global_asm_object_file),
164             dwarf_object: None,
165             bytecode: None,
166         }),
167         existing_work_product: None,
168     })
169 }
170
171 fn emit_module(
172     output_filenames: &OutputFilenames,
173     prof: &SelfProfilerRef,
174     mut object: cranelift_object::object::write::Object<'_>,
175     kind: ModuleKind,
176     name: String,
177 ) -> Result<CompiledModule, String> {
178     if object.format() == cranelift_object::object::BinaryFormat::Elf {
179         let comment_section = object.add_section(
180             Vec::new(),
181             b".comment".to_vec(),
182             cranelift_object::object::SectionKind::OtherString,
183         );
184         let mut producer = vec![0];
185         producer.extend(crate::debuginfo::producer().as_bytes());
186         producer.push(0);
187         object.set_section_data(comment_section, producer, 1);
188     }
189
190     let tmp_file = output_filenames.temp_path(OutputType::Object, Some(&name));
191     let mut file = match File::create(&tmp_file) {
192         Ok(file) => file,
193         Err(err) => return Err(format!("error creating object file: {}", err)),
194     };
195
196     if let Err(err) = object.write_stream(&mut file) {
197         return Err(format!("error writing object file: {}", err));
198     }
199
200     prof.artifact_size("object_file", &*name, file.metadata().unwrap().len());
201
202     Ok(CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None })
203 }
204
205 fn reuse_workproduct_for_cgu(
206     tcx: TyCtxt<'_>,
207     cgu: &CodegenUnit<'_>,
208 ) -> Result<ModuleCodegenResult, String> {
209     let work_product = cgu.previous_work_product(tcx);
210     let obj_out_regular =
211         tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
212     let source_file_regular = rustc_incremental::in_incr_comp_dir_sess(
213         &tcx.sess,
214         &work_product.saved_files.get("o").expect("no saved object file in work product"),
215     );
216
217     if let Err(err) = rustc_fs_util::link_or_copy(&source_file_regular, &obj_out_regular) {
218         return Err(format!(
219             "unable to copy {} to {}: {}",
220             source_file_regular.display(),
221             obj_out_regular.display(),
222             err
223         ));
224     }
225     let obj_out_global_asm =
226         crate::global_asm::add_file_stem_postfix(obj_out_regular.clone(), ".asm");
227     let has_global_asm = if let Some(asm_o) = work_product.saved_files.get("asm.o") {
228         let source_file_global_asm = rustc_incremental::in_incr_comp_dir_sess(&tcx.sess, asm_o);
229         if let Err(err) = rustc_fs_util::link_or_copy(&source_file_global_asm, &obj_out_global_asm)
230         {
231             return Err(format!(
232                 "unable to copy {} to {}: {}",
233                 source_file_regular.display(),
234                 obj_out_regular.display(),
235                 err
236             ));
237         }
238         true
239     } else {
240         false
241     };
242
243     Ok(ModuleCodegenResult {
244         module_regular: CompiledModule {
245             name: cgu.name().to_string(),
246             kind: ModuleKind::Regular,
247             object: Some(obj_out_regular),
248             dwarf_object: None,
249             bytecode: None,
250         },
251         module_global_asm: if has_global_asm {
252             Some(CompiledModule {
253                 name: cgu.name().to_string(),
254                 kind: ModuleKind::Regular,
255                 object: Some(obj_out_global_asm),
256                 dwarf_object: None,
257                 bytecode: None,
258             })
259         } else {
260             None
261         },
262         existing_work_product: Some((cgu.work_product_id(), work_product)),
263     })
264 }
265
266 fn module_codegen(
267     tcx: TyCtxt<'_>,
268     (backend_config, global_asm_config, cgu_name, token): (
269         BackendConfig,
270         Arc<GlobalAsmConfig>,
271         rustc_span::Symbol,
272         ConcurrencyLimiterToken,
273     ),
274 ) -> OngoingModuleCodegen {
275     let (cgu_name, mut cx, mut module, codegened_functions) = tcx.sess.time("codegen cgu", || {
276         let cgu = tcx.codegen_unit(cgu_name);
277         let mono_items = cgu.items_in_deterministic_order(tcx);
278
279         let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string());
280
281         let mut cx = crate::CodegenCx::new(
282             tcx,
283             backend_config.clone(),
284             module.isa(),
285             tcx.sess.opts.debuginfo != DebugInfo::None,
286             cgu_name,
287         );
288         super::predefine_mono_items(tcx, &mut module, &mono_items);
289         let mut codegened_functions = vec![];
290         for (mono_item, _) in mono_items {
291             match mono_item {
292                 MonoItem::Fn(inst) => {
293                     tcx.sess.time("codegen fn", || {
294                         let codegened_function = crate::base::codegen_fn(
295                             tcx,
296                             &mut cx,
297                             Function::new(),
298                             &mut module,
299                             inst,
300                         );
301                         codegened_functions.push(codegened_function);
302                     });
303                 }
304                 MonoItem::Static(def_id) => {
305                     crate::constant::codegen_static(tcx, &mut module, def_id)
306                 }
307                 MonoItem::GlobalAsm(item_id) => {
308                     crate::global_asm::codegen_global_asm_item(tcx, &mut cx.global_asm, item_id);
309                 }
310             }
311         }
312         crate::main_shim::maybe_create_entry_wrapper(
313             tcx,
314             &mut module,
315             &mut cx.unwind_context,
316             false,
317             cgu.is_primary(),
318         );
319
320         let cgu_name = cgu.name().as_str().to_owned();
321
322         (cgu_name, cx, module, codegened_functions)
323     });
324
325     OngoingModuleCodegen::Async(std::thread::spawn(move || {
326         cx.profiler.clone().verbose_generic_activity("compile functions").run(|| {
327             let mut cached_context = Context::new();
328             for codegened_func in codegened_functions {
329                 crate::base::compile_fn(&mut cx, &mut cached_context, &mut module, codegened_func);
330             }
331         });
332
333         let global_asm_object_file =
334             cx.profiler.verbose_generic_activity("compile assembly").run(|| {
335                 crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
336             })?;
337
338         let codegen_result = cx.profiler.verbose_generic_activity("write object file").run(|| {
339             emit_cgu(
340                 &global_asm_config.output_filenames,
341                 &cx.profiler,
342                 cgu_name,
343                 module,
344                 cx.debug_context,
345                 cx.unwind_context,
346                 global_asm_object_file,
347             )
348         });
349         std::mem::drop(token);
350         codegen_result
351     }))
352 }
353
354 pub(crate) fn run_aot(
355     tcx: TyCtxt<'_>,
356     backend_config: BackendConfig,
357     metadata: EncodedMetadata,
358     need_metadata_module: bool,
359 ) -> Box<OngoingCodegen> {
360     let cgus = if tcx.sess.opts.output_types.should_codegen() {
361         tcx.collect_and_partition_mono_items(()).1
362     } else {
363         // If only `--emit metadata` is used, we shouldn't perform any codegen.
364         // Also `tcx.collect_and_partition_mono_items` may panic in that case.
365         &[]
366     };
367
368     if tcx.dep_graph.is_fully_enabled() {
369         for cgu in &*cgus {
370             tcx.ensure().codegen_unit(cgu.name());
371         }
372     }
373
374     let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
375
376     let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
377
378     let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
379         cgus.iter()
380             .map(|cgu| {
381                 let cgu_reuse = if backend_config.disable_incr_cache {
382                     CguReuse::No
383                 } else {
384                     determine_cgu_reuse(tcx, cgu)
385                 };
386                 tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
387
388                 match cgu_reuse {
389                     CguReuse::No => {
390                         let dep_node = cgu.codegen_dep_node(tcx);
391                         tcx.dep_graph
392                             .with_task(
393                                 dep_node,
394                                 tcx,
395                                 (
396                                     backend_config.clone(),
397                                     global_asm_config.clone(),
398                                     cgu.name(),
399                                     concurrency_limiter.acquire(),
400                                 ),
401                                 module_codegen,
402                                 Some(rustc_middle::dep_graph::hash_result),
403                             )
404                             .0
405                     }
406                     CguReuse::PreLto => unreachable!(),
407                     CguReuse::PostLto => {
408                         concurrency_limiter.job_already_done();
409                         OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, &*cgu))
410                     }
411                 }
412             })
413             .collect::<Vec<_>>()
414     });
415
416     let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
417     let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
418     let created_alloc_shim =
419         crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
420
421     let allocator_module = if created_alloc_shim {
422         let mut product = allocator_module.finish();
423         allocator_unwind_context.emit(&mut product);
424
425         match emit_module(
426             tcx.output_filenames(()),
427             &tcx.sess.prof,
428             product.object,
429             ModuleKind::Allocator,
430             "allocator_shim".to_owned(),
431         ) {
432             Ok(allocator_module) => Some(allocator_module),
433             Err(err) => tcx.sess.fatal(err),
434         }
435     } else {
436         None
437     };
438
439     let metadata_module = if need_metadata_module {
440         let _timer = tcx.prof.generic_activity("codegen crate metadata");
441         let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
442             use rustc_middle::mir::mono::CodegenUnitNameBuilder;
443
444             let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
445             let metadata_cgu_name = cgu_name_builder
446                 .build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
447                 .as_str()
448                 .to_string();
449
450             let tmp_file =
451                 tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
452
453             let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
454             let obj = create_compressed_metadata_file(tcx.sess, &metadata, &symbol_name);
455
456             if let Err(err) = std::fs::write(&tmp_file, obj) {
457                 tcx.sess.fatal(&format!("error writing metadata object file: {}", err));
458             }
459
460             (metadata_cgu_name, tmp_file)
461         });
462
463         Some(CompiledModule {
464             name: metadata_cgu_name,
465             kind: ModuleKind::Metadata,
466             object: Some(tmp_file),
467             dwarf_object: None,
468             bytecode: None,
469         })
470     } else {
471         None
472     };
473
474     // FIXME handle `-Ctarget-cpu=native`
475     let target_cpu = match tcx.sess.opts.cg.target_cpu {
476         Some(ref name) => name,
477         None => tcx.sess.target.cpu.as_ref(),
478     }
479     .to_owned();
480
481     Box::new(OngoingCodegen {
482         modules,
483         allocator_module,
484         metadata_module,
485         metadata,
486         crate_info: CrateInfo::new(tcx, target_cpu),
487         concurrency_limiter,
488     })
489 }
490
491 // Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
492 fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
493     if !tcx.dep_graph.is_fully_enabled() {
494         return CguReuse::No;
495     }
496
497     let work_product_id = &cgu.work_product_id();
498     if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
499         // We don't have anything cached for this CGU. This can happen
500         // if the CGU did not exist in the previous session.
501         return CguReuse::No;
502     }
503
504     // Try to mark the CGU as green. If it we can do so, it means that nothing
505     // affecting the LLVM module has changed and we can re-use a cached version.
506     // If we compile with any kind of LTO, this means we can re-use the bitcode
507     // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
508     // know that later). If we are not doing LTO, there is only one optimized
509     // version of each module, so we re-use that.
510     let dep_node = cgu.codegen_dep_node(tcx);
511     assert!(
512         !tcx.dep_graph.dep_node_exists(&dep_node),
513         "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
514         cgu.name()
515     );
516
517     if tcx.try_mark_green(&dep_node) { CguReuse::PostLto } else { CguReuse::No }
518 }