From patchwork Thu Nov 23 23:18:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "juzhe.zhong@rivai.ai" X-Patchwork-Id: 169132 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp769525vqx; Thu, 23 Nov 2023 15:18:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IEzEQwGpd/9aPVKEftomBMDqpi4vSqKyT89Gd6AaInbu19ybHiajU6BtC7AgW6uZnnUG4Yt X-Received: by 2002:a05:6808:6503:b0:3b2:f599:348c with SMTP id fm3-20020a056808650300b003b2f599348cmr1084139oib.49.1700781514419; Thu, 23 Nov 2023 15:18:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700781514; cv=pass; d=google.com; s=arc-20160816; b=KWrh0wdoriC1nShUvcH2TSErXiuA179ChoMdU+x+ZczNqnF/TtT3/5cLmRIQ+I5oxV KNQDhXd53Fqpa2AGNg+mU6giX1e2CE/uiFh85xwpJnfuuxHRiHVpFOInJK5XrgM3xL6u xwjqWA3DhFxMzRcSjblzfrexAQtCUrb8U+a88LDtObWnyODuL3NAhnZLQvEBuKEE9TSq T6batF5EgLgvS+b6h/XHltUNg3SbB1hXv5zKQXvfl/2buOJvOZwdtNscqgV3VrYKsOty th2ybdmAaV5pEA+UxGpntLjF2A0h2kgzghtUzCObu1eXF44CC9GvoTJOb6NCJICL9DFZ DZyA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:arc-filter:dmarc-filter:delivered-to; bh=bVc533TfYhs/1NlLHYNby35E0nnxDTzCEnU3/83AOWs=; fh=12MRPJmZ1mgDpHqWoogMKqnaGRGM2b7lcuJroqfjJiw=; b=uUljyX3PvIfpQUGInZmV/9StECARHYus8RcjHR7P5gz/uCUNqGkPl+z+Xz5BZ+D8rM dfLnS/5RjZzBxkZ9tgKu+9iaT//SxkD+w9F8RRJvJFCZ05EPbpdRbmgsg7jcT3sKGMD/ mVNj8Z4rTW+bMYAlPjAXp6eKLl7hCjwMKGWm9AH1Ef9UTNJ7haIS2IC8y9ekY837SSu5 cu9HeUHhegmhOeIh0qzbVgNfv39rnfqELafv9GDcZhLdfBJYFUOlhQX6VnUJwxxTgSvv 7CJOyT0D5gDRRUbmHsmSwxFInkfQPa2Ada4FsYLjpB15vgdAo+0MI87FyeKWBn5jTzLm GHdw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ea15-20020a05620a488f00b0077d7a26addcsi1102280qkb.64.2023.11.23.15.18.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 15:18:34 -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; arc=pass (i=1); 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 3144138582BE for ; Thu, 23 Nov 2023 23:18:34 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbguseast2.qq.com (smtpbguseast2.qq.com [54.204.34.130]) by sourceware.org (Postfix) with ESMTPS id E4B40385843A for ; Thu, 23 Nov 2023 23:18:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4B40385843A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivai.ai Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivai.ai ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E4B40385843A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=54.204.34.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700781491; cv=none; b=Ngh3K4O5LRFXKkZ/gi/7AKdAlCs0KvMErsqb9Yrrem+qaQKftcCM6RqhCWjJG1C2cSGa9cnCXGwCPVPIVcCGvCYrNl76aJcmXRwy9FLyudZxH9eq8ijVRD7OLCTgNKbYC9QBCGbzAb52G1ffGSZC/dfJEiIroDP5TIt/lBbQmaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700781491; c=relaxed/simple; bh=+6z0sKQOyz5qfRuckpdzsepb/gmowAlUEAzoEBb3ml0=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=AFIZ4gUsiWLpt/2C74Tu77+XMcLuGZRJygyzGoXe4HrwWH4wKwAaT4gqk14myeLDu6R9uGV6lIlUdkkYFhdZx+GeHaFu1/bfaB0dWNN+lVxXDkERm9s2gj2lRnJRhK+18ngtiDraZm4S/QdFQIi1MPAMgXSrA7HGrVGX6NKP3Zg= ARC-Authentication-Results: i=1; server2.sourceware.org X-QQ-mid: bizesmtp67t1700781482tnfqvp0a Received: from rios-cad122.hadoop.rioslab.org ( [58.60.1.26]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 24 Nov 2023 07:18:01 +0800 (CST) X-QQ-SSF: 01400000000000G0V000000A0000000 X-QQ-FEAT: TcB0gMxhP5MJeWMQTECNLC2uJYgWxbAhMaXYonJ9+1nUUCmJcqExvjCM6r12y I0MOi0MQ6uQs3DNBLooFs1b+w9no8a7NWTXMqdq5blWIVQx6gLm6zXseHd9YGAKl5uMiX2I oUiIVIvXG8eSF3uf3zy/KxmKh5JP+LagjJPj8StV1uelWziN+PXIWmTz1w7xtsJmiVgtW7Y JE0OWEncSYzQ/tdxKcce33eUq5O14q/ox21SzerkBXHfLWT8M0FpdRVXT4GQ4MneXBQ2RhZ v2QYjRrI08DRPkxUA7rX8KpQ08UPFUYQyqAEozPzcSVb+52Eej18k1Gu77sG4Lr/h15lv3x LF+CkqTzTJdmhIRqwDHeutG26UbbBs5ZlmyWcJc X-QQ-GoodBg: 2 X-BIZMAIL-ID: 1552543704614713171 From: Juzhe-Zhong To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, kito.cheng@sifive.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, Juzhe-Zhong Subject: [PATCH V2] RISC-V: Optimize a special case of VLA SLP Date: Fri, 24 Nov 2023 07:18:00 +0800 Message-Id: <20231123231800.3823357-1-juzhe.zhong@rivai.ai> X-Mailer: git-send-email 2.36.3 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:rivai.ai:qybglogicsvrgz:qybglogicsvrgz7a-one-0 X-Spam-Status: No, score=-8.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_PASS, 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783398677585140168 X-GMAIL-MSGID: 1783398677585140168 When working on fixing bugs of zvl1024b. I notice a special VLA SLP case can be better optimized. v = vec_perm (op1, op2, { nunits - 1, nunits, nunits + 1, ... }) Before this patch, we are using genriec approach (vrgather): vid vadd.vx vrgather vmsgeu vrgather With this patch, we use vec_extract + slide1up: scalar = vec_extract (last element of op1) v = slide1up (op2, scalar) Tested on zvl128b/zvl256b/zvl512b/zvl1024b of both RV32 and RV64 no regression. Ok for trunk ? PR target/112599 gcc/ChangeLog: * config/riscv/riscv-v.cc (shuffle_extract_and_slide1up_patterns): New function. (expand_vec_perm_const_1): Add new optimization. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/pr112599-2.c: New test. --- gcc/config/riscv/riscv-v.cc | 38 ++++++++++++++ .../gcc.target/riscv/rvv/autovec/pr112599-2.c | 51 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112599-2.c diff --git a/gcc/config/riscv/riscv-v.cc b/gcc/config/riscv/riscv-v.cc index 72b96d8339d..18619a11592 100644 --- a/gcc/config/riscv/riscv-v.cc +++ b/gcc/config/riscv/riscv-v.cc @@ -3232,6 +3232,42 @@ shuffle_bswap_pattern (struct expand_vec_perm_d *d) return true; } +/* Recognize the pattern that can be shuffled by vec_extract and slide1up + approach. */ + +static bool +shuffle_extract_and_slide1up_patterns (struct expand_vec_perm_d *d) +{ + poly_int64 nunits = GET_MODE_NUNITS (d->vmode); + + /* Recognize { nunits - 1, nunits, nunits + 1, ... }. */ + if (!d->perm.series_p (0, 2, nunits - 1, 2) + || !d->perm.series_p (1, 2, nunits, 2)) + return false; + + /* Disable when nunits < 4 since the later generic approach + is more profitable on indice = { nunits - 1, nunits }. */ + if (!known_gt (nunits, 2)) + return false; + + /* Success! */ + if (d->testing_p) + return true; + + /* Extract the last element of the first vector. */ + scalar_mode smode = GET_MODE_INNER (d->vmode); + rtx tmp = gen_reg_rtx (smode); + emit_vec_extract (tmp, d->op0, nunits - 1); + + /* Insert the scalar into element 0. */ + unsigned int unspec + = FLOAT_MODE_P (d->vmode) ? UNSPEC_VFSLIDE1UP : UNSPEC_VSLIDE1UP; + insn_code icode = code_for_pred_slide (unspec, d->vmode); + rtx ops[] = {d->target, d->op1, tmp}; + emit_vlmax_insn (icode, BINARY_OP, ops); + return true; +} + /* Recognize the pattern that can be shuffled by generic approach. */ static bool @@ -3310,6 +3346,8 @@ expand_vec_perm_const_1 (struct expand_vec_perm_d *d) return true; if (shuffle_bswap_pattern (d)) return true; + if (shuffle_extract_and_slide1up_patterns (d)) + return true; if (shuffle_generic_patterns (d)) return true; return false; diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112599-2.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112599-2.c new file mode 100644 index 00000000000..fd87565b054 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112599-2.c @@ -0,0 +1,51 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gcv_zvl1024b -mabi=lp64d -O3" } */ + +struct s { struct s *n; } *p; +struct s ss; +#define MAX 10 +struct s sss[MAX]; +int count = 0; + +int look( struct s *p, struct s **pp ) +{ + for ( ; p; p = p->n ) + ; + *pp = p; + count++; + return( 1 ); +} + +void sub( struct s *p, struct s **pp ) +{ + for ( ; look( p, pp ); ) { + if ( p ) + p = p->n; + else + break; + } +} + +int +foo(void) +{ + struct s *pp; + struct s *next; + int i; + + p = &ss; + next = p; + for ( i = 0; i < MAX; i++ ) { + next->n = &sss[i]; + next = next->n; + } + next->n = 0; + + sub( p, &pp ); + if (count != MAX+2) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-assembler-not {vrgather} } } */ +/* { dg-final { scan-assembler-times {vslide1up\.vx} 1 } } */