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
|
|
|
|
2018-08-16 13:19:04 -07:00
|
|
|
use rustc::hir::{CodegenFnAttrFlags, CodegenFnAttrs};
|
2018-01-05 13:26:26 -08:00
|
|
|
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
|
2018-04-23 15:17:07 -07:00
|
|
|
use rustc::session::Session;
|
2018-10-27 15:29:06 +03:00
|
|
|
use rustc::session::config::{Sanitizer, OptLevel};
|
2018-11-15 06:17:58 -08:00
|
|
|
use rustc::ty::{self, TyCtxt, PolyFnSig};
|
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;
|
2018-12-07 13:51:03 -05:00
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2018-02-10 14:28:17 -08:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-05-24 12:03:05 -07:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2018-11-16 13:45:28 +02:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2017-06-21 12:08:18 -07:00
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::abi::Abi;
|
|
|
|
use crate::attributes;
|
|
|
|
use crate::llvm::{self, Attribute};
|
|
|
|
use crate::llvm::AttributePlace::Function;
|
|
|
|
use crate::llvm_util;
|
2018-10-27 15:29:06 +03:00
|
|
|
pub use syntax::attr::{self, 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]
|
2018-07-18 22:04:27 -05:00
|
|
|
pub 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 {
|
2016-08-03 00:25:19 +03:00
|
|
|
Hint => Attribute::InlineHint.apply_llfn(Function, val),
|
|
|
|
Always => Attribute::AlwaysInline.apply_llfn(Function, val),
|
2018-07-18 22:04:27 -05:00
|
|
|
Never => {
|
|
|
|
if cx.tcx().sess.target.target.arch != "amdgpu" {
|
|
|
|
Attribute::NoInline.apply_llfn(Function, val);
|
|
|
|
}
|
|
|
|
},
|
2015-03-04 01:03:25 +02: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);
|
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]
|
2018-07-10 13:28:39 +03:00
|
|
|
pub 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() {
|
2016-08-03 00:25:19 +03:00
|
|
|
llvm::AddFunctionAttrStringValue(
|
2016-11-29 19:02:00 -05:00
|
|
|
llfn, llvm::AttributePlace::Function,
|
2018-08-07 16:03:57 +02:00
|
|
|
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]
|
|
|
|
pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
|
|
|
|
if cx.sess().instrument_mcount() {
|
|
|
|
// Similar to `clang -pg` behavior. Handled by the
|
|
|
|
// `post-inline-ee-instrument` LLVM pass.
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn, llvm::AttributePlace::Function,
|
|
|
|
const_cstr!("instrument-function-entry-inlined"), const_cstr!("mcount"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub 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 {
|
2017-06-21 12:08:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently stack probes seem somewhat incompatible with the address
|
|
|
|
// sanitizer. With asan we're already protected from stack overflow anyway
|
|
|
|
// so we don't really need stack probes regardless.
|
2018-10-08 16:58:26 +02:00
|
|
|
if let Some(Sanitizer::Address) = cx.sess().opts.debugging_opts.sanitizer {
|
|
|
|
return
|
2017-06-21 12:08:18 -07:00
|
|
|
}
|
|
|
|
|
2018-02-19 01:57:55 +01:00
|
|
|
// probestack doesn't play nice either with pgo-gen.
|
2018-03-12 21:11:25 +01:00
|
|
|
if cx.sess().opts.debugging_opts.pgo_gen.is_some() {
|
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(
|
|
|
|
llfn, llvm::AttributePlace::Function,
|
2018-08-07 16:03:57 +02:00
|
|
|
const_cstr!("probe-stack"), const_cstr!("__rust_probestack"));
|
2017-06-21 12:08:18 -07:00
|
|
|
}
|
|
|
|
|
2018-04-23 15:17:07 -07:00
|
|
|
pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
|
|
|
|
const RUSTC_SPECIFIC_FEATURES: &[&str] = &[
|
|
|
|
"crt-static",
|
|
|
|
];
|
|
|
|
|
|
|
|
let cmdline = sess.opts.cg.target_feature.split(',')
|
|
|
|
.filter(|f| !RUSTC_SPECIFIC_FEATURES.iter().any(|s| f.contains(s)));
|
|
|
|
sess.target.target.options.features.split(',')
|
|
|
|
.chain(cmdline)
|
|
|
|
.filter(|l| !l.is_empty())
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
2018-08-07 16:04:34 +02:00
|
|
|
const_cstr!("target-cpu"),
|
2018-08-02 18:10:26 +02:00
|
|
|
target_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);
|
|
|
|
},
|
|
|
|
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,
|
|
|
|
id: Option<DefId>,
|
2018-11-15 06:17:58 -08:00
|
|
|
sig: PolyFnSig<'tcx>,
|
2018-08-16 13:19:04 -07:00
|
|
|
) {
|
|
|
|
let codegen_fn_attrs = id.map(|id| cx.tcx.codegen_fn_attrs(id))
|
2018-10-12 16:16:00 +02:00
|
|
|
.unwrap_or_else(|| CodegenFnAttrs::new());
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if !cx.sess().no_landing_pads() ||
|
|
|
|
cx.sess().target.target.options.requires_uwtable {
|
|
|
|
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) {
|
2018-01-15 20:08:09 -05:00
|
|
|
Attribute::NoAlias.apply_llfn(
|
|
|
|
llvm::AttributePlace::ReturnValue, llfn);
|
|
|
|
}
|
2018-05-24 12:03:05 -07:00
|
|
|
|
2018-11-15 06:17:58 -08: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
|
|
|
|
true
|
2018-05-24 12:03:05 -07:00
|
|
|
} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
|
2018-11-15 06:17:58 -08:00
|
|
|
// Special attribute for allocator functions, which can't unwind
|
|
|
|
false
|
|
|
|
} else if let Some(id) = id {
|
|
|
|
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
|
|
|
|
if cx.tcx.is_foreign_item(id) {
|
|
|
|
// Foreign items like `extern "C" { fn foo(); }` are assumed not to
|
|
|
|
// unwind
|
|
|
|
false
|
|
|
|
} else if sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
|
|
|
|
// Any items defined in Rust that *don't* have the `extern` ABI are
|
|
|
|
// defined to not unwind. We insert shims to abort if an unwind
|
|
|
|
// happens to enforce this.
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
// Anything else defined in Rust is assumed that it can possibly
|
|
|
|
// unwind
|
|
|
|
true
|
2018-05-24 12:03:05 -07:00
|
|
|
}
|
2018-11-15 06:17:58 -08:00
|
|
|
} else {
|
|
|
|
// assume this can possibly unwind, avoiding the application of a
|
|
|
|
// `nounwind` attribute below.
|
|
|
|
true
|
|
|
|
});
|
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())
|
|
|
|
.chain(
|
2018-05-08 16:10:16 +03:00
|
|
|
codegen_fn_attrs.target_features
|
2018-04-23 15:17:07 -07:00
|
|
|
.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(
|
|
|
|
llfn, llvm::AttributePlace::Function,
|
2018-08-07 16:03:57 +02:00
|
|
|
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.
|
2018-08-16 13:19:04 -07:00
|
|
|
if let Some(id) = id {
|
|
|
|
if cx.tcx.sess.target.target.arch == "wasm32" {
|
|
|
|
if let Some(module) = wasm_import_module(cx.tcx, id) {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
const_cstr!("wasm-import-module"),
|
|
|
|
&module,
|
|
|
|
);
|
|
|
|
}
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 19:02:00 -05:00
|
|
|
}
|
|
|
|
|
2018-01-05 13:26:26 -08:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
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
|
|
|
|
Lrc::new(llvm_util::all_known_features()
|
2018-04-05 08:02:11 -07:00
|
|
|
.map(|(a, b)| (a.to_string(), b.map(|s| s.to_string())))
|
2018-03-20 19:36:30 -05:00
|
|
|
.collect())
|
|
|
|
} else {
|
|
|
|
Lrc::new(llvm_util::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
2018-04-05 08:02:11 -07:00
|
|
|
.map(|&(a, b)| (a.to_string(), b.map(|s| s.to_string())))
|
2018-03-20 19:36:30 -05:00
|
|
|
.collect())
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-02-10 14:28:17 -08:00
|
|
|
pub fn provide_extern(providers: &mut Providers) {
|
|
|
|
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
|
|
|
|
|
|
|
let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
|
2018-07-16 11:31:14 -07:00
|
|
|
if let Some(id) = lib.foreign_module {
|
2018-10-08 16:55:04 +02:00
|
|
|
Some((id, lib))
|
|
|
|
} else {
|
|
|
|
None
|
2018-07-16 11:31:14 -07:00
|
|
|
}
|
2018-10-08 16:55:04 +02:00
|
|
|
).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() {
|
2018-07-16 11:31:14 -07: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
|
|
|
}
|
|
|
|
|
|
|
|
Lrc::new(ret)
|
2018-07-16 11:31:14 -07:00
|
|
|
};
|
2018-02-10 14:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wasm_import_module(tcx: TyCtxt, id: DefId) -> Option<CString> {
|
|
|
|
tcx.wasm_import_module_map(id.krate)
|
|
|
|
.get(&id)
|
|
|
|
.map(|s| CString::new(&s[..]).unwrap())
|
|
|
|
}
|