2015-02-28 23:53:12 +02:00
|
|
|
//! Set and unset common attributes on LLVM values.
|
|
|
|
|
2018-08-07 16:03:57 +02:00
|
|
|
use std::ffi::CString;
|
2016-11-29 19:02:00 -05:00
|
|
|
|
2021-02-13 22:17:15 +11:00
|
|
|
use cstr::cstr;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2018-02-10 14:28:17 -08:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2020-10-09 19:35:17 +02:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
|
|
|
use rustc_middle::ty::layout::HasTyCtxt;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
2020-03-31 08:27:09 -04:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2020-06-14 00:00:00 +00:00
|
|
|
use rustc_session::config::{OptLevel, SanitizerSet};
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::Session;
|
2021-01-09 16:54:20 +02:00
|
|
|
use rustc_target::spec::StackProbeType;
|
2017-06-21 12:08:18 -07:00
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::attributes;
|
|
|
|
use crate::llvm::AttributePlace::Function;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::llvm::{self, Attribute};
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::llvm_util;
|
2020-10-08 23:23:27 +01:00
|
|
|
pub use rustc_attr::{InlineAttr, InstructionSetAttr, OptimizeAttr};
|
2018-07-10 13:28:39 +03:00
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::context::CodegenCx;
|
|
|
|
use crate::value::Value;
|
2015-02-28 23:53:12 +02:00
|
|
|
|
|
|
|
/// Mark LLVM function to use provided inline heuristic.
|
|
|
|
#[inline]
|
2020-11-20 00:00:00 +00:00
|
|
|
fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr) {
|
2015-03-04 01:03:25 +02:00
|
|
|
use self::InlineAttr::*;
|
2015-02-28 23:53:12 +02:00
|
|
|
match inline {
|
2019-12-22 17:42:04 -05:00
|
|
|
Hint => Attribute::InlineHint.apply_llfn(Function, val),
|
2016-08-03 00:25:19 +03:00
|
|
|
Always => Attribute::AlwaysInline.apply_llfn(Function, val),
|
2019-12-22 17:42:04 -05:00
|
|
|
Never => {
|
2020-10-15 11:44:00 +02:00
|
|
|
if cx.tcx().sess.target.arch != "amdgpu" {
|
2018-07-18 22:04:27 -05:00
|
|
|
Attribute::NoInline.apply_llfn(Function, val);
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-11-17 00:00:00 +00:00
|
|
|
None => {}
|
2015-02-28 23:53:12 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-16 00:00:00 +00:00
|
|
|
/// Apply LLVM sanitize attributes.
|
|
|
|
#[inline]
|
2020-06-14 00:00:00 +00:00
|
|
|
pub fn sanitize(cx: &CodegenCx<'ll, '_>, no_sanitize: SanitizerSet, llfn: &'ll Value) {
|
|
|
|
let enabled = cx.tcx.sess.opts.debugging_opts.sanitizer - no_sanitize;
|
|
|
|
if enabled.contains(SanitizerSet::ADDRESS) {
|
|
|
|
llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
if enabled.contains(SanitizerSet::MEMORY) {
|
|
|
|
llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
if enabled.contains(SanitizerSet::THREAD) {
|
|
|
|
llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
|
2020-01-16 00:00:00 +00:00
|
|
|
}
|
2021-01-22 18:32:38 -08:00
|
|
|
if enabled.contains(SanitizerSet::HWADDRESS) {
|
|
|
|
llvm::Attribute::SanitizeHWAddress.apply_llfn(Function, llfn);
|
|
|
|
}
|
2020-01-16 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 23:53:12 +02:00
|
|
|
/// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
|
|
|
|
#[inline]
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn emit_uwtable(val: &'ll Value, emit: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::UWTable.toggle_llfn(Function, val, emit);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Tell LLVM if this function should be 'naked', i.e., skip the epilogue and prologue.
|
2016-03-21 21:01:08 +01:00
|
|
|
#[inline]
|
2019-11-27 12:55:33 +02:00
|
|
|
fn naked(val: &'ll Value, is_naked: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::Naked.toggle_llfn(Function, val, is_naked);
|
2016-03-21 21:01:08 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn set_frame_pointer_elimination(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
|
2018-01-05 07:04:08 +02:00
|
|
|
if cx.sess().must_not_eliminate_frame_pointers() {
|
2020-04-14 12:10:58 -07:00
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("frame-pointer"),
|
|
|
|
cstr!("all"),
|
2020-04-14 12:10:58 -07:00
|
|
|
);
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 12:10:43 -07:00
|
|
|
}
|
2016-05-27 13:27:56 -04:00
|
|
|
}
|
|
|
|
|
2018-12-30 11:59:03 -08:00
|
|
|
/// Tell LLVM what instrument function to insert.
|
|
|
|
#[inline]
|
2019-11-27 12:55:33 +02:00
|
|
|
fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
|
2018-12-30 11:59:03 -08:00
|
|
|
if cx.sess().instrument_mcount() {
|
|
|
|
// Similar to `clang -pg` behavior. Handled by the
|
|
|
|
// `post-inline-ee-instrument` LLVM pass.
|
2019-03-29 06:44:31 +09:00
|
|
|
|
|
|
|
// The function name varies on platforms.
|
|
|
|
// See test/CodeGen/mcount.c in clang.
|
2020-11-08 14:57:55 +03:00
|
|
|
let mcount_name = CString::new(cx.sess().target.mcount.as_str().as_bytes()).unwrap();
|
2019-03-29 06:44:31 +09:00
|
|
|
|
2018-12-30 11:59:03 -08:00
|
|
|
llvm::AddFunctionAttrStringValue(
|
2019-12-22 17:42:04 -05:00
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("instrument-function-entry-inlined"),
|
2019-12-22 17:42:04 -05:00
|
|
|
&mcount_name,
|
|
|
|
);
|
2018-12-30 11:59:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:55:33 +02:00
|
|
|
fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
|
2017-06-21 12:08:18 -07:00
|
|
|
// Currently stack probes seem somewhat incompatible with the address
|
2019-10-04 00:00:00 +00:00
|
|
|
// sanitizer and thread sanitizer. With asan we're already protected from
|
|
|
|
// stack overflow anyway so we don't really need stack probes regardless.
|
2020-06-14 00:00:00 +00:00
|
|
|
if cx
|
|
|
|
.sess()
|
|
|
|
.opts
|
|
|
|
.debugging_opts
|
|
|
|
.sanitizer
|
|
|
|
.intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD)
|
|
|
|
{
|
|
|
|
return;
|
2017-06-21 12:08:18 -07:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:48:03 +02:00
|
|
|
// probestack doesn't play nice either with `-C profile-generate`.
|
|
|
|
if cx.sess().opts.cg.profile_generate.enabled() {
|
2018-02-19 01:57:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-20 22:07:55 +01:00
|
|
|
// probestack doesn't play nice either with gcov profiling.
|
|
|
|
if cx.sess().opts.debugging_opts.profile {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-09 16:54:20 +02:00
|
|
|
let attr_value = match cx.sess().target.stack_probes {
|
|
|
|
StackProbeType::None => None,
|
|
|
|
// Request LLVM to generate the probes inline. If the given LLVM version does not support
|
|
|
|
// this, no probe is generated at all (even if the attribute is specified).
|
2021-02-13 22:17:15 +11:00
|
|
|
StackProbeType::Inline => Some(cstr!("inline-asm")),
|
2021-01-09 16:54:20 +02:00
|
|
|
// Flag our internal `__rust_probestack` function as the stack probe symbol.
|
|
|
|
// This is defined in the `compiler-builtins` crate for each architecture.
|
2021-02-13 22:17:15 +11:00
|
|
|
StackProbeType::Call => Some(cstr!("__rust_probestack")),
|
2021-01-09 16:54:20 +02:00
|
|
|
// Pick from the two above based on the LLVM version.
|
|
|
|
StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
|
|
|
|
if llvm_util::get_version() < min_llvm_version_for_inline {
|
2021-02-13 22:17:15 +11:00
|
|
|
Some(cstr!("__rust_probestack"))
|
2021-01-09 16:54:20 +02:00
|
|
|
} else {
|
2021-02-13 22:17:15 +11:00
|
|
|
Some(cstr!("inline-asm"))
|
2021-01-09 16:54:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(attr_value) = attr_value {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("probe-stack"),
|
2021-01-09 16:54:20 +02:00
|
|
|
attr_value,
|
|
|
|
);
|
|
|
|
}
|
2017-06-21 12:08:18 -07:00
|
|
|
}
|
|
|
|
|
2018-08-22 17:48:32 +02:00
|
|
|
pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
|
2018-12-07 13:51:03 -05:00
|
|
|
let target_cpu = SmallCStr::new(llvm_util::target_cpu(cx.tcx.sess));
|
2018-08-02 18:10:26 +02:00
|
|
|
llvm::AddFunctionAttrStringValue(
|
2019-12-22 17:42:04 -05:00
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("target-cpu"),
|
2019-12-22 17:42:04 -05:00
|
|
|
target_cpu.as_c_str(),
|
|
|
|
);
|
2018-08-02 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 17:39:26 +08:00
|
|
|
pub fn apply_tune_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
|
|
|
|
if let Some(tune) = llvm_util::tune_cpu(cx.tcx.sess) {
|
|
|
|
let tune_cpu = SmallCStr::new(tune);
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("tune-cpu"),
|
2020-09-17 17:39:26 +08:00
|
|
|
tune_cpu.as_c_str(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 19:19:55 +03:00
|
|
|
/// Sets the `NonLazyBind` LLVM attribute on a given function,
|
|
|
|
/// assuming the codegen options allow skipping the PLT.
|
|
|
|
pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
|
|
|
|
// Don't generate calls through PLT if it's not necessary
|
|
|
|
if !sess.needs_plt() {
|
|
|
|
Attribute::NonLazyBind.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:37:52 +02:00
|
|
|
pub(crate) fn default_optimisation_attrs(sess: &Session, llfn: &'ll Value) {
|
|
|
|
match sess.opts.optimize {
|
|
|
|
OptLevel::Size => {
|
|
|
|
llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-01-19 00:37:52 +02:00
|
|
|
OptLevel::SizeMin => {
|
|
|
|
llvm::Attribute::MinSize.apply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
OptLevel::No => {
|
|
|
|
llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeForSize.unapply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
|
2016-05-27 13:27:56 -04:00
|
|
|
/// attributes.
|
2020-03-31 08:27:09 -04:00
|
|
|
pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::Instance<'tcx>) {
|
2019-11-27 12:53:19 +02:00
|
|
|
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
|
2018-01-30 22:39:23 -05:00
|
|
|
|
2018-10-27 15:29:06 +03:00
|
|
|
match codegen_fn_attrs.optimize {
|
|
|
|
OptimizeAttr::None => {
|
2019-01-19 00:37:52 +02:00
|
|
|
default_optimisation_attrs(cx.tcx.sess, llfn);
|
2018-10-27 15:29:06 +03:00
|
|
|
}
|
|
|
|
OptimizeAttr::Speed => {
|
|
|
|
llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeForSize.unapply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
OptimizeAttr::Size => {
|
|
|
|
llvm::Attribute::MinSize.apply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
|
|
|
|
llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 00:00:00 +00:00
|
|
|
let inline_attr = if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
|
|
|
InlineAttr::Never
|
|
|
|
} else if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
|
|
|
|
InlineAttr::Hint
|
|
|
|
} else {
|
|
|
|
codegen_fn_attrs.inline
|
|
|
|
};
|
|
|
|
inline(cx, llfn, inline_attr);
|
2018-11-18 20:51:56 +02:00
|
|
|
|
2018-08-16 13:19:04 -07:00
|
|
|
// The `uwtable` attribute according to LLVM is:
|
|
|
|
//
|
|
|
|
// This attribute indicates that the ABI being targeted requires that an
|
|
|
|
// unwind table entry be produced for this function even if we can show
|
|
|
|
// that no exceptions passes by it. This is normally the case for the
|
|
|
|
// ELF x86-64 abi, but it can be disabled for some compilation units.
|
|
|
|
//
|
|
|
|
// Typically when we're compiling with `-C panic=abort` (which implies this
|
|
|
|
// `no_landing_pads` check) we don't need `uwtable` because we can't
|
|
|
|
// generate any exceptions! On Windows, however, exceptions include other
|
|
|
|
// events such as illegal instructions, segfaults, etc. This means that on
|
|
|
|
// Windows we end up still needing the `uwtable` attribute even if the `-C
|
|
|
|
// panic=abort` flag is passed.
|
|
|
|
//
|
2020-07-07 11:12:44 -04:00
|
|
|
// You can also find more info on why Windows always requires uwtables here:
|
2018-08-16 13:19:04 -07:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1302078
|
Add Option to Force Unwind Tables
When panic != unwind, `nounwind` is added to all functions for a target.
This can cause issues when a panic happens with RUST_BACKTRACE=1, as
there needs to be a way to reconstruct the backtrace. There are three
possible sources of this information: forcing frame pointers (for which
an option exists already), debug info (for which an option exists), or
unwind tables.
Especially for embedded devices, forcing frame pointers can have code
size overheads (RISC-V sees ~10% overheads, ARM sees ~2-3% overheads).
In code, it can be the case that debug info is not kept, so it is useful
to provide this third option, unwind tables, that users can use to
reconstruct the call stack. Reconstructing this stack is harder than
with frame pointers, but it is still possible.
This commit adds a compiler option which allows a user to force the
addition of unwind tables. Unwind tables cannot be disabled on targets
that require them for correctness, or when using `-C panic=unwind`.
2020-05-04 12:08:35 +01:00
|
|
|
if cx.sess().must_emit_unwind_tables() {
|
2018-08-16 13:19:04 -07:00
|
|
|
attributes::emit_uwtable(llfn, true);
|
|
|
|
}
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
set_frame_pointer_elimination(cx, llfn);
|
2018-12-30 11:59:03 -08:00
|
|
|
set_instrument_function(cx, llfn);
|
2018-01-05 07:04:08 +02:00
|
|
|
set_probestack(cx, llfn);
|
2018-01-05 13:26:26 -08:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
|
2018-01-15 20:08:09 -05:00
|
|
|
Attribute::Cold.apply_llfn(Function, llfn);
|
|
|
|
}
|
2019-02-09 15:55:30 +01:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_RETURNS_TWICE) {
|
|
|
|
Attribute::ReturnsTwice.apply_llfn(Function, llfn);
|
|
|
|
}
|
2020-04-14 00:19:46 +02:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
|
|
|
|
Attribute::ReadOnly.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
|
|
|
|
Attribute::ReadNone.apply_llfn(Function, llfn);
|
|
|
|
}
|
2018-05-08 16:10:16 +03:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
2018-01-15 20:08:09 -05:00
|
|
|
naked(llfn, true);
|
|
|
|
}
|
2018-05-08 16:10:16 +03:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
|
2018-01-15 20:08:09 -05:00
|
|
|
}
|
2020-09-28 21:10:38 +01:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
|
2021-02-13 22:17:15 +11:00
|
|
|
llvm::AddFunctionAttrString(llfn, Function, cstr!("cmse_nonsecure_entry"));
|
2020-09-28 21:10:38 +01:00
|
|
|
}
|
2020-06-14 00:00:00 +00:00
|
|
|
sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
|
2018-05-24 12:03:05 -07:00
|
|
|
|
2018-08-02 18:10:26 +02:00
|
|
|
// Always annotate functions with the target-cpu they are compiled for.
|
|
|
|
// Without this, ThinLTO won't inline Rust functions into Clang generated
|
|
|
|
// functions (because Clang annotates functions this way too).
|
2018-12-07 13:51:03 -05:00
|
|
|
apply_target_cpu_attr(cx, llfn);
|
2020-09-17 17:39:26 +08:00
|
|
|
// tune-cpu is only conveyed through the attribute for our purpose.
|
|
|
|
// The target doesn't care; the subtarget reads our attribute.
|
|
|
|
apply_tune_cpu_attr(cx, llfn);
|
2018-08-02 18:10:26 +02:00
|
|
|
|
2021-03-13 15:29:39 +02:00
|
|
|
let function_features = codegen_fn_attrs
|
|
|
|
.target_features
|
|
|
|
.iter()
|
|
|
|
.map(|f| {
|
2019-12-22 17:42:04 -05:00
|
|
|
let feature = &f.as_str();
|
|
|
|
format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
|
2021-03-13 15:29:39 +02:00
|
|
|
})
|
2020-10-08 23:23:27 +01:00
|
|
|
.chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
|
|
|
|
InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
|
|
|
|
InstructionSetAttr::ArmT32 => "+thumb-mode".to_string(),
|
|
|
|
}))
|
2021-03-13 15:29:39 +02:00
|
|
|
.collect::<Vec<String>>();
|
|
|
|
if !function_features.is_empty() {
|
|
|
|
let mut global_features = llvm_util::llvm_global_features(cx.tcx.sess);
|
|
|
|
global_features.extend(function_features.into_iter());
|
|
|
|
let features = global_features.join(",");
|
2018-02-27 20:12:32 -05:00
|
|
|
let val = CString::new(features).unwrap();
|
2016-11-29 19:02:00 -05:00
|
|
|
llvm::AddFunctionAttrStringValue(
|
2019-12-22 17:42:04 -05:00
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("target-features"),
|
2019-12-22 17:42:04 -05:00
|
|
|
&val,
|
|
|
|
);
|
2016-11-29 19:02:00 -05:00
|
|
|
}
|
2018-02-10 14:28:17 -08:00
|
|
|
|
|
|
|
// Note that currently the `wasm-import-module` doesn't do anything, but
|
|
|
|
// eventually LLVM 7 should read this and ferry the appropriate import
|
|
|
|
// module to the output file.
|
2020-10-15 11:44:00 +02:00
|
|
|
if cx.tcx.sess.target.arch == "wasm32" {
|
2019-11-27 12:53:19 +02:00
|
|
|
if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("wasm-import-module"),
|
2019-11-27 12:53:19 +02:00
|
|
|
&module,
|
|
|
|
);
|
2019-12-16 14:15:57 -08:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let name =
|
|
|
|
codegen_fn_attrs.link_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
|
2019-12-16 14:15:57 -08:00
|
|
|
let name = CString::new(&name.as_str()[..]).unwrap();
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2021-02-13 22:17:15 +11:00
|
|
|
cstr!("wasm-import-name"),
|
2019-12-16 14:15:57 -08:00
|
|
|
&name,
|
|
|
|
);
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 19:02:00 -05:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:17 +02:00
|
|
|
pub fn provide_both(providers: &mut Providers) {
|
2018-02-10 14:28:17 -08:00
|
|
|
providers.wasm_import_module_map = |tcx, cnum| {
|
2020-05-18 00:18:50 +03:00
|
|
|
// Build up a map from DefId to a `NativeLib` structure, where
|
|
|
|
// `NativeLib` internally contains information about
|
2018-07-16 11:31:14 -07:00
|
|
|
// `#[link(wasm_import_module = "...")]` for example.
|
|
|
|
let native_libs = tcx.native_libraries(cnum);
|
2018-10-08 16:55:04 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let def_id_to_native_lib = native_libs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
|
|
|
|
.collect::<FxHashMap<_, _>>();
|
2018-07-16 11:31:14 -07:00
|
|
|
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut ret = FxHashMap::default();
|
2020-10-27 15:01:03 +01:00
|
|
|
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
|
|
|
|
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
|
2018-02-10 14:28:17 -08:00
|
|
|
let module = match module {
|
|
|
|
Some(s) => s,
|
|
|
|
None => continue,
|
|
|
|
};
|
2018-10-08 16:55:04 +02:00
|
|
|
ret.extend(lib.foreign_items.iter().map(|id| {
|
2018-02-10 14:28:17 -08:00
|
|
|
assert_eq!(id.krate, cnum);
|
2018-10-08 16:55:04 +02:00
|
|
|
(*id, module.to_string())
|
|
|
|
}));
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
ret
|
2018-07-16 11:31:14 -07:00
|
|
|
};
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<CString> {
|
2019-12-22 17:42:04 -05:00
|
|
|
tcx.wasm_import_module_map(id.krate).get(&id).map(|s| CString::new(&s[..]).unwrap())
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|