source: src/CodeTools/DeclStats.cc @ 31868da

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 31868da was 31868da, checked in by Aaron Moss <a3moss@…>, 7 years ago

Start work on adding expression statistics to DeclStats? pass

  • Property mode set to 100644
File size: 13.3 KB
Line 
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
31namespace 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                        ++exprs_by_fanout_at_depth[ std::make_pair(depth, fanout) ];
189                        for ( Expression* arg : args ) {
190                                if ( UntypedExpr *uearg = dynamic_cast<UntypedExpr*>(arg) ) {
191                                        analyzeExpr( uearg, depth+1 );
192                                }
193                        }
194                }
195
196        public:
197                using Visitor::visit;
198
199                virtual void visit( FunctionDecl *decl ) {
200                        // skip if already seen declaration for this function
201                        const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName();
202                        if ( ! seen_names.insert( mangleName ).second ) return;
203                       
204                        Stats& stats = for_linkage[ decl->get_linkage() ];
205
206                        ++stats.n_decls;
207                        FunctionType* fnTy = decl->get_functionType();
208                        const Type::ForallList& forall = fnTy->get_forall();
209                        ++stats.n_type_params.at( forall.size() );
210                        unsigned n_assns = 0;
211                        for ( TypeDecl* fdecl : forall ) {
212                                n_assns += fdecl->get_assertions().size();
213                                for ( DeclarationWithType* assn : fdecl->get_assertions() ) {
214                                        FunctionType *assnTy = 0;
215                                        if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) {
216                                                if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) {
217                                                        assnTy = dynamic_cast<FunctionType*>(ptrTy->get_base());
218                                                } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type());
219                                        } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) {
220                                                assnTy = assnDecl->get_functionType();
221                                        }
222                                        if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns );
223                                }
224                        }
225                        ++stats.n_assns[ n_assns ];
226
227                        ++stats.by_name[ decl->get_name() ];
228
229                        analyzeFunc( fnTy, stats, stats.params, stats.returns );
230                }
231
232                virtual void visit( UntypedExpr *expr ) {
233                        analyzeExpr( expr, 0 );
234                }
235
236        private:
237                template<typename F>
238                void printAll( const std::string& name, F extract ) {
239                        std::cout << "\"" << name << "\",";
240                        for ( const auto& stats : for_linkage ) {
241                                std::cout << "," << extract(stats);
242                        }
243                        std::cout << "," << extract(total) << std::endl;
244                }
245
246                template<typename F>
247                void printAllMap( const std::string& name, F extract ) {
248                        for ( const auto& entry : extract(total) ) {
249                                const auto& key = entry.first;
250                                std::cout << "\"" << name << "\"," << key;
251                                for ( const auto& stats : for_linkage ) {
252                                        const auto& map = extract(stats);
253                                        auto it = map.find( key );
254                                        if ( it == map.end() ) std::cout << ",0";
255                                        else std::cout << "," << it->second;
256                                }
257                                std::cout  << "," << entry.second << std::endl;
258                        }
259                }
260
261                template<typename F>
262                void printAllHisto( const std::string& name, F extract ) {
263                        VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs];
264                        VectorMap<unsigned> thisto;
265
266                        for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); }
267
268                        for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
269                                // can't be a higher count in one of the sub-histograms than the total
270                                histos[i].reserve( thisto.size() );
271
272                                for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; }
273                        }
274
275                        for ( unsigned i = 0; i < thisto.size(); ++i ) {
276                                std::cout << "\"" << name << "\"," << i;
277                                for ( const auto& histo : histos ) {
278                                        std::cout << "," << histo[i];
279                                }
280                                std::cout << "," << thisto[i] << std::endl;
281                        }
282                }
283
284                template<typename F>
285                void printAllSparseHisto( const std::string& name, F extract ) {
286                        std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs];
287                        std::map<unsigned, unsigned> thisto;
288
289                        for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; }
290
291                        for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
292                                for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; }
293                        }
294
295                        for ( const auto& entry : thisto ) {
296                                const auto& key = entry.first;
297                                std::cout << "\"" << name << "\"," << key;
298                                for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
299                                        auto it = histos[i].find( key );
300                                        if ( it == histos[i].end() ) std::cout << ",0";
301                                        else std::cout << "," << it->second;
302                                }
303                                std::cout << "," << entry.second << std::endl;
304                        }
305                }
306
307                template<typename F>
308                void printAllPack( const std::string& name, F extract ) {
309                        printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; });
310                        printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; });
311                        printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; });
312                        printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; });
313                        printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; });
314                        printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; });
315                        printAllMap("%_new_types_in_" + name, [&extract](const Stats& stats) { return extract(stats).p_new; });
316                }
317
318                void printPairMap( const std::string& name, 
319                                   const std::map<std::pair<unsigned, unsigned>, unsigned>& map ) {
320                        for ( const auto& entry : map ) {
321                                const auto& key = entry.first;
322                                std::cout << "\"" << name << "\"," << key.first << "," << key.second << "," 
323                                          << entry.second << std::endl;
324                        }
325                }
326               
327        public:
328                void print() {
329                        for ( auto& stats : for_linkage ) {
330                                total += stats;
331                        }
332
333                        std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl;
334
335                        printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; });
336                        printAll("n_decls", [](const Stats& stats) { return stats.n_decls; });
337                        printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); });
338                        printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; });
339                        printAll("basic_type_names", [](const Stats& stats) { return stats.basic_type_names.size(); });
340                        printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; });
341                        printAllSparseHisto("decls_using_basic_type", [](const Stats& stats) { return stats.basic_type_decls; });
342                        printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); });
343                        printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; });
344                        printAllSparseHisto("decls_using_compound_type", [](const Stats& stats) { return stats.compound_type_decls; });
345                        printAllPack("params", [](const Stats& stats) { return stats.params; });
346                        printAllPack("returns", [](const Stats& stats) { return stats.returns; });
347                        printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; });
348                        printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; });
349                        printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; });
350                        std::cout << std::endl;
351
352                        printPairMap("exprs_by_depth+fanout", exprs_by_fanout_at_depth);
353                }
354        };
355
356        void printDeclStats( std::list< Declaration * > &translationUnit ) {
357                DeclStats stats;
358                acceptAll( translationUnit, stats );
359                stats.print();
360        }
361       
362} // namespace CodeTools
363
364// Local Variables: //
365// tab-width: 4 //
366// mode: c++ //
367// compile-command: "make install" //
368// End: //
Note: See TracBrowser for help on using the repository browser.