LCOV - code coverage report
Current view: top level - gcc - optinfo-emit-json.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.2 % 243 229
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 15 15
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Emit optimization information as JSON files.
       2              :    Copyright (C) 2018-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #define INCLUDE_STRING
      22              : #include "config.h"
      23              : #include "system.h"
      24              : #include "coretypes.h"
      25              : 
      26              : #include "backend.h"
      27              : #include "tree.h"
      28              : #include "gimple.h"
      29              : #include "diagnostic-core.h"
      30              : 
      31              : #include "profile.h"
      32              : #include "output.h"
      33              : #include "tree-pass.h"
      34              : 
      35              : #include "optinfo.h"
      36              : #include "optinfo-emit-json.h"
      37              : #include "json.h"
      38              : #include "pretty-print.h"
      39              : #include "tree-pretty-print.h"
      40              : #include "gimple-pretty-print.h"
      41              : #include "cgraph.h"
      42              : 
      43              : #include "langhooks.h"
      44              : #include "version.h"
      45              : #include "context.h"
      46              : #include "pass_manager.h"
      47              : #include "selftest.h"
      48              : #include "dump-context.h"
      49              : #include <zlib.h>
      50              : 
      51              : /* optrecord_json_writer's ctor.  Populate the top-level parts of the
      52              :    in-memory JSON representation.  */
      53              : 
      54         1288 : optrecord_json_writer::optrecord_json_writer ()
      55         1288 : : m_root_tuple (nullptr), m_scopes ()
      56              : {
      57         1288 :   m_root_tuple = std::make_unique<json::array> ();
      58              : 
      59              :   /* Populate with metadata; compare with toplev.cc: print_version.  */
      60         1288 :   auto metadata = std::make_unique<json::object> ();
      61         1288 :   metadata->set_string ("format", "1");
      62         1288 :   auto generator = std::make_unique<json::object> ();
      63         1288 :   generator->set_string ("name", lang_hooks.name);
      64         1288 :   generator->set_string ("pkgversion", pkgversion_string);
      65         1288 :   generator->set_string ("version", version_string);
      66              :   /* TARGET_NAME is passed in by the Makefile.  */
      67         1288 :   generator->set_string ("target", TARGET_NAME);
      68         1288 :   metadata->set<json::object> ("generator", std::move (generator));
      69         1288 :   m_root_tuple->append<json::object> (std::move (metadata));
      70              : 
      71              :   /* TODO: capture command-line?
      72              :      see gen_producer_string in dwarf2out.cc (currently static).  */
      73              : 
      74              :   /* TODO: capture "any plugins?" flag (or the plugins themselves).  */
      75              : 
      76         1288 :   auto passes = std::make_unique<json::array> ();
      77              : 
      78              :   /* Call add_pass_list for all of the pass lists.  */
      79         1288 :   {
      80              : #define DEF_PASS_LIST(LIST) \
      81              :     add_pass_list (passes.get (), g->get_passes ()->LIST);
      82         1288 :     GCC_PASS_LISTS
      83              : #undef DEF_PASS_LIST
      84              :   }
      85         1288 :   m_root_tuple->append<json::array> (std::move (passes));
      86              : 
      87         1288 :   auto records = std::make_unique<json::array> ();
      88         1288 :   m_scopes.safe_push (records.get ());
      89         1288 :   m_root_tuple->append (std::move (records));
      90         1288 : }
      91              : 
      92              : /* Choose an appropriate filename, and write the saved records to it.  */
      93              : 
      94              : void
      95           41 : optrecord_json_writer::write () const
      96              : {
      97           41 :   pretty_printer pp;
      98           41 :   m_root_tuple->print (&pp, false);
      99              : 
     100           41 :   bool emitted_error = false;
     101           41 :   char *filename = concat (dump_base_name, ".opt-record.json.gz", nullptr);
     102           41 :   gzFile outfile = gzopen (filename, "w");
     103           41 :   if (outfile == nullptr)
     104              :     {
     105            0 :       error_at (UNKNOWN_LOCATION, "cannot open file %qs for writing optimization records",
     106              :                 filename); // FIXME: more info?
     107            0 :       goto cleanup;
     108              :     }
     109              : 
     110           41 :   if (gzputs (outfile, pp_formatted_text (&pp)) <= 0)
     111              :     {
     112            0 :       int tmp;
     113            0 :       error_at (UNKNOWN_LOCATION, "error writing optimization records to %qs: %s",
     114              :                 filename, gzerror (outfile, &tmp));
     115            0 :       emitted_error = true;
     116              :     }
     117              : 
     118           41 :  cleanup:
     119           41 :   if (outfile)
     120           41 :     if (gzclose (outfile) != Z_OK)
     121            0 :       if (!emitted_error)
     122            0 :         error_at (UNKNOWN_LOCATION, "error closing optimization records %qs",
     123              :                   filename);
     124              : 
     125           41 :   free (filename);
     126           41 : }
     127              : 
     128              : /* Add a record for OPTINFO to the queue of records to be written.  */
     129              : 
     130              : void
     131        48101 : optrecord_json_writer::add_record (const optinfo &optinfo)
     132              : {
     133        48101 :   auto obj = optinfo_to_json (optinfo);
     134        48101 :   auto borrowed_obj = obj.get ();
     135              : 
     136        48101 :   add_record (std::move (obj));
     137              : 
     138              :   /* Potentially push the scope.  */
     139        48101 :   if (optinfo.get_kind () == optinfo::kind::scope)
     140              :     {
     141         3998 :       auto children = std::make_unique<json::array> ();
     142         3998 :       m_scopes.safe_push (children.get ());
     143         3998 :       borrowed_obj->set<json::array> ("children", std::move (children));
     144         3998 :     }
     145        48101 : }
     146              : 
     147              : /* Private methods of optrecord_json_writer.  */
     148              : 
     149              : /* Add record OBJ to the innermost scope.  */
     150              : 
     151              : void
     152        48101 : optrecord_json_writer::add_record (std::unique_ptr<json::object> obj)
     153              : {
     154              :   /* Add to innermost scope.  */
     155        48101 :   gcc_assert (m_scopes.length () > 0);
     156        48101 :   m_scopes[m_scopes.length () - 1]->append<json::object> (std::move (obj));
     157        48101 : }
     158              : 
     159              : /* Pop the innermost scope.  */
     160              : 
     161              : void
     162         3998 : optrecord_json_writer::pop_scope ()
     163              : {
     164         3998 :   m_scopes.pop ();
     165              : 
     166              :   /* We should never pop the top-level records array.  */
     167         3998 :   gcc_assert (m_scopes.length () > 0);
     168         3998 : }
     169              : 
     170              : /* Create a JSON object representing LOC.  */
     171              : 
     172              : std::unique_ptr<json::object>
     173        48105 : optrecord_json_writer::impl_location_to_json (dump_impl_location_t loc)
     174              : {
     175        48105 :   auto obj = std::make_unique<json::object> ();
     176        48105 :   obj->set_string ("file", loc.m_file);
     177        48105 :   obj->set_integer ("line", loc.m_line);
     178        48105 :   if (loc.m_function)
     179        48105 :     obj->set_string ("function", loc.m_function);
     180        48105 :   return obj;
     181              : }
     182              : 
     183              : /* Create a JSON object representing LOC.  */
     184              : 
     185              : std::unique_ptr<json::object>
     186        39602 : optrecord_json_writer::location_to_json (location_t loc)
     187              : {
     188        39602 :   gcc_assert (LOCATION_LOCUS (loc) != UNKNOWN_LOCATION);
     189        39602 :   expanded_location exploc = expand_location (loc);
     190        39602 :   auto obj = std::make_unique<json::object> ();
     191        39602 :   obj->set_string ("file", exploc.file);
     192        39602 :   obj->set_integer ("line", exploc.line);
     193        39602 :   obj->set_integer ("column", exploc.column);
     194        39602 :   return obj;
     195              : }
     196              : 
     197              : /* Create a JSON object representing COUNT.  */
     198              : 
     199              : std::unique_ptr<json::object>
     200        48045 : optrecord_json_writer::profile_count_to_json (profile_count count)
     201              : {
     202        48045 :   auto obj = std::make_unique<json::object> ();
     203        48045 :   obj->set_integer ("value", count.to_gcov_type ());
     204        48045 :   obj->set_string ("quality", profile_quality_as_string (count.quality ()));
     205        48045 :   return obj;
     206              : }
     207              : 
     208              : /* Get a string for use when referring to PASS in the saved optimization
     209              :    records.  */
     210              : 
     211              : std::unique_ptr<json::string>
     212       551757 : optrecord_json_writer::get_id_value_for_pass (const opt_pass &pass)
     213              : {
     214       551757 :   pretty_printer pp;
     215              :   /* this is host-dependent, but will be consistent for a given host.  */
     216       551757 :   pp_pointer (&pp, static_cast<const void *> (&pass));
     217       551757 :   return std::make_unique<json::string> (pp_formatted_text (&pp));
     218       551757 : }
     219              : 
     220              : /* Create a JSON object representing PASS.  */
     221              : 
     222              : std::unique_ptr<json::object>
     223       506184 : optrecord_json_writer::pass_to_json (const opt_pass &pass)
     224              : {
     225       506184 :   auto obj = std::make_unique<json::object> ();
     226       506184 :   const char *type = nullptr;
     227       506184 :   switch (pass.type)
     228              :     {
     229            0 :     default:
     230            0 :       gcc_unreachable ();
     231              :     case GIMPLE_PASS:
     232              :       type = "gimple";
     233              :       break;
     234       136528 :     case RTL_PASS:
     235       136528 :       type = "rtl";
     236       136528 :       break;
     237        28336 :     case SIMPLE_IPA_PASS:
     238        28336 :       type = "simple_ipa";
     239        28336 :       break;
     240        24472 :     case IPA_PASS:
     241        24472 :       type = "ipa";
     242        24472 :       break;
     243              :     }
     244       506184 :   obj->set<json::string> ("id", get_id_value_for_pass (pass));
     245       506184 :   obj->set_string ("type", type);
     246       506184 :   obj->set_string ("name", pass.name);
     247              :   /* Represent the optgroup flags as an array.  */
     248       506184 :   {
     249       506184 :     auto optgroups = std::make_unique<json::array> ();
     250      3543288 :     for (const kv_pair<optgroup_flags_t> *optgroup = optgroup_options;
     251      3543288 :          optgroup->name != nullptr; optgroup++)
     252      3037104 :       if (optgroup->value != OPTGROUP_ALL
     253      2530920 :           && (pass.optinfo_flags & optgroup->value))
     254        91448 :         optgroups->append_string (optgroup->name);
     255       506184 :     obj->set<json::array> ("optgroups", std::move (optgroups));
     256       506184 :   }
     257       506184 :   obj->set_integer ("num", pass.static_pass_number);
     258       506184 :   return obj;
     259              : }
     260              : 
     261              : /* Create a JSON array for LOC representing the chain of inlining
     262              :    locations.
     263              :    Compare with lhd_print_error_function and cp_print_error_function.  */
     264              : 
     265              : std::unique_ptr<json::array>
     266        30887 : optrecord_json_writer::inlining_chain_to_json (location_t loc)
     267              : {
     268        30887 :   auto array = std::make_unique<json::array> ();
     269              : 
     270        30887 :   tree abstract_origin = LOCATION_BLOCK (loc);
     271              : 
     272        60781 :   while (abstract_origin)
     273              :     {
     274        29894 :       location_t *locus;
     275        29894 :       tree block = abstract_origin;
     276              : 
     277        29894 :       locus = &BLOCK_SOURCE_LOCATION (block);
     278        29894 :       tree fndecl = nullptr;
     279        29894 :       block = BLOCK_SUPERCONTEXT (block);
     280        61386 :       while (block && TREE_CODE (block) == BLOCK
     281        46874 :              && BLOCK_ABSTRACT_ORIGIN (block))
     282              :         {
     283         3269 :           tree ao = BLOCK_ABSTRACT_ORIGIN (block);
     284         3269 :           if (TREE_CODE (ao) == FUNCTION_DECL)
     285              :             {
     286              :               fndecl = ao;
     287              :               break;
     288              :             }
     289         1598 :           else if (TREE_CODE (ao) != BLOCK)
     290              :             break;
     291              : 
     292         1598 :           block = BLOCK_SUPERCONTEXT (block);
     293              :         }
     294        29894 :       if (fndecl)
     295              :         abstract_origin = block;
     296              :       else
     297              :         {
     298        57719 :           while (block && TREE_CODE (block) == BLOCK)
     299        29496 :             block = BLOCK_SUPERCONTEXT (block);
     300              : 
     301        28223 :           if (block && TREE_CODE (block) == FUNCTION_DECL)
     302              :             fndecl = block;
     303              :           abstract_origin = nullptr;
     304              :         }
     305              :       if (fndecl)
     306              :         {
     307        29894 :           auto obj = std::make_unique<json::object> ();
     308        29894 :           const char *printable_name
     309        29894 :             = lang_hooks.decl_printable_name (fndecl, 2);
     310        29894 :           obj->set_string ("fndecl", printable_name);
     311        29894 :           if (LOCATION_LOCUS (*locus) != UNKNOWN_LOCATION)
     312         1671 :             obj->set<json::object> ("site", location_to_json (*locus));
     313        29894 :           array->append<json::object> (std::move (obj));
     314        29894 :         }
     315              :     }
     316              : 
     317        30887 :   return array;
     318              : }
     319              : 
     320              : /* Create a JSON object representing OPTINFO.  */
     321              : 
     322              : std::unique_ptr<json::object>
     323        48105 : optrecord_json_writer::optinfo_to_json (const optinfo &optinfo)
     324              : {
     325        48105 :   auto obj = std::make_unique<json::object> ();
     326              : 
     327        48105 :   obj->set<json::object>
     328        48105 :     ("impl_location",
     329        48105 :      impl_location_to_json (optinfo.get_impl_location ()));
     330              : 
     331        48105 :   const char *kind_str = optinfo::kind_to_string (optinfo.get_kind ());
     332        48105 :   obj->set_string ("kind", kind_str);
     333        48105 :   auto message = std::make_unique<json::array> ();
     334       213984 :   for (unsigned i = 0; i < optinfo.num_items (); i++)
     335              :     {
     336       117774 :       const optinfo_item *item = optinfo.get_item (i);
     337       117774 :       switch (item->get_kind ())
     338              :         {
     339            0 :         default:
     340            0 :           gcc_unreachable ();
     341        84347 :         case optinfo_item::kind::text:
     342        84347 :           {
     343        84347 :             message->append_string (item->get_text ());
     344              :           }
     345        84347 :           break;
     346        10941 :         case optinfo_item::kind::tree:
     347        10941 :           {
     348        10941 :             auto json_item = std::make_unique<json::object> ();
     349        10941 :             json_item->set_string ("expr", item->get_text ());
     350              : 
     351              :             /* Capture any location for the node.  */
     352        10941 :             if (LOCATION_LOCUS (item->get_location ()) != UNKNOWN_LOCATION)
     353            0 :               json_item->set<json::object>
     354            0 :                 ("location",
     355            0 :                  location_to_json (item->get_location ()));
     356              : 
     357        10941 :             message->append<json::object> (std::move (json_item));
     358        10941 :           }
     359        10941 :           break;
     360        22224 :         case optinfo_item::kind::gimple:
     361        22224 :           {
     362        22224 :             auto json_item = std::make_unique<json::object> ();
     363        22224 :             json_item->set_string ("stmt", item->get_text ());
     364              : 
     365              :             /* Capture any location for the stmt.  */
     366        22224 :             if (LOCATION_LOCUS (item->get_location ()) != UNKNOWN_LOCATION)
     367         7541 :               json_item->set<json::object>
     368         7541 :                 ("location",
     369         7541 :                  location_to_json (item->get_location ()));
     370              : 
     371        22224 :             message->append<json::object> (std::move (json_item));
     372        22224 :           }
     373        22224 :           break;
     374          262 :         case optinfo_item::kind::symtab_node:
     375          262 :           {
     376          262 :             auto json_item = std::make_unique<json::object> ();
     377          262 :             json_item->set_string ("symtab_node", item->get_text ());
     378              : 
     379              :             /* Capture any location for the node.  */
     380          262 :             if (LOCATION_LOCUS (item->get_location ()) != UNKNOWN_LOCATION)
     381          262 :               json_item->set<json::object>
     382          262 :                 ("location",
     383          262 :                  location_to_json (item->get_location ()));
     384          262 :             message->append<json::object> (std::move (json_item));
     385          262 :           }
     386          262 :           break;
     387              :         }
     388              :    }
     389        48105 :   obj->set<json::array> ("message", std::move (message));
     390              : 
     391        48105 :   if (auto pass = optinfo.get_pass ())
     392        45573 :     obj->set<json::string> ("pass",
     393        45573 :                             get_id_value_for_pass (*pass));
     394              : 
     395        48105 :   profile_count count = optinfo.get_count ();
     396        48105 :   if (count.initialized_p ())
     397        48045 :     obj->set<json::object> ("count", profile_count_to_json (count));
     398              : 
     399              :   /* Record any location, handling the case where of an UNKNOWN_LOCATION
     400              :      within an inlined block.  */
     401        48105 :   location_t loc = optinfo.get_location_t ();
     402        48105 :   if (get_pure_location (line_table, loc) != UNKNOWN_LOCATION)
     403              :     {
     404              :       // TOOD: record the location (just caret for now)
     405              :       // TODO: start/finish also?
     406        30128 :       obj->set<json::object> ("location", location_to_json (loc));
     407              :     }
     408              : 
     409        48105 :   if (current_function_decl)
     410              :     {
     411        45491 :       const char *fnname
     412        45491 :         = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
     413        45491 :       obj->set_string ("function", fnname);
     414              :     }
     415              : 
     416        48105 :   if (loc != UNKNOWN_LOCATION)
     417        30887 :     obj->set<json::array> ("inlining_chain", inlining_chain_to_json (loc));
     418              : 
     419        96210 :   return obj;
     420        48105 : }
     421              : 
     422              : /* Add a json description of PASS and its siblings to ARR, recursing into
     423              :    child passes (adding their descriptions within a "children" array).  */
     424              : 
     425              : void
     426        33488 : optrecord_json_writer::add_pass_list (json::array *arr,
     427              :                                       const opt_pass *pass)
     428              : {
     429        33488 :   gcc_assert (pass);
     430       506184 :   do
     431              :     {
     432       506184 :       auto pass_obj = pass_to_json (*pass);
     433       506184 :       if (pass->sub)
     434              :         {
     435        27048 :           auto child_arr = std::make_unique<json::array> ();
     436        27048 :           add_pass_list (child_arr.get (), pass->sub);
     437        27048 :           pass_obj->set ("children", std::move (child_arr));
     438        27048 :         }
     439       506184 :       arr->append (std::move (pass_obj));
     440       506184 :       pass = pass->next;
     441       506184 :     }
     442       506184 :   while (pass);
     443        33488 : }
     444              : 
     445              : #if CHECKING_P
     446              : 
     447              : namespace selftest {
     448              : 
     449              : /* Verify that we can build a JSON optimization record from dump_*
     450              :    calls.  */
     451              : 
     452              : static void
     453            4 : test_building_json_from_dump_calls ()
     454              : {
     455            4 :   temp_dump_context tmp (true, true, MSG_NOTE);
     456            4 :   dump_user_location_t loc;
     457            4 :   dump_printf_loc (MSG_NOTE, loc, "test of tree: ");
     458            4 :   dump_generic_expr (MSG_NOTE, TDF_SLIM, integer_zero_node);
     459            4 :   optinfo *info = tmp.get_pending_optinfo ();
     460            4 :   ASSERT_TRUE (info != nullptr);
     461            4 :   ASSERT_EQ (info->num_items (), 2);
     462              : 
     463            4 :   optrecord_json_writer writer;
     464            4 :   auto json_obj = writer.optinfo_to_json (*info);
     465            4 :   ASSERT_TRUE (json_obj != nullptr);
     466              : 
     467              :   /* Verify that the json is sane.  */
     468            4 :   pretty_printer pp;
     469            4 :   json_obj->print (&pp, false);
     470            4 :   const char *json_str = pp_formatted_text (&pp);
     471            4 :   ASSERT_STR_CONTAINS (json_str, "impl_location");
     472            4 :   ASSERT_STR_CONTAINS (json_str, "\"kind\": \"note\"");
     473            4 :   ASSERT_STR_CONTAINS (json_str,
     474              :                        "\"message\": [\"test of tree: \", {\"expr\": \"0\"}]");
     475            8 : }
     476              : 
     477              : /* Run all of the selftests within this file.  */
     478              : 
     479              : void
     480            4 : optinfo_emit_json_cc_tests ()
     481              : {
     482            4 :   test_building_json_from_dump_calls ();
     483            4 : }
     484              : 
     485              : } // namespace selftest
     486              : 
     487              : #endif /* CHECKING_P */
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.