Skip to contentSkip to conversation

meson: ensure we enable CMPXCHG128 on x86_64

PATCH Superseded Paolo Bonzini 5 participants 11 messages Oct 7, 2024 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover meson: ensure we enable CMPXCHG128 on x86_64

Alex discovered that CMPXCHG128 was not enabled when building for x86_64, resulting in slow execution for wide atomic instructions, creating a huge contention when combined with a high number of cpus (found while booting android aarch64 guest on x86_64 host).

The problem is that even though we enable -mcx16 option for x86_64, this is not used when testing for CMPXCHG128. Thus, we silently turn it off.

x86_64 is the only architecture adding machine flags for now, so the problem is limited to this host architecture. However, the problem is generic, so define a new variable for all the -m options, so that they can be used for other such tests in the future.

Based-on: <[email protected]>
Supersedes: <[email protected]>

Paolo Bonzini (2):
meson: define qemu_isa_flags
meson: ensure -mcx16 is passed when detecting ATOMIC128

 meson.build | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
Signature · 1 line
2.46.1
#1 lore raw

PATCH v2 1/2 meson: define qemu_isa_flags

Create a separate variable for compiler flags that enable specific instruction set extensions, so that they can be used with cc.compiles/cc.links.

Note that -mfpmath=sse is a code generation option but it does not enable new instructions, therefore I did not make it part of qemu_isa_flags.

---
 meson.build | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
meson.build +15 -9
index 76335bb2a51..bbb0f6d9bcf 100644
@@ -335,6 +335,10 @@ elif host_os == 'windows'
   endif
 endif
 
+# Choose instruction set (currently x86-only)
+
+qemu_isa_flags = []
+
 # __sync_fetch_and_and requires at least -march=i486. Many toolchains
 # use i686 as default anyway, but for those that don't, an explicit
 # specification is necessary
@@ -351,7 +355,7 @@ if host_arch == 'i386' and not cc.links('''
     sfaa(&val);
     return val;
   }''')
-  qemu_common_flags = ['-march=i486'] + qemu_common_flags
+  qemu_isa_flags = ['-march=i486']
 endif
 
 # Pick x86-64 baseline version
@@ -367,29 +371,31 @@ if host_arch in ['i386', 'x86_64']
     else
       # present on basically all processors but technically not part of
       # x86-64-v1, so only include -mneeded for x86-64 version 2 and above
-      qemu_common_flags = ['-mcx16'] + qemu_common_flags
+      qemu_isa_flags = ['-mcx16'] + qemu_isa_flags
     endif
   endif
   if get_option('x86_version') >= '2'
-    qemu_common_flags = ['-mpopcnt'] + qemu_common_flags
-    qemu_common_flags = cc.get_supported_arguments('-mneeded') + qemu_common_flags
+    qemu_isa_flags = ['-mpopcnt'] + qemu_isa_flags
+    qemu_isa_flags = cc.get_supported_arguments('-mneeded') + qemu_isa_flags
   endif
   if get_option('x86_version') >= '3'
-    qemu_common_flags = ['-mmovbe', '-mabm', '-mbmi', '-mbmi2', '-mfma', '-mf16c'] + qemu_common_flags
+    qemu_isa_flags = ['-mmovbe', '-mabm', '-mbmi', '-mbmi2', '-mfma', '-mf16c'] + qemu_isa_flags
   endif
 
   # add required vector instruction set (each level implies those below)
   if get_option('x86_version') == '1'
-    qemu_common_flags = ['-msse2'] + qemu_common_flags
+    qemu_isa_flags = ['-msse2'] + qemu_isa_flags
   elif get_option('x86_version') == '2'
-    qemu_common_flags = ['-msse4.2'] + qemu_common_flags
+    qemu_isa_flags = ['-msse4.2'] + qemu_isa_flags
   elif get_option('x86_version') == '3'
-    qemu_common_flags = ['-mavx2'] + qemu_common_flags
+    qemu_isa_flags = ['-mavx2'] + qemu_isa_flags
   elif get_option('x86_version') == '4'
