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