Skip to content

Commit 717e883

Browse files
committed
Auto merge of #26018 - nrc:codegen, r=@nikomatsakis
Closes #19826 r? @nikomatsakis There is still some work to do until parallel codegen is perfect - we don't pass make check due to missing the symbol for registering a macro. But we do bootstrap with codegen_units=4 and pass make check without codegen_units, so I think it should land. For the curious, make rustc-stage2 -j8: ``` codegen_units=1: real 9m18.074s user 11m59.858s sys 0m13.281s codegen_units=4: real 6m3.672s user 13m5.474s sys 0m12.146s ``` Which is a 33% speedup :-)
2 parents dcc59c0 + 8d9a581 commit 717e883

File tree

14 files changed

+86
-89
lines changed

14 files changed

+86
-89
lines changed

src/librustc/metadata/tydecode.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ pub enum DefIdSource {
5050
// Identifies a type alias (`type X = ...`).
5151
TypeWithId,
5252

53-
// Identifies a type parameter (`fn foo<X>() { ... }`).
54-
TypeParameter,
55-
5653
// Identifies a region parameter (`fn foo<'X>() { ... }`).
5754
RegionParameter,
5855

@@ -193,7 +190,7 @@ pub fn parse_substs_data<'tcx, F>(data: &[u8], crate_num: ast::CrateNum, pos: us
193190
tcx: &ty::ctxt<'tcx>, conv: F) -> subst::Substs<'tcx> where
194191
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
195192
{
196-
debug!("parse_substs_data {}", data_log_string(data, pos));
193+
debug!("parse_substs_data{}", data_log_string(data, pos));
197194
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
198195
parse_substs(&mut st, conv)
199196
}
@@ -542,7 +539,14 @@ fn parse_ty_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F) -> Ty<'tcx> w
542539
len: len };
543540

544541
match tcx.rcache.borrow().get(&key).cloned() {
545-
Some(tt) => return tt,
542+
Some(tt) => {
543+
// If there is a closure buried in the type some where, then we
544+
// need to re-convert any def ids (see case 'k', below). That means
545+
// we can't reuse the cached version.
546+
if !ty::type_has_ty_closure(tt) {
547+
return tt;
548+
}
549+
}
546550
None => {}
547551
}
548552
let mut ps = PState {

src/librustc/middle/astencode.rs

+6-42
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use middle::def;
2020
use metadata::encoder as e;
2121
use middle::region;
2222
use metadata::tydecode;
23-
use metadata::tydecode::{DefIdSource, NominalType, TypeWithId, TypeParameter};
23+
use metadata::tydecode::{DefIdSource, NominalType, TypeWithId};
2424
use metadata::tydecode::{RegionParameter, ClosureSource};
2525
use metadata::tyencode;
2626
use middle::cast;
@@ -346,13 +346,6 @@ impl<D:serialize::Decoder> def_id_decoder_helpers for D
346346
// ______________________________________________________________________
347347
// Encoding and decoding the AST itself
348348
//
349-
// The hard work is done by an autogenerated module astencode_gen. To
350-
// regenerate astencode_gen, run src/etc/gen-astencode. It will
351-
// replace astencode_gen with a dummy file and regenerate its
352-
// contents. If you get compile errors, the dummy file
353-
// remains---resolve the errors and then rerun astencode_gen.
354-
// Annoying, I know, but hopefully only temporary.
355-
//
356349
// When decoding, we have to renumber the AST so that the node ids that
357350
// appear within are disjoint from the node ids in our existing ASTs.
358351
// We also have to adjust the spans: for now we just insert a dummy span,
@@ -656,35 +649,6 @@ impl<'a, 'tcx> read_method_callee_helper<'tcx> for reader::Decoder<'a> {
656649
}
657650
}
658651

