Skip to contentSkip to conversation

compiler.h: remove GCC < 3 __builtin_expect fallback

RFC PATCH In review Philippe Mathieu-Daudé 4 participants 6 messages Dec 11, 2020 – Dec 12, 2020

lore
Share & tools
lore
#0 lore raw

RFC PATCH v4 1/1 compiler.h: remove GCC < 3 __builtin_expect fallback

Since commit efc6c07 ("configure: Add a test for the minimum compiler version"), QEMU explicitely depends on GCC >= 4.8.

(clang >= 3.4 advertizes itself as GCC >= 4.2 compatible and supports __builtin_expect too)

[PMD: #error if likely/unlikely already defined]

---
Supersedes: <[email protected]>

---
 include/qemu/compiler.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
include/qemu/compiler.h +2 -5
index c76281f3540..ae1aee79c8d 100644
@@ -43,14 +43,11 @@
 #define tostring(s)	#s
 #endif
 
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
+#if defined(likely) || defined(unlikely)
+#error building with likely/unlikely is not supported
 #endif
-
 #define likely(x)   __builtin_expect(!!(x), 1)
 #define unlikely(x)   __builtin_expect(!!(x), 0)
-#endif
 
 #ifndef container_of
 #define container_of(ptr, type, member) ({                      \
Signature · 1 line
2.26.2
Reviewed-by on v4 1/1
#1 lore raw
Quoted 20 lines · Philippe Mathieu-Daudé

Since commit efc6c07 ("configure: Add a test for the minimum compiler version"), QEMU explicitely depends on GCC >= 4.8.

(clang >= 3.4 advertizes itself as GCC >= 4.2 compatible and supports __builtin_expect too)

[PMD: #error if likely/unlikely already defined]

---
Supersedes: <[email protected]>

---
 include/qemu/compiler.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
include/qemu/compiler.h +0 -0
index c76281f3540..ae1aee79c8d 100644

include/qemu/compiler.h line 47

Quoted 8 lines
include/qemu/compiler.h +2 -3
@@ -43,14 +43,11 @@
 #define tostring(s)	#s
 #endif
 
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
+#if defined(likely) || defined(unlikely)
+#error building with likely/unlikely is not supported

I'd put the argument of #error as a single string constant delimited by quotes ", doesn't matter in this case though as there are no problem characters in the string.

include/qemu/compiler.h line 53

Quoted 9 lines
include/qemu/compiler.h +0 -2
 #endif
-
 #define likely(x)   __builtin_expect(!!(x), 1)
 #define unlikely(x)   __builtin_expect(!!(x), 0)
-#endif
 
 #ifndef container_of
 #define container_of(ptr, type, member) ({                      \
#2 lore raw
Quoted 24 lines · Claudio Fontana
Quoted 23 lines · Philippe Mathieu-Daudé

Since commit efc6c07 ("configure: Add a test for the minimum compiler version"), QEMU explicitely depends on GCC >= 4.8.

(clang >= 3.4 advertizes itself as GCC >= 4.2 compatible and supports __builtin_expect too)

[PMD: #error if likely/unlikely already defined]

---
Supersedes: <[email protected]>

---
 include/qemu/compiler.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
include/qemu/compiler.h +0 -0
index c76281f3540..ae1aee79c8d 100644
@@ -43,14 +43,11 @@
 #define tostring(s)	#s
 #endif
 
Quoted 8 lines

include/qemu/compiler.h line 47

Quoted 5 lines
include/qemu/compiler.h +2 -3
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
+#if defined(likely) || defined(unlikely)
+#error building with likely/unlikely is not supported

I'd put the argument of #error as a single string constant delimited by quotes ", doesn't matter in this case though as there are no problem characters in the string.

This is the style I prefer too, but I this seems the QEMU style...:

$ git grep -E \#\s*error|wc -l
82
$ git grep -E \#\s*error|fgrep \"|wc -l
11
Quoted 13 lines

include/qemu/compiler.h line 53

Quoted 10 lines
include/qemu/compiler.h +0 -2
 #endif
-
 #define likely(x)   __builtin_expect(!!(x), 1)
 #define unlikely(x)   __builtin_expect(!!(x), 0)
-#endif
 
 #ifndef container_of
 #define container_of(ptr, type, member) ({                      \
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 21 lines · Philippe Mathieu-Daudé

Since commit efc6c07 ("configure: Add a test for the minimum compiler version"), QEMU explicitely depends on GCC >= 4.8.

(clang >= 3.4 advertizes itself as GCC >= 4.2 compatible and supports __builtin_expect too)

[PMD: #error if likely/unlikely already defined]

---
Supersedes: <[email protected]>

---
 include/qemu/compiler.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
include/qemu/compiler.h +0 -0
index c76281f3540..ae1aee79c8d 100644

include/qemu/compiler.h line 47

Quoted 8 lines
include/qemu/compiler.h +2 -3
@@ -43,14 +43,11 @@
 #define tostring(s)    #s
 #endif
 
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
+#if defined(likely) || defined(unlikely)
+#error building with likely/unlikely is not supported

When exactly will the system headers have 'likely' defined, and when would they define it to something other than the obvious __builtin_expect() definition the way we do?

likely() and unlikely() in my view fall into a category of macros like "container_of()" which aren't defined by a system header but which do have a standard known set of semantics.

I think there are two reasonable approaches:
(1) just define the macro, on the assumption that the
system headers won't have done (because these aren't standard macros)
(2) as we do with container_of() currently, wrap our
definitions in #ifndef whatever, so that we assume that whatever version we might have got from the system is fine

I don't think there's any point in explicitly #error-ing here:
in fact, it makes the diagnostic to the user less useful, because instead of the compiler complaining about the macro being defined twice and giving both locations where it was defined, now it won't tell the user where the other definition was...

I think my preference would be "just drop the ifdef", but there isn't much in it really.

thanks
-- PMM

#4 lore raw
Quoted 25 lines · Peter Maydell
Quoted 24 lines · Philippe Mathieu-Daudé

Since commit efc6c07 ("configure: Add a test for the minimum compiler version"), QEMU explicitely depends on GCC >= 4.8.

(clang >= 3.4 advertizes itself as GCC >= 4.2 compatible and supports __builtin_expect too)

[PMD: #error if likely/unlikely already defined]

---
Supersedes: <[email protected]>

---
 include/qemu/compiler.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
include/qemu/compiler.h +0 -0
index c76281f3540..ae1aee79c8d 100644
@@ -43,14 +43,11 @@
 #define tostring(s)    #s
 #endif
 
Quoted 9 lines

include/qemu/compiler.h line 47

Quoted 5 lines
include/qemu/compiler.h +2 -3
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
+#if defined(likely) || defined(unlikely)
+#error building with likely/unlikely is not supported

When exactly will the system headers have 'likely' defined, and when would they define it to something other than the obvious __builtin_expect() definition the way we do?

Since there was a check, I tried to be extra-cautious (better safe than sorry).

Quoted 12 lines

likely() and unlikely() in my view fall into a category of macros like "container_of()" which aren't defined by a system header but which do have a standard known set of semantics.

I think there are two reasonable approaches:
(1) just define the macro, on the assumption that the
system headers won't have done (because these aren't standard macros)
(2) as we do with container_of() currently, wrap our
definitions in #ifndef whatever, so that we assume that whatever version we might have got from the system is fine

Quoted 6 lines

I don't think there's any point in explicitly #error-ing here:
in fact, it makes the diagnostic to the user less useful, because instead of the compiler complaining about the macro being defined twice and giving both locations where it was defined, now it won't tell the user where the other definition was...

"diagnostic less useful" is a good reason (to invalidate this patch).

Quoted 2 lines

I think my preference would be "just drop the ifdef", but there isn't much in it really.

Yes, let's stick to Marc-André v3.

Thanks for your review!

Phil.

#5 lore raw

Hi

Quoted 62 lines · Philippe Mathieu-Daudé
Quoted 1 line · Peter Maydell

On Fri, 11 Dec 2020 at 13:13, Philippe Mathieu-Daudé <[email protected]>

Quoted 24 lines · wrote:
Quoted 24 lines

Since commit efc6c07 ("configure: Add a test for the minimum compiler version"), QEMU explicitely depends on GCC >= 4.8.

(clang >= 3.4 advertizes itself as GCC >= 4.2 compatible and supports __builtin_expect too)

[PMD: #error if likely/unlikely already defined]

---
Supersedes: <[email protected]>

---
 include/qemu/compiler.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
include/qemu/compiler.h +0 -0
index c76281f3540..ae1aee79c8d 100644
@@ -43,14 +43,11 @@
 #define tostring(s)    #s
 #endif
 
Quoted 9 lines

include/qemu/compiler.h line 47

Quoted 5 lines
include/qemu/compiler.h +2 -3
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
+#if defined(likely) || defined(unlikely)
+#error building with likely/unlikely is not supported

When exactly will the system headers have 'likely' defined, and when would they define it to something other than the obvious __builtin_expect() definition the way we do?

Since there was a check, I tried to be extra-cautious (better safe than sorry).

Quoted 12 lines

likely() and unlikely() in my view fall into a category of macros like "container_of()" which aren't defined by a system header but which do have a standard known set of semantics.

I think there are two reasonable approaches:
(1) just define the macro, on the assumption that the
system headers won't have done (because these aren't standard macros)
(2) as we do with container_of() currently, wrap our
definitions in #ifndef whatever, so that we assume that whatever version we might have got from the system is fine

Quoted 6 lines

I don't think there's any point in explicitly #error-ing here:
in fact, it makes the diagnostic to the user less useful, because instead of the compiler complaining about the macro being defined twice and giving both locations where it was defined, now it won't tell the user where the other definition was...

"diagnostic less useful" is a good reason (to invalidate this patch).

Quoted 7 lines
Quoted 2 lines

I think my preference would be "just drop the ifdef", but there isn't much in it really.

Yes, let's stick to Marc-André v3.

Thanks for your review!

Ok to r-b v3 then?
thanks

Signature · 1 line
Marc-André Lureau