1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // DeclStats.cc -- |
---|
8 | // |
---|
9 | // Author : Aaron Moss |
---|
10 | // Created On : Wed Jan 31 16:40:00 2016 |
---|
11 | // Last Modified By : Aaron Moss |
---|
12 | // Last Modified On : Wed Jan 31 16:40:00 2016 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "DeclStats.h" |
---|
17 | |
---|
18 | #include <iostream> |
---|
19 | #include <map> |
---|
20 | #include <sstream> |
---|
21 | #include <string> |
---|
22 | #include <unordered_map> |
---|
23 | #include <unordered_set> |
---|
24 | |
---|
25 | #include "Common/VectorMap.h" |
---|
26 | #include "GenPoly/GenPoly.h" |
---|
27 | #include "Parser/LinkageSpec.h" |
---|
28 | #include "SynTree/Declaration.h" |
---|
29 | #include "SynTree/Visitor.h" |
---|
30 | |
---|
31 | namespace CodeTools { |
---|
32 | |
---|
33 | class DeclStats : public Visitor { |
---|
34 | template<typename T> |
---|
35 | static void sum(T& a, const T& b) { a += b; } |
---|
36 | |
---|
37 | static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) { |
---|
38 | a.reserve( b.size() ); |
---|
39 | for ( unsigned i = 0; i < b.size(); ++i ) { |
---|
40 | a[i] += b[i]; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | template<typename K> |
---|
45 | static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) { |
---|
46 | for ( const auto& entry : b ) { |
---|
47 | a[ entry.first ] += entry.second; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | template<typename K> |
---|
52 | static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) { |
---|
53 | for ( const auto& entry : b ) { |
---|
54 | a[ entry.first ] += entry.second; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | struct ArgPackStats { |
---|
59 | VectorMap<unsigned> n; ///< Count of decls with each number of elements |
---|
60 | VectorMap<unsigned> n_basic; ///< Count of decls with each number of basic type elements |
---|
61 | VectorMap<unsigned> n_poly; ///< Count of decls with each number of polymorphic elements |
---|
62 | std::map<unsigned, unsigned> p_basic; ///< Count of decls with each percentage of basic type elements |
---|
63 | std::map<unsigned, unsigned> p_poly; ///< Count of decls with each percentage of polymorphic elements |
---|
64 | VectorMap<unsigned> n_types; ///< Count of decls with each number of distinct types in the pack |
---|
65 | /// Count of decls with each percentage of new types in lists. |
---|
66 | /// Types used in the parameter list that recur in the return list are not considered to be new. |
---|
67 | std::map<unsigned, unsigned> p_new; |
---|
68 | |
---|
69 | ArgPackStats& operator+= (const ArgPackStats& o) { |
---|
70 | sum(n, o.n); |
---|
71 | sum(n_basic, o.n_basic); |
---|
72 | sum(n_poly, o.n_poly); |
---|
73 | sum(p_basic, o.p_basic); |
---|
74 | sum(p_poly, o.p_poly); |
---|
75 | sum(n_types, o.n_types); |
---|
76 | sum(p_new, o.p_new); |
---|
77 | |
---|
78 | return *this; |
---|
79 | } |
---|
80 | }; |
---|
81 | |
---|
82 | struct Stats { |
---|
83 | unsigned n_decls; ///< Total number of declarations |
---|
84 | /// Count of declarations with each number of assertion parameters |
---|
85 | VectorMap<unsigned> n_type_params; |
---|
86 | /// Count of declarations with each name |
---|
87 | std::unordered_map<std::string, unsigned> by_name; |
---|
88 | /// Count of uses of each basic type |
---|
89 | std::unordered_map<std::string, unsigned> basic_type_names; |
---|
90 | /// Count of uses of each non-basic type |
---|
91 | std::unordered_map<std::string, unsigned> compound_type_names; |
---|
92 | /// Count of decls using each basic type |
---|
93 | std::unordered_map<std::string, unsigned> basic_type_decls; |
---|
94 | /// Count of decls using each compound type |
---|
95 | std::unordered_map<std::string, unsigned> compound_type_decls; |
---|
96 | /// Stats for the parameter list |
---|
97 | ArgPackStats params; |
---|
98 | /// Stats for the return list |
---|
99 | ArgPackStats returns; |
---|
100 | |
---|
101 | /// Count of declarations with each number of assertions |
---|
102 | std::map<unsigned, unsigned> n_assns; |
---|
103 | /// Stats for the assertions' parameters |
---|
104 | ArgPackStats assn_params; |
---|
105 | /// Stats for the assertions' return types |
---|
106 | ArgPackStats assn_returns; |
---|
107 | |
---|
108 | Stats() : n_decls(0), n_type_params(), by_name(), basic_type_names(), compound_type_names(), basic_type_decls(), compound_type_decls(), params(), returns(), n_assns(), assn_params(), assn_returns() {} |
---|
109 | |
---|
110 | public: |
---|
111 | Stats& operator+= (const Stats& o) { |
---|
112 | sum( n_decls, o.n_decls ); |
---|
113 | sum( n_type_params, o.n_type_params ); |
---|
114 | sum( by_name, o.by_name ); |
---|
115 | sum( basic_type_names, o.basic_type_names ); |
---|
116 | sum( compound_type_names, o.compound_type_names ); |
---|
117 | sum( basic_type_decls, o.basic_type_decls ); |
---|
118 | sum( compound_type_decls, o.compound_type_decls ); |
---|
119 | sum( params, o.params ); |
---|
120 | sum( returns, o.returns ); |
---|
121 | sum( n_assns, o.n_assns ); |
---|
122 | sum( assn_params, o.assn_params ); |
---|
123 | sum( assn_returns, o.assn_returns ); |
---|
124 | |
---|
125 | return *this; |
---|
126 | } |
---|
127 | }; |
---|
128 | |
---|
129 | Stats for_linkage[LinkageSpec::NoOfSpecs]; ///< Stores separate stats per linkage |
---|
130 | std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting |
---|
131 | Stats total; |
---|
132 | /// Count of expressions with (depth, fanout) |
---|
133 | std::map<std::pair<unsigned, unsigned>, unsigned> exprs_by_fanout_at_depth; |
---|
134 | |
---|
135 | /// Update arg pack stats based on a declaration list |
---|
136 | void analyze( Stats& stats, std::unordered_set<std::string>& seen, ArgPackStats& pstats, std::list<DeclarationWithType*>& decls ) { |
---|
137 | std::unordered_set<std::string> types; |
---|
138 | unsigned n = 0; ///< number of args/returns |
---|
139 | unsigned n_basic = 0; ///< number of basic types |
---|
140 | unsigned n_poly = 0; ///< number of polymorphic types |
---|
141 | unsigned n_new = 0; ///< number of new types |
---|
142 | for ( auto decl : decls ) { |
---|
143 | Type* dt = decl->get_type(); |
---|
144 | |
---|
145 | n += dt->size(); |
---|
146 | |
---|
147 | std::stringstream ss; |
---|
148 | dt->print( ss ); |
---|
149 | types.insert( ss.str() ); |
---|
150 | bool this_new = seen.insert( ss.str() ).second; |
---|
151 | if ( this_new ) { ++n_new; } |
---|
152 | |
---|
153 | if ( dynamic_cast<BasicType*>( dt ) ) { |
---|
154 | ++n_basic; |
---|
155 | ++stats.basic_type_names[ ss.str() ]; |
---|
156 | if ( this_new ) { |
---|
157 | ++stats.basic_type_decls[ ss.str() ]; |
---|
158 | } |
---|
159 | } else if ( GenPoly::hasPolyBase( dt ) ) { |
---|
160 | ++n_poly; |
---|
161 | } else { |
---|
162 | ++stats.compound_type_names[ ss.str() ]; |
---|
163 | if ( this_new ) { |
---|
164 | ++stats.compound_type_decls[ ss.str() ]; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | ++pstats.n.at( n ); |
---|
169 | ++pstats.n_basic.at( n_basic ); |
---|
170 | ++pstats.n_poly.at( n_poly ); |
---|
171 | if ( n > 0 ) { |
---|
172 | ++pstats.p_basic[ n_basic*100/n ]; |
---|
173 | ++pstats.p_poly[ n_poly*100/n ]; |
---|
174 | ++pstats.p_new[ n_new*100/n ]; |
---|
175 | } |
---|
176 | ++pstats.n_types.at( types.size() ); |
---|
177 | } |
---|
178 | |
---|
179 | void analyzeFunc( FunctionType* fnTy, Stats& stats, ArgPackStats& params, ArgPackStats& returns ) { |
---|
180 | std::unordered_set<std::string> seen; |
---|
181 | analyze( stats, seen, params, fnTy->get_parameters() ); |
---|
182 | analyze( stats, seen, returns, fnTy->get_returnVals() ); |
---|
183 | } |
---|
184 | |
---|
185 | void analyzeExpr( UntypedExpr *expr, unsigned depth ) { |
---|
186 | auto& args = expr->get_args(); |
---|
187 | unsigned fanout = args.size(); |
---|
188 | |
---|
189 | ++exprs_by_fanout_at_depth[ std::make_pair(depth, fanout) ]; |
---|
190 | for ( Expression* arg : args ) { |
---|
191 | if ( UntypedExpr *uearg = dynamic_cast<UntypedExpr*>(arg) ) { |
---|
192 | analyzeExpr( uearg, depth+1 ); |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | public: |
---|
198 | using Visitor::visit; |
---|
199 | |
---|
200 | virtual void visit( FunctionDecl *decl ) { |
---|
201 | // skip if already seen declaration for this function |
---|
202 | const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName(); |
---|
203 | if ( ! seen_names.insert( mangleName ).second ) { |
---|
204 | maybeAccept( decl->get_statements(), *this ); |
---|
205 | return; |
---|
206 | } |
---|
207 | |
---|
208 | Stats& stats = for_linkage[ decl->get_linkage() ]; |
---|
209 | |
---|
210 | ++stats.n_decls; |
---|
211 | FunctionType* fnTy = decl->get_functionType(); |
---|
212 | const Type::ForallList& forall = fnTy->get_forall(); |
---|
213 | ++stats.n_type_params.at( forall.size() ); |
---|
214 | unsigned n_assns = 0; |
---|
215 | for ( TypeDecl* fdecl : forall ) { |
---|
216 | n_assns += fdecl->get_assertions().size(); |
---|
217 | for ( DeclarationWithType* assn : fdecl->get_assertions() ) { |
---|
218 | FunctionType *assnTy = 0; |
---|
219 | if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { |
---|
220 | if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { |
---|
221 | assnTy = dynamic_cast<FunctionType*>(ptrTy->get_base()); |
---|
222 | } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); |
---|
223 | } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { |
---|
224 | assnTy = assnDecl->get_functionType(); |
---|
225 | } |
---|
226 | if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns ); |
---|
227 | } |
---|
228 | } |
---|
229 | ++stats.n_assns[ n_assns ]; |
---|
230 | |
---|
231 | ++stats.by_name[ decl->get_name() ]; |
---|
232 | |
---|
233 | analyzeFunc( fnTy, stats, stats.params, stats.returns ); |
---|
234 | |
---|
235 | // analyze expressions in decl statements |
---|
236 | maybeAccept( decl->get_statements(), *this ); |
---|
237 | } |
---|
238 | |
---|
239 | virtual void visit( UntypedExpr *expr ) { |
---|
240 | analyzeExpr( expr, 0 ); |
---|
241 | } |
---|
242 | |
---|
243 | private: |
---|
244 | template<typename F> |
---|
245 | void printAll( const std::string& name, F extract ) { |
---|
246 | std::cout << "\"" << name << "\","; |
---|
247 | for ( const auto& stats : for_linkage ) { |
---|
248 | std::cout << "," << extract(stats); |
---|
249 | } |
---|
250 | std::cout << "," << extract(total) << std::endl; |
---|
251 | } |
---|
252 | |
---|
253 | template<typename F> |
---|
254 | void printAllMap( const std::string& name, F extract ) { |
---|
255 | for ( const auto& entry : extract(total) ) { |
---|
256 | const auto& key = entry.first; |
---|
257 | std::cout << "\"" << name << "\"," << key; |
---|
258 | for ( const auto& stats : for_linkage ) { |
---|
259 | const auto& map = extract(stats); |
---|
260 | auto it = map.find( key ); |
---|
261 | if ( it == map.end() ) std::cout << ",0"; |
---|
262 | else std::cout << "," << it->second; |
---|
263 | } |
---|
264 | std::cout << "," << entry.second << std::endl; |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | template<typename F> |
---|
269 | void printAllHisto( const std::string& name, F extract ) { |
---|
270 | VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
271 | VectorMap<unsigned> thisto; |
---|
272 | |
---|
273 | for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } |
---|
274 | |
---|
275 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
276 | // can't be a higher count in one of the sub-histograms than the total |
---|
277 | histos[i].reserve( thisto.size() ); |
---|
278 | |
---|
279 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
280 | } |
---|
281 | |
---|
282 | for ( unsigned i = 0; i < thisto.size(); ++i ) { |
---|
283 | std::cout << "\"" << name << "\"," << i; |
---|
284 | for ( const auto& histo : histos ) { |
---|
285 | std::cout << "," << histo[i]; |
---|
286 | } |
---|
287 | std::cout << "," << thisto[i] << std::endl; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | template<typename F> |
---|
292 | void printAllSparseHisto( const std::string& name, F extract ) { |
---|
293 | std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
294 | std::map<unsigned, unsigned> thisto; |
---|
295 | |
---|
296 | for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; } |
---|
297 | |
---|
298 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
299 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
300 | } |
---|
301 | |
---|
302 | for ( const auto& entry : thisto ) { |
---|
303 | const auto& key = entry.first; |
---|
304 | std::cout << "\"" << name << "\"," << key; |
---|
305 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
306 | auto it = histos[i].find( key ); |
---|
307 | if ( it == histos[i].end() ) std::cout << ",0"; |
---|
308 | else std::cout << "," << it->second; |
---|
309 | } |
---|
310 | std::cout << "," << entry.second << std::endl; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | template<typename F> |
---|
315 | void printAllPack( const std::string& name, F extract ) { |
---|
316 | printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; }); |
---|
317 | printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; }); |
---|
318 | printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; }); |
---|
319 | printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; }); |
---|
320 | printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; }); |
---|
321 | printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; }); |
---|
322 | printAllMap("%_new_types_in_" + name, [&extract](const Stats& stats) { return extract(stats).p_new; }); |
---|
323 | } |
---|
324 | |
---|
325 | void printPairMap( const std::string& name, |
---|
326 | const std::map<std::pair<unsigned, unsigned>, unsigned>& map ) { |
---|
327 | for ( const auto& entry : map ) { |
---|
328 | const auto& key = entry.first; |
---|
329 | std::cout << "\"" << name << "\"," << key.first << "," << key.second << "," |
---|
330 | << entry.second << std::endl; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | public: |
---|
335 | void print() { |
---|
336 | for ( auto& stats : for_linkage ) { |
---|
337 | total += stats; |
---|
338 | } |
---|
339 | |
---|
340 | std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl; |
---|
341 | |
---|
342 | printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; }); |
---|
343 | printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); |
---|
344 | printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); |
---|
345 | printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; }); |
---|
346 | printAll("basic_type_names", [](const Stats& stats) { return stats.basic_type_names.size(); }); |
---|
347 | printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; }); |
---|
348 | printAllSparseHisto("decls_using_basic_type", [](const Stats& stats) { return stats.basic_type_decls; }); |
---|
349 | printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); }); |
---|
350 | printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; }); |
---|
351 | printAllSparseHisto("decls_using_compound_type", [](const Stats& stats) { return stats.compound_type_decls; }); |
---|
352 | printAllPack("params", [](const Stats& stats) { return stats.params; }); |
---|
353 | printAllPack("returns", [](const Stats& stats) { return stats.returns; }); |
---|
354 | printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; }); |
---|
355 | printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; }); |
---|
356 | printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; }); |
---|
357 | std::cout << std::endl; |
---|
358 | |
---|
359 | printPairMap("exprs_by_depth+fanout", exprs_by_fanout_at_depth); |
---|
360 | } |
---|
361 | }; |
---|
362 | |
---|
363 | void printDeclStats( std::list< Declaration * > &translationUnit ) { |
---|
364 | DeclStats stats; |
---|
365 | acceptAll( translationUnit, stats ); |
---|
366 | stats.print(); |
---|
367 | } |
---|
368 | |
---|
369 | } // namespace CodeTools |
---|
370 | |
---|
371 | // Local Variables: // |
---|
372 | // tab-width: 4 // |
---|
373 | // mode: c++ // |
---|
374 | // compile-command: "make install" // |
---|
375 | // End: // |
---|