public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Christoph Muellner <christoph.muellner@vrull.eu>,
	gcc-patches@gcc.gnu.org, Kito Cheng <kito.cheng@sifive.com>,
	Jim Wilson <jim.wilson.gcc@gmail.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Andrew Waterman <andrew@sifive.com>,
	Philipp Tomsich <philipp.tomsich@vrull.eu>,
	Vineet Gupta <vineetg@rivosinc.com>
Subject: Re: [PATCH v2 2/2] riscv: Add support for str(n)cmp inline expansion
Date: Mon, 11 Sep 2023 21:34:23 -0600	[thread overview]
Message-ID: <aaae9cfd-854b-461c-a75c-c23165043503@gmail.com> (raw)
In-Reply-To: <20230906160734.2422522-3-christoph.muellner@vrull.eu>



On 9/6/23 10:07, Christoph Muellner wrote:
> From: Christoph Müllner <christoph.muellner@vrull.eu>
> 
> This patch implements expansions for the cmpstrsi and cmpstrnsi
> builtins for RV32/RV64 for xlen-aligned strings if Zbb or XTheadBb
> instructions are available.  The expansion basically emits a comparison
> sequence which compares XLEN bits per step if possible.
> 
> This allows to inline calls to strcmp() and strncmp() if both strings
> are xlen-aligned.  For strncmp() the length parameter needs to be known.
> The benefits over calls to libc are:
> * no call/ret instructions
> * no stack frame allocation
> * no register saving/restoring
> * no alignment tests
> 
> The inlining mechanism is gated by a new switches ('-minline-strcmp' and
> '-minline-strncmp') and by the variable 'optimize_size'.
> The amount of emitted unrolled loop iterations can be controlled by the
> parameter '--param=riscv-strcmp-inline-limit=N', which defaults to 64.
> 
> The comparision sequence is inspired by the strcmp example
> in the appendix of the Bitmanip specification (incl. the fast
> result calculation in case the first word does not contain
> a NULL byte).  Additional inspiration comes from rs6000-string.c.
> 
> The emitted sequence is not triggering any readahead pagefault issues,
> because only aligned strings are accessed by aligned xlen-loads.
> 
> This patch has been tested using the glibc string tests on QEMU:
> * rv64gc_zbb/rv64gc_xtheadbb with riscv-strcmp-inline-limit=64
> * rv64gc_zbb/rv64gc_xtheadbb with riscv-strcmp-inline-limit=8
> * rv32gc_zbb/rv32gc_xtheadbb with riscv-strcmp-inline-limit=64
> 
> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
> 
> gcc/ChangeLog:
> 
> 	* config/riscv/bitmanip.md (*<optab>_not<mode>): Export INSN name.
> 	(<optab>_not<mode>3): Likewise.
> 	* config/riscv/riscv-protos.h (riscv_expand_strcmp): New
> 	prototype.
> 	* config/riscv/riscv-string.cc (GEN_EMIT_HELPER3): New helper
> 	macros.
> 	(GEN_EMIT_HELPER2): Likewise.
> 	(emit_strcmp_scalar_compare_byte): New function.
> 	(emit_strcmp_scalar_compare_subword): Likewise.
> 	(emit_strcmp_scalar_compare_word): Likewise.
> 	(emit_strcmp_scalar_load_and_compare): Likewise.
> 	(emit_strcmp_scalar_call_to_libc): Likewise.
> 	(emit_strcmp_scalar_result_calculation_nonul): Likewise.
> 	(emit_strcmp_scalar_result_calculation): Likewise.
> 	(riscv_expand_strcmp_scalar): Likewise.
> 	(riscv_expand_strcmp): Likewise.
> 	* config/riscv/riscv.md (*slt<u>_<X:mode><GPR:mode>): Export
> 	INSN name.
> 	(@slt<u>_<X:mode><GPR:mode>3): Likewise.
> 	(cmpstrnsi): Invoke expansion function for str(n)cmp.
> 	(cmpstrsi): Likewise.
> 	* config/riscv/riscv.opt: Add new parameter
> 	'-mstring-compare-inline-limit'.
> 	* doc/invoke.texi: Document new parameter
> 	'-mstring-compare-inline-limit'.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/riscv/xtheadbb-strcmp-unaligned.c: New test.
> 	* gcc.target/riscv/xtheadbb-strcmp.c: New test.
> 	* gcc.target/riscv/zbb-strcmp-disabled-2.c: New test.
> 	* gcc.target/riscv/zbb-strcmp-disabled.c: New test.
> 	* gcc.target/riscv/zbb-strcmp-unaligned.c: New test.
> 	* gcc.target/riscv/zbb-strcmp.c: New test.
OK for the trunk.  THanks for pushing this along.

jeff

  reply	other threads:[~2023-09-12  3:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-06 16:07 [PATCH v2 0/2] riscv: Introduce strlen/strcmp/strncmp " Christoph Muellner
2023-09-06 16:07 ` [PATCH v2 1/2] riscv: Add support for strlen " Christoph Muellner
2023-09-06 16:22   ` Palmer Dabbelt
2023-09-06 16:47     ` Jeff Law
2023-09-06 19:29       ` Palmer Dabbelt
2023-09-12  3:28   ` Jeff Law
2023-09-12  9:38   ` Philipp Tomsich
2023-09-06 16:07 ` [PATCH v2 2/2] riscv: Add support for str(n)cmp " Christoph Muellner
2023-09-12  3:34   ` Jeff Law [this message]
2023-09-12  9:38     ` Philipp Tomsich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aaae9cfd-854b-461c-a75c-c23165043503@gmail.com \
    --to=jeffreyalaw@gmail.com \
    --cc=andrew@sifive.com \
    --cc=christoph.muellner@vrull.eu \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jim.wilson.gcc@gmail.com \
    --cc=kito.cheng@sifive.com \
    --cc=palmer@dabbelt.com \
    --cc=philipp.tomsich@vrull.eu \
    --cc=vineetg@rivosinc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).