Skip to content

Commit 6b7cacb

Browse files
committed
Export all fns with extern indicator
1 parent f62903b commit 6b7cacb

10 files changed

+74
-50
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
9090
let def_id = tcx.hir().local_def_id(hir_id);
9191
let generics = tcx.generics_of(def_id);
9292
if !generics.requires_monomorphization(tcx)
93-
&& (!Instance::mono(tcx, def_id.to_def_id())
94-
.def
95-
.generates_cgu_internal_copy(tcx)
96-
|| tcx.inline_exportable(def_id.to_def_id()))
93+
// Functions marked with #[inline] are codegened with "internal"
94+
// linkage and are not exported unless marked with an extern
95+
// inidicator
96+
&& (!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
97+
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator())
9798
{
9899
Some(def_id)
99100
} else {

src/librustc_middle/mir/mono.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,23 @@ impl<'tcx> MonoItem<'tcx> {
9292
MonoItem::Fn(ref instance) => {
9393
let entry_def_id = tcx.entry_fn(LOCAL_CRATE).map(|(id, _)| id);
9494
// If this function isn't inlined or otherwise has explicit
95-
// linkage, then we'll be creating a globally shared version.
95+
// linkage or an extern indicator, then we'll be creating a
96+
// globally shared version.
9697
if self.explicit_linkage(tcx).is_some()
9798
|| !instance.def.generates_cgu_internal_copy(tcx)
98-
|| tcx.inline_exportable(instance.def_id())
9999
|| Some(instance.def_id()) == entry_def_id.map(LocalDefId::to_def_id)
100100
{
101101
return InstantiationMode::GloballyShared { may_conflict: false };
102102
}
103103

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

src/librustc_middle/query/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,6 @@ rustc_queries! {
697697
storage(ArenaCacheSelector<'tcx>)
698698
cache_on_disk_if { true }
699699
}
700-
701-
query inline_exportable(def_id: DefId) -> bool {
702-
desc { |tcx| "computing whether `{}` should be explicitly exported", tcx.def_path_str(def_id) }
703-
}
704700
}
705701

706702
Other {

src/librustc_typeck/collect.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use rustc_middle::ty::util::Discr;
4040
use rustc_middle::ty::util::IntTypeExt;
4141
use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt};
4242
use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
43-
use rustc_session::config::CrateType;
4443
use rustc_session::lint;
4544
use rustc_session::parse::feature_err;
4645
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -80,7 +79,6 @@ pub fn provide(providers: &mut Providers<'_>) {
8079
static_mutability,
8180
generator_kind,
8281
codegen_fn_attrs,
83-
inline_exportable,
8482
collect_mod_item_types,
8583
..*providers
8684
};
@@ -2601,16 +2599,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26012599
codegen_fn_attrs
26022600
}
26032601

2604-
fn inline_exportable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
2605-
// Functions marked with #[inline] are only ever codegened
2606-
// with "internal" linkage and are never exported unless we're
2607-
// building a `staticlib` or `cdylib` and they are marked
2608-
// `#[no_mangle]`.
2609-
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NO_MANGLE)
2610-
&& (tcx.sess.crate_types().contains(&CrateType::Cdylib)
2611-
|| tcx.sess.crate_types().contains(&CrateType::Staticlib))
2612-
}
2613-
26142602
/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
26152603
/// applied to the method prototype.
26162604
fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// compile-flags: -C no-prepopulate-passes
2+
3+
#![crate_type = "cdylib"]
4+
5+
// CHECK: define void @a()
6+
#[no_mangle]
7+
#[inline]
8+
pub extern "C" fn a() {}
9+
10+
// CHECK: define void @b()
11+
#[export_name = "b"]
12+
#[inline]
13+
pub extern "C" fn b() {}
14+
15+
// CHECK: define void @c()
16+
#[no_mangle]
17+
#[inline]
18+
extern "C" fn c() {}
19+
20+
// CHECK: define void @d()
21+
#[export_name = "d"]
22+
#[inline]
23+
extern "C" fn d() {}

src/test/codegen/cdylib-external-no-mangle-fns.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/test/codegen/export-no-mangle.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ mod private {
1818
// CHECK: void @bar()
1919
#[export_name = "bar"]
2020
extern fn bar() {}
21+
22+
// CHECK: void @baz()
23+
#[export_name = "baz"]
24+
#[inline]
25+
extern fn baz() {}
2126
}

src/test/codegen/external-no-mangle-fns.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ fn x() {
5353
core::ptr::read_volatile(&42);
5454
}
5555
}
56+
57+
// CHECK: define void @i()
58+
#[no_mangle]
59+
#[inline]
60+
fn i() {}
61+
62+
// CHECK: define void @j()
63+
#[no_mangle]
64+
#[inline]
65+
pub fn j() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// compile-flags: -C no-prepopulate-passes
2+
3+
#![crate_type = "staticlib"]
4+
5+
// CHECK: define void @a()
6+
#[no_mangle]
7+
#[inline]
8+
pub extern "C" fn a() {}
9+
10+
// CHECK: define void @b()
11+
#[export_name = "b"]
12+
#[inline]
13+
pub extern "C" fn b() {}
14+
15+
// CHECK: define void @c()
16+
#[no_mangle]
17+
#[inline]
18+
extern "C" fn c() {}
19+
20+
// CHECK: define void @d()
21+
#[export_name = "d"]
22+
#[inline]
23+
extern "C" fn d() {}

src/test/codegen/staticlib-external-no-mangle-fns.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)