1
Fork 0

rustc_codegen_llvm: take an Instance in attributes::from_fn_attrs.

This commit is contained in:
Eduard-Mihai Burtescu 2019-11-27 12:53:19 +02:00
parent 8a8749b297
commit cd3c324b07
3 changed files with 24 additions and 36 deletions

View file

@ -2,11 +2,11 @@
use std::ffi::CString; use std::ffi::CString;
use rustc::hir::{CodegenFnAttrFlags, CodegenFnAttrs}; use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::def_id::{DefId, LOCAL_CRATE}; use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::session::Session; use rustc::session::Session;
use rustc::session::config::{Sanitizer, OptLevel}; use rustc::session::config::{Sanitizer, OptLevel};
use rustc::ty::TyCtxt; use rustc::ty::{self, TyCtxt};
use rustc::ty::layout::HasTyCtxt; use rustc::ty::layout::HasTyCtxt;
use rustc::ty::query::Providers; use rustc::ty::query::Providers;
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
@ -202,11 +202,9 @@ pub(crate) fn default_optimisation_attrs(sess: &Session, llfn: &'ll Value) {
pub fn from_fn_attrs( pub fn from_fn_attrs(
cx: &CodegenCx<'ll, 'tcx>, cx: &CodegenCx<'ll, 'tcx>,
llfn: &'ll Value, llfn: &'ll Value,
id: Option<DefId>, instance: ty::Instance<'tcx>,
abi: Abi,
) { ) {
let codegen_fn_attrs = id.map(|id| cx.tcx.codegen_fn_attrs(id)) let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
.unwrap_or_else(|| CodegenFnAttrs::new());
match codegen_fn_attrs.optimize { match codegen_fn_attrs.optimize {
OptimizeAttr::None => { OptimizeAttr::None => {
@ -224,6 +222,11 @@ pub fn from_fn_attrs(
} }
} }
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
if instance.def.is_inline(cx.tcx) {
inline(cx, llfn, attributes::InlineAttr::Hint);
}
inline(cx, llfn, codegen_fn_attrs.inline); inline(cx, llfn, codegen_fn_attrs.inline);
// The `uwtable` attribute according to LLVM is: // The `uwtable` attribute according to LLVM is:
@ -276,6 +279,9 @@ pub fn from_fn_attrs(
// Special attribute for allocator functions, which can't unwind. // Special attribute for allocator functions, which can't unwind.
false false
} else { } else {
// FIXME(eddyb) avoid this `Instance::fn_sig` call.
// Perhaps store the relevant information in `FnAbi`?
let abi = instance.fn_sig(cx.tcx()).abi();
if abi == Abi::Rust || abi == Abi::RustCall { if abi == Abi::Rust || abi == Abi::RustCall {
// Any Rust method (or `extern "Rust" fn` or `extern // Any Rust method (or `extern "Rust" fn` or `extern
// "rust-call" fn`) is explicitly allowed to unwind // "rust-call" fn`) is explicitly allowed to unwind
@ -330,16 +336,14 @@ pub fn from_fn_attrs(
// Note that currently the `wasm-import-module` doesn't do anything, but // Note that currently the `wasm-import-module` doesn't do anything, but
// eventually LLVM 7 should read this and ferry the appropriate import // eventually LLVM 7 should read this and ferry the appropriate import
// module to the output file. // module to the output file.
if let Some(id) = id { if cx.tcx.sess.target.target.arch == "wasm32" {
if cx.tcx.sess.target.target.arch == "wasm32" { if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
if let Some(module) = wasm_import_module(cx.tcx, id) { llvm::AddFunctionAttrStringValue(
llvm::AddFunctionAttrStringValue( llfn,
llfn, llvm::AttributePlace::Function,
llvm::AttributePlace::Function, const_cstr!("wasm-import-module"),
const_cstr!("wasm-import-module"), &module,
&module, );
);
}
} }
} }
} }

View file

@ -80,13 +80,7 @@ pub fn get_fn(
let llfn = cx.declare_fn(&sym, &fn_abi); let llfn = cx.declare_fn(&sym, &fn_abi);
debug!("get_fn: not casting pointer!"); debug!("get_fn: not casting pointer!");
if instance.def.is_inline(tcx) { attributes::from_fn_attrs(cx, llfn, instance);
attributes::inline(cx, llfn, attributes::InlineAttr::Hint);
}
// FIXME(eddyb) avoid this `Instance::fn_sig` call.
// Perhaps store the relevant information in `FnAbi`?
let sig_abi = instance.fn_sig(cx.tcx()).abi();
attributes::from_fn_attrs(cx, llfn, Some(instance.def.def_id()), sig_abi);
let instance_def_id = instance.def_id(); let instance_def_id = instance.def_id();

View file

@ -7,7 +7,7 @@ use crate::type_of::LayoutLlvmExt;
use rustc::hir::def_id::{DefId, LOCAL_CRATE}; use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::mir::mono::{Linkage, Visibility}; use rustc::mir::mono::{Linkage, Visibility};
use rustc::ty::{TypeFoldable, Instance}; use rustc::ty::{TypeFoldable, Instance};
use rustc::ty::layout::{FnAbiExt, LayoutOf, HasTyCtxt}; use rustc::ty::layout::{FnAbiExt, LayoutOf};
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
pub use rustc::mir::mono::MonoItem; pub use rustc::mir::mono::MonoItem;
@ -69,18 +69,8 @@ impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
} }
debug!("predefine_fn: instance = {:?}", instance); debug!("predefine_fn: instance = {:?}", instance);
if instance.def.is_inline(self.tcx) {
attributes::inline(self, lldecl, attributes::InlineAttr::Hint); attributes::from_fn_attrs(self, lldecl, instance);
}
// FIXME(eddyb) avoid this `Instance::fn_sig` call.
// Perhaps store the relevant information in `FnAbi`?
let mono_sig_abi = instance.fn_sig(self.tcx()).abi();
attributes::from_fn_attrs(
self,
lldecl,
Some(instance.def.def_id()),
mono_sig_abi,
);
self.instances.borrow_mut().insert(instance, lldecl); self.instances.borrow_mut().insert(instance, lldecl);
} }