Skip to content

Commit 0107028

Browse files
committed
Resume inlining globals across crates
In #8185 cross-crate condition handlers were fixed by ensuring that globals didn't start appearing in different crates with different addressed. An unfortunate side effect of that pull request is that constants weren't inlined across crates (uint::bits is unknown to everything but libstd). This commit fixes this inlining by using the `available_eternally` linkage provided by LLVM. It partially reverts #8185, and then adds support for this linkage type. The main caveat is that not all statics could be inlined into other crates. Before this patch, all statics were considered "inlineable items", but an unfortunate side effect of how we deal with `&static` and `&[static]` means that these two cases cannot be inlined across crates. The translation of constants was modified to propogate this condition of whether a constant should be considered inlineable into other crates. Closes #9036
1 parent 3e1803f commit 0107028

File tree

9 files changed

+157
-59
lines changed

9 files changed

+157
-59
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub struct EncodeParams<'self> {
6060
reexports2: middle::resolve::ExportMap2,
6161
item_symbols: &'self HashMap<ast::NodeId, ~str>,
6262
discrim_symbols: &'self HashMap<ast::NodeId, @str>,
63+
non_inlineable_statics: &'self HashSet<ast::NodeId>,
6364
link_meta: &'self LinkMeta,
6465
cstore: @mut cstore::CStore,
6566
encode_inlined_item: encode_inlined_item<'self>,
@@ -89,6 +90,7 @@ pub struct EncodeContext<'self> {
8990
reexports2: middle::resolve::ExportMap2,
9091
item_symbols: &'self HashMap<ast::NodeId, ~str>,
9192
discrim_symbols: &'self HashMap<ast::NodeId, @str>,
93+
non_inlineable_statics: &'self HashSet<ast::NodeId>,
9294
link_meta: &'self LinkMeta,
9395
cstore: &'self cstore::CStore,
9496
encode_inlined_item: encode_inlined_item<'self>,
@@ -907,7 +909,9 @@ fn encode_info_for_item(ecx: &EncodeContext,
907909
encode_name(ecx, ebml_w, item.ident);
908910
let elt = ast_map::path_pretty_name(item.ident, item.id as u64);
909911
encode_path(ecx, ebml_w, path, elt);
910-
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
912+
if !ecx.non_inlineable_statics.contains(&item.id) {
913+
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
914+
}
911915
ebml_w.end_tag();
912916
}
913917
item_fn(_, purity, _, ref generics, _) => {
@@ -1728,6 +1732,7 @@ pub fn encode_metadata(parms: EncodeParams, crate: &Crate) -> ~[u8] {
17281732
encode_inlined_item,
17291733
link_meta,
17301734
reachable,
1735+
non_inlineable_statics,
17311736
_
17321737
} = parms;
17331738
let type_abbrevs = @mut HashMap::new();
@@ -1739,6 +1744,7 @@ pub fn encode_metadata(parms: EncodeParams, crate: &Crate) -> ~[u8] {
17391744
reexports2: reexports2,
17401745
item_symbols: item_symbols,
17411746
discrim_symbols: discrim_symbols,
1747+
non_inlineable_statics: non_inlineable_statics,
17421748
link_meta: link_meta,
17431749
cstore: cstore,
17441750
encode_inlined_item: encode_inlined_item,

src/librustc/middle/trans/_match.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,16 @@ fn trans_opt(bcx: @mut Block, o: &Opt) -> opt_result {
324324
return single_result(datumblock.to_result(bcx));
325325
}
326326
lit(ConstLit(lit_id)) => {
327-
let llval = consts::get_const_val(bcx.ccx(), lit_id);
327+
let (llval, _) = consts::get_const_val(bcx.ccx(), lit_id);
328328
return single_result(rslt(bcx, llval));
329329
}
330330
var(disr_val, repr) => {
331331
return adt::trans_case(bcx, repr, disr_val);
332332
}
333333
range(l1, l2) => {
334-
return range_result(rslt(bcx, consts::const_expr(ccx, l1)),
335-
rslt(bcx, consts::const_expr(ccx, l2)));
334+
let (l1, _) = consts::const_expr(ccx, l1);
335+
let (l2, _) = consts::const_expr(ccx, l2);
336+
return range_result(rslt(bcx, l1), rslt(bcx, l2));
336337
}
337338
vec_len(n, vec_len_eq, _) => {
338339
return single_result(rslt(bcx, C_int(ccx, n as int)));

src/librustc/middle/trans/base.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,12 +2494,29 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
24942494
let sym = exported_name(ccx, my_path, ty, i.attrs);
24952495

24962496
let v = match i.node {
2497-
ast::item_static(_, m, expr) => {
2497+
ast::item_static(_, _, expr) => {
2498+
// If this static came from an external crate, then
2499+
// we need to get the symbol from csearch instead of
2500+
// using the current crate's name/version
2501+
// information in the hash of the symbol
2502+
debug!("making %s", sym);
2503+
let sym = match ccx.external_srcs.find(&i.id) {
2504+
Some(&did) => {
2505+
debug!("but found in other crate...");
2506+
csearch::get_symbol(ccx.sess.cstore, did)
2507+
}
2508+
None => sym
2509+
};
2510+
24982511
// We need the translated value here, because for enums the
24992512
// LLVM type is not fully determined by the Rust type.
2500-
let v = consts::const_expr(ccx, expr);
2513+
let (v, inlineable) = consts::const_expr(ccx, expr);
25012514
ccx.const_values.insert(id, v);
2502-
exprt = (m == ast::MutMutable || i.vis == ast::public);
2515+
if !inlineable {
2516+
debug!("%s not inlined", sym);
2517+
ccx.non_inlineable_statics.insert(id);
2518+
}
2519+
exprt = true;
25032520

25042521
unsafe {
25052522
let llty = llvm::LLVMTypeOf(v);
@@ -2950,6 +2967,7 @@ pub fn crate_ctxt_to_encode_parms<'r>(cx: &'r CrateContext, ie: encoder::encode_
29502967
reexports2: cx.exp_map2,
29512968
item_symbols: item_symbols,
29522969
discrim_symbols: discrim_symbols,
2970+
non_inlineable_statics: &cx.non_inlineable_statics,
29532971
link_meta: link_meta,
29542972
cstore: cx.sess.cstore,
29552973
encode_inlined_item: ie,

0 commit comments

Comments
 (0)