Skip to content
Snippets Groups Projects
  • Eugene Rozenfeld's avatar
    f1adf45b
    Add instruction level discriminator support. · f1adf45b
    Eugene Rozenfeld authored
    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.
    f1adf45b
    History
    Add instruction level discriminator support.
    Eugene Rozenfeld authored
    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.