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>
174 lines
5.8 KiB
C++
174 lines
5.8 KiB
C++
/* Print RTL for GCC.
|
|
Copyright (C) 1987-2026 Free Software Foundation, Inc.
|
|
|
|
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/>. */
|
|
|
|
#ifndef GCC_PRINT_RTL_H
|
|
#define GCC_PRINT_RTL_H
|
|
|
|
#ifndef GENERATOR_FILE
|
|
#include "bitmap.h"
|
|
#endif /* #ifndef GENERATOR_FILE */
|
|
|
|
namespace diagnostics { class sarif_builder; }
|
|
namespace json { class object; }
|
|
|
|
class rtx_reuse_manager;
|
|
|
|
/* A class for writing rtx to a FILE *. */
|
|
|
|
class rtx_writer
|
|
{
|
|
public:
|
|
rtx_writer (FILE *outfile, int ind, bool simple, bool compact,
|
|
rtx_reuse_manager *reuse_manager);
|
|
|
|
void print_rtx (const_rtx in_rtx);
|
|
void print_rtl (const_rtx rtx_first);
|
|
void print_rtl_single_with_indent (const_rtx x, int ind);
|
|
|
|
void finish_directive ();
|
|
|
|
private:
|
|
void print_rtx_operand_code_0 (const_rtx in_rtx, int idx);
|
|
void print_rtx_operand_code_e (const_rtx in_rtx, int idx);
|
|
void print_rtx_operand_codes_E_and_V (const_rtx in_rtx, int idx);
|
|
void print_rtx_operand_code_i (const_rtx in_rtx, int idx);
|
|
void print_rtx_operand_code_L (const_rtx in_rtx, int idx);
|
|
void print_rtx_operand_code_r (const_rtx in_rtx);
|
|
void print_rtx_operand_code_u (const_rtx in_rtx, int idx);
|
|
void print_rtx_operand (const_rtx in_rtx, int idx);
|
|
bool operand_has_default_value_p (const_rtx in_rtx, int idx);
|
|
|
|
private:
|
|
FILE *m_outfile;
|
|
int m_indent;
|
|
bool m_sawclose;
|
|
bool m_in_call_function_usage;
|
|
|
|
/* True means use simplified format without flags, modes, etc. */
|
|
bool m_simple;
|
|
|
|
/* If true, use compact dump format:
|
|
- PREV/NEXT_INSN UIDs are omitted
|
|
- INSN_CODEs are omitted,
|
|
- register numbers are omitted for hard and virtual regs, and
|
|
non-virtual pseudos are offset relative to the first such reg, and
|
|
printed with a '%' sigil e.g. "%0" for (LAST_VIRTUAL_REGISTER + 1),
|
|
- insn names are prefixed with "c" (e.g. "cinsn", "cnote", etc). */
|
|
bool m_compact;
|
|
|
|
#ifndef GENERATOR_FILE
|
|
/* An optional instance of rtx_reuse_manager. */
|
|
rtx_reuse_manager *m_rtx_reuse_manager;
|
|
#endif
|
|
};
|
|
|
|
#ifdef BUFSIZ
|
|
extern void print_rtl (FILE *, const_rtx);
|
|
#endif
|
|
extern void print_rtx_insn_vec (FILE *file, const vec<rtx_insn *> &vec);
|
|
|
|
extern void dump_value_slim (FILE *, const_rtx, int);
|
|
extern void dump_insn_slim (FILE *, const rtx_insn *);
|
|
extern void dump_rtl_slim (FILE *, const rtx_insn *, const rtx_insn *,
|
|
int, int);
|
|
extern void print_value (pretty_printer *, const_rtx, int);
|
|
extern void print_pattern (pretty_printer *, const_rtx, int);
|
|
extern void print_insn (pretty_printer *pp, const rtx_insn *x, int verbose);
|
|
extern void print_insn_with_notes (pretty_printer *, const rtx_insn *);
|
|
|
|
extern void rtl_dump_bb_for_graph (pretty_printer *, basic_block);
|
|
extern void
|
|
rtl_dump_bb_as_sarif_properties (diagnostics::sarif_builder *,
|
|
json::object &,
|
|
basic_block);
|
|
extern const char *str_pattern_slim (const_rtx);
|
|
|
|
extern void print_rtx_function (FILE *file, function *fn, bool compact);
|
|
|
|
#ifndef GENERATOR_FILE
|
|
|
|
/* For some rtx codes (such as SCRATCH), instances are defined to only be
|
|
equal for pointer equality: two distinct SCRATCH instances are non-equal.
|
|
copy_rtx preserves this equality by reusing the SCRATCH instance.
|
|
|
|
For example, in this x86 instruction:
|
|
|
|
(cinsn (set (mem/v:BLK (scratch:DI) [0 A8])
|
|
(unspec:BLK [
|
|
(mem/v:BLK (scratch:DI) [0 A8])
|
|
] UNSPEC_MEMORY_BLOCKAGE)) "test.c":2
|
|
(nil))
|
|
|
|
the two instances of "(scratch:DI)" are actually the same underlying
|
|
rtx pointer (and thus "equal"), and the insn will only be recognized
|
|
(as "*memory_blockage") if this pointer-equality is preserved.
|
|
|
|
To be able to preserve this pointer-equality when round-tripping
|
|
through dumping/loading the rtl, we need some syntax. The first
|
|
time a reused rtx is encountered in the dump, we prefix it with
|
|
a reuse ID:
|
|
|
|
(0|scratch:DI)
|
|
|
|
Subsequent references to the rtx in the dump can be expressed using
|
|
"reuse_rtx" e.g.:
|
|
|
|
(reuse_rtx 0)
|
|
|
|
This class is responsible for tracking a set of reuse IDs during a dump.
|
|
|
|
Dumping with reuse-support is done in two passes:
|
|
|
|
(a) a first pass in which "preprocess" is called on each top-level rtx
|
|
to be seen in the dump. This traverses the rtx and its descendents,
|
|
identifying rtx that will be seen more than once in the actual dump,
|
|
and assigning them reuse IDs.
|
|
|
|
(b) the actual dump, via print_rtx etc. print_rtx detect the presence
|
|
of a live rtx_reuse_manager and uses it if there is one. Any rtx
|
|
that were assigned reuse IDs will be printed with it the first time
|
|
that they are seen, and then printed as "(reuse_rtx ID)" subsequently.
|
|
|
|
The first phase is needed since otherwise there would be no way to tell
|
|
if an rtx will be reused when first encountering it. */
|
|
|
|
class rtx_reuse_manager
|
|
{
|
|
public:
|
|
rtx_reuse_manager ();
|
|
|
|
/* The first pass. */
|
|
void preprocess (const_rtx x);
|
|
|
|
/* The second pass (within print_rtx). */
|
|
bool has_reuse_id (const_rtx x, int *out);
|
|
bool seen_def_p (int reuse_id);
|
|
void set_seen_def (int reuse_id);
|
|
|
|
private:
|
|
hash_map<const_rtx, int> m_rtx_occurrence_count;
|
|
hash_map<const_rtx, int> m_rtx_reuse_ids;
|
|
auto_bitmap m_defs_seen;
|
|
int m_next_id;
|
|
};
|
|
|
|
#endif /* #ifndef GENERATOR_FILE */
|
|
|
|
#endif // GCC_PRINT_RTL_H
|