Skip to content

Commit d1c0550

Browse files
committed
auto merge of #9130 : alexcrichton/rust/inline-globals, r=thestinger
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
2 parents 66ecff2 + 0107028 commit d1c0550

File tree

9 files changed

+157
-59
lines changed

9 files changed

+157
-59
lines changed

src/librustc/metadata/encoder.rs

+7-1
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

+4-3
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

+21-3
Original file line numberDiff line numberDiff line change
@@ -2541,12 +2541,29 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
25412541
let sym = exported_name(ccx, my_path, ty, i.attrs);
25422542

25432543
let v = match i.node {
2544-
ast::item_static(_, m, expr) => {
2544+
ast::item_static(_, _, expr) => {
2545+
// If this static came from an external crate, then
2546+
// we need to get the symbol from csearch instead of
2547+
// using the current crate's name/version
2548+
// information in the hash of the symbol
2549+
debug!("making %s", sym);
2550+
let sym = match ccx.external_srcs.find(&i.id) {
2551+
Some(&did) => {
2552+
debug!("but found in other crate...");
2553+
csearch::get_symbol(ccx.sess.cstore, did)
2554+
}
2555+
None => sym
2556+
};
2557+
25452558
// We need the translated value here, because for enums the
25462559
// LLVM type is not fully determined by the Rust type.
2547-
let v = consts::const_expr(ccx, expr);
2560+
let (v, inlineable) = consts::const_expr(ccx, expr);
25482561
ccx.const_values.insert(id, v);
2549-
exprt = (m == ast::MutMutable || i.vis == ast::public);
2562+
if !inlineable {
2563+
debug!("%s not inlined", sym);
2564+
ccx.non_inlineable_statics.insert(id);
2565+
}
2566+
exprt = true;
25502567

25512568
unsafe {
25522569
let llty = llvm::LLVMTypeOf(v);
@@ -2997,6 +3014,7 @@ pub fn crate_ctxt_to_encode_parms<'r>(cx: &'r CrateContext, ie: encoder::encode_
29973014
reexports2: cx.exp_map2,
29983015
item_symbols: item_symbols,
29993016
discrim_symbols: discrim_symbols,
3017+
non_inlineable_statics: &cx.non_inlineable_statics,
30003018
link_meta: link_meta,
30013019
cstore: cx.sess.cstore,
30023020
encode_inlined_item: ie,

0 commit comments

Comments
 (0)