7
7
//! This module will perform all the analyses requested. Callers are responsible for selecting
8
8
//! when the cost of these analyses are worth it.
9
9
10
- use rustc_middle:: mir:: mono:: MonoItem ;
11
- use rustc_middle:: mir:: visit:: Visitor as MirVisitor ;
12
- use rustc_middle:: mir:: { Location , Rvalue , Statement , StatementKind , Terminator , TerminatorKind } ;
10
+ use rustc_middle:: mir:: mono:: MonoItem as InternalMonoItem ;
13
11
use rustc_middle:: ty:: TyCtxt ;
12
+ use rustc_smir:: rustc_internal;
13
+ use stable_mir:: mir:: mono:: MonoItem ;
14
+ use stable_mir:: mir:: {
15
+ visit:: Location , MirVisitor , Rvalue , Statement , StatementKind , Terminator , TerminatorKind ,
16
+ } ;
14
17
use std:: collections:: HashMap ;
15
18
use std:: fmt:: Display ;
16
19
@@ -20,29 +23,30 @@ use std::fmt::Display;
20
23
/// - Number of items per type (Function / Constant / Shims)
21
24
/// - Number of instructions per type.
22
25
/// - Total number of MIR instructions.
23
- pub fn print_stats < ' tcx > ( tcx : TyCtxt < ' tcx > , items : & [ MonoItem < ' tcx > ] ) {
24
- let item_types = items. iter ( ) . collect :: < Counter > ( ) ;
25
- let visitor = items
26
- . iter ( )
27
- . filter_map ( |& mono| {
28
- if let MonoItem :: Fn ( instance) = mono {
29
- Some ( tcx. instance_mir ( instance. def ) )
30
- } else {
31
- None
32
- }
33
- } )
34
- . fold ( StatsVisitor :: default ( ) , |mut visitor, body| {
35
- visitor. visit_body ( body) ;
36
- visitor
37
- } ) ;
38
- eprintln ! ( "====== Reachability Analysis Result =======" ) ;
39
- eprintln ! ( "Total # items: {}" , item_types. total( ) ) ;
40
- eprintln ! ( "Total # statements: {}" , visitor. stmts. total( ) ) ;
41
- eprintln ! ( "Total # expressions: {}" , visitor. exprs. total( ) ) ;
42
- eprintln ! ( "\n Reachable Items:\n {item_types}" ) ;
43
- eprintln ! ( "Statements:\n {}" , visitor. stmts) ;
44
- eprintln ! ( "Expressions:\n {}" , visitor. exprs) ;
45
- eprintln ! ( "-------------------------------------------" )
26
+ pub fn print_stats < ' tcx > ( tcx : TyCtxt < ' tcx > , items : & [ InternalMonoItem < ' tcx > ] ) {
27
+ rustc_internal:: run ( tcx, || {
28
+ let items: Vec < MonoItem > = items. iter ( ) . map ( rustc_internal:: stable) . collect ( ) ;
29
+ let item_types = items. iter ( ) . collect :: < Counter > ( ) ;
30
+ let visitor = items
31
+ . iter ( )
32
+ . filter_map (
33
+ |mono| {
34
+ if let MonoItem :: Fn ( instance) = mono { Some ( instance) } else { None }
35
+ } ,
36
+ )
37
+ . fold ( StatsVisitor :: default ( ) , |mut visitor, body| {
38
+ visitor. visit_body ( & body. body ( ) ) ;
39
+ visitor
40
+ } ) ;
41
+ eprintln ! ( "====== Reachability Analysis Result =======" ) ;
42
+ eprintln ! ( "Total # items: {}" , item_types. total( ) ) ;
43
+ eprintln ! ( "Total # statements: {}" , visitor. stmts. total( ) ) ;
44
+ eprintln ! ( "Total # expressions: {}" , visitor. exprs. total( ) ) ;
45
+ eprintln ! ( "\n Reachable Items:\n {item_types}" ) ;
46
+ eprintln ! ( "Statements:\n {}" , visitor. stmts) ;
47
+ eprintln ! ( "Expressions:\n {}" , visitor. exprs) ;
48
+ eprintln ! ( "-------------------------------------------" )
49
+ } ) ;
46
50
}
47
51
48
52
#[ derive( Default ) ]
@@ -54,20 +58,20 @@ struct StatsVisitor {
54
58
exprs : Counter ,
55
59
}
56
60
57
- impl < ' tcx > MirVisitor < ' tcx > for StatsVisitor {
58
- fn visit_statement ( & mut self , statement : & Statement < ' tcx > , location : Location ) {
61
+ impl MirVisitor for StatsVisitor {
62
+ fn visit_statement ( & mut self , statement : & Statement , location : Location ) {
59
63
self . stmts . add ( statement) ;
60
64
// Also visit the type of expression.
61
65
self . super_statement ( statement, location) ;
62
66
}
63
67
64
- fn visit_terminator ( & mut self , terminator : & Terminator < ' tcx > , _location : Location ) {
68
+ fn visit_terminator ( & mut self , terminator : & Terminator , _location : Location ) {
65
69
self . stmts . add ( terminator) ;
66
70
// Stop here since we don't care today about the information inside the terminator.
67
71
// self.super_terminator(terminator, location);
68
72
}
69
73
70
- fn visit_rvalue ( & mut self , rvalue : & Rvalue < ' tcx > , _location : Location ) {
74
+ fn visit_rvalue ( & mut self , rvalue : & Rvalue , _location : Location ) {
71
75
self . exprs . add ( rvalue) ;
72
76
// Stop here since we don't care today about the information inside the rvalue.
73
77
// self.super_rvalue(rvalue, location);
@@ -115,8 +119,8 @@ impl<T: Into<Key>> FromIterator<T> for Counter {
115
119
#[ derive( Debug , Eq , Hash , PartialEq ) ]
116
120
struct Key ( pub & ' static str ) ;
117
121
118
- impl < ' tcx > From < & MonoItem < ' tcx > > for Key {
119
- fn from ( value : & MonoItem ) -> Self {
122
+ impl From < & MonoItem > for Key {
123
+ fn from ( value : & stable_mir :: mir :: mono :: MonoItem ) -> Self {
120
124
match value {
121
125
MonoItem :: Fn ( _) => Key ( "function" ) ,
122
126
MonoItem :: GlobalAsm ( _) => Key ( "global assembly" ) ,
@@ -125,18 +129,18 @@ impl<'tcx> From<&MonoItem<'tcx>> for Key {
125
129
}
126
130
}
127
131
128
- impl < ' tcx > From < & Statement < ' tcx > > for Key {
129
- fn from ( value : & Statement < ' tcx > ) -> Self {
132
+ impl From < & Statement > for Key {
133
+ fn from ( value : & Statement ) -> Self {
130
134
match value. kind {
131
- StatementKind :: Assign ( _ ) => Key ( "Assign" ) ,
135
+ StatementKind :: Assign ( .. ) => Key ( "Assign" ) ,
132
136
StatementKind :: Deinit ( _) => Key ( "Deinit" ) ,
133
137
StatementKind :: Intrinsic ( _) => Key ( "Intrinsic" ) ,
134
138
StatementKind :: SetDiscriminant { .. } => Key ( "SetDiscriminant" ) ,
135
139
// For now, we don't care about the ones below.
136
- StatementKind :: AscribeUserType ( _ , _ )
140
+ StatementKind :: AscribeUserType { .. }
137
141
| StatementKind :: Coverage ( _)
138
142
| StatementKind :: ConstEvalCounter
139
- | StatementKind :: FakeRead ( _ )
143
+ | StatementKind :: FakeRead ( .. )
140
144
| StatementKind :: Nop
141
145
| StatementKind :: PlaceMention ( _)
142
146
| StatementKind :: Retag ( _, _)
@@ -146,29 +150,26 @@ impl<'tcx> From<&Statement<'tcx>> for Key {
146
150
}
147
151
}
148
152
149
- impl < ' tcx > From < & Terminator < ' tcx > > for Key {
150
- fn from ( value : & Terminator < ' tcx > ) -> Self {
153
+ impl From < & Terminator > for Key {
154
+ fn from ( value : & Terminator ) -> Self {
151
155
match value. kind {
156
+ TerminatorKind :: Abort => Key ( "Abort" ) ,
152
157
TerminatorKind :: Assert { .. } => Key ( "Assert" ) ,
153
158
TerminatorKind :: Call { .. } => Key ( "Call" ) ,
154
159
TerminatorKind :: Drop { .. } => Key ( "Drop" ) ,
155
160
TerminatorKind :: CoroutineDrop => Key ( "CoroutineDrop" ) ,
156
161
TerminatorKind :: Goto { .. } => Key ( "Goto" ) ,
157
- TerminatorKind :: FalseEdge { .. } => Key ( "FalseEdge" ) ,
158
- TerminatorKind :: FalseUnwind { .. } => Key ( "FalseUnwind" ) ,
159
162
TerminatorKind :: InlineAsm { .. } => Key ( "InlineAsm" ) ,
160
- TerminatorKind :: UnwindResume => Key ( "UnwindResume " ) ,
163
+ TerminatorKind :: Resume { .. } => Key ( "Resume " ) ,
161
164
TerminatorKind :: Return => Key ( "Return" ) ,
162
165
TerminatorKind :: SwitchInt { .. } => Key ( "SwitchInt" ) ,
163
- TerminatorKind :: UnwindTerminate ( _) => Key ( "UnwindTerminate" ) ,
164
166
TerminatorKind :: Unreachable => Key ( "Unreachable" ) ,
165
- TerminatorKind :: Yield { .. } => Key ( "Yield" ) ,
166
167
}
167
168
}
168
169
}
169
170
170
- impl < ' tcx > From < & Rvalue < ' tcx > > for Key {
171
- fn from ( value : & Rvalue < ' tcx > ) -> Self {
171
+ impl From < & Rvalue > for Key {
172
+ fn from ( value : & Rvalue ) -> Self {
172
173
match value {
173
174
Rvalue :: Use ( _) => Key ( "Use" ) ,
174
175
Rvalue :: Repeat ( _, _) => Key ( "Repeat" ) ,
@@ -177,8 +178,8 @@ impl<'tcx> From<&Rvalue<'tcx>> for Key {
177
178
Rvalue :: AddressOf ( _, _) => Key ( "AddressOf" ) ,
178
179
Rvalue :: Len ( _) => Key ( "Len" ) ,
179
180
Rvalue :: Cast ( _, _, _) => Key ( "Cast" ) ,
180
- Rvalue :: BinaryOp ( _ , _ ) => Key ( "BinaryOp" ) ,
181
- Rvalue :: CheckedBinaryOp ( _ , _ ) => Key ( "CheckedBinaryOp" ) ,
181
+ Rvalue :: BinaryOp ( .. ) => Key ( "BinaryOp" ) ,
182
+ Rvalue :: CheckedBinaryOp ( .. ) => Key ( "CheckedBinaryOp" ) ,
182
183
Rvalue :: NullaryOp ( _, _) => Key ( "NullaryOp" ) ,
183
184
Rvalue :: UnaryOp ( _, _) => Key ( "UnaryOp" ) ,
184
185
Rvalue :: Discriminant ( _) => Key ( "Discriminant" ) ,
0 commit comments