]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_mir/src/monomorphize/util.rs
New upstream version 1.56.0+dfsg1
[rustc.git] / compiler / rustc_mir / src / monomorphize / util.rs
1 use rustc_middle::ty::{self, ClosureSizeProfileData, Instance, TyCtxt};
2 use std::fs::OpenOptions;
3 use std::io::prelude::*;
4
5 /// For a given closure, writes out the data for the profiling the impact of RFC 2229 on
6 /// closure size into a CSV.
7 ///
8 /// During the same compile all closures dump the information in the same file
9 /// "closure_profile_XXXXX.csv", which is created in the directory where the compiler is invoked.
10 crate fn dump_closure_profile(tcx: TyCtxt<'tcx>, closure_instance: Instance<'tcx>) {
11 let mut file = if let Ok(file) = OpenOptions::new()
12 .create(true)
13 .append(true)
14 .open(&format!("closure_profile_{}.csv", std::process::id()))
15 {
16 file
17 } else {
18 eprintln!("Cound't open file for writing closure profile");
19 return;
20 };
21
22 let closure_def_id = closure_instance.def_id();
23 let typeck_results = tcx.typeck(closure_def_id.expect_local());
24
25 if typeck_results.closure_size_eval.contains_key(&closure_def_id) {
26 let param_env = ty::ParamEnv::reveal_all();
27
28 let ClosureSizeProfileData { before_feature_tys, after_feature_tys } =
29 typeck_results.closure_size_eval[&closure_def_id];
30
31 let before_feature_tys = tcx.subst_and_normalize_erasing_regions(
32 closure_instance.substs,
33 param_env,
34 before_feature_tys,
35 );
36 let after_feature_tys = tcx.subst_and_normalize_erasing_regions(
37 closure_instance.substs,
38 param_env,
39 after_feature_tys,
40 );
41
42 let new_size = tcx
43 .layout_of(param_env.and(after_feature_tys))
44 .map(|l| format!("{:?}", l.size.bytes()))
45 .unwrap_or_else(|e| format!("Failed {:?}", e));
46
47 let old_size = tcx
48 .layout_of(param_env.and(before_feature_tys))
49 .map(|l| format!("{:?}", l.size.bytes()))
50 .unwrap_or_else(|e| format!("Failed {:?}", e));
51
52 let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
53 let closure_span = tcx.hir().span(closure_hir_id);
54 let src_file = tcx.sess.source_map().span_to_filename(closure_span);
55 let line_nos = tcx
56 .sess
57 .source_map()
58 .span_to_lines(closure_span)
59 .map(|l| format!("{:?} {:?}", l.lines.first(), l.lines.last()))
60 .unwrap_or_else(|e| format!("{:?}", e));
61
62 if let Err(e) = writeln!(
63 file,
64 "{}, {}, {}, {:?}",
65 old_size,
66 new_size,
67 src_file.prefer_local(),
68 line_nos
69 ) {
70 eprintln!("Error writting to file {}", e.to_string())
71 }
72 }
73 }