rustc_codegen_llvm/
base.rs1use 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::SanitizerSet;
29
30use super::ModuleLlvm;
31use crate::attributes;
32use crate::builder::Builder;
33use crate::builder::gpu_offload::OffloadGlobals;
34use crate::context::CodegenCx;
35use crate::llvm::{self, Value};
36
37pub(crate) struct ValueIter<'ll> {
38 cur: Option<&'ll Value>,
39 step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
40}
41
42impl<'ll> Iterator for ValueIter<'ll> {
43 type Item = &'ll Value;
44
45 fn next(&mut self) -> Option<&'ll Value> {
46 let old = self.cur;
47 if let Some(old) = old {
48 self.cur = unsafe { (self.step)(old) };
49 }
50 old
51 }
52}
53
54pub(crate) fn iter_globals(llmod: &llvm::Module) -> ValueIter<'_> {
55 unsafe { ValueIter { cur: llvm::LLVMGetFirstGlobal(llmod), step: llvm::LLVMGetNextGlobal } }
56}
57
58pub(crate) fn compile_codegen_unit(
59 tcx: TyCtxt<'_>,
60 cgu_name: Symbol,
61) -> (ModuleCodegen<ModuleLlvm>, u64) {
62 let start_time = Instant::now();
63
64 let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
65 let (module, _) = tcx.dep_graph.with_task(
66 dep_node,
67 tcx,
68 || module_codegen(tcx, cgu_name),
69 Some(dep_graph::hash_result),
70 );
71 let time_to_codegen = start_time.elapsed();
72
73 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 let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
86 {
87 let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
88
89 let has_host_offload = cx
96 .sess()
97 .opts
98 .unstable_opts
99 .offload
100 .iter()
101 .any(|o| #[allow(non_exhaustive_omitted_patterns)] match o {
Offload::Host(_) | Offload::Test => true,
_ => false,
}matches!(o, Offload::Host(_) | Offload::Test));
102 if has_host_offload && !cx.sess().target.is_like_gpu {
103 cx.offload_globals.replace(Some(OffloadGlobals::declare(&cx)));
104 }
105
106 let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
107 for &(mono_item, data) in &mono_items {
108 mono_item.predefine::<Builder<'_, '_, '_>>(
109 &mut cx,
110 cgu_name.as_str(),
111 data.linkage,
112 data.visibility,
113 );
114 }
115
116 for &(mono_item, item_data) in &mono_items {
118 mono_item.define::<Builder<'_, '_, '_>>(&mut cx, cgu_name.as_str(), item_data);
119 }
120
121 if let Some(entry) =
124 maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
125 {
126 let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default());
127 attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
128 }
129
130 if !cx.objc_classrefs.borrow().is_empty() || !cx.objc_selrefs.borrow().is_empty() {
137 if cx.objc_abi_version() == 1 {
138 cx.define_objc_module_info();
139 }
140 cx.add_objc_module_flags();
141 }
142
143 if cx.sess().instrument_coverage() {
146 cx.coverageinfo_finalize();
147 }
148
149 if !cx.used_statics.is_empty() {
151 cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
152 }
153
154 {
156 let compiler_used_statics = cx.compiler_used_statics.borrow();
157 if !compiler_used_statics.is_empty() {
158 cx.create_used_variable_impl(c"llvm.compiler.used", &compiler_used_statics);
159 }
160 }
161
162 for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
165 unsafe {
166 llvm::LLVMReplaceAllUsesWith(old_g, new_g);
167 llvm::LLVMDeleteGlobal(old_g);
168 }
169 }
170
171 if cx.sess().opts.debuginfo != DebugInfo::None {
173 cx.debuginfo_finalize();
174 }
175 }
176
177 ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)
178 }
179
180 (module, cost)
181}
182
183pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
184 let Some(sect) = attrs.link_section else { return };
185 let buf = SmallCStr::new(sect.as_str());
186 llvm::set_section(llval, &buf);
187}
188
189pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
190 match linkage {
191 Linkage::External => llvm::Linkage::ExternalLinkage,
192 Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
193 Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
194 Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
195 Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
196 Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
197 Linkage::Internal => llvm::Linkage::InternalLinkage,
198 Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
199 Linkage::Common => llvm::Linkage::CommonLinkage,
200 }
201}
202
203pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
204 match linkage {
205 Visibility::Default => llvm::Visibility::Default,
206 Visibility::Hidden => llvm::Visibility::Hidden,
207 Visibility::Protected => llvm::Visibility::Protected,
208 }
209}
210
211pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
212 if attrs.sanitizers.disabled.contains(SanitizerSet::ADDRESS)
213 || attrs.sanitizers.disabled.contains(SanitizerSet::KERNELADDRESS)
214 {
215 unsafe { llvm::LLVMRustSetNoSanitizeAddress(llval) };
216 }
217 if attrs.sanitizers.disabled.contains(SanitizerSet::HWADDRESS)
218 || attrs.sanitizers.disabled.contains(SanitizerSet::KERNELHWADDRESS)
219 {
220 unsafe { llvm::LLVMRustSetNoSanitizeHWAddress(llval) };
221 }
222}