|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! This pass implements instrumentation to gather the layout of every type. |
| 12 | +
|
| 13 | +use rustc::session::{VariantSize}; |
| 14 | +use rustc::traits::{Reveal}; |
| 15 | +use rustc::ty::{self, Ty, TyCtxt}; |
| 16 | +use rustc::ty::fold::{TypeFoldable}; |
| 17 | +use rustc::ty::layout::{Layout}; |
| 18 | +use rustc::mir::{Mir}; |
| 19 | +use rustc::mir::transform::{MirPass, MirPassHook, MirSource, Pass}; |
| 20 | +use rustc::mir::visit::Visitor; |
| 21 | + |
| 22 | +use std::collections::HashSet; |
| 23 | + |
| 24 | +pub struct GatherTypeSizesMir { |
| 25 | + _hidden: (), |
| 26 | +} |
| 27 | + |
| 28 | +impl GatherTypeSizesMir { |
| 29 | + pub fn new() -> Self { |
| 30 | + GatherTypeSizesMir { _hidden: () } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Pass for GatherTypeSizesMir { |
| 35 | +} |
| 36 | + |
| 37 | +impl<'tcx> MirPassHook<'tcx> for GatherTypeSizesMir { |
| 38 | + fn on_mir_pass<'a>(&mut self, |
| 39 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 40 | + src: MirSource, |
| 41 | + mir: &Mir<'tcx>, |
| 42 | + _pass: &Pass, |
| 43 | + _is_after: bool) { |
| 44 | + debug!("on_mir_pass: {}", tcx.node_path_str(src.item_id())); |
| 45 | + self.go(tcx, mir); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'tcx> MirPass<'tcx> for GatherTypeSizesMir { |
| 50 | + fn run_pass<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 51 | + src: MirSource, mir: &mut Mir<'tcx>) { |
| 52 | + debug!("run_pass: {}", tcx.node_path_str(src.item_id())); |
| 53 | + self.go(tcx, mir); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl GatherTypeSizesMir { |
| 58 | + fn go<'a, 'tcx>(&mut self, |
| 59 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 60 | + mir: &Mir<'tcx>) { |
| 61 | + if tcx.sess.err_count() > 0 { |
| 62 | + // compiling a broken program can obviously result in a |
| 63 | + // broken MIR, so do not bother trying to process it. |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + let mut visitor = TypeVisitor { |
| 68 | + tcx: tcx, |
| 69 | + seen: HashSet::new(), |
| 70 | + }; |
| 71 | + visitor.visit_mir(mir); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +struct TypeVisitor<'a, 'tcx: 'a> { |
| 76 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 77 | + seen: HashSet<Ty<'tcx>>, |
| 78 | +} |
| 79 | + |
| 80 | +impl<'a, 'tcx: 'a> Visitor<'tcx> for TypeVisitor<'a, 'tcx> { |
| 81 | + fn visit_ty(&mut self, ty: &Ty<'tcx>) { |
| 82 | + debug!("TypeVisitor::visit_ty ty=`{:?}`", ty); |
| 83 | + |
| 84 | + match ty.sty { |
| 85 | + ty::TyAdt(..) | |
| 86 | + ty::TyClosure(..) => {} // fall through |
| 87 | + _ => { |
| 88 | + debug!("print-type-size t: `{:?}` skip non-nominal", ty); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if ty.has_param_types() { |
| 94 | + debug!("print-type-size t: `{:?}` skip has param types", ty); |
| 95 | + return; |
| 96 | + } |
| 97 | + if ty.has_projection_types() { |
| 98 | + debug!("print-type-size t: `{:?}` skip has projections", ty); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if self.seen.contains(ty) { |
| 103 | + return; |
| 104 | + } |
| 105 | + self.seen.insert(ty); |
| 106 | + |
| 107 | + let reveal = Reveal::All; |
| 108 | + // let reveal = Reveal::NotSpecializable; |
| 109 | + |
| 110 | + self.tcx.infer_ctxt(None, None, reveal).enter(|infcx| { |
| 111 | + match ty.layout(&infcx) { |
| 112 | + Ok(layout) => { |
| 113 | + let type_desc = format!("{:?}", ty); |
| 114 | + let overall_size = layout.size(&Default::default()); |
| 115 | + |
| 116 | + let variant_sizes: Vec<_> = match *layout { |
| 117 | + Layout::General { ref variants, .. } => { |
| 118 | + variants.iter() |
| 119 | + .map(|v| if v.sized { |
| 120 | + VariantSize::Exact(v.min_size.bytes()) |
| 121 | + } else { |
| 122 | + VariantSize::Min(v.min_size.bytes()) |
| 123 | + }) |
| 124 | + .collect() |
| 125 | + } |
| 126 | + |
| 127 | + Layout::UntaggedUnion { variants: _ } => { |
| 128 | + /* layout does not currently store info about each variant... */ |
| 129 | + Vec::new() |
| 130 | + } |
| 131 | + |
| 132 | + // RawNullablePointer/StructWrappedNullablePointer |
| 133 | + // don't provide any interesting size info |
| 134 | + // beyond what we already reported for their |
| 135 | + // total size. |
| 136 | + _ => { |
| 137 | + Vec::new() |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + self.tcx.sess.code_stats.borrow_mut() |
| 142 | + .record_type_size(type_desc, |
| 143 | + overall_size.bytes(), |
| 144 | + variant_sizes); |
| 145 | + } |
| 146 | + Err(err) => { |
| 147 | + self.tcx.sess.warn(&format!("print-type-size t: `{:?}` err: {:?}", ty, err)); |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | +} |
0 commit comments