Skip to content

Commit ca270ec

Browse files
committed
Modify trans to use an outer walk and ensure that we rotate as we
encounter each module. This is somewhat different than how it used to work; it should ensure a more equitable distribution of work than before. The reason is that, before, when we rotated, we would rotate before we had seen the full contents of the current module. So e.g. if we have `mod a { mod b { .. } .. }`, then we rotate when we encounter `b`, but we haven't processed the remainder of `a` yet. Unclear if this makes any difference in practice, but it seemed suboptimal. Also, this structure (with an outer walk over modules) is closer to what we will want for an incremental setting.
1 parent 1a262bf commit ca270ec

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

src/librustc_trans/trans/base.rs

+54-12
Original file line numberDiff line numberDiff line change
@@ -2142,16 +2142,6 @@ fn enum_variant_size_lint(ccx: &CrateContext, enum_def: &hir::EnumDef, sp: Span,
21422142
}
21432143
}
21442144

2145-
pub struct TransItemVisitor<'a, 'tcx: 'a> {
2146-
pub ccx: &'a CrateContext<'a, 'tcx>,
2147-
}
2148-
2149-
impl<'a, 'tcx, 'v> Visitor<'v> for TransItemVisitor<'a, 'tcx> {
2150-
fn visit_item(&mut self, i: &hir::Item) {
2151-
trans_item(self.ccx, i);
2152-
}
2153-
}
2154-
21552145
pub fn llvm_linkage_by_name(name: &str) -> Option<Linkage> {
21562146
// Use the names from src/llvm/docs/LangRef.rst here. Most types are only
21572147
// applicable to variable declarations and may not really make sense for
@@ -2963,10 +2953,12 @@ pub fn trans_crate<'tcx>(tcx: &ty::ctxt<'tcx>,
29632953
// First, verify intrinsics.
29642954
intrinsic::check_intrinsics(&ccx);
29652955

2966-
// Next, translate all items.
2956+
// Next, translate all items. See `TransModVisitor` for
2957+
// details on why we walk in this particular way.
29672958
{
29682959
let _icx = push_ctxt("text");
2969-
krate.visit_all_items(&mut TransItemVisitor { ccx: &ccx });
2960+
intravisit::walk_mod(&mut TransItemsWithinModVisitor { ccx: &ccx }, &krate.module);
2961+
krate.visit_all_items(&mut TransModVisitor { ccx: &ccx });
29702962
}
29712963
}
29722964

@@ -3069,3 +3061,53 @@ pub fn trans_crate<'tcx>(tcx: &ty::ctxt<'tcx>,
30693061
no_builtins: no_builtins,
30703062
}
30713063
}
3064+
3065+
/// We visit all the items in the krate and translate them. We do
3066+
/// this in two walks. The first walk just finds module items. It then
3067+
/// walks the full contents of those module items and translates all
3068+
/// the items within. Note that this entire process is O(n). The
3069+
/// reason for this two phased walk is that each module is
3070+
/// (potentially) placed into a distinct codegen-unit. This walk also
3071+
/// ensures that the immediate contents of each module is processed
3072+
/// entirely before we proceed to find more modules, helping to ensure
3073+
/// an equitable distribution amongst codegen-units.
3074+
pub struct TransModVisitor<'a, 'tcx: 'a> {
3075+
pub ccx: &'a CrateContext<'a, 'tcx>,
3076+
}
3077+
3078+
impl<'a, 'tcx, 'v> Visitor<'v> for TransModVisitor<'a, 'tcx> {
3079+
fn visit_item(&mut self, i: &hir::Item) {
3080+
match i.node {
3081+
hir::ItemMod(_) => {
3082+
let item_ccx = self.ccx.rotate();
3083+
intravisit::walk_item(&mut TransItemsWithinModVisitor { ccx: &item_ccx }, i);
3084+
}
3085+
_ => { }
3086+
}
3087+
}
3088+
}
3089+
3090+
/// Translates all the items within a given module. Expects owner to
3091+
/// invoke `walk_item` on a module item. Ignores nested modules.
3092+
pub struct TransItemsWithinModVisitor<'a, 'tcx: 'a> {
3093+
pub ccx: &'a CrateContext<'a, 'tcx>,
3094+
}
3095+
3096+
impl<'a, 'tcx, 'v> Visitor<'v> for TransItemsWithinModVisitor<'a, 'tcx> {
3097+
fn visit_nested_item(&mut self, item_id: hir::ItemId) {
3098+
self.visit_item(self.ccx.tcx().map.expect_item(item_id.id));
3099+
}
3100+
3101+
fn visit_item(&mut self, i: &hir::Item) {
3102+
match i.node {
3103+
hir::ItemMod(..) => {
3104+
// skip modules, they will be uncovered by the TransModVisitor
3105+
}
3106+
_ => {
3107+
trans_item(self.ccx, i);
3108+
intravisit::walk_item(self, i);
3109+
}
3110+
}
3111+
}
3112+
}
3113+

0 commit comments

Comments
 (0)