From patchwork Wed Oct 11 09:03:09 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: 151232 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp396139vqb; Wed, 11 Oct 2023 02:03:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8NDQX1zKMgFmNGuYKsnPUR/MV+cjX9HPymV3vRmu9XLKEyrzN8tkoGjVYdemsjKQKflCK X-Received: by 2002:a17:906:53d4:b0:9ae:5bd7:d2b4 with SMTP id p20-20020a17090653d400b009ae5bd7d2b4mr19275970ejo.68.1697015031276; Wed, 11 Oct 2023 02:03:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697015031; cv=none; d=google.com; s=arc-20160816; b=BDwsKO9wvZFvkkHgQhp5r7sfK1kWxcWOd0sGWYNPOLKZI9Ei3ySZx5f//Q+vCuYaCp KkbODX6q3S7iUX4f+jDi2NNQBzcu+2KyCWHQ49z6dp7FDPFECQpOxCHTprCYxC2I/Ssf cpavxO7Nzj5PBV3uVm1aP+xK1KaGjRYIFgRxQzKeRfxruXmrmAwO9HPCvimfkQeaO88o dezTx3sznJ/ysb6yva0TKznKrjQeh5ZpueN+DkNXB3YelAqRWEkNUOSwbmvxWCapkP/u zDQ8NQlcB8v51+hAErIbgYtH12FybfcgdLKMOk6PvF0K/ZcXZgxw0GVZ3gZmD5BNqld7 SLxQ== ARC-Message-Signature: i=1; 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:dmarc-filter:delivered-to; bh=3JeMVz716NZjeAbOE6uFHfv72hGks8y99jP7hKxPxWQ=; fh=12MRPJmZ1mgDpHqWoogMKqnaGRGM2b7lcuJroqfjJiw=; b=C/BplLuZyjYonTuZbCgs4f+ejslXZrUA9wXqmFWJxzjHTIxmfhLYaRD/8Ux+2lTeY2 Eh0x/iSnxl+/ATJGTUDhPQ5Igpkua7SdUB7tkfCKmZdc028U7bCaSvi4uSXU5DZIF1Lq kMXboMeP8kBYqCIUYeEP+MMy56yb+fnXubpxDdigRe0o8tc5SNWfGXPgJyUtwSR9QaEO ILNO3xz8EWDDaw/YDCEStlKaTJ5Fykt3yTqRLFLt4sBcycCbG6M98VQplAIxPu7y7Ugr WAv7+So+aNe0CTsb2t79aIAftbM8EGcAXnRBBw96CnHTzFfRU1Y2mB0hznwHVSyvcjss nkXQ== 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 server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o25-20020a1709062e9900b0099342fc2a87si6356821eji.692.2023.10.11.02.03.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 02:03:51 -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 28D5F3857725 for ; Wed, 11 Oct 2023 09:03:46 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbgeu2.qq.com (smtpbgeu2.qq.com [18.194.254.142]) by sourceware.org (Postfix) with ESMTPS id 102B33858CDA for ; Wed, 11 Oct 2023 09:03:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 102B33858CDA 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: bizesmtp68t1697014991t48nv9hq Received: from rios-cad122.hadoop.rioslab.org ( [58.60.1.26]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 11 Oct 2023 17:03:10 +0800 (CST) X-QQ-SSF: 01400000000000G0V000000A0000000 X-QQ-FEAT: RrZlkntZBfn0idaTLQBtEtJ9C3IA7JP8U2NqC6DwmRDorDmKgSwnbNvkZSrH0 OoGkXkoFvK5WUyQvra415jkI+Ey9AyUOYtBTy8E1IOqxGUxLNA67R16STdvZ6mdKESMlCnj N/MbY+641FFCsclYuLPy9APueA2fnklC9vV1xOKdjLkeSXmD81bmlq5WU1l1YmHt1vo8M21 kfsg8SgBxaaZbe9sZA7V5Wi1L3/vBB/DtK+CUH04JqHLOO321SlV2b2xR1AUISUrU6A+xln GyxadaStj3vRj6BETkfuq6tvI8vNsKQkwnmvzvoO7Aa5aLePbAaZ3qyZ+wnhdlalqx3itNF NkE22QiVAxc96jGjrgg9ZYqmYgJG3bVfKarwi3pv8DYVgZjXaGcO5ibUYembMwexP7iJUUu TIupwua/Ouurb71shZaEvQt6WExxiToi X-QQ-GoodBg: 2 X-BIZMAIL-ID: 16419434981364300098 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] RISC-V: Fix incorrect index(offset) of gather/scatter Date: Wed, 11 Oct 2023 17:03:09 +0800 Message-Id: <20231011090309.2987108-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=-10.3 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, SPF_PASS, TXREP, 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.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: 1779449233182984862 X-GMAIL-MSGID: 1779449233182984862 I suddenly I made a mistake that was lucky un-exposed. https://godbolt.org/z/c3jzrh7or GCC is using 32 bit index offset: vsll.vi v1,v1,2 vsetvli zero,a5,e32,m1,ta,ma vluxei32.v v1,(a1),v1 This is wrong since v1 may overflow 32bit after vsll.vi. After this patch: vsext.vf2 v8,v4 vsll.vi v8,v8,2 vluxei64.v v8,(a1),v8 Same as Clang. Regression passed. Ok for trunk ? gcc/ChangeLog: * config/riscv/autovec.md: Fix offset bug. * config/riscv/riscv-protos.h (gather_scatter_valid_offset_p): New function. * config/riscv/riscv-v.cc (expand_gather_scatter): Fix offset bug. (gather_scatter_valid_offset_p): New function. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/gather-scatter/offset_extend-1.c: New test. --- gcc/config/riscv/autovec.md | 28 +++++++++---------- gcc/config/riscv/riscv-protos.h | 1 + gcc/config/riscv/riscv-v.cc | 16 +++++++++-- .../autovec/gather-scatter/offset_extend-1.c | 14 ++++++++++ 4 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/gather-scatter/offset_extend-1.c diff --git a/gcc/config/riscv/autovec.md b/gcc/config/riscv/autovec.md index 41bff3a318f..07607bff71e 100644 --- a/gcc/config/riscv/autovec.md +++ b/gcc/config/riscv/autovec.md @@ -59,7 +59,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -74,7 +74,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -89,7 +89,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -104,7 +104,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -119,7 +119,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -134,7 +134,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -153,7 +153,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, true); DONE; @@ -172,7 +172,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; @@ -187,7 +187,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; @@ -202,7 +202,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; @@ -217,7 +217,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; @@ -232,7 +232,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; @@ -247,7 +247,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; @@ -266,7 +266,7 @@ (match_operand: 5 "vector_mask_operand") (match_operand 6 "autovec_length_operand") (match_operand 7 "const_0_operand")] - "TARGET_VECTOR" + "TARGET_VECTOR && riscv_vector::gather_scatter_valid_offset_p (mode)" { riscv_vector::expand_gather_scatter (operands, false); DONE; diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index 43426a5326b..f805332f378 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -543,6 +543,7 @@ opt_machine_mode vectorize_related_mode (machine_mode, scalar_mode, unsigned int autovectorize_vector_modes (vec *, bool); bool cmp_lmul_le_one (machine_mode); bool cmp_lmul_gt_one (machine_mode); +bool gather_scatter_valid_offset_p (machine_mode); } /* We classify builtin types into two classes: diff --git a/gcc/config/riscv/riscv-v.cc b/gcc/config/riscv/riscv-v.cc index c72e411f125..6e90f1bbee1 100644 --- a/gcc/config/riscv/riscv-v.cc +++ b/gcc/config/riscv/riscv-v.cc @@ -3491,15 +3491,14 @@ expand_gather_scatter (rtx *ops, bool is_load) machine_mode vec_mode = GET_MODE (vec_reg); machine_mode idx_mode = GET_MODE (vec_offset); - scalar_mode inner_vec_mode = GET_MODE_INNER (vec_mode); scalar_mode inner_idx_mode = GET_MODE_INNER (idx_mode); - unsigned inner_vsize = GET_MODE_BITSIZE (inner_vec_mode); unsigned inner_offsize = GET_MODE_BITSIZE (inner_idx_mode); poly_int64 nunits = GET_MODE_NUNITS (vec_mode); poly_int64 value; bool is_vlmax = poly_int_rtx_p (len, &value) && known_eq (value, nunits); - if (inner_offsize < inner_vsize) + /* Extend the offset element to address width. */ + if (inner_offsize < GET_MODE_BITSIZE (GET_MODE (ptr)).to_constant ()) { /* 7.2. Vector Load/Store Addressing Modes. If the vector offset elements are narrower than XLEN, they are @@ -3796,6 +3795,17 @@ cmp_lmul_gt_one (machine_mode mode) return false; } +/* Return true if the gather/scatter offset is valid. */ +bool +gather_scatter_valid_offset_p (machine_mode offset_mode) +{ + machine_mode new_offset_mode; + if (get_vector_mode (Pmode, GET_MODE_NUNITS (offset_mode)) + .exists (&new_offset_mode)) + return true; + return false; +} + /* We don't have to convert the floating point to integer when the mantissa is zero. Thus, ther will be a limitation for both the single and double precision floating point. There will be no diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/gather-scatter/offset_extend-1.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/gather-scatter/offset_extend-1.c new file mode 100644 index 00000000000..b7936eb749a --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/gather-scatter/offset_extend-1.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-march=rv64gcv -mabi=lp64d" } */ + +void +f (int *restrict y, int *restrict x, int *restrict indices, int n) +{ + for (int i = 0; i < n; ++i) + y[i] = x[indices[i]] + 1; +} + +/* { dg-final { scan-assembler {vluxei64\.v} } } */ +/* { dg-final { scan-assembler {vsll\.vi} } } */ +/* { dg-final { scan-assembler {vsext\.vf2} } } */ +/* { dg-final { scan-assembler-not {vluxei32\.v} } } */