]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/driver/aot.rs
Merge commit '7d53619064ab7045c383644cb445052d2a3d46db' into sync_cg_clif-2023-02-09
[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) =
276         tcx.prof.verbose_generic_activity_with_arg("codegen cgu", cgu_name.as_str()).run(|| {
277             let cgu = tcx.codegen_unit(cgu_name);
278             let mono_items = cgu.items_in_deterministic_order(tcx);
279
280             let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string());
281
282             let mut cx = crate::CodegenCx::new(
283                 tcx,
284                 backend_config.clone(),
285                 module.isa(),
286                 tcx.sess.opts.debuginfo != DebugInfo::None,
287                 cgu_name,
288             );
289             super::predefine_mono_items(tcx, &mut module, &mono_items);
290             let mut codegened_functions = vec![];
291             for (mono_item, _) in mono_items {
292                 match mono_item {
293                     MonoItem::Fn(inst) => {
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                     MonoItem::Static(def_id) => {
304                         crate::constant::codegen_static(tcx, &mut module, def_id)
305                     }
306                     MonoItem::GlobalAsm(item_id) => {
307                         crate::global_asm::codegen_global_asm_item(
308                             tcx,
309                             &mut cx.global_asm,
310                             item_id,
311                         );
312                     }
313                 }
314             }
315             crate::main_shim::maybe_create_entry_wrapper(
316                 tcx,
317                 &mut module,
318                 &mut cx.unwind_context,
319                 false,
320                 cgu.is_primary(),
321             );
322
323             let cgu_name = cgu.name().as_str().to_owned();
324
325             (cgu_name, cx, module, codegened_functions)
326         });
327
328     OngoingModuleCodegen::Async(std::thread::spawn(move || {
329         cx.profiler.clone().verbose_generic_activity_with_arg("compile functions", &*cgu_name).run(
330             || {
331                 let mut cached_context = Context::new();
332                 for codegened_func in codegened_functions {
333                     crate::base::compile_fn(
334                         &mut cx,
335                         &mut cached_context,
336                         &mut module,
337                         codegened_func,
338                     );
339                 }
340             },
341         );
342
343         let global_asm_object_file = cx
344             .profiler
345             .verbose_generic_activity_with_arg("compile assembly", &*cgu_name)
346             .run(|| {
347                 crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
348             })?;
349
350         let codegen_result = cx
351             .profiler
352             .verbose_generic_activity_with_arg("write object file", &*cgu_name)
353             .run(|| {
354                 emit_cgu(
355                     &global_asm_config.output_filenames,
356                     &cx.profiler,
357                     cgu_name,
358                     module,
359                     cx.debug_context,
360                     cx.unwind_context,
361                     global_asm_object_file,
362                 )
363             });
364         std::mem::drop(token);
365         codegen_result
366     }))
367 }
368
369 pub(crate) fn run_aot(
370     tcx: TyCtxt<'_>,
371     backend_config: BackendConfig,
372     metadata: EncodedMetadata,
373     need_metadata_module: bool,
374 ) -> Box<OngoingCodegen> {
375     let cgus = if tcx.sess.opts.output_types.should_codegen() {
376         tcx.collect_and_partition_mono_items(()).1
377     } else {
378         // If only `--emit metadata` is used, we shouldn't perform any codegen.
379         // Also `tcx.collect_and_partition_mono_items` may panic in that case.
380         &[]
381     };
382
383     if tcx.dep_graph.is_fully_enabled() {
384         for cgu in &*cgus {
385             tcx.ensure().codegen_unit(cgu.name());
386         }
387     }
388
389     let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
390
391     let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
392
393     let modules = tcx.sess.time("codegen mono items", || {
394         cgus.iter()
395             .map(|cgu| {
396                 let cgu_reuse = if backend_config.disable_incr_cache {
397                     CguReuse::No
398                 } else {
399                     determine_cgu_reuse(tcx, cgu)
400                 };
401                 tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
402
403                 match cgu_reuse {
404                     CguReuse::No => {
405                         let dep_node = cgu.codegen_dep_node(tcx);
406                         tcx.dep_graph
407                             .with_task(
408                                 dep_node,
409                                 tcx,
410                                 (
411                                     backend_config.clone(),
412                                     global_asm_config.clone(),
413                                     cgu.name(),
414                                     concurrency_limiter.acquire(),
415                                 ),
416                                 module_codegen,
417                                 Some(rustc_middle::dep_graph::hash_result),
418                             )
419                             .0
420                     }
421                     CguReuse::PreLto => unreachable!(),
422                     CguReuse::PostLto => {
423                         concurrency_limiter.job_already_done();
424                         OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, &*cgu))
425                     }
426                 }
427             })
428             .collect::<Vec<_>>()
429     });
430
431     let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
432     let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
433     let created_alloc_shim =
434         crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
435
436     let allocator_module = if created_alloc_shim {
437         let mut product = allocator_module.finish();
438         allocator_unwind_context.emit(&mut product);
439
440         match emit_module(
441             tcx.output_filenames(()),
442             &tcx.sess.prof,
443             product.object,
444             ModuleKind::Allocator,
445             "allocator_shim".to_owned(),
446         ) {
447             Ok(allocator_module) => Some(allocator_module),
448             Err(err) => tcx.sess.fatal(err),
449         }
450     } else {
451         None
452     };
453
454     let metadata_module = if need_metadata_module {
455         let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
456             use rustc_middle::mir::mono::CodegenUnitNameBuilder;
457
458             let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
459             let metadata_cgu_name = cgu_name_builder
460                 .build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
461                 .as_str()
462                 .to_string();
463
464             let tmp_file =
465                 tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
466
467             let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
468             let obj = create_compressed_metadata_file(tcx.sess, &metadata, &symbol_name);
469
470             if let Err(err) = std::fs::write(&tmp_file, obj) {
471                 tcx.sess.fatal(&format!("error writing metadata object file: {}", err));
472             }
473
474             (metadata_cgu_name, tmp_file)
475         });
476
477         Some(CompiledModule {
478             name: metadata_cgu_name,
479             kind: ModuleKind::Metadata,
480             object: Some(tmp_file),
481             dwarf_object: None,
482             bytecode: None,
483         })
484     } else {
485         None
486     };
487
488     // FIXME handle `-Ctarget-cpu=native`
489     let target_cpu = match tcx.sess.opts.cg.target_cpu {
490         Some(ref name) => name,
491         None => tcx.sess.target.cpu.as_ref(),
492     }
493     .to_owned();
494
495     Box::new(OngoingCodegen {
496         modules,
497         allocator_module,
498         metadata_module,
499         metadata,
500         crate_info: CrateInfo::new(tcx, target_cpu),
501         concurrency_limiter,
502     })
503 }
504
505 // Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
506 fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
507     if !tcx.dep_graph.is_fully_enabled() {
508         return CguReuse::No;
509     }
510
511     let work_product_id = &cgu.work_product_id();
512     if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
513         // We don't have anything cached for this CGU. This can happen
514         // if the CGU did not exist in the previous session.
515         return CguReuse::No;
516     }
517
518     // Try to mark the CGU as green. If it we can do so, it means that nothing
519     // affecting the LLVM module has changed and we can re-use a cached version.
520     // If we compile with any kind of LTO, this means we can re-use the bitcode
521     // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
522     // know that later). If we are not doing LTO, there is only one optimized
523     // version of each module, so we re-use that.
524     let dep_node = cgu.codegen_dep_node(tcx);
525     assert!(
526         !tcx.dep_graph.dep_node_exists(&dep_node),
527         "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
528         cgu.name()
529     );
530
531     if tcx.try_mark_green(&dep_node) { CguReuse::PostLto } else { CguReuse::No }
532 }