source: src/CodeTools/DeclStats.cc @ 6ce9f7c7

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 6ce9f7c7 was eed5e48, checked in by Aaron Moss <a3moss@…>, 7 years ago

Update to DeclStats?

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