-    qemu_common_flags = ['-mavx512f', '-mavx512bw', '-mavx512cd', '-mavx512dq', '-mavx512vl'] + qemu_common_flags
+    qemu_isa_flags = ['-mavx512f', '-mavx512bw', '-mavx512cd', '-mavx512dq', '-mavx512vl'] + qemu_isa_flags
   endif
 endif
 
+qemu_common_flags = qemu_isa_flags + qemu_common_flags
+
 if get_option('prefer_static')
   qemu_ldflags += get_option('b_pie') ? '-static-pie' : '-static'
 endif
Signature · 1 line
2.46.1
#2 lore raw

Re: [PATCH 1/2] meson: define qemu_isa_flags

Quoted 31 lines · Paolo Bonzini

Create a separate variable for compiler flags that enable specific instruction set extensions, so that they can be used with cc.compiles/cc.links.

Note that -mfpmath=sse is a code generation option but it does not enable new instructions, therefore I did not make it part of qemu_isa_flags.

---
  meson.build | 24 +++++++++++++++---------
  1 file changed, 15 insertions(+), 9 deletions(-)
meson.build +4 -0
index 76335bb2a51..bbb0f6d9bcf 100644
@@ -335,6 +335,10 @@ elif host_os == 'windows'
    endif
  endif
 
+# Choose instruction set (currently x86-only)
+
+qemu_isa_flags = []
+
  # __sync_fetch_and_and requires at least -march=i486. Many toolchains
  # use i686 as default anyway, but for those that don't, an explicit
  # specification is necessary

meson.build if host_arch == 'i386' and not cc.links(''' · line 358

Quoted 5 lines
meson.build +1 -1
@@ -351,7 +355,7 @@ if host_arch == 'i386' and not cc.links('''
      sfaa(&val);
      return val;
    }''')
-  qemu_common_flags = ['-march=i486'] + qemu_common_flags
+  qemu_isa_flags = ['-march=i486']

Use += ?

meson.build if host_arch in ['i386', 'x86_64'] · line 374

Quoted 2 lines
meson.build +1 -1
-      qemu_common_flags = ['-mcx16'] + qemu_common_flags
+      qemu_isa_flags = ['-mcx16'] + qemu_isa_flags

Likewise, why verbosely prepend, rather than append with += ? The same for all others, including

meson.build if host_arch in ['i386', 'x86_64'] · line 397

Quoted 1 line
meson.build +1 -0
+qemu_common_flags = qemu_isa_flags + qemu_common_flags

r~

#3 lore raw

Re: [PATCH 1/2] meson: define qemu_isa_flags

Quoted 4 lines · Richard Henderson

meson.build if host_arch == 'i386' and not cc.links(''' · line 358

Quoted 2 lines
meson.build +1 -1
-  qemu_common_flags = ['-march=i486'] + qemu_common_flags
+  qemu_isa_flags = ['-march=i486']

Use += ?

Here the qemu_isa_flags are known to be empty.

Quoted 7 lines

meson.build if host_arch in ['i386', 'x86_64'] · line 374

Quoted 2 lines
meson.build +1 -1
-      qemu_common_flags = ['-mcx16'] + qemu_common_flags
+      qemu_isa_flags = ['-mcx16'] + qemu_isa_flags

Likewise, why verbosely prepend, rather than append with += ? The same for all others, including

Quoted 1 line

+qemu_common_flags = qemu_isa_flags + qemu_common_flags

The prepending behavior came from CPU_CFLAGS originally being before all other compiler flags.

I just didn't want to change behavior here (especially since this patch was already about subtle changes in behavior). But you're right that at least within qemu_isa_flags the order should not matter.

Paolo

#4 lore raw

Re: [PATCH 1/2] meson: define qemu_isa_flags

Quoted 8 lines · Paolo Bonzini
Quoted 4 lines · Richard Henderson

