From patchwork Fri Dec 1 15:21:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Dapp X-Patchwork-Id: 81120 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCE083858415 for ; Fri, 1 Dec 2023 15:21:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 49EBE385841F for ; Fri, 1 Dec 2023 15:21:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 49EBE385841F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 49EBE385841F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701444075; cv=none; b=LgnerjENqaDFGLmXZXw6SYzPoLxUEaKoxn0T18PKc5ilrsrPWHUr26Jca+X1H5nPY+oPMbE7k2b3oFpEaLNFxk+OTCMylKClvOog5rdC2z7MyB7Dz1mDY+sFHs6PrzZ7SqpxrImhzFolDgnDK+EVw6EObUjJhRclLp//R8FoYwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701444075; c=relaxed/simple; bh=sZ0azRzfVyHj3vEpzRHMAlxewdF44PqJ+zlem8EjlkQ=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=FZRULWAKb5JO8Uez6f6ZJBLnbb665OwDk5xGJSMR3HWetuhA4H34zTBV3EJn2PahJg7Ucy3r3qiTocGQC+VYVZUbaNhuZS2ohSU7X4QrUOYYHnHMwZG7f7zqWf17wf41sJhHva1gTe+LFUmljRGGBPaTtGov0TiRAcwQQ8QfHuc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a1975fe7befso174499066b.2 for ; Fri, 01 Dec 2023 07:21:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701444071; x=1702048871; darn=gcc.gnu.org; h=content-transfer-encoding:subject:from:to:content-language:cc :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=8Y9353088w8C91l+b0FNuwAkG+XGabSuk3hgZMG9pUw=; b=RIYH+VsG8Wlh28McnqTjJ3cKdytPU2snOix7YTJ6ZUQAC7vnMVRfdles+MU5OJsOhq rWm4LpfnWnkm2Ak//+HYwvDTUqqZT+QPB8+PD8ST678jZ7M/Dwu0PJ8G900yWOXAHl2f 4cV09fzB91P1VobNnYeUFuLX3oSD5OGZlQIMBmzjacHkjnb26GJKPm0lsAVTyr8LSx8C q4ejBD1kBmb7lK92CpEUwn8JNPpyAJdH8a05/fEZYn7eQlXMp4bctUxSixaFJiUaXH4F lqQsrKqDk0oIgSrmyEKCk0RjKrt1NzbvFXS4T67cK9RkMNKIINjERD0SwYf/AvfWPN2e NIJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701444071; x=1702048871; h=content-transfer-encoding:subject:from:to:content-language:cc :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=8Y9353088w8C91l+b0FNuwAkG+XGabSuk3hgZMG9pUw=; b=n85dr25+/1B30XxiAwVM5gGCQXV9cACNcDofCN9jDxr+I0qzgw8TGzyrlJr87DOGsB p2Cxg+whtqE+LuQ7pvB5wNKboudIGIY08dB0sdoLVegULyhekRAwBJnPv8DoRnv8G6PN vYWYIWrhnozdo1hjapSGuV0n2MzFQrilCi1dV2NPHe3XGYnPuFnXBvzqwelxXIrsSafU yKuRLsXxBxMR63U6p9cpDpp/JjxT20w1tJ7nu7dq5ZsMyAdtFZ5bd8tGxQYMdcDS/lOD P9Bnw10pIXJJ3PIzVlFv6iBMAI1vyJ7icz8vNUz+IZ4JUGrMi6o6AuuncSgoJ9AzJ5hh SRFg== X-Gm-Message-State: AOJu0Yz+C7sUvafURMXhgtY/8LHwcYa4iVs4g6URwBS9LB7Knqtfd8z1 om2oQFavLl77hCSPJlv1LhNmwjvs/Xk= X-Google-Smtp-Source: AGHT+IHDRoB7bipsLrvZrkwoNz96TNvrEjsiVEmHM2cAqvD7zkbU323cF44C/jUTgTMmzysXQPptYQ== X-Received: by 2002:a17:906:190:b0:a19:a19a:eaa9 with SMTP id 16-20020a170906019000b00a19a19aeaa9mr774043ejb.98.1701444070991; Fri, 01 Dec 2023 07:21:10 -0800 (PST) Received: from [192.168.1.23] (ip-149-172-150-237.um42.pools.vodafone-ip.de. [149.172.150.237]) by smtp.gmail.com with ESMTPSA id lu5-20020a170906fac500b00a18e21b4dcasm1742012ejb.126.2023.12.01.07.21.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Dec 2023 07:21:10 -0800 (PST) Message-ID: <10a0156e-6bbb-4832-9a3c-350a99f3fa46@gmail.com> Date: Fri, 1 Dec 2023 16:21:10 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: rdapp.gcc@gmail.com Content-Language: en-US To: gcc-patches , palmer , Kito Cheng , jeffreyalaw , "juzhe.zhong@rivai.ai" From: Robin Dapp Subject: [PATCH] RISC-V: Rename and unify stringop strategy handling [NFC]. X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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+patchwork=sourceware.org@gcc.gnu.org Hi, now split into multiple patches. In preparation for the vectorized strlen and strcmp support this NFC patch unifies the stringop strategy handling a bit. The "auto" strategy now is a combination of scalar and vector and an expander should try the strategies in their preferred order. For the block_move expander this patch does just that. Regards Robin gcc/ChangeLog: * config/riscv/riscv-opts.h (enum riscv_stringop_strategy_enum): Rename... (enum stringop_strategy_enum): ... to this. * config/riscv/riscv-string.cc (riscv_expand_block_move): New wrapper expander handling the strategies and delegation. (riscv_expand_block_move_scalar): Rename function and make static. (expand_block_move): Remove strategy handling. * config/riscv/riscv.md: Call expander wrapper. * config/riscv/riscv.opt: Rename. --- gcc/config/riscv/riscv-opts.h | 18 ++-- gcc/config/riscv/riscv-string.cc | 92 +++++++++++-------- gcc/config/riscv/riscv.md | 4 +- gcc/config/riscv/riscv.opt | 18 ++-- .../riscv/rvv/base/cpymem-strategy-1.c | 2 +- .../riscv/rvv/base/cpymem-strategy-2.c | 2 +- .../riscv/rvv/base/cpymem-strategy-3.c | 2 +- .../riscv/rvv/base/cpymem-strategy-4.c | 2 +- .../riscv/rvv/base/cpymem-strategy-5.c | 2 +- 9 files changed, 78 insertions(+), 64 deletions(-) diff --git a/gcc/config/riscv/riscv-opts.h b/gcc/config/riscv/riscv-opts.h index e6e55ad7071..30efebbf07b 100644 --- a/gcc/config/riscv/riscv-opts.h +++ b/gcc/config/riscv/riscv-opts.h @@ -104,15 +104,15 @@ enum riscv_entity }; /* RISC-V stringop strategy. */ -enum riscv_stringop_strategy_enum { - /* Use scalar or vector instructions. */ - USE_AUTO, - /* Always use a library call. */ - USE_LIBCALL, - /* Only use scalar instructions. */ - USE_SCALAR, - /* Only use vector instructions. */ - USE_VECTOR +enum stringop_strategy_enum { + /* No expansion. */ + STRATEGY_LIBCALL = 1, + /* Use scalar expansion if possible. */ + STRATEGY_SCALAR = 2, + /* Only vector expansion if possible. */ + STRATEGY_VECTOR = 4, + /* Use any. */ + STRATEGY_AUTO = STRATEGY_SCALAR | STRATEGY_VECTOR }; #define TARGET_ZICOND_LIKE (TARGET_ZICOND || (TARGET_XVENTANACONDOPS && TARGET_64BIT)) diff --git a/gcc/config/riscv/riscv-string.cc b/gcc/config/riscv/riscv-string.cc index 80e3b5981af..f3a4d3ddd47 100644 --- a/gcc/config/riscv/riscv-string.cc +++ b/gcc/config/riscv/riscv-string.cc @@ -707,51 +707,68 @@ riscv_block_move_loop (rtx dest, rtx src, unsigned HOST_WIDE_INT length, /* Expand a cpymemsi instruction, which copies LENGTH bytes from memory reference SRC to memory reference DEST. */ -bool -riscv_expand_block_move (rtx dest, rtx src, rtx length) +static bool +riscv_expand_block_move_scalar (rtx dest, rtx src, rtx length) { - if (riscv_memcpy_strategy == USE_LIBCALL - || riscv_memcpy_strategy == USE_VECTOR) + if (!CONST_INT_P (length)) return false; - if (CONST_INT_P (length)) - { - unsigned HOST_WIDE_INT hwi_length = UINTVAL (length); - unsigned HOST_WIDE_INT factor, align; + unsigned HOST_WIDE_INT hwi_length = UINTVAL (length); + unsigned HOST_WIDE_INT factor, align; - align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD); - factor = BITS_PER_WORD / align; + align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD); + factor = BITS_PER_WORD / align; - if (optimize_function_for_size_p (cfun) - && hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false)) - return false; + if (optimize_function_for_size_p (cfun) + && hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false)) + return false; - if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor)) + if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor)) + { + riscv_block_move_straight (dest, src, INTVAL (length)); + return true; + } + else if (optimize && align >= BITS_PER_WORD) + { + unsigned min_iter_words + = RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD; + unsigned iter_words = min_iter_words; + unsigned HOST_WIDE_INT bytes = hwi_length; + unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD; + + /* Lengthen the loop body if it shortens the tail. */ + for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++) { - riscv_block_move_straight (dest, src, INTVAL (length)); - return true; + unsigned cur_cost = iter_words + words % iter_words; + unsigned new_cost = i + words % i; + if (new_cost <= cur_cost) + iter_words = i; } - else if (optimize && align >= BITS_PER_WORD) - { - unsigned min_iter_words - = RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD; - unsigned iter_words = min_iter_words; - unsigned HOST_WIDE_INT bytes = hwi_length; - unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD; - - /* Lengthen the loop body if it shortens the tail. */ - for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++) - { - unsigned cur_cost = iter_words + words % iter_words; - unsigned new_cost = i + words % i; - if (new_cost <= cur_cost) - iter_words = i; - } - riscv_block_move_loop (dest, src, bytes, iter_words * UNITS_PER_WORD); - return true; - } + riscv_block_move_loop (dest, src, bytes, iter_words * UNITS_PER_WORD); + return true; + } + + return false; +} + +/* This function delegates block-move expansion to either the vector + implementation or the scalar one. Return TRUE if successful or FALSE + otherwise. */ + +bool +riscv_expand_block_move (rtx dest, rtx src, rtx length) +{ + if (TARGET_VECTOR && stringop_strategy & STRATEGY_VECTOR) + { + bool ok = riscv_vector::expand_block_move (dest, src, length); + if (ok) + return true; } + + if (stringop_strategy & STRATEGY_SCALAR) + return riscv_expand_block_move_scalar (dest, src, length); + return false; } @@ -777,9 +794,8 @@ expand_block_move (rtx dst_in, rtx src_in, rtx length_in) bnez a2, loop # Any more? ret # Return */ - if (!TARGET_VECTOR || riscv_memcpy_strategy == USE_LIBCALL - || riscv_memcpy_strategy == USE_SCALAR) - return false; + gcc_assert (TARGET_VECTOR); + HOST_WIDE_INT potential_ew = (MIN (MIN (MEM_ALIGN (src_in), MEM_ALIGN (dst_in)), BITS_PER_WORD) / BITS_PER_UNIT); diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 6bf2dfdf9b4..ad5f1691295 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -2336,9 +2336,7 @@ (define_expand "cpymem" (use (match_operand:SI 3 "const_int_operand"))])] "" { - if (riscv_vector::expand_block_move (operands[0], operands[1], operands[2])) - DONE; - else if (riscv_expand_block_move (operands[0], operands[1], operands[2])) + if (riscv_expand_block_move (operands[0], operands[1], operands[2])) DONE; else FAIL; diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt index 0c6517bdc8b..0e569d35760 100644 --- a/gcc/config/riscv/riscv.opt +++ b/gcc/config/riscv/riscv.opt @@ -536,21 +536,21 @@ Enable the use of vector registers for function arguments and return value. This is an experimental switch and may be subject to change in the future. Enum -Name(riscv_stringop_strategy) Type(enum riscv_stringop_strategy_enum) -Valid arguments to -mmemcpy-strategy=: +Name(stringop_strategy) Type(enum stringop_strategy_enum) +Valid arguments to -mstringop-strategy=: EnumValue -Enum(riscv_stringop_strategy) String(auto) Value(USE_AUTO) +Enum(stringop_strategy) String(auto) Value(STRATEGY_AUTO) EnumValue -Enum(riscv_stringop_strategy) String(libcall) Value(USE_LIBCALL) +Enum(stringop_strategy) String(libcall) Value(STRATEGY_LIBCALL) EnumValue -Enum(riscv_stringop_strategy) String(scalar) Value(USE_SCALAR) +Enum(stringop_strategy) String(scalar) Value(STRATEGY_SCALAR) EnumValue -Enum(riscv_stringop_strategy) String(vector) Value(USE_VECTOR) +Enum(stringop_strategy) String(vector) Value(STRATEGY_VECTOR) -mmemcpy-strategy= -Target RejectNegative Joined Enum(riscv_stringop_strategy) Var(riscv_memcpy_strategy) Init(USE_AUTO) -Specify memcpy expansion strategy. +mstringop-strategy= +Target RejectNegative Joined Enum(stringop_strategy) Var(stringop_strategy) Init(STRATEGY_AUTO) +Specify stringop expansion strategy. diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-1.c b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-1.c index ae49706dca5..adad5abcef2 100644 --- a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-1.c +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-1.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=libcall" } */ +/* { dg-options "-march=rv32gcv -mabi=ilp32d -mstringop-strategy=libcall" } */ #include "cpymem-strategy.h" diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-2.c b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-2.c index 73ffc5783d0..7a7c97d560c 100644 --- a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-2.c +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-2.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=scalar" } */ +/* { dg-options "-O2 -march=rv32gcv -mabi=ilp32d -mstringop-strategy=scalar" } */ #include "cpymem-strategy.h" diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-3.c b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-3.c index 44f5f783962..83e5a837730 100644 --- a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-3.c +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-3.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=vector" } */ +/* { dg-options "-march=rv32gcv -mabi=ilp32d -mstringop-strategy=vector" } */ #include "cpymem-strategy.h" diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-4.c b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-4.c index 8056895334a..800549c8556 100644 --- a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-4.c +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-4.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=auto" } */ +/* { dg-options "-march=rv32gcv -mabi=ilp32d -mstringop-strategy=auto" } */ #include "cpymem-strategy.h" diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-5.c b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-5.c index 82ecab04a40..134fd2e9e9b 100644 --- a/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-5.c +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-5.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-march=rv32gc -mabi=ilp32d -mmemcpy-strategy=vector" } */ +/* { dg-options "-march=rv32gc -mabi=ilp32d -mstringop-strategy=vector" } */ #include "cpymem-strategy.h"