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 <string> |
---|
21 | #include <unordered_map> |
---|
22 | #include <unordered_set> |
---|
23 | |
---|
24 | #include "Common/VectorMap.h" |
---|
25 | #include "GenPoly/GenPoly.h" |
---|
26 | #include "Parser/LinkageSpec.h" |
---|
27 | #include "SynTree/Declaration.h" |
---|
28 | #include "SynTree/Visitor.h" |
---|
29 | |
---|
30 | namespace CodeTools { |
---|
31 | |
---|
32 | class DeclStats : public Visitor { |
---|
33 | template<typename T> |
---|
34 | static void sum(T& a, const T& b) { a += b; } |
---|
35 | |
---|
36 | static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) { |
---|
37 | a.reserve( b.size() ); |
---|
38 | for ( unsigned i = 0; i < b.size(); ++i ) { |
---|
39 | a[i] += b[i]; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | template<typename K> |
---|
44 | static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) { |
---|
45 | for ( const auto& entry : b ) { |
---|
46 | a[ entry.first ] += entry.second; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | template<typename K> |
---|
51 | static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) { |
---|
52 | for ( const auto& entry : b ) { |
---|
53 | a[ entry.first ] += entry.second; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | struct ArgPackStats { |
---|
58 | VectorMap<unsigned> n; ///< Count of decls with each number of elements |
---|
59 | VectorMap<unsigned> n_basic; ///< Count of decls with each number of basic type elements |
---|
60 | VectorMap<unsigned> n_poly; ///< Count of decls with each number of polymorphic elements |
---|
61 | std::map<unsigned, unsigned> p_basic; ///< Count of decls with each percentage of basic type elements |
---|
62 | std::map<unsigned, unsigned> p_poly; ///< Count of decls with each percentage of polymorphic elements |
---|
63 | |
---|
64 | ArgPackStats& operator+= (const ArgPackStats& o) { |
---|
65 | sum(n, o.n); |
---|
66 | sum(n_basic, o.n_basic); |
---|
67 | sum(n_poly, o.n_poly); |
---|
68 | sum(p_basic, o.p_basic); |
---|
69 | sum(p_poly, o.p_poly); |
---|
70 | |
---|
71 | return *this; |
---|
72 | } |
---|
73 | |
---|
74 | /// Update based on a declaration list |
---|
75 | ArgPackStats& operator+= ( std::list<DeclarationWithType*>& decls ) { |
---|
76 | unsigned nn = 0; |
---|
77 | unsigned nn_basic = 0; |
---|
78 | unsigned nn_poly = 0; |
---|
79 | for ( auto decl : decls ) { |
---|
80 | nn += decl->get_type()->size(); |
---|
81 | if ( dynamic_cast<BasicType*>( decl->get_type() ) ) ++nn_basic; |
---|
82 | else if ( GenPoly::hasPolyBase( decl->get_type() ) ) ++nn_poly; |
---|
83 | } |
---|
84 | ++n.at( nn ); |
---|
85 | ++n_basic.at( nn_basic ); |
---|
86 | ++n_poly.at( nn_poly ); |
---|
87 | if ( nn > 0 ) { |
---|
88 | ++p_basic[ nn_basic*100/nn ]; |
---|
89 | ++p_poly[ nn_poly*100/nn ]; |
---|
90 | } |
---|
91 | |
---|
92 | return *this; |
---|
93 | } |
---|
94 | }; |
---|
95 | |
---|
96 | struct Stats { |
---|
97 | unsigned n_decls; ///< Total number of declarations |
---|
98 | /// Count of declarations with each number of assertion parameters |
---|
99 | VectorMap<unsigned> n_type_params; |
---|
100 | /// Count of declarations with each name |
---|
101 | std::unordered_map<std::string, unsigned> by_name; |
---|
102 | /// Stats for the parameter list |
---|
103 | ArgPackStats params; |
---|
104 | /// Stats for the return list |
---|
105 | ArgPackStats returns; |
---|
106 | |
---|
107 | /// Count of declarations with each number of assertions |
---|
108 | std::map<unsigned, unsigned> n_assns; |
---|
109 | /// Stats for the assertions' parameters |
---|
110 | ArgPackStats assn_params; |
---|
111 | /// Stats for the assertions' return types |
---|
112 | ArgPackStats assn_returns; |
---|
113 | |
---|
114 | Stats() : n_decls(0), n_type_params(), by_name(), params(), returns(), n_assns(), assn_params(), assn_returns() {} |
---|
115 | |
---|
116 | public: |
---|
117 | Stats& operator+= (const Stats& o) { |
---|
118 | sum( n_decls, o.n_decls ); |
---|
119 | sum( n_type_params, o.n_type_params ); |
---|
120 | sum( by_name, o.by_name ); |
---|
121 | sum( params, o.params ); |
---|
122 | sum( returns, o.returns ); |
---|
123 | sum( n_assns, o.n_assns ); |
---|
124 | sum( assn_params, o.assn_params ); |
---|
125 | sum( assn_returns, o.assn_returns ); |
---|
126 | |
---|
127 | return *this; |
---|
128 | } |
---|
129 | }; |
---|
130 | |
---|
131 | Stats for_linkage[LinkageSpec::NoOfSpecs]; ///< Stores separate stats per linkage |
---|
132 | std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting |
---|
133 | Stats total; |
---|
134 | |
---|
135 | void analyzeFunc( FunctionType* fnTy, ArgPackStats& params, ArgPackStats& returns ) { |
---|
136 | params += fnTy->get_parameters(); |
---|
137 | returns += fnTy->get_returnVals(); |
---|
138 | } |
---|
139 | |
---|
140 | public: |
---|
141 | using Visitor::visit; |
---|
142 | |
---|
143 | virtual void visit( FunctionDecl *decl ) { |
---|
144 | // skip if already seen declaration for this function |
---|
145 | const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName(); |
---|
146 | if ( ! seen_names.insert( mangleName ).second ) return; |
---|
147 | |
---|
148 | Stats& stats = for_linkage[ decl->get_linkage() ]; |
---|
149 | |
---|
150 | ++stats.n_decls; |
---|
151 | FunctionType* fnTy = decl->get_functionType(); |
---|
152 | const Type::ForallList& forall = fnTy->get_forall(); |
---|
153 | ++stats.n_type_params.at( forall.size() ); |
---|
154 | unsigned n_assns = 0; |
---|
155 | for ( TypeDecl* fdecl : forall ) { |
---|
156 | n_assns += fdecl->get_assertions().size(); |
---|
157 | for ( DeclarationWithType* assn : fdecl->get_assertions() ) { |
---|
158 | FunctionType *assnTy = 0; |
---|
159 | if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { |
---|
160 | if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { |
---|
161 | assnTy = dynamic_cast<FunctionType*>(ptrTy->get_base()); |
---|
162 | } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); |
---|
163 | } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { |
---|
164 | assnTy = assnDecl->get_functionType(); |
---|
165 | } |
---|
166 | if ( assnTy ) analyzeFunc( assnTy, stats.assn_params, stats.assn_returns ); |
---|
167 | } |
---|
168 | } |
---|
169 | ++stats.n_assns[ n_assns ]; |
---|
170 | |
---|
171 | ++stats.by_name[ decl->get_name() ]; |
---|
172 | |
---|
173 | analyzeFunc( fnTy, stats.params, stats.returns ); |
---|
174 | } |
---|
175 | |
---|
176 | private: |
---|
177 | template<typename F> |
---|
178 | void printAll( const std::string& name, F extract ) { |
---|
179 | std::cout << "\"" << name << "\","; |
---|
180 | for ( const auto& stats : for_linkage ) { |
---|
181 | std::cout << "," << extract(stats); |
---|
182 | } |
---|
183 | std::cout << "," << extract(total) << std::endl; |
---|
184 | } |
---|
185 | |
---|
186 | template<typename F> |
---|
187 | void printAllMap( const std::string& name, F extract ) { |
---|
188 | for ( const auto& entry : extract(total) ) { |
---|
189 | const auto& key = entry.first; |
---|
190 | std::cout << "\"" << name << "\"," << key; |
---|
191 | for ( const auto& stats : for_linkage ) { |
---|
192 | const auto& map = extract(stats); |
---|
193 | auto it = map.find( key ); |
---|
194 | if ( it == map.end() ) std::cout << ",0"; |
---|
195 | else std::cout << "," << it->second; |
---|
196 | } |
---|
197 | std::cout << "," << entry.second << std::endl; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | template<typename F> |
---|
202 | void printAllHisto( const std::string& name, F extract ) { |
---|
203 | VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
204 | VectorMap<unsigned> thisto; |
---|
205 | |
---|
206 | for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } |
---|
207 | |
---|
208 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
209 | // can't be a higher count in one of the sub-histograms than the total |
---|
210 | histos[i].reserve( thisto.size() ); |
---|
211 | |
---|
212 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
213 | } |
---|
214 | |
---|
215 | for ( unsigned i = 0; i < thisto.size(); ++i ) { |
---|
216 | std::cout << "\"" << name << "\"," << i; |
---|
217 | for ( const auto& histo : histos ) { |
---|
218 | std::cout << "," << histo[i]; |
---|
219 | } |
---|
220 | std::cout << "," << thisto[i] << std::endl; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | template<typename F> |
---|
225 | void printAllSparseHisto( const std::string& name, F extract ) { |
---|
226 | std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
227 | std::map<unsigned, unsigned> thisto; |
---|
228 | |
---|
229 | for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; } |
---|
230 | |
---|
231 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
232 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
233 | } |
---|
234 | |
---|
235 | for ( const auto& entry : thisto ) { |
---|
236 | const auto& key = entry.first; |
---|
237 | std::cout << "\"" << name << "\"," << key; |
---|
238 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
239 | auto it = histos[i].find( key ); |
---|
240 | if ( it == histos[i].end() ) std::cout << ",0"; |
---|
241 | else std::cout << "," << it->second; |
---|
242 | } |
---|
243 | std::cout << "," << entry.second << std::endl; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | template<typename F> |
---|
248 | void printAllPack( const std::string& name, F extract ) { |
---|
249 | printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; }); |
---|
250 | printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; }); |
---|
251 | printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; }); |
---|
252 | printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; }); |
---|
253 | printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; }); |
---|
254 | } |
---|
255 | |
---|
256 | public: |
---|
257 | void print() { |
---|
258 | for ( auto& stats : for_linkage ) { |
---|
259 | total += stats; |
---|
260 | } |
---|
261 | |
---|
262 | std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl; |
---|
263 | |
---|
264 | printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; }); |
---|
265 | printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); |
---|
266 | printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); |
---|
267 | printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; }); |
---|
268 | printAllPack("params", [](const Stats& stats) { return stats.params; }); |
---|
269 | printAllPack("returns", [](const Stats& stats) { return stats.returns; }); |
---|
270 | printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; }); |
---|
271 | printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; }); |
---|
272 | printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; }); |
---|
273 | } |
---|
274 | }; |
---|
275 | |
---|
276 | void printDeclStats( std::list< Declaration * > &translationUnit ) { |
---|
277 | DeclStats stats; |
---|
278 | acceptAll( translationUnit, stats ); |
---|
279 | stats.print(); |
---|
280 | } |
---|
281 | |
---|
282 | } // namespace CodeTools |
---|
283 | |
---|
284 | // Local Variables: // |
---|
285 | // tab-width: 4 // |
---|
286 | // mode: c++ // |
---|
287 | // compile-command: "make install" // |
---|
288 | // End: // |
---|