]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/base.rs
Rollup merge of #104976 - WaffleLapkin:move_comments, r=cjgillot
[rust.git] / compiler / rustc_codegen_llvm / src / base.rs
1 //! Codegen the MIR to the LLVM IR.
2 //!
3 //! Hopefully useful general knowledge about codegen:
4 //!
5 //! * There's no way to find out the [`Ty`] type of a [`Value`]. Doing so
6 //!   would be "trying to get the eggs out of an omelette" (credit:
7 //!   pcwalton). You can, instead, find out its [`llvm::Type`] by calling [`val_ty`],
8 //!   but one [`llvm::Type`] corresponds to many [`Ty`]s; for instance, `tup(int, int,
9 //!   int)` and `rec(x=int, y=int, z=int)` will have the same [`llvm::Type`].
10 //!
11 //! [`Ty`]: rustc_middle::ty::Ty
12 //! [`val_ty`]: crate::common::val_ty
13
14 use super::ModuleLlvm;
15
16 use crate::attributes;
17 use crate::builder::Builder;
18 use crate::context::CodegenCx;
19 use crate::llvm;
20 use crate::value::Value;
21
22 use cstr::cstr;
23
24 use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
25 use rustc_codegen_ssa::mono_item::MonoItemExt;
26 use rustc_codegen_ssa::traits::*;
27 use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
28 use rustc_data_structures::small_c_str::SmallCStr;
29 use rustc_middle::dep_graph;
30 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
31 use rustc_middle::mir::mono::{Linkage, Visibility};
32 use rustc_middle::ty::TyCtxt;
33 use rustc_session::config::DebugInfo;
34 use rustc_span::symbol::Symbol;
35 use rustc_target::spec::SanitizerSet;
36
37 use std::time::Instant;
38
39 pub struct ValueIter<'ll> {
40     cur: Option<&'ll Value>,
41     step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
42 }
43
44 impl<'ll> Iterator for ValueIter<'ll> {
45     type Item = &'ll Value;
46
47     fn next(&mut self) -> Option<&'ll Value> {
48         let old = self.cur;
49         if let Some(old) = old {
50             self.cur = unsafe { (self.step)(old) };
51         }
52         old
53     }
54 }
55
56 pub fn iter_globals(llmod: &llvm::Module) -> ValueIter<'_> {
57     unsafe { ValueIter { cur: llvm::LLVMGetFirstGlobal(llmod), step: llvm::LLVMGetNextGlobal } }
58 }
59
60 pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen<ModuleLlvm>, u64) {
61     let start_time = Instant::now();
62
63     let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
64     let (module, _) = tcx.dep_graph.with_task(
65         dep_node,
66         tcx,
67         cgu_name,
68         module_codegen,
69         Some(dep_graph::hash_result),
70     );
71     let time_to_codegen = start_time.elapsed();
72
73     // We assume that the cost to run LLVM on a CGU is proportional to
74     // the time we needed for codegenning it.
75     let cost = time_to_codegen.as_nanos() as u64;
76
77     fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm> {
78         let cgu = tcx.codegen_unit(cgu_name);
79         let _prof_timer =
80             tcx.prof.generic_activity_with_arg_recorder("codegen_module", |recorder| {
81                 recorder.record_arg(cgu_name.to_string());
82                 recorder.record_arg(cgu.size_estimate().to_string());
83             });
84         // Instantiate monomorphizations without filling out definitions yet...
85         let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
86         {
87             let cx = CodegenCx::new(tcx, cgu, &llvm_module);
88             let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
89             for &(mono_item, (linkage, visibility)) in &mono_items {
90                 mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
91             }
92
93             // ... and now that we have everything pre-defined, fill out those definitions.
94             for &(mono_item, _) in &mono_items {
95                 mono_item.define::<Builder<'_, '_, '_>>(&cx);
96             }
97
98             // If this codegen unit contains the main function, also create the
99             // wrapper here
100             if let Some(entry) = maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx) {
101                 let attrs = attributes::sanitize_attrs(&cx, SanitizerSet::empty());
102                 attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
103             }
104
105             // Finalize code coverage by injecting the coverage map. Note, the coverage map will
106             // also be added to the `llvm.compiler.used` variable, created next.
107             if cx.sess().instrument_coverage() {
108                 cx.coverageinfo_finalize();
109             }
110
111             // Create the llvm.used and llvm.compiler.used variables.
112             if !cx.used_statics.borrow().is_empty() {
113                 cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow());
114             }
115             if !cx.compiler_used_statics.borrow().is_empty() {
116                 cx.create_used_variable_impl(
117                     cstr!("llvm.compiler.used"),
118                     &*cx.compiler_used_statics.borrow(),
119                 );
120             }
121
122             // Run replace-all-uses-with for statics that need it. This must
123             // happen after the llvm.used variables are created.
124             for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
125                 unsafe {
126                     let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
127                     llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
128                     llvm::LLVMDeleteGlobal(old_g);
129                 }
130             }
131
132             // Finalize debuginfo
133             if cx.sess().opts.debuginfo != DebugInfo::None {
134                 cx.debuginfo_finalize();
135             }
136         }
137
138         ModuleCodegen {
139             name: cgu_name.to_string(),
140             module_llvm: llvm_module,
141             kind: ModuleKind::Regular,
142         }
143     }
144
145     (module, cost)
146 }
147
148 pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
149     let Some(sect) = attrs.link_section else { return };
150     unsafe {
151         let buf = SmallCStr::new(sect.as_str());
152         llvm::LLVMSetSection(llval, buf.as_ptr());
153     }
154 }
155
156 pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
157     match linkage {
158         Linkage::External => llvm::Linkage::ExternalLinkage,
159         Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
160         Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
161         Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
162         Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
163         Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
164         Linkage::Appending => llvm::Linkage::AppendingLinkage,
165         Linkage::Internal => llvm::Linkage::InternalLinkage,
166         Linkage::Private => llvm::Linkage::PrivateLinkage,
167         Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
168         Linkage::Common => llvm::Linkage::CommonLinkage,
169     }
170 }
171
172 pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
173     match linkage {
174         Visibility::Default => llvm::Visibility::Default,
175         Visibility::Hidden => llvm::Visibility::Hidden,
176         Visibility::Protected => llvm::Visibility::Protected,
177     }
178 }