[PATCH v4 1/1] riscv: Add RVV memset via multiarch/IFUNC
Peter Bergner
bergner@tenstorrent.com
Thu Dec 11 00:38:28 GMT 2025
Jeff asked me to take over reviewing this series, since he was slammed
with other work, so....
> +#ifndef SHARED
> +asm ("memset = __memset_generic");
> +asm ("memcpy = __memcpy_generic");
> +#endif
The memcpy change is independent of the memset patch, so shouldn't be
included with this patch. Please submit adding the memcpy change to
this file as a separate patch.
> diff --git a/sysdeps/riscv/multiarch/memset-generic.c b/sysdeps/riscv/multiarch/memset-generic.c
> new file mode 100644
> index 0000000000..c93bb43c8f
> --- /dev/null
> +++ b/sysdeps/riscv/multiarch/memset-generic.c
> @@ -0,0 +1,26 @@
> +/* Re-include the default memset implementation.
> + Copyright (C) 2025 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <string.h>
> +
> +#if IS_IN(libc)
> +# define MEMSET __memset_generic
> +# undef libc_hidden_builtin_def
> +# define libc_hidden_builtin_def(x)
> +#endif
> +#include <string/memset.c>
In [1], Adhemerval laid out a directory structure that we should follow:
Adding the routine only to the multiarch folder would make it unselectable if
—disable-multi-arch is used, and it would always enable iFUNC even when not
strictly required (for instance, if your toolchain/system defaults to RVA23).
If you check other ABIs, like powerpc and s390x, that have some ways to optimize
the build depending on the target ISA. On RVV, I think one strategy could be:
1. Add a configure check for RVV support [1] and add a new sysdeps/riscv/rvv in
the Implies sysdep directories.
2. Add each new RVV optimize implementation to the sysdeps/riscv/rvv folder and
check if it builds with the —disable-multi-arch option.
3. Then add the ifunc variant on sysdeps/riscv/multiarch that includes the one
on sysdeps/riscv/rvv.
4. And you can optimize it to just enable the iFUNC variant if the compiler
targets an ABI variant without RVV support (assuming that RVV is always
faster than the default one).
I know in [2], Jeff said to "defer the non-multiarch stuff temporarily", but
we should still place the files where they should eventually end up when
supporting non-multiarch builds, so we don't have to move them later.
In addition, the preconfigure code change Jeff said to defer is (partially)
already upstream as part of my change to add the vector registers to the
__SYSCALL_CLOBBERS macro [3], so I don't think adding non-multiarch support
is going to be that much extra work. Therefore, I'd like to at least try to
add that now.
> @@ -3,6 +3,9 @@ sysdep_routines += \
> memcpy \
> memcpy-generic \
> memcpy_noalignment \
> + memset \
> + memset-generic \
> + memset_vector \
> # sysdep_routines
Let's use a common file naming scheme, so how about renaming memset_vector.S
to memset-vector.S to match the memset-generic and memcpy-generic file names.
I know the memcpy_noalignment is named that way, but let's choose a scheme
and stick to it.
So the takeaway is, memset-generic.c and memset-vector.S should be moved to
sysdeps/riscv/rvv/ and the sysdeps/riscv/multiarch/ versions should
#include those, similar to how s390/powerpc do.
Then in the .../riscv/multiarch/memset.c file, we can check for whether
__riscv_v is defined (we're guaranteed that __riscv_v implies RVV1.0,
since the preconfigure fragment I added disallows RVV0.7) and forces
always using the RVV variant. If __riscv_v is false, then setup the
ifunc as in your patch.
Is that clear what needs to be done? If not, please reach out.
I can also help cobble this together too if you need help or get
stuck.
Hopefully Adhemerval will keep me honest on how this redirection all works! :-)
Peter
[1] https://sourceware.org/pipermail/libc-alpha/2025-October/170945.html
[2] https://sourceware.org/pipermail/libc-alpha/2025-October/171563.html
[3] https://sourceware.org/pipermail/libc-alpha/2025-September/169804.html
More information about the Libc-alpha
mailing list