This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [google] Add options to pattern match function name for hotness attributes


On Fri, Jun 1, 2012 at 1:26 AM, Dehao Chen <dehao@google.com> wrote:
> Hi,
>
> This patch adds 4 flags to enable user to type in a list of name
> patterns. Compiler will match the function name with the given
> patterns, and add "hot", "cold", "likely_hot", "likely_cold"
> attributes to function declaration. The static branch prediction
> checks if a basic block contains call to a annotated function, and set
> the branch probability accordingly.

This is generally useful for cases where shared library code exhibits
different behavior among the applications  (so that source annotation
is not applicable).

You need to explain more on the likely_cold|hot attribute and the
intuition behind it.

>
> Bootstrapped and passed gcc testsuites.
>
> Ok for google branches?
>
> Thanks,
> Dehao
>
> gcc/ChangeLog.google-4_6
> 2012-06-01 ?Dehao Chen ?<dehao@google.com>
>
> ? ? ? ?* gcc/cgraph.c (cgraph_match_hotness_by_name): New function.
> ? ? ? ?(cgraph_add_hotness_attribute): New function.
> ? ? ? ?(cgraph_node): Add hotness attribute to function decl.
> ? ? ? ?* gcc/opts.c (common_handle_option): Handle new options.
> ? ? ? ?* gcc/predict.c (tree_bb_level_predictions): New prediction.
> ? ? ? ?* gcc/predict.def (PRED_LIKELY_COLD_FUNCTION): New prediction.
> ? ? ? ?* gcc/common.opt (flag_function_hot_list): New option.
> ? ? ? ?(flag_function_cold_list): New option.
> ? ? ? ?(flag_function_likely_hot_list): New option.
> ? ? ? ?(flag_function_likely_cold_list): New option.
>
> Index: gcc/doc/invoke.texi
> ===================================================================
> --- gcc/doc/invoke.texi (revision 188050)
> +++ gcc/doc/invoke.texi (working copy)
> @@ -362,7 +362,9 @@
> ?-fdelete-null-pointer-checks -fdse -fdevirtualize -fdse @gol
> ?-fearly-inlining -fipa-sra -fexpensive-optimizations -ffast-math @gol
> ?-ffinite-math-only -ffloat-store -fexcess-precision=@var{style} @gol
> --fforward-propagate -ffp-contract=@var{style} -ffunction-sections @gol
> +-fforward-propagate -ffp-contract=@var{style} @gol
> +-ffunction-cold-list -ffunction-hot-list -ffunction-likely-cold-list @gol
> +-ffunction-likely-hot-list -ffunction-sections @gol
> ?-fgcse -fgcse-after-reload -fgcse-las -fgcse-lm -fgraphite-identity @gol
> ?-fgcse-sm -fif-conversion -fif-conversion2 -findirect-inlining @gol
> ?-finline-functions -finline-functions-called-once -finline-limit=@var{n} @gol
> @@ -8585,6 +8587,22 @@
> ?specify this option and you may have problems with debugging if
> ?you specify both this option and @option{-g}.
>
> +@item -ffunction-cold-list
> +@opindex ffunction-cold-list
> +List of function name patterns that will be applied "cold" attribute.
> +
> +@item -ffunction-hot-list
> +@opindex ffunction-hot-list
> +List of function name patterns that will be applied "hot" attribute.
> +
> +@item -ffunction-likely-cold-list
> +@opindex ffunction-likely-cold-list
> +List of function name patterns that will be applied "likely_cold" attribute.
> +
> +@item -ffunction-likely-hot-list
> +@opindex ffunction-likely-hot-list
> +List of function name patterns that will be applied "likely_hot" attribute.
> +
> ?@item -fbranch-target-load-optimize
> ?@opindex fbranch-target-load-optimize
> ?Perform branch target register load optimization before prologue / epilogue
> Index: gcc/cgraph.c
> ===================================================================
> --- gcc/cgraph.c ? ? ? ?(revision 188050)
> +++ gcc/cgraph.c ? ? ? ?(working copy)
> @@ -520,6 +520,70 @@
> ? ? }
> ?}
>
> +typedef char *char_pointer;
> +DEF_VEC_P(char_pointer);
> +DEF_VEC_ALLOC_P(char_pointer,heap);
> +
> +/* Match FNDECL's name with PATTERNS. If matched, add HOTNESS attribute
> + ? to FNDECL.
> + ? name matches with pattern, iff one of the following conditions satisfy:
> + ? ? 1. strcmp (name, pattern) != 0


strcmp(name, pattern) == 0


> + ? ? 2. pattern[len - 1] = '*' && strncmp (name, pattern, len - 1) != 0 ?*/
> +static bool

