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: 24401 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2221332wrr; Tue, 22 Nov 2022 06:13:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf5X1c0n7nZmG+2/W36uXqd83Xl6EzQE6B8QVWUt7fxjqAYE2TKaLZ/donlLKqGf2A02fF7y X-Received: by 2002:a17:906:3b48:b0:78d:b65a:aabe with SMTP id h8-20020a1709063b4800b0078db65aaabemr7181434ejf.5.1669126388914; Tue, 22 Nov 2022 06:13:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669126388; cv=none; d=google.com; s=arc-20160816; b=gLwocM+L19EFl4X342yZJt7uY1T5Fu5jX43aoRY+LJX+HvThrrYnU3QLRRp9PlwIJA LaiQFgNUCQ+5BSzMrHedE3nITT1k8x0oBQCxKFtpX4gyI04j4c1mAe2+md0X9qoMtDd8 GCgB25AKfYLf1RShH9xW8j+qDwIodByJ1osV0dHxdoz/Fk1eQogB8jBghCrsbC/H+mBi DoqEvRknpbH4oLPOSxyHT+RAydS3td/Gwe/Kwg3u/Uk28nnN6EwncII7nmnaTeMhDSw4 p6YXi1FU/PqRZwI8Z2gJ5VIxRsnX9phavoAkGxvN8hRa7z+UoxYLulUgcBvTAmmtIZqQ R7aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:message-id:date :user-agent:subject:cc:to:from:dkim-signature:dkim-signature :dmarc-filter:delivered-to; bh=9+dn+Z5b1kaYJC6LYgQ5KZ+LgN1cX5qkhuPgYDiyer4=; b=zfyO4gA6gfYk2eed0pIA34qULeXMbEMyCXkzImdcn1SaxyZadwnOiEGXWvjqn5/1Ua YB3LoP9BR/gAIYZUCfkNtM7EL+R5/2y9Pwo9Obq2+IBR3gNdGeR38PMwYbsgkpKsV23E Bve2PQ8YszKFb3L0O8PqBiv1FdZVgMc+/odXAVxbGWsQjoY/SdgSK6RyZbFgKADHpZbP Q6Mgb99lvhyHKhxelV3wsGlv3LBGm2nl1iSPxDToGxF1U0hGVdf1oTBZJjfKZMls6hSy x3yFWHtEP4vm5zaVvi5qxkb3Alwlf/1vdIhHGOR80N/xCo1+D/dreRYZdLy7iTupgTC8 Itcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=S5Y51fzh; dkim=neutral (no key) header.i=@gcc.gnu.org header.s=susede2_ed25519; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g10-20020a1709067c4a00b007adc8d6b77esi39897ejp.308.2022.11.22.06.13.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 06:13:08 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=S5Y51fzh; dkim=neutral (no key) header.i=@gcc.gnu.org header.s=susede2_ed25519; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C77383854544 for ; Tue, 22 Nov 2022 14:13:04 +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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750205872377922935?= X-GMAIL-MSGID: =?utf-8?q?1750205872377922935?= 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;