]>
Commit | Line | Data |
---|---|---|
94b46f34 | 1 | //! Handles codegen of callees as well as other call-related |
9fa01778 XL |
2 | //! things. Callees are a superset of normal rust values and sometimes |
3 | //! have different representations. In particular, top-level fn items | |
54a0048b SL |
4 | //! and methods are represented as just a fn ptr and not a full |
5 | //! closure. | |
6 | ||
60c5eb7d | 7 | use crate::abi::{FnAbi, FnAbiLlvmExt}; |
9fa01778 | 8 | use crate::attributes; |
9fa01778 | 9 | use crate::context::CodegenCx; |
dfeec247 | 10 | use crate::llvm; |
9fa01778 | 11 | use crate::value::Value; |
dfeec247 | 12 | use rustc_codegen_ssa::traits::*; |
3dfed10e | 13 | use tracing::debug; |
ff7c6d11 | 14 | |
ba9703b0 | 15 | use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt}; |
3dfed10e | 16 | use rustc_middle::ty::{self, Instance, TypeFoldable}; |
cdc7bbd5 | 17 | use rustc_target::spec::RelocModel; |
54a0048b | 18 | |
94b46f34 | 19 | /// Codegens a reference to a fn/method item, monomorphizing and |
54a0048b SL |
20 | /// inlining as it goes. |
21 | /// | |
22 | /// # Parameters | |
23 | /// | |
2c00a5a8 | 24 | /// - `cx`: the crate context |
cc61c64b | 25 | /// - `instance`: the instance to be instantiated |
dfeec247 | 26 | pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value { |
a1dfa0c6 | 27 | let tcx = cx.tcx(); |
54a0048b | 28 | |
cc61c64b | 29 | debug!("get_fn(instance={:?})", instance); |
54a0048b | 30 | |
cc61c64b | 31 | assert!(!instance.substs.needs_infer()); |
a1dfa0c6 | 32 | assert!(!instance.substs.has_escaping_bound_vars()); |
54a0048b | 33 | |
e74abb32 | 34 | if let Some(&llfn) = cx.instances.borrow().get(&instance) { |
cc61c64b | 35 | return llfn; |
54a0048b SL |
36 | } |
37 | ||
3dfed10e XL |
38 | let sym = tcx.symbol_name(instance).name; |
39 | debug!( | |
40 | "get_fn({:?}: {:?}) => {}", | |
41 | instance, | |
42 | instance.ty(cx.tcx(), ty::ParamEnv::reveal_all()), | |
43 | sym | |
44 | ); | |
54a0048b | 45 | |
60c5eb7d | 46 | let fn_abi = FnAbi::of_instance(cx, instance, &[]); |
3157f602 | 47 | |
a1dfa0c6 | 48 | let llfn = if let Some(llfn) = cx.get_declared_value(&sym) { |
60c5eb7d XL |
49 | // Create a fn pointer with the new signature. |
50 | let llptrty = fn_abi.ptr_to_llvm_type(cx); | |
51 | ||
ea8adc8c XL |
52 | // This is subtle and surprising, but sometimes we have to bitcast |
53 | // the resulting fn pointer. The reason has to do with external | |
54 | // functions. If you have two crates that both bind the same C | |
55 | // library, they may not use precisely the same types: for | |
56 | // example, they will probably each declare their own structs, | |
57 | // which are distinct types from LLVM's point of view (nominal | |
58 | // types). | |
59 | // | |
60 | // Now, if those two crates are linked into an application, and | |
61 | // they contain inlined code, you can wind up with a situation | |
62 | // where both of those functions wind up being loaded into this | |
63 | // application simultaneously. In that case, the same function | |
64 | // (from LLVM's point of view) requires two types. But of course | |
65 | // LLVM won't allow one function to have two types. | |
66 | // | |
67 | // What we currently do, therefore, is declare the function with | |
68 | // one of the two types (whichever happens to come first) and then | |
69 | // bitcast as needed when the function is referenced to make sure | |
70 | // it has the type we expect. | |
71 | // | |
72 | // This can occur on either a crate-local or crate-external | |
73 | // reference. It also occurs when testing libcore and in some | |
74 | // other weird situations. Annoying. | |
a1dfa0c6 | 75 | if cx.val_ty(llfn) != llptrty { |
54a0048b | 76 | debug!("get_fn: casting {:?} to {:?}", llfn, llptrty); |
a1dfa0c6 | 77 | cx.const_ptrcast(llfn, llptrty) |
54a0048b SL |
78 | } else { |
79 | debug!("get_fn: not casting pointer!"); | |
80 | llfn | |
81 | } | |
82 | } else { | |
60c5eb7d | 83 | let llfn = cx.declare_fn(&sym, &fn_abi); |
54a0048b SL |
84 | debug!("get_fn: not casting pointer!"); |
85 | ||
ba9703b0 | 86 | attributes::from_fn_attrs(cx, llfn, instance); |
9e0c209e | 87 | |
3b2f2976 XL |
88 | let instance_def_id = instance.def_id(); |
89 | ||
ea8adc8c XL |
90 | // Apply an appropriate linkage/visibility value to our item that we |
91 | // just declared. | |
92 | // | |
93 | // This is sort of subtle. Inside our codegen unit we started off | |
94b46f34 XL |
94 | // compilation by predefining all our own `MonoItem` instances. That |
95 | // is, everything we're codegenning ourselves is already defined. That | |
96 | // means that anything we're actually codegenning in this codegen unit | |
83c7162d XL |
97 | // will have hit the above branch in `get_declared_value`. As a result, |
98 | // we're guaranteed here that we're declaring a symbol that won't get | |
99 | // defined, or in other words we're referencing a value from another | |
100 | // codegen unit or even another crate. | |
ea8adc8c XL |
101 | // |
102 | // So because this is a foreign value we blanket apply an external | |
103 | // linkage directive because it's coming from a different object file. | |
104 | // The visibility here is where it gets tricky. This symbol could be | |
105 | // referencing some foreign crate or foreign library (an `extern` | |
106 | // block) in which case we want to leave the default visibility. We may | |
83c7162d XL |
107 | // also, though, have multiple codegen units. It could be a |
108 | // monomorphization, in which case its expected visibility depends on | |
109 | // whether we are sharing generics or not. The important thing here is | |
110 | // that the visibility we apply to the declaration is the same one that | |
111 | // has been applied to the definition (wherever that definition may be). | |
3b2f2976 XL |
112 | unsafe { |
113 | llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage); | |
114 | ||
532ac7d7 | 115 | let is_generic = instance.substs.non_erasable_generics().next().is_some(); |
83c7162d XL |
116 | |
117 | if is_generic { | |
118 | // This is a monomorphization. Its expected visibility depends | |
119 | // on whether we are in share-generics mode. | |
120 | ||
b7449926 | 121 | if cx.tcx.sess.opts.share_generics() { |
83c7162d XL |
122 | // We are in share_generics mode. |
123 | ||
f9f354fc | 124 | if let Some(instance_def_id) = instance_def_id.as_local() { |
83c7162d XL |
125 | // This is a definition from the current crate. If the |
126 | // definition is unreachable for downstream crates or | |
127 | // the current crate does not re-export generics, the | |
128 | // definition of the instance will have been declared | |
129 | // as `hidden`. | |
dfeec247 XL |
130 | if cx.tcx.is_unreachable_local_definition(instance_def_id) |
131 | || !cx.tcx.local_crate_exports_generics() | |
132 | { | |
83c7162d XL |
133 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); |
134 | } | |
135 | } else { | |
136 | // This is a monomorphization of a generic function | |
137 | // defined in an upstream crate. | |
dfeec247 | 138 | if instance.upstream_monomorphization(tcx).is_some() { |
83c7162d XL |
139 | // This is instantiated in another crate. It cannot |
140 | // be `hidden`. | |
141 | } else { | |
142 | // This is a local instantiation of an upstream definition. | |
143 | // If the current crate does not re-export it | |
144 | // (because it is a C library or an executable), it | |
145 | // will have been declared `hidden`. | |
146 | if !cx.tcx.local_crate_exports_generics() { | |
147 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); | |
148 | } | |
149 | } | |
3b2f2976 XL |
150 | } |
151 | } else { | |
83c7162d XL |
152 | // When not sharing generics, all instances are in the same |
153 | // crate and have hidden visibility | |
3b2f2976 XL |
154 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); |
155 | } | |
83c7162d XL |
156 | } else { |
157 | // This is a non-generic function | |
94b46f34 | 158 | if cx.tcx.is_codegened_item(instance_def_id) { |
83c7162d XL |
159 | // This is a function that is instantiated in the local crate |
160 | ||
161 | if instance_def_id.is_local() { | |
162 | // This is function that is defined in the local crate. | |
163 | // If it is not reachable, it is hidden. | |
164 | if !cx.tcx.is_reachable_non_generic(instance_def_id) { | |
165 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); | |
166 | } | |
167 | } else { | |
168 | // This is a function from an upstream crate that has | |
169 | // been instantiated here. These are always hidden. | |
170 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); | |
171 | } | |
172 | } | |
9e0c209e | 173 | } |
cc61c64b | 174 | |
cdc7bbd5 XL |
175 | // MinGW: For backward compatibility we rely on the linker to decide whether it |
176 | // should use dllimport for functions. | |
177 | if cx.use_dll_storage_attrs | |
178 | && tcx.is_dllimport_foreign_item(instance_def_id) | |
179 | && tcx.sess.target.env != "gnu" | |
180 | { | |
476ff2be SL |
181 | llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport); |
182 | } | |
cdc7bbd5 XL |
183 | |
184 | if cx.tcx.sess.relocation_model() == RelocModel::Static { | |
185 | llvm::LLVMRustSetDSOLocal(llfn, true); | |
186 | } | |
476ff2be | 187 | } |
ea8adc8c | 188 | |
54a0048b SL |
189 | llfn |
190 | }; | |
191 | ||
2c00a5a8 | 192 | cx.instances.borrow_mut().insert(instance, llfn); |
54a0048b | 193 | |
cc61c64b XL |
194 | llfn |
195 | } |