Skip to contentSkip to conversation

x86/string: Clean up the declarations of __builtin_mem*()

PATCH In review Andrew Cooper 3 participants 11 messages May 12, 2017 – May 15, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 x86/string: Clean up the declarations of __builtin_mem*()

None of the GCC docs mention memmove() in its list of builtins even today, but 4.1 does have the builtin, meaning that all currently supported compilers have it.

Rather than #undef'ing macros to avoid altering the function names, use the method recommended by the C specification by enclosing the function name in brackets to avoid the macro being expanded. This means that optimisation opportunities continue to work in the rest of the translation unit.

---
CC: Jan Beulich <[email protected]>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=21602 confirms that the builtin was present in 4.1.0

---
 xen/arch/x86/string.c        | 9 +++------
 xen/include/asm-x86/string.h | 7 +++----
 2 files changed, 6 insertions(+), 10 deletions(-)
xen/arch/x86/string.c +3 -6
index 1387dfb..f4d7f2a 100644
@@ -7,8 +7,7 @@
 
 #include <xen/lib.h>
 
-#undef memcpy
-void *memcpy(void *dest, const void *src, size_t n)
+void *(memcpy)(void *dest, const void *src, size_t n)
 {
     long d0, d1, d2;
 
@@ -23,8 +22,7 @@ void *memcpy(void *dest, const void *src, size_t n)
     return dest;
 }
 
-#undef memset
-void *memset(void *s, int c, size_t n)
+void *(memset)(void *s, int c, size_t n)
 {
     long d0, d1;
 
@@ -37,8 +35,7 @@ void *memset(void *s, int c, size_t n)
     return s;
 }
 