strncmp (name, pattern, len -1 ) == 0

> +cgraph_match_hotness_by_name (tree fndecl,
> + ? ? ? ? ? ? ? ? ? ? ? ? ? ? VEC(char_pointer, heap) *patterns,
> + ? ? ? ? ? ? ? ? ? ? ? ? ? ? const char *hotness)
> +{
> + ?unsigned i;
> + ?char_pointer n;
> + ?const char *name = lang_hooks.decl_printable_name(fndecl, 0);
> +
> + ?if (!name)
> + ? ?return false;
> +
> + ?FOR_EACH_VEC_ELT (char_pointer, patterns, i, n)
> + ? ?{
> + ? ? ?int len = strlen (n);
> + ? ? ?if ((n[len - 1] == '*' && !strncmp (name, n, len - 1))
> + ? ? ? ? ?|| !strcmp (name, n))
> + ? ? ? ?{
> + ? ? ? ? ?decl_attributes (&fndecl, tree_cons (
> + ? ? ? ? ? ? ?get_identifier (hotness), NULL, NULL), 0);
> + ? ? ? ? ?return true;
> + ? ? ? ?}
> + ? ?}
> + ?return false;
> +}
> +
> +/* Add hotness attributes to FNDECL. ?*/

Empty line after.

> +static void
> +cgraph_add_hotness_attribute (tree fndecl)
> +{
> + ?if (lookup_attribute ("cold", DECL_ATTRIBUTES (fndecl))
> + ? ? ?|| lookup_attribute ("hot", DECL_ATTRIBUTES (fndecl))
> + ? ? ?|| lookup_attribute ("likely_cold", DECL_ATTRIBUTES (fndecl))
> + ? ? ?|| lookup_attribute ("likely_hot", DECL_ATTRIBUTES (fndecl)))
> + ? ?return;
> +
> + ?if (cgraph_match_hotness_by_name (fndecl,
> + ? ? ? (VEC(char_pointer, heap) *) flag_function_cold_list, "cold"))
> + ? ?return;
> +
> + ?if (cgraph_match_hotness_by_name (fndecl,
> + ? ? ? (VEC(char_pointer, heap) *) flag_function_hot_list, "hot"))
> + ? ?return;
> +
> + ?if (cgraph_match_hotness_by_name (fndecl,
> + ? ? ? (VEC(char_pointer, heap) *) flag_function_likely_cold_list,
> + ? ? ? "likely_cold"))
> + ? ?return;
> +
> + ?if (cgraph_match_hotness_by_name (fndecl,
> + ? ? ? (VEC(char_pointer, heap) *) flag_function_likely_hot_list,
> + ? ? ? "likely_hot"))
> + ? ?return;
> +}
> +
> ?/* Return cgraph node assigned to DECL. ?Create new one when needed. ?*/
>
> ?struct cgraph_node *
> @@ -554,6 +618,7 @@
> ? ? ? node->origin->nested = node;
> ? ? }
> ? cgraph_add_assembler_hash_node (node);
> + ?cgraph_add_hotness_attribute (decl);
> ? return node;
> ?}
>
> Index: gcc/opts.c
> ===================================================================
> --- gcc/opts.c ?(revision 188050)
> +++ gcc/opts.c ?(working copy)
> @@ -1539,6 +1539,26 @@
> ? ? ? ?(&opts->x_flag_instrument_functions_exclude_files, arg);
> ? ? ? break;
>
> + ? ?case OPT_ffunction_hot_list_:
> + ? ? ?add_comma_separated_to_vector
> + ? ? ? (&opts->x_flag_function_hot_list, arg);
> + ? ? ?break;
> +
> + ? ?case OPT_ffunction_cold_list_:
> + ? ? ?add_comma_separated_to_vector
> + ? ? ? (&opts->x_flag_function_cold_list, arg);
> + ? ? ?break;
> +
> + ? ?case OPT_ffunction_likely_hot_list_:
> + ? ? ?add_comma_separated_to_vector
> + ? ? ? (&opts->x_flag_function_likely_hot_list, arg);
> + ? ? ?break;
> +
> + ? ?case OPT_ffunction_likely_cold_list_:
> + ? ? ?add_comma_separated_to_vector
> + ? ? ? (&opts->x_flag_function_likely_cold_list, arg);
> + ? ? ?break;
> +
> ? ? case OPT_fmessage_length_:
> ? ? ? pp_set_line_maximum_length (dc->printer, value);
> ? ? ? break;

thanks,

David


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]