meson.build if host_arch == 'i386' and not cc.links(''' · line 358

Quoted 2 lines
meson.build +1 -1
-  qemu_common_flags = ['-march=i486'] + qemu_common_flags
+  qemu_isa_flags = ['-march=i486']

Use += ?

Here the qemu_isa_flags are known to be empty.

... until we add something else beforehand and fail to update. Surely it's easier to assume that it's not empty.

Quoted 3 lines

I just didn't want to change behavior here (especially since this patch was already about subtle changes in behavior). But you're right that at least within qemu_isa_flags the order should not matter.

Fair enough.

r~

#5 lore raw

Re: [PATCH 1/2] meson: define qemu_isa_flags

In reply to Richard Henderson

07.10.2024 17:46, Richard Henderson пишет:

Quoted 30 lines · Paolo Bonzini
Quoted 29 lines

Create a separate variable for compiler flags that enable specific instruction set extensions, so that they can be used with cc.compiles/cc.links.

Note that -mfpmath=sse is a code generation option but it does not enable new instructions, therefore I did not make it part of qemu_isa_flags.

---
  meson.build | 24 +++++++++++++++---------

  1 file changed, 15 insertions(+), 9 deletions(-)
meson.build +0 -0
index 76335bb2a51..bbb0f6d9bcf 100644
@@ -335,6 +335,10 @@ elif host_os == 'windows'

    endif
  endif

meson.build +4 -0
+# Choose instruction set (currently x86-only)
+
+qemu_isa_flags = []
+

  # __sync_fetch_and_and requires at least -march=i486. Many toolchains

  # use i686 as default anyway, but for those that don't, an explicit
  # specification is necessary
Quoted 8 lines

meson.build if host_arch == 'i386' and not cc.links(''' · line 358

Quoted 6 lines
meson.build +1 -1
@@ -351,7 +355,7 @@ if host_arch == 'i386' and not cc.links('''
       sfaa(&val);
       return val;
     }''')
-  qemu_common_flags = ['-march=i486'] + qemu_common_flags
+  qemu_isa_flags = ['-march=i486']

Use += ?

Yes, it is better to use += here - when reviewing the patch this is obvious qemu_isa_flags is empty at this point, but it wont be as obvious in the actual code when initial assignment is quite far away from this prepending.

Quoted 7 lines

meson.build if host_arch in ['i386', 'x86_64'] · line 374

Quoted 2 lines
meson.build +1 -1
-      qemu_common_flags = ['-mcx16'] + qemu_common_flags
+      qemu_isa_flags = ['-mcx16'] + qemu_isa_flags

Likewise, why verbosely prepend, rather than append with += ? The same for all others, including

Quoted 1 line
meson.build +1 -0
+qemu_common_flags = qemu_isa_flags + qemu_common_flags

I'd keep prepending here (but not in qemu_isa_flags above) because this way it is possible to override some flags by the user.

/mjt

#6 lore raw

PATCH v2 2/2 meson: ensure -mcx16 is passed when detecting ATOMIC128

In reply to Paolo Bonzini

Moving -mcx16 out of CPU_CFLAGS caused the detection of ATOMIC128 to fail, because flags have to be specified by hand in cc.compiles and cc.links invocations (why oh why??).

Ensure that these tests enable all the instruction set extensions that will be used to build the emulators.

Fixes c2bf2ccb266 ("configure: move -mcx16 flag out of CPU_CFLAGS", 2024-05-24)
---
 meson.build | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
meson.build +5 -4
index bbb0f6d9bcf..4ceae98ee87 100644
@@ -2831,7 +2831,7 @@ config_host_data.set('CONFIG_ATOMIC64', cc.links('''
     __atomic_exchange_n(&x, y, __ATOMIC_RELAXED);
     __atomic_fetch_add(&x, y, __ATOMIC_RELAXED);
     return 0;
-  }'''))
+  }''', args: qemu_isa_flags))
 
 has_int128_type = cc.compiles('''
   __int128_t a;
