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: 186917 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp825905dyi; Wed, 10 Jan 2024 06:27:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IGwnEA1FxJXB4DXXmXUIc+13E/nC1DbbYrbiR4dgh3DX7vPkY34XgPgqmbQjXwlc6BEQvzD X-Received: by 2002:a05:6214:401:b0:67f:454c:84de with SMTP id z1-20020a056214040100b0067f454c84demr1292197qvx.62.1704896823827; Wed, 10 Jan 2024 06:27:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704896823; cv=pass; d=google.com; s=arc-20160816; b=LBXNptJpmVY49fRQM1N2iOyAhDqSn3usoXOPh/+pD/NRickXS3zb6qooZixnQbWZZC 9Ukb7fn8TKcn1ClEQjwyKVxx4zscty7tpI1KH0wZD3mcBuYDUn3h61XSoWSBDAfqE1bB dyHxAI6dG8TaRGGkCmijOIbAwOkTCARQFb7FgMXjXh9UXe5NWac8XMb0zj2SvdWth5VA OcfyBNQxKlU9/7D6h0n6iILBn0W017ONHb4IdPEdrH2xah3Cy4jGW4FX4rZblmhA01QM 2Q6PlQnCzv0Q+l9yVGbFskO1zoG6CYtBbqmDt1hYeqUKgqU1aDsvWVZkHJGo0gRnGrO1 6U4g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :subject:to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=hL0iZQog0A7IxsY/BluAEMYfenRAhNVMSEMOT9H3FlA=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=eUc3y+osMKfZfQQbLCM5tOCtAS4ENyhi8vtgX9N2HGlE5/mAqCGh+loJBym9lmRloy 9MVbyLTTb+PzAzD0Kqs6Ny3G6F0jZfWN7tYIwiLi+/IDj9CklLFij7lyNLGRIALOHx9a T5sWHMAmQazbroPmIThh/RblUi8+RQFe6g1s606376tH5FvLg8YnhHGcwvkLvwyGBeHT dzLMwr7+xP00Bvv/IHsHD2zGCw4DZ0lpVLBQfeAVBfEI6wA2jlUjJNLZDjZ520+G1Ix2 b90s7u26aiu5LyfgdQpeUsKuy4deC11OSMVtVO/A/R1uh57OS4AyRRT8wE6JkKnignwH Zz5A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="sa/jgGb1"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=SWl9DP0d; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="sa/jgGb1"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r3-20020a0ce283000000b0067f6204a180si4418783qvl.19.2024.01.10.06.27.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 06:27:03 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="sa/jgGb1"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=SWl9DP0d; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b="sa/jgGb1"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6AEDA3857C62 for ; Wed, 10 Jan 2024 14:27:02 +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+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20240110142702.6AEDA3857C62@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787713892241670640 X-GMAIL-MSGID: 1787713892241670640 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]))) ;