659-
impl<'tcx> tr for MethodOrigin<'tcx> {
660-
fn tr(&self, dcx: &DecodeContext) -> MethodOrigin<'tcx> {
661-
match *self {
662-
ty::MethodStatic(did) => ty::MethodStatic(did.tr(dcx)),
663-
ty::MethodStaticClosure(did) => {
664-
ty::MethodStaticClosure(did.tr(dcx))
665-
}
666-
ty::MethodTypeParam(ref mp) => {
667-
ty::MethodTypeParam(
668-
ty::MethodParam {
669-
// def-id is already translated when we read it out
670-
trait_ref: mp.trait_ref.clone(),
671-
method_num: mp.method_num,
672-
impl_def_id: mp.impl_def_id.tr(dcx),
673-
}
674-
)
675-
}
676-
ty::MethodTraitObject(ref mo) => {
677-
ty::MethodTraitObject(
678-
ty::MethodObject {
679-
trait_ref: mo.trait_ref.clone(),
680-
.. *mo
681-
}
682-
)
683-
}
684-
}
685-
}
686-
}
687-
688652
pub fn encode_closure_kind(ebml_w: &mut Encoder, kind: ty::ClosureKind) {
689653
kind.encode(ebml_w).unwrap();
690654
}
@@ -1473,10 +1437,10 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
14731437
-> subst::Substs<'tcx> {
14741438
self.read_opaque(|this, doc| {
14751439
Ok(tydecode::parse_substs_data(doc.data,
1476-
dcx.cdata.cnum,
1477-
doc.start,
1478-
dcx.tcx,
1479-
|s, a| this.convert_def_id(dcx, s, a)))
1440+
dcx.cdata.cnum,
1441+
doc.start,
1442+
dcx.tcx,
1443+
|s, a| this.convert_def_id(dcx, s, a)))
14801444
}).unwrap()
14811445
}
14821446

@@ -1617,7 +1581,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
16171581
-> ast::DefId {
16181582
let r = match source {
16191583
NominalType | TypeWithId | RegionParameter => dcx.tr_def_id(did),
1620-
TypeParameter | ClosureSource => dcx.tr_intern_def_id(did)
1584+
ClosureSource => dcx.tr_intern_def_id(did)
16211585
};
16221586
debug!("convert_def_id(source={:?}, did={:?})={:?}", source, did, r);
16231587
return r;

src/librustc/middle/ty.rs

+5
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ bitflags! {
814814
const HAS_REGIONS = 1 << 5,
815815
const HAS_TY_ERR = 1 << 6,
816816
const HAS_PROJECTION = 1 << 7,
817+
const HAS_TY_CLOSURE = 1 << 8,
817818
const NEEDS_SUBST = TypeFlags::HAS_PARAMS.bits |
818819
TypeFlags::HAS_SELF.bits |
819820
TypeFlags::HAS_REGIONS.bits,
@@ -985,6 +986,9 @@ pub fn type_needs_infer(ty: Ty) -> bool {
985986
pub fn type_has_projection(ty: Ty) -> bool {
986987
ty.flags.get().intersects(TypeFlags::HAS_PROJECTION)
987988
}
989+
pub fn type_has_ty_closure(ty: Ty) -> bool {
990+
ty.flags.get().intersects(TypeFlags::HAS_TY_CLOSURE)
991+
}
988992

989993
pub fn type_has_late_bound_regions(ty: Ty) -> bool {
990994
ty.flags.get().intersects(TypeFlags::HAS_RE_LATE_BOUND)
@@ -2960,6 +2964,7 @@ impl FlagComputation {
29602964
}
29612965

29622966
&ty_closure(_, substs) => {
2967+
self.add_flags(TypeFlags::HAS_TY_CLOSURE);
29632968
self.add_substs(substs);
29642969
}
29652970

src/librustc/util/ppaux.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,18 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
304304
s
305305
}
306306

307-
fn closure_to_string<'tcx>(cx: &ctxt<'tcx>, cty: &ty::ClosureTy<'tcx>) -> String {
307+
fn closure_to_string<'tcx>(cx: &ctxt<'tcx>,
308+
cty: &ty::ClosureTy<'tcx>,
309+
did: &ast::DefId)
310+
-> String {
308311
let mut s = String::new();
309312
s.push_str("[closure");
310313
push_sig_to_string(cx, &mut s, '(', ')', &cty.sig);
311-
s.push(']');
314+
if cx.sess.verbose() {
315+
s.push_str(&format!(" id={:?}]", did));
316+
} else {
317+
s.push(']');
318+
}
312319
s
313320
}
314321

@@ -407,13 +414,20 @@ pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String {
407414
ty_closure(ref did, substs) => {
408415
let closure_tys = cx.closure_tys.borrow();
409416
closure_tys.get(did).map(|closure_type| {
410-
closure_to_string(cx, &closure_type.subst(cx, substs))
417+
closure_to_string(cx, &closure_type.subst(cx, substs), did)
411418
}).unwrap_or_else(|| {
419+
let id_str = if cx.sess.verbose() {
420+
format!(" id={:?}", did)
421+
} else {
422+
"".to_owned()
423+
};
424+
425+
412426
if did.krate == ast::LOCAL_CRATE {
413427
let span = cx.map.span(did.node);
414-
format!("[closure {}]", span.repr(cx))
428+
format!("[closure {}{}]", span.repr(cx), id_str)
415429
} else {
416-
format!("[closure]")
430+
format!("[closure{}]", id_str)
417431
}
418432
})
419433
}

src/librustc_trans/trans/callee.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
117117
_ => {
118118
bcx.tcx().sess.span_bug(
119119
expr.span,
120-
&format!("type of callee is neither bare-fn nor closure: \
121-
{}",
122-
bcx.ty_to_string(datum.ty)));
120+
&format!("type of callee is neither bare-fn nor closure: {}",
121+
bcx.ty_to_string(datum.ty)));
123122
}
124123
}
125124
}
@@ -506,6 +505,9 @@ pub fn trans_fn_ref_with_substs<'a, 'tcx>(
506505
false
507506
};
508507

