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_codegen_ssa::common;
8use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv};
9use rustc_middle::ty::{self, Instance, TypeVisitableExt};
10use rustc_target::spec::{Arch, Env};
11use tracing::debug;
1213use crate::context::CodegenCx;
14use crate::llvm::{self, Value};
1516/// 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 {
19let tcx = cx.tcx();
2021{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/callee.rs:21",
"rustc_codegen_llvm::callee", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/callee.rs"),
::tracing_core::__macro_support::Option::Some(21u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("get_fn(instance={0:?})",
instance) as &dyn Value))])
});
} else { ; }
};debug!("get_fn(instance={:?})", instance);
2223if !!instance.args.has_infer() {
::core::panicking::panic("assertion failed: !instance.args.has_infer()")
};assert!(!instance.args.has_infer());
24if !!instance.args.has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !instance.args.has_escaping_bound_vars()")
};assert!(!instance.args.has_escaping_bound_vars());
2526if let Some(&llfn) = cx.instances.borrow().get(&instance) {
27return llfn;
28 }
2930let sym = tcx.symbol_name(instance).name;
31{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/callee.rs:31",
"rustc_codegen_llvm::callee", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/callee.rs"),
::tracing_core::__macro_support::Option::Some(31u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("get_fn({0:?}: {1:?}) => {2}",
instance, instance.ty(cx.tcx(), cx.typing_env()), sym) as
&dyn Value))])
});
} else { ; }
};debug!("get_fn({:?}: {:?}) => {}", instance, instance.ty(cx.tcx(), cx.typing_env()), sym);
3233let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
3435let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
36llfn37 } else {
38let instance_def_id = instance.def_id();
39let llfn = if tcx.sess.target.arch == Arch::X8640 && 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.
46let mingw_gnu_toolchain = common::is_mingw_gnu_toolchain(&tcx.sess.target);
47let llfn = cx.declare_fn(
48&common::i686_decorated_name(
49dllimport,
50mingw_gnu_toolchain,
51true,
52 !mingw_gnu_toolchain,
53 ),
54fn_abi,
55Some(instance),
56 );
5758// 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.
69llvm::set_dllimport_storage_class(llfn);
70llfn71 } else {
72cx.declare_fn(sym, fn_abi, Some(instance))
73 };
74{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/callee.rs:74",
"rustc_codegen_llvm::callee", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/callee.rs"),
::tracing_core::__macro_support::Option::Some(74u32),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("get_fn: not casting pointer!")
as &dyn Value))])
});
} else { ; }
};debug!("get_fn: not casting pointer!");
7576// Apply an appropriate linkage/visibility value to our item that we
77 // just declared.
78 //
79 // This is sort of subtle. Inside our codegen unit we started off
80 // compilation by predefining all our own `MonoItem` instances. That
81 // is, everything we're codegenning ourselves is already defined. That
82 // means that anything we're actually codegenning in this codegen unit
83 // will have hit the above branch in `get_declared_value`. As a result,
84 // we're guaranteed here that we're declaring a symbol that won't get
85 // defined, or in other words we're referencing a value from another
86 // codegen unit or even another crate.
87 //
88 // So because this is a foreign value we blanket apply an external
89 // linkage directive because it's coming from a different object file.
90 // The visibility here is where it gets tricky. This symbol could be
91 // referencing some foreign crate or foreign library (an `extern`
92 // block) in which case we want to leave the default visibility. We may
93 // also, though, have multiple codegen units. It could be a
94 // monomorphization, in which case its expected visibility depends on
95 // whether we are sharing generics or not. The important thing here is
96 // that the visibility we apply to the declaration is the same one that
97 // has been applied to the definition (wherever that definition may be).
9899llvm::set_linkage(llfn, llvm::Linkage::ExternalLinkage);
100let is_generic = instance.args.non_erasable_generics().next().is_some();
101102let is_hidden = if is_generic {
103// This is a monomorphization of a generic function.
104if !(cx.tcx.sess.opts.share_generics()
105 || tcx.codegen_instance_attrs(instance.def).inline
106 == rustc_hir::attrs::InlineAttr::Never)
107 {
108// When not sharing generics, all instances are in the same
109 // crate and have hidden visibility.
110true
111} else {
112if let Some(instance_def_id) = instance_def_id.as_local() {
113// This is a monomorphization of a generic function
114 // defined in the current crate. It is hidden if:
115 // - the definition is unreachable for downstream
116 // crates, or
117 // - the current crate does not re-export generics
118 // (because the crate is a C library or executable)
119cx.tcx.is_unreachable_local_definition(instance_def_id)
120 || !cx.tcx.local_crate_exports_generics()
121 } else {
122// This is a monomorphization of a generic function
123 // defined in an upstream crate. It is hidden if:
124 // - it is instantiated in this crate, and
125 // - the current crate does not re-export generics
126instance.upstream_monomorphization(tcx).is_none()
127 && !cx.tcx.local_crate_exports_generics()
128 }
129 }
130 } else {
131// This is a non-generic function. It is hidden if:
132 // - it is instantiated in the local crate, and
133 // - it is defined an upstream crate (non-local), or
134 // - it is not reachable
135cx.tcx.is_codegened_item(instance_def_id)
136 && (!instance_def_id.is_local()
137 || !cx.tcx.is_reachable_non_generic(instance_def_id))
138 };
139if is_hidden {
140 llvm::set_visibility(llfn, llvm::Visibility::Hidden);
141 }
142143// MinGW: For backward compatibility we rely on the linker to decide whether it
144 // should use dllimport for functions.
145if cx.use_dll_storage_attrs
146 && let Some(library) = tcx.native_library(instance_def_id)
147 && library.kind.is_dllimport()
148 && !#[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)149 {
150 llvm::set_dllimport_storage_class(llfn);
151 }
152153cx.assume_dso_local(llfn, true);
154155llfn156 };
157158cx.instances.borrow_mut().insert(instance, llfn);
159160llfn161}