Skip to content

Commit fbb70d9

Browse files
committed
librustc: De-@mut the reachable map
1 parent 4173785 commit fbb70d9

File tree

6 files changed

+80
-36
lines changed

6 files changed

+80
-36
lines changed

src/librustc/driver/driver.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub struct CrateAnalysis {
212212
exported_items: middle::privacy::ExportedItems,
213213
ty_cx: ty::ctxt,
214214
maps: astencode::Maps,
215-
reachable: @mut HashSet<ast::NodeId>
215+
reachable: @RefCell<HashSet<ast::NodeId>>
216216
}
217217

218218
/// Run the resolution, typechecking, region checking and other
@@ -309,9 +309,16 @@ pub fn phase_3_run_analysis_passes(sess: Session,
309309
time(time_passes, "reachability checking", (), |_|
310310
reachable::find_reachable(ty_cx, method_map, &exported_items));
311311

312-
time(time_passes, "death checking", (), |_|
313-
middle::dead::check_crate(ty_cx, method_map,
314-
&exported_items, reachable_map, crate));
312+
{
313+
let reachable_map = reachable_map.borrow();
314+
time(time_passes, "death checking", (), |_| {
315+
middle::dead::check_crate(ty_cx,
316+
method_map,
317+
&exported_items,
318+
reachable_map.get(),
319+
crate)
320+
});
321+
}
315322

316323
time(time_passes, "lint checking", (), |_|
317324
lint::check_crate(ty_cx, method_map, &exported_items, crate));

src/librustc/metadata/encoder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct EncodeParams<'a> {
6565
link_meta: &'a LinkMeta,
6666
cstore: @cstore::CStore,
6767
encode_inlined_item: encode_inlined_item<'a>,
68-
reachable: @mut HashSet<ast::NodeId>,
68+
reachable: @RefCell<HashSet<ast::NodeId>>,
6969
}
7070

7171
struct Stats {
@@ -95,11 +95,12 @@ pub struct EncodeContext<'a> {
9595
cstore: &'a cstore::CStore,
9696
encode_inlined_item: encode_inlined_item<'a>,
9797
type_abbrevs: abbrev_map,
98-
reachable: @mut HashSet<ast::NodeId>,
98+
reachable: @RefCell<HashSet<ast::NodeId>>,
9999
}
100100

101101
pub fn reachable(ecx: &EncodeContext, id: NodeId) -> bool {
102-
ecx.reachable.contains(&id)
102+
let reachable = ecx.reachable.borrow();
103+
reachable.get().contains(&id)
103104
}
104105

