From patchwork Fri Aug 20 17:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1520242 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=DxL7VLxO; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=IRC6kbZ9; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gv6vf5SkSz9sXM for ; Tue, 24 Aug 2021 21:53:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 900FF3858409 for ; Tue, 24 Aug 2021 11:53:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id C2E913858416 for ; Tue, 24 Aug 2021 11:52:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2E913858416 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id CE838220ED for ; Tue, 24 Aug 2021 11:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1629805977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=FJXIAuEM0GM6Z5ho5bItxQsORjM/iBYA7e28Gu+o8Xs=; b=DxL7VLxOWeNcrHhoqsrEgjDGkgBg3mXUgwLVmwr/bk7VOMl8o5RC/mogg9D8gH3UlrdPOo 66oe6FdL0mB9QFwLIEajd+wbDjRlkzjrwHGPLlAHUBLaGNvi6x555xZLqahhZ4SRFs3uey gwHNgBUBsX+B05jqu8JI79n+onDAmlQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1629805977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=FJXIAuEM0GM6Z5ho5bItxQsORjM/iBYA7e28Gu+o8Xs=; b=IRC6kbZ9qis+YgZqcr+AKPl69GlGBkXgh77xlC9CTuimqW8+EeJeDD0NVvQzgV8XhYkJsb kPNYVW4JiWVw9qBg== Received: from suse.cz (virgil.suse.cz [10.100.13.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id BC7B8A3BC0 for ; Tue, 24 Aug 2021 11:52:57 +0000 (UTC) Resent-From: Martin Jambor Resent-Date: Tue, 24 Aug 2021 13:52:57 +0200 Resent-Message-ID: Resent-To: GCC Patches Message-Id: <96903a9151b41b623dc0103942b0db3f12ab50e6.1629805719.git.mjambor@suse.cz> In-Reply-To: References: From: Martin Jambor Date: Fri, 20 Aug 2021 19:43:54 +0200 Subject: [PATCH 1/4] cgraph: Do not warn about caller count mismatches of removed functions To: GCC Patches X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jan Hubicka , Xionghu Luo Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" To verify other changes in the patch series, I have been searching for "Invalid sum of caller counts" string in symtab dump but found that there are false warnings about functions which have their body removed because they are now unreachable. Those are of course invalid and so this patches avoids checking such cgraph_nodes. gcc/ChangeLog: 2021-08-20 Martin Jambor * cgraph.c (cgraph_node::dump): Do not check caller count sums if the body has been removed. Remove trailing whitespace. --- gcc/cgraph.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gcc/cgraph.c b/gcc/cgraph.c index 8f3af003f2a..de078653781 100644 --- a/gcc/cgraph.c +++ b/gcc/cgraph.c @@ -2236,7 +2236,7 @@ cgraph_node::dump (FILE *f) } fprintf (f, "\n"); - if (count.ipa ().initialized_p ()) + if (!body_removed && count.ipa ().initialized_p ()) { bool ok = true; bool min = false; @@ -2245,7 +2245,7 @@ cgraph_node::dump (FILE *f) FOR_EACH_ALIAS (this, ref) if (dyn_cast (ref->referring)->count.initialized_p ()) sum += dyn_cast (ref->referring)->count.ipa (); - + if (inlined_to || (symtab->state < EXPANSION && ultimate_alias_target () == this && only_called_directly_p ())) From patchwork Fri Aug 20 17:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1520247 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=W653D/1H; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=zrnS+MaK; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gv6xb1qtVz9sSs for ; Tue, 24 Aug 2021 21:55:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12BAE385AC28 for ; Tue, 24 Aug 2021 11:55:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 1C6853858422 for ; Tue, 24 Aug 2021 11:52:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1C6853858422 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id F07E3220F1 for ; Tue, 24 Aug 2021 11:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1629805977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=aVcP1sht5BVcwyy7oxfzZlNJeJ0/Nd4t88eduNDgyCc=; b=W653D/1HfKD2kEiZozAEV2wQZEE6AL1r6orhhgAvMTthYVk+XzB0p9FSC6/Jjq9wGa8Fwr unTSHTxZRCbP8LKU+aHe0W/U+zV2JCECIJvCTVee9LSJHkhZmriiteZM5DGX42n3Yo/qH/ B6O5q/HsBSAYtJr4IYoV1yJ5o/sCbus= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1629805977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=aVcP1sht5BVcwyy7oxfzZlNJeJ0/Nd4t88eduNDgyCc=; b=zrnS+MaK6KUwi7Yb3dBwnsL/R8fs7tRDutvy526cGrqge05mzyT5sj1ZqjveOR40F/poER u+tJtJgs4o7I3iBA== Received: from suse.cz (virgil.suse.cz [10.100.13.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id DDAF2A3BC0 for ; Tue, 24 Aug 2021 11:52:57 +0000 (UTC) Resent-From: Martin Jambor Resent-Date: Tue, 24 Aug 2021 13:52:57 +0200 Resent-Message-ID: Resent-To: GCC Patches Message-Id: <3068c6c4ee451244031d8198d663de6e614f28f9.1629805719.git.mjambor@suse.cz> In-Reply-To: References: From: Martin Jambor Date: Fri, 20 Aug 2021 19:43:54 +0200 Subject: [PATCH 2/4] ipa-cp: Propagation boost for recursion generated values To: GCC Patches X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jan Hubicka , Xionghu Luo Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Recursive call graph edges, even when they are hot and important for the compiled program, can never have frequency bigger than one, even when the actual time savings in the next recursion call are not realized just once but depend on the depth of recursion. The current IPA-CP effect propagation code did not take that into account and just used the frequency, thus severely underestimating the effect. This patch artificially boosts values taking part in such calls. If a value feeds into itself through a recursive call, the frequency of the edge is multiplied by a parameter with default value of 6, basically assuming that the recursion will take place 6 times. This value can of course be subject to change. Moreover, values which do not feed into themselves but which were generated for a self-recursive call with an arithmetic pass-function (aka the 548.exchange "hack" which however is generally applicable for recursive functions which count the recursion depth in a parameter) have the edge frequency multiplied as many times as there are generated values in the chain. In essence, we will assume they are all useful. This patch partially fixes the current situation when we fail to optimize 548.exchange with PGO. In the benchmark one recursive edge count overwhelmingly dominates all other counts in the program and so we fail to perform the first cloning (for the nonrecursive entry call) because it looks totally insignificant. gcc/ChangeLog: 2021-07-16 Martin Jambor * params.opt (ipa-cp-recursive-freq-factor): New. * ipa-cp.c (ipcp_value): Switch to inline initialization. New members scc_no, self_recursion_generated_level, same_scc and self_recursion_generated_p. (ipcp_lattice::add_value): Replaced parameter unlimited with same_lat_gen_level, usit it determine limit of values and store it to the value. (ipcp_lattice::print): Dump the new fileds. (allocate_and_init_ipcp_value): Take same_lat_gen_level as a new parameter and store it to the new value. (self_recursively_generated_p): Removed. (propagate_vals_across_arith_jfunc): Use self_recursion_generated_p instead of self_recursively_generated_p, store self generation level to such values. (value_topo_info::add_val): Set scc_no. (value_topo_info::propagate_effects): Multiply frequencies of recursively feeding values and self generated values by appropriate new factors. --- gcc/ipa-cp.c | 161 ++++++++++++++++++++++++------------------------- gcc/params.opt | 4 ++ 2 files changed, 84 insertions(+), 81 deletions(-) diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c index 55b9216337f..b987d975793 100644 --- a/gcc/ipa-cp.c +++ b/gcc/ipa-cp.c @@ -184,30 +184,52 @@ public: /* The actual value for the given parameter. */ valtype value; /* The list of sources from which this value originates. */ - ipcp_value_source *sources; + ipcp_value_source *sources = nullptr; /* Next pointers in a linked list of all values in a lattice. */ - ipcp_value *next; + ipcp_value *next = nullptr; /* Next pointers in a linked list of values in a strongly connected component of values. */ - ipcp_value *scc_next; + ipcp_value *scc_next = nullptr; /* Next pointers in a linked list of SCCs of values sorted topologically according their sources. */ - ipcp_value *topo_next; + ipcp_value *topo_next = nullptr; /* A specialized node created for this value, NULL if none has been (so far) created. */ - cgraph_node *spec_node; + cgraph_node *spec_node = nullptr; /* Depth first search number and low link for topological sorting of values. */ - int dfs, low_link; + int dfs = 0; + int low_link = 0; + /* SCC number to identify values which recursively feed into each other. + Values in the same SCC have the same SCC number. */ + int scc_no = 0; + /* Non zero if the value is generated from another value in the same lattice + for a self-recursive call, the actual number is how many times the + operation has been performed. In the unlikely event of the value being + present in two chains fo self-recursive value generation chains, it is the + maximum. */ + unsigned self_recursion_generated_level = 0; /* True if this value is currently on the topo-sort stack. */ - bool on_stack; - - ipcp_value() - : sources (0), next (0), scc_next (0), topo_next (0), - spec_node (0), dfs (0), low_link (0), on_stack (false) {} + bool on_stack = false; void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset); + + /* Return true if both THIS value and O feed into each other. */ + + bool same_scc (const ipcp_value *o) + { + return o->scc_no == scc_no; + } + +/* Return true, if a this value has been generated for a self-recursive call as + a result of an arithmetic pass-through jump-function acting on a value in + the same lattice function. */ + + bool self_recursion_generated_p () + { + return self_recursion_generated_level > 0; + } }; /* Lattice describing potential values of a formal parameter of a function, or @@ -239,7 +261,7 @@ public: ipcp_value *src_val = NULL, int src_idx = 0, HOST_WIDE_INT offset = -1, ipcp_value **val_p = NULL, - bool unlimited = false); + unsigned same_lat_gen_level = 0); void print (FILE * f, bool dump_sources, bool dump_benefits); }; @@ -498,7 +520,11 @@ ipcp_lattice::print (FILE * f, bool dump_sources, bool dump_benefits) { ipcp_value_source *s; - fprintf (f, " [from:"); + if (val->self_recursion_generated_p ()) + fprintf (f, " [self_gen(%i), from:", + val->self_recursion_generated_level); + else + fprintf (f, " [scc: %i, from:", val->scc_no); for (s = val->sources; s; s = s->next) fprintf (f, " %i(%f)", s->cs->caller->order, s->cs->sreal_frequency ().to_double ()); @@ -1837,12 +1863,13 @@ ipcp_value::add_source (cgraph_edge *cs, ipcp_value *src_val, SOURCE and clear all other fields. */ static ipcp_value * -allocate_and_init_ipcp_value (tree source) +allocate_and_init_ipcp_value (tree cst, unsigned same_lat_gen_level) { ipcp_value *val; val = new (ipcp_cst_values_pool.allocate ()) ipcp_value(); - val->value = source; + val->value = cst; + val->self_recursion_generated_level = same_lat_gen_level; return val; } @@ -1850,14 +1877,15 @@ allocate_and_init_ipcp_value (tree source) value to SOURCE and clear all other fields. */ static ipcp_value * -allocate_and_init_ipcp_value (ipa_polymorphic_call_context source) +allocate_and_init_ipcp_value (ipa_polymorphic_call_context ctx, + unsigned same_lat_gen_level) { ipcp_value *val; - // TODO val = new (ipcp_poly_ctx_values_pool.allocate ()) ipcp_value(); - val->value = source; + val->value = ctx; + val->self_recursion_generated_level = same_lat_gen_level; return val; } @@ -1865,8 +1893,12 @@ allocate_and_init_ipcp_value (ipa_polymorphic_call_context source) SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same meaning. OFFSET -1 means the source is scalar and not a part of an aggregate. If non-NULL, VAL_P records address of existing or newly added - ipcp_value. UNLIMITED means whether value count should not exceed the limit - given by PARAM_IPA_CP_VALUE_LIST_SIZE. */ + ipcp_value. + + If the value is generated for a self-recursive call as a result of an + arithmetic pass-through jump-function acting on a value in the same lattice, + SAME_LAT_GEN_LEVEL must be the length of such chain, otherwise it must be + zero. If it is non-zero, PARAM_IPA_CP_VALUE_LIST_SIZE limit is ignored. */ template bool @@ -1874,7 +1906,7 @@ ipcp_lattice::add_value (valtype newval, cgraph_edge *cs, ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset, ipcp_value **val_p, - bool unlimited) + unsigned same_lat_gen_level) { ipcp_value *val, *last_val = NULL; @@ -1890,6 +1922,9 @@ ipcp_lattice::add_value (valtype newval, cgraph_edge *cs, if (val_p) *val_p = val; + if (val->self_recursion_generated_level < same_lat_gen_level) + val->self_recursion_generated_level = same_lat_gen_level; + if (ipa_edge_within_scc (cs)) { ipcp_value_source *s; @@ -1904,7 +1939,7 @@ ipcp_lattice::add_value (valtype newval, cgraph_edge *cs, return false; } - if (!unlimited && values_count == opt_for_fn (cs->caller->decl, + if (!same_lat_gen_level && values_count == opt_for_fn (cs->caller->decl, param_ipa_cp_value_list_size)) { /* We can only free sources, not the values themselves, because sources @@ -1923,7 +1958,7 @@ ipcp_lattice::add_value (valtype newval, cgraph_edge *cs, } values_count++; - val = allocate_and_init_ipcp_value (newval); + val = allocate_and_init_ipcp_value (newval, same_lat_gen_level); val->add_source (cs, src_val, src_idx, offset); val->next = NULL; @@ -1940,60 +1975,6 @@ ipcp_lattice::add_value (valtype newval, cgraph_edge *cs, return true; } -/* Return true, if a ipcp_value VAL is orginated from parameter value of - self-feeding recursive function via some kind of pass-through jump - function. */ - -static bool -self_recursively_generated_p (ipcp_value *val) -{ - class ipa_node_params *info = NULL; - - for (ipcp_value_source *src = val->sources; src; src = src->next) - { - cgraph_edge *cs = src->cs; - - if (!src->val || cs->caller != cs->callee->function_symbol ()) - return false; - - if (src->val == val) - continue; - - if (!info) - info = ipa_node_params_sum->get (cs->caller); - - class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, - src->index); - ipcp_lattice *src_lat; - ipcp_value *src_val; - - if (src->offset == -1) - src_lat = &plats->itself; - else - { - struct ipcp_agg_lattice *src_aglat; - - for (src_aglat = plats->aggs; src_aglat; src_aglat = src_aglat->next) - if (src_aglat->offset == src->offset) - break; - - if (!src_aglat) - return false; - - src_lat = src_aglat; - } - - for (src_val = src_lat->values; src_val; src_val = src_val->next) - if (src_val == val) - break; - - if (!src_val) - return false; - } - - return true; -} - /* A helper function that returns result of operation specified by OPCODE on the value of SRC_VAL. If non-NULL, OPND1_TYPE is expected type for the value of SRC_VAL. If the operation is binary, OPND2 is a constant value @@ -2068,7 +2049,7 @@ propagate_vals_across_arith_jfunc (cgraph_edge *cs, source, this is absolutely conservative, but could avoid explosion of lattice's value space, especially when one recursive function calls another recursive. */ - if (self_recursively_generated_p (src_val)) + if (src_val->self_recursion_generated_p ()) { ipcp_value_source *s; @@ -2096,7 +2077,7 @@ propagate_vals_across_arith_jfunc (cgraph_edge *cs, break; ret |= dest_lat->add_value (cstval, cs, src_val, src_idx, - src_offset, &src_val, true); + src_offset, &src_val, j); gcc_checking_assert (src_val); } } @@ -2108,7 +2089,7 @@ propagate_vals_across_arith_jfunc (cgraph_edge *cs, /* Now we do not use self-recursively generated value as propagation source, otherwise it is easy to make value space of normal lattice overflow. */ - if (self_recursively_generated_p (src_val)) + if (src_val->self_recursion_generated_p ()) { ret |= dest_lat->set_contains_variable (); continue; @@ -3732,6 +3713,7 @@ value_topo_info::add_val (ipcp_value *cur_val) v = stack; stack = v->topo_next; v->on_stack = false; + v->scc_no = cur_val->dfs; v->scc_next = scc_list; scc_list = v; @@ -3905,8 +3887,25 @@ value_topo_info::propagate_effects () else continue; } + + int special_factor = 1; + if (val->same_scc (src->val)) + special_factor + = opt_for_fn(src->cs->caller->decl, + param_ipa_cp_recursive_freq_factor); + else if (val->self_recursion_generated_p () + && (src->cs->callee->function_symbol () + == src->cs->caller)) + { + int max_recur_gen_depth + = opt_for_fn(src->cs->caller->decl, + param_ipa_cp_max_recursive_depth); + special_factor = max_recur_gen_depth + - val->self_recursion_generated_level + 1; + } + src->val->prop_time_benefit - += time * src->cs->sreal_frequency (); + += time * special_factor * src->cs->sreal_frequency (); } if (size < INT_MAX) diff --git a/gcc/params.opt b/gcc/params.opt index 92b003e38cb..8d772309407 100644 --- a/gcc/params.opt +++ b/gcc/params.opt @@ -266,6 +266,10 @@ Maximum depth of recursive cloning for self-recursive function. Common Joined UInteger Var(param_ipa_cp_min_recursive_probability) Init(2) Param Optimization Recursive cloning only when the probability of call being executed exceeds the parameter. +-param=ipa-cp-recursive-freq-factor= +Common Joined UInteger Var(param_ipa_cp_recursive_freq_factor) Init(6) Param Optimization +When propagating IPA-CP effect estimates, multiply frequencies of recursive edges that that bring back an unchanged value by this factor. + -param=ipa-cp-recursion-penalty= Common Joined UInteger Var(param_ipa_cp_recursion_penalty) Init(40) IntegerRange(0, 100) Param Optimization Percentage penalty the recursive functions will receive when they are evaluated for cloning. From patchwork Fri Aug 20 17:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1520244 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=tCkO09RM; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=bqVwWoMV; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gv6wp0bqJz9sSs for ; Tue, 24 Aug 2021 21:54:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E888B3857C5F for ; Tue, 24 Aug 2021 11:54:47 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 183FF3858421 for ; Tue, 24 Aug 2021 11:52:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 183FF3858421 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 214431FD86 for ; Tue, 24 Aug 2021 11:52:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1629805978; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=S6GvEEv805ozA1pXiP3zbEIJL0PWc4QNk94rVvz2vhQ=; b=tCkO09RMKtSE3NLaEvoL8bJ1dbOCqIkISlTEHQ+Z+RAlBZzGnm05krC/lnvobSej8ITe2I zzBtouxJWlLAl5PvExpRIyohi0T9ynauhog6vhHCOSHSBh11Tn8cn6Clro6AQlehdoyoON iU5mcVn4NhA6aiOoXvp93Dp30x3Lf5o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1629805978; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=S6GvEEv805ozA1pXiP3zbEIJL0PWc4QNk94rVvz2vhQ=; b=bqVwWoMV+PGQtfM9gJIwSj0lIB12OmUiQuVVMW95qoWv2IDdOGoqxN5HBAODHBBkRXdteJ Ofw24c2zMCoSJ9Aw== Received: from suse.cz (virgil.suse.cz [10.100.13.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 0D9CAA3BC0 for ; Tue, 24 Aug 2021 11:52:58 +0000 (UTC) Resent-From: Martin Jambor Resent-Date: Tue, 24 Aug 2021 13:52:57 +0200 Resent-Message-ID: Resent-To: GCC Patches Message-Id: <055af750e7a6bd722e55c7046b8b9a38eefa4986.1629805719.git.mjambor@suse.cz> In-Reply-To: References: From: Martin Jambor Date: Fri, 20 Aug 2021 19:43:54 +0200 Subject: [PATCH 3/4] ipa-cp: Fix updating of profile counts and self-gen value evaluation To: GCC Patches X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jan Hubicka , Xionghu Luo Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" IPA-CP does not do a reasonable job when it is updating profile counts after it has created clones of recursive functions. This patch addresses that by: 1. Only updating counts for special-context clones. When a clone is created for all contexts, the original is going to be dead and the cgraph machinery has copied counts to the new node which is the right thing to do. Therefore updating counts has been moved from create_specialized_node to decide_about_value and decide_whether_version_node. 2. The current profile updating code artificially increased the assumed old count when the sum of counts of incoming edges to both the original and new node were bigger than the count of the original node. This always happened when self-recursive edge from the clone was also redirected to the clone because both the original edge and its clone had original high counts. This clutch was removed and replaced by the next point. 3. When cloning creates also redirects a self-recursive clone to the clone itself, new logic has been added to divide the counts brought by such recursive edges between the original node and the clone. This is impossible to do well without special knowledge about the function and which non-recursive entry calls are responsible for what portion of recursion depth, so the approach taken is rather crude. For non-local nodes which can have unknown callers, the algorithm just takes half of the counts - we may decide that taking just a third or some other portion is more reasonable, but I do not think we can attempt anything more clever. For local nodes, we detect the case when the original node is never called (in the training run at least) with another value and if so, steal all its counts like if it was dead. If that is not the case, we try to divide the count brought by recursive edges (or rather not brought by direct edges) proportionally to the counts brought by non-recursive edges - but with artificial limits in place so that we do not take too many or too few, because that was happening with detrimental effect in mcf_r. 4. When cloning creates extra clones for values brought by a formerly self-recursive edge with an arithmetic pass-through jump function on it, such as it does in exchange2_r, all such clones are processed at once rather than one after another. The counts of all such nodes are distributed evenly (modulo even-formerly-non-recursive-edges) and the whole situation is then fixed up so that the edge counts fit. This is what new function update_counts_for_self_gen_clones does. 5. When values brought by a formerly self-recursive edge with an arithmetic pass-through jump function on it are evaluated by heuristics which assumes vast majority of node counts are result of recursive calls and so we simply divide those with the number of clones there would be if we created another one. 6. The mechanisms in init_caller_stats and gather_caller_stats and get_info_about_necessary_edges was enhanced to gather data required for the above and a missing check not to count dead incoming edges was also added. gcc/ChangeLog: 2021-08-23 Martin Jambor * ipa-cp.c (struct caller_statistics): New fields rec_count_sum, n_nonrec_calls and itself, document all fields. (init_caller_stats): Initialize the above new fields. (gather_caller_stats): Gather self-recursive counts and calls number. (get_info_about_necessary_edges): Gather counts of self-recursive and other edges bringing in the requested value separately. (dump_profile_updates): Rework to dump info about a single node only. (lenient_count_portion_handling): New function. (struct gather_other_count_struct): New type. (gather_count_of_non_rec_edges): New function. (struct desc_incoming_count_struct): New type. (analyze_clone_icoming_counts): New function. (adjust_clone_incoming_counts): Likewise. (update_counts_for_self_gen_clones): Likewise. (update_profiling_info): Rewritten. (update_specialized_profile): Adjust call to dump_profile_updates. (create_specialized_node): Do not update profiling info. (decide_about_value): New parameter self_gen_clones, either push new clones into it or updat their profile counts. For self-recursively generated values, use a portion of the node count instead of count from self-recursive edges to estimate goodness. (decide_whether_version_node): Gather clones for self-generated values in a new vector, update their profiles at once at the end. --- gcc/ipa-cp.c | 543 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 457 insertions(+), 86 deletions(-) diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c index b987d975793..53cca7aa804 100644 --- a/gcc/ipa-cp.c +++ b/gcc/ipa-cp.c @@ -701,20 +701,36 @@ ipcp_versionable_function_p (struct cgraph_node *node) struct caller_statistics { + /* If requested (see below), self-recursive call counts are summed into this + field. */ + profile_count rec_count_sum; + /* The sum of all ipa counts of all the other (non-recursive) calls. */ profile_count count_sum; + /* Sum of all frequencies for all calls. */ sreal freq_sum; + /* Number of calls and hot calls respectively. */ int n_calls, n_hot_calls; + /* If itself is set up, also count the number of non-self-recursive + calls. */ + int n_nonrec_calls; + /* If non-NULL, this is the node itself and calls from it should have their + counts included in rec_count_sum and not count_sum. */ + cgraph_node *itself; }; -/* Initialize fields of STAT to zeroes. */ +/* Initialize fields of STAT to zeroes and optionally set it up so that edges + from IGNORED_CALLER are not counted. */ static inline void -init_caller_stats (struct caller_statistics *stats) +init_caller_stats (caller_statistics *stats, cgraph_node *itself = NULL) { + stats->rec_count_sum = profile_count::zero (); stats->count_sum = profile_count::zero (); stats->n_calls = 0; stats->n_hot_calls = 0; + stats->n_nonrec_calls = 0; stats->freq_sum = 0; + stats->itself = itself; } /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of @@ -729,10 +745,22 @@ gather_caller_stats (struct cgraph_node *node, void *data) for (cs = node->callers; cs; cs = cs->next_caller) if (!cs->caller->thunk) { - if (cs->count.ipa ().initialized_p ()) - stats->count_sum += cs->count.ipa (); + ipa_node_params *info = ipa_node_params_sum->get (cs->caller); + if (info && info->node_dead) + continue; + + if (cs->count.ipa ().initialized_p ()) + { + if (stats->itself && stats->itself == cs->caller) + stats->rec_count_sum += cs->count.ipa (); + else + stats->count_sum += cs->count.ipa (); + } stats->freq_sum += cs->sreal_frequency (); stats->n_calls++; + if (stats->itself && stats->itself != cs->caller) + stats->n_nonrec_calls++; + if (cs->maybe_hot_p ()) stats->n_hot_calls ++; } @@ -4202,19 +4230,22 @@ get_next_cgraph_edge_clone (struct cgraph_edge *cs) /* Given VAL that is intended for DEST, iterate over all its sources and if any of them is viable and hot, return true. In that case, for those that still - hold, add their edge frequency and their number into *FREQUENCY and - *CALLER_COUNT respectively. */ + hold, add their edge frequency and their number and cumulative profile + counts of self-ecursive and other edges into *FREQUENCY, *CALLER_COUNT, + REC_COUNT_SUM and NONREC_COUNT_SUM respectively. */ template static bool get_info_about_necessary_edges (ipcp_value *val, cgraph_node *dest, - sreal *freq_sum, profile_count *count_sum, - int *caller_count) + sreal *freq_sum, int *caller_count, + profile_count *rec_count_sum, + profile_count *nonrec_count_sum) { ipcp_value_source *src; sreal freq = 0; int count = 0; - profile_count cnt = profile_count::zero (); + profile_count rec_cnt = profile_count::zero (); + profile_count nonrec_cnt = profile_count::zero (); bool hot = false; bool non_self_recursive = false; @@ -4227,11 +4258,15 @@ get_info_about_necessary_edges (ipcp_value *val, cgraph_node *dest, { count++; freq += cs->sreal_frequency (); - if (cs->count.ipa ().initialized_p ()) - cnt += cs->count.ipa (); hot |= cs->maybe_hot_p (); if (cs->caller != dest) - non_self_recursive = true; + { + non_self_recursive = true; + if (cs->count.ipa ().initialized_p ()) + rec_cnt += cs->count.ipa (); + } + else if (cs->count.ipa ().initialized_p ()) + nonrec_cnt += cs->count.ipa (); } cs = get_next_cgraph_edge_clone (cs); } @@ -4243,8 +4278,9 @@ get_info_about_necessary_edges (ipcp_value *val, cgraph_node *dest, return false; *freq_sum = freq; - *count_sum = cnt; *caller_count = count; + *rec_count_sum = rec_cnt; + *nonrec_count_sum = nonrec_cnt; if (!hot && ipa_node_params_sum->get (dest)->node_within_scc) { @@ -4349,112 +4385,408 @@ get_replacement_map (class ipa_node_params *info, tree value, int parm_num, return replace_map; } -/* Dump new profiling counts */ +/* Dump new profiling counts of NODE. SPEC is true when NODE is a specialzied + one, otherwise it will be referred to as the original node. */ static void -dump_profile_updates (struct cgraph_node *orig_node, - struct cgraph_node *new_node) +dump_profile_updates (cgraph_node *node, bool spec) { - struct cgraph_edge *cs; + if (spec) + fprintf (dump_file, " setting count of the specialized node %s to ", + node->dump_name ()); + else + fprintf (dump_file, " setting count of the original node %s to ", + node->dump_name ()); - fprintf (dump_file, " setting count of the specialized node to "); - new_node->count.dump (dump_file); + node->count.dump (dump_file); fprintf (dump_file, "\n"); - for (cs = new_node->callees; cs; cs = cs->next_callee) + for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee) { - fprintf (dump_file, " edge to %s has count ", - cs->callee->dump_name ()); - cs->count.dump (dump_file); - fprintf (dump_file, "\n"); - } - - fprintf (dump_file, " setting count of the original node to "); - orig_node->count.dump (dump_file); - fprintf (dump_file, "\n"); - for (cs = orig_node->callees; cs; cs = cs->next_callee) - { - fprintf (dump_file, " edge to %s is left with ", + fprintf (dump_file, " edge to %s has count ", cs->callee->dump_name ()); cs->count.dump (dump_file); fprintf (dump_file, "\n"); } } +/* With partial train run we do not want to assume that original's count is + zero whenever we redurect all executed edges to clone. Simply drop profile + to local one in this case. In eany case, return the new value. ORIG_NODE + is the original node and its count has not been updaed yet. */ + +profile_count +lenient_count_portion_handling (profile_count remainder, cgraph_node *orig_node) +{ + if (remainder.ipa_p () && !remainder.ipa ().nonzero_p () + && orig_node->count.ipa_p () && orig_node->count.ipa ().nonzero_p () + && opt_for_fn (orig_node->decl, flag_profile_partial_training)) + remainder = remainder.guessed_local (); + + return remainder; +} + +/* Structure to sum counts coming from nodes other than the original node and + its clones. */ + +struct gather_other_count_struct +{ + cgraph_node *orig; + profile_count other_count; +}; + +/* Worker callback of call_for_symbol_thunks_and_aliases summing the number of + counts that come from non-self-recursive calls.. */ + +static bool +gather_count_of_non_rec_edges (cgraph_node *node, void *data) +{ + gather_other_count_struct *desc = (gather_other_count_struct *) data; + for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller) + if (cs->caller != desc->orig && cs->caller->clone_of != desc->orig) + desc->other_count += cs->count.ipa (); + return false; +} + +/* Structure to help analyze if we need to boost counts of some clones of some + non-recursive edges to match the new callee count. */ + +struct desc_incoming_count_struct +{ + cgraph_node *orig; + hash_set *processed_edges; + profile_count count; + unsigned unproc_orig_rec_edges; +}; + +/* Go over edges calling NODE and its thunks and gather information about + incoming counts so that we know if we need to make any adjustments. */ + +static void +analyze_clone_icoming_counts (cgraph_node *node, + desc_incoming_count_struct *desc) +{ + for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller) + if (cs->caller->thunk) + { + analyze_clone_icoming_counts (cs->caller, desc); + continue; + } + else + { + if (cs->count.initialized_p ()) + desc->count += cs->count.ipa (); + if (!desc->processed_edges->contains (cs) + && cs->caller->clone_of == desc->orig) + desc->unproc_orig_rec_edges++; + } +} + +/* If caller edge counts of a clone created for a self-recursive arithmetic jump + function must be adjusted, do so. NODE is the node or its thunk. */ + +static void +adjust_clone_incoming_counts (cgraph_node *node, + desc_incoming_count_struct *desc) +{ + for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller) + if (cs->caller->thunk) + { + adjust_clone_incoming_counts (cs->caller, desc); + profile_count sum = profile_count::zero (); + for (cgraph_edge *e = cs->caller->callers; e; e = e->next_caller) + if (e->count.initialized_p ()) + sum += e->count.ipa (); + cs->count = cs->count.combine_with_ipa_count (sum); + } + else if (!desc->processed_edges->contains (cs) + && cs->caller->clone_of == desc->orig) + { + cs->count += desc->count; + if (dump_file) + { + fprintf (dump_file, " Adjusted count of an incoming edge of " + "a clone %s -> %s to ", cs->caller->dump_name (), + cs->callee->dump_name ()); + cs->count.dump (dump_file); + fprintf (dump_file, "\n"); + } + } +} + +/* When ORIG_NODE has been cloned for values which have been generated fora + self-recursive call as a result of an arithmetic pass-through + jump-functions, adjust its count together with counts of all such clones in + SELF_GEN_CLONES which also at this point contains ORIG_NODE itself. + + The function sums the counts of the original node and all its clones that + cannot be attributed to a specific clone because it comes from a + non-recursive edge. This sum is then evenly divided between the clones and + on top of that each one gets all the counts which can be attributed directly + to it. */ + +static void +update_counts_for_self_gen_clones (cgraph_node *orig_node, + const vec &self_gen_clones) +{ + profile_count redist_sum = orig_node->count.ipa (); + if (!(redist_sum > profile_count::zero ())) + return; + + if (dump_file) + fprintf (dump_file, " Updating profile of self recursive clone " + "series\n"); + + gather_other_count_struct gocs; + gocs.orig = orig_node; + gocs.other_count = profile_count::zero (); + + auto_vec other_edges_count; + for (cgraph_node *n : self_gen_clones) + { + gocs.other_count = profile_count::zero (); + n->call_for_symbol_thunks_and_aliases (gather_count_of_non_rec_edges, + &gocs, false); + other_edges_count.safe_push (gocs.other_count); + redist_sum -= gocs.other_count; + } + + hash_set processed_edges; + unsigned i = 0; + for (cgraph_node *n : self_gen_clones) + { + profile_count orig_count = n->count; + profile_count new_count + = (redist_sum.apply_scale (1, self_gen_clones.length ()) + + other_edges_count[i]); + new_count = lenient_count_portion_handling (new_count, orig_node); + n->count = new_count; + profile_count::adjust_for_ipa_scaling (&new_count, &orig_count); + for (cgraph_edge *cs = n->callees; cs; cs = cs->next_callee) + { + cs->count = cs->count.apply_scale (new_count, orig_count); + processed_edges.add (cs); + } + for (cgraph_edge *cs = n->indirect_calls; cs; cs = cs->next_callee) + cs->count = cs->count.apply_scale (new_count, orig_count); + + i++; + } + + /* There are still going to be edges to ORIG_NODE that have one or more + clones from another clone in SELF_GEN_CLONES to ORIG_NODE and which we + scaled by the same amount, which means that the total incoming sum of + counts to ORIG_NODE will be too high, scale such edges back. */ + for (cgraph_edge *cs = orig_node->callees; cs; cs = cs->next_callee) + { + if (cs->callee->ultimate_alias_target () == orig_node) + { + unsigned den = 0; + for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e)) + if (e->callee->ultimate_alias_target () == orig_node + && processed_edges.contains (e)) + den++; + if (den > 0) + for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e)) + if (e->callee->ultimate_alias_target () == orig_node + && processed_edges.contains (e)) + e->count = e->count.apply_scale (1, den); + } + } + + /* Edges from the seeds of the valus generated for arithmetic jump-functions + along self-recursive edges are likely to have fairly low count and so + edges from them to nodes in the self_gen_clones do not correspond to the + artificially distributed count of the nodes, the total sum of incoming + edges to some clones might be too low. Detect this situation and correct + it. */ + for (cgraph_node *n : self_gen_clones) + { + if (!(n->count.ipa () > profile_count::zero ())) + continue; + + desc_incoming_count_struct desc; + desc.orig = orig_node; + desc.processed_edges = &processed_edges; + desc.count = profile_count::zero (); + desc.unproc_orig_rec_edges = 0; + analyze_clone_icoming_counts (n, &desc); + + if (n->count.differs_from_p (desc.count)) + { + if (n->count > desc.count + && desc.unproc_orig_rec_edges > 0) + { + desc.count = n->count - desc.count; + desc.count + = desc.count.apply_scale (1, desc.unproc_orig_rec_edges); + adjust_clone_incoming_counts (n, &desc); + } + else if (dump_file) + fprintf (dump_file, + " Unable to fix up incoming counts for %s.\n", + n->dump_name ()); + } + } + + if (dump_file) + for (cgraph_node *n : self_gen_clones) + dump_profile_updates (n, n != orig_node); + return; +} + /* After a specialized NEW_NODE version of ORIG_NODE has been created, update - their profile information to reflect this. */ + their profile information to reflect this. This function should not be used + for clones generated for arithmetic pass-through jump functions on a + self-recursive call graph edge, that situation is handled by + update_counts_for_self_gen_clones. */ static void update_profiling_info (struct cgraph_node *orig_node, struct cgraph_node *new_node) { - struct cgraph_edge *cs; struct caller_statistics stats; - profile_count new_sum, orig_sum; - profile_count remainder, orig_node_count = orig_node->count; - profile_count orig_new_node_count = new_node->count; + profile_count new_sum; + profile_count remainder, orig_node_count = orig_node->count.ipa (); - if (!(orig_node_count.ipa () > profile_count::zero ())) + if (!(orig_node_count > profile_count::zero ())) return; - init_caller_stats (&stats); - orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, - false); - orig_sum = stats.count_sum; - init_caller_stats (&stats); + if (dump_file) + { + fprintf (dump_file, " Updating profile from original count: "); + orig_node_count.dump (dump_file); + fprintf (dump_file, "\n"); + } + + init_caller_stats (&stats, new_node); new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false); new_sum = stats.count_sum; - if (orig_node_count < orig_sum + new_sum) - { - if (dump_file) - { - fprintf (dump_file, " Problem: node %s has too low count ", - orig_node->dump_name ()); - orig_node_count.dump (dump_file); - fprintf (dump_file, "while the sum of incoming count is "); - (orig_sum + new_sum).dump (dump_file); - fprintf (dump_file, "\n"); - } - orig_node_count = (orig_sum + new_sum).apply_scale (12, 10); - if (dump_file) + if (new_sum > orig_node_count) + { + /* TODO: Perhaps this should be gcc_unreachable ()? */ + remainder = profile_count::zero ().guessed_local (); + } + else if (stats.rec_count_sum.nonzero_p ()) + { + int new_nonrec_calls = stats.n_nonrec_calls; + /* There are self-recursive edges which are likely to bring in the + majority of calls but which we must divide in between the original and + new node. */ + init_caller_stats (&stats, orig_node); + orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, + &stats, false); + if (orig_node->local) { - fprintf (dump_file, " proceeding by pretending it was "); - orig_node_count.dump (dump_file); - fprintf (dump_file, "\n"); + /* If ORIG_NODE is local, divide all "unexplained" counts roughly + proportionally to sums of counts of non-recursive calls, unless + the orig_node does not seem to have any counts from elsewhere, + then assume it is dead. */ + if (!stats.count_sum.nonzero_p ()) + { + if (dump_file) + fprintf (dump_file, " The original is local and only " + "has self-recursive edges with counts from non-dead " + "callers, assuming it is dead too.\n"); + new_sum = orig_node_count; + if (opt_for_fn (orig_node->decl, flag_profile_partial_training)) + remainder = profile_count::zero ().guessed_local (); + else + { + /* The NEW_NODE count and counts of all its outgoing edges + are still unmodified copies of ORIG_NODE's. Just clear + the latter and bail out. */ + orig_node->count = profile_count::zero ().guessed_local (); + for (cgraph_edge *cs = orig_node->callees; + cs; + cs = cs->next_callee) + cs->count = profile_count::zero ().guessed_local (); + for (cgraph_edge *cs = orig_node->indirect_calls; + cs; + cs = cs->next_callee) + cs->count = profile_count::zero ().guessed_local (); + return; + } + } + else + { + profile_count unexp = orig_node_count - stats.count_sum; + /* We put rather arbitrary limits on how many counts we claim + because the number of non-self-recursive incoming count is + only a rough guideline and there are cases (such as mcf) where + using it blindly just takes too many. And if clattices are + considered in the opposite order we could also take too + few. */ + int limit_den = 2 * (stats.n_nonrec_calls + new_nonrec_calls); + profile_count new_part + = MAX(MIN (unexp.apply_scale (new_sum, + new_sum + stats.count_sum), + unexp.apply_scale (limit_den - new_nonrec_calls, + limit_den)), + unexp.apply_scale (new_nonrec_calls, limit_den)); + + if (dump_file) + { + fprintf (dump_file, " Claiming "); + new_part.dump (dump_file); + fprintf (dump_file, " of unexplained "); + unexp.dump (dump_file); + fprintf (dump_file, " counts because of self-recursive " + "calls\n"); + } + new_sum += new_part; + unexp -= new_part; + remainder = lenient_count_portion_handling (stats.count_sum + + unexp, + orig_node); + } + } + else + { + /* If ORIG_NODE is not local, take a wild guess and simply claim a + half of all unexplained counts. */ + profile_count half + = (orig_node_count - stats.count_sum).apply_scale (1, 2); + if (dump_file) + { + fprintf (dump_file, " Claiming half of unexplained " + "counts ("); + half.dump (dump_file); + fprintf (dump_file, ") because of relf-recursive calls\n"); + } + new_sum += half; + remainder = lenient_count_portion_handling (stats.count_sum + half, + orig_node); } } - - remainder = orig_node_count.combine_with_ipa_count (orig_node_count.ipa () - - new_sum.ipa ()); - - /* With partial train run we do not want to assume that original's - count is zero whenever we redurect all executed edges to clone. - Simply drop profile to local one in this case. */ - if (remainder.ipa_p () && !remainder.ipa ().nonzero_p () - && orig_node->count.ipa_p () && orig_node->count.ipa ().nonzero_p () - && flag_profile_partial_training) - remainder = remainder.guessed_local (); + else + remainder = lenient_count_portion_handling (orig_node_count - new_sum, + orig_node); new_sum = orig_node_count.combine_with_ipa_count (new_sum); new_node->count = new_sum; orig_node->count = remainder; + profile_count orig_new_node_count = orig_node_count; profile_count::adjust_for_ipa_scaling (&new_sum, &orig_new_node_count); - for (cs = new_node->callees; cs; cs = cs->next_callee) + for (cgraph_edge *cs = new_node->callees; cs; cs = cs->next_callee) cs->count = cs->count.apply_scale (new_sum, orig_new_node_count); - for (cs = new_node->indirect_calls; cs; cs = cs->next_callee) + for (cgraph_edge *cs = new_node->indirect_calls; cs; cs = cs->next_callee) cs->count = cs->count.apply_scale (new_sum, orig_new_node_count); profile_count::adjust_for_ipa_scaling (&remainder, &orig_node_count); - for (cs = orig_node->callees; cs; cs = cs->next_callee) + for (cgraph_edge *cs = orig_node->callees; cs; cs = cs->next_callee) cs->count = cs->count.apply_scale (remainder, orig_node_count); - for (cs = orig_node->indirect_calls; cs; cs = cs->next_callee) + for (cgraph_edge *cs = orig_node->indirect_calls; cs; cs = cs->next_callee) cs->count = cs->count.apply_scale (remainder, orig_node_count); if (dump_file) - dump_profile_updates (orig_node, new_node); + { + dump_profile_updates (new_node, true); + dump_profile_updates (orig_node, false); + } } /* Update the respective profile of specialized NEW_NODE and the original @@ -4495,7 +4827,10 @@ update_specialized_profile (struct cgraph_node *new_node, } if (dump_file) - dump_profile_updates (orig_node, new_node); + { + dump_profile_updates (new_node, true); + dump_profile_updates (orig_node, false); + } } static void adjust_references_in_caller (cgraph_edge *cs, @@ -4795,8 +5130,7 @@ create_specialized_node (struct cgraph_node *node, if (aggvals) ipa_dump_agg_replacement_values (dump_file, aggvals); } - ipa_check_create_node_params (); - update_profiling_info (node, new_node); + new_info = ipa_node_params_sum->get (new_node); new_info->ipcp_orig_node = node; new_node->ipcp_clone = true; @@ -5621,17 +5955,20 @@ ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *, /* Decide whether to create a special version of NODE for value VAL of parameter at the given INDEX. If OFFSET is -1, the value is for the parameter itself, otherwise it is stored at the given OFFSET of the - parameter. AVALS describes the other already known values. */ + parameter. AVALS describes the other already known values. SELF_GEN_CLONES + is a vector which contains clones created for self-recursive calls with an + arithmetic pass-through jump function. */ template static bool decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset, - ipcp_value *val, ipa_auto_call_arg_values *avals) + ipcp_value *val, ipa_auto_call_arg_values *avals, + vec *self_gen_clones) { struct ipa_agg_replacement_value *aggvals; int caller_count; sreal freq_sum; - profile_count count_sum; + profile_count count_sum, rec_count_sum; vec callers; if (val->spec_node) @@ -5647,13 +5984,31 @@ decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset, val->local_size_cost + overall_size); return false; } - else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum, - &caller_count)) + else if (!get_info_about_necessary_edges (val, node, &freq_sum, &caller_count, + &rec_count_sum, &count_sum)) return false; if (!dbg_cnt (ipa_cp_values)) return false; + if (val->self_recursion_generated_p ()) + { + /* The edge counts in this case might not have been adjusted yet. + Nevertleless, even if they were it would be only a guesswork which we + can do now. The recursive part of the counts can be derived from the + count of the original node anyway. */ + if (node->count.ipa ().nonzero_p ()) + { + unsigned dem = self_gen_clones->length () + 1; + rec_count_sum = node->count.ipa ().apply_scale (1, dem); + } + else + rec_count_sum = profile_count::zero (); + } + + /* get_info_about_necessary_edges only sums up ipa counts. */ + count_sum += rec_count_sum; + if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, " - considering value "); @@ -5694,6 +6049,12 @@ decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset, offset, val->value)); val->spec_node = create_specialized_node (node, known_csts, known_contexts, aggvals, callers); + + if (val->self_recursion_generated_p ()) + self_gen_clones->safe_push (val->spec_node); + else + update_profiling_info (node, val->spec_node); + callers.release (); overall_size += val->local_size_cost; if (dump_file && (dump_flags & TDF_DETAILS)) @@ -5722,6 +6083,7 @@ decide_whether_version_node (struct cgraph_node *node) fprintf (dump_file, "\nEvaluating opportunities for %s.\n", node->dump_name ()); + auto_vec self_gen_clones; ipa_auto_call_arg_values avals; gather_context_independent_values (info, &avals, false, NULL); @@ -5736,7 +6098,8 @@ decide_whether_version_node (struct cgraph_node *node) { ipcp_value *val; for (val = lat->values; val; val = val->next) - ret |= decide_about_value (node, i, -1, val, &avals); + ret |= decide_about_value (node, i, -1, val, &avals, + &self_gen_clones); } if (!plats->aggs_bottom) @@ -5750,7 +6113,8 @@ decide_whether_version_node (struct cgraph_node *node) && (plats->aggs_contain_variable || !aglat->is_single_const ())) for (val = aglat->values; val; val = val->next) - ret |= decide_about_value (node, i, aglat->offset, val, &avals); + ret |= decide_about_value (node, i, aglat->offset, val, &avals, + &self_gen_clones); } if (!ctxlat->bottom @@ -5758,10 +6122,17 @@ decide_whether_version_node (struct cgraph_node *node) { ipcp_value *val; for (val = ctxlat->values; val; val = val->next) - ret |= decide_about_value (node, i, -1, val, &avals); + ret |= decide_about_value (node, i, -1, val, &avals, + &self_gen_clones); } } + if (!self_gen_clones.is_empty ()) + { + self_gen_clones.safe_push (node); + update_counts_for_self_gen_clones (node, self_gen_clones); + } + if (info->do_clone_for_all_contexts) { if (!dbg_cnt (ipa_cp_values)) From patchwork Mon Aug 23 18:49:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1520243 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=eIkuM9oy; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=B9LNZRIz; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gv6wS2hnfz9sWl for ; Tue, 24 Aug 2021 21:54:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 604F13857423 for ; Tue, 24 Aug 2021 11:54:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 129233858420 for ; Tue, 24 Aug 2021 11:52:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 129233858420 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 406B320051 for ; Tue, 24 Aug 2021 11:52:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1629805978; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=occ3iOWcV2EZUasBnOlDdCM/lPrsi0gfR0H+XswA75w=; b=eIkuM9oyIjScxWBEU0KcmJ0DeCEM8OVeQCZ33Vs9Vj26frDNb/x8xHSaPW9247kPTz/8lp CK8mA/dOWyXH0JpcOvOTCAlVrRvQG2++6coUlYnUjwcrOWyNxz4uNNP3eNSZtAQFEOHWzb 3q+14+UL1SNFR6CECzqRs1w1ni/BeGg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1629805978; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: resent-to:resent-from:resent-message-id:in-reply-to:in-reply-to: references:references; bh=occ3iOWcV2EZUasBnOlDdCM/lPrsi0gfR0H+XswA75w=; b=B9LNZRIzPRb+A8Zb6EdmlmplSBEyQTTeKUx4tjqeJY/Izsqx9kxdCDywzxhjxuyS2CurAj C6B7sfgZ4Vg1UUDw== Received: from suse.cz (virgil.suse.cz [10.100.13.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 2F0C4A3BC0 for ; Tue, 24 Aug 2021 11:52:58 +0000 (UTC) Resent-From: Martin Jambor Resent-Date: Tue, 24 Aug 2021 13:52:58 +0200 Resent-Message-ID: Resent-To: GCC Patches Message-Id: <96160a5131c9e5eb302fb9f4db43c5d8b4cfe042.1629805719.git.mjambor@suse.cz> In-Reply-To: References: From: Martin Jambor Date: Mon, 23 Aug 2021 20:49:14 +0200 Subject: [PATCH 4/4] ipa-cp: Select saner profile count to base heuristics on To: GCC Patches X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jan Hubicka , Xionghu Luo Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" When profile feedback is available, IPA-CP takes the count of the hottest node and then evaluates all call contexts relative to it. This means that typically almost no clones for specialized contexts are ever created because the maximum is some special function, called from everywhere (that is likely to get inlined anyway) and all the examined edges look cold compared to it. This patch changes the selection. It simply sorts counts of all edges eligible for cloning in a vector and then picks the count in 90th percentile (the actual number is configurable via a parameter). I also tried more complex approaches which were summing the counts and picking the edge which together with all hotter edges accounted for a given portion of the total sum of all edge counts. But first it was not apparently clear to me that they make more logical sense that the simple method and practically I always also had to ignore a few percent of the hottest edges with really extreme counts (looking at bash and python). And when I had to do that anyway, it seemed simpler to just "ignore" more and take the first non-ignored count as the base. Nevertheless, if people think some more sophisticated method should be used anyway, I am willing to be persuaded. But this patch is a clear improvement over the current situation. gcc/ChangeLog: 2021-08-23 Martin Jambor * params.opt (param_ipa_cp_profile_count_base): New parameter. * ipa-cp.c (max_count): Replace with base_count, replace all occurrences too, unless otherwise stated. (ipcp_cloning_candidate_p): identify mostly-directly called functions based on their counts, not max_count. (compare_edge_profile_counts): New function. (ipcp_propagate_stage): Instead of setting max_count, find the appropriate edge count in a sorted vector of counts of eligible edges and make it the base_count. --- gcc/ipa-cp.c | 82 +++++++++++++++++++++++++++++++++++++++++++++----- gcc/params.opt | 4 +++ 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c index 53cca7aa804..6ab74f61e83 100644 --- a/gcc/ipa-cp.c +++ b/gcc/ipa-cp.c @@ -400,9 +400,9 @@ object_allocator > ipcp_sources_pool object_allocator ipcp_agg_lattice_pool ("IPA_CP aggregate lattices"); -/* Maximal count found in program. */ +/* Base count to use in heuristics when using profile feedback. */ -static profile_count max_count; +static profile_count base_count; /* Original overall size of the program. */ @@ -809,7 +809,8 @@ ipcp_cloning_candidate_p (struct cgraph_node *node) /* When profile is available and function is hot, propagate into it even if calls seems cold; constant propagation can improve function's speed significantly. */ - if (max_count > profile_count::zero ()) + if (stats.count_sum > profile_count::zero () + && node->count.ipa ().initialized_p ()) { if (stats.count_sum > node->count.ipa ().apply_scale (90, 100)) { @@ -3310,10 +3311,10 @@ good_cloning_opportunity_p (struct cgraph_node *node, sreal time_benefit, ipa_node_params *info = ipa_node_params_sum->get (node); int eval_threshold = opt_for_fn (node->decl, param_ipa_cp_eval_threshold); - if (max_count > profile_count::zero ()) + if (base_count > profile_count::zero ()) { - sreal factor = count_sum.probability_in (max_count).to_sreal (); + sreal factor = count_sum.probability_in (base_count).to_sreal (); sreal evaluation = (time_benefit * factor) / size_cost; evaluation = incorporate_penalties (node, info, evaluation); evaluation *= 1000; @@ -3950,6 +3951,21 @@ value_topo_info::propagate_effects () } } +/* Callback for qsort to sort counts of all edges. */ + +static int +compare_edge_profile_counts (const void *a, const void *b) +{ + const profile_count *cnt1 = (const profile_count *) a; + const profile_count *cnt2 = (const profile_count *) b; + + if (*cnt1 < *cnt2) + return 1; + if (*cnt1 > *cnt2) + return -1; + return 0; +} + /* Propagate constants, polymorphic contexts and their effects from the summaries interprocedurally. */ @@ -3962,8 +3978,10 @@ ipcp_propagate_stage (class ipa_topo_info *topo) if (dump_file) fprintf (dump_file, "\n Propagating constants:\n\n"); - max_count = profile_count::uninitialized (); + base_count = profile_count::uninitialized (); + bool compute_count_base = false; + unsigned base_count_pos_percent = 0; FOR_EACH_DEFINED_FUNCTION (node) { if (node->has_gimple_body_p () @@ -3981,9 +3999,57 @@ ipcp_propagate_stage (class ipa_topo_info *topo) ipa_size_summary *s = ipa_size_summaries->get (node); if (node->definition && !node->alias && s != NULL) overall_size += s->self_size; - max_count = max_count.max (node->count.ipa ()); + if (node->count.ipa ().initialized_p ()) + { + compute_count_base = true; + unsigned pos_percent = opt_for_fn (node->decl, + param_ipa_cp_profile_count_base); + base_count_pos_percent = MAX (base_count_pos_percent, pos_percent); + } } + if (compute_count_base) + { + auto_vec all_edge_counts; + all_edge_counts.reserve_exact (symtab->edges_count); + FOR_EACH_DEFINED_FUNCTION (node) + for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee) + { + profile_count count = cs->count.ipa (); + if (!(count > profile_count::zero ())) + continue; + + enum availability avail; + cgraph_node *tgt + = cs->callee->function_or_virtual_thunk_symbol (&avail); + ipa_node_params *info = ipa_node_params_sum->get (tgt); + if (info && info->versionable) + all_edge_counts.quick_push (count); + } + + if (!all_edge_counts.is_empty ()) + { + gcc_assert (base_count_pos_percent <= 100); + all_edge_counts.qsort (compare_edge_profile_counts); + + unsigned base_count_pos + = ((all_edge_counts.length () * (base_count_pos_percent)) / 100); + base_count = all_edge_counts[base_count_pos]; + + if (dump_file) + { + fprintf (dump_file, "\nSelected base_count from %u edges at " + "position %u, arriving at: ", all_edge_counts.length (), + base_count_pos); + base_count.dump (dump_file); + fprintf (dump_file, "\n"); + } + } + else if (dump_file) + fprintf (dump_file, "\nNo candidates with non-zero call count found, " + "continuing as if without profile feedback.\n"); + } + orig_overall_size = overall_size; if (dump_file) @@ -6576,7 +6642,7 @@ make_pass_ipa_cp (gcc::context *ctxt) void ipa_cp_c_finalize (void) { - max_count = profile_count::uninitialized (); + base_count = profile_count::uninitialized (); overall_size = 0; orig_overall_size = 0; ipcp_free_transformation_sum (); diff --git a/gcc/params.opt b/gcc/params.opt index 8d772309407..5223f784bf0 100644 --- a/gcc/params.opt +++ b/gcc/params.opt @@ -290,6 +290,10 @@ The size of translation unit that IPA-CP pass considers large. Common Joined UInteger Var(param_ipa_cp_value_list_size) Init(8) Param Optimization Maximum size of a list of values associated with each parameter for interprocedural constant propagation. +-param=ipa-cp-profile-count-base= +Common Joined UInteger Var(param_ipa_cp_profile_count_base) Init(10) IntegerRange(0, 100) Param Optimization +When using profile feedback, use the edge at this percentage position in frequncy histogram as the bases for IPA-CP heuristics. + -param=ipa-jump-function-lookups= Common Joined UInteger Var(param_ipa_jump_function_lookups) Init(8) Param Optimization Maximum number of statements visited during jump function offset discovery.