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::mir::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 cgu_name,
69 module_codegen,
70 Some(dep_graph::hash_result),
71 );
72 let time_to_codegen = start_time.elapsed();
73
74 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 let llvm_module = ModuleLlvm::new(tcx, cgu_name.as_str());
87 {
88 let mut cx = CodegenCx::new(tcx, cgu, &llvm_module);
89
90 let has_host_offload = cx
97 .sess()
98 .opts
99 .unstable_opts
100 .offload
101 .iter()
102 .any(|o| 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 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 let Some(entry) =
125 maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
126 {
127 let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default());
128 attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
129 }
130
131 if !cx.objc_classrefs.borrow().is_empty() || !cx.objc_selrefs.borrow().is_empty() {
138 if cx.objc_abi_version() == 1 {
139 cx.define_objc_module_info();
140 }
141 cx.add_objc_module_flags();
142 }
143
144 if cx.sess().instrument_coverage() {
147 cx.coverageinfo_finalize();
148 }
149
150 if !cx.used_statics.is_empty() {
152 cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
153 }
154
155 {
157 let compiler_used_statics = cx.compiler_used_statics.borrow();
158 if !compiler_used_statics.is_empty() {
159 cx.create_used_variable_impl(c"llvm.compiler.used", &compiler_used_statics);
160 }
161 }
162
163 for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
166 unsafe {
167 llvm::LLVMReplaceAllUsesWith(old_g, new_g);
168 llvm::LLVMDeleteGlobal(old_g);
169 }
170 }
171
172 if cx.sess().opts.debuginfo != DebugInfo::None {
174 cx.debuginfo_finalize();
175 }
176 }
177
178 ModuleCodegen::new_regular(cgu_name.to_string(), llvm_module)
179 }
180
181 (module, cost)
182}
183
184pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
185 let Some(sect) = attrs.link_section else { return };
186 let buf = SmallCStr::new(sect.as_str());
187 llvm::set_section(llval, &buf);
188}
189
190pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
191 match linkage {
192 Linkage::External => llvm::Linkage::ExternalLinkage,
193 Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
194 Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
195 Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
196 Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
197 Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
198 Linkage::Internal => llvm::Linkage::InternalLinkage,
199 Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
200 Linkage::Common => llvm::Linkage::CommonLinkage,
201 }
202}
203
204pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
205 match linkage {
206 Visibility::Default => llvm::Visibility::Default,
207 Visibility::Hidden => llvm::Visibility::Hidden,
208 Visibility::Protected => llvm::Visibility::Protected,
209 }
210}
211
212pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
213 if attrs.sanitizers.disabled.contains(SanitizerSet::ADDRESS) {
214 unsafe { llvm::LLVMRustSetNoSanitizeAddress(llval) };
215 }
216 if attrs.sanitizers.disabled.contains(SanitizerSet::HWADDRESS) {
217 unsafe { llvm::LLVMRustSetNoSanitizeHWAddress(llval) };
218 }
219}