Skip to content

Commit 95b9442

Browse files
committed
rustc_codegen_ssa: take a FnAbi instead of a FnSig in declare_fn.
1 parent db477af commit 95b9442

File tree

7 files changed

+45
-39
lines changed

7 files changed

+45
-39
lines changed

src/librustc_codegen_llvm/attributes.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::hir::{CodegenFnAttrFlags, CodegenFnAttrs};
66
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
77
use rustc::session::Session;
88
use rustc::session::config::{Sanitizer, OptLevel};
9-
use rustc::ty::{self, TyCtxt, PolyFnSig};
9+
use rustc::ty::TyCtxt;
1010
use rustc::ty::layout::HasTyCtxt;
1111
use rustc::ty::query::Providers;
1212
use rustc_data_structures::small_c_str::SmallCStr;
@@ -203,7 +203,7 @@ pub fn from_fn_attrs(
203203
cx: &CodegenCx<'ll, 'tcx>,
204204
llfn: &'ll Value,
205205
id: Option<DefId>,
206-
sig: PolyFnSig<'tcx>,
206+
abi: Abi,
207207
) {
208208
let codegen_fn_attrs = id.map(|id| cx.tcx.codegen_fn_attrs(id))
209209
.unwrap_or_else(|| CodegenFnAttrs::new());
@@ -276,8 +276,7 @@ pub fn from_fn_attrs(
276276
// Special attribute for allocator functions, which can't unwind.
277277
false
278278
} else {
279-
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
280-
if sig.abi == Abi::Rust || sig.abi == Abi::RustCall {
279+
if abi == Abi::Rust || abi == Abi::RustCall {
281280
// Any Rust method (or `extern "Rust" fn` or `extern
282281
// "rust-call" fn`) is explicitly allowed to unwind
283282
// (unless it has no-unwind attribute, handled above).

src/librustc_codegen_llvm/callee.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
//! and methods are represented as just a fn ptr and not a full
55
//! closure.
66
7+
use crate::abi::FnAbi;
78
use crate::attributes;
89
use crate::llvm;
910
use crate::context::CodegenCx;
1011
use crate::value::Value;
1112
use rustc_codegen_ssa::traits::*;
1213

13-
use rustc::ty::{TypeFoldable, Instance};
14-
use rustc::ty::layout::{LayoutOf, HasTyCtxt};
14+
use rustc::ty::{self, TypeFoldable, Instance};
15+
use rustc::ty::layout::{FnAbiExt, LayoutOf, HasTyCtxt};
1516

1617
/// Codegens a reference to a fn/method item, monomorphizing and
1718
/// inlining as it goes.
@@ -32,19 +33,19 @@ pub fn get_fn(
3233
assert!(!instance.substs.has_escaping_bound_vars());
3334
assert!(!instance.substs.has_param_types());
3435

35-
let sig = instance.fn_sig(cx.tcx());
3636
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
3737
return llfn;
3838
}
3939

40+
let sig = instance.fn_sig(cx.tcx());
4041
let sym = tcx.symbol_name(instance).name.as_str();
4142
debug!("get_fn({:?}: {:?}) => {}", instance, sig, sym);
4243

43-
// Create a fn pointer with the substituted signature.
44-
let fn_ptr_ty = tcx.mk_fn_ptr(sig);
45-
let llptrty = cx.backend_type(cx.layout_of(fn_ptr_ty));
46-
4744
let llfn = if let Some(llfn) = cx.get_declared_value(&sym) {
45+
// Create a fn pointer with the substituted signature.
46+
let fn_ptr_ty = tcx.mk_fn_ptr(sig);
47+
let llptrty = cx.backend_type(cx.layout_of(fn_ptr_ty));
48+
4849
// This is subtle and surprising, but sometimes we have to bitcast
4950
// the resulting fn pointer. The reason has to do with external
5051
// functions. If you have two crates that both bind the same C
@@ -76,14 +77,15 @@ pub fn get_fn(
7677
llfn
7778
}
7879
} else {
79-
let llfn = cx.declare_fn(&sym, sig);
80-
assert_eq!(cx.val_ty(llfn), llptrty);
80+
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
81+
let fn_abi = FnAbi::new(cx, sig, &[]);
82+
let llfn = cx.declare_fn(&sym, &fn_abi);
8183
debug!("get_fn: not casting pointer!");
8284

8385
if instance.def.is_inline(tcx) {
8486
attributes::inline(cx, llfn, attributes::InlineAttr::Hint);
8587
}
86-
attributes::from_fn_attrs(cx, llfn, Some(instance.def.def_id()), sig);
88+
attributes::from_fn_attrs(cx, llfn, Some(instance.def.def_id()), sig.abi);
8789

8890
let instance_def_id = instance.def_id();
8991

src/librustc_codegen_llvm/context.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::abi::FnAbi;
12
use crate::attributes;
23
use crate::llvm;
34
use crate::llvm_util;
@@ -15,7 +16,7 @@ use rustc::mir::mono::CodegenUnit;
1516
use rustc::session::config::{self, DebugInfo};
1617
use rustc::session::Session;
1718
use rustc::ty::layout::{
18-
LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx, HasParamEnv
19+
FnAbiExt, LayoutError, LayoutOf, PointeeInfo, Size, TyLayout, VariantIdx, HasParamEnv
1920
};
2021
use rustc::ty::{self, Ty, TyCtxt, Instance};
2122
use rustc::util::nodemap::FxHashMap;
@@ -412,15 +413,16 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
412413
return llfn;
413414
}
414415

415-
let sig = ty::Binder::bind(tcx.mk_fn_sig(
416+
let sig = tcx.mk_fn_sig(
416417
iter::once(tcx.mk_mut_ptr(tcx.types.u8)),
417418
tcx.types.never,
418419
false,
419420
hir::Unsafety::Unsafe,
420421
Abi::C
421-
));
422+
);
422423

423-
let llfn = self.declare_fn("rust_eh_unwind_resume", sig);
424+
let fn_abi = FnAbi::new(self, sig, &[]);
425+
let llfn = self.declare_fn("rust_eh_unwind_resume", &fn_abi);
424426
attributes::apply_target_cpu_attr(self, llfn);
425427
unwresume.set(Some(llfn));
426428
llfn

src/librustc_codegen_llvm/declare.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use crate::attributes;
1818
use crate::context::CodegenCx;
1919
use crate::type_::Type;
2020
use crate::value::Value;
21-
use rustc::ty::{self, PolyFnSig};
22-
use rustc::ty::layout::{FnAbiExt, LayoutOf};
21+
use rustc::ty::Ty;
2322
use rustc::session::config::Sanitizer;
2423
use rustc_data_structures::small_c_str::SmallCStr;
2524
use rustc_codegen_ssa::traits::*;
@@ -94,16 +93,14 @@ impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
9493
fn declare_fn(
9594
&self,
9695
name: &str,
97-
sig: PolyFnSig<'tcx>,
96+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
9897
) -> &'ll Value {
99-
debug!("declare_rust_fn(name={:?}, sig={:?})", name, sig);
100-
let sig = self.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
101-
debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
98+
debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
10299

103-
let fn_abi = FnAbi::new(self, sig, &[]);
104100
let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
105101

106-
if self.layout_of(sig.output()).abi.is_uninhabited() {
102+
// FIXME(eddyb) move into `FnAbi::apply_attrs_llfn`.
103+
if fn_abi.ret.layout.abi.is_uninhabited() {
107104
llvm::Attribute::NoReturn.apply_llfn(Function, llfn);
108105
}
109106

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::attributes;
21
use crate::llvm;
32
use crate::llvm_util;
43
use crate::abi::{Abi, FnAbi, LlvmType, PassMode};
@@ -14,7 +13,7 @@ use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1413
use rustc_codegen_ssa::glue;
1514
use rustc_codegen_ssa::base::{to_immediate, wants_msvc_seh, compare_simd_types};
1615
use rustc::ty::{self, Ty};
17-
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
16+
use rustc::ty::layout::{self, FnAbiExt, LayoutOf, HasTyCtxt, Primitive};
1817
use rustc::mir::interpret::GlobalId;
1918
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
2019
use rustc::hir;
@@ -1006,17 +1005,17 @@ fn gen_fn<'ll, 'tcx>(
10061005
output: Ty<'tcx>,
10071006
codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>),
10081007
) -> &'ll Value {
1009-
let rust_fn_sig = ty::Binder::bind(cx.tcx.mk_fn_sig(
1008+
let rust_fn_sig = cx.tcx.mk_fn_sig(
10101009
inputs.into_iter(),
10111010
output,
10121011
false,
10131012
hir::Unsafety::Unsafe,
10141013
Abi::Rust
1015-
));
1016-
let llfn = cx.declare_fn(name, rust_fn_sig);
1014+
);
1015+
let fn_abi = FnAbi::new(cx, rust_fn_sig, &[]);
1016+
let llfn = cx.declare_fn(name, &fn_abi);
10171017
// FIXME(eddyb) find a nicer way to do this.
10181018
unsafe { llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::InternalLinkage) };
1019-
attributes::from_fn_attrs(cx, llfn, None, rust_fn_sig);
10201019
let bx = Builder::new_block(cx, llfn, "entry-block");
10211020
codegen(bx);
10221021
llfn

src/librustc_codegen_llvm/mono_item.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use crate::abi::FnAbi;
12
use crate::attributes;
23
use crate::base;
34
use crate::context::CodegenCx;
45
use crate::llvm;
56
use crate::type_of::LayoutLlvmExt;
67
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
78
use rustc::mir::mono::{Linkage, Visibility};
8-
use rustc::ty::{TypeFoldable, Instance};
9-
use rustc::ty::layout::{LayoutOf, HasTyCtxt};
9+
use rustc::ty::{self, TypeFoldable, Instance};
10+
use rustc::ty::layout::{FnAbiExt, LayoutOf, HasTyCtxt};
1011
use rustc_codegen_ssa::traits::*;
1112

1213
pub use rustc::mir::mono::MonoItem;
@@ -43,9 +44,14 @@ impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
4344
!instance.substs.has_param_types());
4445

4546
let mono_sig = instance.fn_sig(self.tcx());
46-
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
47-
let lldecl = self.declare_fn(symbol_name, mono_sig);
47+
let mono_sig = self.tcx().normalize_erasing_late_bound_regions(
48+
ty::ParamEnv::reveal_all(),
49+
&mono_sig,
50+
);
51+
let fn_abi = FnAbi::new(self, mono_sig, &[]);
52+
let lldecl = self.declare_fn(symbol_name, &fn_abi);
4853
unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
54+
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
4955
base::set_link_section(lldecl, &attrs);
5056
if linkage == Linkage::LinkOnceODR ||
5157
linkage == Linkage::WeakODR {
@@ -75,7 +81,7 @@ impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
7581
self,
7682
lldecl,
7783
Some(instance.def.def_id()),
78-
mono_sig,
84+
mono_sig.abi,
7985
);
8086

8187
self.instances.borrow_mut().insert(instance, lldecl);

src/librustc_codegen_ssa/traits/declare.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::BackendTypes;
22
use rustc::hir::def_id::DefId;
33
use rustc::mir::mono::{Linkage, Visibility};
4-
use rustc::ty::{self, Instance};
4+
use rustc::ty::{Instance, Ty};
5+
use rustc_target::abi::call::FnAbi;
56

67
pub trait DeclareMethods<'tcx>: BackendTypes {
78
/// Declare a global value.
@@ -23,7 +24,7 @@ pub trait DeclareMethods<'tcx>: BackendTypes {
2324
///
2425
/// If there’s a value with the same name already declared, the function will
2526
/// update the declaration and return existing Value instead.
26-
fn declare_fn(&self, name: &str, sig: ty::PolyFnSig<'tcx>) -> Self::Function;
27+
fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Function;
2728

2829
/// Declare a global with an intention to define it.
2930
///

0 commit comments

Comments
 (0)