mirror of
https://forge.sourceware.org/marek/gcc.git
synced 2026-02-21 19:35:36 -05:00
This patch adds a new key/value pair "cfgs={yes,no}" to diagnostics
sinks, "no" by default.
If set to "yes" for a SARIF sink, then GCC will add the internal state
of the CFG for all functions after each pertinent optimization pass in
graph form to theRun.graphs in the SARIF output.
If set to "yes" for an HTML sink, the generated HTML will contain SVG
displaying the graphs, adapted from code in graph.cc
Text sinks ignore it.
The SARIF output is thus a machine-readable serialization of (some of)
GCC's intermediate representation (as JSON), but it's much less than
GCC-XML used to provide. The precise form of the information is
documented as subject to change without notice.
Currently it shows both gimple statements and RTL instructions,
depending on the pass. My hope is that it should be possible to write a
"cfg-grep" tool that can read the SARIF and automatically identify
in which pass a particular piece of our IR appeared or disappeared,
for tracking down bugs in our optimization passes.
Implementation-wise:
* this uses the publish-subscribe mechanism from the earlier patch, by
having the diagnostics sink subscribe to pass_events::after_pass
messages from the pass_events_channel.
* the patch adds a new hook to cfghooks.h for dumping a basic block
into a SARIF property bag
gcc/ChangeLog:
* Makefile.in (OBJS): Add tree-diagnostic-cfg.o.
(OBJS-libcommon): Add custom-sarif-properties/cfg.o,
diagnostics/digraphs-to-dot.o, and
diagnostics/digraphs-to-dot-from-cfg.o.
* cfghooks.cc: Define INCLUDE_VECTOR. Add includes of
"diagnostics/sarif-sink.h" and "custom-sarif-properties/cfg.h".
(dump_bb_as_sarif_properties): New.
* cfghooks.h (diagnostics::sarif_builder): New forward decl.
(json::object): New forward decl.
(cfg_hooks::dump_bb_as_sarif_properties): New callback field.
(dump_bb_as_sarif_properties): New decl.
* cfgrtl.cc (rtl_cfg_hooks): Populate the new callback
field with rtl_dump_bb_as_sarif_properties.
(cfg_layout_rtl_cfg_hooks): Likewise.
* custom-sarif-properties/cfg.cc: New file.
* custom-sarif-properties/cfg.h: New file.
* diagnostics/digraphs-to-dot-from-cfg.cc: New file, partly
adapted from gcc/graph.cc.
* diagnostics/digraphs-to-dot.cc: New file.
* diagnostics/digraphs-to-dot.h: New file, based on material in...
* diagnostics/digraphs.cc: Include
"diagnostics/digraphs-to-dot.h".
(class conversion_to_dot): Rework and move to above.
(make_dot_graph_from_diagnostic_graph): Likewise.
(make_dot_node_from_digraph_node): Likewise.
(make_dot_edge_from_digraph_edge): Likewise.
(conversion_to_dot::get_dot_id_for_node): Likewise.
(conversion_to_dot::has_edges_p): Likewise.
(digraph::make_dot_graph): Use to_dot::converter::make and invoke
the result to make the dot graph.
* diagnostics/digraphs.h (digraph:get_all_nodes): New accessor.
* diagnostics/html-sink.cc
(html_builder::m_per_logical_loc_graphs): New field.
(html_builder::add_graph_for_logical_loc): New.
(html_sink::report_digraph_for_logical_location): New.
* diagnostics/sarif-sink.cc (sarif_array_of_unique::get_element):
New.
(sarif_builder::report_digraph_for_logical_location): New.
(sarif_sink::report_digraph_for_logical_location): New.
* diagnostics/sink.h: Include "diagnostics/logical-locations.h".
(sink::report_digraph_for_logical_location): New vfunc.
* diagnostics/text-sink.h
(text_sink::report_digraph_for_logical_location): New.
* doc/invoke.texi (fdiagnostics-add-output): Clarify wording.
Distinguish between scheme-specific vs GCC-specific keys, and add
"cfgs" as the first example of the latter.
* gimple-pretty-print.cc: Include "cfghooks.h", "json.h", and
"custom-sarif-properties/cfg.h".
(gimple_dump_bb_as_sarif_properties): New.
* gimple-pretty-print.h (diagnostics::sarif_builder): New forward
decl.
(json::object): Likewise.
(gimple_dump_bb_as_sarif_properties): New.
* graphviz.cc (get_compass_pt_from_string): New
* graphviz.h (get_compass_pt_from_string): New decl.
* libsarifreplay.cc (sarif_replayer::handle_graph_object): Fix
overlong line.
* opts-common.cc: Define INCLUDE_VECTOR.
* opts-diagnostic.cc: Define INCLUDE_LIST. Include
"diagnostics/sarif-sink.h", "tree-diagnostic-sink-extensions.h",
"opts-diagnostic.h", and "pub-sub.h".
(class gcc_extra_keys): New class.
(opt_spec_context::opt_spec_context): Add "client_keys" param and
pass to dc_spec_context.
(handle_gcc_specific_keys): New.
(try_to_make_sink): New.
(gcc_extension_factory::singleton): New.
(handle_OPT_fdiagnostics_add_output_): Rework to use
try_to_make_sink.
(handle_OPT_fdiagnostics_set_output_): Likewise.
* opts-diagnostic.h: Include "diagnostics/sink.h".
(class gcc_extension_factory): New.
* opts.cc: Define INCLUDE_LIST.
* print-rtl.cc: Include "dumpfile.h", "cfghooks.h", "json.h", and
"custom-sarif-properties/cfg.h".
(rtl_dump_bb_as_sarif_properties): New.
* print-rtl.h (diagnostics::sarif_builder): New forward decl.
(json::object): Likewise.
(rtl_dump_bb_as_sarif_properties): New decl.
* tree-cfg.cc (gimple_cfg_hooks): Use
gimple_dump_bb_as_sarif_properties for new callback field.
* tree-diagnostic-cfg.cc: New file, based on material in graph.cc.
* tree-diagnostic-sink-extensions.h: New file.
* tree-diagnostic.cc: Define INCLUDE_LIST. Include
"tree-diagnostic-sink-extensions.h".
(compiler_ext_factory): New.
(tree_diagnostics_defaults): Set gcc_extension_factory::singleton
to be compiler_ext_factory.
gcc/testsuite/ChangeLog:
* gcc.dg/diagnostic-cfgs-html.py: New test.
* gcc.dg/diagnostic-cfgs-sarif.py: New test.
* gcc.dg/diagnostic-cfgs.c: New test.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
391 lines
11 KiB
C++
391 lines
11 KiB
C++
/* Generating diagnostics graphs from GCC CFGs.
|
|
Copyright (C) 2025 Free Software Foundation, Inc.
|
|
Contributed by David Malcolm <dmalcolm@redhat.com>.
|
|
|
|
This file is part of GCC.
|
|
|
|
GCC is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
version.
|
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with GCC; see the file COPYING3. If not see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#define INCLUDE_LIST
|
|
#define INCLUDE_MAP
|
|
#define INCLUDE_STRING
|
|
#define INCLUDE_VECTOR
|
|
#include "config.h"
|
|
#include "system.h"
|
|
#include "coretypes.h"
|
|
#include "version.h"
|
|
#include "tree.h"
|
|
#include "lazily-created.h"
|
|
#include "diagnostics/digraphs.h"
|
|
#include "diagnostics/dumping.h"
|
|
#include "diagnostic.h"
|
|
#include "tree-pass.h"
|
|
#include "custom-sarif-properties/cfg.h"
|
|
#include "tree-diagnostic-sink-extensions.h"
|
|
#include "tree-logical-location.h"
|
|
#include "tree-pass.h"
|
|
#include "function.h"
|
|
#include "topics/pass-events.h"
|
|
#include "diagnostics/digraphs.h"
|
|
#include "diagnostics/sink.h"
|
|
#include "context.h"
|
|
#include "channels.h"
|
|
#include "bitmap.h"
|
|
#include "sbitmap.h"
|
|
#include "cfghooks.h"
|
|
#include "cfganal.h"
|
|
#include "cfgloop.h"
|
|
#include "graph.h"
|
|
#include "basic-block.h"
|
|
#include "cfg.h"
|
|
|
|
|
|
namespace {
|
|
namespace graph_properties = custom_sarif_properties::cfg::graph;
|
|
namespace node_properties = custom_sarif_properties::cfg::node;
|
|
namespace edge_properties = custom_sarif_properties::cfg::edge;
|
|
}
|
|
|
|
/* Disable warnings about quoting issues in the pp_xxx calls below
|
|
that (intentionally) don't follow GCC diagnostic conventions. */
|
|
#if __GNUC__ >= 10
|
|
# pragma GCC diagnostic push
|
|
# pragma GCC diagnostic ignored "-Wformat-diag"
|
|
#endif
|
|
|
|
class cfg_diagnostic_digraph
|
|
: public lazily_created<diagnostics::digraphs::digraph>
|
|
{
|
|
public:
|
|
cfg_diagnostic_digraph (function *fun,
|
|
opt_pass *pass)
|
|
: m_fun (fun), m_pass (pass)
|
|
{}
|
|
|
|
std::unique_ptr<diagnostics::digraphs::digraph>
|
|
create_object () const final override
|
|
{
|
|
auto g = std::make_unique<diagnostics::digraphs::digraph> ();
|
|
g->set_graph_kind ("cfg");
|
|
|
|
pretty_printer pp;
|
|
pp_printf (&pp, "%s: %s", function_name (m_fun), m_pass->name);
|
|
g->set_description (pp_formatted_text (&pp));
|
|
|
|
g->set_property (graph_properties::pass_name, m_pass->name);
|
|
g->set_property (graph_properties::pass_number, m_pass->static_pass_number);
|
|
|
|
add_cluster_for_function (*g, m_fun);
|
|
return g;
|
|
}
|
|
|
|
void
|
|
add_cluster_for_function (diagnostics::digraphs::digraph &g,
|
|
function *fun) const
|
|
{
|
|
const char *funcname = function_name (fun);
|
|
auto cluster = std::make_unique<diagnostics::digraphs::node> (g, funcname);
|
|
cluster->set_property (node_properties::kind, "function");
|
|
|
|
bb_to_node_map node_map;
|
|
add_cfg_nodes (g, node_map, *cluster, fun);
|
|
add_cfg_edges (g, node_map, fun);
|
|
g.add_node (std::move (cluster));
|
|
}
|
|
|
|
private:
|
|
typedef std::map<basic_block, diagnostics::digraphs::node *> bb_to_node_map;
|
|
|
|
/* Add a basic block BB belonging to the function with FUNCDEF_NO
|
|
as its unique number. */
|
|
void
|
|
add_cfg_node (diagnostics::digraphs::digraph &g,
|
|
bb_to_node_map &node_map,
|
|
diagnostics::digraphs::node &parent_node,
|
|
int funcdef_no,
|
|
basic_block bb) const
|
|
{
|
|
pretty_printer pp;
|
|
pp_printf (&pp, "fn_%d_basic_block_%d",
|
|
funcdef_no, bb->index);
|
|
std::string name (pp_formatted_text (&pp));
|
|
auto bb_node = std::make_unique<diagnostics::digraphs::node> (g, name);
|
|
node_map.insert ({bb, bb_node.get ()});
|
|
|
|
bb_node->set_property (node_properties::kind, "basic_block");
|
|
|
|
dump_bb_as_sarif_properties (nullptr,
|
|
bb_node->ensure_property_bag (),
|
|
bb);
|
|
|
|
parent_node.add_child (std::move (bb_node));
|
|
}
|
|
|
|
/* Add all successor edges of a basic block BB belonging to the function
|
|
with FUNCDEF_NO as its unique number. */
|
|
void
|
|
add_cfg_node_succ_edges (diagnostics::digraphs::digraph &g,
|
|
bb_to_node_map &node_map,
|
|
int /*funcdef_no*/,
|
|
basic_block bb) const
|
|
{
|
|
edge e;
|
|
edge_iterator ei;
|
|
FOR_EACH_EDGE (e, ei, bb->succs)
|
|
{
|
|
auto src_node = node_map.find (e->src);
|
|
gcc_assert (src_node != node_map.end ());
|
|
auto dst_node = node_map.find (e->dest);
|
|
gcc_assert (dst_node != node_map.end ());
|
|
|
|
auto diag_edge
|
|
= std::make_unique<diagnostics::digraphs::edge> (g, nullptr,
|
|
*(src_node->second),
|
|
*(dst_node->second));
|
|
auto flag_arr = std::make_unique<json::array> ();
|
|
#define DEF_EDGE_FLAG(NAME,IDX) \
|
|
{ handle_edge_flag (*flag_arr, #NAME, (e->flags & EDGE_##NAME)); }
|
|
#include "cfg-flags.def"
|
|
#undef DEF_EDGE_FLAG
|
|
|
|
auto &bag = diag_edge->ensure_property_bag ();
|
|
bag.set<json::array> (edge_properties::flags.m_key.get (),
|
|
std::move (flag_arr));
|
|
|
|
if (e->probability.initialized_p ())
|
|
diag_edge->set_property (edge_properties::probability_pc,
|
|
(e->probability.to_reg_br_prob_base ()
|
|
* 100 / REG_BR_PROB_BASE));
|
|
|
|
g.add_edge (std::move (diag_edge));
|
|
}
|
|
}
|
|
|
|
void
|
|
handle_edge_flag (json::array &flag_arr,
|
|
const char *flag_name,
|
|
bool value) const
|
|
{
|
|
if (value)
|
|
flag_arr.append_string (flag_name);
|
|
}
|
|
|
|
/* Add all the basic blocks in the CFG in case loops are not available.
|
|
First compute a topological order of the blocks to get a good ranking of
|
|
the nodes. Then, if any nodes are not reachable from ENTRY, add them at
|
|
the end. */
|
|
void
|
|
add_cfg_nodes_no_loops (diagnostics::digraphs::digraph &g,
|
|
bb_to_node_map &node_map,
|
|
diagnostics::digraphs::node &parent_node,
|
|
function *fun) const
|
|
{
|
|
int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
|
|
int i, n;
|
|
|
|
auto_sbitmap visited (last_basic_block_for_fn (fun));
|
|
bitmap_clear (visited);
|
|
|
|
n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, true);
|
|
for (i = n_basic_blocks_for_fn (fun) - n;
|
|
i < n_basic_blocks_for_fn (fun); i++)
|
|
{
|
|
basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
|
|
add_cfg_node (g, node_map, parent_node, fun->funcdef_no, bb);
|
|
bitmap_set_bit (visited, bb->index);
|
|
}
|
|
free (rpo);
|
|
|
|
if (n != n_basic_blocks_for_fn (fun))
|
|
{
|
|
/* Some blocks are unreachable. We still want to dump them. */
|
|
basic_block bb;
|
|
FOR_ALL_BB_FN (bb, fun)
|
|
if (! bitmap_bit_p (visited, bb->index))
|
|
add_cfg_node (g, node_map, parent_node, fun->funcdef_no, bb);
|
|
}
|
|
}
|
|
|
|
/* Add all the basic blocks in LOOP. Add the blocks in breath-first
|
|
order to get a good ranking of the nodes. This function is recursive:
|
|
It first adds inner loops, then the body of LOOP itself. */
|
|
|
|
void
|
|
add_cfg_nodes_for_loop (diagnostics::digraphs::digraph &g,
|
|
bb_to_node_map &node_map,
|
|
diagnostics::digraphs::node *parent_node,
|
|
int funcdef_no,
|
|
class loop *loop) const
|
|
{
|
|
namespace loop_properties = custom_sarif_properties::cfg::loop;
|
|
|
|
gcc_assert (parent_node);
|
|
diagnostics::digraphs::node &orig_parent_node = *parent_node;
|
|
|
|
unsigned int i;
|
|
std::unique_ptr<diagnostics::digraphs::node> loop_node;
|
|
|
|
if (loop->header != NULL
|
|
&& loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
|
|
{
|
|
pretty_printer pp;
|
|
pp_printf (&pp, "fun_%d_loop_%d", funcdef_no, loop->num);
|
|
std::string name (pp_formatted_text (&pp));
|
|
loop_node
|
|
= std::make_unique<diagnostics::digraphs::node> (g, name);
|
|
parent_node = loop_node.get ();
|
|
loop_node->set_property (node_properties::kind, "loop");
|
|
loop_node->set_property (loop_properties::num, loop->num);
|
|
loop_node->set_property (loop_properties::depth, loop_depth (loop));
|
|
}
|
|
|
|
for (class loop *inner = loop->inner; inner; inner = inner->next)
|
|
add_cfg_nodes_for_loop (g, node_map, parent_node, funcdef_no, inner);
|
|
|
|
if (loop->header == NULL)
|
|
return;
|
|
|
|
basic_block *body;
|
|
if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
|
|
body = get_loop_body (loop);
|
|
else
|
|
body = get_loop_body_in_bfs_order (loop);
|
|
|
|
for (i = 0; i < loop->num_nodes; i++)
|
|
{
|
|
basic_block bb = body[i];
|
|
if (bb->loop_father == loop)
|
|
add_cfg_node (g, node_map, *parent_node, funcdef_no, bb);
|
|
}
|
|
|
|
free (body);
|
|
|
|
if (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
|
|
{
|
|
gcc_assert (loop_node);
|
|
orig_parent_node.add_child (std::move (loop_node));
|
|
}
|
|
}
|
|
|
|
void
|
|
add_cfg_nodes (diagnostics::digraphs::digraph &g,
|
|
bb_to_node_map &node_map,
|
|
diagnostics::digraphs::node &parent_node,
|
|
function *fun) const
|
|
{
|
|
/* ??? The loop and dominance APIs are dependent on fun == cfun. */
|
|
if (fun == cfun && loops_for_fn (fun))
|
|
add_cfg_nodes_for_loop (g, node_map, &parent_node, fun->funcdef_no,
|
|
get_loop (fun, 0));
|
|
else
|
|
add_cfg_nodes_no_loops (g, node_map, parent_node, fun);
|
|
}
|
|
|
|
void
|
|
add_cfg_edges (diagnostics::digraphs::digraph &g,
|
|
bb_to_node_map &node_map,
|
|
function *fun) const
|
|
{
|
|
basic_block bb;
|
|
|
|
/* Save EDGE_DFS_BACK flag to dfs_back. */
|
|
auto_bitmap dfs_back;
|
|
edge e;
|
|
edge_iterator ei;
|
|
unsigned int idx = 0;
|
|
FOR_EACH_BB_FN (bb, fun)
|
|
FOR_EACH_EDGE (e, ei, bb->succs)
|
|
{
|
|
if (e->flags & EDGE_DFS_BACK)
|
|
bitmap_set_bit (dfs_back, idx);
|
|
idx++;
|
|
}
|
|
|
|
mark_dfs_back_edges (fun);
|
|
FOR_ALL_BB_FN (bb, fun)
|
|
add_cfg_node_succ_edges (g, node_map, fun->funcdef_no, bb);
|
|
|
|
/* Restore EDGE_DFS_BACK flag from dfs_back. */
|
|
idx = 0;
|
|
FOR_EACH_BB_FN (bb, fun)
|
|
FOR_EACH_EDGE (e, ei, bb->succs)
|
|
{
|
|
if (bitmap_bit_p (dfs_back, idx))
|
|
e->flags |= EDGE_DFS_BACK;
|
|
else
|
|
e->flags &= ~EDGE_DFS_BACK;
|
|
idx++;
|
|
}
|
|
}
|
|
|
|
function *m_fun;
|
|
opt_pass *m_pass;
|
|
};
|
|
|
|
#if __GNUC__ >= 10
|
|
# pragma GCC diagnostic pop
|
|
#endif
|
|
|
|
namespace pass_events = gcc::topics::pass_events;
|
|
|
|
/* A diagnostics::sink::extension which subscribes to pass_events
|
|
and responds to "after_pass" events by adding a diagnostics digraph
|
|
for the CFG for the relevant function. */
|
|
|
|
class compiler_capture_cfgs : public diagnostics::sink::extension
|
|
{
|
|
public:
|
|
compiler_capture_cfgs (diagnostics::sink &sink)
|
|
: extension (sink),
|
|
m_event_subscriber (sink)
|
|
{
|
|
g->get_channels ().pass_events_channel.add_subscriber (m_event_subscriber);
|
|
}
|
|
|
|
void
|
|
dump (FILE *out, int indent) const
|
|
{
|
|
diagnostics::dumping::emit_heading (out, indent, "compiler_capture_cfgs");
|
|
}
|
|
|
|
private:
|
|
class event_subscriber : public pass_events::subscriber
|
|
{
|
|
public:
|
|
event_subscriber (diagnostics::sink &sink) : m_sink (sink) {}
|
|
void on_message (const pass_events::before_pass &) final override
|
|
{
|
|
}
|
|
void on_message (const pass_events::after_pass &m) final override
|
|
{
|
|
if (m.fun
|
|
&& m.fun->cfg
|
|
&& m.pass->static_pass_number > 0)
|
|
m_sink.report_digraph_for_logical_location
|
|
(cfg_diagnostic_digraph (m.fun, m.pass),
|
|
tree_logical_location_manager::key_from_tree (m.fun->decl));
|
|
}
|
|
|
|
private:
|
|
diagnostics::sink &m_sink;
|
|
} m_event_subscriber;
|
|
};
|
|
|
|
std::unique_ptr<diagnostics::sink::extension>
|
|
compiler_extension_factory::make_cfg_extension (diagnostics::sink &sink) const
|
|
{
|
|
return std::make_unique<compiler_capture_cfgs> (sink);
|
|
}
|