public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs
@ 2024-05-27 11:18 Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 01/15] cdefs: Add mechanism to add attributes to __always_inline functions Christoph Müllner
                   ` (16 more replies)
  0 siblings, 17 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

Glibc recently got hwprobe() support for RISC-V, which allows querying
avaiable extensions at runtime.  On top of that an optimized memcpy()
routine (for fast unaligned accesses) has been merged, which is built by
recompiling the generic C code with a different compiler flag.  An ifunc
resolver then detects which routine should be run using hwprobe().

This patchset follows this idea and recompiles the following functions
for Zbb (via function attributes) and enables the existing Zbb/orc.b
optimization in riscv/string-fza.h:
memchr, memrchr, strchrnul, strcmp, strlen, strncmp.
The resulting optimized routines are then selected by the resolver function
if the Zbb extension is present at runtime.

To use target function attributes, a few issues had to be resovled:
- The functions above got a mechanism to be compiled with function attributes
  (patches 2-7).  Only those routines have been touched, which are
  required for the purpose of this patchset.
- Ensuring that inlined functions also get the same function attributes
  (first patch).
- Add mechanism to explicitly enable the orc.b optimization for string functions
  (patch 8), which is a bit inspired by USE_FFS_BUILTIN.

One of the design questions is, if Zbb represents a broad enough optimization
target.  Tests with Zb* extensions showed, that no further code improvements
can be achieved with them.  Also most other extensions likely won't affect
the generated code for string routines (ignoring vector instructions, which
are a different topic).  Therefore, Zbb seemed like a sufficient target.

This series was tested by writing a simple test program to invoke the
libc routines (e.g. strcmp) and a modified QEMU that reports the
emulation of orc.b on stderr.  With that the QEMU can be used to test
if the optimized routines are executed (-cpu "rv64,zbb=[false,true]").
Further, this series was tested with SPEC CPU 2017 intrate with Zbb
enabled.  The function attribute detection mechanism was tested with
GCC 13 and GCC 14.

Changes in v2:
- Drop "Use .insn directive form for orc.b"
- Introduce use of target function attribute (and all depenendcies)
- Introduce detection of target function attribute support
- Make orc.b optimization explicit
- Small cleanups

Christoph Müllner (15):
  cdefs: Add mechanism to add attributes to __always_inline functions
  string/memchr: Add mechanism to set function attributes
  string/memrchr: Add mechanism to set function attributes
  string/strchrnul: Add mechanism to set function attributes
  string/strcmp: Add mechanism to set function attributes
  string/strlen: Add mechanism to set function attributes
  string/strncmp: Add mechanism to set function attributes
  RISC-V: string-fz[a,i].h: Make orc.b optimization explicit
  RISC-V: Add compiler test for Zbb function attribute support
  RISC-V: Add Zbb optimized memchr as ifunc
  RISC-V: Add Zbb optimized memrchr as ifunc
  RISC-V: Add Zbb optimized strchrnul as ifunc
  RISC-V: Add Zbb optimized strcmp as ifunc
  RISC-V: Add Zbb optimized strlen as ifunc
  RISC-V: Add Zbb optimized strncmp as ifunc

 config.h.in                                   |  3 +
 misc/sys/cdefs.h                              |  8 ++-
 string/memchr.c                               |  5 ++
 string/memrchr.c                              |  5 ++
 string/strchrnul.c                            |  5 ++
 string/strcmp.c                               |  8 +++
 string/strlen.c                               |  5 ++
 string/strncmp.c                              |  8 +++
 sysdeps/riscv/configure                       | 27 ++++++++
 sysdeps/riscv/configure.ac                    | 18 +++++
 sysdeps/riscv/multiarch/memchr-generic.c      | 24 +++++++
 sysdeps/riscv/multiarch/memchr-zbb.c          | 23 +++++++
 sysdeps/riscv/multiarch/memrchr-generic.c     | 24 +++++++
 sysdeps/riscv/multiarch/memrchr-zbb.c         | 23 +++++++
 sysdeps/riscv/multiarch/strchrnul-generic.c   | 24 +++++++
 sysdeps/riscv/multiarch/strchrnul-zbb.c       | 23 +++++++
 sysdeps/riscv/multiarch/strcmp-generic.c      | 24 +++++++
 sysdeps/riscv/multiarch/strcmp-zbb.c          | 23 +++++++
 sysdeps/riscv/multiarch/strlen-generic.c      | 24 +++++++
 sysdeps/riscv/multiarch/strlen-zbb.c          | 23 +++++++
 sysdeps/riscv/multiarch/strncmp-generic.c     | 26 +++++++
 sysdeps/riscv/multiarch/strncmp-zbb.c         | 25 +++++++
 sysdeps/riscv/string-fza.h                    | 22 +++++-
 sysdeps/riscv/string-fzi.h                    | 20 +++++-
 .../unix/sysv/linux/riscv/multiarch/Makefile  | 23 +++++++
 .../linux/riscv/multiarch/ifunc-impl-list.c   | 67 +++++++++++++++++--
 .../unix/sysv/linux/riscv/multiarch/memchr.c  | 60 +++++++++++++++++
 .../unix/sysv/linux/riscv/multiarch/memrchr.c | 63 +++++++++++++++++
 .../sysv/linux/riscv/multiarch/strchrnul.c    | 63 +++++++++++++++++
 .../unix/sysv/linux/riscv/multiarch/strcmp.c  | 59 ++++++++++++++++
 .../unix/sysv/linux/riscv/multiarch/strlen.c  | 59 ++++++++++++++++
 .../unix/sysv/linux/riscv/multiarch/strncmp.c | 59 ++++++++++++++++
 32 files changed, 863 insertions(+), 10 deletions(-)
 create mode 100644 sysdeps/riscv/multiarch/memchr-generic.c
 create mode 100644 sysdeps/riscv/multiarch/memchr-zbb.c
 create mode 100644 sysdeps/riscv/multiarch/memrchr-generic.c
 create mode 100644 sysdeps/riscv/multiarch/memrchr-zbb.c
 create mode 100644 sysdeps/riscv/multiarch/strchrnul-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strchrnul-zbb.c
 create mode 100644 sysdeps/riscv/multiarch/strcmp-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strcmp-zbb.c
 create mode 100644 sysdeps/riscv/multiarch/strlen-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strlen-zbb.c
 create mode 100644 sysdeps/riscv/multiarch/strncmp-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strncmp-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c

-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 01/15] cdefs: Add mechanism to add attributes to __always_inline functions
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 02/15] string/memchr: Add mechanism to set function attributes Christoph Müllner
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

Contrary to LLVM, GCC does not propagate function attributes accross
inlined functions ("target specific option mismatch").  To overcome this
limiation, this patch introduces a __CODEGEN_ATTRIBUTE macro, which can
be used to add function attributes to __always_inline functions.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 misc/sys/cdefs.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index ab9620bd0d..91dceab1f2 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -587,15 +587,19 @@
 #endif
 
 /* Forces a function to be always inlined.  */
+# ifndef __CODEGEN_ATTRIBUTES
+#  define __CODEGEN_ATTRIBUTES
+# endif
 #if __GNUC_PREREQ (3,2) || __glibc_has_attribute (__always_inline__)
 /* The Linux kernel defines __always_inline in stddef.h (283d7573), and
    it conflicts with this definition.  Therefore undefine it first to
    allow either header to be included first.  */
 # undef __always_inline
-# define __always_inline __inline __attribute__ ((__always_inline__))
+# define __always_inline __inline __attribute__ ((__always_inline__)) \
+   __CODEGEN_ATTRIBUTES
 #else
 # undef __always_inline
-# define __always_inline __inline
+# define __always_inline __inline __CODEGEN_ATTRIBUTES
 #endif
 
 /* Associate error messages with the source location of the call site rather
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 02/15] string/memchr: Add mechanism to set function attributes
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 01/15] cdefs: Add mechanism to add attributes to __always_inline functions Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 03/15] string/memrchr: " Christoph Müllner
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

This patch introduces the __CODEGEN_ATTRIBUTES macro which can be
used to set function attributes when building the code.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 string/memchr.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/string/memchr.c b/string/memchr.c
index 08b5c41667..a2bcd0419e 100644
--- a/string/memchr.c
+++ b/string/memchr.c
@@ -29,6 +29,10 @@
 # define __memchr MEMCHR
 #endif
 
+#ifndef __CODEGEN_ATTRIBUTES
+# define __CODEGEN_ATTRIBUTES
+#endif
+
 static __always_inline const char *
 sadd (uintptr_t x, uintptr_t y)
 {
@@ -36,6 +40,7 @@ sadd (uintptr_t x, uintptr_t y)
 }
 
 /* Search no more than N bytes of S for C.  */
+__CODEGEN_ATTRIBUTES
 void *
 __memchr (void const *s, int c_in, size_t n)
 {
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 03/15] string/memrchr: Add mechanism to set function attributes
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 01/15] cdefs: Add mechanism to add attributes to __always_inline functions Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 02/15] string/memchr: Add mechanism to set function attributes Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 04/15] string/strchrnul: " Christoph Müllner
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

This patch introduces the __CODEGEN_ATTRIBUTES macro which can be
used to set function attributes when building the code.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 string/memrchr.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/string/memrchr.c b/string/memrchr.c
index 6511dac591..129fa9a681 100644
--- a/string/memrchr.c
+++ b/string/memrchr.c
@@ -30,6 +30,11 @@
 # define __memrchr MEMRCHR
 #endif
 
+#ifndef __CODEGEN_ATTRIBUTES
+# define __CODEGEN_ATTRIBUTES
+#endif
+
+__CODEGEN_ATTRIBUTES
 void *
 __memrchr (const void *s, int c_in, size_t n)
 {
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 04/15] string/strchrnul: Add mechanism to set function attributes
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (2 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 03/15] string/memrchr: " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 05/15] string/strcmp: " Christoph Müllner
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

This patch introduces the __CODEGEN_ATTRIBUTES macro which can be
used to set function attributes when building the code.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 string/strchrnul.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/string/strchrnul.c b/string/strchrnul.c
index f64d52ea67..feff66dddf 100644
--- a/string/strchrnul.c
+++ b/string/strchrnul.c
@@ -29,7 +29,12 @@
 # define __strchrnul STRCHRNUL
 #endif
 
+#ifndef __CODEGEN_ATTRIBUTES
+# define __CODEGEN_ATTRIBUTES
+#endif
+
 /* Find the first occurrence of C in S or the final NUL byte.  */
+__CODEGEN_ATTRIBUTES
 char *
 __strchrnul (const char *str, int c_in)
 {
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 05/15] string/strcmp: Add mechanism to set function attributes
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (3 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 04/15] string/strchrnul: " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 06/15] string/strlen: " Christoph Müllner
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

This patch introduces the __CODEGEN_ATTRIBUTES macro which can be
used to set function attributes when building the code.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 string/strcmp.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/string/strcmp.c b/string/strcmp.c
index 2bca399f52..59d7d98e5e 100644
--- a/string/strcmp.c
+++ b/string/strcmp.c
@@ -26,6 +26,11 @@
 # define strcmp STRCMP
 #endif
 
+#ifndef __CODEGEN_ATTRIBUTES
+# define __CODEGEN_ATTRIBUTES
+#endif
+
+__CODEGEN_ATTRIBUTES
 static inline int
 final_cmp (const op_t w1, const op_t w2)
 {
@@ -35,6 +40,7 @@ final_cmp (const op_t w1, const op_t w2)
 
 /* Aligned loop: if a difference is found, exit to compare the bytes.  Else
    if a zero is found we have equal strings.  */
+__CODEGEN_ATTRIBUTES
 static inline int
 strcmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1)
 {
@@ -55,6 +61,7 @@ strcmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1)
    the bytes so that we can also apply the has_zero test to see if we have
    already reached EOS.  If we have, then we can simply fall through to the
    final comparison.  */
+__CODEGEN_ATTRIBUTES
 static inline int
 strcmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs)
 {
@@ -98,6 +105,7 @@ strcmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs)
 /* Compare S1 and S2, returning less than, equal to or
    greater than zero if S1 is lexicographically less than,
    equal to or greater than S2.  */
+__CODEGEN_ATTRIBUTES
 int
 strcmp (const char *p1, const char *p2)
 {
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 06/15] string/strlen: Add mechanism to set function attributes
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (4 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 05/15] string/strcmp: " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 07/15] string/strncmp: " Christoph Müllner
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

This patch introduces the __CODEGEN_ATTRIBUTES macro which can be
used to set function attributes when building the code.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 string/strlen.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/string/strlen.c b/string/strlen.c
index a0378c500e..dcf03769d7 100644
--- a/string/strlen.c
+++ b/string/strlen.c
@@ -26,8 +26,13 @@
 # define __strlen STRLEN
 #endif
 
+#ifndef __CODEGEN_ATTRIBUTES
+# define __CODEGEN_ATTRIBUTES
+#endif
+
 /* Return the length of the null-terminated string STR.  Scan for
    the null terminator quickly by testing four bytes at a time.  */
+__CODEGEN_ATTRIBUTES
 size_t
 __strlen (const char *str)
 {
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 07/15] string/strncmp: Add mechanism to set function attributes
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (5 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 06/15] string/strlen: " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 08/15] RISC-V: string-fz[a,i].h: Make orc.b optimization explicit Christoph Müllner
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

This patch introduces the __CODEGEN_ATTRIBUTES macro which can be
used to set function attributes when building the code.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 string/strncmp.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/string/strncmp.c b/string/strncmp.c
index 67cc97542a..13c656dc2e 100644
--- a/string/strncmp.c
+++ b/string/strncmp.c
@@ -29,6 +29,11 @@
 #define STRNCMP strncmp
 #endif
 
+#ifndef __CODEGEN_ATTRIBUTES
+# define __CODEGEN_ATTRIBUTES
+#endif
+
+__CODEGEN_ATTRIBUTES
 static inline int
 final_cmp (const op_t w1, const op_t w2, size_t n)
 {
@@ -40,6 +45,7 @@ final_cmp (const op_t w1, const op_t w2, size_t n)
 
 /* Aligned loop: if a difference is found, exit to compare the bytes.  Else
    if a zero is found we have equal strings.  */
+__CODEGEN_ATTRIBUTES
 static inline int
 strncmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1, size_t n)
 {
@@ -64,6 +70,7 @@ strncmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1, size_t n)
    the bytes so that we can also apply the has_zero test to see if we have
    already reached EOS.  If we have, then we can simply fall through to the
    final comparison.  */
+__CODEGEN_ATTRIBUTES
 static inline int
 strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
 			size_t n)
@@ -110,6 +117,7 @@ strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
    returning less than, equal to or greater than zero
    if S1 is lexicographically less than, equal to or
    greater than S2.  */
+__CODEGEN_ATTRIBUTES
 int
 STRNCMP (const char *p1, const char *p2, size_t n)
 {
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 08/15] RISC-V: string-fz[a,i].h: Make orc.b optimization explicit
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (6 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 07/15] string/strncmp: " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 09/15] RISC-V: Add compiler test for Zbb function attribute support Christoph Müllner
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

RISC-V Zbb has the orc.b instruction, which can be used
in find_zero_all. T-Head has a similar instruction (th.tstnbz)
in their XTheadBb extension.

Currently, we pick the optimized code, whenever the corresponding
extension test macros are enabled (e.g. __riscv_zbb for Zbb).
However, these test macros are not set in case the extensions
are enabled via function attributes.

This patch adds a mechanism to enable the optimization explicitly
to overcome this limitation.  The old behavior (enabled optimization
via __riscv_zbb) remains the same.

By evaluating the value (instead of just checking if these macros are
defined) we could also explicitly disable the optimization by defining
the macros as 0 (e.g. #define USE_ZBB_ORCB 0).

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/string-fza.h | 22 ++++++++++++++++++++--
 sysdeps/riscv/string-fzi.h | 20 +++++++++++++++++++-
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/sysdeps/riscv/string-fza.h b/sysdeps/riscv/string-fza.h
index ee5c31317f..34d6972aea 100644
--- a/sysdeps/riscv/string-fza.h
+++ b/sysdeps/riscv/string-fza.h
@@ -19,7 +19,25 @@
 #ifndef _RISCV_STRING_FZA_H
 #define _RISCV_STRING_FZA_H 1
 
-#if defined __riscv_zbb || defined __riscv_xtheadbb
+/* Enable use of orc.b if Zbb is available.  */
+#ifndef USE_ZBB_ORCB
+# ifdef __riscv_zbb
+#  define USE_ZBB_ORCB 1
+# else
+#  define USE_ZBB_ORCB 0
+# endif
+#endif
+
+/* Enable use of th.tstnbz if XTheadBb is available.  */
+#ifndef USE_XTHEADBB_TSTNBZ
+# ifdef __riscv_xtheadbb
+#  define USE_XTHEADBB_TSTNBZ 1
+# else
+#  define USE_XTHEADBB_TSTNBZ 0
+# endif
+#endif
+
+#if USE_ZBB_ORCB || USE_XTHEADBB_TSTNBZ
 /* With bitmap extension we can use orc.b to find all zero bytes.  */
 # include <string-misc.h>
 # include <string-optype.h>
@@ -32,7 +50,7 @@ static __always_inline find_t
 find_zero_all (op_t x)
 {
   find_t r;
-#ifdef __riscv_xtheadbb
+#if USE_XTHEADBB_TSTNBZ
   asm ("th.tstnbz %0, %1" : "=r" (r) : "r" (x));
   return r;
 #else
diff --git a/sysdeps/riscv/string-fzi.h b/sysdeps/riscv/string-fzi.h
index 66237c2f03..e3c3a96d62 100644
--- a/sysdeps/riscv/string-fzi.h
+++ b/sysdeps/riscv/string-fzi.h
@@ -19,7 +19,25 @@
 #ifndef _STRING_RISCV_FZI_H
 #define _STRING_RISCV_FZI_H 1
 
-#if defined __riscv_zbb || defined __riscv_xtheadbb
+/* Enable use of orc.b if Zbb is available.  */
+#ifndef USE_ZBB_ORCB
+# ifdef __riscv_zbb
+#  define USE_ZBB_ORCB 1
+# else
+#  define USE_ZBB_ORCB 0
+# endif
+#endif
+
+/* Enable use of th.tstnbz if XTheadBb is available.  */
+#ifndef USE_XTHEADBB_TSTNBZ
+# ifdef __riscv_xtheadbb
+#  define USE_XTHEADBB_TSTNBZ 1
+# else
+#  define USE_XTHEADBB_TSTNBZ 0
+# endif
+#endif
+
+#if USE_ZBB_ORCB || USE_XTHEADBB_TSTNBZ
 # include <sysdeps/generic/string-fzi.h>
 #else
 /* Without bitmap clz/ctz extensions, it is faster to direct test the bits
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 09/15] RISC-V: Add compiler test for Zbb function attribute support
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (7 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 08/15] RISC-V: string-fz[a,i].h: Make orc.b optimization explicit Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 10/15] RISC-V: Add Zbb optimized memchr as ifunc Christoph Müllner
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

Target function attributes that allow enabling RISC-V extensions
have been added to GCC and LLVM a few months ago.
This patch adds a configure test to see if the compiler supports
enabling Zbb via this mechanism.

Note, that unknown target attributes just issue a warning, so this
test needs to build with -Werror.

Tested with GCC 13 (fails) and GCC 14 (succeeds).

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 config.h.in                |  3 +++
 sysdeps/riscv/configure    | 27 +++++++++++++++++++++++++++
 sysdeps/riscv/configure.ac | 18 ++++++++++++++++++
 3 files changed, 48 insertions(+)

diff --git a/config.h.in b/config.h.in
index 9a83b774fa..10a670441c 100644
--- a/config.h.in
+++ b/config.h.in
@@ -273,6 +273,9 @@
 /* PowerPC32 uses fctidz for floating point to long long conversions.  */
 #define HAVE_PPC_FCTIDZ 0
 
+/* RISC-V function attributes for Zbb.  */
+#define HAVE_RISCV_FATTRIBUTE_ZBB 0
+
 /* Define if PIE is unsupported.  */
 #undef PIE_UNSUPPORTED
 
diff --git a/sysdeps/riscv/configure b/sysdeps/riscv/configure
index c8f01709f8..e0f57fcf9b 100644
--- a/sysdeps/riscv/configure
+++ b/sysdeps/riscv/configure
@@ -80,3 +80,30 @@ if test "$libc_cv_static_pie_on_riscv" = yes; then
   printf "%s\n" "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
 
 fi
+
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for RISC-V Zbb target attribute support" >&5
+printf %s "checking for RISC-V Zbb target attribute support... " >&6; }
+if test ${libc_cv_compiler_fattribute_zbb+y}
+then :
+  printf %s "(cached) " >&6
+else $as_nop
+  cat > conftest.c <<EOF
+__attribute__((target("arch=+zbb")))
+void foo() {}
+EOF
+libc_cv_compiler_fattribute_zbb=no
+if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&5 \
+   2>&5 ; then
+  libc_cv_compiler_fattribute_zbb=yes
+fi
+rm -f conftest*
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_cv_compiler_fattribute_zbb" >&5
+printf "%s\n" "$libc_cv_compiler_fattribute_zbb" >&6; }
+if test "$libc_cv_compiler_fattribute_zbb" = yes; then
+  printf "%s\n" "#define HAVE_RISCV_FATTRIBUTE_ZBB 1" >>confdefs.h
+
+fi
+config_vars="$config_vars
+have-fattribute-zbb-support = $libc_cv_compiler_fattribute_zbb"
+
diff --git a/sysdeps/riscv/configure.ac b/sysdeps/riscv/configure.ac
index ee3d1ed014..a7e16dcf83 100644
--- a/sysdeps/riscv/configure.ac
+++ b/sysdeps/riscv/configure.ac
@@ -43,3 +43,21 @@ EOF
 if test "$libc_cv_static_pie_on_riscv" = yes; then
   AC_DEFINE(SUPPORT_STATIC_PIE)
 fi
+
+dnl Test if we have Zbb via function attributes
+AC_CACHE_CHECK([for RISC-V Zbb target attribute support],
+		libc_cv_compiler_fattribute_zbb, [dnl
+cat > conftest.c <<EOF
+__attribute__((target("arch=+zbb")))
+void foo() {}
+EOF
+libc_cv_compiler_fattribute_zbb=no
+if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&AS_MESSAGE_LOG_FD \
+   2>&AS_MESSAGE_LOG_FD ; then
+  libc_cv_compiler_fattribute_zbb=yes
+fi
+rm -f conftest*])
+if test "$libc_cv_compiler_fattribute_zbb" = yes; then
+  AC_DEFINE(HAVE_RISCV_FATTRIBUTE_ZBB)
+fi
+LIBC_CONFIG_VAR([have-fattribute-zbb-support], [$libc_cv_compiler_fattribute_zbb])
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 10/15] RISC-V: Add Zbb optimized memchr as ifunc
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (8 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 09/15] RISC-V: Add compiler test for Zbb function attribute support Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 11/15] RISC-V: Add Zbb optimized memrchr " Christoph Müllner
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

When building with Zbb enabled, memchr benefits from using orc.b in
find_zero_all().  This patch changes the build system such, that a
non-Zbb version as well as a Zbb version of this routine is built.
Further, a ifunc resolver is provided that selects the right routine
based on the outcome of extension probing via hwprobe().

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/multiarch/memchr-generic.c      | 24 ++++++++
 sysdeps/riscv/multiarch/memchr-zbb.c          | 23 +++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  8 +++
 .../linux/riscv/multiarch/ifunc-impl-list.c   | 37 ++++++++++--
 .../unix/sysv/linux/riscv/multiarch/memchr.c  | 60 +++++++++++++++++++
 5 files changed, 147 insertions(+), 5 deletions(-)
 create mode 100644 sysdeps/riscv/multiarch/memchr-generic.c
 create mode 100644 sysdeps/riscv/multiarch/memchr-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c

diff --git a/sysdeps/riscv/multiarch/memchr-generic.c b/sysdeps/riscv/multiarch/memchr-generic.c
new file mode 100644
index 0000000000..c44616c95c
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memchr-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default memchr implementation.
+   Copyright (C) 2024 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 MEMCHR __memchr_generic
+#endif
+#include <string/memchr.c>
diff --git a/sysdeps/riscv/multiarch/memchr-zbb.c b/sysdeps/riscv/multiarch/memchr-zbb.c
new file mode 100644
index 0000000000..e5de76ec18
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memchr-zbb.c
@@ -0,0 +1,23 @@
+/* Re-include the default memchr implementation for Zbb.
+   Copyright (C) 2024 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/>.  */
+
+/* Build with Zbb enabled.  */
+#define __CODEGEN_ATTRIBUTES __attribute__((target("arch=+zbb")))
+#define USE_ZBB_ORCB 1
+#define MEMCHR __memchr_zbb
+#include <string/memchr.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index fcef5659d4..12ebe0a960 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -1,9 +1,17 @@
 ifeq ($(subdir),string)
 sysdep_routines += \
+  memchr \
+  memchr-generic \
   memcpy \
   memcpy-generic \
   memcpy_noalignment \
   # sysdep_routines
 
+ifeq ($(have-fattribute-zbb-support),yes)
+sysdep_routines += \
+  memchr-zbb \
+  # Zbb sysdep_routines
+endif
+
 CFLAGS-memcpy_noalignment.c += -mno-strict-align
 endif
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index 9f806d7a9e..e7bb604c5a 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -20,19 +20,46 @@
 #include <string.h>
 #include <sys/hwprobe.h>
 
+#define ARRAY_SIZE(A) (sizeof (A) / sizeof ((A)[0]))
+
 size_t
 __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 			size_t max)
 {
   size_t i = max;
+  struct riscv_hwprobe pairs[] = {
+    { .key = RISCV_HWPROBE_KEY_IMA_EXT_0 },
+    { .key = RISCV_HWPROBE_KEY_CPUPERF_0 },
+  };
 
   bool fast_unaligned = false;
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  bool has_zbb = false;
+#endif
+
+  if (__riscv_hwprobe (pairs, ARRAY_SIZE (pairs), 0, NULL, 0) == 0)
+    {
+      struct riscv_hwprobe *pair;
+
+      /* RISCV_HWPROBE_KEY_IMA_EXT_0  */
+      pair = &pairs[0];
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+      if (pair->value & RISCV_HWPROBE_EXT_ZBB)
+        has_zbb = true;
+#endif
+
+      /* RISCV_HWPROBE_KEY_CPUPERF_0  */
+      pair = &pairs[1];
+      if ((pair->value & RISCV_HWPROBE_MISALIGNED_MASK)
+	   == RISCV_HWPROBE_MISALIGNED_FAST)
+        fast_unaligned = true;
+    }
 
-  struct riscv_hwprobe pair = { .key = RISCV_HWPROBE_KEY_CPUPERF_0 };
-  if (__riscv_hwprobe (&pair, 1, 0, NULL, 0) == 0
-      && (pair.value & RISCV_HWPROBE_MISALIGNED_MASK)
-          == RISCV_HWPROBE_MISALIGNED_FAST)
-    fast_unaligned = true;
+  IFUNC_IMPL (i, name, memchr,
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+	      IFUNC_IMPL_ADD (array, i, memchr, has_zbb, __memchr_zbb)
+#endif
+	      IFUNC_IMPL_ADD (array, i, memchr, 1, __memchr_generic))
 
   IFUNC_IMPL (i, name, memcpy,
 	      IFUNC_IMPL_ADD (array, i, memcpy, fast_unaligned,
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c
new file mode 100644
index 0000000000..71a0c039bd
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c
@@ -0,0 +1,60 @@
+/* Multiple versions of memchr.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2017-2024 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/>.  */
+
+#if IS_IN (libc)
+
+/* Redefine memchr so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef memchr
+# define memchr __redirect_memchr
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_memchr) __libc_memchr;
+
+extern __typeof (__redirect_memchr) __memchr_generic attribute_hidden;
+extern __typeof (__redirect_memchr) __memchr_zbb attribute_hidden;
+
+static inline __typeof (__redirect_memchr) *
+select_memchr_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_EXT_ZBB))
+    return __memchr_zbb;
+#endif
+
+  return __memchr_generic;
+}
+
+riscv_libc_ifunc (__libc_memchr, select_memchr_ifunc);
+
+# undef memchr
+strong_alias (__libc_memchr, memchr);
+# ifdef SHARED
+__hidden_ver1 (memchr, __GI_memchr, __redirect_memchr)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memchr);
+# endif
+#else
+# include <string/memchr.c>
+#endif
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 11/15] RISC-V: Add Zbb optimized memrchr as ifunc
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (9 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 10/15] RISC-V: Add Zbb optimized memchr as ifunc Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 12/15] RISC-V: Add Zbb optimized strchrnul " Christoph Müllner
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

When building with Zbb enabled, memrchr benefits from using orc.b in
find_zero_all().  This patch changes the build system such, that a
non-Zbb version as well as a Zbb version of this routine is built.
Further, a ifunc resolver is provided that selects the right routine
based on the outcome of extension probing via hwprobe().

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/multiarch/memrchr-generic.c     | 24 +++++++
 sysdeps/riscv/multiarch/memrchr-zbb.c         | 23 +++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  6 ++
 .../unix/sysv/linux/riscv/multiarch/memrchr.c | 63 +++++++++++++++++++
 5 files changed, 119 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/memrchr-generic.c
 create mode 100644 sysdeps/riscv/multiarch/memrchr-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c

diff --git a/sysdeps/riscv/multiarch/memrchr-generic.c b/sysdeps/riscv/multiarch/memrchr-generic.c
new file mode 100644
index 0000000000..787e9ce5ca
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memrchr-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default memrchr implementation.
+   Copyright (C) 2024 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 MEMRCHR __memrchr_generic
+#endif
+#include <string/memrchr.c>
diff --git a/sysdeps/riscv/multiarch/memrchr-zbb.c b/sysdeps/riscv/multiarch/memrchr-zbb.c
new file mode 100644
index 0000000000..10565d95a9
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memrchr-zbb.c
@@ -0,0 +1,23 @@
+/* Re-include the default memrchr implementation for Zbb.
+   Copyright (C) 2024 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/>.  */
+
+/* Build with Zbb enabled.  */
+#define __CODEGEN_ATTRIBUTES __attribute__((target("arch=+zbb")))
+#define USE_ZBB_ORCB 1
+#define MEMRCHR __memrchr_zbb
+#include <string/memrchr.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 12ebe0a960..ca7b924007 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -5,11 +5,14 @@ sysdep_routines += \
   memcpy \
   memcpy-generic \
   memcpy_noalignment \
+  memrchr \
+  memrchr-generic \
   # sysdep_routines
 
 ifeq ($(have-fattribute-zbb-support),yes)
 sysdep_routines += \
   memchr-zbb \
+  memrchr-zbb \
   # Zbb sysdep_routines
 endif
 
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index e7bb604c5a..fbd08162ca 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -66,5 +66,11 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 			      __memcpy_noalignment)
 	      IFUNC_IMPL_ADD (array, i, memcpy, 1, __memcpy_generic))
 
+  IFUNC_IMPL (i, name, memrchr,
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+	      IFUNC_IMPL_ADD (array, i, memrchr, has_zbb, __memrchr_zbb)
+#endif
+	      IFUNC_IMPL_ADD (array, i, memrchr, 1, __memrchr_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
new file mode 100644
index 0000000000..065557ed7b
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
@@ -0,0 +1,63 @@
+/* Multiple versions of memrchr.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2017-2024 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/>.  */
+
+#if IS_IN (libc)
+/* Redefine memrchr so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef memrchr
+# undef __memrchr
+# define memrchr __redirect_memrchr
+# define __memrchr __redirect___memrchr
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_memrchr) __libc_memrchr;
+
+extern __typeof (__redirect_memrchr) __memrchr_generic attribute_hidden;
+extern __typeof (__redirect_memrchr) __memrchr_zbb attribute_hidden;
+
+static inline __typeof (__redirect_memrchr) *
+select_memrchr_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_EXT_ZBB))
+    return __memrchr_zbb;
+#endif
+
+  return __memrchr_generic;
+}
+
+riscv_libc_ifunc (__libc_memrchr, select_memrchr_ifunc);
+
+# undef memrchr
+# undef __memrchr
+strong_alias (__libc_memrchr, __memrchr);
+weak_alias (__memrchr, memrchr)
+# ifdef SHARED
+__hidden_ver1 (memrchr, __GI___memrchr, __redirect_memrchr)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memrchr);
+# endif
+#else
+# include <string/memrchr.c>
+#endif
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 12/15] RISC-V: Add Zbb optimized strchrnul as ifunc
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (10 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 11/15] RISC-V: Add Zbb optimized memrchr " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 13/15] RISC-V: Add Zbb optimized strcmp " Christoph Müllner
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

When building with Zbb enabled, strchrnul benefits from using orc.b in
find_zero_all().  This patch changes the build system such, that a
non-Zbb version as well as a Zbb version of this routine is built.
Further, a ifunc resolver is provided that selects the right routine
based on the outcome of extension probing via hwprobe().

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/multiarch/strchrnul-generic.c   | 24 +++++++
 sysdeps/riscv/multiarch/strchrnul-zbb.c       | 23 +++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  6 ++
 .../sysv/linux/riscv/multiarch/strchrnul.c    | 63 +++++++++++++++++++
 5 files changed, 119 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/strchrnul-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strchrnul-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c

diff --git a/sysdeps/riscv/multiarch/strchrnul-generic.c b/sysdeps/riscv/multiarch/strchrnul-generic.c
new file mode 100644
index 0000000000..c59500798d
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strchrnul-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default strchrnul implementation.
+   Copyright (C) 2024 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 STRCHRNUL __strchrnul_generic
+#endif
+#include <string/strchrnul.c>
diff --git a/sysdeps/riscv/multiarch/strchrnul-zbb.c b/sysdeps/riscv/multiarch/strchrnul-zbb.c
new file mode 100644
index 0000000000..f5f290621e
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strchrnul-zbb.c
@@ -0,0 +1,23 @@
+/* Re-include the default strchrnul implementation for Zbb.
+   Copyright (C) 2024 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/>.  */
+
+/* Build with Zbb enabled.  */
+#define __CODEGEN_ATTRIBUTES __attribute__((target("arch=+zbb")))
+#define USE_ZBB_ORCB 1
+#define STRCHRNUL __strchrnul_zbb
+#include <string/strchrnul.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index ca7b924007..5a442905b5 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -7,12 +7,15 @@ sysdep_routines += \
   memcpy_noalignment \
   memrchr \
   memrchr-generic \
+  strchrnul \
+  strchrnul-generic \
   # sysdep_routines
 
 ifeq ($(have-fattribute-zbb-support),yes)
 sysdep_routines += \
   memchr-zbb \
   memrchr-zbb \
+  strchrnul-zbb \
   # Zbb sysdep_routines
 endif
 
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index fbd08162ca..7a769122ec 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -72,5 +72,11 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 #endif
 	      IFUNC_IMPL_ADD (array, i, memrchr, 1, __memrchr_generic))
 
+  IFUNC_IMPL (i, name, strchrnul,
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+	      IFUNC_IMPL_ADD (array, i, strchrnul, has_zbb, __strchrnul_zbb)
+#endif
+	      IFUNC_IMPL_ADD (array, i, strchrnul, 1, __strchrnul_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c
new file mode 100644
index 0000000000..b2c6665e90
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c
@@ -0,0 +1,63 @@
+/* Multiple versions of strchrnul.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2017-2024 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/>.  */
+
+#if IS_IN (libc)
+/* Redefine strchrnul so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef strchrnul
+# undef __strchrnul
+# define strchrnul __redirect_strchrnul
+# define __strchrnul __redirect_strchrnul
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strchrnul) __libc_strchrnul;
+
+extern __typeof (__redirect_strchrnul) __strchrnul_generic attribute_hidden;
+extern __typeof (__redirect_strchrnul) __strchrnul_zbb attribute_hidden;
+
+static inline __typeof (__redirect_strchrnul) *
+select_strchrnul_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_EXT_ZBB))
+    return __strchrnul_zbb;
+#endif
+
+  return __strchrnul_generic;
+}
+
+riscv_libc_ifunc (__libc_strchrnul, select_strchrnul_ifunc);
+
+# undef strchrnul
+# undef __strchrnul
+strong_alias (__libc_strchrnul, __strchrnul);
+weak_alias (__strchrnul, strchrnul)
+# ifdef SHARED
+__hidden_ver1 (strchrnul, __GI___strchrnul, __redirect_strchrnul)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strchrnul);
+# endif
+#else
+# include <string/strchrnul.c>
+#endif
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 13/15] RISC-V: Add Zbb optimized strcmp as ifunc
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (11 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 12/15] RISC-V: Add Zbb optimized strchrnul " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:18 ` [PATCH v2 14/15] RISC-V: Add Zbb optimized strlen " Christoph Müllner
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

