rustc_codegen_llvm/
base.rs1use std::time::Instant;
15
16use rustc_attr_ir::Linkage;
17use rustc_codegen_ssa::ModuleCodegen;
18use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
19use rustc_codegen_ssa::mono_item::MonoItemExt;
20use rustc_codegen_ssa::traits::*;
21use rustc_data_structures::small_c_str::SmallCStr;
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::SanitizerSet;
29
30use crate::builder::Builder;
31use crate::builder::gpu_offload::OffloadGlobals;
32use crate::context::CodegenCx;
33use crate::llvm::{self, Value};
34use crate::{ModuleLlvm, attributes};
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 iter_global_aliases(llmod: &llvm::Module) -> ValueIter<'_> {
58 unsafe {
59 ValueIter { cur: llvm::LLVMGetFirstGlobalAlias(llmod), step: llvm::LLVMGetNextGlobalAlias }
60 }
61}
62
63pub(crate) fn compile_codegen_unit(
64 tcx: TyCtxt<'_>,
65 cgu_name: Symbol,
66 bitcode_needed: bool,
67) -> (ModuleCodegen<ModuleLlvm>, u64) {
68 let start_time = Instant::now();
69
70 let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
71 let (module, _) = tcx.dep_graph.with_task(
72 dep_node,
73 tcx,
74 || module_codegen(tcx, cgu_name, bitcode_needed),
75 Some(dep_graph::hash_result),
76 );
77 let time_to_codegen = start_time.elapsed();
78
79 let cost = time_to_codegen.as_nanos() as u64;
82
83 fn module_codegen(
84 tcx: TyCtxt<'_>,
85 cgu_name: Symbol,
86 needs_bitcode: bool,
87 ) -> ModuleCodegen<ModuleLlvm> {
88 let cgu = tcx.codegen_unit(cgu_name);
89 let _prof_timer =
90 tcx.prof.generic_activity_with_arg_recorder("codegen_module", |recorder| {
91 recorder.record_arg(cgu_name.to_string());
92 recorder.record_arg(cgu.size_estimate().to_string());
93 });
94 let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
96 {
97 let mut cx = CodegenCx::new(tcx, cgu, &llvm_module, needs_bitcode);
98
99 let has_host_offload = cx
106 .sess()
107 .opts
108 .unstable_opts
109 .offload
110 .iter()
111 .any(|o| #[allow(non_exhaustive_omitted_patterns)] match o {
Offload::Host(_) | Offload::Test => true,
_ => false,
}matches!(o, Offload::Host(_) | Offload::Test));
112 if has_host_offload && !cx.sess().target.is_like_gpu {
113 cx.offload_globals.replace(Some(OffloadGlobals::declare(&cx)));
114 }
115
116 let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
117 for &(mono_item, data) in &mono_items {
118 mono_item.predefine::<Builder<'_, '_, '_>>(
119 &mut cx,
120 cgu_name.as_str(),
121 data.linkage,
122 data.visibility,
123 );
124 }
125
126 for &(mono_item, item_data) in &mono_items {
128 mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
129 }
130
131 if let Some(entry) =
134 maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
135 {
136 let mut attrs =
137 attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default(), None, None);
138 if cx.sess().pointer_authentication() {
145 let cfg = cx.sess().pointer_auth_config.as_ref().unwrap();
146 for ptrauth_attr in cfg.fn_attrs() {
147 attrs.push(llvm::CreateAttrString(cx.llcx, ptrauth_attr));
148 }
149 }
150 attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
151 }
152
153 if !cx.objc_classrefs.borrow().is_empty() || !cx.objc_selrefs.borrow().is_empty() {
160 if cx.objc_abi_version() == 1 {
161 cx.define_objc_module_info();
162 }
163 cx.add_objc_module_flags();
164 }
165
166 if cx.sess().pointer_authentication() {
167 let cfg = cx.sess().pointer_auth_config.as_ref().unwrap();
168
169 let aarch64_elf_pauthabi_version =
170 cfg.calculate_pauth_abi_version(&cx.sess().target);
171 if aarch64_elf_pauthabi_version != 0 {
172 cx.add_ptrauth_pauthabi_version_and_platform_flags(
173 aarch64_elf_pauthabi_version,
174 );
175 }
176 if cfg.elf_got {
177 cx.add_ptrauth_elf_got_flag();
178 }
179 if cx.sess().pointer_authentication_functions().is_some() {
180 cx.add_ptrauth_sign_personality_flag();
181 }
182 }
183
184 if cx.sess().instrument_coverage() {
187 cx.coverageinfo_finalize();
188 }
189
190 if !cx.used_statics.is_empty() {
192 cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
193 }
194
195 {
197 let compiler_used_statics = cx.compiler_used_statics.borrow();
198 if !compiler_used_statics.is_empty() {
199 cx.create_used_variable_impl(c"llvm.compiler.used", &compiler_used_statics);
200 }
201 }
202
203 for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
206 unsafe {
207 llvm::LLVMReplaceAllUsesWith(old_g, new_g);
208 llvm::LLVMDeleteGlobal(old_g);
209 }
210 }
211
212 if cx.sess().opts.debuginfo != DebugInfo::None {
214 cx.debuginfo_finalize();
215 }
216 }
217
218 ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)
219 }
220
221 (module, cost)
222}
223
224pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
225 let Some(sect) = attrs.link_section else { return };
226 let buf = SmallCStr::new(sect.as_str());
227 llvm::set_section(llval, &buf);
228}
229
230pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
231 match linkage {
232 Linkage::External => llvm::Linkage::ExternalLinkage,
233 Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
234 Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
235 Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
236 Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
237 Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
238 Linkage::Internal => llvm::Linkage::InternalLinkage,
239 Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
240 }
241}
242
243pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
244 match linkage {
245 Visibility::Default => llvm::Visibility::Default,
246 Visibility::Hidden => llvm::Visibility::Hidden,
247 Visibility::Protected => llvm::Visibility::Protected,
248 }
249}
250
251pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
252 if attrs.sanitizers.disabled.contains(SanitizerSet::ADDRESS)
253 || attrs.sanitizers.disabled.contains(SanitizerSet::KERNELADDRESS)
254 {
255 unsafe { llvm::LLVMRustSetNoSanitizeAddress(llval) };
256 }
257 if attrs.sanitizers.disabled.contains(SanitizerSet::HWADDRESS)
258 || attrs.sanitizers.disabled.contains(SanitizerSet::KERNELHWADDRESS)
259 {
260 unsafe { llvm::LLVMRustSetNoSanitizeHWAddress(llval) };
261 }
262}