508+
debug!("trans_fn_ref_with_substs({}) must_monomorphise: {}",
509+
def_id.repr(tcx), must_monomorphise);
510+
509511
// Create a monomorphic version of generic functions
510512
if must_monomorphise {
511513
// Should be either intra-crate or inlined.

src/librustc_trans/trans/closure.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ pub fn get_or_create_declaration_if_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tc
153153

154154
match ccx.closure_vals().borrow().get(&mono_id) {
155155
Some(&llfn) => {
156-
debug!("get_or_create_declaration_if_closure(): found closure");
156+
debug!("get_or_create_declaration_if_closure(): found closure {:?}: {:?}",
157+
mono_id, ccx.tn().val_to_string(llfn));
157158
return Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
158159
}
159160
None => {}
@@ -173,9 +174,10 @@ pub fn get_or_create_declaration_if_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tc
173174
attributes::inline(llfn, attributes::InlineAttr::Hint);
174175

175176
debug!("get_or_create_declaration_if_closure(): inserting new \
176-
closure {:?} (type {})",
177+
closure {:?} (type {}): {:?}",
177178
mono_id,
178-
ccx.tn().type_to_string(val_ty(llfn)));
179+
ccx.tn().type_to_string(val_ty(llfn)),
180+
ccx.tn().val_to_string(llfn));
179181
ccx.closure_vals().borrow_mut().insert(mono_id, llfn);
180182

181183
Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
@@ -198,9 +200,9 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
198200
Dest::Ignore(ccx) => ccx
199201
};
200202
let tcx = ccx.tcx();
201-
let _icx = push_ctxt("closure::trans_closure");
203+
let _icx = push_ctxt("closure::trans_closure_expr");
202204

203-
debug!("trans_closure()");
205+
debug!("trans_closure_expr()");
204206

205207
let closure_id = ast_util::local_def(id);
206208
let llfn = get_or_create_declaration_if_closure(
@@ -230,15 +232,15 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
230232
&[],
231233
sig.output,
232234
function_type.abi,
233-
ClosureEnv::Closure(&freevars[..]));
235+
ClosureEnv::Closure(&freevars));
234236

235237
// Don't hoist this to the top of the function. It's perfectly legitimate
236238
// to have a zero-size closure (in which case dest will be `Ignore`) and
237239
// we must still generate the closure body.
238240
let (mut bcx, dest_addr) = match dest {
239241
Dest::SaveIn(bcx, p) => (bcx, p),
240242
Dest::Ignore(_) => {
241-
debug!("trans_closure() ignoring result");
243+
debug!("trans_closure_expr() ignoring result");
242244
return None;
243245
}
244246
};

src/librustc_trans/trans/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,8 @@ pub fn fulfill_obligation<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
10001000
None => { }
10011001
}
10021002

1003-
debug!("trans fulfill_obligation: trait_ref={}", trait_ref.repr(ccx.tcx()));
1003+
debug!("trans fulfill_obligation: trait_ref={} def_id={:?}",
1004+
trait_ref.repr(ccx.tcx()), trait_ref.def_id());
10041005

10051006
ty::populate_implementations_for_trait_if_necessary(tcx, trait_ref.def_id());
10061007
let infcx = infer::new_infer_ctxt(tcx);

