2015-02-28 23:53:12 +02:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
//! Set and unset common attributes on LLVM values.
|
|
|
|
|
2016-11-29 19:02:00 -05:00
|
|
|
use std::ffi::{CStr, CString};
|
|
|
|
|
2018-03-09 09:26:15 -08:00
|
|
|
use rustc::hir::{self, TransFnAttrFlags};
|
2018-01-05 13:26:26 -08:00
|
|
|
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
|
2018-03-09 09:26:15 -08:00
|
|
|
use rustc::hir::itemlikevisit::ItemLikeVisitor;
|
2017-06-21 12:08:18 -07:00
|
|
|
use rustc::session::config::Sanitizer;
|
2018-03-09 09:26:15 -08:00
|
|
|
use rustc::ty::TyCtxt;
|
2018-01-05 13:26:26 -08:00
|
|
|
use rustc::ty::maps::Providers;
|
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;
|
2017-06-21 12:08:18 -07:00
|
|
|
|
2016-08-03 00:25:19 +03:00
|
|
|
use llvm::{self, Attribute, ValueRef};
|
|
|
|
use llvm::AttributePlace::Function;
|
2018-01-05 13:26:26 -08:00
|
|
|
use llvm_util;
|
2017-06-22 13:14:00 -07:00
|
|
|
pub use syntax::attr::{self, InlineAttr};
|
2018-01-05 07:01:54 +02:00
|
|
|
use context::CodegenCx;
|
2015-02-28 23:53:12 +02:00
|
|
|
|
|
|
|
/// Mark LLVM function to use provided inline heuristic.
|
|
|
|
#[inline]
|
|
|
|
pub fn inline(val: ValueRef, 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),
|
|
|
|
Never => 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]
|
|
|
|
pub fn emit_uwtable(val: ValueRef, 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]
|
|
|
|
pub fn unwind(val: ValueRef, 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
|
|
|
}
|
|
|
|
|
2017-08-15 21:45:21 +02:00
|
|
|
/// Tell LLVM whether it should optimize function for size.
|
2015-02-28 23:53:12 +02:00
|
|
|
#[inline]
|
|
|
|
#[allow(dead_code)] // possibly useful function
|
|
|
|
pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 21:01:08 +01:00
|
|
|
/// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
|
|
|
|
#[inline]
|
|
|
|
pub fn naked(val: ValueRef, 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-01-05 07:04:08 +02:00
|
|
|
pub fn set_frame_pointer_elimination(cx: &CodegenCx, llfn: ValueRef) {
|
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
|
|
|
// FIXME: #11906: Omitting frame pointers breaks retrieving the value of a
|
|
|
|
// parameter.
|
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,
|
|
|
|
cstr("no-frame-pointer-elim\0"), cstr("true\0"));
|
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-01-05 07:04:08 +02:00
|
|
|
pub fn set_probestack(cx: &CodegenCx, llfn: ValueRef) {
|
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-01-05 07:04:08 +02:00
|
|
|
match cx.sess().opts.debugging_opts.sanitizer {
|
2017-06-21 12:08:18 -07:00
|
|
|
Some(Sanitizer::Address) => return,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
cstr("probe-stack\0"), cstr("__rust_probestack\0"));
|
|
|
|
}
|
|
|
|
|
2016-05-27 13:27:56 -04:00
|
|
|
/// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
|
|
|
|
/// attributes.
|
2018-01-05 07:04:08 +02:00
|
|
|
pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
|
2018-01-30 22:39:23 -05:00
|
|
|
let trans_fn_attrs = cx.tcx.trans_fn_attrs(id);
|
|
|
|
|
|
|
|
inline(llfn, trans_fn_attrs.inline);
|
2016-05-27 13:27:56 -04:00
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
set_frame_pointer_elimination(cx, llfn);
|
|
|
|
set_probestack(cx, llfn);
|
2018-01-05 13:26:26 -08:00
|
|
|
|
2018-01-15 20:08:09 -05:00
|
|
|
if trans_fn_attrs.flags.contains(TransFnAttrFlags::COLD) {
|
|
|
|
Attribute::Cold.apply_llfn(Function, llfn);
|
|
|
|
}
|
|
|
|
if trans_fn_attrs.flags.contains(TransFnAttrFlags::NAKED) {
|
|
|
|
naked(llfn, true);
|
|
|
|
}
|
|
|
|
if trans_fn_attrs.flags.contains(TransFnAttrFlags::ALLOCATOR) {
|
|
|
|
Attribute::NoAlias.apply_llfn(
|
|
|
|
llvm::AttributePlace::ReturnValue, llfn);
|
|
|
|
}
|
|
|
|
if trans_fn_attrs.flags.contains(TransFnAttrFlags::UNWIND) {
|
|
|
|
unwind(llfn, true);
|
|
|
|
}
|
|
|
|
if trans_fn_attrs.flags.contains(TransFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
|
|
|
|
unwind(llfn, false);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
2018-01-05 13:26:26 -08:00
|
|
|
|
2018-02-27 20:12:32 -05:00
|
|
|
let features =
|
|
|
|
trans_fn_attrs.target_features
|
|
|
|
.iter()
|
|
|
|
.map(|f| {
|
|
|
|
let feature = &*f.as_str();
|
|
|
|
format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
|
|
|
|
})
|
|
|
|
.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,
|
|
|
|
cstr("target-features\0"), &val);
|
|
|
|
}
|
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.
|
|
|
|
if cx.tcx.sess.target.target.arch == "wasm32" {
|
|
|
|
if let Some(module) = wasm_import_module(cx.tcx, id) {
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn,
|
|
|
|
llvm::AttributePlace::Function,
|
|
|
|
cstr("wasm-import-module\0"),
|
|
|
|
&module,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 19:02:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cstr(s: &'static str) -> &CStr {
|
|
|
|
CStr::from_bytes_with_nul(s.as_bytes()).expect("null-terminated string")
|
2015-02-28 23:53:12 +02: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()
|
|
|
|
.map(|c| c.to_string())
|
|
|
|
.collect())
|
|
|
|
} else {
|
|
|
|
Lrc::new(llvm_util::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.to_string())
|
|
|
|
.collect())
|
|
|
|
}
|
2018-01-05 13:26:26 -08:00
|
|
|
};
|
2018-03-09 09:26:15 -08:00
|
|
|
|
|
|
|
providers.wasm_custom_sections = |tcx, cnum| {
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
let mut finder = WasmSectionFinder { tcx, list: Vec::new() };
|
|
|
|
tcx.hir.krate().visit_all_item_likes(&mut finder);
|
|
|
|
Lrc::new(finder.list)
|
|
|
|
};
|
2018-02-10 14:28:17 -08:00
|
|
|
|
|
|
|
provide_extern(providers);
|
2018-03-09 09:26:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct WasmSectionFinder<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
list: Vec<DefId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for WasmSectionFinder<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, i: &'tcx hir::Item) {
|
|
|
|
match i.node {
|
|
|
|
hir::ItemConst(..) => {}
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
if i.attrs.iter().any(|i| i.check_name("wasm_custom_section")) {
|
|
|
|
self.list.push(self.tcx.hir.local_def_id(i.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) {}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) {}
|
2018-01-05 13:26:26 -08:00
|
|
|
}
|
2018-02-10 14:28:17 -08:00
|
|
|
|
|
|
|
pub fn provide_extern(providers: &mut Providers) {
|
|
|
|
providers.wasm_import_module_map = |tcx, cnum| {
|
|
|
|
let mut ret = FxHashMap();
|
|
|
|
for lib in tcx.foreign_modules(cnum).iter() {
|
|
|
|
let attrs = tcx.get_attrs(lib.def_id);
|
|
|
|
let mut module = None;
|
|
|
|
for attr in attrs.iter().filter(|a| a.check_name("wasm_import_module")) {
|
|
|
|
module = attr.value_str();
|
|
|
|
}
|
|
|
|
let module = match module {
|
|
|
|
Some(s) => s,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
for id in lib.foreign_items.iter() {
|
|
|
|
assert_eq!(id.krate, cnum);
|
|
|
|
ret.insert(*id, module.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lrc::new(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|