rustc_codegen_llvm/callee.rs
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
5//! closure.
6
7use rustc_codegen_ssa::common;
8use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv};
9use rustc_middle::ty::{self, Instance, TypeVisitableExt};
10use tracing::debug;
11
12use crate::context::CodegenCx;
13use crate::llvm;
14use crate::value::Value;
15
16/// Codegens a reference to a fn/method item, monomorphizing and
17/// inlining as it goes.
18pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value {
19 let tcx = cx.tcx();
20
21 debug!("get_fn(instance={:?})", instance);
22
23 assert!(!instance.args.has_infer());
24 assert!(!instance.args.has_escaping_bound_vars());
25
26 if let Some(&llfn) = cx.instances.borrow().get(&instance) {
27 return llfn;
28 }
29
30 let sym = tcx.symbol_name(instance).name;
31 debug!("get_fn({:?}: {:?}) => {}", instance, instance.ty(cx.tcx(), cx.typing_env()), sym);
32
33 let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
34
35 let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
36 llfn
37 } else {
38 let instance_def_id = instance.def_id();
39 let llfn = if tcx.sess.target.arch == "x86"
40 && let Some(dllimport) = crate::common::get_dllimport(tcx, instance_def_id, sym)
41 {
42 // When calling functions in generated import libraries, MSVC needs
43 // the fully decorated name (as would have been in the declaring
44 // object file), but MinGW wants the name as exported (as would be
45 // in the def file) which may be missing decorations.
46 let mingw_gnu_toolchain = common::is_mingw_gnu_toolchain(&tcx.sess.target);
47 let llfn = cx.declare_fn(
48 &common::i686_decorated_name(
49 dllimport,
50 mingw_gnu_toolchain,
51 true,
52 !mingw_gnu_toolchain,
53 ),
54 fn_abi,
55 Some(instance),
56 );
57
58 // Fix for https://github.com/rust-lang/rust/issues/104453
59 // On x86 Windows, LLVM uses 'L' as the prefix for any private
60 // global symbols, so when we create an undecorated function symbol
61 // that begins with an 'L' LLVM misinterprets that as a private
62 // global symbol that it created and so fails the compilation at a
63 // later stage since such a symbol must have a definition.
64 //
65 // To avoid this, we set the Storage Class to "DllImport" so that
66 // LLVM will prefix the name with `__imp_`. Ideally, we'd like the
67 // existing logic below to set the Storage Class, but it has an
68 // exemption for MinGW for backwards compatibility.
69 unsafe {
70 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
71 }
72 llfn
73 } else {
74 cx.declare_fn(sym, fn_abi, Some(instance))
75 };
76 debug!("get_fn: not casting pointer!");
77
78 // Apply an appropriate linkage/visibility value to our item that we
79 // just declared.
80 //
81 // This is sort of subtle. Inside our codegen unit we started off
82 // compilation by predefining all our own `MonoItem` instances. That
83 // is, everything we're codegenning ourselves is already defined. That
84 // means that anything we're actually codegenning in this codegen unit
85 // will have hit the above branch in `get_declared_value`. As a result,
86 // we're guaranteed here that we're declaring a symbol that won't get
87 // defined, or in other words we're referencing a value from another
88 // codegen unit or even another crate.
89 //
90 // So because this is a foreign value we blanket apply an external
91 // linkage directive because it's coming from a different object file.
92 // The visibility here is where it gets tricky. This symbol could be
93 // referencing some foreign crate or foreign library (an `extern`
94 // block) in which case we want to leave the default visibility. We may
95 // also, though, have multiple codegen units. It could be a
96 // monomorphization, in which case its expected visibility depends on
97 // whether we are sharing generics or not. The important thing here is
98 // that the visibility we apply to the declaration is the same one that
99 // has been applied to the definition (wherever that definition may be).
100
101 llvm::set_linkage(llfn, llvm::Linkage::ExternalLinkage);
102 unsafe {
103 let is_generic = instance.args.non_erasable_generics().next().is_some();
104
105 let is_hidden = if is_generic {
106 // This is a monomorphization of a generic function.
107 if !(cx.tcx.sess.opts.share_generics()
108 || tcx.codegen_fn_attrs(instance_def_id).inline
109 == rustc_attr_parsing::InlineAttr::Never)
110 {
111 // When not sharing generics, all instances are in the same
112 // crate and have hidden visibility.
113 true
114 } else {
115 if let Some(instance_def_id) = instance_def_id.as_local() {
116 // This is a monomorphization of a generic function
117 // defined in the current crate. It is hidden if:
118 // - the definition is unreachable for downstream
119 // crates, or
120 // - the current crate does not re-export generics
121 // (because the crate is a C library or executable)
122 cx.tcx.is_unreachable_local_definition(instance_def_id)
123 || !cx.tcx.local_crate_exports_generics()
124 } else {
125 // This is a monomorphization of a generic function
126 // defined in an upstream crate. It is hidden if:
127 // - it is instantiated in this crate, and
128 // - the current crate does not re-export generics
129 instance.upstream_monomorphization(tcx).is_none()
130 && !cx.tcx.local_crate_exports_generics()
131 }
132 }
133 } else {
134 // This is a non-generic function. It is hidden if:
135 // - it is instantiated in the local crate, and
136 // - it is defined an upstream crate (non-local), or
137 // - it is not reachable
138 cx.tcx.is_codegened_item(instance_def_id)
139 && (!instance_def_id.is_local()
140 || !cx.tcx.is_reachable_non_generic(instance_def_id))
141 };
142 if is_hidden {
143 llvm::set_visibility(llfn, llvm::Visibility::Hidden);
144 }
145
146 // MinGW: For backward compatibility we rely on the linker to decide whether it
147 // should use dllimport for functions.
148 if cx.use_dll_storage_attrs
149 && let Some(library) = tcx.native_library(instance_def_id)
150 && library.kind.is_dllimport()
151 && !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
152 {
153 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
154 }
155
156 if cx.should_assume_dso_local(llfn, true) {
157 llvm::LLVMRustSetDSOLocal(llfn, true);
158 }
159 }
160
161 llfn
162 };
163
164 cx.instances.borrow_mut().insert(instance, llfn);
165
166 llfn
167}