Skip to content

Commit

Permalink
Export all fns with extern indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
doctorn committed Jun 15, 2020
1 parent f62903b commit 6b7cacb
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 50 deletions.
9 changes: 5 additions & 4 deletions src/librustc_codegen_ssa/back/symbol_export.rs
Expand Up @@ -90,10 +90,11 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
let def_id = tcx.hir().local_def_id(hir_id);
let generics = tcx.generics_of(def_id);
if !generics.requires_monomorphization(tcx)
&& (!Instance::mono(tcx, def_id.to_def_id())
.def
.generates_cgu_internal_copy(tcx)
|| tcx.inline_exportable(def_id.to_def_id()))
// Functions marked with #[inline] are codegened with "internal"
// linkage and are not exported unless marked with an extern
// inidicator
&& (!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator())
{
Some(def_id)
} else {
Expand Down
12 changes: 8 additions & 4 deletions src/librustc_middle/mir/mono.rs
Expand Up @@ -92,19 +92,23 @@ impl<'tcx> MonoItem<'tcx> {
MonoItem::Fn(ref instance) => {
let entry_def_id = tcx.entry_fn(LOCAL_CRATE).map(|(id, _)| id);
// If this function isn't inlined or otherwise has explicit
// linkage, then we'll be creating a globally shared version.
// linkage or an extern indicator, then we'll be creating a
// globally shared version.
if self.explicit_linkage(tcx).is_some()
|| !instance.def.generates_cgu_internal_copy(tcx)
|| tcx.inline_exportable(instance.def_id())
|| Some(instance.def_id()) == entry_def_id.map(LocalDefId::to_def_id)
{
return InstantiationMode::GloballyShared { may_conflict: false };
}

// At this point we don't have explicit linkage and we're an
// inlined function. If we're inlining into all CGUs then we'll
// be creating a local copy per CGU
if generate_cgu_internal_copies {
// be creating a local copy per CGU. We need to watch out here
// for an extern indicator as we don't want to optimise away
// inlined functions that should be exported.
if generate_cgu_internal_copies
&& !tcx.codegen_fn_attrs(instance.def_id()).contains_extern_indicator()
{
return InstantiationMode::LocalCopy;
}

Expand Down
4 changes: 0 additions & 4 deletions src/librustc_middle/query/mod.rs
Expand Up @@ -697,10 +697,6 @@ rustc_queries! {
storage(ArenaCacheSelector<'tcx>)
cache_on_disk_if { true }
}

query inline_exportable(def_id: DefId) -> bool {
desc { |tcx| "computing whether `{}` should be explicitly exported", tcx.def_path_str(def_id) }
}
}

Other {
Expand Down
12 changes: 0 additions & 12 deletions src/librustc_typeck/collect.rs
Expand Up @@ -40,7 +40,6 @@ use rustc_middle::ty::util::Discr;
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt};
use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
use rustc_session::config::CrateType;
use rustc_session::lint;
use rustc_session::parse::feature_err;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
Expand Down Expand Up @@ -80,7 +79,6 @@ pub fn provide(providers: &mut Providers<'_>) {
static_mutability,
generator_kind,
codegen_fn_attrs,
inline_exportable,
collect_mod_item_types,
..*providers
};
Expand Down Expand Up @@ -2601,16 +2599,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
codegen_fn_attrs
}

fn inline_exportable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
// Functions marked with #[inline] are only ever codegened
// with "internal" linkage and are never exported unless we're
// building a `staticlib` or `cdylib` and they are marked
// `#[no_mangle]`.
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NO_MANGLE)
&& (tcx.sess.crate_types().contains(&CrateType::Cdylib)
|| tcx.sess.crate_types().contains(&CrateType::Staticlib))
}

/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
/// applied to the method prototype.
fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
Expand Down
23 changes: 23 additions & 0 deletions src/test/codegen/cdylib-external-inline-fns.rs
@@ -0,0 +1,23 @@
// compile-flags: -C no-prepopulate-passes

#![crate_type = "cdylib"]

// CHECK: define void @a()
#[no_mangle]
#[inline]
pub extern "C" fn a() {}

// CHECK: define void @b()
#[export_name = "b"]
#[inline]
pub extern "C" fn b() {}

// CHECK: define void @c()
#[no_mangle]
#[inline]
extern "C" fn c() {}

// CHECK: define void @d()
#[export_name = "d"]
#[inline]
extern "C" fn d() {}
13 changes: 0 additions & 13 deletions src/test/codegen/cdylib-external-no-mangle-fns.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/test/codegen/export-no-mangle.rs
Expand Up @@ -18,4 +18,9 @@ mod private {
// CHECK: void @bar()
#[export_name = "bar"]
extern fn bar() {}

// CHECK: void @baz()
#[export_name = "baz"]
#[inline]
extern fn baz() {}
}
10 changes: 10 additions & 0 deletions src/test/codegen/external-no-mangle-fns.rs
Expand Up @@ -53,3 +53,13 @@ fn x() {
core::ptr::read_volatile(&42);
}
}

// CHECK: define void @i()
#[no_mangle]
#[inline]
fn i() {}

// CHECK: define void @j()
#[no_mangle]
#[inline]
pub fn j() {}
23 changes: 23 additions & 0 deletions src/test/codegen/staticlib-external-inline-fns.rs
@@ -0,0 +1,23 @@
// compile-flags: -C no-prepopulate-passes

#![crate_type = "staticlib"]

// CHECK: define void @a()
#[no_mangle]
#[inline]
pub extern "C" fn a() {}

// CHECK: define void @b()
#[export_name = "b"]
#[inline]
pub extern "C" fn b() {}

// CHECK: define void @c()
#[no_mangle]
#[inline]
extern "C" fn c() {}

// CHECK: define void @d()
#[export_name = "d"]
#[inline]
extern "C" fn d() {}
13 changes: 0 additions & 13 deletions src/test/codegen/staticlib-external-no-mangle-fns.rs

This file was deleted.

0 comments on commit 6b7cacb

Please sign in to comment.