Skip to content

Commit ad212ec

Browse files
committed
auto merge of #7822 : huonw/rust/cond-debug, r=graydon
As per @pcwalton's request, `debug!(..)` is only activated when the `debug` cfg is set; that is, for `RUST_LOG=some_module=4 ./some_program` to work, it needs to be compiled with `rustc --cfg debug some_program.rs`. (Although, there is the sneaky `__debug!(..)` macro that is always active, if you *really* need it.) It functions by making `debug!` expand to `if false { __debug!(..) }` (expanding to an `if` like this is required to make sure `debug!` statements are typechecked and to avoid unused variable warnings), and adjusting trans to skip the pointless branches in `if true ...` and `if false ...`. The conditional expansion change also required moving the inject-std-macros step into a new pass, and makes it actually insert them at the top of the crate; this means that the cfg stripping traverses over the macros and so filters out the unused ones. This appears to takes an unoptimised build of `librustc` from 65s to 59s; and the full bootstrap from 18m41s to 18m26s on my computer (with general background use). `./configure --enable-debug` will enable `debug!` statements in the bootstrap build.
2 parents a317584 + 4797dd4 commit ad212ec

File tree

226 files changed

+839
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+839
-683
lines changed

Diff for: src/librustc/driver/driver.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ pub fn compile_rest(sess: Session,
194194
// mod bar { macro_rules! baz!(() => {{}}) }
195195
//
196196
// baz! should not use this definition unless foo is enabled.
197+
crate = time(time_passes, ~"std macros injection", ||
198+
syntax::ext::expand::inject_std_macros(sess.parse_sess, copy cfg,
199+
crate));
200+
197201
crate = time(time_passes, ~"configuration 1", ||
198202
front::config::strip_unconfigured_items(crate));
199203

@@ -214,7 +218,7 @@ pub fn compile_rest(sess: Session,
214218
assert!(phases.from != cu_no_trans);
215219

216220
let (llcx, llmod, link_meta) = {
217-
crate = time(time_passes, ~"extra injection", ||
221+
crate = time(time_passes, ~"std injection", ||
218222
front::std_inject::maybe_inject_libstd_ref(sess, crate));
219223

220224
let ast_map = time(time_passes, ~"ast indexing", ||

Diff for: src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ pub fn alloca_maybe_zeroed(cx: block, ty: Type, name: &str, zero: bool) -> Value
15301530
let _icx = push_ctxt("alloca");
15311531
if cx.unreachable {
15321532
unsafe {
1533-
return llvm::LLVMGetUndef(ty.to_ref());
1533+
return llvm::LLVMGetUndef(ty.ptr_to().to_ref());
15341534
}
15351535
}
15361536
let initcx = base::raw_block(cx.fcx, false, cx.fcx.get_llstaticallocas());

Diff for: src/librustc/middle/trans/build.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,17 @@ pub fn LoadRangeAssert(cx: block, PointerVal: ValueRef, lo: c_ulonglong,
569569
hi: c_ulonglong, signed: lib::llvm::Bool) -> ValueRef {
570570
let value = Load(cx, PointerVal);
571571

572-
unsafe {
573-
let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(PointerVal));
574-
let min = llvm::LLVMConstInt(t, lo, signed);
575-
let max = llvm::LLVMConstInt(t, hi, signed);
576-
577-
do [min, max].as_imm_buf |ptr, len| {
578-
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
579-
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
580-
ptr, len as c_uint));
572+
if !cx.unreachable {
573+
unsafe {
574+
let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(PointerVal));
575+
let min = llvm::LLVMConstInt(t, lo, signed);
576+
let max = llvm::LLVMConstInt(t, hi, signed);
577+
578+
do [min, max].as_imm_buf |ptr, len| {
579+
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
580+
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
581+
ptr, len as c_uint));
582+
}
581583
}
582584
}
583585

Diff for: src/librustc/middle/trans/controlflow.rs

+55-17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ pub fn trans_if(bcx: block,
6363
let _indenter = indenter();
6464

6565
let _icx = push_ctxt("trans_if");
66+
67+
match cond.node {
68+
// `if true` and `if false` can be trans'd more efficiently,
69+
// by dropping branches that are known to be impossible.
70+
ast::expr_lit(@ref l) => match l.node {
71+
ast::lit_bool(true) => {
72+
// if true { .. } [else { .. }]
73+
let then_bcx_in = scope_block(bcx, thn.info(), "if_true_then");
74+
let then_bcx_out = trans_block(then_bcx_in, thn, dest);
75+
let then_bcx_out = trans_block_cleanups(then_bcx_out,
76+
block_cleanups(then_bcx_in));
77+
Br(bcx, then_bcx_in.llbb);
78+
return then_bcx_out;
79+
}
80+
ast::lit_bool(false) => {
81+
match els {
82+
// if false { .. } else { .. }
83+
Some(elexpr) => {
84+
let (else_bcx_in, else_bcx_out) =
85+
trans_if_else(bcx, elexpr, dest, "if_false_else");
86+
Br(bcx, else_bcx_in.llbb);
87+
return else_bcx_out;
88+
}
89+
// if false { .. }
90+
None => return bcx,
91+
}
92+
}
93+
_ => {}
94+
},
95+
_ => {}
96+
}
97+
6698
let Result {bcx, val: cond_val} =
6799
expr::trans_to_datum(bcx, cond).to_result();
68100

@@ -80,22 +112,8 @@ pub fn trans_if(bcx: block,
80112
// 'else' context
81113
let (else_bcx_in, next_bcx) = match els {
82114
Some(elexpr) => {
83-
let else_bcx_in = scope_block(bcx, els.info(), "else");
84-
let else_bcx_out = match elexpr.node {
85-
ast::expr_if(_, _, _) => {
86-
let elseif_blk = ast_util::block_from_expr(elexpr);
87-
trans_block(else_bcx_in, &elseif_blk, dest)
88-
}
89-
ast::expr_block(ref blk) => {
90-
trans_block(else_bcx_in, blk, dest)
91-
}
92-
// would be nice to have a constraint on ifs
93-
_ => bcx.tcx().sess.bug("strange alternative in if")
94-
};
95-
let else_bcx_out = trans_block_cleanups(else_bcx_out,
96-
block_cleanups(else_bcx_in));
97-
98-
(else_bcx_in, join_blocks(bcx, [then_bcx_out, else_bcx_out]))
115+
let (else_bcx_in, else_bcx_out) = trans_if_else(bcx, elexpr, dest, "else");
116+
(else_bcx_in, join_blocks(bcx, [then_bcx_out, else_bcx_out]))
99117
}
100118
_ => {
101119
let next_bcx = sub_block(bcx, "next");
@@ -109,7 +127,27 @@ pub fn trans_if(bcx: block,
109127
then_bcx_in.to_str(), else_bcx_in.to_str());
110128

111129
CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
112-
next_bcx
130+
return next_bcx;
131+
132+
// trans `else [ if { .. } ... | { .. } ]`
133+
fn trans_if_else(bcx: block, elexpr: @ast::expr,
134+
dest: expr::Dest, scope_name: &str) -> (block, block) {
135+
let else_bcx_in = scope_block(bcx, elexpr.info(), scope_name);
136+
let else_bcx_out = match elexpr.node {
137+
ast::expr_if(_, _, _) => {
138+
let elseif_blk = ast_util::block_from_expr(elexpr);
139+
trans_block(else_bcx_in, &elseif_blk, dest)
140+
}
141+
ast::expr_block(ref blk) => {
142+
trans_block(else_bcx_in, blk, dest)
143+
}
144+
// would be nice to have a constraint on ifs
145+
_ => bcx.tcx().sess.bug("strange alternative in if")
146+
};
147+
let else_bcx_out = trans_block_cleanups(else_bcx_out,
148+
block_cleanups(else_bcx_in));
149+
(else_bcx_in, else_bcx_out)
150+
}
113151
}
114152

115153
pub fn join_blocks(parent_bcx: block, in_cxs: &[block]) -> block {

Diff for: src/librustc/middle/trans/datum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl Datum {
413413
pub fn to_value_datum(&self, bcx: block) -> Datum {
414414
/*!
415415
*
416-
* Yields a by-ref form of this datum. This may involve
416+
* Yields a by-value form of this datum. This may involve
417417
* creation of a temporary stack slot. The value returned by
418418
* this function is not separately rooted from this datum, so
419419
* it will not live longer than the current datum. */

Diff for: src/librustdoc/astsrv.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ fn build_ctxt(sess: Session,
113113

114114
use rustc::front::config;
115115

116+
let ast = syntax::ext::expand::inject_std_macros(sess.parse_sess,
117+
copy sess.opts.cfg, ast);
116118
let ast = config::strip_unconfigured_items(ast);
117119
let ast = syntax::ext::expand::expand_crate(sess.parse_sess,
118120
copy sess.opts.cfg, ast);
@@ -138,7 +140,8 @@ fn should_prune_unconfigured_items() {
138140
let source = ~"#[cfg(shut_up_and_leave_me_alone)]fn a() { }";
139141
do from_str(source) |srv| {
140142
do exec(srv) |ctxt| {
141-
assert!(ctxt.ast.node.module.items.is_empty());
143+
// one item: the __std_macros secret module
144+
assert_eq!(ctxt.ast.node.module.items.len(), 1);
142145
}
143146
}
144147
}

Diff for: src/librustdoc/attr_pass.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ mod test {
255255
#[test]
256256
fn should_should_extract_mod_attributes() {
257257
let doc = mk_doc(~"#[doc = \"test\"] mod a { }");
258-
assert!(doc.cratemod().mods()[0].desc() == Some(~"test"));
258+
// hidden __std_macros module at the start.
259+
assert!(doc.cratemod().mods()[1].desc() == Some(~"test"));
259260
}
260261
261262
#[test]

Diff for: src/librustdoc/desc_to_brief_pass.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ mod test {
190190
#[test]
191191
fn should_promote_desc() {
192192
let doc = mk_doc(~"#[doc = \"desc\"] mod m { }");
193-
assert_eq!(doc.cratemod().mods()[0].brief(), Some(~"desc"));
193+
// hidden __std_macros module at the start.
194+
assert_eq!(doc.cratemod().mods()[1].brief(), Some(~"desc"));
194195
}
195196
196197
#[test]

Diff for: src/librustdoc/markdown_index_pass.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ mod test {
172172
use extract;
173173
use markdown_index_pass::run;
174174
use path_pass;
175+
use prune_hidden_pass;
175176
use super::pandoc_header_id;
176177

177178
fn mk_doc(output_style: config::OutputStyle, source: ~str)
@@ -183,8 +184,10 @@ mod test {
183184
};
184185
let doc = extract::from_srv(srv.clone(), ~"");
185186
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
187+
let doc = (prune_hidden_pass::mk_pass().f)(srv.clone(), doc);
186188
let doc = (desc_to_brief_pass::mk_pass().f)(srv.clone(), doc);
187189
let doc = (path_pass::mk_pass().f)(srv.clone(), doc);
190+
188191
run(srv.clone(), doc, config)
189192
}
190193
}
@@ -240,13 +243,13 @@ mod test {
240243
config::DocPerMod,
241244
~"mod a { } fn b() { }"
242245
);
243-
assert!(doc.cratemod().index.get().entries[0] == doc::IndexEntry {
246+
assert_eq!(doc.cratemod().index.get().entries[0], doc::IndexEntry {
244247
kind: ~"Module",
245248
name: ~"a",
246249
brief: None,
247250
link: ~"a.html"
248251
});
249-
assert!(doc.cratemod().index.get().entries[1] == doc::IndexEntry {
252+
assert_eq!(doc.cratemod().index.get().entries[1], doc::IndexEntry {
250253
kind: ~"Function",
251254
name: ~"b",
252255
brief: None,
@@ -260,8 +263,7 @@ mod test {
260263
config::DocPerMod,
261264
~"#[doc = \"test\"] mod a { }"
262265
);
263-
assert!(doc.cratemod().index.get().entries[0].brief
264-
== Some(~"test"));
266+
assert_eq!(doc.cratemod().index.get().entries[0].brief, Some(~"test"));
265267
}
266268
267269
#[test]
@@ -270,12 +272,13 @@ mod test {
270272
config::DocPerCrate,
271273
~"extern { fn b(); }"
272274
);
273-
assert!(doc.cratemod().nmods()[0].index.get().entries[0]
274-
== doc::IndexEntry {
275-
kind: ~"Function",
276-
name: ~"b",
277-
brief: None,
278-
link: ~"#function-b"
279-
});
275+
// hidden __std_macros module at the start.
276+
assert_eq!(doc.cratemod().nmods()[0].index.get().entries[0],
277+
doc::IndexEntry {
278+
kind: ~"Function",
279+
name: ~"b",
280+
brief: None,
281+
link: ~"#function-b"
282+
});
280283
}
281284
}

Diff for: src/librustdoc/markdown_pass.rs

+3
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ mod test {
529529
use markdown_writer;
530530
use path_pass;
531531
use page_pass;
532+
use prune_hidden_pass;
532533
use sectionalize_pass;
533534
use trim_pass;
534535
use tystr_pass;
@@ -557,6 +558,8 @@ mod test {
557558
debug!("doc (path): %?", doc);
558559
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
559560
debug!("doc (attr): %?", doc);
561+
let doc = (prune_hidden_pass::mk_pass().f)(srv.clone(), doc);
562+
debug!("doc (prune_hidden): %?", doc);
560563
let doc = (desc_to_brief_pass::mk_pass().f)(srv.clone(), doc);
561564
debug!("doc (desc_to_brief): %?", doc);
562565
let doc = (unindent_pass::mk_pass().f)(srv.clone(), doc);

Diff for: src/librustdoc/markdown_writer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ mod test {
277277
.. config::default_config(&Path("input/test.rc"))
278278
};
279279
let doc = mk_doc(~"", ~"mod a { mod b { } }");
280-
let modb = copy doc.cratemod().mods()[0].mods()[0];
280+
// hidden __std_macros module at the start.
281+
let modb = copy doc.cratemod().mods()[1].mods()[0];
281282
let page = doc::ItemPage(doc::ModTag(modb));
282283
let filename = make_local_filename(&config, page);
283284
assert_eq!(filename, Path("output/dir/a_b.html"));

Diff for: src/librustdoc/page_pass.rs

+5
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ fn fold_nmod(
152152
mod test {
153153
use astsrv;
154154
use config;
155+
use attr_pass;
155156
use doc;
156157
use extract;
158+
use prune_hidden_pass;
157159
use page_pass::run;
158160

159161
fn mk_doc_(
@@ -162,6 +164,8 @@ mod test {
162164
) -> doc::Doc {
163165
do astsrv::from_str(copy source) |srv| {
164166
let doc = extract::from_srv(srv.clone(), ~"");
167+
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
168+
let doc = (prune_hidden_pass::mk_pass().f)(srv.clone(), doc);
165169
run(srv.clone(), doc, output_style)
166170
}
167171
}
@@ -182,6 +186,7 @@ mod test {
182186
#[test]
183187
fn should_make_a_page_for_every_mod() {
184188
let doc = mk_doc(~"mod a { }");
189+
// hidden __std_macros module at the start.
185190
assert_eq!(doc.pages.mods()[0].name(), ~"a");
186191
}
187192

Diff for: src/librustdoc/path_pass.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ fn should_record_mod_paths() {
9797
do astsrv::from_str(source) |srv| {
9898
let doc = extract::from_srv(srv.clone(), ~"");
9999
let doc = run(srv.clone(), doc);
100-
assert!(doc.cratemod().mods()[0].mods()[0].mods()[0].path()
101-
== ~[~"a", ~"b"]);
102-
assert!(doc.cratemod().mods()[0].mods()[1].mods()[0].path()
103-
== ~[~"a", ~"d"]);
100+
// hidden __std_macros module at the start.
101+
assert_eq!(doc.cratemod().mods()[1].mods()[0].mods()[0].path(),
102+
~[~"a", ~"b"]);
103+
assert_eq!(doc.cratemod().mods()[1].mods()[1].mods()[0].path(),
104+
~[~"a", ~"d"]);
104105
}
105106
}
106107
@@ -110,6 +111,7 @@ fn should_record_fn_paths() {
110111
do astsrv::from_str(source) |srv| {
111112
let doc = extract::from_srv(srv.clone(), ~"");
112113
let doc = run(srv.clone(), doc);
113-
assert_eq!(doc.cratemod().mods()[0].fns()[0].path(), ~[~"a"]);
114+
// hidden __std_macros module at the start.
115+
assert_eq!(doc.cratemod().mods()[1].fns()[0].path(), ~[~"a"]);
114116
}
115117
}

Diff for: src/librustdoc/sectionalize_pass.rs

+2
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ mod test {
166166
use attr_pass;
167167
use doc;
168168
use extract;
169+
use prune_hidden_pass;
169170
use sectionalize_pass::run;
170171

171172
fn mk_doc(source: ~str) -> doc::Doc {
172173
do astsrv::from_str(copy source) |srv| {
173174
let doc = extract::from_srv(srv.clone(), ~"");
174175
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
176+
let doc = (prune_hidden_pass::mk_pass().f)(srv.clone(), doc);
175177
run(srv.clone(), doc)
176178
}
177179
}

Diff for: src/librustdoc/sort_item_name_pass.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ fn test() {
3131
do astsrv::from_str(source) |srv| {
3232
let doc = extract::from_srv(srv.clone(), ~"");
3333
let doc = (mk_pass().f)(srv.clone(), doc);
34-
assert_eq!(doc.cratemod().items[0].name(), ~"y");
35-
assert_eq!(doc.cratemod().items[1].name(), ~"z");
34+
// hidden __std_macros module at the start.
35+
assert_eq!(doc.cratemod().items[1].name(), ~"y");
36+
assert_eq!(doc.cratemod().items[2].name(), ~"z");
3637
}
3738
}

Diff for: src/librustdoc/sort_item_type_pass.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ fn test() {
5353
do astsrv::from_str(source) |srv| {
5454
let doc = extract::from_srv(srv.clone(), ~"");
5555
let doc = (mk_pass().f)(srv.clone(), doc);
56+
// hidden __std_macros module at the start.
5657
assert_eq!(doc.cratemod().items[0].name(), ~"iconst");
5758
assert_eq!(doc.cratemod().items[1].name(), ~"itype");
5859
assert_eq!(doc.cratemod().items[2].name(), ~"ienum");
5960
assert_eq!(doc.cratemod().items[3].name(), ~"istruct");
6061
assert_eq!(doc.cratemod().items[4].name(), ~"itrait");
6162
assert_eq!(doc.cratemod().items[5].name(), ~"__extensions__");
6263
assert_eq!(doc.cratemod().items[6].name(), ~"ifn");
63-
assert_eq!(doc.cratemod().items[7].name(), ~"imod");
64+
// hidden __std_macros module fits here.
65+
assert_eq!(doc.cratemod().items[8].name(), ~"imod");
6466
}
6567
}

0 commit comments

Comments
 (0)