From patchwork Wed Jan 10 14:21:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 83749 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 2092B3858298 for ; Wed, 10 Jan 2024 14:26: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 [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id C2BFA3858C41 for ; Wed, 10 Jan 2024 14:26:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2BFA3858C41 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 C2BFA3858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704896778; cv=none; b=E6pUZAmEeNVoCa3O+fXZu0JEDjovk4mfGYwvVgLRC3dJuRBeMugU5eZ9FjKCvC3DbZMJDiIVdPJs/g4NyRpSM1wg9FnKe0kZww+ll9+zjaBUBuQ8szQ5C/a/Cntz/qIJjgyLsBhhtLhsraQ8cUCC4cQXd3JpufX0jUmt6syZyo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704896778; c=relaxed/simple; bh=6dyb+jwlUPXiA0QFe1JXfJz0BFTo/0P28HoIkJhdjL8=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version; b=xbWDhHh4PQN4FX+dtCvOmnkgU7wS+3h8dZL1+Pu1kcfoMjCTMpYCF5n6S4weV253AyYJuuFTr5Jye59YcGgfNkMJVJzJaIbeWFc+o2fViJ/d1ez0V5nbesch6NsEZ21vibaMO5iKLGgshUkADNs05acewMjzs8gPc6iqVAqv/X8= 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-out2.suse.de (Postfix) with ESMTPS id 66A5A1F8BE for ; Wed, 10 Jan 2024 14:26:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1704896773; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=hL0iZQog0A7IxsY/BluAEMYfenRAhNVMSEMOT9H3FlA=; b=sa/jgGb1W5u1+eue64Rwrlbv+DlWp/aRXnkD7MzTiakOVXBATtoJ727Uk5U5O9xBTitHpM x6J7T8nTqf5NXmtMqshp8MzYhACf8cjEjIYJPWx0nME0ZmSMC+Djkkl1r3ChpU3RH//+hB 7iecXjEjRqwCqGtchKfOxjQRnYT7mmk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1704896773; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=hL0iZQog0A7IxsY/BluAEMYfenRAhNVMSEMOT9H3FlA=; b=SWl9DP0dwVgBbt97UA8iC6d0GcyfVxQ3K/ls0zQVu1JICPk1s9IEp2qOufLrulLh6+Xlw8 IdUBwnJZN8ZlXiDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1704896773; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=hL0iZQog0A7IxsY/BluAEMYfenRAhNVMSEMOT9H3FlA=; b=sa/jgGb1W5u1+eue64Rwrlbv+DlWp/aRXnkD7MzTiakOVXBATtoJ727Uk5U5O9xBTitHpM x6J7T8nTqf5NXmtMqshp8MzYhACf8cjEjIYJPWx0nME0ZmSMC+Djkkl1r3ChpU3RH//+hB 7iecXjEjRqwCqGtchKfOxjQRnYT7mmk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1704896773; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=hL0iZQog0A7IxsY/BluAEMYfenRAhNVMSEMOT9H3FlA=; b=SWl9DP0dwVgBbt97UA8iC6d0GcyfVxQ3K/ls0zQVu1JICPk1s9IEp2qOufLrulLh6+Xlw8 IdUBwnJZN8ZlXiDw== Date: Wed, 10 Jan 2024 15:21:13 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/113078 - conditional subtraction reduction vectorization MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -0.80 X-Spamd-Result: default: False [-0.80 / 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]; NEURAL_HAM_SHORT(-0.20)[-1.000]; DBL_BLOCKED_OPENRESOLVER(0.00)[gcc.target:url]; 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-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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: <20240110142647.2092B3858298@sourceware.org> When if-conversion was changed to use .COND_ADD/SUB for conditional reduction it was forgotten to update reduction path handling to canonicalize .COND_SUB to .COND_ADD for vectorizable_reduction similar to what we do for MINUS_EXPR. The following adds this and testcases exercising this at runtime and looking for the appropriate masked subtraction in the vectorized code on x86. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/113078 * tree-vect-loop.cc (check_reduction_path): Canonicalize .COND_SUB to .COND_ADD. * gcc.dg/vect/vect-reduc-cond-sub.c: New testcase. * gcc.target/i386/vect-pr113078.c: Likewise. --- .../gcc.dg/vect/vect-reduc-cond-sub.c | 29 +++++++++++++++++++ gcc/testsuite/gcc.target/i386/vect-pr113078.c | 16 ++++++++++ gcc/tree-vect-loop.cc | 7 +++++ 3 files changed, 52 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c create mode 100644 gcc/testsuite/gcc.target/i386/vect-pr113078.c diff --git a/gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c b/gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c new file mode 100644 index 00000000000..0213a0ab4fd --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/vect-reduc-cond-sub.c @@ -0,0 +1,29 @@ +/* { dg-require-effective-target vect_int } */ + +#include "tree-vect.h" + +int __attribute__((noipa)) +foo (int n, int* p, int* pi) +{ + int sum = 0; + for (int i = 0; i != n; i++) + { + if (pi[i] > 0) + sum -= p[i]; + } + return sum; +} + +int p[16] __attribute__((aligned(__BIGGEST_ALIGNMENT__))) + = { 7, 3, 1, 4, 9, 10, 14, 7, -10, -55, 20, 9, 1, 2, 0, -17 }; +int pi[16] __attribute__((aligned(__BIGGEST_ALIGNMENT__))) + = { 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1 }; +int +main() +{ + check_vect (); + + if (foo (16, p, pi) != 57) + abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.target/i386/vect-pr113078.c b/gcc/testsuite/gcc.target/i386/vect-pr113078.c new file mode 100644 index 00000000000..e7666054324 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/vect-pr113078.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -mavx512vl" } */ + +int +foo (int n, int* p, int* pi) +{ + int sum = 0; + for (int i = 0; i != n; i++) + { + if (pi[i] > 0) + sum -= p[i]; + } + return sum; +} + +/* { dg-final { scan-assembler-times "vpsub\[^\r\n\]*%k" 2 } } */ diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index c5b2799be23..1bdad0fbe0f 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -4116,6 +4116,13 @@ pop: if (op.ops[1] == op.ops[opi]) neg = ! neg; } + else if (op.code == IFN_COND_SUB) + { + op.code = IFN_COND_ADD; + /* Track whether we negate the reduction value each iteration. */ + if (op.ops[2] == op.ops[opi]) + neg = ! neg; + } if (CONVERT_EXPR_CODE_P (op.code) && tree_nop_conversion_p (op.type, TREE_TYPE (op.ops[0]))) ;