From patchwork Mon May 15 03:15:14 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: 93829 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp6631673vqo; Sun, 14 May 2023 20:16:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7o0O4yj7X5tAWV/ovZYIgetgCBd1YTtdr8SHEU62TcwBvk1XS5mLwvu0FzS8jxipv1F6Gt X-Received: by 2002:aa7:d455:0:b0:50d:a27b:10c2 with SMTP id q21-20020aa7d455000000b0050da27b10c2mr17749454edr.6.1684120612887; Sun, 14 May 2023 20:16:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684120612; cv=none; d=google.com; s=arc-20160816; b=FtrDDOB/3GPJPXabXuOGHsvOR/JFRwci3/VuTDM2vYkAux6fC6aNNAV0YEJEnwN1bs nEIE9VmK2fhlTVqzWjhhD7Rr2uKnMolZCJ8Pi6tCkB6xPWn0GiNCn1a9kP6GDxCJSYBZ fCpNgIcenCNav2FJJckMHkC3Q7e9qCIm7uRSwoUnEd9KuWMRC42sE5fyC1Iu/I+UquSE jMouvcskwkRvLCwzhUGwWh+/DCfmKDeXMBkWC0f5bZL1hah/l3YvMWerah04NK1DxruD DYdvNqm8j+2SQmNDX6VrN627ywNfNgBnvtoPf6RChn29LEvB0CGbEe3UXWs+hy25gWdh Wh5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender: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:dmarc-filter:delivered-to; bh=1XzuejZ1PQXh1oVjUIihEJrQFxMD943Xhm/oPy4Ftu4=; b=rXCH9Pc3dKIR/ioAByDjDKCnE7uXgjv41ys9J0mIgI+t+TwgxnBnKSZCcjzyNz9rKd rxwNtInCZQ5Aonv31IHvAln9AzooCsZjdhjsZi66XWd70qgyjFeO9hGLMpmsj0ECZ0QW 2KBEr48tuSU4uaE8W0XOlrMlJrAJ48kU/SI29sIDB+Nofk4uGyZDQbH/zlRxrxHR0A8V D4d//bI3PkunSScM0cRoQTd3aHYQu/H2F+nIpamCK1WJY7h8WXmzHd3zqZKG7q1lci1z aNf5+/zjt2tdpwdTLHgUWykLNaF6eIi1NUNlbRRyFisOK7/hoyxadWKTzGZqP7zDyLqL gw/g== ARC-Authentication-Results: i=1; mx.google.com; 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u6-20020aa7db86000000b0050d9b5885fesi10820545edt.217.2023.05.14.20.16.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 May 2023 20:16:52 -0700 (PDT) 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; 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 755163853814 for ; Mon, 15 May 2023 03:16:24 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbg150.qq.com (smtpbg150.qq.com [18.132.163.193]) by sourceware.org (Postfix) with ESMTPS id B25203858438 for ; Mon, 15 May 2023 03:15:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B25203858438 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivai.ai Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivai.ai X-QQ-mid: bizesmtp88t1684120516tj6kf4vb Received: from server1.localdomain ( [58.60.1.22]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 15 May 2023 11:15:15 +0800 (CST) X-QQ-SSF: 01400000000000F0R000000A0000000 X-QQ-FEAT: ZkxZBO9qcf66Piruf80mUp/0p7azRpUUkjR7tJDeIemX6MrBZHR58Hd6tpbmy Ge2ZdTfRwR6PaiX/tQcMBa+K+w13a3U7g/9av46Dazz1tC65XrdIeNER0k5eBAlhDHefZFO sk0gvUYMI3Ffbnd2rD871pjo7enjeMMsMYchge00b2UmaIhzKVdj12Zchxw77Vj3NHVes55 plsCsLcHjTm+f+KSWbXaP4y9qAQU4TFa6US14u6QhPnrRBYSFTuTXAHpTOTEj/mIyHxzHox vlt9lS19Jt00AiKBd9RrEUynSJtum5d1eAgpv19YmmLkvNhyC7DcUIasP+S5fn+wKiFp35V Gd+38w9Y5z3Du7bnCZ7kvkf2ZJK8xbpsaqznZUGZzQlnShI7TXbRN6IOF7nxj5bq0J/c6eX l7QqdoFZsn8= X-QQ-GoodBg: 2 X-BIZMAIL-ID: 13938973471011282040 From: juzhe.zhong@rivai.ai To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, kito.cheng@sifive.com, palmer@dabbelt.com, palmer@rivosinc.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.co, Juzhe-Zhong Subject: [PATCH] RISC-V: Support TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT to optimize codegen of both VLA && VLS auto-vectorization. Date: Mon, 15 May 2023 11:15:14 +0800 Message-Id: <20230515031514.241945-1-juzhe.zhong@rivai.ai> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:rivai.ai:qybglogicsvrgz:qybglogicsvrgz7a-one-0 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SCC_10_SHORT_WORD_LINES, SCC_5_SHORT_WORD_LINES, 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.29 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 Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765928456252757525?= X-GMAIL-MSGID: =?utf-8?q?1765928456252757525?= From: Juzhe-Zhong This patch optimizes both RVV VLA && VLS vectorization. Consider this following case: void __attribute__((noinline, noclone)) f (int * __restrict dst, int * __restrict op1, int * __restrict op2, int count) { for (int i = 0; i < count; ++i) dst[i] = op1[i] + op2[i]; } VLA: Before this patch: ble a3,zero,.L1 srli a4,a1,2 negw a4,a4 andi a5,a4,3 sext.w a3,a3 beq a5,zero,.L3 lw a7,0(a1) lw a6,0(a2) andi a4,a4,2 addw a6,a6,a7 sw a6,0(a0) beq a4,zero,.L3 lw a7,4(a1) lw a4,4(a2) li a6,3 addw a4,a4,a7 sw a4,4(a0) bne a5,a6,.L3 lw a6,8(a2) lw a4,8(a1) addw a4,a4,a6 sw a4,8(a0) .L3: subw a3,a3,a5 slli a4,a3,32 csrr a6,vlenb srli a4,a4,32 srli a6,a6,2 slli a3,a5,2 mv a5,a4 bgtu a4,a6,.L17 .L5: csrr a6,vlenb add a1,a1,a3 add a2,a2,a3 add a0,a0,a3 srli a7,a6,2 li a3,0 .L8: vsetvli zero,a5,e32,m1,ta,ma vle32.v v1,0(a1) vle32.v v2,0(a2) vsetvli t1,zero,e32,m1,ta,ma add a3,a3,a7 vadd.vv v1,v1,v2 vsetvli zero,a5,e32,m1,ta,ma vse32.v v1,0(a0) mv a5,a4 bleu a4,a3,.L6 mv a5,a3 .L6: sub a5,a4,a5 bleu a5,a7,.L7 mv a5,a7 .L7: add a1,a1,a6 add a2,a2,a6 add a0,a0,a6 bne a5,zero,.L8 .L1: ret .L17: mv a5,a6 j .L5 After this patch: f: ble a3,zero,.L1 csrr a4,vlenb srli a4,a4,2 mv a5,a3 bgtu a3,a4,.L9 .L3: csrr a6,vlenb li a4,0 srli a7,a6,2 .L6: vsetvli zero,a5,e32,m1,ta,ma vle32.v v2,0(a1) vle32.v v1,0(a2) vsetvli t1,zero,e32,m1,ta,ma add a4,a4,a7 vadd.vv v1,v1,v2 vsetvli zero,a5,e32,m1,ta,ma vse32.v v1,0(a0) mv a5,a3 bleu a3,a4,.L4 mv a5,a4 .L4: sub a5,a3,a5 bleu a5,a7,.L5 mv a5,a7 .L5: add a0,a0,a6 add a2,a2,a6 add a1,a1,a6 bne a5,zero,.L6 .L1: ret .L9: mv a5,a4 j .L3 VLS: Before this patch: f3: ble a3,zero,.L1 srli a5,a1,2 negw a5,a5 andi a4,a5,3 sext.w a3,a3 beq a4,zero,.L3 lw a7,0(a1) lw a6,0(a2) andi a5,a5,2 addw a6,a6,a7 sw a6,0(a0) beq a5,zero,.L3 lw a7,4(a1) lw a5,4(a2) li a6,3 addw a5,a5,a7 sw a5,4(a0) bne a4,a6,.L3 lw a6,8(a2) lw a5,8(a1) addw a5,a5,a6 sw a5,8(a0) .L3: subw a3,a3,a4 slli a6,a4,2 slli a5,a3,32 srli a5,a5,32 add a1,a1,a6 add a2,a2,a6 add a0,a0,a6 li a3,4 .L6: mv a4,a5 bleu a5,a3,.L5 li a4,4 .L5: vsetvli zero,a4,e32,m1,ta,ma vle32.v v1,0(a1) vle32.v v2,0(a2) vsetivli zero,4,e32,m1,ta,ma sub a5,a5,a4 vadd.vv v1,v1,v2 vsetvli zero,a4,e32,m1,ta,ma vse32.v v1,0(a0) addi a1,a1,16 addi a2,a2,16 addi a0,a0,16 bne a5,zero,.L6 .L1: ret After this patch: f3: ble a3,zero,.L1 li a4,4 .L4: mv a5,a3 bleu a3,a4,.L3 li a5,4 .L3: vsetvli zero,a5,e32,m1,ta,ma vle32.v v2,0(a1) vle32.v v1,0(a2) vsetivli zero,4,e32,m1,ta,ma sub a3,a3,a5 vadd.vv v1,v1,v2 vsetvli zero,a5,e32,m1,ta,ma vse32.v v1,0(a0) addi a2,a2,16 addi a0,a0,16 addi a1,a1,16 bne a3,zero,.L4 .L1: ret gcc/ChangeLog: * config/riscv/riscv.cc (riscv_vectorize_preferred_vector_alignment): New function. (TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT): New target hook. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/binop/shift-rv32gcv.c: Adapt testcase. * gcc.target/riscv/rvv/autovec/align-1.c: New test. * gcc.target/riscv/rvv/autovec/align-2.c: New test. --- gcc/config/riscv/riscv.cc | 14 ++++++++++++++ .../gcc.target/riscv/rvv/autovec/align-1.c | 12 ++++++++++++ .../gcc.target/riscv/rvv/autovec/align-2.c | 12 ++++++++++++ .../riscv/rvv/autovec/binop/shift-rv32gcv.c | 10 ++++++---- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/align-1.c create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/align-2.c diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index de578b5b899..a770fdfaa0e 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -7499,6 +7499,16 @@ riscv_preferred_simd_mode (scalar_mode mode) return word_mode; } +/* Implement target hook TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT. */ + +static poly_uint64 +riscv_vectorize_preferred_vector_alignment (const_tree type) +{ + if (riscv_v_ext_vector_mode_p (TYPE_MODE (type))) + return TYPE_ALIGN (TREE_TYPE (type)); + return TYPE_ALIGN (type); +} + /* Initialize the GCC target structure. */ #undef TARGET_ASM_ALIGNED_HI_OP #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" @@ -7771,6 +7781,10 @@ riscv_preferred_simd_mode (scalar_mode mode) #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE riscv_preferred_simd_mode +#undef TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT +#define TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT \ + riscv_vectorize_preferred_vector_alignment + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-riscv.h" diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/align-1.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/align-1.c new file mode 100644 index 00000000000..14201e1f7e0 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/align-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv32gcv -mabi=ilp32d -O3 --param riscv-autovec-preference=scalable" } */ + +void __attribute__((noinline, noclone)) +f (int * __restrict dst, int * __restrict op1, int * __restrict op2, int count) +{ + for (int i = 0; i < count; ++i) + dst[i] = op1[i] + op2[i]; +} + +/* { dg-final { scan-assembler-not "lw" } } */ +/* { dg-final { scan-assembler-not "sw" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/align-2.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/align-2.c new file mode 100644 index 00000000000..812584e9d25 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/align-2.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv32gcv -mabi=ilp32d -O3 --param riscv-autovec-preference=fixed-vlmax" } */ + +void __attribute__((noinline, noclone)) +f (int * __restrict dst, int * __restrict op1, int * __restrict op2, int count) +{ + for (int i = 0; i < count; ++i) + dst[i] = op1[i] + op2[i]; +} + +/* { dg-final { scan-assembler-not "lw" } } */ +/* { dg-final { scan-assembler-not "sw" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/shift-rv32gcv.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/shift-rv32gcv.c index da0f79a1cf0..d98100b3276 100644 --- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/shift-rv32gcv.c +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/binop/shift-rv32gcv.c @@ -4,8 +4,10 @@ #include "shift-template.h" /* TODO: For int16_t and uint16_t we need widening/promotion patterns. - Therefore, expect only 4 vsll.vv instead of 6 for now. */ + We don't check the assembler number since lacking patterns make + auto-vectorization inconsistent in LMUL = 1/2/4/8. */ + +/* { dg-final { scan-assembler {\tvsll\.vv} } } */ +/* { dg-final { scan-assembler {\tvsrl\.vv} } } */ +/* { dg-final { scan-assembler {\tvsra\.vv} } } */ -/* { dg-final { scan-assembler-times {\tvsll\.vv} 4 } } */ -/* { dg-final { scan-assembler-times {\tvsrl\.vv} 3 } } */ -/* { dg-final { scan-assembler-times {\tvsra\.vv} 3 } } */