From patchwork Wed Jan 10 09:58:28 2024 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: 186748 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp687882dyi; Wed, 10 Jan 2024 01:59:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHcvtATUY+1TcK+yJl0hTQgjDKZv0I4UEsqncJQM/PNCMNr/h9R3ilRqEOvksBhIhbYLRCF X-Received: by 2002:a1f:eec3:0:b0:4b6:ccd0:cfa3 with SMTP id m186-20020a1feec3000000b004b6ccd0cfa3mr330416vkh.20.1704880765706; Wed, 10 Jan 2024 01:59:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704880765; cv=pass; d=google.com; s=arc-20160816; b=CwC5zXVbfvYEvdTe5cClxOvbd1i4qvKOXzKlG2e5fpXpIbz6ceVmnr2yB0iaZ9JdFo ls5opQENVHz+uE348NIm/ptnXq8A8S9SIq4L0P5ORYbInFQ17h+BBDTrO86ZYKPkFTs2 jwwtx1OcUZFDBXnH81avaRMHCyT9vnMIF1ENsnSxS3H2bH1w7Uihafp1oOxq106HrKOn zcUWRY681BF4a9zhriEvoYvPPLr3xLmsA6ZD3QsFqF2uMqWkFEdT0gDOJzzPG8O3UD/x KAG5xKleswRIzY3Rt6g0N0CnF/B4KME6KpqR2iCvh8QvMyjBteOzniMd1hUwcv61lW3b OH7g== 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=ECP4YIaN6HFYU23LCGdaKOsqwHs72e0asB4CGBNeIHg=; fh=12MRPJmZ1mgDpHqWoogMKqnaGRGM2b7lcuJroqfjJiw=; b=PxBc+dzgrzJNj+L29HoiK1o/conXUWvLEVRBe2xgxdYIvSn73lUfLLbL/D4gwbDam8 DHZMwgPgx93rqdiF4eyXdugPMl5YDJmlDXKvXvNkxdOfryFHIb7LyqsT04Tj9yw6CdT5 ufDAfIokPc9f6jBTCBo8qDVP6Q9gtN4O1qLpOaWHwP62vu1VmFZi43iVroWlAbOjFdRB yu/u8C5M5PHAGEIb+h0+QJ3xfEcaqdrKP2CZ3wtvD7uNGQwDq7/C3IAwuHR75YBANwUI RmvSoW31xVS9yZQlICeOcK62pSi+0mypgGLDJR1P3BMhtAfxojeGwutVqGfHvS50o5qE BvuQ== 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 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ay18-20020a05622a229200b004299200ed9bsi3780148qtb.370.2024.01.10.01.59.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 01:59:25 -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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6677438582B3 for ; Wed, 10 Jan 2024 09:59:25 +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 6A510385840A for ; Wed, 10 Jan 2024 09:58:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6A510385840A 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 6A510385840A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=54.204.34.129 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704880718; cv=none; b=LlvDOOtztip70TAMjWcB51iLsFnc6IVKIKTwKngDN4xJoB09KOLBDImHjHCAhtII0ek1daYSnsJa91xNTx8pgkYYVcfBz/oIgFMu7Qzd5WO4zcw2oNqv62hQ+fnVMZHgugOlfSJFWQ9WCQ7F2EkurmSK44b9oVpUy5cbOnPTUY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704880718; c=relaxed/simple; bh=Hnq5wJiGnL6GULYTRubCm6NCnrTvwjdpRc4dGGoj9sI=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=kIra42p3mtYMkVw3Gx+vHm8gR+/NPMfw6H9eDmZWUygNqFDSGf2gX8rKT4ZjvgFjMpGAjr07ZMmJ5CSIP1//jqEY5pQAviWX+jZiHCbfdzFKTWu6MA7Z0mYk/ffaKtGyZu73FpOj3i9Zir1YMt8ZJlpfH/hDCqgYli3OMHPX5ec= ARC-Authentication-Results: i=1; server2.sourceware.org X-QQ-mid: bizesmtp75t1704880710t6jpx5p0 Received: from rios-cad121.hadoop.rioslab.org ( [58.60.1.9]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 10 Jan 2024 17:58:29 +0800 (CST) X-QQ-SSF: 01400000000000G0V000000A0000000 X-QQ-FEAT: zT6n3Y95oi1K9H9dbHt0NjmrbUNA08e2vFWZ/pxWifLy7VNX+D6dPj/dGuJLh mSzCGSAOe60Hm6o9L4p8nYDhG2RE3r7dsJudsk7RRRSMsAncuz1WBKBtVfI6SE1t4DUMdlX 7r6Z5YS/Otls9qjhbsvmARMh/5hcHTe9vAIeVWzDVQQPfLhNR4qIYFYCXGg8WHNVFt5Wlu3 e4iSdOiIUQyUn7V/1th7jSsoCd0RBCdP8+jd/87Z2DdOcKB2+vKI/egZGwUQDmCsHfhKmaC exOBLDW7zR2Cp2Y0eG1ro+D2c7NxerKCxEYS9lQmk3/lsT93peiL6b/QlbijSBhz3/cNrDX pNLiqiAWJnjZNT+kAi3jCQPs8moIpvo3xNtNEukx+5yQR3zH6Ey59btox3DaawY+wBlZCyz r7TslhPoZFI= X-QQ-GoodBg: 2 X-BIZMAIL-ID: 1038447651381228920 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: Switch RVV cost model to generic vector cost model Date: Wed, 10 Jan 2024 17:58:28 +0800 Message-Id: <20240110095828.3863165-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=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_BLACK 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: 1787697053816718305 X-GMAIL-MSGID: 1787697053816718305 This patch is preparing patch for the following cost model tweak. Since we don't have vector cost model in default tune info (rocket), we set the cost model default as generic cost model by default. The reason we want to switch to generic vector cost model is the default cost model generates inferior codegen for various benchmarks. For example, PR113247, we have performance bug that we end up having over 70% performance drop of SHA256. Currently, no matter how we adapt cost model, we are not able to fix the performance bug since we always use default cost model by default. Also, tweak the generic cost model back to default cost model since we have some FAILs in current tests. After this patch, we (me an Robin) can work on cost model tunning together to improve performane in various benchmarks. Tested on both RV32 and RV64, ok for trunk ? gcc/ChangeLog: * config/riscv/riscv.cc (get_common_costs): Switch RVV cost model. (get_vector_costs): Ditto. (riscv_builtin_vectorization_cost): Ditto. --- gcc/config/riscv/riscv.cc | 117 ++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 32183d63180..d72058039ce 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -358,13 +358,13 @@ static const common_vector_cost generic_vls_vector_cost = { 1, /* fp_stmt_cost */ 1, /* gather_load_cost */ 1, /* scatter_store_cost */ - 2, /* vec_to_scalar_cost */ + 1, /* vec_to_scalar_cost */ 1, /* scalar_to_vec_cost */ - 2, /* permute_cost */ + 1, /* permute_cost */ 1, /* align_load_cost */ 1, /* align_store_cost */ - 1, /* unalign_load_cost */ - 1, /* unalign_store_cost */ + 2, /* unalign_load_cost */ + 2, /* unalign_store_cost */ }; /* Generic costs for VLA vector operations. */ @@ -374,13 +374,13 @@ static const scalable_vector_cost generic_vla_vector_cost = { 1, /* fp_stmt_cost */ 1, /* gather_load_cost */ 1, /* scatter_store_cost */ - 2, /* vec_to_scalar_cost */ + 1, /* vec_to_scalar_cost */ 1, /* scalar_to_vec_cost */ - 2, /* permute_cost */ + 1, /* permute_cost */ 1, /* align_load_cost */ 1, /* align_store_cost */ - 1, /* unalign_load_cost */ - 1, /* unalign_store_cost */ + 2, /* unalign_load_cost */ + 2, /* unalign_store_cost */ }, }; @@ -10372,11 +10372,10 @@ riscv_frame_pointer_required (void) return riscv_save_frame_pointer && !crtl->is_leaf; } -/* Return the appropriate common costs for vectors of type VECTYPE. */ +/* Return the appropriate common costs according to VECTYPE from COSTS. */ static const common_vector_cost * -get_common_costs (tree vectype) +get_common_costs (const cpu_vector_cost *costs, tree vectype) { - const cpu_vector_cost *costs = tune_param->vec_costs; gcc_assert (costs); if (vectype && riscv_v_ext_vls_mode_p (TYPE_MODE (vectype))) @@ -10384,78 +10383,84 @@ get_common_costs (tree vectype) return costs->vla; } +/* Return the CPU vector costs according to -mtune if tune info has non-NULL + vector cost. Otherwide, return the default generic vector costs. */ +static const cpu_vector_cost * +get_vector_costs () +{ + const cpu_vector_cost *costs = tune_param->vec_costs; + if (!costs) + return &generic_vector_cost; + return costs; +} + /* Implement targetm.vectorize.builtin_vectorization_cost. */ static int riscv_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost, tree vectype, int misalign ATTRIBUTE_UNUSED) { - unsigned elements; - const cpu_vector_cost *costs = tune_param->vec_costs; + const cpu_vector_cost *costs = get_vector_costs (); bool fp = false; if (vectype != NULL) fp = FLOAT_TYPE_P (vectype); - if (costs != NULL) + const common_vector_cost *common_costs = get_common_costs (costs, vectype); + gcc_assert (common_costs != NULL); + switch (type_of_cost) { - const common_vector_cost *common_costs = get_common_costs (vectype); - gcc_assert (common_costs != NULL); - switch (type_of_cost) - { - case scalar_stmt: - return fp ? costs->scalar_fp_stmt_cost : costs->scalar_int_stmt_cost; + case scalar_stmt: + return fp ? costs->scalar_fp_stmt_cost : costs->scalar_int_stmt_cost; - case scalar_load: - return costs->scalar_load_cost; + case scalar_load: + return costs->scalar_load_cost; - case scalar_store: - return costs->scalar_store_cost; + case scalar_store: + return costs->scalar_store_cost; - case vector_stmt: - return fp ? common_costs->fp_stmt_cost : common_costs->int_stmt_cost; + case vector_stmt: + return fp ? common_costs->fp_stmt_cost : common_costs->int_stmt_cost; - case vector_load: - return common_costs->align_load_cost; + case vector_load: + return common_costs->align_load_cost; - case vector_store: - return common_costs->align_store_cost; + case vector_store: + return common_costs->align_store_cost; - case vec_to_scalar: - return common_costs->vec_to_scalar_cost; + case vec_to_scalar: + return common_costs->vec_to_scalar_cost; - case scalar_to_vec: - return common_costs->scalar_to_vec_cost; + case scalar_to_vec: + return common_costs->scalar_to_vec_cost; - case unaligned_load: - return common_costs->unalign_load_cost; - case vector_gather_load: - return common_costs->gather_load_cost; + case unaligned_load: + return common_costs->unalign_load_cost; + case vector_gather_load: + return common_costs->gather_load_cost; - case unaligned_store: - return common_costs->unalign_store_cost; - case vector_scatter_store: - return common_costs->scatter_store_cost; + case unaligned_store: + return common_costs->unalign_store_cost; + case vector_scatter_store: + return common_costs->scatter_store_cost; - case cond_branch_taken: - return costs->cond_taken_branch_cost; + case cond_branch_taken: + return costs->cond_taken_branch_cost; - case cond_branch_not_taken: - return costs->cond_not_taken_branch_cost; + case cond_branch_not_taken: + return costs->cond_not_taken_branch_cost; - case vec_perm: - return common_costs->permute_cost; + case vec_perm: + return common_costs->permute_cost; - case vec_promote_demote: - return fp ? common_costs->fp_stmt_cost : common_costs->int_stmt_cost; + case vec_promote_demote: + return fp ? common_costs->fp_stmt_cost : common_costs->int_stmt_cost; - case vec_construct: - elements = estimated_poly_value (TYPE_VECTOR_SUBPARTS (vectype)); - return elements / 2 + 1; + case vec_construct: + return estimated_poly_value (TYPE_VECTOR_SUBPARTS (vectype)) - 1; - default: - gcc_unreachable (); - } + default: + gcc_unreachable (); } return default_builtin_vectorization_cost (type_of_cost, vectype, misalign);