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
|
|
|
|
2019-12-24 05:30:02 +01:00
|
|
|
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::session::config::{OptLevel, Sanitizer};
|
2018-04-23 15:17:07 -07:00
|
|
|
use rustc::session::Session;
|
2018-07-18 22:04:27 -05:00
|
|
|
use rustc::ty::layout::HasTyCtxt;
|
2018-06-13 16:44:43 +03:00
|
|
|
use rustc::ty::query::Providers;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
|
|
|
use rustc_codegen_ssa::traits::*;
|
|
|
|
use rustc_data_structures::const_cstr;
|
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-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
2019-11-27 13:23:30 +02:00
|
|
|
use rustc_target::abi::call::Conv;
|
2018-05-24 12:03:05 -07:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2017-06-21 12:08:18 -07:00
|
|
|
|
2019-11-27 13:23:30 +02:00
|
|
|
use crate::abi::FnAbi;
|
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-01-11 13:15:20 +01:00
|
|
|
pub use rustc_attr::{self as attr, InlineAttr, 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]
|
2019-11-27 12:55:33 +02: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 => {
|
2018-07-18 22:04:27 -05:00
|
|
|
if cx.tcx().sess.target.target.arch != "amdgpu" {
|
|
|
|
Attribute::NoInline.apply_llfn(Function, val);
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
None => {
|
2016-11-16 23:36:08 +01:00
|
|
|
Attribute::InlineHint.unapply_llfn(Function, val);
|
|
|
|
Attribute::AlwaysInline.unapply_llfn(Function, val);
|
|
|
|
Attribute::NoInline.unapply_llfn(Function, val);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2015-02-28 23:53:12 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-16 00:00:00 +00:00
|
|
|
/// Apply LLVM sanitize attributes.
|
|
|
|
#[inline]
|
|
|
|
pub fn sanitize(cx: &CodegenCx<'ll, '_>, codegen_fn_flags: CodegenFnAttrFlags, llfn: &'ll Value) {
|
|
|
|
if let Some(ref sanitizer) = cx.tcx.sess.opts.debugging_opts.sanitizer {
|
|
|
|
match *sanitizer {
|
|
|
|
Sanitizer::Address => {
|
|
|
|
if !codegen_fn_flags.contains(CodegenFnAttrFlags::NO_SANITIZE_ADDRESS) {
|
|
|
|
llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Sanitizer::Memory => {
|
|
|
|
if !codegen_fn_flags.contains(CodegenFnAttrFlags::NO_SANITIZE_MEMORY) {
|
|
|
|
llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Sanitizer::Thread => {
|
|
|
|
if !codegen_fn_flags.contains(CodegenFnAttrFlags::NO_SANITIZE_THREAD) {
|
|
|
|
llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Sanitizer::Leak => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell LLVM whether the function can or cannot unwind.
|
|
|
|
#[inline]
|
2018-11-15 06:17:58 -08:00
|
|
|
fn unwind(val: &'ll Value, can_unwind: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
|
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() {
|
2019-12-30 21:35:13 -08:00
|
|
|
if llvm_util::get_major_version() >= 8 {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
const_cstr!("frame-pointer"),
|
|
|
|
const_cstr!("all"),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
const_cstr!("no-frame-pointer-elim"),
|
|
|
|
const_cstr!("true"),
|
|
|
|
);
|
|
|
|
}
|
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.
|
2019-12-22 17:42:04 -05:00
|
|
|
let mcount_name =
|
|
|
|
CString::new(cx.sess().target.target.options.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,
|
|
|
|
const_cstr!("instrument-function-entry-inlined"),
|
|
|
|
&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
|
|
|
// Only use stack probes if the target specification indicates that we
|
|
|
|
// should be using stack probes
|
2018-01-05 07:04:08 +02:00
|
|
|
if !cx.sess().target.target.options.stack_probes {
|
2019-12-22 17:42:04 -05:00
|
|
|
return;
|
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.
|
|
|
|
match cx.sess().opts.debugging_opts.sanitizer {
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Sanitizer::Address) | Some(Sanitizer::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;
|
|
|
|
}
|
|
|
|
|
2017-06-21 12:08:18 -07:00
|
|
|
// Flag our internal `__rust_probestack` function as the stack probe symbol.
|
|
|
|
// This is defined in the `compiler-builtins` crate for each architecture.
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
2019-12-22 17:42:04 -05:00
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
const_cstr!("probe-stack"),
|
|
|
|
const_cstr!("__rust_probestack"),
|
|
|
|
);
|
2017-06-21 12:08:18 -07:00
|
|
|
}
|
|
|
|
|
2019-07-07 17:44:00 +02:00
|
|
|
fn translate_obsolete_target_features(feature: &str) -> &str {
|
2019-12-22 17:42:04 -05:00
|
|
|
const LLVM9_FEATURE_CHANGES: &[(&str, &str)] =
|
|
|
|
&[("+fp-only-sp", "-fp64"), ("-fp-only-sp", "+fp64"), ("+d16", "-d32"), ("-d16", "+d32")];
|
2019-07-07 17:44:00 +02:00
|
|
|
if llvm_util::get_major_version() >= 9 {
|
|
|
|
for &(old, new) in LLVM9_FEATURE_CHANGES {
|
|
|
|
if feature == old {
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for &(old, new) in LLVM9_FEATURE_CHANGES {
|
|
|
|
if feature == new {
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
feature
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:17:07 -07:00
|
|
|
pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
|
2019-12-22 17:42:04 -05:00
|
|
|
const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
|
2018-04-23 15:17:07 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let cmdline = sess
|
|
|
|
.opts
|
|
|
|
.cg
|
|
|
|
.target_feature
|
|
|
|
.split(',')
|
2018-04-23 15:17:07 -07:00
|
|
|
.filter(|f| !RUSTC_SPECIFIC_FEATURES.iter().any(|s| f.contains(s)));
|
2019-12-22 17:42:04 -05:00
|
|
|
sess.target
|
|
|
|
.target
|
|
|
|
.options
|
|
|
|
.features
|
|
|
|
.split(',')
|
2018-04-23 15:17:07 -07:00
|
|
|
.chain(cmdline)
|
|
|
|
.filter(|l| !l.is_empty())
|
2019-07-07 17:44:00 +02:00
|
|
|
.map(translate_obsolete_target_features)
|
2018-04-23 15:17:07 -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,
|
|
|
|
const_cstr!("target-cpu"),
|
|
|
|
target_cpu.as_c_str(),
|
|
|
|
);
|
2018-08-02 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2018-08-16 13:19:04 -07:00
|
|
|
pub fn from_fn_attrs(
|
2018-11-15 06:17:58 -08:00
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
2018-08-16 13:19:04 -07:00
|
|
|
llfn: &'ll Value,
|
2019-11-27 12:53:19 +02:00
|
|
|
instance: ty::Instance<'tcx>,
|
2019-11-27 13:23:30 +02:00
|
|
|
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
|
2018-08-16 13:19:04 -07:00
|
|
|
) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:53:19 +02:00
|
|
|
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
|
2020-01-20 15:54:40 +01:00
|
|
|
if instance.def.requires_inline(cx.tcx) {
|
2019-11-27 12:53:19 +02:00
|
|
|
inline(cx, llfn, attributes::InlineAttr::Hint);
|
|
|
|
}
|
|
|
|
|
2018-11-18 20:51:56 +02:00
|
|
|
inline(cx, llfn, codegen_fn_attrs.inline);
|
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// You can also find more info on why Windows is whitelisted here in:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1302078
|
2019-12-22 17:42:04 -05:00
|
|
|
if !cx.sess().no_landing_pads() || cx.sess().target.target.options.requires_uwtable {
|
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);
|
|
|
|
}
|
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-01-16 00:00:00 +00:00
|
|
|
sanitize(cx, codegen_fn_attrs.flags, llfn);
|
2018-05-24 12:03:05 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
unwind(
|
|
|
|
llfn,
|
|
|
|
if cx.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
|
|
|
|
// In panic=abort mode we assume nothing can unwind anywhere, so
|
|
|
|
// optimize based on this!
|
|
|
|
false
|
|
|
|
} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::UNWIND) {
|
|
|
|
// If a specific #[unwind] attribute is present, use that.
|
2018-11-15 06:17:58 -08:00
|
|
|
true
|
2019-12-22 17:42:04 -05:00
|
|
|
} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
|
|
|
|
// Special attribute for allocator functions, which can't unwind.
|
2019-10-02 21:43:24 +02:00
|
|
|
false
|
2019-12-22 17:42:04 -05:00
|
|
|
} else {
|
|
|
|
if fn_abi.conv == Conv::Rust {
|
|
|
|
// Any Rust method (or `extern "Rust" fn` or `extern
|
|
|
|
// "rust-call" fn`) is explicitly allowed to unwind
|
|
|
|
// (unless it has no-unwind attribute, handled above).
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
// Anything else is either:
|
|
|
|
//
|
|
|
|
// 1. A foreign item using a non-Rust ABI (like `extern "C" { fn foo(); }`), or
|
|
|
|
//
|
|
|
|
// 2. A Rust item using a non-Rust ABI (like `extern "C" fn foo() { ... }`).
|
|
|
|
//
|
|
|
|
// Foreign items (case 1) are assumed to not unwind; it is
|
|
|
|
// UB otherwise. (At least for now; see also
|
|
|
|
// rust-lang/rust#63909 and Rust RFC 2753.)
|
|
|
|
//
|
|
|
|
// Items defined in Rust with non-Rust ABIs (case 2) are also
|
|
|
|
// not supposed to unwind. Whether this should be enforced
|
|
|
|
// (versus stating it is UB) and *how* it would be enforced
|
|
|
|
// is currently under discussion; see rust-lang/rust#58794.
|
|
|
|
//
|
|
|
|
// In either case, we mark item as explicitly nounwind.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2018-01-05 13:26:26 -08: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);
|
2018-08-02 18:10:26 +02:00
|
|
|
|
2018-04-23 15:17:07 -07:00
|
|
|
let features = llvm_target_features(cx.tcx.sess)
|
|
|
|
.map(|s| s.to_string())
|
2019-12-22 17:42:04 -05:00
|
|
|
.chain(codegen_fn_attrs.target_features.iter().map(|f| {
|
|
|
|
let feature = &f.as_str();
|
|
|
|
format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
|
|
|
|
}))
|
2018-02-27 20:12:32 -05:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
|
|
|
|
|
|
|
if !features.is_empty() {
|
|
|
|
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,
|
|
|
|
const_cstr!("target-features"),
|
|
|
|
&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.
|
2019-11-27 12:53:19 +02:00
|
|
|
if cx.tcx.sess.target.target.arch == "wasm32" {
|
|
|
|
if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
const_cstr!("wasm-import-module"),
|
|
|
|
&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,
|
|
|
|
const_cstr!("wasm-import-name"),
|
|
|
|
&name,
|
|
|
|
);
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 19:02:00 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 08:40:18 +01:00
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
2018-01-05 13:26:26 -08:00
|
|
|
providers.target_features_whitelist = |tcx, cnum| {
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
2018-03-20 19:36:30 -05:00
|
|
|
if tcx.sess.opts.actually_rustdoc {
|
|
|
|
// rustdoc needs to be able to document functions that use all the features, so
|
|
|
|
// whitelist them all
|
2019-12-22 17:42:04 -05:00
|
|
|
tcx.arena
|
|
|
|
.alloc(llvm_util::all_known_features().map(|(a, b)| (a.to_string(), b)).collect())
|
2018-03-20 19:36:30 -05:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
tcx.arena.alloc(
|
|
|
|
llvm_util::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
|
|
|
.map(|&(a, b)| (a.to_string(), b))
|
|
|
|
.collect(),
|
|
|
|
)
|
2018-03-20 19:36:30 -05:00
|
|
|
}
|
2018-01-05 13:26:26 -08:00
|
|
|
};
|
2018-03-09 09:26:15 -08:00
|
|
|
|
2018-02-10 14:28:17 -08:00
|
|
|
provide_extern(providers);
|
2018-03-09 09:26:15 -08:00
|
|
|
}
|
|
|
|
|
2019-02-25 08:40:18 +01:00
|
|
|
pub fn provide_extern(providers: &mut Providers<'_>) {
|
2018-02-10 14:28:17 -08:00
|
|
|
providers.wasm_import_module_map = |tcx, cnum| {
|
2018-07-16 11:31:14 -07:00
|
|
|
// Build up a map from DefId to a `NativeLibrary` structure, where
|
|
|
|
// `NativeLibrary` internally contains information about
|
|
|
|
// `#[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();
|
2018-02-10 14:28:17 -08:00
|
|
|
for lib in tcx.foreign_modules(cnum).iter() {
|
2019-12-22 17:42:04 -05:00
|
|
|
let module = def_id_to_native_lib.get(&lib.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
|
|
|
}
|
|
|
|
|
2018-12-01 16:30:42 +01:00
|
|
|
tcx.arena.alloc(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
|
|
|
}
|