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