Skip to content

Commit 9cbe0c8

Browse files
committed
librustc: Stop exporting all cross-crate inlined functions. Shaves 800K off librustc. r=graydon
1 parent e92e3e4 commit 9cbe0c8

File tree

3 files changed

+93
-28
lines changed

3 files changed

+93
-28
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,9 @@ pub fn trans_stmt(cx: block, s: ast::stmt) -> block {
11391139
}
11401140
}
11411141
}
1142-
ast::decl_item(i) => trans_item(cx.fcx.ccx, *i)
1142+
ast::decl_item(i) => {
1143+
trans_item(cx.fcx.ccx, *i, DontForceInternal)
1144+
}
11431145
}
11441146
}
11451147
ast::stmt_mac(*) => cx.tcx().sess.bug(~"unexpanded macro")
@@ -1972,26 +1974,36 @@ pub fn trans_struct_dtor(ccx: @crate_ctxt,
19721974
lldecl
19731975
}
19741976
1975-
pub fn trans_enum_def(ccx: @crate_ctxt, enum_definition: ast::enum_def,
1976-
id: ast::node_id, degen: bool,
1977-
path: @ast_map::path, vi: @~[ty::VariantInfo],
1978-
i: &mut uint) {
1977+
pub fn trans_enum_def(ccx: @crate_ctxt,
1978+
enum_definition: ast::enum_def,
1979+
id: ast::node_id,
1980+
degen: bool,
1981+
path: @ast_map::path,
1982+
vi: @~[ty::VariantInfo],
1983+
i: &mut uint,
1984+
force_internal: ForceInternalFlag) {
19791985
for vec::each(enum_definition.variants) |variant| {
19801986
let disr_val = vi[*i].disr_val;
19811987
*i += 1;
19821988
19831989
match variant.node.kind {
19841990
ast::tuple_variant_kind(ref args) if args.len() > 0 => {
19851991
let llfn = get_item_val(ccx, variant.node.id);
1992+
if force_internal == ForceInternal {
1993+
internalize_item(llfn);
1994+
}
19861995
trans_enum_variant(ccx, id, *variant, /*bad*/copy *args,
19871996
disr_val, degen, None, llfn);
19881997
}
19891998
ast::tuple_variant_kind(_) => {
19901999
// Nothing to do.
19912000
}
19922001
ast::struct_variant_kind(struct_def) => {
1993-
trans_struct_def(ccx, struct_def, path,
1994-
variant.node.id);
2002+
trans_struct_def(ccx,
2003+
struct_def,
2004+
path,
2005+
variant.node.id,
2006+
force_internal);
19952007
}
19962008
ast::enum_variant_kind(ref enum_definition) => {
19972009
trans_enum_def(ccx,
@@ -2000,13 +2012,28 @@ pub fn trans_enum_def(ccx: @crate_ctxt, enum_definition: ast::enum_def,
20002012
degen,
20012013
path,
20022014
vi,
2003-
&mut *i);
2015+
&mut *i,
2016+
force_internal);
20042017
}
20052018
}
20062019
}
20072020
}
20082021
2009-
pub fn trans_item(ccx: @crate_ctxt, item: ast::item) {
2022+
#[deriving_eq]
2023+
pub enum ForceInternalFlag {
2024+
DontForceInternal,
2025+
ForceInternal,
2026+
}
2027+
2028+
// Marks the LLVM ValueRef corresponding to the given item as internal, so
2029+
// that if it gets inlined away LLVM won't bother translating it.
2030+
pub fn internalize_item(llval: ValueRef) {
2031+
lib::llvm::SetLinkage(llval, lib::llvm::InternalLinkage);
2032+
}
2033+
2034+
pub fn trans_item(ccx: @crate_ctxt,
2035+
item: ast::item,
2036+
force_internal: ForceInternalFlag) {
20102037
let _icx = ccx.insn_ctxt("trans_item");
20112038
let path = match ccx.tcx.items.get(&item.id) {
20122039
ast_map::node_item(_, p) => p,
@@ -2017,13 +2044,19 @@ pub fn trans_item(ccx: @crate_ctxt, item: ast::item) {
20172044
ast::item_fn(ref decl, purity, ref tps, ref body) => {
20182045
if purity == ast::extern_fn {
20192046
let llfndecl = get_item_val(ccx, item.id);
2047+
if force_internal == ForceInternal {
2048+
internalize_item(llfndecl);
2049+
}
20202050
foreign::trans_foreign_fn(ccx,
20212051
vec::append(
20222052
/*bad*/copy *path,
20232053
~[path_name(item.ident)]),
20242054
decl, body, llfndecl, item.id);
20252055
} else if tps.is_empty() {
20262056
let llfndecl = get_item_val(ccx, item.id);
2057+
if force_internal == ForceInternal {
2058+
internalize_item(llfndecl);
2059+
}
20272060
trans_fn(ccx,
20282061
vec::append(/*bad*/copy *path, ~[path_name(item.ident)]),
20292062
decl, body, llfndecl, no_self, None, item.id, None);
@@ -2032,16 +2065,22 @@ pub fn trans_item(ccx: @crate_ctxt, item: ast::item) {
20322065
match stmt.node {
20332066
ast::stmt_decl(@codemap::spanned { node: ast::decl_item(i),
20342067
_ }, _) => {
2035-
trans_item(ccx, *i);
2068+
trans_item(ccx, *i, DontForceInternal);
20362069
}
20372070
_ => ()
20382071
}
20392072
}
20402073
}
20412074
}
20422075
ast::item_impl(tps, _, _, ms) => {
2043-
meth::trans_impl(ccx, /*bad*/copy *path, item.ident, ms, tps, None,
2044-
item.id);
2076+
meth::trans_impl(ccx,
2077+
/*bad*/copy *path,
2078+
item.ident,
2079+
ms,
2080+
tps,
2081+
None,
2082+
item.id,
2083+
force_internal);
20452084
}
20462085
ast::item_mod(m) => {
20472086
trans_mod(ccx, m);
@@ -2051,8 +2090,14 @@ pub fn trans_item(ccx: @crate_ctxt, item: ast::item) {
20512090
let degen = (*enum_definition).variants.len() == 1u;
20522091
let vi = ty::enum_variants(ccx.tcx, local_def(item.id));
20532092
let mut i = 0;
2054-
trans_enum_def(ccx, (*enum_definition), item.id,
2055-
degen, path, vi, &mut i);
2093+
trans_enum_def(ccx,
2094+
*enum_definition,
2095+
item.id,
2096+
degen,
2097+
path,
2098+
vi,
2099+
&mut i,
2100+
force_internal);
20562101
}
20572102
}
20582103
ast::item_const(_, expr) => consts::trans_const(ccx, expr, item.id),
@@ -2066,16 +2111,22 @@ pub fn trans_item(ccx: @crate_ctxt, item: ast::item) {
20662111
}
20672112
ast::item_struct(struct_def, tps) => {
20682113
if tps.is_empty() {
2069-
trans_struct_def(ccx, struct_def, path, item.id);
2114+
trans_struct_def(ccx,
2115+
struct_def,
2116+
path,
2117+
item.id,
2118+
DontForceInternal);
20702119
}
20712120
}
20722121
_ => {/* fall through */ }
20732122
}
20742123
}
20752124
2076-
pub fn trans_struct_def(ccx: @crate_ctxt, struct_def: @ast::struct_def,
2125+
pub fn trans_struct_def(ccx: @crate_ctxt,
2126+
struct_def: @ast::struct_def,
20772127
path: @ast_map::path,
2078-
id: ast::node_id) {
2128+
id: ast::node_id,
2129+
force_internal: ForceInternalFlag) {
20792130
// Translate the destructor.
20802131
do option::iter(&struct_def.dtor) |dtor| {
20812132
trans_struct_dtor(ccx, /*bad*/copy *path, &dtor.node.body,
@@ -2088,6 +2139,9 @@ pub fn trans_struct_def(ccx: @crate_ctxt, struct_def: @ast::struct_def,
20882139
// otherwise this is a unit-like struct.
20892140
Some(ctor_id) if struct_def.fields.len() > 0 => {
20902141
let llfndecl = get_item_val(ccx, ctor_id);
2142+
if force_internal == ForceInternal {
2143+
internalize_item(llfndecl);
2144+
}
20912145
trans_tuple_struct(ccx, /*bad*/copy struct_def.fields,
20922146
ctor_id, None, llfndecl);
20932147
}
@@ -2103,7 +2157,7 @@ pub fn trans_struct_def(ccx: @crate_ctxt, struct_def: @ast::struct_def,
21032157
pub fn trans_mod(ccx: @crate_ctxt, m: ast::_mod) {
21042158
let _icx = ccx.insn_ctxt("trans_mod");
21052159
for vec::each(m.items) |item| {
2106-
trans_item(ccx, **item);
2160+
trans_item(ccx, **item, DontForceInternal);
21072161
}
21082162
}
21092163
@@ -2477,8 +2531,9 @@ pub fn get_item_val(ccx: @crate_ctxt, id: ast::node_id) -> ValueRef {
24772531
ccx.sess.bug(~"get_item_val(): unexpected variant")
24782532
}
24792533
};
2480-
if !(exprt || ccx.reachable.contains_key(&id)) {
2481-
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
2534+
if !*ccx.sess.building_library ||
2535+
(!exprt && !ccx.reachable.contains_key(&id)) {
2536+
internalize_item(val);
24822537
}
24832538
ccx.item_vals.insert(id, val);
24842539
val

src/librustc/middle/trans/inline.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use core::prelude::*;
1212

1313
use middle::astencode;
14-
use middle::trans::base::{get_insn_ctxt};
14+
use middle::trans::base::{ForceInternal, get_insn_ctxt, internalize_item};
1515
use middle::trans::base::{impl_owned_self, impl_self, no_self};
1616
use middle::trans::base::{trans_item, get_item_val, self_arg, trans_fn};
1717
use middle::trans::common::*;
@@ -54,7 +54,7 @@ pub fn maybe_instantiate_inline(ccx: @crate_ctxt, fn_id: ast::def_id,
5454
csearch::found(ast::ii_item(item)) => {
5555
ccx.external.insert(fn_id, Some(item.id));
5656
ccx.stats.n_inlines += 1;
57-
if translate { trans_item(ccx, *item); }
57+
if translate { trans_item(ccx, *item, ForceInternal); }
5858
local_def(item.id)
5959
}
6060
csearch::found(ast::ii_foreign(item)) => {
@@ -64,9 +64,10 @@ pub fn maybe_instantiate_inline(ccx: @crate_ctxt, fn_id: ast::def_id,
6464
csearch::found_parent(parent_id, ast::ii_item(item)) => {
6565
ccx.external.insert(parent_id, Some(item.id));
6666
let mut my_id = 0;
67+
let vs_here;
6768
match item.node {
6869
ast::item_enum(_, _) => {
69-
let vs_here = ty::enum_variants(ccx.tcx, local_def(item.id));
70+
vs_here = ty::enum_variants(ccx.tcx, local_def(item.id));
7071
let vs_there = ty::enum_variants(ccx.tcx, parent_id);
7172
for vec::each2(*vs_here, *vs_there) |here, there| {
7273
if there.id == fn_id { my_id = here.id.node; }
@@ -76,7 +77,7 @@ pub fn maybe_instantiate_inline(ccx: @crate_ctxt, fn_id: ast::def_id,
7677
_ => ccx.sess.bug(~"maybe_instantiate_inline: item has a \
7778
non-enum parent")
7879
}
79-
if translate { trans_item(ccx, *item); }
80+
if translate { trans_item(ccx, *item, ForceInternal); }
8081
local_def(my_id)
8182
}
8283
csearch::found_parent(_, _) => {
@@ -90,6 +91,7 @@ pub fn maybe_instantiate_inline(ccx: @crate_ctxt, fn_id: ast::def_id,
9091
ty::lookup_item_type(ccx.tcx, impl_did);
9192
if translate && (*impl_bnds).len() + mth.tps.len() == 0u {
9293
let llfn = get_item_val(ccx, mth.id);
94+
internalize_item(llfn);
9395
let path = vec::append(
9496
ty::item_path(ccx.tcx, impl_did),
9597
~[path_name(mth.ident)]);
@@ -119,7 +121,7 @@ pub fn maybe_instantiate_inline(ccx: @crate_ctxt, fn_id: ast::def_id,
119121
local_def(mth.id)
120122
}
121123
csearch::found(ast::ii_dtor(ref dtor, _, _, _)) => {
122-
ccx.external.insert(fn_id, Some((*dtor).node.id));
124+
ccx.external.insert(fn_id, Some(dtor.node.id));
123125
local_def((*dtor).node.id)
124126
}
125127
}

src/librustc/middle/trans/meth.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,23 @@ for non-monomorphized methods only. Other methods will
5050
be generated once they are invoked with specific type parameters,
5151
see `trans::base::lval_static_fn()` or `trans::base::monomorphic_fn()`.
5252
*/
53-
pub fn trans_impl(ccx: @crate_ctxt, +path: path, name: ast::ident,
54-
methods: ~[@ast::method], tps: ~[ast::ty_param],
55-
self_ty: Option<ty::t>, id: ast::node_id) {
53+
pub fn trans_impl(ccx: @crate_ctxt,
54+
+path: path,
55+
name: ast::ident,
56+
methods: ~[@ast::method],
57+
tps: ~[ast::ty_param],
58+
self_ty: Option<ty::t>,
59+
id: ast::node_id,
60+
force_internal: ForceInternalFlag) {
5661
let _icx = ccx.insn_ctxt("impl::trans_impl");
5762
if tps.len() > 0u { return; }
5863
let sub_path = vec::append_one(path, path_name(name));
5964
for vec::each(methods) |method| {
6065
if method.tps.len() == 0u {
6166
let llfn = get_item_val(ccx, method.id);
67+
if force_internal == ForceInternal {
68+
internalize_item(llfn);
69+
}
6270
let path = vec::append_one(/*bad*/copy sub_path,
6371
path_name(method.ident));
6472

0 commit comments

Comments
 (0)