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