source: src/CodeTools/DeclStats.cc @ b04a408

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 b04a408 was e5c74d1, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Convert DeclStats? to PassVisitor?

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