Skip to content

Commit f80d6dc

Browse files
committed
rustc: improve -Z trans-stats to report per-fn LLVM instruction counts and translation timing
1 parent 0c6fc46 commit f80d6dc

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

src/librustc/middle/trans/base.rs

+54-11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use std::uint;
7272
use std::vec;
7373
use std::local_data;
7474
use extra::time;
75+
use extra::sort;
7576
use syntax::ast::ident;
7677
use syntax::ast_map::{path, path_elt_to_str, path_name};
7778
use syntax::ast_util::{local_def, path_to_ident};
@@ -141,6 +142,48 @@ fn fcx_has_nonzero_span(fcx: fn_ctxt) -> bool {
141142
}
142143
}
143144

145+
struct StatRecorder<'self> {
146+
ccx: @mut CrateContext,
147+
name: &'self str,
148+
start: u64,
149+
istart: uint,
150+
}
151+
152+
impl<'self> StatRecorder<'self> {
153+
pub fn new(ccx: @mut CrateContext,
154+
name: &'self str) -> StatRecorder<'self> {
155+
let start = if ccx.sess.trans_stats() {
156+
time::precise_time_ns()
157+
} else {
158+
0
159+
};
160+
let istart = ccx.stats.n_llvm_insns;
161+
StatRecorder {
162+
ccx: ccx,
163+
name: name,
164+
start: start,
165+
istart: istart,
166+
}
167+
}
168+
}
169+
170+
#[unsafe_destructor]
171+
impl<'self> Drop for StatRecorder<'self> {
172+
pub fn drop(&self) {
173+
if self.ccx.sess.trans_stats() {
174+
let end = time::precise_time_ns();
175+
let elapsed = ((end - self.start) / 1_000_000) as uint;
176+
let iend = self.ccx.stats.n_llvm_insns;
177+
self.ccx.stats.fn_stats.push((self.name.to_owned(),
178+
elapsed,
179+
iend - self.istart));
180+
self.ccx.stats.n_fns += 1;
181+
// Reset LLVM insn count to avoid compound costs.
182+
self.ccx.stats.n_llvm_insns = self.istart;
183+
}
184+
}
185+
}
186+
144187
pub fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv, ty: Type) -> ValueRef {
145188
let llfn: ValueRef = do name.as_c_str |buf| {
146189
unsafe {
@@ -1866,18 +1909,16 @@ pub fn trans_fn(ccx: @mut CrateContext,
18661909
param_substs: Option<@param_substs>,
18671910
id: ast::node_id,
18681911
attrs: &[ast::attribute]) {
1869-
let do_time = ccx.sess.trans_stats();
1870-
let start = if do_time { time::get_time() }
1871-
else { time::Timespec::new(0, 0) };
1912+
1913+
let the_path_str = path_str(ccx.sess, path);
1914+
let _s = StatRecorder::new(ccx, the_path_str);
18721915
debug!("trans_fn(self_arg=%?, param_substs=%s)",
18731916
self_arg,
18741917
param_substs.repr(ccx.tcx));
18751918
let _icx = push_ctxt("trans_fn");
1876-
ccx.stats.n_fns += 1;
1877-
let the_path_str = path_str(ccx.sess, path);
18781919
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, id));
18791920
trans_closure(ccx,
1880-
path,
1921+
copy path,
18811922
decl,
18821923
body,
18831924
llfndecl,
@@ -1893,10 +1934,6 @@ pub fn trans_fn(ccx: @mut CrateContext,
18931934
}
18941935
},
18951936
|_bcx| { });
1896-
if do_time {
1897-
let end = time::get_time();
1898-
ccx.log_fn_time(the_path_str, start, end);
1899-
}
19001937
}
19011938

