source: src/CodeTools/DeclStats.cc @ 65e9bc1

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 65e9bc1 was 1e6b350, checked in by Aaron Moss <a3moss@…>, 7 years ago

Added basic expression analysis to DeclStats?

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