2021-05-05 15:57:08 -04:00
|
|
|
use rustc_middle::ty::{self, ClosureSizeProfileData, Instance, TyCtxt};
|
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
|
|
/// For a given closure, writes out the data for the profiling the impact of RFC 2229 on
|
|
|
|
/// closure size into a CSV.
|
|
|
|
///
|
|
|
|
/// During the same compile all closures dump the information in the same file
|
|
|
|
/// "closure_profile_XXXXX.csv", which is created in the directory where the compiler is invoked.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn dump_closure_profile<'tcx>(tcx: TyCtxt<'tcx>, closure_instance: Instance<'tcx>) {
|
2022-02-15 05:58:25 +01:00
|
|
|
let Ok(mut file) = OpenOptions::new()
|
2021-05-05 15:57:08 -04:00
|
|
|
.create(true)
|
|
|
|
.append(true)
|
|
|
|
.open(&format!("closure_profile_{}.csv", std::process::id()))
|
2022-02-15 05:58:25 +01:00
|
|
|
else {
|
2022-08-18 10:13:37 +08:00
|
|
|
eprintln!("Couldn't open file for writing closure profile");
|
2021-05-05 15:57:08 -04:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2022-07-12 09:10:22 -05:00
|
|
|
let closure_def_id = closure_instance.def_id().expect_local();
|
|
|
|
let typeck_results = tcx.typeck(closure_def_id);
|
2021-05-05 15:57:08 -04:00
|
|
|
|
|
|
|
if typeck_results.closure_size_eval.contains_key(&closure_def_id) {
|
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
|
|
|
|
|
|
|
let ClosureSizeProfileData { before_feature_tys, after_feature_tys } =
|
|
|
|
typeck_results.closure_size_eval[&closure_def_id];
|
|
|
|
|
|
|
|
let before_feature_tys = tcx.subst_and_normalize_erasing_regions(
|
|
|
|
closure_instance.substs,
|
|
|
|
param_env,
|
|
|
|
before_feature_tys,
|
|
|
|
);
|
|
|
|
let after_feature_tys = tcx.subst_and_normalize_erasing_regions(
|
|
|
|
closure_instance.substs,
|
|
|
|
param_env,
|
|
|
|
after_feature_tys,
|
|
|
|
);
|
|
|
|
|
|
|
|
let new_size = tcx
|
|
|
|
.layout_of(param_env.and(after_feature_tys))
|
|
|
|
.map(|l| format!("{:?}", l.size.bytes()))
|
|
|
|
.unwrap_or_else(|e| format!("Failed {:?}", e));
|
|
|
|
|
|
|
|
let old_size = tcx
|
|
|
|
.layout_of(param_env.and(before_feature_tys))
|
|
|
|
.map(|l| format!("{:?}", l.size.bytes()))
|
|
|
|
.unwrap_or_else(|e| format!("Failed {:?}", e));
|
|
|
|
|
2021-10-20 20:59:15 +02:00
|
|
|
let closure_span = tcx.def_span(closure_def_id);
|
2021-05-05 15:57:08 -04:00
|
|
|
let src_file = tcx.sess.source_map().span_to_filename(closure_span);
|
|
|
|
let line_nos = tcx
|
|
|
|
.sess
|
|
|
|
.source_map()
|
|
|
|
.span_to_lines(closure_span)
|
|
|
|
.map(|l| format!("{:?} {:?}", l.lines.first(), l.lines.last()))
|
|
|
|
.unwrap_or_else(|e| format!("{:?}", e));
|
|
|
|
|
|
|
|
if let Err(e) = writeln!(
|
|
|
|
file,
|
|
|
|
"{}, {}, {}, {:?}",
|
|
|
|
old_size,
|
|
|
|
new_size,
|
|
|
|
src_file.prefer_local(),
|
|
|
|
line_nos
|
|
|
|
) {
|
2021-11-04 20:16:57 +01:00
|
|
|
eprintln!("Error writing to file {}", e)
|
2021-05-05 15:57:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|