]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/mono_item.rs
Rollup merge of #86947 - m-ou-se:assert-matches-to-submodule, r=yaahc
[rust.git] / compiler / rustc_codegen_llvm / src / mono_item.rs
1 use crate::abi::FnAbi;
2 use crate::attributes;
3 use crate::base;
4 use crate::context::CodegenCx;
5 use crate::llvm;
6 use crate::type_of::LayoutLlvmExt;
7 use rustc_codegen_ssa::traits::*;
8 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9 pub use rustc_middle::mir::mono::MonoItem;
10 use rustc_middle::mir::mono::{Linkage, Visibility};
11 use rustc_middle::ty::layout::FnAbiExt;
12 use rustc_middle::ty::{self, Instance, TypeFoldable};
13 use rustc_session::config::CrateType;
14 use rustc_target::abi::LayoutOf;
15 use rustc_target::spec::RelocModel;
16 use tracing::debug;
17
18 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
19     fn predefine_static(
20         &self,
21         def_id: DefId,
22         linkage: Linkage,
23         visibility: Visibility,
24         symbol_name: &str,
25     ) {
26         let instance = Instance::mono(self.tcx, def_id);
27         let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
28         let llty = self.layout_of(ty).llvm_type(self);
29
30         let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
31             self.sess().span_fatal(
32                 self.tcx.def_span(def_id),
33                 &format!("symbol `{}` is already defined", symbol_name),
34             )
35         });
36
37         unsafe {
38             llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
39             llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
40             if self.should_assume_dso_local(g, false) {
41                 llvm::LLVMRustSetDSOLocal(g, true);
42             }
43         }
44
45         self.instances.borrow_mut().insert(instance, g);
46     }
47
48     fn predefine_fn(
49         &self,
50         instance: Instance<'tcx>,
51         linkage: Linkage,
52         visibility: Visibility,
53         symbol_name: &str,
54     ) {
55         assert!(!instance.substs.needs_infer());
56
57         let fn_abi = FnAbi::of_instance(self, instance, &[]);
58         let lldecl = self.declare_fn(symbol_name, &fn_abi);
59         unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
60         let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
61         base::set_link_section(lldecl, &attrs);
62         if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
63             llvm::SetUniqueComdat(self.llmod, lldecl);
64         }
65
66         // If we're compiling the compiler-builtins crate, e.g., the equivalent of
67         // compiler-rt, then we want to implicitly compile everything with hidden
68         // visibility as we're going to link this object all over the place but
69         // don't want the symbols to get exported.
70         if linkage != Linkage::Internal
71             && linkage != Linkage::Private
72             && self.tcx.is_compiler_builtins(LOCAL_CRATE)
73         {
74             unsafe {
75                 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
76             }
77         } else {
78             unsafe {
79                 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
80             }
81         }
82
83         debug!("predefine_fn: instance = {:?}", instance);
84
85         attributes::from_fn_attrs(self, lldecl, instance);
86
87         unsafe {
88             if self.should_assume_dso_local(lldecl, false) {
89                 llvm::LLVMRustSetDSOLocal(lldecl, true);
90             }
91         }
92
93         self.instances.borrow_mut().insert(instance, lldecl);
94     }
95 }
96
97 impl CodegenCx<'ll, 'tcx> {
98     /// Whether a definition or declaration can be assumed to be local to a group of
99     /// libraries that form a single DSO or executable.
100     pub(crate) unsafe fn should_assume_dso_local(
101         &self,
102         llval: &llvm::Value,
103         is_declaration: bool,
104     ) -> bool {
105         let linkage = llvm::LLVMRustGetLinkage(llval);
106         let visibility = llvm::LLVMRustGetVisibility(llval);
107
108         if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
109             return true;
110         }
111
112         if visibility != llvm::Visibility::Default && linkage != llvm::Linkage::ExternalWeakLinkage
113         {
114             return true;
115         }
116
117         // Symbols from executables can't really be imported any further.
118         let all_exe = self.tcx.sess.crate_types().iter().all(|ty| *ty == CrateType::Executable);
119         let is_declaration_for_linker =
120             is_declaration || linkage == llvm::Linkage::AvailableExternallyLinkage;
121         if all_exe && !is_declaration_for_linker {
122             return true;
123         }
124
125         // PowerPC64 prefers TOC indirection to avoid copy relocations.
126         if matches!(&*self.tcx.sess.target.arch, "powerpc64" | "powerpc64le") {
127             return false;
128         }
129
130         // Thread-local variables generally don't support copy relocations.
131         let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
132             .map(|v| llvm::LLVMIsThreadLocal(v) == llvm::True)
133             .unwrap_or(false);
134         if is_thread_local_var {
135             return false;
136         }
137
138         // Static relocation model should force copy relocations everywhere.
139         if self.tcx.sess.relocation_model() == RelocModel::Static {
140             return true;
141         }
142
143         return false;
144     }
145 }