When building with Zbb enabled, strcmp benefits from using orc.b in
find_zero_all().  This patch changes the build system such, that a
non-Zbb version as well as a Zbb version of this routine is built.
Further, a ifunc resolver is provided that selects the right routine
based on the outcome of extension probing via hwprobe().

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/multiarch/strcmp-generic.c      | 24 ++++++++
 sysdeps/riscv/multiarch/strcmp-zbb.c          | 23 ++++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  6 ++
 .../unix/sysv/linux/riscv/multiarch/strcmp.c  | 59 +++++++++++++++++++
 5 files changed, 115 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/strcmp-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strcmp-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c

diff --git a/sysdeps/riscv/multiarch/strcmp-generic.c b/sysdeps/riscv/multiarch/strcmp-generic.c
new file mode 100644
index 0000000000..ef9fb9eb46
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strcmp-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default strcmp implementation.
+   Copyright (C) 2024 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 STRCMP __strcmp_generic
+#endif
+#include <string/strcmp.c>
diff --git a/sysdeps/riscv/multiarch/strcmp-zbb.c b/sysdeps/riscv/multiarch/strcmp-zbb.c
new file mode 100644
index 0000000000..86a2996358
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strcmp-zbb.c
@@ -0,0 +1,23 @@
+/* Re-include the default strcmp implementation for Zbb.
+   Copyright (C) 2024 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/>.  */
+
+/* Build with Zbb enabled.  */
+#define __CODEGEN_ATTRIBUTES __attribute__((target("arch=+zbb")))
+#define USE_ZBB_ORCB 1
+#define STRCMP __strcmp_zbb
+#include <string/strcmp.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 5a442905b5..69c4dd2d07 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -9,6 +9,8 @@ sysdep_routines += \
   memrchr-generic \
   strchrnul \
   strchrnul-generic \
