Skip to main content

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, SanitizerFnAttrs};
24use rustc_middle::mono::Visibility;
25use rustc_middle::ty::TyCtxt;
26use rustc_session::config::{DebugInfo, Offload};
27use rustc_span::Symbol;
28use rustc_target::spec::{LlvmAbi, SanitizerSet};
29
30use super::ModuleLlvm;
31use crate::attributes;
32use crate::builder::Builder;
33use crate::builder::gpu_offload::OffloadGlobals;
34use crate::common::pauth_fn_attrs;
35use crate::context::CodegenCx;
36use crate::llvm::{self, Value};
37
38pub(crate) struct ValueIter<'ll> {
39    cur: Option<&'ll Value>,
40    step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
41}
42
43impl<'ll> Iterator for ValueIter<'ll> {
44    type Item = &'ll Value;
45
46    fn next(&mut self) -> Option<&'ll Value> {
47        let old = self.cur;
48        if let Some(old) = old {
49            self.cur = unsafe { (self.step)(old) };
50        }
51        old
52    }
53}
54
55pub(crate) fn iter_globals(llmod: &llvm::Module) -> ValueIter<'_> {
56    unsafe { ValueIter { cur: llvm::LLVMGetFirstGlobal(llmod), step: llvm::LLVMGetNextGlobal } }
57}
58
59pub(crate) fn compile_codegen_unit(
60    tcx: TyCtxt<'_>,
61    cgu_name: Symbol,
62) -> (ModuleCodegen<ModuleLlvm>, u64) {
63    let start_time = Instant::now();
64
65    let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
66    let (module, _) = tcx.dep_graph.with_task(
67        dep_node,
68        tcx,
69        || module_codegen(tcx, cgu_name),
70        Some(dep_graph::hash_result),
71    );
72    let time_to_codegen = start_time.elapsed();
73
74    // We assume that the cost to run LLVM on a CGU is proportional to
75    // the time we needed for codegenning it.
76    let cost = time_to_codegen.as_nanos() as u64;
77
78    fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm> {
79        let cgu = tcx.codegen_unit(cgu_name);
80        let _prof_timer =
81            tcx.prof.generic_activity_with_arg_recorder("codegen_module", |recorder| {
82                recorder.record_arg(cgu_name.to_string());
83                recorder.record_arg(cgu.size_estimate().to_string());
84            });
85        // Instantiate monomorphizations without filling out definitions yet...
86        let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
87        {
88            let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
89
90            // Declare and store globals shared by all offload kernels
91            //
92            // These globals are left in the LLVM-IR host module so all kernels can access them.
93            // They are necessary for correct offload execution. We do this here to simplify the
94            // `offload` intrinsic, avoiding the need for tracking whether it's the first
95            // intrinsic call or not.
96            let has_host_offload = cx
97                .sess()
98                .opts
99                .unstable_opts
100                .offload
101                .iter()
102                .any(|o| #[allow(non_exhaustive_omitted_patterns)] match o {
    Offload::Host(_) | Offload::Test => true,
    _ => false,
}matches!(o, Offload::Host(_) | Offload::Test));
103            if has_host_offload && !cx.sess().target.is_like_gpu {
104                cx.offload_globals.replace(Some(OffloadGlobals::declare(&cx)));
105            }
106
107            let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
108            for &(mono_item, data) in &mono_items {
109                mono_item.predefine::<Builder<'_, '_, '_>>(
110                    &mut cx,
111                    cgu_name.as_str(),
112                    data.linkage,
113                    data.visibility,
114                );
115            }
116
117            // ... and now that we have everything pre-defined, fill out those definitions.
118            for &(mono_item, item_data) in &mono_items {
119                mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
120            }
121
122            // If this codegen unit contains the main function, also create the
123            // wrapper here
124            if let Some(entry) =
125                maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
126            {
127                let mut attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default());
128                // When pointer authentication is enabled, ensure that the ptrauth-* attributes are
129                // also attached to the entry wrapper.
130                //
131                // FIXME(jchlanda) If it ever becomes necessary to ensure that all compiler
132                // generated functions receive the ptrauth-* attributes, `declare_fn` or
133                // `declare_raw_fn` could be used to provide those.
134                if cx.sess().target.llvm_abiname == LlvmAbi::Pauthtest {
135                    for &ptrauth_attr in pauth_fn_attrs() {
136                        attrs.push(llvm::CreateAttrString(cx.llcx, ptrauth_attr));
137                    }
138                }
139                attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
140            }
141
142            // Define Objective-C module info and module flags. Note, the module info will
143            // also be added to the `llvm.compiler.used` variable, created later.
144            //
145            // These are only necessary when we need the linker to do its Objective-C-specific
146            // magic. We could theoretically do it unconditionally, but at a slight cost to linker
147            // performance in the common case where it's unnecessary.
148            if !cx.objc_classrefs.borrow().is_empty() || !cx.objc_selrefs.borrow().is_empty() {
149                if cx.objc_abi_version() == 1 {
150                    cx.define_objc_module_info();
151                }
152                cx.add_objc_module_flags();
153            }
154
155            if cx.sess().target.llvm_abiname == LlvmAbi::Pauthtest {
156                // FIXME(jchlanda): In LLVM/Clang, there are also `aarch64-elf-pauthabi-platform`
157                // and `aarch64-elf-pauthabi-version` module flags. These are emitted into the
158                // PAuth core info section of the resulting ELF, which the linker uses to enforce
159                // binary compatibility.
160                //
161                // We intentionally do not emit these flags now, since only a subset of features
162                // included in clang's pauthtest is currently supported. By default, the absence of
163                // this info is treated as compatible with any binary.
164                //
165                // Please note, that this would cause compatibility issues, specifically runtime
166                // crashes due to authentication failures (while compiling and linking
167                // successfully) when linking against binaries that support larger set of features
168                // (for example, signing of C++ member function pointers, virtual function
169                // pointers, virtual table pointers).
170                //
171                // Link to PAuth core info documentation:
172                // <https://github.com/ARM-software/abi-aa/blob/2025Q4/pauthabielf64/pauthabielf64.rst#core-information>
173                if cx.sess().opts.unstable_opts.ptrauth_elf_got {
174                    cx.add_ptrauth_elf_got_flag();
175                }
176                cx.add_ptrauth_sign_personality_flag();
177            }
178
179            // Finalize code coverage by injecting the coverage map. Note, the coverage map will
180            // also be added to the `llvm.compiler.used` variable, created next.
181            if cx.sess().instrument_coverage() {
182                cx.coverageinfo_finalize();
183            }
184
185            // Create the llvm.used variable.
186            if !cx.used_statics.is_empty() {
187                cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
188            }
189
190            // Create the llvm.compiler.used variable.
191            {
192                let compiler_used_statics = cx.compiler_used_statics.borrow();
193                if !compiler_used_statics.is_empty() {
194                    cx.create_used_variable_impl(c"llvm.compiler.used", &compiler_used_statics);
195                }
196            }
197
198            // Run replace-all-uses-with for statics that need it. This must
199            // happen after the llvm.used variables are created.
200            for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
201                unsafe {
202                    llvm::LLVMReplaceAllUsesWith(old_g, new_g);
203                    llvm::LLVMDeleteGlobal(old_g);
204                }
205            }
206
207            // Finalize debuginfo
208            if cx.sess().opts.debuginfo != DebugInfo::None {
209                cx.debuginfo_finalize();
210            }
211        }
212
213        ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)
214    }
215
216    (module, cost)
217}
218
219pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
220    let Some(sect) = attrs.link_section else { return };
221    let buf = SmallCStr::new(sect.as_str());
222    llvm::set_section(llval, &buf);
223}
224
225pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
226    match linkage {
227        Linkage::External => llvm::Linkage::ExternalLinkage,
228        Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
229        Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
230        Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
231        Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
232        Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
233        Linkage::Internal => llvm::Linkage::InternalLinkage,
234        Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
235        Linkage::Common => llvm::Linkage::CommonLinkage,
236    }
237}
238
239pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
240    match linkage {
241        Visibility::Default => llvm::Visibility::Default,
242        Visibility::Hidden => llvm::Visibility::Hidden,
243        Visibility::Protected => llvm::Visibility::Protected,
244    }
245}
246
247pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
248    if attrs.sanitizers.disabled.contains(SanitizerSet::ADDRESS)
249        || attrs.sanitizers.disabled.contains(SanitizerSet::KERNELADDRESS)
250    {
251        unsafe { llvm::LLVMRustSetNoSanitizeAddress(llval) };
252    }
253    if attrs.sanitizers.disabled.contains(SanitizerSet::HWADDRESS)
254        || attrs.sanitizers.disabled.contains(SanitizerSet::KERNELHWADDRESS)
255    {
256        unsafe { llvm::LLVMRustSetNoSanitizeHWAddress(llval) };
257    }
258}