src/librustc_trans/trans/consts.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,16 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
221221
qualif: check_const::ConstQualif,
222222
param_substs: &'tcx Substs<'tcx>)
223223
-> ValueRef {
224+
debug!("get_const_expr_as_global: {:?}", expr.id);
224225
// Special-case constants to cache a common global for all uses.
225226
match expr.node {
226227
ast::ExprPath(..) => {
227228
let def = ccx.tcx().def_map.borrow().get(&expr.id).unwrap().full_def();
228229
match def {
229230
def::DefConst(def_id) | def::DefAssociatedConst(def_id, _) => {
230231
if !ccx.tcx().adjustments.borrow().contains_key(&expr.id) {
232+
debug!("get_const_expr_as_global ({:?}): found const {:?}",
233+
expr.id, def_id);
231234
return get_const_val(ccx, def_id, expr);
232235
}
233236
}
@@ -911,7 +914,9 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
911914
}
912915
ast::ExprClosure(_, ref decl, ref body) => {
913916
closure::trans_closure_expr(closure::Dest::Ignore(cx),
914-
&**decl, &**body, e.id,
917+
decl,
918+
body,
919+
e.id,
915920
param_substs);
916921
C_null(type_of::type_of(cx, ety))
917922
}

src/librustc_trans/trans/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub struct SharedCrateContext<'tcx> {
7373
check_overflow: bool,
7474
check_drop_flag_for_sanity: bool,
7575

76-
available_monomorphizations: RefCell<FnvHashSet<String>>,
7776
available_drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, String>>,
7877
use_dll_storage_attrs: bool,
7978
}
@@ -100,6 +99,7 @@ pub struct LocalCrateContext<'tcx> {
10099
/// Cache instances of monomorphized functions
101100
monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
102101
monomorphizing: RefCell<DefIdMap<usize>>,
102+
available_monomorphizations: RefCell<FnvHashSet<String>>,
103103
/// Cache generated vtables
104104
vtables: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>>,
105105
/// Cache of constant strings,
@@ -321,7 +321,6 @@ impl<'tcx> SharedCrateContext<'tcx> {
321321
},
322322
check_overflow: check_overflow,
323323
check_drop_flag_for_sanity: check_drop_flag_for_sanity,
324-
available_monomorphizations: RefCell::new(FnvHashSet()),
325324
available_drop_glues: RefCell::new(FnvHashMap()),
326325
use_dll_storage_attrs: use_dll_storage_attrs,
327326
};
@@ -452,6 +451,7 @@ impl<'tcx> LocalCrateContext<'tcx> {
452451
external_srcs: RefCell::new(NodeMap()),
453452
monomorphized: RefCell::new(FnvHashMap()),
454453
monomorphizing: RefCell::new(DefIdMap()),
454+
available_monomorphizations: RefCell::new(FnvHashSet()),
455455
vtables: RefCell::new(FnvHashMap()),
456456
const_cstr_cache: RefCell::new(FnvHashMap()),
457457
const_unsized: RefCell::new(FnvHashMap()),
@@ -709,7 +709,7 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
709709
}
710710

711711
pub fn available_monomorphizations<'a>(&'a self) -> &'a RefCell<FnvHashSet<String>> {
712-
&self.shared.available_monomorphizations
712+
&self.local.available_monomorphizations
713713
}
714714

715715
pub fn available_drop_glues(&self) -> &RefCell<FnvHashMap<DropGlueKind<'tcx>, String>> {

src/librustc_trans/trans/controlflow.rs

-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use syntax::ast;
3030
use syntax::ast_util;
3131
use syntax::parse::token::InternedString;
3232
use syntax::parse::token;
33-
use syntax::visit::Visitor;
3433

3534
pub fn trans_stmt<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
3635
s: &ast::Stmt)
@@ -171,16 +170,7 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
171170
// if true { .. } [else { .. }]
172171
bcx = trans_block(bcx, &*thn, dest);
173172
trans::debuginfo::clear_source_location(bcx.fcx);
174-
175-
if let Some(elexpr) = els {
176-
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
177-
trans.visit_expr(&*elexpr);
178-
}
179173
} else {
180-
// if false { .. } [else { .. }]
181-
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
182-
trans.visit_block(&*thn);
183-
184174
if let Some(elexpr) = els {
185175
bcx = expr::trans_into(bcx, &*elexpr, dest);
186176
trans::debuginfo::clear_source_location(bcx.fcx);

src/librustc_trans/trans/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11451145
SaveIn(lldest) => closure::Dest::SaveIn(bcx, lldest),
11461146
Ignore => closure::Dest::Ignore(bcx.ccx())
11471147
};
1148-
closure::trans_closure_expr(dest, &**decl, &**body, expr.id, bcx.fcx.param_substs)
1148+
closure::trans_closure_expr(dest, decl, body, expr.id, bcx.fcx.param_substs)
11491149
.unwrap_or(bcx)
11501150
}
11511151
ast::ExprCall(ref f, ref args) => {
@@ -1956,6 +1956,7 @@ fn trans_overloaded_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
19561956
args: &'a [P<ast::Expr>],
19571957
dest: Option<Dest>)
19581958
-> Block<'blk, 'tcx> {
1959+
debug!("trans_overloaded_call {}", expr.id);
19591960
let method_call = MethodCall::expr(expr.id);
19601961
let method_type = bcx.tcx()
19611962
.method_map

0 commit comments

Comments
 (0)