105106
fn encode_name(ecx: &EncodeContext,

src/librustc/middle/reachable.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use middle::ty;
1919
use middle::typeck;
2020
use middle::privacy;
2121

22+
use std::cell::RefCell;
2223
use std::hashmap::HashSet;
2324
use syntax::ast;
2425
use syntax::ast_map;
@@ -84,7 +85,7 @@ struct ReachableContext {
8485
// methods they've been resolved to.
8586
method_map: typeck::method_map,
8687
// The set of items which must be exported in the linkage sense.
87-
reachable_symbols: @mut HashSet<ast::NodeId>,
88+
reachable_symbols: @RefCell<HashSet<ast::NodeId>>,
8889
// A worklist of item IDs. Each item ID in this worklist will be inlined
8990
// and will be scanned for further references.
9091
worklist: @mut ~[ast::NodeId],
@@ -94,7 +95,7 @@ struct MarkSymbolVisitor {
9495
worklist: @mut ~[ast::NodeId],
9596
method_map: typeck::method_map,
9697
tcx: ty::ctxt,
97-
reachable_symbols: @mut HashSet<ast::NodeId>,
98+
reachable_symbols: @RefCell<HashSet<ast::NodeId>>,
9899
}
99100

100101
impl Visitor<()> for MarkSymbolVisitor {
@@ -129,7 +130,9 @@ impl Visitor<()> for MarkSymbolVisitor {
129130
// If this wasn't a static, then this destination is
130131
// surely reachable.
131132
_ => {
132-
self.reachable_symbols.insert(def_id.node);
133+
let mut reachable_symbols =
134+
self.reachable_symbols.borrow_mut();
135+
reachable_symbols.get().insert(def_id.node);
133136
}
134137
}
135138
}
@@ -146,9 +149,13 @@ impl Visitor<()> for MarkSymbolVisitor {
146149
def_id_represents_local_inlined_item(
147150
self.tcx,
148151
def_id) {
149-
self.worklist.push(def_id.node)
150-
}
151-
self.reachable_symbols.insert(def_id.node);
152+
self.worklist.push(def_id.node)
153+
}
154+
{
155+
let mut reachable_symbols =
156+
self.reachable_symbols.borrow_mut();
157+
reachable_symbols.get().insert(def_id.node);
158+
}
152159
}
153160
}
154161
Some(_) => {}
@@ -177,7 +184,7 @@ impl ReachableContext {
177184
ReachableContext {
178185
tcx: tcx,
179186
method_map: method_map,
180-
reachable_symbols: @mut HashSet::new(),
187+
reachable_symbols: @RefCell::new(HashSet::new()),
181188
worklist: @mut ~[],
182189
}
183190
}
@@ -289,7 +296,9 @@ impl ReachableContext {
289296
ast_map::node_item(item, _) => {
290297
match item.node {
291298
ast::item_fn(_, ast::extern_fn, _, _, _) => {
292-
self.reachable_symbols.insert(search_item);
299+
let mut reachable_symbols =
300+
self.reachable_symbols.borrow_mut();
301+
reachable_symbols.get().insert(search_item);
293302
}
294303
_ => {}
295304
}
@@ -301,7 +310,8 @@ impl ReachableContext {
301310
// continue to participate in linkage after this product is
302311
// produced. In this case, we traverse the ast node, recursing on
303312
// all reachable nodes from this one.
304-
self.reachable_symbols.insert(search_item);
313+
let mut reachable_symbols = self.reachable_symbols.borrow_mut();
314+
reachable_symbols.get().insert(search_item);
305315
}
306316

307317
match *node {
@@ -318,7 +328,9 @@ impl ReachableContext {
318328
ast::item_static(..) => {
319329
if attr::contains_name(item.attrs,
320330
"address_insignificant") {
321-
self.reachable_symbols.remove(&search_item);
331+
let mut reachable_symbols =
332+
self.reachable_symbols.borrow_mut();
333+
reachable_symbols.get().remove(&search_item);
322334
}
323335
}
324336

@@ -377,7 +389,9 @@ impl ReachableContext {
377389
let destructor_for_type = self.tcx.destructor_for_type.borrow();
378390
for (_, destructor_def_id) in destructor_for_type.get().iter() {
379391
if destructor_def_id.crate == ast::LOCAL_CRATE {
380-
self.reachable_symbols.insert(destructor_def_id.node);
392+
let mut reachable_symbols = self.reachable_symbols
393+
.borrow_mut();
394+
reachable_symbols.get().insert(destructor_def_id.node);
381395
}
382396
}
383397
}
@@ -386,7 +400,7 @@ impl ReachableContext {
386400
pub fn find_reachable(tcx: ty::ctxt,
387401
method_map: typeck::method_map,
388402
exported_items: &privacy::ExportedItems)
389-
-> @mut HashSet<ast::NodeId> {
403+
-> @RefCell<HashSet<ast::NodeId>> {
390404
let reachable_context = ReachableContext::new(tcx, method_map);
391405

392406
// Step 1: Seed the worklist with all nodes which were found to be public as

src/librustc/middle/trans/base.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,8 +2318,11 @@ fn finish_register_fn(ccx: @CrateContext, sp: Span, sym: ~str, node_id: ast::Nod
23182318
item_symbols.get().insert(node_id, sym);
23192319
}
23202320

2321-
if !ccx.reachable.contains(&node_id) {
2322-
lib::llvm::SetLinkage(llfn, lib::llvm::InternalLinkage);
2321+
{
2322+
let reachable = ccx.reachable.borrow();
2323+
if !reachable.get().contains(&node_id) {
2324+
lib::llvm::SetLinkage(llfn, lib::llvm::InternalLinkage);
2325+
}
23232326
}
23242327

23252328
if is_entry_fn(&ccx.sess, node_id) && !*ccx.sess.building_library {
@@ -2531,18 +2534,27 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::NodeId) -> ValueRef {
25312534
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
25322535
});
25332536

2534-
if !ccx.reachable.contains(&id) {
2535-
lib::llvm::SetLinkage(g, lib::llvm::InternalLinkage);
2537+
{
2538+
let reachable = ccx.reachable.borrow();
2539+
if !reachable.get().contains(&id) {
2540+
lib::llvm::SetLinkage(
2541+
g,
2542+
lib::llvm::InternalLinkage);
2543+
}
25362544
}
25372545

25382546
// Apply the `unnamed_addr` attribute if
25392547
// requested
25402548
if attr::contains_name(i.attrs,
25412549
"address_insignificant"){
2542-
if ccx.reachable.contains(&id) {
2543-
ccx.sess.span_bug(i.span,
2544-
"insignificant static is \
2545-
reachable");
2550+
{
2551+
let reachable =
2552+
ccx.reachable.borrow();
2553+
if reachable.get().contains(&id) {
2554+
ccx.sess.span_bug(i.span,
2555+
"insignificant static is \
2556+
reachable");
2557+
}
25462558
}
25472559
lib::llvm::SetUnnamedAddr(g, true);
25482560

@@ -2731,8 +2743,11 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::NodeId) -> ValueRef {
27312743
// foreign items (extern fns and extern statics) don't have internal
27322744
// linkage b/c that doesn't quite make sense. Otherwise items can
27332745
// have internal linkage if they're not reachable.
2734-
if !foreign && !ccx.reachable.contains(&id) {
2735-
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
2746+
{
2747+
let reachable = ccx.reachable.borrow();
2748+
if !foreign && !reachable.get().contains(&id) {
2749+
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
2750+
}
27362751
}
27372752

27382753
let mut item_vals = ccx.item_vals.borrow_mut();
@@ -3245,10 +3260,14 @@ pub fn trans_crate(sess: session::Session,
32453260
let llcx = ccx.llcx;
32463261
let link_meta = ccx.link_meta.clone();
32473262
let llmod = ccx.llmod;
3248-
let mut reachable = ccx.reachable.iter().filter_map(|id| {
3249-
let item_symbols = ccx.item_symbols.borrow();
3250-
item_symbols.get().find(id).map(|s| s.to_owned())
3251-
}).to_owned_vec();
3263+
3264+
let mut reachable = {
3265+
let reachable_map = ccx.reachable.borrow();
3266+
reachable_map.get().iter().filter_map(|id| {
3267+
let item_symbols = ccx.item_symbols.borrow();
3268+
item_symbols.get().find(id).map(|s| s.to_owned())
3269+
}).to_owned_vec()
3270+
};
32523271

32533272
// Make sure that some other crucial symbols are not eliminated from the
32543273
// module. This includes the main function, the crate map (used for debug

src/librustc/middle/trans/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct CrateContext {
4949
intrinsics: HashMap<&'static str, ValueRef>,
5050
item_vals: RefCell<HashMap<ast::NodeId, ValueRef>>,
5151
exp_map2: resolve::ExportMap2,
52-
reachable: @mut HashSet<ast::NodeId>,
52+
reachable: @RefCell<HashSet<ast::NodeId>>,
5353
item_symbols: RefCell<HashMap<ast::NodeId, ~str>>,
5454
link_meta: LinkMeta,
5555
tydescs: RefCell<HashMap<ty::t, @mut tydesc_info>>,
@@ -123,7 +123,7 @@ impl CrateContext {
123123
maps: astencode::Maps,
124124
symbol_hasher: Sha256,
125125
link_meta: LinkMeta,
126-
reachable: @mut HashSet<ast::NodeId>)
126+
reachable: @RefCell<HashSet<ast::NodeId>>)
127127
-> CrateContext {
128128
unsafe {
129129
let llcx = llvm::LLVMContextCreate();

src/librustc/middle/trans/debuginfo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,10 @@ pub fn create_function_debug_context(cx: &CrateContext,
732732
// (by being externally visible or by being inlined into something externally visible). It might
733733
// better to use the `exported_items` set from `driver::CrateAnalysis` in the future, but (atm)
734734
// this set is not available in the translation pass.
735-
let is_local_to_unit = !cx.reachable.contains(&fn_ast_id);
735+
let is_local_to_unit = {
736+
let reachable = cx.reachable.borrow();
737+
!reachable.get().contains(&fn_ast_id)
738+
};
736739

737740
let fn_metadata = function_name.with_c_str(|function_name| {
738741
linkage_name.with_c_str(|linkage_name| {

0 commit comments

Comments
 (0)