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.
67use rustc_attr_ir::InlineAttr;
8use rustc_codegen_ssa::common;
9use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv};
10use rustc_middle::ty::{self, Instance, TypeVisitableExt};
11use rustc_target::spec::{Arch, Env};
12use tracing::debug;
1314use crate::context::CodegenCx;
15use crate::llvm::{self, Value};
1617/// Codegens a reference to a fn/method item, monomorphizing and
18/// inlining as it goes.
19pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value {
20let tcx = cx.tcx();
2122{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/db8f076d2619ce2585b0380dda06e8da25a40da4/compiler/rustc_codegen_llvm/src/callee.rs:22",
"rustc_codegen_llvm::callee", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/db8f076d2619ce2585b0380dda06e8da25a40da4/compiler/rustc_codegen_llvm/src/callee.rs"),
::tracing_core::__macro_support::Option::Some(22u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::callee"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_fn(instance={0:?})",
instance) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_fn(instance={:?})", instance);
2324if !!instance.args.has_infer() {
::core::panicking::panic("assertion failed: !instance.args.has_infer()")
};assert!(!instance.args.has_infer());
25if !!instance.args.has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !instance.args.has_escaping_bound_vars()")
};assert!(!instance.args.has_escaping_bound_vars());
2627if let Some(&llfn) = cx.instances.borrow().get(&instance) {
28return llfn;
29 }
3031let sym = tcx.symbol_name(instance).name;
32{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/db8f076d2619ce2585b0380dda06e8da25a40da4/compiler/rustc_codegen_llvm/src/callee.rs:32",
"rustc_codegen_llvm::callee", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/db8f076d2619ce2585b0380dda06e8da25a40da4/compiler/rustc_codegen_llvm/src/callee.rs"),
::tracing_core::__macro_support::Option::Some(32u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::callee"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_fn({0:?}: {1:?}) => {2}",
instance, instance.ty(cx.tcx(), cx.typing_env()), sym) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_fn({:?}: {:?}) => {}", instance, instance.ty(cx.tcx(), cx.typing_env()), sym);
3334let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
3536let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
37llfn38 } else {
39let instance_def_id = instance.def_id();
40let llfn = if tcx.sess.target.arch == Arch::X8641 && let Some(dllimport) = crate::common::get_dllimport(tcx, instance_def_id, sym)
42 {
43// When calling functions in generated import libraries,
44 // LLVM/ar_archive_writer needs the fully decorated name
45 // (as would have been in the declaring object file), but dlltool
46 // wants the name as exported (as would be in the def file)
47 // which may be missing decorations.
48let using_dlltool = common::is_using_dlltool(&tcx.sess.target);
49let llfn = cx.declare_fn(
50&common::i686_decorated_name(dllimport, using_dlltool, true, !using_dlltool),
51fn_abi,
52Some(instance),
53 );
5455// Fix for https://github.com/rust-lang/rust/issues/104453
56 // On x86 Windows, LLVM uses 'L' as the prefix for any private
57 // global symbols, so when we create an undecorated function symbol
58 // that begins with an 'L' LLVM misinterprets that as a private
59 // global symbol that it created and so fails the compilation at a
60 // later stage since such a symbol must have a definition.
61 //
62 // To avoid this, we set the Storage Class to "DllImport" so that
63 // LLVM will prefix the name with `__imp_`. Ideally, we'd like the
64 // existing logic below to set the Storage Class, but it has an
65 // exemption for MinGW for backwards compatibility.
66llvm::set_dllimport_storage_class(llfn);
67llfn68 } else {
69cx.declare_fn(sym, fn_abi, Some(instance))
70 };
71{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/db8f076d2619ce2585b0380dda06e8da25a40da4/compiler/rustc_codegen_llvm/src/callee.rs:71",
"rustc_codegen_llvm::callee", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/db8f076d2619ce2585b0380dda06e8da25a40da4/compiler/rustc_codegen_llvm/src/callee.rs"),
::tracing_core::__macro_support::Option::Some(71u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::callee"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_fn: not casting pointer!")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_fn: not casting pointer!");
7273// Apply an appropriate linkage/visibility value to our item that we
74 // just declared.
75 //
76 // This is sort of subtle. Inside our codegen unit we started off
77 // compilation by predefining all our own `MonoItem` instances. That
78 // is, everything we're codegenning ourselves is already defined. That
79 // means that anything we're actually codegenning in this codegen unit
80 // will have hit the above branch in `get_declared_value`. As a result,
81 // we're guaranteed here that we're declaring a symbol that won't get
82 // defined, or in other words we're referencing a value from another
83 // codegen unit or even another crate.
84 //
85 // So because this is a foreign value we blanket apply an external
86 // linkage directive because it's coming from a different object file.
87 // The visibility here is where it gets tricky. This symbol could be
88 // referencing some foreign crate or foreign library (an `extern`
89 // block) in which case we want to leave the default visibility. We may
90 // also, though, have multiple codegen units. It could be a
91 // monomorphization, in which case its expected visibility depends on
92 // whether we are sharing generics or not. The important thing here is
93 // that the visibility we apply to the declaration is the same one that
94 // has been applied to the definition (wherever that definition may be).
9596llvm::set_linkage(llfn, llvm::Linkage::ExternalLinkage);
97let is_generic = instance.args.non_erasable_generics().next().is_some();
9899let is_hidden = if is_generic {
100// This is a monomorphization of a generic function.
101if !(cx.tcx.sess.opts.share_generics()
102 || tcx.codegen_instance_attrs(instance.def).inline == InlineAttr::Never)
103 {
104// When not sharing generics, all instances are in the same
105 // crate and have hidden visibility.
106true
107} else {
108if let Some(instance_def_id) = instance_def_id.as_local() {
109// This is a monomorphization of a generic function
110 // defined in the current crate. It is hidden if:
111 // - the definition is unreachable for downstream
112 // crates, or
113 // - the current crate does not re-export generics
114 // (because the crate is a C library or executable)
115cx.tcx.is_unreachable_local_definition(instance_def_id)
116 || !cx.tcx.local_crate_exports_generics()
117 } else {
118// This is a monomorphization of a generic function
119 // defined in an upstream crate. It is hidden if:
120 // - it is instantiated in this crate, and
121 // - the current crate does not re-export generics
122instance.upstream_monomorphization(tcx).is_none()
123 && !cx.tcx.local_crate_exports_generics()
124 }
125 }
126 } else {
127// This is a non-generic function. It is hidden if:
128 // - it is instantiated in the local crate, and
129 // - it is defined an upstream crate (non-local), or
130 // - it is not reachable
131cx.tcx.is_codegened_item(instance_def_id)
132 && (!instance_def_id.is_local()
133 || !cx.tcx.is_reachable_non_generic(instance_def_id))
134 };
135if is_hidden {
136 llvm::set_visibility(llfn, llvm::Visibility::Hidden);
137 }
138139// MinGW: For backward compatibility we rely on the linker to decide whether it
140 // should use dllimport for functions.
141if cx.use_dll_storage_attrs
142 && let Some(library) = tcx.native_library(instance_def_id)
143 && library.kind.is_dllimport()
144 && !#[allow(non_exhaustive_omitted_patterns)] match tcx.sess.target.env {
Env::Gnu | Env::Uclibc => true,
_ => false,
}matches!(tcx.sess.target.env, Env::Gnu | Env::Uclibc)145 {
146 llvm::set_dllimport_storage_class(llfn);
147 }
148149cx.assume_dso_local(llfn, true);
150151llfn152 };
153154cx.instances.borrow_mut().insert(instance, llfn);
155156llfn157}