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