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

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

Add assertion analysis to DeclStats?

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