From patchwork Thu Jan 21 09:59:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 1429707 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@gcc.gnu.org; receiver=) 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 4DLyXj6PPdz9sVF for ; Thu, 21 Jan 2021 20:59:17 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C406D3836C58; Thu, 21 Jan 2021 09:59:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id C89303870902 for ; Thu, 21 Jan 2021 09:59:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C89303870902 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mjambor@suse.cz X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id AE27AAAAE; Thu, 21 Jan 2021 09:59:12 +0000 (UTC) From: Martin Jambor To: GCC Patches Subject: [PATCH] ipa: Fix resolving speculations through cgraph_edge::set_call_stmt (PR 98078) User-Agent: Notmuch/0.31.2 (https://notmuchmail.org) Emacs/27.1 (x86_64-suse-linux-gnu) Date: Thu, 21 Jan 2021 10:59:12 +0100 Message-ID: MIME-Version: 1.0 X-Spam-Status: No, score=-3039.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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 Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, in the PR 98078 testcase, speculative call-graph edges which were created by IPA-CP are confirmed during inlining but cgraph_edge::set_call_stmt does not take it very well. The function enters the update_speculative branch and updates the edges in the speculation bundle separately (by a recursive call), but when it processes the first direct edge, most of the bundle actually ceases to exist because it is devirtualized. It nevertheless goes on to attempt to update the indirect edge (that has just been removed), which surprisingly gets as far as adding the edge to the call_site_hash, the same devirtualized edge for the second time, and that triggers an assert. Fixed by this patch which makes the function aware that it is about to resolve a speculation and do so instead of updating components of speculation. Also, it does so before dealing with the hash because the speculation resolution code needs the hash to point to the first speculative direct edge and also cleans the hash up by calling update_call_stmt_hash_for_removing_direct_edge. I don't have a testcase, at least not yet, the one in BZ does not link when it does not ICE. I can try to find some time to make it link it is deemed very important. Bootstrapped and tested on x86_64-linux, also profile-LTO-bootstrapped on the same system. OK for trunk? What about gcc10, where we cannot trigger it but I suppose the bug is there? Thanks, Martin gcc/ChangeLog: 2021-01-20 Martin Jambor PR ipa/98078 * cgraph.c (cgraph_edge::set_call_stmt): Do not update all corresponding speculative edges if we are about to resolve sepculation. Make edge direct (and so resolve speculations) before removing it from call_site_hash. (cgraph_edge::make_direct): Relax the initial assert to allow calling the function on speculative direct edges. --- gcc/cgraph.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/gcc/cgraph.c b/gcc/cgraph.c index db038306e19..80140757d16 100644 --- a/gcc/cgraph.c +++ b/gcc/cgraph.c @@ -789,9 +789,22 @@ cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt, { tree decl; + cgraph_node *new_direct_callee = NULL; + if ((e->indirect_unknown_callee || e->speculative) + && (decl = gimple_call_fndecl (new_stmt))) + { + /* Constant propagation and especially inlining can turn an indirect call + into a direct one. */ + new_direct_callee = cgraph_node::get (decl); + gcc_checking_assert (new_direct_callee); + } + /* Speculative edges has three component, update all of them when asked to. */ - if (update_speculative && e->speculative) + if (update_speculative && e->speculative + /* If we are about to resolve the speculation by calling make_direct + below, do not bother going over all the speculative edges now. */ + && !new_direct_callee) { cgraph_edge *direct, *indirect, *next; ipa_ref *ref; @@ -821,6 +834,9 @@ cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt, return e_indirect ? indirect : direct; } + if (new_direct_callee) + e = make_direct (e, new_direct_callee); + /* Only direct speculative edges go to call_site_hash. */ if (e->caller->call_site_hash && (!e->speculative || !e->indirect_unknown_callee) @@ -831,16 +847,6 @@ cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt, (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt)); e->call_stmt = new_stmt; - if (e->indirect_unknown_callee - && (decl = gimple_call_fndecl (new_stmt))) - { - /* Constant propagation (and possibly also inlining?) can turn an - indirect call into a direct one. */ - cgraph_node *new_callee = cgraph_node::get (decl); - - gcc_checking_assert (new_callee); - e = make_direct (e, new_callee); - } function *fun = DECL_STRUCT_FUNCTION (e->caller->decl); e->can_throw_external = stmt_can_throw_external (fun, new_stmt); @@ -1279,14 +1285,15 @@ cgraph_edge::speculative_call_for_target (cgraph_node *target) return NULL; } -/* Make an indirect edge with an unknown callee an ordinary edge leading to - CALLEE. Speculations can be resolved in the process and EDGE can be removed - and deallocated. Return the edge that now represents the call. */ +/* Make an indirect or speculative EDGE with an unknown callee an ordinary edge + leading to CALLEE. Speculations can be resolved in the process and EDGE can + be removed and deallocated. Return the edge that now represents the + call. */ cgraph_edge * cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee) { - gcc_assert (edge->indirect_unknown_callee); + gcc_assert (edge->indirect_unknown_callee || edge->speculative); /* If we are redirecting speculative call, make it non-speculative. */ if (edge->speculative)