Ignore:
Timestamp:
Feb 6, 2017, 3:03:27 PM (7 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
b4d65c7
Parents:
567903e
Message:

Add assertion analysis to DeclStats?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/DeclStats.cc

    r567903e r6a5f0e7  
    3333                struct Stats {
    3434                        unsigned n_decls;     ///< Total number of declarations
    35                         unsigned mono_decls;  ///< Monomorphic declarations
    36                         unsigned poly_decls;  ///< Polymorphic declarations
     35                        /// Count of declarations with each number of assertion parameters
     36                        VectorMap<unsigned> n_type_params;
    3737                        /// Count of declarations with each name
    3838                        std::unordered_map<std::string, unsigned> by_name;
     39
    3940                        /// Count of declarations with each number of parameters
    40                         VectorMap<unsigned> by_params;
     41                        VectorMap<unsigned> n_params;
    4142                        /// Count of declarations with each number of return types
    42                         VectorMap<unsigned> by_returns;
     43                        VectorMap<unsigned> n_returns;
    4344                        /// Count of declarations with each number of polymorphic parameters
    4445                        VectorMap<unsigned> n_poly_params;
     
    5051                        std::map<unsigned, unsigned> p_poly_returns;
    5152
    52                         Stats() : n_decls(0), mono_decls(0), poly_decls(0), by_name(), by_params(), by_returns(), n_poly_params(), n_poly_returns(), p_poly_params(), p_poly_returns() {}
    53 
     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;
     67
     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() {}
     72
     73                private:
     74                        template<typename T>
     75                        static void sum(T& a, const T& b) { a += b; }
     76
     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];
     81                                }
     82                        }
     83
     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;
     88                                }
     89                        }
     90
     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;
     95                                }
     96                        }
     97
     98                public:
    5499                        Stats& operator+= (const Stats& o) {
    55                                 n_decls += o.n_decls;
    56                                 mono_decls += o.mono_decls;
    57                                 poly_decls += o.poly_decls;
     100                                sum( n_decls, o.n_decls );
     101                                sum( n_type_params, o.n_type_params );
     102                                sum( by_name, o.by_name );
    58103                               
    59                                 for ( auto& entry : o.by_name ) {
    60                                         by_name[ entry.first ] += entry.second;
    61                                 }
    62                                
    63                                 by_params.reserve( o.by_params.size() );
    64                                 for ( unsigned i = 0; i < o.by_params.size(); ++i ) {
    65                                         by_params[i] += o.by_params[i];
    66                                 }
    67 
    68                                 by_returns.reserve( o.by_returns.size() );
    69                                 for ( unsigned i = 0; i < o.by_returns.size(); ++i ) {
    70                                         by_returns[i] += o.by_returns[i];
    71                                 }
    72 
    73                                 n_poly_params.reserve( o.n_poly_params.size() );
    74                                 for ( unsigned i = 0; i < o.n_poly_params.size(); ++i ) {
    75                                         n_poly_params[i] += o.n_poly_params[i];
    76                                 }
    77 
    78                                 n_poly_returns.reserve( o.n_poly_returns.size() );
    79                                 for ( unsigned i = 0; i < o.n_poly_returns.size(); ++i ) {
    80                                         n_poly_returns[i] += o.n_poly_returns[i];
    81                                 }
    82 
    83                                 for ( const auto& entry : o.p_poly_params ) {
    84                                         p_poly_params[ entry.first ] += entry.second;
    85                                 }
    86 
    87                                 for ( const auto& entry : o.p_poly_returns ) {
    88                                         p_poly_returns[ entry.first ] += entry.second;
    89                                 }
     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 );
    90118
    91119                                return *this;
     
    96124                std::unordered_set<std::string> seen_names;  ///< Stores manglenames already seen to avoid double-counting
    97125                Stats total;
     126
     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                }
    98150
    99151        public:
     
    109161                        ++stats.n_decls;
    110162                        FunctionType* fnTy = decl->get_functionType();
    111                         if ( fnTy->get_forall().empty() ) ++stats.mono_decls; else ++stats.poly_decls;
     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 ];
    112182
    113183                        ++stats.by_name[ decl->get_name() ];
    114184
    115                         unsigned n_params = 0;
    116                         unsigned n_poly_params = 0;
    117                         for ( auto pdecl : fnTy->get_parameters() ) {
    118                                 n_params += pdecl->get_type()->size();
    119                                 if ( GenPoly::hasPolyBase( pdecl->get_type() ) ) ++n_poly_params;
    120                         }
    121                         ++stats.by_params.at( n_params );
    122                         ++stats.n_poly_params.at( n_poly_params );
    123                         if ( n_params > 0 ) ++stats.p_poly_params[ n_poly_params*100/n_params ];
    124 
    125                         unsigned n_returns = 0;
    126                         unsigned n_poly_returns = 0;
    127                         for ( auto rdecl : fnTy->get_returnVals() ) {
    128                                 n_returns += rdecl->get_type()->size();
    129                                 if ( GenPoly::hasPolyBase( rdecl->get_type() ) ) ++n_poly_returns;
    130                         }
    131                         ++stats.by_returns.at( n_returns );
    132                         ++stats.n_poly_returns.at( n_poly_returns );
    133                         if ( n_returns > 0 ) ++stats.p_poly_returns[ n_poly_returns*100/n_returns ];
     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 );
    134187                }
    135188
     
    181234                        }
    182235                }
     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                }
    183259               
    184260        public:
     
    190266                        std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl;
    191267
    192                         printAll("mono_decls", [](const Stats& stats) { return stats.mono_decls; });
    193                         printAll("poly_decls", [](const Stats& stats) { return stats.poly_decls; });
     268                        printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; });
    194269                        printAll("n_decls", [](const Stats& stats) { return stats.n_decls; });
    195270                        printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); });
    196 
    197                         printAllHisto("overloads", [](const Stats& stats) { return stats.by_name; });
     271                        printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; });
     272                       
    198273                        printAllMap("n_poly_params", [](const Stats& stats) { return stats.n_poly_params; });
    199                         printAllMap("n_params", [](const Stats& stats) { return stats.by_params; });
     274                        printAllMap("n_params", [](const Stats& stats) { return stats.n_params; });
    200275                        printAllMap("%_poly_params", [](const Stats& stats) { return stats.p_poly_params; });
    201276                        printAllMap("n_poly_returns", [](const Stats& stats) { return stats.n_poly_returns; });
    202                         printAllMap("n_returns", [](const Stats& stats) { return stats.by_returns; });
     277                        printAllMap("n_returns", [](const Stats& stats) { return stats.n_returns; });
    203278                        printAllMap("%_poly_returns", [](const Stats& stats) { return stats.p_poly_returns; });
     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; });
    204287                }
    205288        };
Note: See TracChangeset for help on using the changeset viewer.