@@ -2865,7 +2865,7 @@ if has_int128_type
       __atomic_compare_exchange_n(&p[4], &p[5], p[6], 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
       return 0;
     }'''
-  has_atomic128 = cc.links(atomic_test_128)
+  has_atomic128 = cc.links(atomic_test_128, args: qemu_isa_flags)
 
   config_host_data.set('CONFIG_ATOMIC128', has_atomic128)
 
@@ -2874,7 +2874,8 @@ if has_int128_type
     # without optimization enabled.  Try again with optimizations locally
     # enabled for the function.  See
     #   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107389
-    has_atomic128_opt = cc.links('__attribute__((optimize("O1")))' + atomic_test_128)
+    has_atomic128_opt = cc.links('__attribute__((optimize("O1")))' + atomic_test_128,
+                                 args: qemu_isa_flags)
     config_host_data.set('CONFIG_ATOMIC128_OPT', has_atomic128_opt)
 
     if not has_atomic128_opt
@@ -2885,7 +2886,7 @@ if has_int128_type
           __sync_val_compare_and_swap_16(&x, y, x);
           return 0;
         }
-      '''))
+      ''', args: qemu_isa_flags))
     endif
   endif
 endif
Signature · 1 line
2.46.1
Reviewed-by on v2 2/2
#7 lore raw

Re: [PATCH 2/2] meson: ensure -mcx16 is passed when detecting ATOMIC128

Quoted 7 lines · Paolo Bonzini

Moving -mcx16 out of CPU_CFLAGS caused the detection of ATOMIC128 to fail, because flags have to be specified by hand in cc.compiles and cc.links invocations (why oh why??).

Ensure that these tests enable all the instruction set extensions that will be used to build the emulators.

Quoted 7 lines
Fixes c2bf2ccb266 ("configure: move -mcx16 flag out of CPU_CFLAGS", 2024-05-24)
---
  meson.build | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)

r~

Reviewed-by on the v2 series
#8 lore raw
In reply to Paolo Bonzini
Quoted 17 lines · Paolo Bonzini

Alex discovered that CMPXCHG128 was not enabled when building for x86_64, resulting in slow execution for wide atomic instructions, creating a huge contention when combined with a high number of cpus (found while booting android aarch64 guest on x86_64 host).

The problem is that even though we enable -mcx16 option for x86_64, this is not used when testing for CMPXCHG128. Thus, we silently turn it off.

x86_64 is the only architecture adding machine flags for now, so the problem is limited to this host architecture. However, the problem is generic, so define a new variable for all the -m options, so that they can be used for other such tests in the future.

Based-on: <[email protected]>
Supersedes: <[email protected]>

Quoted 6 lines

Paolo Bonzini (2):
meson: define qemu_isa_flags
meson: ensure -mcx16 is passed when detecting ATOMIC128

  meson.build | 33 ++++++++++++++++++++-------------
  1 file changed, 20 insertions(+), 13 deletions(-)

Series:

Yeah, this is exactly what I thought about when replying to email by Pierrick Bouvier. Well, with less details ofc :)

Thanks,

/mjt

Reviewed-by Tested-by on the v2 series
#9 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Alex discovered that CMPXCHG128 was not enabled when building for x86_64, resulting in slow execution for wide atomic instructions, creating a huge contention when combined with a high number of cpus (found while booting android aarch64 guest on x86_64 host).

The problem is that even though we enable -mcx16 option for x86_64, this is not used when testing for CMPXCHG128. Thus, we silently turn it off.

Quoted 7 lines

x86_64 is the only architecture adding machine flags for now, so the problem is limited to this host architecture. However, the problem is generic, so define a new variable for all the -m options, so that they can be used for other such tests in the future.

Based-on: <[email protected]>
Supersedes: <[email protected]>

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on the v2 series
#10 lore raw
In reply to Paolo Bonzini
Quoted 17 lines · Paolo Bonzini

Alex discovered that CMPXCHG128 was not enabled when building for x86_64, resulting in slow execution for wide atomic instructions, creating a huge contention when combined with a high number of cpus (found while booting android aarch64 guest on x86_64 host).

The problem is that even though we enable -mcx16 option for x86_64, this is not used when testing for CMPXCHG128. Thus, we silently turn it off.

x86_64 is the only architecture adding machine flags for now, so the problem is limited to this host architecture. However, the problem is generic, so define a new variable for all the -m options, so that they can be used for other such tests in the future.

Based-on: <[email protected]>
Supersedes: <[email protected]>

Quoted 7 lines

Paolo Bonzini (2):
meson: define qemu_isa_flags
meson: ensure -mcx16 is passed when detecting ATOMIC128

  meson.build | 33 ++++++++++++++++++++-------------
  1 file changed, 20 insertions(+), 13 deletions(-)

Hi Paolo,

Thanks for fixing this.

Series:

I was wondering why we have a specific flags path for x64.
People who are interested in getting machine specific binaries can always play with CFLAGS, so what was the original intent here?

Thanks,
Pierrick