From patchwork Thu Jan 18 10:34:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 84332 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCED7385783E for ; Thu, 18 Jan 2024 10:35:55 +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.223.130]) by sourceware.org (Postfix) with ESMTPS id 163093858435 for ; Thu, 18 Jan 2024 10:35:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 163093858435 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 163093858435 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705574119; cv=none; b=eIhGU0DxU8umhoG4+rOyPq4ReshHq0HMnMWHwNa6qhdsa1pQqj1VXDrnN0bcQfedETxHIOpJFhrbeVt75hD6vNyn4wmpPLd4ncg6wZTVhg6vYdvVwO8nuacv+CGzx8rWZ4jFpkcDvSEaXI/4UtvdgrMIM8g/wzZRxv4nfzOVeBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705574119; c=relaxed/simple; bh=p/F2I/sJXEAQImIV61cwaC2AR4rDs+5yqgOza14JBpU=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=IhhF6a32aqUF/HGwoKwVBTmNMrvQjBmLovDIyI+u2zeXI7+4hmOzIxvGzwsh/pI2vIWCSO/PRLJo8+IsU4bajf5WD0GPd2IbVj41SiPWaBGYNZ7Io2z2uTlffeROhgucomWcRVKCDVQ3D1OCsipGrvBB/hd8a6YfLz3XmwAr8+s= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.4.150] (unknown [10.168.4.150]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id C8A1121F63 for ; Thu, 18 Jan 2024 10:35:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1705574115; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=tVwUiZegwTn5aaccaCWXQxETsZ84avh0MZLcOB5jE6w=; b=RwdA5BkRUXgaHdn04QuyxlocKTG1JMr6MpLNBtCiOP7gkTamBrKgLVlKZcdTPoQBK2IPKq +mVop2PQ6DPbwHgZahUY7Oz6bkr+qws+cy9R6eK6TwKJu/S9f5eR+Lh5ZCrOBnXQkpHRtm jLpxyJAq0L1GDBoUt/aaW6kUkaSdmSg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1705574115; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=tVwUiZegwTn5aaccaCWXQxETsZ84avh0MZLcOB5jE6w=; b=j0Q8HkSEYEUjQOD0G+Df1hLZSfFwL+UOFVDGu5iIzvPqfUANrj3N69vdnLrcA0PE1ZA5ML fpXHSdIfx4o3/lDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1705574115; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=tVwUiZegwTn5aaccaCWXQxETsZ84avh0MZLcOB5jE6w=; b=RwdA5BkRUXgaHdn04QuyxlocKTG1JMr6MpLNBtCiOP7gkTamBrKgLVlKZcdTPoQBK2IPKq +mVop2PQ6DPbwHgZahUY7Oz6bkr+qws+cy9R6eK6TwKJu/S9f5eR+Lh5ZCrOBnXQkpHRtm jLpxyJAq0L1GDBoUt/aaW6kUkaSdmSg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1705574115; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=tVwUiZegwTn5aaccaCWXQxETsZ84avh0MZLcOB5jE6w=; b=j0Q8HkSEYEUjQOD0G+Df1hLZSfFwL+UOFVDGu5iIzvPqfUANrj3N69vdnLrcA0PE1ZA5ML fpXHSdIfx4o3/lDw== Date: Thu, 18 Jan 2024 11:34:16 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix memory leak in vect_analyze_loop_form MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [-0.60 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: X-Spam-Score: -0.60 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Message-Id: <20240118103555.DCED7385783E@sourceware.org> The following fixes a memory leak in vect_analyze_loop_form which fails to free the loop body it gets. It also allows more countable exits, matching what we can handle later, when we decide which exit to use as main exit. Finally some no longer applying comments are adjusted. Bootstrap and regtest running on x86_64-unknown-linux-gnu. * tree-vect-loop.cc (vec_init_loop_exit_info): Adjust comment, prefer all later exits we can handle. (vect_analyze_loop_form): Free the allocated loop body. Adjust comments. --- gcc/tree-vect-loop.cc | 45 ++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 330c4571c8d..c815c606f21 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -977,8 +977,8 @@ vec_init_loop_exit_info (class loop *loop) if (exits.length () == 1) return exits[0]; - /* If we have multiple exits we only support counting IV at the moment. Analyze - all exits and return one */ + /* If we have multiple exits we only support counting IV at the moment. + Analyze all exits and return the last one we can analyze. */ class tree_niter_desc niter_desc; edge candidate = NULL; for (edge exit : exits) @@ -990,7 +990,9 @@ vec_init_loop_exit_info (class loop *loop) && !chrec_contains_undetermined (niter_desc.niter)) { tree may_be_zero = niter_desc.may_be_zero; - if (integer_zerop (may_be_zero) + if ((integer_zerop (may_be_zero) + || integer_nonzerop (may_be_zero) + || COMPARISON_CLASS_P (may_be_zero)) && (!candidate || dominated_by_p (CDI_DOMINATORS, exit->src, candidate->src))) @@ -1745,14 +1747,18 @@ vect_analyze_loop_form (class loop *loop, vect_loop_form_info *info) /* Check if we have any control flow that doesn't leave the loop. */ class loop *v_loop = loop->inner ? loop->inner : loop; - basic_block *bbs= get_loop_body (v_loop); + basic_block *bbs = get_loop_body (v_loop); for (unsigned i = 0; i < v_loop->num_nodes; i++) if (EDGE_COUNT (bbs[i]->succs) != 1 && (EDGE_COUNT (bbs[i]->succs) != 2 || !loop_exits_from_bb_p (bbs[i]->loop_father, bbs[i]))) - return opt_result::failure_at (vect_location, - "not vectorized:" - " unsupported control flow in loop.\n"); + { + free (bbs); + return opt_result::failure_at (vect_location, + "not vectorized:" + " unsupported control flow in loop.\n"); + } + free (bbs); /* Different restrictions apply when we are considering an inner-most loop, vs. an outer (nested) loop. @@ -1761,17 +1767,7 @@ vect_analyze_loop_form (class loop *loop, vect_loop_form_info *info) info->inner_loop_cond = NULL; if (!loop->inner) { - /* Inner-most loop. We currently require that the number of BBs is - exactly 2 (the header and latch). Vectorizable inner-most loops - look like this: - - (pre-header) - | - header <--------+ - | | | - | +--> latch --+ - | - (exit-bb) */ + /* Inner-most loop. */ if (empty_block_p (loop->header)) return opt_result::failure_at (vect_location, @@ -1783,7 +1779,8 @@ vect_analyze_loop_form (class loop *loop, vect_loop_form_info *info) edge entryedge; /* Nested loop. We currently require that the loop is doubly-nested, - contains a single inner loop, and the number of BBs is exactly 5. + contains a single inner loop with a single exit to the block + with the single exit condition in the outer loop. Vectorizable outer-loops look like this: (pre-header) @@ -1796,7 +1793,7 @@ vect_analyze_loop_form (class loop *loop, vect_loop_form_info *info) | (exit-bb) - The inner-loop has the properties expected of inner-most loops + The inner-loop also has the properties expected of inner-most loops as described above. */ if ((loop->inner)->inner || (loop->inner)->next) @@ -1845,16 +1842,13 @@ vect_analyze_loop_form (class loop *loop, vect_loop_form_info *info) "not vectorized:" " too many incoming edges.\n"); - /* We assume that the loop exit condition is at the end of the loop. i.e, - that the loop is represented as a do-while (with a proper if-guard - before the loop if needed), where the loop header contains all the - executable statements, and the latch is empty. */ + /* We assume that the latch is empty. */ if (!empty_block_p (loop->latch) || !gimple_seq_empty_p (phi_nodes (loop->latch))) return opt_result::failure_at (vect_location, "not vectorized: latch block not empty.\n"); - /* Make sure the exit is not abnormal. */ + /* Make sure there is no abnormal exit. */ auto_vec exits = get_loop_exit_edges (loop); for (edge e : exits) { @@ -1868,7 +1862,6 @@ vect_analyze_loop_form (class loop *loop, vect_loop_form_info *info) = vect_get_loop_niters (loop, exit_e, &info->assumptions, &info->number_of_iterations, &info->number_of_iterationsm1); - if (info->conds.is_empty ()) return opt_result::failure_at (vect_location,