+  strcmp \
+  strcmp-generic \
   # sysdep_routines
 
 ifeq ($(have-fattribute-zbb-support),yes)
@@ -16,6 +18,7 @@ sysdep_routines += \
   memchr-zbb \
   memrchr-zbb \
   strchrnul-zbb \
+  strcmp-zbb \
   # Zbb sysdep_routines
 endif
 
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index 7a769122ec..c761807a7a 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -78,5 +78,11 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 #endif
 	      IFUNC_IMPL_ADD (array, i, strchrnul, 1, __strchrnul_generic))
 
+  IFUNC_IMPL (i, name, strcmp,
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+	      IFUNC_IMPL_ADD (array, i, strcmp, has_zbb, __strcmp_zbb)
+#endif
+	      IFUNC_IMPL_ADD (array, i, strcmp, 1, __strcmp_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
new file mode 100644
index 0000000000..f32f87a8e9
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
@@ -0,0 +1,59 @@
+/* Multiple versions of strcmp.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2017-2024 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/>.  */
+
+#if IS_IN (libc)
+/* Redefine strcmp so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef strcmp
+# define strcmp __redirect_strcmp
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strcmp) __libc_strcmp;
+
+extern __typeof (__redirect_strcmp) __strcmp_generic attribute_hidden;
+extern __typeof (__redirect_strcmp) __strcmp_zbb attribute_hidden;
+
+static inline __typeof (__redirect_strcmp) *
+select_strcmp_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_EXT_ZBB))
+    return __strcmp_zbb;
+#endif
+
+  return __strcmp_generic;
+}
+
+riscv_libc_ifunc (__libc_strcmp, select_strcmp_ifunc);
+
+# undef strcmp
+strong_alias (__libc_strcmp, strcmp);
+# ifdef SHARED
+__hidden_ver1 (strcmp, __GI_strcmp, __redirect_strcmp)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strcmp);
+# endif
+#else
+# include <string/strcmp.c>
+#endif
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 14/15] RISC-V: Add Zbb optimized strlen as ifunc
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (12 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 13/15] RISC-V: Add Zbb optimized strcmp " Christoph Müllner
@ 2024-05-27 11:18 ` Christoph Müllner
  2024-05-27 11:19 ` [PATCH v2 15/15] RISC-V: Add Zbb optimized strncmp " Christoph Müllner
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:18 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

When building with Zbb enabled, strlen benefits from using orc.b in
find_zero_all().  This patch changes the build system such, that a
non-Zbb version as well as a Zbb version of this routine is built.
Further, a ifunc resolver is provided that selects the right routine
based on the outcome of extension probing via hwprobe().

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/multiarch/strlen-generic.c      | 24 ++++++++
 sysdeps/riscv/multiarch/strlen-zbb.c          | 23 ++++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  6 ++
 .../unix/sysv/linux/riscv/multiarch/strlen.c  | 59 +++++++++++++++++++
 5 files changed, 115 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/strlen-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strlen-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c

diff --git a/sysdeps/riscv/multiarch/strlen-generic.c b/sysdeps/riscv/multiarch/strlen-generic.c
new file mode 100644
index 0000000000..e2d59bd4ef
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strlen-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default strlen implementation.
+   Copyright (C) 2024 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 STRLEN __strlen_generic
+#endif
+#include <string/strlen.c>
diff --git a/sysdeps/riscv/multiarch/strlen-zbb.c b/sysdeps/riscv/multiarch/strlen-zbb.c
new file mode 100644
index 0000000000..3be9e78556
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strlen-zbb.c
@@ -0,0 +1,23 @@
+/* Re-include the default strlen implementation for Zbb.
+   Copyright (C) 2024 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/>.  */
+
+/* Build with Zbb enabled.  */
+#define __CODEGEN_ATTRIBUTES __attribute__((target("arch=+zbb")))
+#define USE_ZBB_ORCB 1
+#define STRLEN __strlen_zbb
+#include <string/strlen.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 69c4dd2d07..e48ca11149 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -11,6 +11,8 @@ sysdep_routines += \
   strchrnul-generic \
   strcmp \
   strcmp-generic \
+  strlen \
+  strlen-generic \
   # sysdep_routines
 
 ifeq ($(have-fattribute-zbb-support),yes)
@@ -19,6 +21,7 @@ sysdep_routines += \
   memrchr-zbb \
   strchrnul-zbb \
   strcmp-zbb \
+  strlen-zbb \
   # Zbb sysdep_routines
 endif
 
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index c761807a7a..9b4ae40941 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -84,5 +84,11 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 #endif
 	      IFUNC_IMPL_ADD (array, i, strcmp, 1, __strcmp_generic))
 
+  IFUNC_IMPL (i, name, strlen,
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+	      IFUNC_IMPL_ADD (array, i, strlen, has_zbb, __strlen_zbb)
+#endif
+	      IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c
new file mode 100644
index 0000000000..dec274af18
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c
@@ -0,0 +1,59 @@
+/* Multiple versions of strlen.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2017-2024 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/>.  */
+
+#if IS_IN (libc)
+/* Redefine strlen so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef strlen
+# define strlen __redirect_strlen
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strlen) __libc_strlen;
+
+extern __typeof (__redirect_strlen) __strlen_generic attribute_hidden;
+extern __typeof (__redirect_strlen) __strlen_zbb attribute_hidden;
+
+static inline __typeof (__redirect_strlen) *
+select_strlen_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_EXT_ZBB))
+    return __strlen_zbb;
+#endif
+
+  return __strlen_generic;
+}
+
+riscv_libc_ifunc (__libc_strlen, select_strlen_ifunc);
+
+# undef strlen
+strong_alias (__libc_strlen, strlen);
+# ifdef SHARED
+__hidden_ver1 (strlen, __GI_strlen, __redirect_strlen)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strlen);
+# endif
+#else
+# include <string/strlen.c>
+#endif
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 15/15] RISC-V: Add Zbb optimized strncmp as ifunc
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (13 preceding siblings ...)
  2024-05-27 11:18 ` [PATCH v2 14/15] RISC-V: Add Zbb optimized strlen " Christoph Müllner
@ 2024-05-27 11:19 ` Christoph Müllner
  2024-06-03 21:08 ` [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
  2024-06-19 14:26 ` Adhemerval Zanella Netto
  16 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-05-27 11:19 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law
  Cc: Christoph Müllner

When building with Zbb enabled, strncmp benefits from using orc.b in
find_zero_all().  This patch changes the build system such, that a
non-Zbb version as well as a Zbb version of this routine is built.
Further, a ifunc resolver is provided that selects the right routine
based on the outcome of extension probing via hwprobe().

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/multiarch/strncmp-generic.c     | 26 ++++++++
 sysdeps/riscv/multiarch/strncmp-zbb.c         | 25 ++++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  6 ++
 .../unix/sysv/linux/riscv/multiarch/strncmp.c | 59 +++++++++++++++++++
 5 files changed, 119 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/strncmp-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strncmp-zbb.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c

diff --git a/sysdeps/riscv/multiarch/strncmp-generic.c b/sysdeps/riscv/multiarch/strncmp-generic.c
new file mode 100644
index 0000000000..67eb89e62e
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncmp-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default strncmp implementation.
+   Copyright (C) 2024 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 STRNCMP __strncmp_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+#endif
+#include <string/strncmp.c>
diff --git a/sysdeps/riscv/multiarch/strncmp-zbb.c b/sysdeps/riscv/multiarch/strncmp-zbb.c
new file mode 100644
index 0000000000..8485bdecb1
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncmp-zbb.c
@@ -0,0 +1,25 @@
+/* Re-include the default strncmp implementation for Zbb.
+   Copyright (C) 2024 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/>.  */
+
+/* Build with Zbb enabled.  */
+#define __CODEGEN_ATTRIBUTES __attribute__((target("arch=+zbb")))
+#define USE_ZBB_ORCB 1
+#define STRNCMP __strncmp_zbb
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(x)
+#include <string/strncmp.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index e48ca11149..a4320d6419 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -13,6 +13,8 @@ sysdep_routines += \
   strcmp-generic \
   strlen \
   strlen-generic \
+  strncmp \
+  strncmp-generic \
   # sysdep_routines
 
 ifeq ($(have-fattribute-zbb-support),yes)
@@ -22,6 +24,7 @@ sysdep_routines += \
   strchrnul-zbb \
   strcmp-zbb \
   strlen-zbb \
+  strncmp-zbb \
   # Zbb sysdep_routines
 endif
 
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index 9b4ae40941..a19d88fcf8 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -90,5 +90,11 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 #endif
 	      IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_generic))
 
