]> git.lizzy.rs Git - rust.git/blob - src/driver/aot.rs
fmt: Run cargo fmt since it is available
[rust.git] / src / driver / aot.rs
1 use std::path::PathBuf;
2
3 use rustc_codegen_ssa::back::linker::LinkerInfo;
4 use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
5 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
7 use rustc_middle::middle::cstore::EncodedMetadata;
8 use rustc_middle::mir::mono::CodegenUnit;
9 use rustc_session::cgu_reuse_tracker::CguReuse;
10 use rustc_session::config::{DebugInfo, OutputType};
11
12 use crate::prelude::*;
13
14 use crate::backend::{AddConstructor, Emit, WriteDebugInfo};
15
16 fn new_module(tcx: TyCtxt<'_>, name: String) -> Module<crate::backend::Backend> {
17     let module = crate::backend::make_module(tcx.sess, name);
18     assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
19     module
20 }
21
22 struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
23
24 impl<HCX> HashStable<HCX> for ModuleCodegenResult {
25     fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
26         // do nothing
27     }
28 }
29
30 fn emit_module<B: Backend>(
31     tcx: TyCtxt<'_>,
32     name: String,
33     kind: ModuleKind,
34     mut module: Module<B>,
35     debug: Option<DebugContext<'_>>,
36     unwind_context: UnwindContext<'_>,
37     map_product: impl FnOnce(B::Product) -> B::Product,
38 ) -> ModuleCodegenResult
39 where
40     B::Product: AddConstructor + Emit + WriteDebugInfo,
41 {
42     module.finalize_definitions();
43     let mut product = module.finish();
44
45     if let Some(mut debug) = debug {
46         debug.emit(&mut product);
47     }
48
49     unwind_context.emit(&mut product);
50
51     let product = map_product(product);
52
53     let tmp_file = tcx
54         .output_filenames(LOCAL_CRATE)
55         .temp_path(OutputType::Object, Some(&name));
56     let obj = product.emit();
57     if let Err(err) = std::fs::write(&tmp_file, obj) {
58         tcx.sess
59             .fatal(&format!("error writing object file: {}", err));
60     }
61
62     let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() {
63         None
64     } else {
65         rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
66             tcx.sess,
67             &name,
68             &Some(tmp_file.clone()),
69         )
70     };
71
72     ModuleCodegenResult(
73         CompiledModule {
74             name,
75             kind,
76             object: Some(tmp_file),
77             bytecode: None,
78         },
79         work_product,
80     )
81 }
82
83 fn reuse_workproduct_for_cgu(
84     tcx: TyCtxt<'_>,
85     cgu: &CodegenUnit<'_>,
86     work_products: &mut FxHashMap<WorkProductId, WorkProduct>,
87 ) -> CompiledModule {
88     let incr_comp_session_dir = tcx.sess.incr_comp_session_dir();
89     let mut object = None;
90     let work_product = cgu.work_product(tcx);
91     if let Some(saved_file) = &work_product.saved_file {
92         let obj_out = tcx
93             .output_filenames(LOCAL_CRATE)
94             .temp_path(OutputType::Object, Some(&cgu.name().as_str()));
95         object = Some(obj_out.clone());
96         let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, &saved_file);
97         if let Err(err) = rustc_fs_util::link_or_copy(&source_file, &obj_out) {
98             tcx.sess.err(&format!(
99                 "unable to copy {} to {}: {}",
100                 source_file.display(),
101                 obj_out.display(),
102                 err
103             ));
104         }
105     }
106
107     work_products.insert(cgu.work_product_id(), work_product);
108
109     CompiledModule {
110         name: cgu.name().to_string(),
111         kind: ModuleKind::Regular,
112         object,
113         bytecode: None,
114     }
115 }
116
117 fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult {
118     let cgu = tcx.codegen_unit(cgu_name);
119     let mono_items = cgu.items_in_deterministic_order(tcx);
120
121     let mut module = new_module(tcx, cgu_name.as_str().to_string());
122
123     // Initialize the global atomic mutex using a constructor for proc-macros.
124     // FIXME implement atomic instructions in Cranelift.
125     let mut init_atomics_mutex_from_constructor = None;
126     if tcx
127         .sess
128         .crate_types()
129         .contains(&rustc_session::config::CrateType::ProcMacro)
130     {
131         if mono_items.iter().any(|(mono_item, _)| match mono_item {
132             rustc_middle::mir::mono::MonoItem::Static(def_id) => tcx
133                 .symbol_name(Instance::mono(tcx, *def_id))
134                 .name
135                 .contains("__rustc_proc_macro_decls_"),
136             _ => false,
137         }) {
138             init_atomics_mutex_from_constructor =
139                 Some(crate::atomic_shim::init_global_lock_constructor(
140                     &mut module,
141                     &format!("{}_init_atomics_mutex", cgu_name.as_str()),
142                 ));
143         }
144     }
145
146     let mut cx = crate::CodegenCx::new(tcx, module, tcx.sess.opts.debuginfo != DebugInfo::None);
147     super::codegen_mono_items(&mut cx, mono_items);
148     let (mut module, global_asm, debug, mut unwind_context) =
149         tcx.sess.time("finalize CodegenCx", || cx.finalize());
150     crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
151
152     let codegen_result = emit_module(
153         tcx,
154         cgu.name().as_str().to_string(),
155         ModuleKind::Regular,
156         module,
157         debug,
158         unwind_context,
159         |mut product| {
160             if let Some(func_id) = init_atomics_mutex_from_constructor {
161                 product.add_constructor(func_id);
162             }
163
164             product
165         },
166     );
167
168     codegen_global_asm(tcx, &cgu.name().as_str(), &global_asm);
169
170     codegen_result
171 }
172
173 pub(super) fn run_aot(
174     tcx: TyCtxt<'_>,
175     metadata: EncodedMetadata,
176     need_metadata_module: bool,
177 ) -> Box<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)> {
178     let mut work_products = FxHashMap::default();
179
180     let cgus = if tcx.sess.opts.output_types.should_codegen() {
181         tcx.collect_and_partition_mono_items(LOCAL_CRATE).1
182     } else {
183         // If only `--emit metadata` is used, we shouldn't perform any codegen.
184         // Also `tcx.collect_and_partition_mono_items` may panic in that case.
185         &[]
186     };
187
188     if tcx.dep_graph.is_fully_enabled() {
189         for cgu in &*cgus {
190             tcx.ensure().codegen_unit(cgu.name());
191         }
192     }
193
194     let modules = super::time(tcx, "codegen mono items", || {
195         cgus.iter()
196             .map(|cgu| {
197                 let cgu_reuse = determine_cgu_reuse(tcx, cgu);
198                 tcx.sess
199                     .cgu_reuse_tracker
200                     .set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
201
202                 match cgu_reuse {
203                     _ if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() => {}
204                     CguReuse::No => {}
205                     CguReuse::PreLto => {
206                         return reuse_workproduct_for_cgu(tcx, &*cgu, &mut work_products);
207                     }
208                     CguReuse::PostLto => unreachable!(),
209                 }
210
211                 let dep_node = cgu.codegen_dep_node(tcx);
212                 let (ModuleCodegenResult(module, work_product), _) = tcx.dep_graph.with_task(
213                     dep_node,
214                     tcx,
215                     cgu.name(),
216                     module_codegen,
217                     rustc_middle::dep_graph::hash_result,
218                 );
219
220                 if let Some((id, product)) = work_product {
221                     work_products.insert(id, product);
222                 }
223
224                 module
225             })
226             .collect::<Vec<_>>()
227     });
228
229     tcx.sess.abort_if_errors();
230
231     let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
232     let mut allocator_unwind_context = UnwindContext::new(tcx, allocator_module.isa());
233     let created_alloc_shim =
234         crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
235
236     let allocator_module = if created_alloc_shim {
237         let ModuleCodegenResult(module, work_product) = emit_module(
238             tcx,
239             "allocator_shim".to_string(),
240             ModuleKind::Allocator,
241             allocator_module,
242             None,
243             allocator_unwind_context,
244             |product| product,
245         );
246         if let Some((id, product)) = work_product {
247             work_products.insert(id, product);
248         }
249         Some(module)
250     } else {
251         None
252     };
253
254     rustc_incremental::assert_dep_graph(tcx);
255     rustc_incremental::save_dep_graph(tcx);
256
257     let metadata_module = if need_metadata_module {
258         let _timer = tcx.prof.generic_activity("codegen crate metadata");
259         let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
260             use rustc_middle::mir::mono::CodegenUnitNameBuilder;
261
262             let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
263             let metadata_cgu_name = cgu_name_builder
264                 .build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
265                 .as_str()
266                 .to_string();
267
268             let tmp_file = tcx
269                 .output_filenames(LOCAL_CRATE)
270                 .temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
271
272             let obj = crate::backend::with_object(tcx.sess, &metadata_cgu_name, |object| {
273                 crate::metadata::write_metadata(tcx, object);
274             });
275
276             if let Err(err) = std::fs::write(&tmp_file, obj) {
277                 tcx.sess
278                     .fatal(&format!("error writing metadata object file: {}", err));
279             }
280
281             (metadata_cgu_name, tmp_file)
282         });
283
284         Some(CompiledModule {
285             name: metadata_cgu_name,
286             kind: ModuleKind::Metadata,
287             object: Some(tmp_file),
288             bytecode: None,
289         })
290     } else {
291         None
292     };
293
294     if tcx.sess.opts.output_types.should_codegen() {
295         rustc_incremental::assert_module_sources::assert_module_sources(tcx);
296     }
297
298     Box::new((
299         CodegenResults {
300             crate_name: tcx.crate_name(LOCAL_CRATE),
301             modules,
302             allocator_module,
303             metadata_module,
304             crate_hash: tcx.crate_hash(LOCAL_CRATE),
305             metadata,
306             windows_subsystem: None, // Windows is not yet supported
307             linker_info: LinkerInfo::new(tcx),
308             crate_info: CrateInfo::new(tcx),
309         },
310         work_products,
311     ))
312 }
313
314 fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
315     use std::io::Write;
316     use std::process::{Command, Stdio};
317
318     if global_asm.is_empty() {
319         return;
320     }
321
322     if cfg!(not(feature = "inline_asm"))
323         || tcx.sess.target.target.options.is_like_osx
324         || tcx.sess.target.target.options.is_like_windows
325     {
326         if global_asm.contains("__rust_probestack") {
327             return;
328         }
329
330         // FIXME fix linker error on macOS
331         if cfg!(not(feature = "inline_asm")) {
332             tcx.sess.fatal(
333                 "asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
334             );
335         } else {
336             tcx.sess
337                 .fatal("asm! and global_asm! are not yet supported on macOS and Windows");
338         }
339     }
340
341     let assembler = crate::toolchain::get_toolchain_binary(tcx.sess, "as");
342     let linker = crate::toolchain::get_toolchain_binary(tcx.sess, "ld");
343
344     // Remove all LLVM style comments
345     let global_asm = global_asm
346         .lines()
347         .map(|line| {
348             if let Some(index) = line.find("//") {
349                 &line[0..index]
350             } else {
351                 line
352             }
353         })
354         .collect::<Vec<_>>()
355         .join("\n");
356
357     let output_object_file = tcx
358         .output_filenames(LOCAL_CRATE)
359         .temp_path(OutputType::Object, Some(cgu_name));
360
361     // Assemble `global_asm`
362     let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm");
363     let mut child = Command::new(assembler)
364         .arg("-o")
365         .arg(&global_asm_object_file)
366         .stdin(Stdio::piped())
367         .spawn()
368         .expect("Failed to spawn `as`.");
369     child
370         .stdin
371         .take()
372         .unwrap()
373         .write_all(global_asm.as_bytes())
374         .unwrap();
375     let status = child.wait().expect("Failed to wait for `as`.");
376     if !status.success() {
377         tcx.sess
378             .fatal(&format!("Failed to assemble `{}`", global_asm));
379     }
380
381     // Link the global asm and main object file together
382     let main_object_file = add_file_stem_postfix(output_object_file.clone(), ".main");
383     std::fs::rename(&output_object_file, &main_object_file).unwrap();
384     let status = Command::new(linker)
385         .arg("-r") // Create a new object file
386         .arg("-o")
387         .arg(output_object_file)
388         .arg(&main_object_file)
389         .arg(&global_asm_object_file)
390         .status()
391         .unwrap();
392     if !status.success() {
393         tcx.sess.fatal(&format!(
394             "Failed to link `{}` and `{}` together",
395             main_object_file.display(),
396             global_asm_object_file.display(),
397         ));
398     }
399
400     std::fs::remove_file(global_asm_object_file).unwrap();
401     std::fs::remove_file(main_object_file).unwrap();
402 }
403
404 fn add_file_stem_postfix(mut path: PathBuf, postfix: &str) -> PathBuf {
405     let mut new_filename = path.file_stem().unwrap().to_owned();
406     new_filename.push(postfix);
407     if let Some(extension) = path.extension() {
408         new_filename.push(".");
409         new_filename.push(extension);
410     }
411     path.set_file_name(new_filename);
412     path
413 }
414
415 // Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
416 fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
417     if !tcx.dep_graph.is_fully_enabled() {
418         return CguReuse::No;
419     }
420
421     let work_product_id = &cgu.work_product_id();
422     if tcx
423         .dep_graph
424         .previous_work_product(work_product_id)
425         .is_none()
426     {
427         // We don't have anything cached for this CGU. This can happen
428         // if the CGU did not exist in the previous session.
429         return CguReuse::No;
430     }
431
432     // Try to mark the CGU as green. If it we can do so, it means that nothing
433     // affecting the LLVM module has changed and we can re-use a cached version.
434     // If we compile with any kind of LTO, this means we can re-use the bitcode
435     // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
436     // know that later). If we are not doing LTO, there is only one optimized
437     // version of each module, so we re-use that.
438     let dep_node = cgu.codegen_dep_node(tcx);
439     assert!(
440         !tcx.dep_graph.dep_node_exists(&dep_node),
441         "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
442         cgu.name()
443     );
444
445     if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
446         CguReuse::PreLto
447     } else {
448         CguReuse::No
449     }
450 }