1 //! Handles codegen of callees as well as other call-related
2 //! things. Callees are a superset of normal rust values and sometimes
3 //! have different representations. In particular, top-level fn items
4 //! and methods are represented as just a fn ptr and not a full
7 use crate::abi::FnAbiLlvmExt;
10 use crate::context::CodegenCx;
12 use crate::value::Value;
13 use rustc_codegen_ssa::traits::*;
15 use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
16 use rustc_middle::ty::{self, Instance, TypeVisitable};
18 /// Codegens a reference to a fn/method item, monomorphizing and
19 /// inlining as it goes.
23 /// - `cx`: the crate context
24 /// - `instance`: the instance to be instantiated
25 pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value {
28 debug!("get_fn(instance={:?})", instance);
30 assert!(!instance.substs.needs_infer());
31 assert!(!instance.substs.has_escaping_bound_vars());
33 if let Some(&llfn) = cx.instances.borrow().get(&instance) {
37 let sym = tcx.symbol_name(instance).name;
39 "get_fn({:?}: {:?}) => {}",
41 instance.ty(cx.tcx(), ty::ParamEnv::reveal_all()),
45 let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
47 let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
48 // Create a fn pointer with the new signature.
49 let llptrty = fn_abi.ptr_to_llvm_type(cx);
51 // This is subtle and surprising, but sometimes we have to bitcast
52 // the resulting fn pointer. The reason has to do with external
53 // functions. If you have two crates that both bind the same C
54 // library, they may not use precisely the same types: for
55 // example, they will probably each declare their own structs,
56 // which are distinct types from LLVM's point of view (nominal
59 // Now, if those two crates are linked into an application, and
60 // they contain inlined code, you can wind up with a situation
61 // where both of those functions wind up being loaded into this
62 // application simultaneously. In that case, the same function
63 // (from LLVM's point of view) requires two types. But of course
64 // LLVM won't allow one function to have two types.
66 // What we currently do, therefore, is declare the function with
67 // one of the two types (whichever happens to come first) and then
68 // bitcast as needed when the function is referenced to make sure
69 // it has the type we expect.
71 // This can occur on either a crate-local or crate-external
72 // reference. It also occurs when testing libcore and in some
73 // other weird situations. Annoying.
74 if cx.val_ty(llfn) != llptrty {
75 debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
76 cx.const_ptrcast(llfn, llptrty)
78 debug!("get_fn: not casting pointer!");
82 let instance_def_id = instance.def_id();
83 let llfn = if tcx.sess.target.arch == "x86" &&
84 let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym)
86 // Fix for https://github.com/rust-lang/rust/issues/104453
87 // On x86 Windows, LLVM uses 'L' as the prefix for any private
88 // global symbols, so when we create an undecorated function symbol
89 // that begins with an 'L' LLVM misinterprets that as a private
90 // global symbol that it created and so fails the compilation at a
91 // later stage since such a symbol must have a definition.
93 // To avoid this, we set the Storage Class to "DllImport" so that
94 // LLVM will prefix the name with `__imp_`. Ideally, we'd like the
95 // existing logic below to set the Storage Class, but it has an
96 // exemption for MinGW for backwards compatability.
97 let llfn = cx.declare_fn(&common::i686_decorated_name(&dllimport, common::is_mingw_gnu_toolchain(&tcx.sess.target), true), fn_abi);
98 unsafe { llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport); }
101 cx.declare_fn(sym, fn_abi)
103 debug!("get_fn: not casting pointer!");
105 attributes::from_fn_attrs(cx, llfn, instance);
107 // Apply an appropriate linkage/visibility value to our item that we
110 // This is sort of subtle. Inside our codegen unit we started off
111 // compilation by predefining all our own `MonoItem` instances. That
112 // is, everything we're codegenning ourselves is already defined. That
113 // means that anything we're actually codegenning in this codegen unit
114 // will have hit the above branch in `get_declared_value`. As a result,
115 // we're guaranteed here that we're declaring a symbol that won't get
116 // defined, or in other words we're referencing a value from another
117 // codegen unit or even another crate.
119 // So because this is a foreign value we blanket apply an external
120 // linkage directive because it's coming from a different object file.
121 // The visibility here is where it gets tricky. This symbol could be
122 // referencing some foreign crate or foreign library (an `extern`
123 // block) in which case we want to leave the default visibility. We may
124 // also, though, have multiple codegen units. It could be a
125 // monomorphization, in which case its expected visibility depends on
126 // whether we are sharing generics or not. The important thing here is
127 // that the visibility we apply to the declaration is the same one that
128 // has been applied to the definition (wherever that definition may be).
130 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
132 let is_generic = instance.substs.non_erasable_generics().next().is_some();
135 // This is a monomorphization. Its expected visibility depends
136 // on whether we are in share-generics mode.
138 if cx.tcx.sess.opts.share_generics() {
139 // We are in share_generics mode.
141 if let Some(instance_def_id) = instance_def_id.as_local() {
142 // This is a definition from the current crate. If the
143 // definition is unreachable for downstream crates or
144 // the current crate does not re-export generics, the
145 // definition of the instance will have been declared
147 if cx.tcx.is_unreachable_local_definition(instance_def_id)
148 || !cx.tcx.local_crate_exports_generics()
150 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
153 // This is a monomorphization of a generic function
154 // defined in an upstream crate.
155 if instance.upstream_monomorphization(tcx).is_some() {
156 // This is instantiated in another crate. It cannot
159 // This is a local instantiation of an upstream definition.
160 // If the current crate does not re-export it
161 // (because it is a C library or an executable), it
162 // will have been declared `hidden`.
163 if !cx.tcx.local_crate_exports_generics() {
164 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
169 // When not sharing generics, all instances are in the same
170 // crate and have hidden visibility
171 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
174 // This is a non-generic function
175 if cx.tcx.is_codegened_item(instance_def_id) {
176 // This is a function that is instantiated in the local crate
178 if instance_def_id.is_local() {
179 // This is function that is defined in the local crate.
180 // If it is not reachable, it is hidden.
181 if !cx.tcx.is_reachable_non_generic(instance_def_id) {
182 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
185 // This is a function from an upstream crate that has
186 // been instantiated here. These are always hidden.
187 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
192 // MinGW: For backward compatibility we rely on the linker to decide whether it
193 // should use dllimport for functions.
194 if cx.use_dll_storage_attrs
195 && let Some(library) = tcx.native_library(instance_def_id)
196 && library.kind.is_dllimport()
197 && !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
199 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
202 if cx.should_assume_dso_local(llfn, true) {
203 llvm::LLVMRustSetDSOLocal(llfn, true);
210 cx.instances.borrow_mut().insert(instance, llfn);