This is the first in a series of patches to enable discriminator support
in AutoFDO.
This patch switches to tracking discriminators per statement/instruction
instead of per basic block. Tracking per basic block was problematic since
not all statements in a basic block needed a discriminator and, also, later
optimizations could move statements between basic blocks making correlation
during AutoFDO compilation unreliable. Tracking per statement also allows
us to assign different discriminators to multiple function calls in the same
basic block. A subsequent patch will add that support.
The idea of this patch is based on commit
4c311d95cf6d9519c3c20f641cc77af7df491fdf
by Dehao Chen in vendors/google/heads/gcc-4_8 but uses a slightly different
approach. In Dehao's work special (normally unused) location ids and side tables
were used to keep track of locations with discriminators. Things have changed
since then and I don't think we have unused location ids anymore. Instead,
I made discriminators a part of ad-hoc locations.
The difference from Dehao's work also includes support for discriminator
reading/writing in lto streaming and in modules.
Tested on x86_64-pc-linux-gnu.
gcc/ChangeLog:
* basic-block.h: Remove discriminator from basic blocks.
* cfghooks.cc (split_block_1): Remove discriminator from basic blocks.
* final.cc (final_start_function_1): Switch from per-bb to per statement
discriminator.
(final_scan_insn_1): Don't keep track of basic block discriminators.
(compute_discriminator): Switch from basic block discriminators to
instruction discriminators.
(insn_discriminator): New function to return instruction discriminator.
(notice_source_line): Use insn_discriminator.
* gimple-pretty-print.cc (dump_gimple_bb_header): Remove dumping of
basic block discriminators.
* gimple-streamer-in.cc (input_bb): Remove reading of basic block
discriminators.
* gimple-streamer-out.cc (output_bb): Remove writing of basic block
discriminators.
* input.cc (make_location): Pass 0 discriminator to COMBINE_LOCATION_DATA.
(location_with_discriminator): New function to combine locus with
a discriminator.
(has_discriminator): New function to check if a location has a discriminator.
(get_discriminator_from_loc): New function to get the discriminator
from a location.
* input.h: Declarations of new functions.
* lto-streamer-in.cc (cmp_loc): Use discriminators in location comparison.
(apply_location_cache): Keep track of current discriminator.
(input_location_and_block): Read discriminator from stream.
* lto-streamer-out.cc (clear_line_info): Set current discriminator to
UINT_MAX.
(lto_output_location_1): Write discriminator to stream.
* lto-streamer.h: Add discriminator to cached_location.
Add current_discr to lto_location_cache.
Add current_discr to output_block.
* print-rtl.cc (print_rtx_operand_code_i): Print discriminator.
* rtl.h: Add extern declaration of insn_discriminator.
* tree-cfg.cc (assign_discriminator): New function to assign a unique
discriminator value to all statements in a basic block that have the given
line number.
(assign_discriminators): Assign discriminators to statement locations.
* tree-pretty-print.cc (dump_location): Dump discriminators.
* tree.cc (set_block): Preserve discriminator when setting block.
(set_source_range): Preserve discriminator when setting source range.
gcc/cp/ChangeLog:
* module.cc (write_location): Write discriminator.
(read_location): Read discriminator.
libcpp/ChangeLog:
* include/line-map.h: Add discriminator to location_adhoc_data.
(get_combined_adhoc_loc): Add discriminator parameter.
(get_discriminator_from_adhoc_loc): Add external declaration.
(get_discriminator_from_loc): Add external declaration.
(COMBINE_LOCATION_DATA): Add discriminator parameter.
* lex.cc (get_location_for_byte_range_in_cur_line) Pass 0 discriminator
in a call to COMBINE_LOCATION_DATA.
(warn_about_normalization): Pass 0 discriminator in a call to
COMBINE_LOCATION_DATA.
(_cpp_lex_direct): Pass 0 discriminator in a call to
COMBINE_LOCATION_DATA.
* line-map.cc (location_adhoc_data_hash): Use discriminator compute
location_adhoc_data hash.
(location_adhoc_data_eq): Use discriminator when comparing
location_adhoc_data.
(can_be_stored_compactly_p): Check discriminator to determine
compact storage.
(get_combined_adhoc_loc): Add discriminator parameter.
(get_discriminator_from_adhoc_loc): New function to get the discriminator
from an ad-hoc location.
(get_discriminator_from_loc): New function to get the discriminator
from a location.
gcc/testsuite/ChangeLog:
* c-c++-common/ubsan/pr85213.c: Pass -gno-statement-frontiers.
/* Expected number of executions: calculated in profile.cc. */
profile_count count;
-
- /* The discriminator for this block. The discriminator distinguishes
- among several basic blocks that share a common locus, allowing for
- more accurate sample-based profiling. */
- int discriminator;
};
/* This ensures that struct gimple_bb_info is smaller than
return NULL;
new_bb->count = bb->count;
- new_bb->discriminator = bb->discriminator;
if (dom_info_available_p (CDI_DOMINATORS))
{
range.m_start = UNKNOWN_LOCATION;
write_location (sec, range.m_start);
write_location (sec, range.m_finish);
+ unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
+ sec.u (discriminator);
}
else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
{
if (range.m_start == UNKNOWN_LOCATION)
range.m_start = locus;
range.m_finish = read_location (sec);
+ unsigned discriminator = sec.u ();
if (locus != loc && range.m_start != loc && range.m_finish != loc)
- locus = get_combined_adhoc_loc (line_table, locus, range, NULL);
+ locus = get_combined_adhoc_loc (line_table, locus, range, NULL, discriminator);
}
break;
/* Discriminator written to assembly. */
static int last_discriminator;
-/* Discriminator to be written to assembly for current instruction.
+/* Compute discriminator to be written to assembly for current instruction.
Note: actual usage depends on loc_discriminator_kind setting. */
-static int discriminator;
static inline int compute_discriminator (location_t loc);
-/* Discriminator identifying current basic block among others sharing
- the same locus. */
-static int bb_discriminator;
-
-/* Basic block discriminator for previous instruction. */
-static int last_bb_discriminator;
-
/* Highest line number in current block. */
static int high_block_linenum;
last_filename = LOCATION_FILE (prologue_location);
last_linenum = LOCATION_LINE (prologue_location);
last_columnnum = LOCATION_COLUMN (prologue_location);
- last_discriminator = discriminator = 0;
- last_bb_discriminator = bb_discriminator = 0;
+ last_discriminator = 0;
force_source_line = false;
high_block_linenum = high_function_linenum = last_linenum;
if (targetm.asm_out.unwind_emit)
targetm.asm_out.unwind_emit (asm_out_file, insn);
- bb_discriminator = NOTE_BASIC_BLOCK (insn)->discriminator;
break;
case NOTE_INSN_EH_REGION_BEG:
int discriminator;
if (!decl_to_instance_map)
- discriminator = bb_discriminator;
+ discriminator = get_discriminator_from_loc (loc);
else
{
tree block = LOCATION_BLOCK (loc);
return discriminator;
}
+/* Return discriminator of the statement that produced this insn. */
+int
+insn_discriminator (const rtx_insn *insn)
+{
+ return compute_discriminator (INSN_LOCATION (insn));
+}
+
/* Return whether a source line note needs to be emitted before INSN.
Sets IS_STMT to TRUE if the line should be marked as a possible
breakpoint location. */
{
const char *filename;
int linenum, columnnum;
+ int discriminator;
if (NOTE_MARKER_P (insn))
{
filename = xloc.file;
linenum = xloc.line;
columnnum = xloc.column;
- discriminator = compute_discriminator (INSN_LOCATION (insn));
+ discriminator = insn_discriminator (insn);
}
else
{
indent, "", get_lineno (gsi_stmt (gsi)));
break;
}
- if (bb->discriminator)
- fprintf (outf, ", discriminator %i", bb->discriminator);
fputc ('\n', outf);
}
}
bb->count
= bb->count.apply_scale (count_materialization_scale, REG_BR_PROB_BASE);
bb->flags = streamer_read_hwi (ib);
- bb->discriminator = streamer_read_hwi (ib);
/* LTO_bb1 has statements. LTO_bb0 does not. */
if (tag == LTO_bb0)
streamer_write_uhwi (ob, bb->index);
bb->count.stream_out (ob);
streamer_write_hwi (ob, bb->flags);
- streamer_write_hwi (ob, bb->discriminator);
if (!gsi_end_p (bsi) || phi_nodes (bb))
{
location_t combined_loc = COMBINE_LOCATION_DATA (line_table,
pure_loc,
src_range,
- NULL);
+ NULL,
+ 0);
return combined_loc;
}
make_location (location_t caret, source_range src_range)
{
location_t pure_loc = get_pure_location (caret);
- return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, NULL);
+ return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, NULL, 0);
}
/* An expanded_location stores the column in byte units. This function
return NULL;
}
+/* Associate the DISCRIMINATOR with LOCUS, and return a new locus. */
+
+location_t
+location_with_discriminator (location_t locus, int discriminator)
+{
+ tree block = LOCATION_BLOCK (locus);
+ source_range src_range = get_range_from_loc (line_table, locus);
+ locus = get_pure_location (locus);
+
+ if (locus == UNKNOWN_LOCATION)
+ return locus;
+
+ return COMBINE_LOCATION_DATA (line_table, locus, src_range, block, discriminator);
+}
+
+/* Return TRUE if LOCUS represents a location with a discriminator. */
+
+bool
+has_discriminator (location_t locus)
+{
+ return get_discriminator_from_loc (locus) != 0;
+}
+
+/* Return the discriminator for LOCUS. */
+
+int
+get_discriminator_from_loc (location_t locus)
+{
+ return get_discriminator_from_loc (line_table, locus);
+}
+
#if CHECKING_P
namespace selftest {
extern location_t input_location;
+extern location_t location_with_discriminator (location_t, int);
+extern bool has_discriminator (location_t);
+extern int get_discriminator_from_loc (location_t);
+
#define LOCATION_FILE(LOC) ((expand_location (LOC)).file)
#define LOCATION_LINE(LOC) ((expand_location (LOC)).line)
#define LOCATION_COLUMN(LOC)((expand_location (LOC)).column)
return a->line - b->line;
if (a->col != b->col)
return a->col - b->col;
+ if (a->discr != b->discr)
+ return a->discr - b->discr;
if ((a->block == NULL_TREE) != (b->block == NULL_TREE))
return a->block ? 1 : -1;
if (a->block)
current_loc = linemap_position_for_column (line_table, loc.col);
if (loc.block)
current_loc = set_block (current_loc, loc.block);
+ if (loc.discr)
+ current_loc = location_with_discriminator (current_loc, loc.discr);
}
else if (current_block != loc.block)
{
current_loc = set_block (current_loc, loc.block);
else
current_loc = LOCATION_LOCUS (current_loc);
+ if (loc.discr)
+ current_loc = location_with_discriminator (current_loc, loc.discr);
}
+ else if (current_discr != loc.discr)
+ current_loc = location_with_discriminator (current_loc, loc.discr);
*loc.loc = current_loc;
current_line = loc.line;
prev_file = current_file = loc.file;
current_col = loc.col;
current_block = loc.block;
+ current_discr = loc.discr;
}
loc_cache.truncate (0);
accepted_length = 0;
static int stream_col;
static bool stream_sysp;
static tree stream_block;
+ static unsigned stream_discr;
static const char *stream_relative_path_prefix;
gcc_assert (current_cache == this);
*loc = RESERVED_LOCATION_COUNT;
bool line_change = bp_unpack_value (bp, 1);
bool column_change = bp_unpack_value (bp, 1);
+ bool discr_change = bp_unpack_value (bp, 1);
if (file_change)
{
if (column_change)
stream_col = bp_unpack_var_len_unsigned (bp);
+ if (discr_change)
+ stream_discr = bp_unpack_var_len_unsigned (bp);
+
tree block = NULL_TREE;
if (ib)
{
if (current_file == stream_file
&& current_line == stream_line
&& current_col == stream_col
- && current_sysp == stream_sysp)
+ && current_sysp == stream_sysp
+ && current_discr == stream_discr)
{
if (current_block == block)
*loc = current_loc;
}
struct cached_location entry
- = {stream_file, loc, stream_line, stream_col, stream_sysp, block};
+ = {stream_file, loc, stream_line, stream_col, stream_sysp, block, stream_discr};
loc_cache.safe_push (entry);
}
so that the first location with block in a function etc.
always streams a change_block bit and the first block. */
ob->current_block = void_node;
+ ob->current_discr = UINT_MAX;
}
if (loc >= RESERVED_LOCATION_COUNT)
{
expanded_location xloc = expand_location (loc);
+ unsigned discr = get_discriminator_from_loc (orig_loc);
if (ob->reset_locus)
{
bp_pack_value (bp, ob->current_line != xloc.line, 1);
bp_pack_value (bp, ob->current_col != xloc.column, 1);
+ bp_pack_value (bp, ob->current_discr != discr, 1);
if (ob->current_file != xloc.file)
{
if (ob->current_col != xloc.column)
bp_pack_var_len_unsigned (bp, xloc.column);
ob->current_col = xloc.column;
+
+ if (ob->current_discr != discr)
+ bp_pack_var_len_unsigned (bp, discr);
+ ob->current_discr = discr;
}
else
bp_pack_int_in_range (bp, 0, RESERVED_LOCATION_COUNT + 1, loc);
int line, col;
bool sysp;
tree block;
+ unsigned discr;
};
/* The location cache. */
bool current_sysp;
location_t current_loc;
tree current_block;
+ unsigned current_discr;
};
/* Structure used as buffer for reading an LTO file. */
bool reset_locus;
bool emit_pwd;
tree current_block;
+ unsigned current_discr;
/* Cache of nodes written in this section. */
struct streamer_tree_cache_d *writer_cache;
expanded_location xloc = insn_location (in_insn);
fprintf (m_outfile, " \"%s\":%i:%i", xloc.file, xloc.line,
xloc.column);
+ int discriminator = insn_discriminator (in_insn);
+ if (discriminator)
+ fprintf (m_outfile, " discrim %d", discriminator);
+
}
#endif
}
extern const char * insn_file (const rtx_insn *);
extern tree insn_scope (const rtx_insn *);
extern expanded_location insn_location (const rtx_insn *);
+extern int insn_discriminator (const rtx_insn *);
extern location_t prologue_location, epilogue_location;
/* In jump.cc */
/* PR sanitizer/85213 */
/* { dg-do compile } */
-/* { dg-options "-O1 -fsanitize=undefined -fcompare-debug" } */
+/* Pass -gno-statement-frontiers to work around
+ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100733 :
+ without it the IR coming from the front end may be different with and without
+ debug information turned on. That may cause e.g., different discriminator values
+ and -fcompare-debug failures. */
+/* { dg-options "-O1 -fsanitize=undefined -fcompare-debug -gno-statement-frontiers" } */
int
foo (int x)
&& filename_cmp (from->file, to.file) == 0);
}
-/* Assign discriminators to each basic block. */
+/* Assign a unique discriminator value to all statements in block bb that
+ have the same line number as locus. */
+
+static void
+assign_discriminator (location_t locus, basic_block bb)
+{
+ gimple_stmt_iterator gsi;
+ int discriminator;
+
+ if (locus == UNKNOWN_LOCATION)
+ return;
+
+ expanded_location locus_e = expand_location (locus);
+
+ discriminator = next_discriminator_for_locus (locus_e.line);
+
+ for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+ {
+ gimple *stmt = gsi_stmt (gsi);
+ location_t stmt_locus = gimple_location (stmt);
+ if (same_line_p (locus, &locus_e, stmt_locus))
+ gimple_set_location (stmt,
+ location_with_discriminator (stmt_locus, discriminator));
+ }
+}
+
+/* Assign discriminators to statement locations. */
static void
assign_discriminators (void)
{
gimple *first = first_non_label_stmt (e->dest);
gimple *last = last_stmt (e->dest);
- if ((first && same_line_p (locus, &locus_e,
+
+ gimple *stmt_on_same_line = NULL;
+ if (first && same_line_p (locus, &locus_e,
gimple_location (first)))
- || (last && same_line_p (locus, &locus_e,
- gimple_location (last))))
+ stmt_on_same_line = first;
+ else if (last && same_line_p (locus, &locus_e,
+ gimple_location (last)))
+ stmt_on_same_line = last;
+
+ if (stmt_on_same_line)
{
- if (e->dest->discriminator != 0 && bb->discriminator == 0)
- bb->discriminator
- = next_discriminator_for_locus (locus_e.line);
+ if (has_discriminator (gimple_location (stmt_on_same_line))
+ && !has_discriminator (locus))
+ assign_discriminator (locus, bb);
else
- e->dest->discriminator
- = next_discriminator_for_locus (locus_e.line);
+ assign_discriminator (locus, e->dest);
}
}
}
dump_location (pretty_printer *pp, location_t loc)
{
expanded_location xloc = expand_location (loc);
+ int discriminator = get_discriminator_from_loc (loc);
pp_left_bracket (pp);
if (xloc.file)
pp_decimal_int (pp, xloc.line);
pp_colon (pp);
pp_decimal_int (pp, xloc.column);
+ if (discriminator)
+ {
+ pp_string (pp, " discrim ");
+ pp_decimal_int (pp, discriminator);
+ }
pp_string (pp, "] ");
}
{
location_t pure_loc = get_pure_location (loc);
source_range src_range = get_range_from_loc (line_table, loc);
- return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block);
+ unsigned discriminator = get_discriminator_from_loc (line_table, loc);
+ return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block, discriminator);
}
location_t
if (!EXPR_P (expr))
return UNKNOWN_LOCATION;
- location_t pure_loc = get_pure_location (EXPR_LOCATION (expr));
+ location_t expr_location = EXPR_LOCATION (expr);
+ location_t pure_loc = get_pure_location (expr_location);
+ unsigned discriminator = get_discriminator_from_loc (expr_location);
location_t adhoc = COMBINE_LOCATION_DATA (line_table,
pure_loc,
src_range,
- NULL);
+ NULL,
+ discriminator);
SET_EXPR_LOCATION (expr, adhoc);
return adhoc;
}
location_t locus;
source_range src_range;
void * GTY((skip)) data;
+ unsigned discriminator;
};
struct htab;
}
extern location_t get_combined_adhoc_loc (line_maps *, location_t,
- source_range, void *);
+ source_range, void *, unsigned);
extern void *get_data_from_adhoc_loc (const line_maps *, location_t);
+extern unsigned get_discriminator_from_adhoc_loc (const line_maps *, location_t);
extern location_t get_location_from_adhoc_loc (const line_maps *,
location_t);
extern source_range get_range_from_loc (line_maps *set, location_t loc);
+extern unsigned get_discriminator_from_loc (line_maps *set, location_t loc);
/* Get whether location LOC is a "pure" location, or
whether it is an ad-hoc location, or embeds range information. */
COMBINE_LOCATION_DATA (class line_maps *set,
location_t loc,
source_range src_range,
- void *block)
+ void *block,
+ unsigned discriminator)
{
- return get_combined_adhoc_loc (set, loc, src_range, block);
+ return get_combined_adhoc_loc (set, loc, src_range, block, discriminator);
}
extern void rebuild_location_adhoc_htab (class line_maps *);
location_t combined_loc = COMBINE_LOCATION_DATA (pfile->line_table,
start_loc,
src_range,
- NULL);
+ NULL,
+ 0);
return combined_loc;
}
CPP_BUF_COLUMN (pfile->buffer,
pfile->buffer->cur));
loc = COMBINE_LOCATION_DATA (pfile->line_table,
- loc, tok_range, NULL);
+ loc, tok_range, NULL, 0);
}
encoding_rich_location rich_loc (pfile, loc);
result->src_loc = COMBINE_LOCATION_DATA (pfile->line_table,
result->src_loc,
- tok_range, NULL);
+ tok_range, NULL, 0);
}
return result;
return ((hashval_t) lb->locus
+ (hashval_t) lb->src_range.m_start
+ (hashval_t) lb->src_range.m_finish
- + (size_t) lb->data);
+ + (size_t) lb->data
+ + lb->discriminator);
}
/* Compare function for location_adhoc_data hashtable. */
return (lb1->locus == lb2->locus
&& lb1->src_range.m_start == lb2->src_range.m_start
&& lb1->src_range.m_finish == lb2->src_range.m_finish
- && lb1->data == lb2->data);
+ && lb1->data == lb2->data
+ && lb1->discriminator == lb2->discriminator);
}
/* Update the hashtable when location_adhoc_data_map::data is reallocated.
can_be_stored_compactly_p (line_maps *set,
location_t locus,
source_range src_range,
- void *data)
+ void *data,
+ unsigned discriminator)
{
/* If there's an ad-hoc pointer, we can't store it directly in the
location_t, we need the lookaside. */
if (data)
return false;
+ if (discriminator != 0)
+ return false;
+
/* We only store ranges that begin at the locus and that are sufficiently
"sane". */
if (src_range.m_start != locus)
get_combined_adhoc_loc (line_maps *set,
location_t locus,
source_range src_range,
- void *data)
+ void *data,
+ unsigned discriminator)
{
struct location_adhoc_data lb;
struct location_adhoc_data **slot;
|| pure_location_p (set, locus));
/* Consider short-range optimization. */
- if (can_be_stored_compactly_p (set, locus, src_range, data))
+ if (can_be_stored_compactly_p (set, locus, src_range, data, discriminator))
{
/* The low bits ought to be clear. */
linemap_assert (pure_location_p (set, locus));
when locus == start == finish (and data is NULL). */
if (locus == src_range.m_start
&& locus == src_range.m_finish
- && !data)
+ && !data && discriminator == 0)
return locus;
- if (!data)
+ if (!data && discriminator == 0)
set->num_unoptimized_ranges++;
lb.locus = locus;
lb.src_range = src_range;
lb.data = data;
+ lb.discriminator = discriminator;
slot = (struct location_adhoc_data **)
htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
if (*slot == NULL)
return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].data;
}
+unsigned
+get_discriminator_from_adhoc_loc (const class line_maps *set, location_t loc)
+{
+ linemap_assert (IS_ADHOC_LOC (loc));
+ return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].discriminator;
+}
+
/* Return the location for the adhoc loc. */
location_t
return source_range::from_location (loc);
}
+unsigned
+get_discriminator_from_loc (line_maps *set,
+ location_t loc)
+{
+ if (IS_ADHOC_LOC (loc))
+ return get_discriminator_from_adhoc_loc (set, loc);
+ return 0;
+}
+
/* Get whether location LOC is a "pure" location, or
whether it is an ad-hoc location, or embeds range information. */