From patchwork Tue Nov 22 14:12:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1707887 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.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: legolas.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=S5Y51fzh; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=oXKt+Qlj; 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NGmRy71RMz23nm for ; Wed, 23 Nov 2022 01:12:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D47F63858025 for ; Tue, 22 Nov 2022 14:12:44 +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 B074D3858C20 for ; Tue, 22 Nov 2022 14:12:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B074D3858C20 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 imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id AC5511F899; Tue, 22 Nov 2022 14:12:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1669126323; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=9+dn+Z5b1kaYJC6LYgQ5KZ+LgN1cX5qkhuPgYDiyer4=; b=S5Y51fzhkDjZ6PbIFmKD4zelzFzx3ud4kLBqD0ZVtBDulZip2TVHN/Bp5w05+i3gOrW5AE Tgz6AkVdP2tuM2a6cSY1Awt8XIqldBchc9KOv1veBH6GnLS15sBXPecKJsjUpOotbGZJ3+ YBD3Ehv2xA6eW8oYGZG3xk2QmoUPGEY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1669126323; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=9+dn+Z5b1kaYJC6LYgQ5KZ+LgN1cX5qkhuPgYDiyer4=; b=oXKt+QljF9nYE9L0MMO+qnMz0/FdToHAkxRME1PMI4cBxC4tj+73K1KsMjzRe72Gqqx8K3 eGLst+vGep33rnDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 9F2B913AA1; Tue, 22 Nov 2022 14:12:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id mjXMJrPYfGPbaAAAMHmgww (envelope-from ); Tue, 22 Nov 2022 14:12:03 +0000 From: Martin Jambor To: GCC Patches Cc: Jan Hubicka Subject: [PATCH] ipa-cp: Do not be too optimistic about self-recursive edges (PR 107661) User-Agent: Notmuch/0.37 (https://notmuchmail.org) Emacs/28.1 (x86_64-suse-linux-gnu) Date: Tue, 22 Nov 2022 15:12:03 +0100 Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-11.8 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, PR 107661 shows that function push_agg_values_for_index_from_edge should not attempt to optimize self-recursive call graph edges when called from cgraph_edge_brings_all_agg_vals_for_node. Unlike when being called from find_aggregate_values_for_callers_subset, we cannot expect that any cloning for constants would lead to the edge leading from a new clone to the same new clone, in this case it would only be redirected to a new callee. Fixed by adding a parameter to push_agg_values_from_edge whether being optimistic about self-recursive edges is possible. Bootstrapped, LTO-bootstrapped and tested on x86_64-linux. OK for trunk? Thanks, Martin gcc/ChangeLog: 2022-11-22 Martin Jambor PR ipa/107661 * ipa-cp.cc (push_agg_values_from_edge): New parameter optimize_self_recursion, use it to decide whether to pass interim to the helper function. (find_aggregate_values_for_callers_subset): Pass true in the new parameter of push_agg_values_from_edge. (cgraph_edge_brings_all_agg_vals_for_node): Pass false in the new parameter of push_agg_values_from_edge. gcc/testsuite/ChangeLog: 2022-11-22 Martin Jambor PR ipa/107661 * g++.dg/ipa/pr107661.C: New test. --- gcc/ipa-cp.cc | 18 +++++++----- gcc/testsuite/g++.dg/ipa/pr107661.C | 45 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ipa/pr107661.C diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc index d2bcd5e5e69..f0feb4beb8f 100644 --- a/gcc/ipa-cp.cc +++ b/gcc/ipa-cp.cc @@ -5752,14 +5752,16 @@ push_agg_values_for_index_from_edge (struct cgraph_edge *cs, int index, description of ultimate callee of CS or the one it was cloned from (the summary where lattices are). If INTERIM is non-NULL, it contains the current interim state of collected aggregate values which can be used to - compute values passed over self-recursive edges and to skip values which - clearly will not be part of intersection with INTERIM. */ + compute values passed over self-recursive edges (if OPTIMIZE_SELF_RECURSION + is true) and to skip values which clearly will not be part of intersection + with INTERIM. */ static void push_agg_values_from_edge (struct cgraph_edge *cs, ipa_node_params *dest_info, vec *res, - const ipa_argagg_value_list *interim) + const ipa_argagg_value_list *interim, + bool optimize_self_recursion) { ipa_edge_args *args = ipa_edge_args_sum->get (cs); if (!args) @@ -5785,7 +5787,9 @@ push_agg_values_from_edge (struct cgraph_edge *cs, ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, index); if (plats->aggs_bottom) continue; - push_agg_values_for_index_from_edge (cs, index, res, interim); + push_agg_values_for_index_from_edge (cs, index, res, + optimize_self_recursion ? interim + : NULL); } } @@ -5804,7 +5808,7 @@ find_aggregate_values_for_callers_subset (struct cgraph_node *node, /* gather_edges_for_value puts a non-recursive call into the first element of callers if it can. */ auto_vec interim; - push_agg_values_from_edge (callers[0], dest_info, &interim, NULL); + push_agg_values_from_edge (callers[0], dest_info, &interim, NULL, true); unsigned valid_entries = interim.length (); if (!valid_entries) @@ -5815,7 +5819,7 @@ find_aggregate_values_for_callers_subset (struct cgraph_node *node, { auto_vec last; ipa_argagg_value_list avs (&interim); - push_agg_values_from_edge (callers[i], dest_info, &last, &avs); + push_agg_values_from_edge (callers[i], dest_info, &last, &avs, true); valid_entries = intersect_argaggs_with (interim, last); if (!valid_entries) @@ -5882,7 +5886,7 @@ cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs, ipa_node_params *dest_info = ipa_node_params_sum->get (node); gcc_checking_assert (dest_info->ipcp_orig_node); dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node); - push_agg_values_from_edge (cs, dest_info, &edge_values, &existing); + push_agg_values_from_edge (cs, dest_info, &edge_values, &existing, false); const ipa_argagg_value_list avl (&edge_values); return avl.superset_of_p (existing); } diff --git a/gcc/testsuite/g++.dg/ipa/pr107661.C b/gcc/testsuite/g++.dg/ipa/pr107661.C new file mode 100644 index 00000000000..cc6f8538dbf --- /dev/null +++ b/gcc/testsuite/g++.dg/ipa/pr107661.C @@ -0,0 +1,45 @@ +/* { dg-do run { target c++11 } } */ +/* { dg-options "-O1 -fipa-cp -fipa-cp-clone" } */ + +struct R {} RGood; +struct L {} LBad; + +volatile int vi; +static void __attribute__((noipa)) L_run(void) { vi = 0; __builtin_abort (); } +static void callback_fn_L(void) { vi = 1; L_run(); } +static void callback_fn_R(void) { vi = 2; } + +struct function_ref { + void (*callback)(void) = nullptr; + + function_ref(L * pl) { callback = callback_fn_L; } + function_ref(R * pr) { callback = callback_fn_R; } +}; + +// allow one level of recursion to call callback twice +static int is_recur(void) { + static int n = 0; + switch (n++) { + case 0: return 1; + default: return 0; + } +} + +static void do3(volatile int * punused, function_ref Expired) { + Expired.callback(); + + if (is_recur()) + do3(punused, Expired); +} + +static void do1(function_ref Expired) { + volatile int unused = 42; + + do3(&unused, Expired); +} + +int main(int, const char **) { do1(&RGood); return 0; } + +void seemingly_unused_foo(void) { do1(&LBad); } + +void (*fnptr)(void) = seemingly_unused_foo;