2018-05-08 13:10:16 +00:00
|
|
|
//! Handles codegen of callees as well as other call-related
|
2019-02-08 13:53:55 +00:00
|
|
|
//! things. Callees are a superset of normal rust values and sometimes
|
|
|
|
//! have different representations. In particular, top-level fn items
|
2014-11-26 02:17:11 +00:00
|
|
|
//! and methods are represented as just a fn ptr and not a full
|
|
|
|
//! closure.
|
2012-08-28 22:54:45 +00:00
|
|
|
|
2019-02-17 18:58:58 +00:00
|
|
|
use crate::attributes;
|
2022-07-12 20:52:35 +00:00
|
|
|
use crate::common;
|
2019-02-17 18:58:58 +00:00
|
|
|
use crate::context::CodegenCx;
|
|
|
|
use crate::llvm;
|
|
|
|
use crate::value::Value;
|
2017-09-20 20:07:47 +00:00
|
|
|
|
2021-09-01 21:09:34 +00:00
|
|
|
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
|
2024-05-22 04:50:24 +00:00
|
|
|
use tracing::debug;
|
2014-12-01 14:23:40 +00:00
|
|
|
|
2018-05-08 13:10:16 +00:00
|
|
|
/// Codegens a reference to a fn/method item, monomorphizing and
|
2014-11-26 02:17:11 +00:00
|
|
|
/// inlining as it goes.
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
2018-01-05 05:04:08 +00:00
|
|
|
/// - `cx`: the crate context
|
2017-03-08 21:19:09 +00:00
|
|
|
/// - `instance`: the instance to be instantiated
|
2021-12-14 18:49:49 +00:00
|
|
|
pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value {
|
2018-09-20 13:47:22 +00:00
|
|
|
let tcx = cx.tcx();
|
2012-08-28 22:54:45 +00:00
|
|
|
|
2017-02-08 17:31:03 +00:00
|
|
|
debug!("get_fn(instance={:?})", instance);
|
2012-09-10 19:25:45 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
assert!(!instance.args.has_infer());
|
|
|
|
assert!(!instance.args.has_escaping_bound_vars());
|
2016-02-23 20:04:51 +00:00
|
|
|
|
2019-10-13 10:05:40 +00:00
|
|
|
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
|
2017-03-07 23:41:26 +00:00
|
|
|
return llfn;
|
2016-02-23 20:04:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:45:05 +00:00
|
|
|
let sym = tcx.symbol_name(instance).name;
|
2020-06-22 12:57:03 +00:00
|
|
|
debug!(
|
|
|
|
"get_fn({:?}: {:?}) => {}",
|
|
|
|
instance,
|
|
|
|
instance.ty(cx.tcx(), ty::ParamEnv::reveal_all()),
|
|
|
|
sym
|
|
|
|
);
|
2019-10-29 18:01:31 +00:00
|
|
|
|
2021-09-01 21:29:15 +00:00
|
|
|
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
|
2012-08-28 22:54:45 +00:00
|
|
|
|
2021-09-30 17:38:50 +00:00
|
|
|
let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
|
2022-12-06 05:07:28 +00:00
|
|
|
llfn
|
2014-05-29 05:26:56 +00:00
|
|
|
} else {
|
2022-07-12 20:52:35 +00:00
|
|
|
let instance_def_id = instance.def_id();
|
|
|
|
let llfn = if tcx.sess.target.arch == "x86"
|
|
|
|
&& let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym)
|
|
|
|
{
|
2022-11-16 23:34:12 +00:00
|
|
|
// Fix for https://github.com/rust-lang/rust/issues/104453
|
|
|
|
// On x86 Windows, LLVM uses 'L' as the prefix for any private
|
|
|
|
// global symbols, so when we create an undecorated function symbol
|
|
|
|
// that begins with an 'L' LLVM misinterprets that as a private
|
|
|
|
// global symbol that it created and so fails the compilation at a
|
|
|
|
// later stage since such a symbol must have a definition.
|
|
|
|
//
|
|
|
|
// To avoid this, we set the Storage Class to "DllImport" so that
|
|
|
|
// LLVM will prefix the name with `__imp_`. Ideally, we'd like the
|
|
|
|
// existing logic below to set the Storage Class, but it has an
|
2023-11-14 15:06:50 +00:00
|
|
|
// exemption for MinGW for backwards compatibility.
|
2023-05-03 22:22:24 +00:00
|
|
|
let llfn = cx.declare_fn(
|
|
|
|
&common::i686_decorated_name(
|
2023-11-21 19:07:32 +00:00
|
|
|
dllimport,
|
2023-05-03 22:22:24 +00:00
|
|
|
common::is_mingw_gnu_toolchain(&tcx.sess.target),
|
|
|
|
true,
|
2023-10-13 08:58:33 +00:00
|
|
|
),
|
2023-05-03 22:22:24 +00:00
|
|
|
fn_abi,
|
|
|
|
Some(instance),
|
|
|
|
);
|
2022-11-16 23:34:12 +00:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
|
|
|
|
}
|
|
|
|
llfn
|
2022-07-12 20:52:35 +00:00
|
|
|
} else {
|
2023-05-03 22:22:24 +00:00
|
|
|
cx.declare_fn(sym, fn_abi, Some(instance))
|
2022-07-12 20:52:35 +00:00
|
|
|
};
|
2016-02-23 20:04:51 +00:00
|
|
|
debug!("get_fn: not casting pointer!");
|
2016-04-05 10:01:00 +00:00
|
|
|
|
2020-03-31 12:27:09 +00:00
|
|
|
attributes::from_fn_attrs(cx, llfn, instance);
|
2016-08-17 19:50:55 +00:00
|
|
|
|
2017-09-13 22:24:13 +00:00
|
|
|
// Apply an appropriate linkage/visibility value to our item that we
|
|
|
|
// just declared.
|
|
|
|
//
|
|
|
|
// This is sort of subtle. Inside our codegen unit we started off
|
2018-05-08 13:10:16 +00:00
|
|
|
// compilation by predefining all our own `MonoItem` instances. That
|
|
|
|
// is, everything we're codegenning ourselves is already defined. That
|
|
|
|
// means that anything we're actually codegenning in this codegen unit
|
2018-03-14 15:45:06 +00:00
|
|
|
// will have hit the above branch in `get_declared_value`. As a result,
|
|
|
|
// we're guaranteed here that we're declaring a symbol that won't get
|
|
|
|
// defined, or in other words we're referencing a value from another
|
|
|
|
// codegen unit or even another crate.
|
2017-09-13 22:24:13 +00:00
|
|
|
//
|
|
|
|
// So because this is a foreign value we blanket apply an external
|
|
|
|
// linkage directive because it's coming from a different object file.
|
|
|
|
// The visibility here is where it gets tricky. This symbol could be
|
|
|
|
// referencing some foreign crate or foreign library (an `extern`
|
|
|
|
// block) in which case we want to leave the default visibility. We may
|
2018-03-14 15:45:06 +00:00
|
|
|
// also, though, have multiple codegen units. It could be a
|
|
|
|
// monomorphization, in which case its expected visibility depends on
|
|
|
|
// whether we are sharing generics or not. The important thing here is
|
|
|
|
// that the visibility we apply to the declaration is the same one that
|
|
|
|
// has been applied to the definition (wherever that definition may be).
|
2017-07-12 15:37:58 +00:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
|
|
|
|
|
2023-09-13 13:55:23 +00:00
|
|
|
let is_generic =
|
|
|
|
instance.args.non_erasable_generics(tcx, instance.def_id()).next().is_some();
|
2018-03-06 13:44:14 +00:00
|
|
|
|
|
|
|
if is_generic {
|
2018-03-14 15:45:06 +00:00
|
|
|
// This is a monomorphization. Its expected visibility depends
|
|
|
|
// on whether we are in share-generics mode.
|
|
|
|
|
2018-07-26 19:20:47 +00:00
|
|
|
if cx.tcx.sess.opts.share_generics() {
|
2018-03-06 13:44:14 +00:00
|
|
|
// We are in share_generics mode.
|
|
|
|
|
2020-04-12 12:45:41 +00:00
|
|
|
if let Some(instance_def_id) = instance_def_id.as_local() {
|
2018-03-06 13:44:14 +00:00
|
|
|
// This is a definition from the current crate. If the
|
|
|
|
// definition is unreachable for downstream crates or
|
|
|
|
// the current crate does not re-export generics, the
|
2018-03-14 15:45:06 +00:00
|
|
|
// definition of the instance will have been declared
|
|
|
|
// as `hidden`.
|
2018-03-06 13:44:14 +00:00
|
|
|
if cx.tcx.is_unreachable_local_definition(instance_def_id)
|
|
|
|
|| !cx.tcx.local_crate_exports_generics()
|
|
|
|
{
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-14 15:45:06 +00:00
|
|
|
// This is a monomorphization of a generic function
|
|
|
|
// defined in an upstream crate.
|
2020-01-22 11:17:21 +00:00
|
|
|
if instance.upstream_monomorphization(tcx).is_some() {
|
2018-03-14 15:45:06 +00:00
|
|
|
// This is instantiated in another crate. It cannot
|
|
|
|
// be `hidden`.
|
2018-03-06 13:44:14 +00:00
|
|
|
} else {
|
|
|
|
// This is a local instantiation of an upstream definition.
|
2018-03-14 15:45:06 +00:00
|
|
|
// If the current crate does not re-export it
|
|
|
|
// (because it is a C library or an executable), it
|
|
|
|
// will have been declared `hidden`.
|
2018-03-06 13:44:14 +00:00
|
|
|
if !cx.tcx.local_crate_exports_generics() {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When not sharing generics, all instances are in the same
|
|
|
|
// crate and have hidden visibility
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
2018-03-06 09:33:42 +00:00
|
|
|
} else {
|
2018-03-06 13:44:14 +00:00
|
|
|
// This is a non-generic function
|
2018-05-08 13:10:16 +00:00
|
|
|
if cx.tcx.is_codegened_item(instance_def_id) {
|
2018-03-06 13:44:14 +00:00
|
|
|
// This is a function that is instantiated in the local crate
|
|
|
|
|
2018-03-06 09:33:42 +00:00
|
|
|
if instance_def_id.is_local() {
|
2018-03-06 13:44:14 +00:00
|
|
|
// This is function that is defined in the local crate.
|
|
|
|
// If it is not reachable, it is hidden.
|
2018-03-06 09:33:42 +00:00
|
|
|
if !cx.tcx.is_reachable_non_generic(instance_def_id) {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-06 13:44:14 +00:00
|
|
|
// This is a function from an upstream crate that has
|
|
|
|
// been instantiated here. These are always hidden.
|
2017-07-12 15:37:58 +00:00
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 19:50:55 +00:00
|
|
|
}
|
2017-04-14 14:51:22 +00:00
|
|
|
|
2021-03-27 22:11:24 +00:00
|
|
|
// MinGW: For backward compatibility we rely on the linker to decide whether it
|
|
|
|
// should use dllimport for functions.
|
|
|
|
if cx.use_dll_storage_attrs
|
2022-10-18 14:55:32 +00:00
|
|
|
&& let Some(library) = tcx.native_library(instance_def_id)
|
|
|
|
&& library.kind.is_dllimport()
|
2021-09-17 21:18:11 +00:00
|
|
|
&& !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
|
2021-03-27 22:11:24 +00:00
|
|
|
{
|
2017-09-28 09:58:45 +00:00
|
|
|
llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
|
2016-11-24 00:09:51 +00:00
|
|
|
}
|
2021-03-27 22:11:24 +00:00
|
|
|
|
2021-05-25 21:54:07 +00:00
|
|
|
if cx.should_assume_dso_local(llfn, true) {
|
2021-03-27 22:11:24 +00:00
|
|
|
llvm::LLVMRustSetDSOLocal(llfn, true);
|
|
|
|
}
|
2017-09-28 09:58:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 20:04:51 +00:00
|
|
|
llfn
|
|
|
|
};
|
|
|
|
|
2018-01-05 05:04:08 +00:00
|
|
|
cx.instances.borrow_mut().insert(instance, llfn);
|
2013-05-21 19:25:44 +00:00
|
|
|
|
2017-03-07 23:41:26 +00:00
|
|
|
llfn
|
2012-08-28 22:54:45 +00:00
|
|
|
}
|