1 /* Emit optimization information as JSON files.
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
28 #include "diagnostic-core.h"
32 #include "tree-pass.h"
35 #include "optinfo-emit-json.h"
37 #include "pretty-print.h"
38 #include "tree-pretty-print.h"
39 #include "gimple-pretty-print.h"
42 #include "langhooks.h"
45 #include "pass_manager.h"
47 #include "dump-context.h"
50 /* optrecord_json_writer's ctor. Populate the top-level parts of the
51 in-memory JSON representation. */
53 optrecord_json_writer::optrecord_json_writer ()
54 : m_root_tuple (NULL
), m_scopes ()
56 m_root_tuple
= new json::array ();
58 /* Populate with metadata; compare with toplev.cc: print_version. */
59 json::object
*metadata
= new json::object ();
60 m_root_tuple
->append (metadata
);
61 metadata
->set_string ("format", "1");
62 json::object
*generator
= new json::object ();
63 metadata
->set ("generator", generator
);
64 generator
->set_string ("name", lang_hooks
.name
);
65 generator
->set_string ("pkgversion", pkgversion_string
);
66 generator
->set_string ("version", version_string
);
67 /* TARGET_NAME is passed in by the Makefile. */
68 generator
->set_string ("target", TARGET_NAME
);
70 /* TODO: capture command-line?
71 see gen_producer_string in dwarf2out.cc (currently static). */
73 /* TODO: capture "any plugins?" flag (or the plugins themselves). */
75 json::array
*passes
= new json::array ();
76 m_root_tuple
->append (passes
);
78 /* Call add_pass_list for all of the pass lists. */
80 #define DEF_PASS_LIST(LIST) \
81 add_pass_list (passes, g->get_passes ()->LIST);
86 json::array
*records
= new json::array ();
87 m_root_tuple
->append (records
);
89 m_scopes
.safe_push (records
);
92 /* optrecord_json_writer's ctor.
93 Delete the in-memory JSON representation. */
95 optrecord_json_writer::~optrecord_json_writer ()
100 /* Choose an appropriate filename, and write the saved records to it. */
103 optrecord_json_writer::write () const
106 m_root_tuple
->print (&pp
);
108 bool emitted_error
= false;
109 char *filename
= concat (dump_base_name
, ".opt-record.json.gz", NULL
);
110 gzFile outfile
= gzopen (filename
, "w");
113 error_at (UNKNOWN_LOCATION
, "cannot open file %qs for writing optimization records",
114 filename
); // FIXME: more info?
118 if (gzputs (outfile
, pp_formatted_text (&pp
)) <= 0)
121 error_at (UNKNOWN_LOCATION
, "error writing optimization records to %qs: %s",
122 filename
, gzerror (outfile
, &tmp
));
123 emitted_error
= true;
128 if (gzclose (outfile
) != Z_OK
)
130 error_at (UNKNOWN_LOCATION
, "error closing optimization records %qs",
136 /* Add a record for OPTINFO to the queue of records to be written. */
139 optrecord_json_writer::add_record (const optinfo
*optinfo
)
141 json::object
*obj
= optinfo_to_json (optinfo
);
145 /* Potentially push the scope. */
146 if (optinfo
->get_kind () == OPTINFO_KIND_SCOPE
)
148 json::array
*children
= new json::array ();
149 obj
->set ("children", children
);
150 m_scopes
.safe_push (children
);
154 /* Private methods of optrecord_json_writer. */
156 /* Add record OBJ to the innermost scope. */
159 optrecord_json_writer::add_record (json::object
*obj
)
161 /* Add to innermost scope. */
162 gcc_assert (m_scopes
.length () > 0);
163 m_scopes
[m_scopes
.length () - 1]->append (obj
);
166 /* Pop the innermost scope. */
169 optrecord_json_writer::pop_scope ()
173 /* We should never pop the top-level records array. */
174 gcc_assert (m_scopes
.length () > 0);
177 /* Create a JSON object representing LOC. */
180 optrecord_json_writer::impl_location_to_json (dump_impl_location_t loc
)
182 json::object
*obj
= new json::object ();
183 obj
->set_string ("file", loc
.m_file
);
184 obj
->set_integer ("line", loc
.m_line
);
186 obj
->set_string ("function", loc
.m_function
);
190 /* Create a JSON object representing LOC. */
193 optrecord_json_writer::location_to_json (location_t loc
)
195 gcc_assert (LOCATION_LOCUS (loc
) != UNKNOWN_LOCATION
);
196 expanded_location exploc
= expand_location (loc
);
197 json::object
*obj
= new json::object ();
198 obj
->set_string ("file", exploc
.file
);
199 obj
->set_integer ("line", exploc
.line
);
200 obj
->set_integer ("column", exploc
.column
);
204 /* Create a JSON object representing COUNT. */
207 optrecord_json_writer::profile_count_to_json (profile_count count
)
209 json::object
*obj
= new json::object ();
210 obj
->set_integer ("value", count
.to_gcov_type ());
211 obj
->set_string ("quality", profile_quality_as_string (count
.quality ()));
215 /* Get a string for use when referring to PASS in the saved optimization
219 optrecord_json_writer::get_id_value_for_pass (opt_pass
*pass
)
222 /* this is host-dependent, but will be consistent for a given host. */
223 pp_pointer (&pp
, static_cast<void *> (pass
));
224 return new json::string (pp_formatted_text (&pp
));
227 /* Create a JSON object representing PASS. */
230 optrecord_json_writer::pass_to_json (opt_pass
*pass
)
232 json::object
*obj
= new json::object ();
233 const char *type
= NULL
;
244 case SIMPLE_IPA_PASS
:
251 obj
->set ("id", get_id_value_for_pass (pass
));
252 obj
->set_string ("type", type
);
253 obj
->set_string ("name", pass
->name
);
254 /* Represent the optgroup flags as an array. */
256 json::array
*optgroups
= new json::array ();
257 obj
->set ("optgroups", optgroups
);
258 for (const kv_pair
<optgroup_flags_t
> *optgroup
= optgroup_options
;
259 optgroup
->name
!= NULL
; optgroup
++)
260 if (optgroup
->value
!= OPTGROUP_ALL
261 && (pass
->optinfo_flags
& optgroup
->value
))
262 optgroups
->append (new json::string (optgroup
->name
));
264 obj
->set_integer ("num", pass
->static_pass_number
);
268 /* Create a JSON array for LOC representing the chain of inlining
270 Compare with lhd_print_error_function and cp_print_error_function. */
273 optrecord_json_writer::inlining_chain_to_json (location_t loc
)
275 json::array
*array
= new json::array ();
277 tree abstract_origin
= LOCATION_BLOCK (loc
);
279 while (abstract_origin
)
282 tree block
= abstract_origin
;
284 locus
= &BLOCK_SOURCE_LOCATION (block
);
286 block
= BLOCK_SUPERCONTEXT (block
);
287 while (block
&& TREE_CODE (block
) == BLOCK
288 && BLOCK_ABSTRACT_ORIGIN (block
))
290 tree ao
= BLOCK_ABSTRACT_ORIGIN (block
);
291 if (TREE_CODE (ao
) == FUNCTION_DECL
)
296 else if (TREE_CODE (ao
) != BLOCK
)
299 block
= BLOCK_SUPERCONTEXT (block
);
302 abstract_origin
= block
;
305 while (block
&& TREE_CODE (block
) == BLOCK
)
306 block
= BLOCK_SUPERCONTEXT (block
);
308 if (block
&& TREE_CODE (block
) == FUNCTION_DECL
)
310 abstract_origin
= NULL
;
314 json::object
*obj
= new json::object ();
315 const char *printable_name
316 = lang_hooks
.decl_printable_name (fndecl
, 2);
317 obj
->set_string ("fndecl", printable_name
);
318 if (LOCATION_LOCUS (*locus
) != UNKNOWN_LOCATION
)
319 obj
->set ("site", location_to_json (*locus
));
327 /* Create a JSON object representing OPTINFO. */
330 optrecord_json_writer::optinfo_to_json (const optinfo
*optinfo
)
332 json::object
*obj
= new json::object ();
334 obj
->set ("impl_location",
335 impl_location_to_json (optinfo
->get_impl_location ()));
337 const char *kind_str
= optinfo_kind_to_string (optinfo
->get_kind ());
338 obj
->set_string ("kind", kind_str
);
339 json::array
*message
= new json::array ();
340 obj
->set ("message", message
);
341 for (unsigned i
= 0; i
< optinfo
->num_items (); i
++)
343 const optinfo_item
*item
= optinfo
->get_item (i
);
344 switch (item
->get_kind ())
348 case OPTINFO_ITEM_KIND_TEXT
:
350 message
->append (new json::string (item
->get_text ()));
353 case OPTINFO_ITEM_KIND_TREE
:
355 json::object
*json_item
= new json::object ();
356 json_item
->set_string ("expr", item
->get_text ());
358 /* Capture any location for the node. */
359 if (LOCATION_LOCUS (item
->get_location ()) != UNKNOWN_LOCATION
)
360 json_item
->set ("location",
361 location_to_json (item
->get_location ()));
363 message
->append (json_item
);
366 case OPTINFO_ITEM_KIND_GIMPLE
:
368 json::object
*json_item
= new json::object ();
369 json_item
->set_string ("stmt", item
->get_text ());
371 /* Capture any location for the stmt. */
372 if (LOCATION_LOCUS (item
->get_location ()) != UNKNOWN_LOCATION
)
373 json_item
->set ("location",
374 location_to_json (item
->get_location ()));
376 message
->append (json_item
);
379 case OPTINFO_ITEM_KIND_SYMTAB_NODE
:
381 json::object
*json_item
= new json::object ();
382 json_item
->set_string ("symtab_node", item
->get_text ());
384 /* Capture any location for the node. */
385 if (LOCATION_LOCUS (item
->get_location ()) != UNKNOWN_LOCATION
)
386 json_item
->set ("location",
387 location_to_json (item
->get_location ()));
388 message
->append (json_item
);
394 if (optinfo
->get_pass ())
395 obj
->set ("pass", get_id_value_for_pass (optinfo
->get_pass ()));
397 profile_count count
= optinfo
->get_count ();
398 if (count
.initialized_p ())
399 obj
->set ("count", profile_count_to_json (count
));
401 /* Record any location, handling the case where of an UNKNOWN_LOCATION
402 within an inlined block. */
403 location_t loc
= optinfo
->get_location_t ();
404 if (get_pure_location (line_table
, loc
) != UNKNOWN_LOCATION
)
406 // TOOD: record the location (just caret for now)
407 // TODO: start/finish also?
408 obj
->set ("location", location_to_json (loc
));
411 if (current_function_decl
)
414 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
));
415 obj
->set_string ("function", fnname
);
418 if (loc
!= UNKNOWN_LOCATION
)
419 obj
->set ("inlining_chain", inlining_chain_to_json (loc
));
424 /* Add a json description of PASS and its siblings to ARR, recursing into
425 child passes (adding their descriptions within a "children" array). */
428 optrecord_json_writer::add_pass_list (json::array
*arr
, opt_pass
*pass
)
432 json::object
*pass_obj
= pass_to_json (pass
);
433 arr
->append (pass_obj
);
436 json::array
*sub
= new json::array ();
437 pass_obj
->set ("children", sub
);
438 add_pass_list (sub
, pass
->sub
);
449 /* Verify that we can build a JSON optimization record from dump_*
453 test_building_json_from_dump_calls ()
455 temp_dump_context
tmp (true, true, MSG_NOTE
);
456 dump_user_location_t loc
;
457 dump_printf_loc (MSG_NOTE
, loc
, "test of tree: ");
458 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, integer_zero_node
);
459 optinfo
*info
= tmp
.get_pending_optinfo ();
460 ASSERT_TRUE (info
!= NULL
);
461 ASSERT_EQ (info
->num_items (), 2);
463 optrecord_json_writer writer
;
464 json::object
*json_obj
= writer
.optinfo_to_json (info
);
465 ASSERT_TRUE (json_obj
!= NULL
);
467 /* Verify that the json is sane. */
469 json_obj
->print (&pp
);
470 const char *json_str
= pp_formatted_text (&pp
);
471 ASSERT_STR_CONTAINS (json_str
, "impl_location");
472 ASSERT_STR_CONTAINS (json_str
, "\"kind\": \"note\"");
473 ASSERT_STR_CONTAINS (json_str
,
474 "\"message\": [\"test of tree: \", {\"expr\": \"0\"}]");
478 /* Run all of the selftests within this file. */
481 optinfo_emit_json_cc_tests ()
483 test_building_json_from_dump_calls ();
486 } // namespace selftest
488 #endif /* CHECKING_P */