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-01-05 13:26:26 -08:00
|
|
|
use std::rc::Rc;
|
2016-11-29 19:02:00 -05:00
|
|
|
|
2018-01-05 13:26:26 -08:00
|
|
|
use rustc::hir::Unsafety;
|
|
|
|
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
|
2017-06-21 12:08:18 -07:00
|
|
|
use rustc::session::config::Sanitizer;
|
2018-01-05 13:26:26 -08:00
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use rustc::ty::maps::Providers;
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
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};
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::ast;
|
2016-03-22 19:23:36 +02:00
|
|
|
use context::CrateContext;
|
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
|
|
|
}
|
|
|
|
|
2016-05-27 13:27:56 -04:00
|
|
|
pub fn set_frame_pointer_elimination(ccx: &CrateContext, 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.
|
2016-05-27 13:27:56 -04:00
|
|
|
if ccx.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
|
|
|
}
|
|
|
|
|
2017-06-21 12:08:18 -07:00
|
|
|
pub fn set_probestack(ccx: &CrateContext, llfn: ValueRef) {
|
|
|
|
// Only use stack probes if the target specification indicates that we
|
|
|
|
// should be using stack probes
|
|
|
|
if !ccx.sess().target.target.options.stack_probes {
|
|
|
|
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.
|
|
|
|
match ccx.sess().opts.debugging_opts.sanitizer {
|
|
|
|
Some(Sanitizer::Address) => return,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 13:26:26 -08:00
|
|
|
pub fn from_fn_attrs(ccx: &CrateContext, llfn: ValueRef, id: DefId) {
|
2016-05-27 13:27:56 -04:00
|
|
|
use syntax::attr::*;
|
2018-01-05 06:58:34 +02:00
|
|
|
let attrs = ccx.tcx.get_attrs(id);
|
2018-01-05 13:26:26 -08:00
|
|
|
inline(llfn, find_inline_attr(Some(ccx.sess().diagnostic()), &attrs));
|
2016-05-27 13:27:56 -04:00
|
|
|
|
|
|
|
set_frame_pointer_elimination(ccx, llfn);
|
2017-06-21 12:08:18 -07:00
|
|
|
set_probestack(ccx, llfn);
|
2018-01-05 13:26:26 -08:00
|
|
|
|
|
|
|
for attr in attrs.iter() {
|
|
|
|
if attr.check_name("cold") {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::Cold.apply_llfn(Function, llfn);
|
2016-03-21 21:01:08 +01:00
|
|
|
} else if attr.check_name("naked") {
|
|
|
|
naked(llfn, true);
|
2015-02-28 23:53:12 +02:00
|
|
|
} else if attr.check_name("allocator") {
|
2016-08-03 00:25:19 +03:00
|
|
|
Attribute::NoAlias.apply_llfn(
|
2017-10-10 20:54:50 +03:00
|
|
|
llvm::AttributePlace::ReturnValue, llfn);
|
2015-09-11 20:09:19 +02:00
|
|
|
} else if attr.check_name("unwind") {
|
|
|
|
unwind(llfn, true);
|
2017-08-22 14:36:49 -07:00
|
|
|
} else if attr.check_name("rustc_allocator_nounwind") {
|
|
|
|
unwind(llfn, false);
|
2015-02-28 23:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 13:26:26 -08:00
|
|
|
|
2018-01-05 06:58:34 +02:00
|
|
|
let target_features = ccx.tcx.target_features_enabled(id);
|
2016-11-29 19:02:00 -05:00
|
|
|
if !target_features.is_empty() {
|
|
|
|
let val = CString::new(target_features.join(",")).unwrap();
|
|
|
|
llvm::AddFunctionAttrStringValue(
|
|
|
|
llfn, llvm::AttributePlace::Function,
|
|
|
|
cstr("target-features\0"), &val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
Rc::new(llvm_util::target_feature_whitelist(tcx.sess)
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.to_str().unwrap().to_string())
|
|
|
|
.collect())
|
|
|
|
};
|
|
|
|
|
|
|
|
providers.target_features_enabled = |tcx, id| {
|
|
|
|
let whitelist = tcx.target_features_whitelist(LOCAL_CRATE);
|
|
|
|
let mut target_features = Vec::new();
|
|
|
|
for attr in tcx.get_attrs(id).iter() {
|
|
|
|
if !attr.check_name("target_feature") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if let Some(val) = attr.value_str() {
|
|
|
|
for feat in val.as_str().split(",").map(|f| f.trim()) {
|
|
|
|
if !feat.is_empty() && !feat.contains('\0') {
|
|
|
|
target_features.push(feat.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let msg = "#[target_feature = \"..\"] is deprecated and will \
|
|
|
|
eventually be removed, use \
|
|
|
|
#[target_feature(enable = \"..\")] instead";
|
|
|
|
tcx.sess.span_warn(attr.span, &msg);
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcx.fn_sig(id).unsafety() == Unsafety::Normal {
|
|
|
|
let msg = "#[target_feature(..)] can only be applied to \
|
|
|
|
`unsafe` function";
|
|
|
|
tcx.sess.span_err(attr.span, msg);
|
|
|
|
}
|
|
|
|
from_target_feature(tcx, attr, &whitelist, &mut target_features);
|
|
|
|
}
|
|
|
|
Rc::new(target_features)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_target_feature(
|
|
|
|
tcx: TyCtxt,
|
|
|
|
attr: &ast::Attribute,
|
|
|
|
whitelist: &FxHashSet<String>,
|
|
|
|
target_features: &mut Vec<String>,
|
|
|
|
) {
|
|
|
|
let list = match attr.meta_item_list() {
|
|
|
|
Some(list) => list,
|
|
|
|
None => {
|
|
|
|
let msg = "#[target_feature] attribute must be of the form \
|
|
|
|
#[target_feature(..)]";
|
|
|
|
tcx.sess.span_err(attr.span, &msg);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for item in list {
|
|
|
|
if !item.check_name("enable") {
|
|
|
|
let msg = "#[target_feature(..)] only accepts sub-keys of `enable` \
|
|
|
|
currently";
|
|
|
|
tcx.sess.span_err(item.span, &msg);
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let value = match item.value_str() {
|
|
|
|
Some(list) => list,
|
|
|
|
None => {
|
|
|
|
let msg = "#[target_feature] attribute must be of the form \
|
|
|
|
#[target_feature(enable = \"..\")]";
|
|
|
|
tcx.sess.span_err(item.span, &msg);
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let value = value.as_str();
|
|
|
|
for feature in value.split(',') {
|
|
|
|
if whitelist.contains(feature) {
|
|
|
|
target_features.push(format!("+{}", feature));
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = format!("the feature named `{}` is not valid for \
|
|
|
|
this target", feature);
|
|
|
|
let mut err = tcx.sess.struct_span_err(item.span, &msg);
|
|
|
|
|
|
|
|
if feature.starts_with("+") {
|
|
|
|
let valid = whitelist.contains(&feature[1..]);
|
|
|
|
if valid {
|
|
|
|
err.help("consider removing the leading `+` in the feature name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|