Skip to contentSkip to conversation

Optimization with AVX2 instruction

PATCH Superseded Liang Li 3 participants 6 messages Aug 28, 2015 – Sep 8, 2015

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Liang Li

author
#0 lore raw

cover Optimization with AVX2 instruction

The buffer_find_nonzero_offset() will be called to check the zero page during live migration, it's a hot function. buffer_find_nonzero_offset() has already been optimized with SSE2 instructions, for platform that supports AVX2, we can optimize this function with AVX2 instructions and achieve about 25% performance gain.

Liang Li (2):
cutils: add the AVX2 optimization
configure: add --enable-avx2 option

 configure             | 16 ++++++++++++++++
 include/qemu-common.h |  7 +++++++
 2 files changed, 23 insertions(+)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/2 cutils: add the AVX2 optimization

For platform that supports AVX2 instructions, use the AVX2 instructions instead of SSE2 instructions in buffer_find_nonzero_offset() can help to improve the performance about 30%. Zero page check during live migration can be faster with this optimization.

---
 include/qemu-common.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu-common.h +7 -0
index bbaffd1..629fcac 100644
@@ -468,6 +468,13 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
 /* altivec.h may redefine the bool macro as vector type.
  * Reset it to POSIX semantics. */
 #define bool _Bool
+#elif defined __AVX2__
+#include <immintrin.h>
+#define VECTYPE        __m256i
+#define SPLAT(p)       _mm256_set1_epi8(*(p))
+#define ALL_EQ(v1, v2) \
+    (_mm256_movemask_epi8(_mm256_cmpeq_epi8(v1, v2)) == 0xFFFFFFFF)
+#define VEC_OR(v1, v2) (_mm256_or_si256(v1, v2))
 #elif defined __SSE2__
 #include <emmintrin.h>
 #define VECTYPE        __m128i
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH 2/2 configure: add --enable-avx2 option

In reply to Liang Li

Add the --enable-avx2 option so as to enable the AVX2 instruction optimization for buffer_find_nonzero_offset().

---
 configure | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
configure +16 -0
index 9d24d59..ee84172 100755
@@ -307,6 +307,7 @@ smartcard_nss=""
 libusb=""
 usb_redir=""
 opengl=""
+avx2="no"
 zlib="yes"
 lzo=""
 snappy=""
@@ -1052,6 +1053,8 @@ for opt do
   ;;
   --enable-usb-redir) usb_redir="yes"
   ;;
+  --enable-avx2) avx2="yes"
+  ;;
   --disable-zlib-test) zlib="no"
   ;;
   --disable-lzo) lzo="no"
@@ -1354,6 +1357,7 @@ disabled with --disable-FEATURE, default is enabled if available:
   smartcard-nss   smartcard nss support
   libusb          libusb (for usb passthrough)
   usb-redir       usb network redirection support
+  avx2            support of avx2 instruction
   lzo             support of lzo compression library
   snappy          support of snappy compression library
   bzip2           support of bzip2 compression library
@@ -1758,6 +1762,13 @@ EOF
   fi
 fi
 
+########################################
+# avx2 check
+
+if test "$avx2" = "yes" ; then
+        CFLAGS="$CFLAGS -mavx2"
+fi
+
 ##########################################
 # zlib check
 
@@ -4589,6 +4600,7 @@ echo "libssh2 support   $libssh2"
 echo "TPM passthrough   $tpm_passthrough"
 echo "QOM debugging     $qom_cast_debug"
 echo "vhdx              $vhdx"
+echo "avx2 support      $avx2"
 echo "lzo support       $lzo"
 echo "snappy support    $snappy"
 echo "bzip2 support     $bzip2"
@@ -4962,6 +4974,10 @@ if test "$opengl" = "yes" ; then
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
+if test "$avx2" = "yes" ; then
+  echo "CONFIG_AVX2=y" >> $config_host_mak
+fi
+
 if test "$lzo" = "yes" ; then
   echo "CONFIG_LZO=y" >> $config_host_mak
 fi
Signature · 1 line
1.9.1
#3 lore raw
In reply to Liang Li
Quoted 5 lines · Liang Li

The buffer_find_nonzero_offset() will be called to check the zero page during live migration, it's a hot function. buffer_find_nonzero_offset() has already been optimized with SSE2 instructions, for platform that supports AVX2, we can optimize this function with AVX2 instructions and achieve about 25% performance gain.

This should be a good improvement. I recall Dave and I had a chat about this in the past too.

I've not yet reviewed the patchset, but I doubt anyone will have objections. I'll review this shortly.

Thanks,

Amit

#4 lore raw
Quoted 10 lines · Amit Shah
Quoted 5 lines

The buffer_find_nonzero_offset() will be called to check the zero page

Quoted 4 lines

during live migration, it's a hot function. buffer_find_nonzero_offset() has already been optimized with SSE2 instructions, for platform that supports AVX2, we can optimize this function with AVX2 instructions and achieve about 25% performance gain.

This should be a good improvement. I recall Dave and I had a chat about this in the past too.

I've not yet reviewed the patchset, but I doubt anyone will have objections. I'll review this shortly.

I think we need a better way to enable it than a configure option, however. AVX2 machines are rare, and no one would end up using it except perhaps Gentoo or other source-based distros.

Perhaps something like the GCC ifunc attribute?

Paolo

#5 lore raw
Quoted 14 lines · Amit Shah
Quoted 6 lines
Quoted 6 lines

The buffer_find_nonzero_offset() will be called to check the zero page

Quoted 4 lines

during live migration, it's a hot function. buffer_find_nonzero_offset() has already been optimized with SSE2 instructions, for platform that supports AVX2, we can optimize this function with AVX2 instructions and achieve about 25% performance

gain.

Quoted 5 lines

This should be a good improvement. I recall Dave and I had a chat about this in the past too.

I've not yet reviewed the patchset, but I doubt anyone will have objections. I'll review this shortly.

Quoted 6 lines

I think we need a better way to enable it than a configure option, however. AVX2 machines are rare, and no one would end up using it except perhaps Gentoo or other source-based distros.

Perhaps something like the GCC ifunc attribute?
Paolo

Thanks for your comments. ifunc is a good solution, I will send out the v2 soon.

Liang