+  IFUNC_IMPL (i, name, strncmp,
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+	      IFUNC_IMPL_ADD (array, i, strncmp, has_zbb, __strncmp_zbb)
+#endif
+	      IFUNC_IMPL_ADD (array, i, strncmp, 1, __strncmp_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
new file mode 100644
index 0000000000..31a91adcac
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
@@ -0,0 +1,59 @@
+/* Multiple versions of strncmp.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2017-2024 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/>.  */
+
+#if IS_IN (libc)
+/* Redefine strncmp so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef strncmp
+# define strncmp __redirect_strncmp
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strncmp) __libc_strncmp;
+
+extern __typeof (__redirect_strncmp) __strncmp_generic attribute_hidden;
+extern __typeof (__redirect_strncmp) __strncmp_zbb attribute_hidden;
+
+static inline __typeof (__redirect_strncmp) *
+select_strncmp_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+#if HAVE_RISCV_FATTRIBUTE_ZBB
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_EXT_ZBB))
+    return __strncmp_zbb;
+#endif
+
+  return __strncmp_generic;
+}
+
+riscv_libc_ifunc (__libc_strncmp, select_strncmp_ifunc);
+
+# undef strncmp
+strong_alias (__libc_strncmp, strncmp);
+# ifdef SHARED
+__hidden_ver1 (strncmp, __GI_strncmp, __redirect_strncmp)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strncmp);
+# endif
+#else
+# include <string/strncmp.c>
+#endif
-- 
2.45.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (14 preceding siblings ...)
  2024-05-27 11:19 ` [PATCH v2 15/15] RISC-V: Add Zbb optimized strncmp " Christoph Müllner
@ 2024-06-03 21:08 ` Christoph Müllner
  2024-06-11 10:00   ` Christoph Müllner
  2024-06-19 14:26 ` Adhemerval Zanella Netto
  16 siblings, 1 reply; 20+ messages in thread
From: Christoph Müllner @ 2024-06-03 21:08 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law

Ping.

On Mon, May 27, 2024 at 1:19 PM Christoph Müllner
<christoph.muellner@vrull.eu> wrote:
>
> Glibc recently got hwprobe() support for RISC-V, which allows querying
> avaiable extensions at runtime.  On top of that an optimized memcpy()
> routine (for fast unaligned accesses) has been merged, which is built by
> recompiling the generic C code with a different compiler flag.  An ifunc
> resolver then detects which routine should be run using hwprobe().
>
> This patchset follows this idea and recompiles the following functions
> for Zbb (via function attributes) and enables the existing Zbb/orc.b
> optimization in riscv/string-fza.h:
> memchr, memrchr, strchrnul, strcmp, strlen, strncmp.
> The resulting optimized routines are then selected by the resolver function
> if the Zbb extension is present at runtime.
>
> To use target function attributes, a few issues had to be resovled:
> - The functions above got a mechanism to be compiled with function attributes
>   (patches 2-7).  Only those routines have been touched, which are
>   required for the purpose of this patchset.
> - Ensuring that inlined functions also get the same function attributes
>   (first patch).
> - Add mechanism to explicitly enable the orc.b optimization for string functions
>   (patch 8), which is a bit inspired by USE_FFS_BUILTIN.
>
> One of the design questions is, if Zbb represents a broad enough optimization
> target.  Tests with Zb* extensions showed, that no further code improvements
> can be achieved with them.  Also most other extensions likely won't affect
> the generated code for string routines (ignoring vector instructions, which
> are a different topic).  Therefore, Zbb seemed like a sufficient target.
>
> This series was tested by writing a simple test program to invoke the
> libc routines (e.g. strcmp) and a modified QEMU that reports the
> emulation of orc.b on stderr.  With that the QEMU can be used to test
> if the optimized routines are executed (-cpu "rv64,zbb=[false,true]").
> Further, this series was tested with SPEC CPU 2017 intrate with Zbb
> enabled.  The function attribute detection mechanism was tested with
> GCC 13 and GCC 14.
>
> Changes in v2:
> - Drop "Use .insn directive form for orc.b"
> - Introduce use of target function attribute (and all depenendcies)
> - Introduce detection of target function attribute support
> - Make orc.b optimization explicit
> - Small cleanups
>
> Christoph Müllner (15):
>   cdefs: Add mechanism to add attributes to __always_inline functions
>   string/memchr: Add mechanism to set function attributes
>   string/memrchr: Add mechanism to set function attributes
>   string/strchrnul: Add mechanism to set function attributes
>   string/strcmp: Add mechanism to set function attributes
>   string/strlen: Add mechanism to set function attributes
>   string/strncmp: Add mechanism to set function attributes
>   RISC-V: string-fz[a,i].h: Make orc.b optimization explicit
>   RISC-V: Add compiler test for Zbb function attribute support
>   RISC-V: Add Zbb optimized memchr as ifunc
>   RISC-V: Add Zbb optimized memrchr as ifunc
>   RISC-V: Add Zbb optimized strchrnul as ifunc
>   RISC-V: Add Zbb optimized strcmp as ifunc
>   RISC-V: Add Zbb optimized strlen as ifunc
>   RISC-V: Add Zbb optimized strncmp as ifunc
>
>  config.h.in                                   |  3 +
>  misc/sys/cdefs.h                              |  8 ++-
>  string/memchr.c                               |  5 ++
>  string/memrchr.c                              |  5 ++
>  string/strchrnul.c                            |  5 ++
>  string/strcmp.c                               |  8 +++
>  string/strlen.c                               |  5 ++
>  string/strncmp.c                              |  8 +++
>  sysdeps/riscv/configure                       | 27 ++++++++
>  sysdeps/riscv/configure.ac                    | 18 +++++
>  sysdeps/riscv/multiarch/memchr-generic.c      | 24 +++++++
>  sysdeps/riscv/multiarch/memchr-zbb.c          | 23 +++++++
>  sysdeps/riscv/multiarch/memrchr-generic.c     | 24 +++++++
>  sysdeps/riscv/multiarch/memrchr-zbb.c         | 23 +++++++
>  sysdeps/riscv/multiarch/strchrnul-generic.c   | 24 +++++++
>  sysdeps/riscv/multiarch/strchrnul-zbb.c       | 23 +++++++
>  sysdeps/riscv/multiarch/strcmp-generic.c      | 24 +++++++
>  sysdeps/riscv/multiarch/strcmp-zbb.c          | 23 +++++++
>  sysdeps/riscv/multiarch/strlen-generic.c      | 24 +++++++
>  sysdeps/riscv/multiarch/strlen-zbb.c          | 23 +++++++
>  sysdeps/riscv/multiarch/strncmp-generic.c     | 26 +++++++
>  sysdeps/riscv/multiarch/strncmp-zbb.c         | 25 +++++++
>  sysdeps/riscv/string-fza.h                    | 22 +++++-
>  sysdeps/riscv/string-fzi.h                    | 20 +++++-
>  .../unix/sysv/linux/riscv/multiarch/Makefile  | 23 +++++++
>  .../linux/riscv/multiarch/ifunc-impl-list.c   | 67 +++++++++++++++++--
>  .../unix/sysv/linux/riscv/multiarch/memchr.c  | 60 +++++++++++++++++
>  .../unix/sysv/linux/riscv/multiarch/memrchr.c | 63 +++++++++++++++++
>  .../sysv/linux/riscv/multiarch/strchrnul.c    | 63 +++++++++++++++++
>  .../unix/sysv/linux/riscv/multiarch/strcmp.c  | 59 ++++++++++++++++
>  .../unix/sysv/linux/riscv/multiarch/strlen.c  | 59 ++++++++++++++++
>  .../unix/sysv/linux/riscv/multiarch/strncmp.c | 59 ++++++++++++++++
>  32 files changed, 863 insertions(+), 10 deletions(-)
>  create mode 100644 sysdeps/riscv/multiarch/memchr-generic.c
>  create mode 100644 sysdeps/riscv/multiarch/memchr-zbb.c
>  create mode 100644 sysdeps/riscv/multiarch/memrchr-generic.c
>  create mode 100644 sysdeps/riscv/multiarch/memrchr-zbb.c
>  create mode 100644 sysdeps/riscv/multiarch/strchrnul-generic.c
>  create mode 100644 sysdeps/riscv/multiarch/strchrnul-zbb.c
>  create mode 100644 sysdeps/riscv/multiarch/strcmp-generic.c
>  create mode 100644 sysdeps/riscv/multiarch/strcmp-zbb.c
>  create mode 100644 sysdeps/riscv/multiarch/strlen-generic.c
>  create mode 100644 sysdeps/riscv/multiarch/strlen-zbb.c
>  create mode 100644 sysdeps/riscv/multiarch/strncmp-generic.c
>  create mode 100644 sysdeps/riscv/multiarch/strncmp-zbb.c
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
>
> --
> 2.45.1
>

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs
  2024-06-03 21:08 ` [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
@ 2024-06-11 10:00   ` Christoph Müllner
  0 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-06-11 10:00 UTC (permalink / raw)
  To: libc-alpha, Adhemerval Zanella, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law

Ping.

On Mon, Jun 3, 2024 at 11:08 PM Christoph Müllner
<christoph.muellner@vrull.eu> wrote:
>
> Ping.
>
> On Mon, May 27, 2024 at 1:19 PM Christoph Müllner
> <christoph.muellner@vrull.eu> wrote:
> >
> > Glibc recently got hwprobe() support for RISC-V, which allows querying
> > avaiable extensions at runtime.  On top of that an optimized memcpy()
> > routine (for fast unaligned accesses) has been merged, which is built by
> > recompiling the generic C code with a different compiler flag.  An ifunc
> > resolver then detects which routine should be run using hwprobe().
> >
> > This patchset follows this idea and recompiles the following functions
> > for Zbb (via function attributes) and enables the existing Zbb/orc.b
> > optimization in riscv/string-fza.h:
> > memchr, memrchr, strchrnul, strcmp, strlen, strncmp.
> > The resulting optimized routines are then selected by the resolver function
> > if the Zbb extension is present at runtime.
> >
> > To use target function attributes, a few issues had to be resovled:
> > - The functions above got a mechanism to be compiled with function attributes
> >   (patches 2-7).  Only those routines have been touched, which are
> >   required for the purpose of this patchset.
> > - Ensuring that inlined functions also get the same function attributes
> >   (first patch).
> > - Add mechanism to explicitly enable the orc.b optimization for string functions
> >   (patch 8), which is a bit inspired by USE_FFS_BUILTIN.
> >
> > One of the design questions is, if Zbb represents a broad enough optimization
> > target.  Tests with Zb* extensions showed, that no further code improvements
> > can be achieved with them.  Also most other extensions likely won't affect
> > the generated code for string routines (ignoring vector instructions, which
> > are a different topic).  Therefore, Zbb seemed like a sufficient target.
> >
> > This series was tested by writing a simple test program to invoke the
> > libc routines (e.g. strcmp) and a modified QEMU that reports the
> > emulation of orc.b on stderr.  With that the QEMU can be used to test
> > if the optimized routines are executed (-cpu "rv64,zbb=[false,true]").
> > Further, this series was tested with SPEC CPU 2017 intrate with Zbb
> > enabled.  The function attribute detection mechanism was tested with
> > GCC 13 and GCC 14.
> >
> > Changes in v2:
> > - Drop "Use .insn directive form for orc.b"
> > - Introduce use of target function attribute (and all depenendcies)
> > - Introduce detection of target function attribute support
> > - Make orc.b optimization explicit
> > - Small cleanups
> >
> > Christoph Müllner (15):
> >   cdefs: Add mechanism to add attributes to __always_inline functions
> >   string/memchr: Add mechanism to set function attributes
> >   string/memrchr: Add mechanism to set function attributes
> >   string/strchrnul: Add mechanism to set function attributes
> >   string/strcmp: Add mechanism to set function attributes
> >   string/strlen: Add mechanism to set function attributes
> >   string/strncmp: Add mechanism to set function attributes
> >   RISC-V: string-fz[a,i].h: Make orc.b optimization explicit
> >   RISC-V: Add compiler test for Zbb function attribute support
> >   RISC-V: Add Zbb optimized memchr as ifunc
> >   RISC-V: Add Zbb optimized memrchr as ifunc
> >   RISC-V: Add Zbb optimized strchrnul as ifunc
> >   RISC-V: Add Zbb optimized strcmp as ifunc
> >   RISC-V: Add Zbb optimized strlen as ifunc
> >   RISC-V: Add Zbb optimized strncmp as ifunc
> >
> >  config.h.in                                   |  3 +
> >  misc/sys/cdefs.h                              |  8 ++-
> >  string/memchr.c                               |  5 ++
> >  string/memrchr.c                              |  5 ++
> >  string/strchrnul.c                            |  5 ++
> >  string/strcmp.c                               |  8 +++
> >  string/strlen.c                               |  5 ++
> >  string/strncmp.c                              |  8 +++
> >  sysdeps/riscv/configure                       | 27 ++++++++
> >  sysdeps/riscv/configure.ac                    | 18 +++++
> >  sysdeps/riscv/multiarch/memchr-generic.c      | 24 +++++++
> >  sysdeps/riscv/multiarch/memchr-zbb.c          | 23 +++++++
> >  sysdeps/riscv/multiarch/memrchr-generic.c     | 24 +++++++
> >  sysdeps/riscv/multiarch/memrchr-zbb.c         | 23 +++++++
> >  sysdeps/riscv/multiarch/strchrnul-generic.c   | 24 +++++++
> >  sysdeps/riscv/multiarch/strchrnul-zbb.c       | 23 +++++++
> >  sysdeps/riscv/multiarch/strcmp-generic.c      | 24 +++++++
> >  sysdeps/riscv/multiarch/strcmp-zbb.c          | 23 +++++++
> >  sysdeps/riscv/multiarch/strlen-generic.c      | 24 +++++++
> >  sysdeps/riscv/multiarch/strlen-zbb.c          | 23 +++++++
> >  sysdeps/riscv/multiarch/strncmp-generic.c     | 26 +++++++
> >  sysdeps/riscv/multiarch/strncmp-zbb.c         | 25 +++++++
> >  sysdeps/riscv/string-fza.h                    | 22 +++++-
> >  sysdeps/riscv/string-fzi.h                    | 20 +++++-
> >  .../unix/sysv/linux/riscv/multiarch/Makefile  | 23 +++++++
> >  .../linux/riscv/multiarch/ifunc-impl-list.c   | 67 +++++++++++++++++--
> >  .../unix/sysv/linux/riscv/multiarch/memchr.c  | 60 +++++++++++++++++
> >  .../unix/sysv/linux/riscv/multiarch/memrchr.c | 63 +++++++++++++++++
> >  .../sysv/linux/riscv/multiarch/strchrnul.c    | 63 +++++++++++++++++
> >  .../unix/sysv/linux/riscv/multiarch/strcmp.c  | 59 ++++++++++++++++
> >  .../unix/sysv/linux/riscv/multiarch/strlen.c  | 59 ++++++++++++++++
> >  .../unix/sysv/linux/riscv/multiarch/strncmp.c | 59 ++++++++++++++++
> >  32 files changed, 863 insertions(+), 10 deletions(-)
> >  create mode 100644 sysdeps/riscv/multiarch/memchr-generic.c
> >  create mode 100644 sysdeps/riscv/multiarch/memchr-zbb.c
> >  create mode 100644 sysdeps/riscv/multiarch/memrchr-generic.c
> >  create mode 100644 sysdeps/riscv/multiarch/memrchr-zbb.c
> >  create mode 100644 sysdeps/riscv/multiarch/strchrnul-generic.c
> >  create mode 100644 sysdeps/riscv/multiarch/strchrnul-zbb.c
> >  create mode 100644 sysdeps/riscv/multiarch/strcmp-generic.c
> >  create mode 100644 sysdeps/riscv/multiarch/strcmp-zbb.c
> >  create mode 100644 sysdeps/riscv/multiarch/strlen-generic.c
> >  create mode 100644 sysdeps/riscv/multiarch/strlen-zbb.c
> >  create mode 100644 sysdeps/riscv/multiarch/strncmp-generic.c
> >  create mode 100644 sysdeps/riscv/multiarch/strncmp-zbb.c
> >  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c
> >  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
> >  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strchrnul.c
> >  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
> >  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c
> >  create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
> >
> > --
> > 2.45.1
> >

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs
  2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
                   ` (15 preceding siblings ...)
  2024-06-03 21:08 ` [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
@ 2024-06-19 14:26 ` Adhemerval Zanella Netto
  2024-06-20 15:52   ` Christoph Müllner
  16 siblings, 1 reply; 20+ messages in thread
From: Adhemerval Zanella Netto @ 2024-06-19 14:26 UTC (permalink / raw)
  To: Christoph Müllner, libc-alpha, Palmer Dabbelt, Darius Rad,
	Andrew Waterman, Philipp Tomsich, Evan Green, DJ Delorie,
	Vineet Gupta, Kito Cheng, Jeff Law



On 27/05/24 08:18, Christoph Müllner wrote:
> Glibc recently got hwprobe() support for RISC-V, which allows querying
> avaiable extensions at runtime.  On top of that an optimized memcpy()
> routine (for fast unaligned accesses) has been merged, which is built by
> recompiling the generic C code with a different compiler flag.  An ifunc
> resolver then detects which routine should be run using hwprobe().
> 
> This patchset follows this idea and recompiles the following functions
> for Zbb (via function attributes) and enables the existing Zbb/orc.b
> optimization in riscv/string-fza.h:
> memchr, memrchr, strchrnul, strcmp, strlen, strncmp.
> The resulting optimized routines are then selected by the resolver function
> if the Zbb extension is present at runtime.
> 
> To use target function attributes, a few issues had to be resovled:
> - The functions above got a mechanism to be compiled with function attributes
>   (patches 2-7).  Only those routines have been touched, which are
>   required for the purpose of this patchset.
> - Ensuring that inlined functions also get the same function attributes
>   (first patch).
> - Add mechanism to explicitly enable the orc.b optimization for string functions
>   (patch 8), which is a bit inspired by USE_FFS_BUILTIN.
> 
> One of the design questions is, if Zbb represents a broad enough optimization
> target.  Tests with Zb* extensions showed, that no further code improvements
> can be achieved with them.  Also most other extensions likely won't affect
> the generated code for string routines (ignoring vector instructions, which
> are a different topic).  Therefore, Zbb seemed like a sufficient target.
> 
> This series was tested by writing a simple test program to invoke the
> libc routines (e.g. strcmp) and a modified QEMU that reports the
> emulation of orc.b on stderr.  With that the QEMU can be used to test
> if the optimized routines are executed (-cpu "rv64,zbb=[false,true]").
> Further, this series was tested with SPEC CPU 2017 intrate with Zbb
> enabled.  The function attribute detection mechanism was tested with
> GCC 13 and GCC 14.
> 

I tried check this patchset with gcc 14 [1] (commit 6f6103ccc5b3bf8cb,
built with build-many-glibcs.py) and it shows an ICE:

$ riscv64-glibc-linux-gnu-gcc ../sysdeps/riscv/multiarch/memchr-zbb.c [...]
In file included from ../include/bits/string_fortified.h:1,
                 from ../string/string.h:548,
                 from ../include/string.h:60,
                 from ../string/memchr.c:24,
                 from ../sysdeps/riscv/multiarch/memchr-zbb.c:23:
../string/bits/string_fortified.h:110:1: internal compiler error: in riscv_func_target_put, at common/config/riscv/riscv-common.cc:510
  110 | {
      | ^
0x7af52762a1c9 __libc_start_call_main
        ../sysdeps/nptl/libc_start_call_main.h:58
0x7af52762a28a __libc_start_main_impl
        ../csu/libc-start.c:360

I am building with --enable-fortify-source=2, with --enable-fortify-source=no
the build does not fail. A gcc 13 build [2] seems to be in better shape but 
the version I am using does not have support for required attributes 
(HAVE_RISCV_FATTRIBUTE_ZBB is set to 0).

So I think we will need to check whether this is still happens with
gcc master/15 and see if indeed it was a regression added on gcc 14.

[1] gcc version 14.1.1 20240619 [releases/gcc-14 r14-10324-g6f6103ccc5b]
[2] gcc version 13.1.1 20230525 [releases/gcc-13 r13-7376-ge80487dcbe2]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs
  2024-06-19 14:26 ` Adhemerval Zanella Netto
@ 2024-06-20 15:52   ` Christoph Müllner
  0 siblings, 0 replies; 20+ messages in thread
From: Christoph Müllner @ 2024-06-20 15:52 UTC (permalink / raw)
  To: Adhemerval Zanella Netto
  Cc: libc-alpha, Palmer Dabbelt, Darius Rad, Andrew Waterman,
	Philipp Tomsich, Evan Green, DJ Delorie, Vineet Gupta,
	Kito Cheng, Jeff Law

On Wed, Jun 19, 2024 at 4:26 PM Adhemerval Zanella Netto
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 27/05/24 08:18, Christoph Müllner wrote:
> > Glibc recently got hwprobe() support for RISC-V, which allows querying
> > avaiable extensions at runtime.  On top of that an optimized memcpy()
> > routine (for fast unaligned accesses) has been merged, which is built by
> > recompiling the generic C code with a different compiler flag.  An ifunc
> > resolver then detects which routine should be run using hwprobe().
> >
> > This patchset follows this idea and recompiles the following functions
> > for Zbb (via function attributes) and enables the existing Zbb/orc.b
> > optimization in riscv/string-fza.h:
> > memchr, memrchr, strchrnul, strcmp, strlen, strncmp.
> > The resulting optimized routines are then selected by the resolver function
> > if the Zbb extension is present at runtime.
> >
> > To use target function attributes, a few issues had to be resovled:
> > - The functions above got a mechanism to be compiled with function attributes
> >   (patches 2-7).  Only those routines have been touched, which are
> >   required for the purpose of this patchset.
> > - Ensuring that inlined functions also get the same function attributes
> >   (first patch).
> > - Add mechanism to explicitly enable the orc.b optimization for string functions
> >   (patch 8), which is a bit inspired by USE_FFS_BUILTIN.
> >
> > One of the design questions is, if Zbb represents a broad enough optimization
> > target.  Tests with Zb* extensions showed, that no further code improvements
> > can be achieved with them.  Also most other extensions likely won't affect
> > the generated code for string routines (ignoring vector instructions, which
> > are a different topic).  Therefore, Zbb seemed like a sufficient target.
> >
> > This series was tested by writing a simple test program to invoke the
> > libc routines (e.g. strcmp) and a modified QEMU that reports the
> > emulation of orc.b on stderr.  With that the QEMU can be used to test
> > if the optimized routines are executed (-cpu "rv64,zbb=[false,true]").
> > Further, this series was tested with SPEC CPU 2017 intrate with Zbb
> > enabled.  The function attribute detection mechanism was tested with
> > GCC 13 and GCC 14.
> >
>
> I tried check this patchset with gcc 14 [1] (commit 6f6103ccc5b3bf8cb,
> built with build-many-glibcs.py) and it shows an ICE:
>
> $ riscv64-glibc-linux-gnu-gcc ../sysdeps/riscv/multiarch/memchr-zbb.c [...]
> In file included from ../include/bits/string_fortified.h:1,
>                  from ../string/string.h:548,
>                  from ../include/string.h:60,
>                  from ../string/memchr.c:24,
>                  from ../sysdeps/riscv/multiarch/memchr-zbb.c:23:
> ../string/bits/string_fortified.h:110:1: internal compiler error: in riscv_func_target_put, at common/config/riscv/riscv-common.cc:510
>   110 | {
>       | ^
> 0x7af52762a1c9 __libc_start_call_main
>         ../sysdeps/nptl/libc_start_call_main.h:58
> 0x7af52762a28a __libc_start_main_impl
>         ../csu/libc-start.c:360
>
> I am building with --enable-fortify-source=2, with --enable-fortify-source=no
> the build does not fail. A gcc 13 build [2] seems to be in better shape but
> the version I am using does not have support for required attributes
> (HAVE_RISCV_FATTRIBUTE_ZBB is set to 0).
>
> So I think we will need to check whether this is still happens with
> gcc master/15 and see if indeed it was a regression added on gcc 14.

I can reproduce the issue with gcc 14 and master/15.

While analysing this, I've discovered two issues in GCC
when processing target-arch attributes:
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115554
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115562

Both issues trigger an ICE because of a violated assertion in
riscv_func_target_put().
While the first issue could be fixed by replacing the assertion with
an error message,
the second requires more work. Unfortunately, it is the second issue
that is causing
this patchset to not build.

GCC 13 or earlier are not affected, because the RISC-V backend did not support
target-arch attributes back then.

Thanks,
Christoph

> [1] gcc version 14.1.1 20240619 [releases/gcc-14 r14-10324-g6f6103ccc5b]
> [2] gcc version 13.1.1 20230525 [releases/gcc-13 r13-7376-ge80487dcbe2]

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2024-06-20 15:52 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-27 11:18 [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 01/15] cdefs: Add mechanism to add attributes to __always_inline functions Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 02/15] string/memchr: Add mechanism to set function attributes Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 03/15] string/memrchr: " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 04/15] string/strchrnul: " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 05/15] string/strcmp: " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 06/15] string/strlen: " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 07/15] string/strncmp: " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 08/15] RISC-V: string-fz[a,i].h: Make orc.b optimization explicit Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 09/15] RISC-V: Add compiler test for Zbb function attribute support Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 10/15] RISC-V: Add Zbb optimized memchr as ifunc Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 11/15] RISC-V: Add Zbb optimized memrchr " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 12/15] RISC-V: Add Zbb optimized strchrnul " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 13/15] RISC-V: Add Zbb optimized strcmp " Christoph Müllner
2024-05-27 11:18 ` [PATCH v2 14/15] RISC-V: Add Zbb optimized strlen " Christoph Müllner
2024-05-27 11:19 ` [PATCH v2 15/15] RISC-V: Add Zbb optimized strncmp " Christoph Müllner
2024-06-03 21:08 ` [PATCH v2 00/15] RISC-V: Add Zbb-optimized string routines as ifuncs Christoph Müllner
2024-06-11 10:00   ` Christoph Müllner
2024-06-19 14:26 ` Adhemerval Zanella Netto
2024-06-20 15:52   ` Christoph Müllner

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).