19021939
pub fn trans_enum_variant(ccx: @mut CrateContext,
@@ -2961,8 +2998,14 @@ pub fn trans_crate(sess: session::Session,
29612998
io::println(fmt!("n_monos: %u", ccx.stats.n_monos));
29622999
io::println(fmt!("n_inlines: %u", ccx.stats.n_inlines));
29633000
io::println(fmt!("n_closures: %u", ccx.stats.n_closures));
3001+
io::println("fn stats:");
3002+
do sort::quick_sort(ccx.stats.fn_stats) |&(_, _, insns_a), &(_, _, insns_b)| {
3003+
insns_a > insns_b
3004+
}
3005+
for ccx.stats.fn_stats.iter().advance |&(name, ms, insns)| {
3006+
io::println(fmt!("%u insns, %u ms, %s", insns, ms, name));
3007+
}
29643008
}
2965-
29663009
if ccx.sess.count_llvm_insns() {
29673010
for ccx.stats.llvm_insns.iter().advance |(&k, &v)| {
29683011
io::println(fmt!("%-7u %s", v, k));

src/librustc/middle/trans/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub fn B(cx: block) -> BuilderRef {
4646
}
4747

4848
pub fn count_insn(cx: block, category: &str) {
49+
if cx.ccx().sess.trans_stats() {
50+
cx.ccx().stats.n_llvm_insns += 1;
51+
}
4952
do base::with_insn_ctxt |v| {
5053
let h = &mut cx.ccx().stats.llvm_insns;
5154

src/librustc/middle/trans/common.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ pub struct Stats {
9696
n_monos: uint,
9797
n_inlines: uint,
9898
n_closures: uint,
99+
n_llvm_insns: uint,
100+
llvm_insn_ctxt: ~[~str],
99101
llvm_insns: HashMap<~str, uint>,
100-
fn_times: ~[(~str, int)] // (ident, time)
102+
fn_stats: ~[(~str, uint, uint)] // (ident, time-in-ms, llvm-instructions)
101103
}
102104

103105
pub struct BuilderRef_res {

src/librustc/middle/trans/context.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ impl CrateContext {
210210
n_monos: 0u,
211211
n_inlines: 0u,
212212
n_closures: 0u,
213+
n_llvm_insns: 0u,
214+
llvm_insn_ctxt: ~[],
213215
llvm_insns: HashMap::new(),
214-
fn_times: ~[]
216+
fn_stats: ~[]
215217
},
216218
upcalls: upcall::declare_upcalls(targ_cfg, llmod),
217219
tydesc_type: tydesc_type,
@@ -226,12 +228,6 @@ impl CrateContext {
226228
}
227229
}
228230
}
229-
230-
pub fn log_fn_time(&mut self, name: ~str, start: time::Timespec, end: time::Timespec) {
231-
let elapsed = 1000 * ((end.sec - start.sec) as int) +
232-
((end.nsec as int) - (start.nsec as int)) / 1000000;
233-
self.stats.fn_times.push((name, elapsed));
234-
}
235231
}
236232

237233
#[unsafe_destructor]

src/librustc/middle/trans/glue.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,9 @@ pub fn make_generic_glue(ccx: @mut CrateContext,
738738
name: &str)
739739
-> ValueRef {
740740
let _icx = push_ctxt("make_generic_glue");
741-
if !ccx.sess.trans_stats() {
742-
return make_generic_glue_inner(ccx, t, llfn, helper);
743-
}
744-
745-
let start = time::get_time();
746-
let llval = make_generic_glue_inner(ccx, t, llfn, helper);
747-
let end = time::get_time();
748-
ccx.log_fn_time(fmt!("glue %s %s", name, ty_to_short_str(ccx.tcx, t)), start, end);
749-
return llval;
741+
let glue_name = fmt!("glue %s %s", name, ty_to_short_str(ccx.tcx, t));
742+
let _s = StatRecorder::new(ccx, glue_name);
743+
make_generic_glue_inner(ccx, t, llfn, helper)
750744
}
751745

752746
pub fn emit_tydescs(ccx: &mut CrateContext) {

0 commit comments

Comments
 (0)