-#undef memmove
-void *memmove(void *dest, const void *src, size_t n)
+void *(memmove)(void *dest, const void *src, size_t n)
 {
     long d0, d1, d2;
 
xen/include/asm-x86/string.h +3 -4
index c48d9c3..d636e82 100644
@@ -2,13 +2,12 @@
 #define __X86_STRING_H__
 
 #define __HAVE_ARCH_MEMCPY
-#define memcpy(t,f,n) (__builtin_memcpy((t),(f),(n)))
+#define memcpy(d, s, n)       __builtin_memcpy(d, s, n)
 
-/* Some versions of gcc don't have this builtin. It's non-critical anyway. */
 #define __HAVE_ARCH_MEMMOVE
-extern void *memmove(void *dest, const void *src, size_t n);
+#define memmove(d, s, n)      __builtin_memmove(d, s, n)
 
 #define __HAVE_ARCH_MEMSET
-#define memset(s,c,n) (__builtin_memset((s),(c),(n)))
+#define memset(s, c, n)       __builtin_memset(s, c, n)
 
 #endif /* __X86_STRING_H__ */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH v2 2/2 x86/string: Use compiler __builtin_str*() where possible

The use of -fno-builtin inhibits this automatic transformation. Manually transform the callsites. This causes constructs such as strlen("literal") to be evaluated at compile time, and certain simple operations to be replaced with repeated string operations.

---
CC: Jan Beulich <[email protected]>

v2:
* Fix the build with Clang, which objects when the define renames the underlying implementation.

---
 xen/common/string.c          | 14 +++++++-------
 xen/include/asm-x86/string.h |  8 ++++++++
 2 files changed, 15 insertions(+), 7 deletions(-)
xen/common/string.c +7 -7
index 9a5a4ba..5302db0 100644
@@ -42,7 +42,7 @@ int strnicmp(const char *s1, const char *s2, size_t len)
 #endif
 
 #ifndef __HAVE_ARCH_STRCASECMP
-int strcasecmp(const char *s1, const char *s2)
+int (strcasecmp)(const char *s1, const char *s2)
 {
     int c1, c2;
 
@@ -117,7 +117,7 @@ EXPORT_SYMBOL(strlcat);
  * @cs: One string
  * @ct: Another string
  */
-int strcmp(const char * cs,const char * ct)
+int (strcmp)(const char *cs, const char *ct)
 {
 	register signed char __res;
 
@@ -137,7 +137,7 @@ int strcmp(const char * cs,const char * ct)
  * @ct: Another string
  * @count: The maximum number of bytes to compare
  */
-int strncmp(const char * cs,const char * ct,size_t count)
+int (strncmp)(const char *cs, const char *ct, size_t count)
 {
 	register signed char __res = 0;
 
@@ -157,7 +157,7 @@ int strncmp(const char * cs,const char * ct,size_t count)
  * @s: The string to be searched
  * @c: The character to search for
  */
-char * strchr(const char * s, int c)
+char *(strchr)(const char *s, int c)
 {
 	for(; *s != (char) c; ++s)
 		if (*s == '\0')
@@ -172,7 +172,7 @@ char * strchr(const char * s, int c)
  * @s: The string to be searched
  * @c: The character to search for
  */
-char * strrchr(const char * s, int c)
+char *(strrchr)(const char *s, int c)
 {
        const char *p = s + strlen(s);
        do {
@@ -188,7 +188,7 @@ char * strrchr(const char * s, int c)
  * strlen - Find the length of a string
  * @s: The string to be sized
  */
-size_t strlen(const char * s)
+size_t (strlen)(const char * s)
 {
 	const char *sc;
 
@@ -409,7 +409,7 @@ void * memscan(void * addr, int c, size_t size)
  * @s1: The string to be searched
  * @s2: The string to search for
  */
-char * strstr(const char * s1,const char * s2)
+char *(strstr)(const char *s1, const char *s2)
 {
 	int l1, l2;
 
xen/include/asm-x86/string.h +8 -0
index d636e82..535fc0e 100644
@@ -10,4 +10,12 @@
 #define __HAVE_ARCH_MEMSET
 #define memset(s, c, n)       __builtin_memset(s, c, n)
 
+#define strcmp(s1, s2)        __builtin_strcmp(s1, s2)
+#define strncmp(s1, s2, n)    __builtin_strncmp(s1, s2, n)
+#define strcasecmp(s1, s2)    __builtin_strcasecmp(s1, s2)
+#define strchr(s1, c)         __builtin_strchr(s1, c)
+#define strrchr(s1, c)        __builtin_strrchr(s1, c)
+#define strstr(s1, s2)        __builtin_strstr(s1, s2)
+#define strlen(s1)            __builtin_strlen(s1)
+
 #endif /* __X86_STRING_H__ */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH v2 for-next 2/2] x86/string: Use compiler __builtin_str*() where possible

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:

xen/include/asm-x86/string.h line 19

Quoted 10 lines
xen/include/asm-x86/string.h +7 -0
@@ -10,4 +10,12 @@
 #define __HAVE_ARCH_MEMSET
 #define memset(s, c, n)       __builtin_memset(s, c, n)
 
+#define strcmp(s1, s2)        __builtin_strcmp(s1, s2)
+#define strncmp(s1, s2, n)    __builtin_strncmp(s1, s2, n)
+#define strcasecmp(s1, s2)    __builtin_strcasecmp(s1, s2)
+#define strchr(s1, c)         __builtin_strchr(s1, c)
+#define strrchr(s1, c)        __builtin_strrchr(s1, c)
+#define strstr(s1, s2)        __builtin_strstr(s1, s2)
+#define strlen(s1)            __builtin_strlen(s1)

If the lack of __HAVE_ARCH_* additions is intentional here, why do you keep them for mem*()? In any event does their absence result in __builtin_str*() being (re-)declared in xen/string.h, which is at least pointless.

Of course the same reservations regarding function pointer use apply here.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw

Re: [PATCH v2 for-next 2/2] x86/string: Use compiler __builtin_str*() where possible

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:
xen/include/asm-x86/string.h +0 -0
@@ -10,4 +10,12 @@
 #define __HAVE_ARCH_MEMSET
 #define memset(s, c, n)       __builtin_memset(s, c, n)
 
Quoted 8 lines

xen/include/asm-x86/string.h line 19

Quoted 7 lines
xen/include/asm-x86/string.h +7 -0
+#define strcmp(s1, s2)        __builtin_strcmp(s1, s2)
+#define strncmp(s1, s2, n)    __builtin_strncmp(s1, s2, n)
+#define strcasecmp(s1, s2)    __builtin_strcasecmp(s1, s2)
+#define strchr(s1, c)         __builtin_strchr(s1, c)
+#define strrchr(s1, c)        __builtin_strrchr(s1, c)
+#define strstr(s1, s2)        __builtin_strstr(s1, s2)
+#define strlen(s1)            __builtin_strlen(s1)

If the lack of __HAVE_ARCH_* additions is intentional here,

Yes - it is deliberate.

Quoted 1 line

why do you keep them for mem*()?

We have x86-specific implementation of mem*(), while we use the common implementation of str*().

Defining __HAVE_ARCH_STR* causes the common implementation to be omitted, resulting in a link failure.

Given that all supported compilers have these builtins, I think it might be better to make this adjustment in common code. The arguments for using them in x86 are the same as ARM.

Julien/Stefano: Thoughts?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH v2 for-next 2/2] x86/string: Use compiler __builtin_str*() where possible

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 17:30, <[email protected]> wrote:

Given that all supported compilers have these builtins, I think it might be better to make this adjustment in common code. The arguments for using them in x86 are the same as ARM.

Generally I agree. But Linux not having used them originally was
- iirc - because there were a few compiler bugs. This could be the case namely on arm64, which isn't all that old (and hence mature) yet. Otoh the compiler folks may also have learned from past mistakes.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

Re: [PATCH v2 for-next 2/2] x86/string: Use compiler __builtin_str*() where possible

In reply to Andrew Cooper

Hi Andrew,

Quoted 28 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:
xen/include/asm-x86/string.h +0 -0
@@ -10,4 +10,12 @@
 #define __HAVE_ARCH_MEMSET
 #define memset(s, c, n)       __builtin_memset(s, c, n)
 
Quoted 8 lines

xen/include/asm-x86/string.h line 19

Quoted 7 lines
xen/include/asm-x86/string.h +7 -0
+#define strcmp(s1, s2)        __builtin_strcmp(s1, s2)
+#define strncmp(s1, s2, n)    __builtin_strncmp(s1, s2, n)
+#define strcasecmp(s1, s2)    __builtin_strcasecmp(s1, s2)
+#define strchr(s1, c)         __builtin_strchr(s1, c)
+#define strrchr(s1, c)        __builtin_strrchr(s1, c)
+#define strstr(s1, s2)        __builtin_strstr(s1, s2)
+#define strlen(s1)            __builtin_strlen(s1)

If the lack of __HAVE_ARCH_* additions is intentional here,

Yes - it is deliberate.

Quoted 1 line

why do you keep them for mem*()?

We have x86-specific implementation of mem*(), while we use the common implementation of str*().

Defining __HAVE_ARCH_STR* causes the common implementation to be omitted, resulting in a link failure.

Quoted 5 lines

Given that all supported compilers have these builtins, I think it might be better to make this adjustment in common code. The arguments for using them in x86 are the same as ARM.

Julien/Stefano: Thoughts?

Using our own implementation rather than the built-in version gives us the liberty to do optimization based on the processor using alternative.

I know that we already use built-in for arch_fetch_and_add, but I am planning to revert that as we want to take advantage of new atomics instruction in ARMv8.1.

Furthemore, someone mentioned a potential legal issue to linked against the built-in. Anyone heard about that?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw

Re: [PATCH v2 for-next 2/2] x86/string: Use compiler __builtin_str*() where possible

Quoted 35 lines · Julien Grall

Hi Andrew,

Quoted 32 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:
xen/include/asm-x86/string.h +0 -0
@@ -10,4 +10,12 @@
 #define __HAVE_ARCH_MEMSET
 #define memset(s, c, n)       __builtin_memset(s, c, n)
 
Quoted 8 lines

xen/include/asm-x86/string.h line 19

Quoted 7 lines
xen/include/asm-x86/string.h +7 -0
+#define strcmp(s1, s2)        __builtin_strcmp(s1, s2)
+#define strncmp(s1, s2, n)    __builtin_strncmp(s1, s2, n)
+#define strcasecmp(s1, s2)    __builtin_strcasecmp(s1, s2)
+#define strchr(s1, c)         __builtin_strchr(s1, c)
+#define strrchr(s1, c)        __builtin_strrchr(s1, c)
+#define strstr(s1, s2)        __builtin_strstr(s1, s2)
+#define strlen(s1)            __builtin_strlen(s1)

If the lack of __HAVE_ARCH_* additions is intentional here,

Yes - it is deliberate.

Quoted 1 line

why do you keep them for mem*()?

We have x86-specific implementation of mem*(), while we use the common implementation of str*().

Defining __HAVE_ARCH_STR* causes the common implementation to be omitted, resulting in a link failure.

Given that all supported compilers have these builtins, I think it might be better to make this adjustment in common code. The arguments for using them in x86 are the same as ARM.

Quoted 8 lines
Quoted 1 line

Julien/Stefano: Thoughts?

Using our own implementation rather than the built-in version gives us the liberty to do optimization based on the processor using alternative.

I know that we already use built-in for arch_fetch_and_add, but I am planning to revert that as we want to take advantage of new atomics instruction in ARMv8.1.

Of course, my "adjustment to common code" would only apply in the case that an arch doesn't define __HAVE_ARCH_*.

If an arch has an override, that would take priority.

Quoted 3 lines

Furthemore, someone mentioned a potential legal issue to linked against the built-in. Anyone heard about that?

The main use of these are optimising things like strlen("literal") at compile time. The use of -fno-bultin disables this optimisation, but as we dont link against any runtime libraries, we still provide the underling implementation in the case that __builtin_*() fall back to a straight function call.

I can't see any possibility for legal issues here. It is all down to exactly which instructions your compiler wants to assemble.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw

Re: [PATCH v2 for-next 2/2] x86/string: Use compiler __builtin_str*() where possible

Hi Andrew,

Quoted 5 lines · Andrew Cooper
Quoted 3 lines · Julien Grall

Furthemore, someone mentioned a potential legal issue to linked against the built-in. Anyone heard about that?

Quoted 8 lines

The main use of these are optimising things like strlen("literal") at compile time. The use of -fno-bultin disables this optimisation, but as we dont link against any runtime libraries, we still provide the underling implementation in the case that __builtin_*() fall back to a straight function call.

I can't see any possibility for legal issues here. It is all down to exactly which instructions your compiler wants to assemble.

Thank you for the explanation. I misundertood what you suggested. I guess this should be fine for ARM.

I will have a look if we can leverage built-in too.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#8 lore raw
In reply to Andrew Cooper
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:

Rather than #undef'ing macros to avoid altering the function names, use the method recommended by the C specification by enclosing the function name in brackets to avoid the macro being expanded. This means that optimisation opportunities continue to work in the rest of the translation unit.

That's the upside. The downside is that ...

Quoted 7 lines
xen/include/asm-x86/string.h +0 -1
@@ -2,13 +2,12 @@
 #define __X86_STRING_H__
 
 #define __HAVE_ARCH_MEMCPY
-#define memcpy(t,f,n) (__builtin_memcpy((t),(f),(n)))

xen/include/asm-x86/string.h line 11

Quoted 10 lines
xen/include/asm-x86/string.h +3 -3
+#define memcpy(d, s, n)       __builtin_memcpy(d, s, n)
 
-/* Some versions of gcc don't have this builtin. It's non-critical anyway. */
 #define __HAVE_ARCH_MEMMOVE
-extern void *memmove(void *dest, const void *src, size_t n);
+#define memmove(d, s, n)      __builtin_memmove(d, s, n)
 
 #define __HAVE_ARCH_MEMSET
-#define memset(s,c,n) (__builtin_memset((s),(c),(n)))
+#define memset(s, c, n)       __builtin_memset(s, c, n)

with these kinds of #define-s (and with xen/string.h including asm/string.h before the declarations, which don't get activated anyway due to the __HAVE_ARCH_MEM* symbols being #define-d in the context above) you can't use the symbols in places where function pointers are wanted. IOW I'd prefer the #undef-s to stay (and string.c explicitly calling the builtins) and the macros to change to object like ones.

An alternative would be to move the (or add x86 specific) declarations, making sure the symbols are always available.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#9 lore raw
Quoted 32 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:

Rather than #undef'ing macros to avoid altering the function names, use the method recommended by the C specification by enclosing the function name in brackets to avoid the macro being expanded. This means that optimisation opportunities continue to work in the rest of the translation unit.

That's the upside. The downside is that ...

Quoted 7 lines
xen/include/asm-x86/string.h +0 -1
@@ -2,13 +2,12 @@
 #define __X86_STRING_H__
 
 #define __HAVE_ARCH_MEMCPY
-#define memcpy(t,f,n) (__builtin_memcpy((t),(f),(n)))

xen/include/asm-x86/string.h line 11

Quoted 10 lines
xen/include/asm-x86/string.h +3 -3
+#define memcpy(d, s, n)       __builtin_memcpy(d, s, n)
 
-/* Some versions of gcc don't have this builtin. It's non-critical anyway. */
 #define __HAVE_ARCH_MEMMOVE
-extern void *memmove(void *dest, const void *src, size_t n);
+#define memmove(d, s, n)      __builtin_memmove(d, s, n)
 
 #define __HAVE_ARCH_MEMSET
-#define memset(s,c,n) (__builtin_memset((s),(c),(n)))
+#define memset(s, c, n)       __builtin_memset(s, c, n)

with these kinds of #define-s (and with xen/string.h including asm/string.h before the declarations, which don't get activated anyway due to the __HAVE_ARCH_MEM* symbols being #define-d in the context above) you can't use the symbols in places where function pointers are wanted. IOW I'd prefer the #undef-s to stay (and string.c explicitly calling the builtins) and the macros to change to object like ones.

Quoted 2 lines

An alternative would be to move the (or add x86 specific) declarations, making sure the symbols are always available.

Unfortunately, Clang objects to using object-like macros

setup.c:1671:29: error: builtin functions must be directly called

    printk("Test: %zu", foo(__builtin_strlen));

The only way this is going to work is in the same style as how the compiler header files already do it, which is in the style above. I will see about fixing the symbol declarations so they can be used as function pointers.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#10 lore raw
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 17:20, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 12.05.17 at 16:34, <[email protected]> wrote:

Rather than #undef'ing macros to avoid altering the function names, use the method recommended by the C specification by enclosing the function name in brackets to avoid the macro being expanded. This means that optimisation opportunities continue to work in the rest of the translation unit.

That's the upside. The downside is that ...

Quoted 7 lines
xen/include/asm-x86/string.h +0 -1
@@ -2,13 +2,12 @@
 #define __X86_STRING_H__
 
 #define __HAVE_ARCH_MEMCPY
-#define memcpy(t,f,n) (__builtin_memcpy((t),(f),(n)))

xen/include/asm-x86/string.h line 11

Quoted 10 lines
xen/include/asm-x86/string.h +3 -3
+#define memcpy(d, s, n)       __builtin_memcpy(d, s, n)
 
-/* Some versions of gcc don't have this builtin. It's non-critical anyway. */
 #define __HAVE_ARCH_MEMMOVE
-extern void *memmove(void *dest, const void *src, size_t n);
+#define memmove(d, s, n)      __builtin_memmove(d, s, n)
 
 #define __HAVE_ARCH_MEMSET
-#define memset(s,c,n) (__builtin_memset((s),(c),(n)))
+#define memset(s, c, n)       __builtin_memset(s, c, n)

with these kinds of #define-s (and with xen/string.h including asm/string.h before the declarations, which don't get activated anyway due to the __HAVE_ARCH_MEM* symbols being #define-d in the context above) you can't use the symbols in places where function pointers are wanted. IOW I'd prefer the #undef-s to stay (and string.c explicitly calling the builtins) and the macros to change to object like ones.

Quoted 7 lines
Quoted 2 lines

An alternative would be to move the (or add x86 specific) declarations, making sure the symbols are always available.

Unfortunately, Clang objects to using object-like macros

setup.c:1671:29: error: builtin functions must be directly called

    printk("Test: %zu", foo(__builtin_strlen));

Which makes sense, as they aren't actual functions.

Quoted 4 lines

The only way this is going to work is in the same style as how the compiler header files already do it, which is in the style above. I will see about fixing the symbol declarations so they can be used as function pointers.

Thanks.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel