From patchwork Sat May 13 11:44:21 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: 93511 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp5716967vqo; Sat, 13 May 2023 04:45:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7sNXV/IIHWse73Z7t345bNWebKi49fn3KCIm96JYql0tVry1LLNkr2u96P+BOZb69f7buG X-Received: by 2002:a17:907:7b98:b0:947:55ad:dd00 with SMTP id ne24-20020a1709077b9800b0094755addd00mr27308233ejc.26.1683978313658; Sat, 13 May 2023 04:45:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683978313; cv=none; d=google.com; s=arc-20160816; b=Y9Ap1vS5kY0dab071dkHaNAtWrSol9lPVEqxttQxkPQuCXfHqqfrBK9NRIVM8Hcqym aRlEjOSkZddiK+WyH9InulwUyJyUZ7zNsI2StWUJhEsCDFYDpftFPOapv4G71/g4TkwT zzi6blxzMBZkmzuhRmQHj0WMkV47495v28J2E/f1Z+YIDggsoKwEU4XD6wNsdV+aCYQ3 Q8wLD1UOX19njA2RDwnJLBs8otOMMRJJhod4GZF7UMzofOHmznJRBwB3g54lVFAFPLhw GWNOovczjGUDHMOQM+hdA/c4ou8h/Pv1p6hbS4/7QpnVxN+UP0v6k6v3ILs3wTR/ECca Sgqg== 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=SwFWGmVucYZqbPVaisZDVgafcFnIEwBsim3h3xseu8k=; b=Yp80PvAv+K8qBuHUnijYmToN4hxQDKH4jU83g056H8HF3rY0WOvlpHQqBF8VnzgL7h Graiw2vOqO4FnZKHXURMuVwFAezvkw9+xJ72Xw1IQWUmP54kC2eLAyOV8+x4hqJPDHkN zBXjTQIPvrtrq6ExVBjnCY9g/moj8ypXtU5QvByEFi+SslxDjVOPF2qv43K4rws7cuZ9 aJKqWIgec9Zxj4rB4RC+Mrh7C62bqlw7KWX7t+jzvalFMeqjVUpGesKhJLNV+TaDxNBY bgzdRIVUHNcd7S3Y4TWNw0+cg0Fs+Pfmm/C2CNQ29jLBSrbEwDVa3x7uq1Roirqh3CKC wirg== 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 j23-20020a170906411700b0095f071fb512si7951867ejk.266.2023.05.13.04.45.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 May 2023 04:45:13 -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 54BD338555BE for ; Sat, 13 May 2023 11:45:02 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbguseast1.qq.com (smtpbguseast1.qq.com [54.204.34.129]) by sourceware.org (Postfix) with ESMTPS id 396FD3858C60 for ; Sat, 13 May 2023 11:44:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 396FD3858C60 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: bizesmtp88t1683978266tse4n55e Received: from server1.localdomain ( [58.60.1.22]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 13 May 2023 19:44:24 +0800 (CST) X-QQ-SSF: 01400000000000F0R000000A0000000 X-QQ-FEAT: rZJGTgY0+YM28n2fXoyJuS3dsuGUew8Du89okaoTr5ERSc9556jpuJKvaaElo PBYqBcBbFkIycL8DDmk/9Qm1RBSJqlh/ghUniFuYa+iYyxesnxwIVKVESP8zHa5d11rTgk6 VBTIgWH/ATwryg7gEozMTogx8kN8AFtLtSxnV2slKd+CFGNwobb50zrJ+8D4OSyVZJ50nQD O/pvizujM42EtAnoCehJF6/UdmNk4/YR0lPatmjBKkNrh7/VR5a82I0DzOsbCOYL2mIodYN YjPWUhOU/nq0DRH1bFJK2GIHjcvhzQL95ADDBiOdOSk6qU7kkX97uq8pDFYp7Zm9rnByTSi 8mSmwlsTNadiDWj4FLw5gkj+WNloFzStts4wVeSeX4B4aC63djrO5olnRN/QsxTHCfI46/j 6tsBW5n6oow= X-QQ-GoodBg: 2 X-BIZMAIL-ID: 14592174195565556753 From: juzhe.zhong@rivai.ai To: gcc-patches@gcc.gnu.org Cc: kito.cheng@sifive.com, palmer@rivosinc.com, rdapp.gcc@gmail.com, jeffreyalaw@gmail.com, Juzhe-Zhong Subject: [PATCH] RISC-V: Support TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT to optimize codegen of RVV auto-vectorization Date: Sat, 13 May 2023 19:44:21 +0800 Message-Id: <20230513114421.196081-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=-10.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SCC_5_SHORT_WORD_LINES, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, T_SPF_HELO_TEMPERROR 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?1765779244014333077?= X-GMAIL-MSGID: =?utf-8?q?1765779244014333077?= From: Juzhe-Zhong This patch support vector alignement for RVV auto-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]; } 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 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/config/riscv/riscv.cc | 22 +++++++++++++++++++ .../gcc.target/riscv/rvv/autovec/align-1.c | 12 ++++++++++ .../riscv/rvv/autovec/binop/shift-rv32gcv.c | 10 +++++---- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/align-1.c diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index de578b5b899..a5776a550b2 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -7499,6 +7499,24 @@ 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))) + { + /* If the length of the vector is a fixed power of 2, try to align + to that length, otherwise don't try to align at all. */ + HOST_WIDE_INT result; + if (!GET_MODE_BITSIZE (TYPE_MODE (type)).is_constant (&result) + || !pow2p_hwi (result)) + result = TYPE_ALIGN (TREE_TYPE (type)); + return result; + } + 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 +7789,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/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 } } */