Improve buffer_is_zero
v3 cover Improve buffer_is_zero
Changes from v2 to v3:
* Unit testing. This includes having x86 attempt all versions of the accelerator that will run on the hardware. Thus an avx2 host will run the basic test 5 times (1.5sec on my laptop).
* Drop the ppc and aarch64 specializations. I have improved the basic integer version to the point that those vectorized versions are not a win.
In the case of my aarch64 mustang, the integer version is 4 times faster than the neon version that I delete. With effort I was able to rewrite the neon version to come to within a factor of 1.1, but it remained slower than the integer. To be fair, gcc6 makes very good use of ldp, so the integer path is *also* loading 16 bytes per insn.
I can forward my standalone aarch64 benchmark if anyone is interested.
Note however that at least the avx2 acceleration is still very much a win, being about 3 times faster on my laptop. Of course, it's handling 4 times as much data per loop as the integer version, so one can still see the overhead caused by using vector insns.
For grins I wrote an avx512 version, if someone has a skylake upon which to test and benchmark. That requires additional configure checks, so I didn't bother to include it here.
r~
Richard Henderson (9):
cutils: Move buffer_is_zero and subroutines to a new file
cutils: Remove SPLAT macro
cutils: Export only buffer_is_zero
cutils: Rearrange buffer_is_zero acceleration
cutils: Add test for buffer_is_zero
cutils: Add generic prefetch
cutils: Rewrite x86 buffer zero checking
cutils: Remove aarch64 buffer zero checking
cutils: Remove ppc buffer zero checking
configure | 21 +-- include/qemu/cutils.h | 3 +- migration/ram.c | 2 +- migration/rdma.c | 5 +- tests/Makefile.include | 3 + tests/test-bufferiszero.c | 78 +++++++++++ util/Makefile.objs | 1 + util/bufferiszero.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 244 ---------------------------------- 9 files changed, 423 insertions(+), 266 deletions(-) create mode 100644 tests/test-bufferiszero.c create mode 100644 util/bufferiszero.c
Signature · 1 line
2.7.4
PATCH v3 1/9 cutils: Move buffer_is_zero and subroutines to a new file
--- util/Makefile.objs | 1 + util/bufferiszero.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 244 ---------------------------------------------- 3 files changed, 273 insertions(+), 244 deletions(-) create mode 100644 util/bufferiszero.c
util/Makefile.objs +1 -0
@@ -1,4 +1,5 @@ util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o +util-obj-y += bufferiszero.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o util-obj-$(CONFIG_POSIX) += mmap-alloc.o
util/bufferiszero.c new file +272 -0
272 lines not on this page · raw message
util/cutils.c +0 -244
250 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 3/9 cutils: Export only buffer_is_zero
In reply to Richard HendersonSince the two users don't make use of the returned offset, beyond ensuring that the entire buffer is zero, consider the can_use_buffer_find_nonzero_offset and buffer_find_nonzero_offset functions internal.
--- include/qemu/cutils.h | 2 -- migration/ram.c | 2 +- migration/rdma.c | 5 +---- util/bufferiszero.c | 8 ++++---- 4 files changed, 6 insertions(+), 11 deletions(-)
include/qemu/cutils.h +0 -2
@@ -168,8 +168,6 @@ int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end,/* used to print char* safely */ #define STR_OR_NULL (str) ((str) ? (str) : "null") -bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len); -size_t buffer_find_nonzero_offset (const void *buf,size_t len);bool buffer_is_zero (const void *buf,size_t len);/*
migration/ram.c +1 -1
@@ -73,7 +73,7 @@ static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];static inline bool is_zero_range (uint8_t *p,uint64_t size) { -return buffer_find_nonzero_offset (p, size) == size; +return buffer_is_zero (p, size); }/* struct contains XBZRLE cache and a static page
migration/rdma.c +1 -4
@@ -1934,10 +1934,7 @@ retry:* memset() + madvise() the entire chunk without RDMA. */ -if (can_use_buffer_find_nonzero_offset ((void *)(uintptr_t )sge.addr, - length) - &&buffer_find_nonzero_offset ((void *)(uintptr_t )sge.addr, - length) == length) { +if (buffer_is_zero ((void *)(uintptr_t )sge.addr, length)) {RDMACompress comp = { .offset = current_addr, .value =0 ,
util/bufferiszero.c +4 -4
@@ -192,9 +192,9 @@ static bool avx2_support(void)return b & bit_AVX2; } -bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) \ +static bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) \ __attribute__ ((ifunc ("can_use_buffer_find_nonzero_offset_ifunc" ))); -size_t buffer_find_nonzero_offset (const void *buf,size_t len) \ +static size_t buffer_find_nonzero_offset (const void *buf,size_t len) \ __attribute__ ((ifunc ("buffer_find_nonzero_offset_ifunc" )));static void *buffer_find_nonzero_offset_ifunc (void )
@@ -215,12 +215,12 @@ static void *can_use_buffer_find_nonzero_offset_ifunc(void) }#pragma GCC pop_options#else -bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) +static bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) {return can_use_buffer_find_nonzero_offset_inner (buf, len); } -size_t buffer_find_nonzero_offset (const void *buf,size_t len) +static size_t buffer_find_nonzero_offset (const void *buf,size_t len) {return buffer_find_nonzero_offset_inner (buf, len); }
Signature · 1 line
2.7.4
PATCH v3 4/9 cutils: Rearrange buffer_is_zero acceleration
In reply to Richard HendersonAllow selection of several acceleration functions based on the size and alignment of the buffer. Do not require ifunc support for AVX2 acceleration.
--- configure | 21 +--- util/bufferiszero.c | 349 ++++++++++++++++++++++++---------------------------- 2 files changed, 166 insertions(+), 204 deletions(-)
configure +6 -15
@@ -1788,28 +1788,19 @@ fi ########################################## # avx2 optimization requirement check - -if test "$static" = "no" ; then - cat > $TMPC << EOF +cat > $TMPC << EOF #pragma GCC push_options #pragma GCC target("avx2") #include <cpuid.h> #include <immintrin.h> - static int bar(void *a) { - return _mm256_movemask_epi8(_mm256_cmpeq_epi8(*(__m256i *)a, (__m256i){0})); + __m256i x = *(__m256i *)a; + return _mm256_testz_si256(x, x); } -static void *bar_ifunc(void) {return (void*) bar;} -int foo(void *a) __attribute__((ifunc("bar_ifunc"))); -int main(int argc, char *argv[]) { return foo(argv[0]);} +int main(int argc, char *argv[]) { return bar(argv[0]); } EOF - if compile_object "" ; then - if has readelf; then - if readelf --syms $TMPO 2>/dev/null |grep -q "IFUNC.*foo"; then - avx2_opt="yes" - fi - fi - fi +if compile_object "" ; then + avx2_opt="yes" fi #########################################
util/bufferiszero.c +160 -189
405 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v3 5/9 cutils: Add test for buffer_is_zero
In reply to Richard Henderson--- include/qemu/cutils.h | 1 + tests/Makefile.include | 3 ++ tests/test-bufferiszero.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++ util/bufferiszero.c | 7 +++++ 4 files changed, 89 insertions(+) create mode 100644 tests/test-bufferiszero.c
include/qemu/cutils.h +1 -0
@@ -169,6 +169,7 @@ int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end,#define STR_OR_NULL (str) ((str) ? (str) : "null")bool buffer_is_zero (const void *buf,size_t len); +bool test_buffer_is_zero_next_accel (void );/* * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
tests/Makefile.include +3 -0
@@ -112,6 +112,8 @@ check-unit-y += tests/test-crypto-xts$(EXESUF) check-unit-y += tests/test-crypto-block$(EXESUF) gcov-files-test-logging-y = tests/test-logging.c check-unit-y += tests/test-logging$(EXESUF) +check-unit-y += tests/test-bufferiszero$(EXESUF) +gcov-files-check-bufferiszero-y = util/bufferiszero.c check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
@@ -465,6 +467,7 @@ tests/test-qdist$(EXESUF): tests/test-qdist.o $(test-util-obj-y) tests/test-qht$(EXESUF): tests/test-qht.o $(test-util-obj-y) tests/test-qht-par$(EXESUF): tests/test-qht-par.o tests/qht-bench$(EXESUF) $(test-util-obj-y) tests/qht-bench$(EXESUF): tests/qht-bench.o $(test-util-obj-y) +tests/test-bufferiszero$(EXESUF): tests/test-bufferiszero.o $(test-util-obj-y) tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \ hw/core/qdev.o hw/core/qdev-properties.o hw/core/hotplug.o\
tests/test-bufferiszero.c new file +78 -0
78 lines not on this page · raw message
util/bufferiszero.c +7 -0
@@ -223,6 +223,13 @@ static bool select_accel_fn(const void *buf, size_t len)#define select_accel_fn buffer_zero_int#endif +#ifndef HAVE_NEXT_ACCEL +bool test_buffer_is_zero_next_accel (void ) +{ +return false; +} +#endif +/* * Checks if a buffer is all zeroes */
Signature · 1 line
2.7.4
PATCH v3 6/9 cutils: Add generic prefetch
In reply to Richard HendersonThere's no real knowledge of the cacheline size,
just prefetching one loop ahead.
--- util/bufferiszero.c | 5 +++++ 1 file changed, 5 insertions(+)
util/bufferiszero.c +5 -0
@@ -38,6 +38,8 @@ static bool NAME(const void *buf, size_t len) \ do { \ const VECTYPE *p = buf; \ VECTYPE t; \ + __builtin_prefetch(buf + SIZE); \ + barrier(); \ if (SIZE == sizeof(VECTYPE) * 4) { \ t = (p[0] | p[1]) | (p[2] | p[3]); \ } else if (SIZE == sizeof(VECTYPE) * 8) { \
@@ -239,6 +241,9 @@ bool buffer_is_zero(const void *buf, size_t len)return true; } +/* Fetch the beginning of the buffer while we select the accelerator. */ +__builtin_prefetch (buf); +/* Use an optimized zero check if possible. Note that this also includes a check for an unrolled loop over longs, as well as the unsized, unaligned fallback to buffer_zero_base. */
Signature · 1 line
2.7.4
PATCH v3 7/9 cutils: Rewrite x86 buffer zero checking
In reply to Richard HendersonHandle alignment of buffers, so that the vector paths can be used more often. Add versions for AVX1 and SSE4.1, both of which have incremental improvements over SSE2.
--- util/bufferiszero.c | 209 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 179 insertions(+), 30 deletions(-)
util/bufferiszero.c +179 -30
@@ -122,29 +122,177 @@ static bool select_accel_fn(const void *buf, size_t len) return buffer_zero_int(buf, len); } -#elif defined(CONFIG_AVX2_OPT ) +#elif defined(CONFIG_AVX2_OPT) || defined(__SSE2__) #include <cpuid.h> #include <x86intrin.h> +/* Note that we're going to check for LEN >= 64 for all of these. */ + +#ifdef CONFIG_AVX2_OPT #pragma GCC push_options #pragma GCC target("avx2") -#define AVX2_NONZERO (X ) !_mm256_testz_si256((X), (X)) -ACCEL_BUFFER_ZERO(buffer_zero_avx2, 128, __m256i, AVX2_NONZERO) + +static bool +buffer_zero_avx2(const void *buf, size_t len) +{ + /* Begin with an unaligned head of 32 bytes. */ + __m256i t = _mm256_loadu_si256(buf); + __m256i *p = (__m256i *)(((uintptr_t)buf + 5 * 32) & -32); + __m256i *e = (__m256i *)(((uintptr_t)buf + len) & -32); + + if (likely(p <= e)) { + /* Loop over 32-byte aligned blocks of 128. */ + do { + __builtin_prefetch(p); + if (unlikely(!_mm256_testz_si256(t, t))) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } while (p <= e); + } else { + t |= _mm256_loadu_si256(buf + 32); + if (len <= 128) { + goto last2; + } + } + + /* Finish the last block of 128 unaligned. */ + t |= _mm256_loadu_si256(buf + len - 4 * 32); + t |= _mm256_loadu_si256(buf + len - 3 * 32); + last2: + t |= _mm256_loadu_si256(buf + len - 2 * 32); + t |= _mm256_loadu_si256(buf + len - 1 * 32); + + return _mm256_testz_si256(t, t); +} + +#pragma GCC pop_options +#pragma GCC push_options +#pragma GCC target("avx") + +static bool +buffer_zero_avx(const void *buf, size_t len) +{ + __m128i t = _mm_loadu_si128(buf); + __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16); + __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16); + + /* Loop over 16-byte aligned blocks of 64. */ + while (likely(p <= e)) { + __builtin_prefetch(p); + if (unlikely(!_mm_testz_si128(t, t))) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } + + /* Finish the last block of 64 unaligned. */ + t |= _mm_loadu_si128(buf + len - 4 * 16); + t |= _mm_loadu_si128(buf + len - 3 * 16); + t |= _mm_loadu_si128(buf + len - 2 * 16); + t |= _mm_loadu_si128(buf + len - 1 * 16); + + return _mm_testz_si128(t, t); +} + #pragma GCC pop_options +#pragma GCC push_options +#pragma GCC target("sse4") + +static bool +buffer_zero_sse4(const void *buf, size_t len) +{ + __m128i t = _mm_loadu_si128(buf); + __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16); + __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16); + + /* Loop over 16-byte aligned blocks of 64. */ + while (likely(p <= e)) { + __builtin_prefetch(p); + if (unlikely(!_mm_testz_si128(t, t))) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } + /* Finish the aligned tail. */ + t |= e[-3]; + t |= e[-2]; + t |= e[-1]; + + /* Finish the unaligned tail. */ + t |= _mm_loadu_si128(buf + len - 16); + + return _mm_testz_si128(t, t); +} + +#pragma GCC pop_options #pragma GCC push_options #pragma GCC target("sse2") -#define SSE2_NONZERO (X ) \ - (_mm_movemask_epi8(_mm_cmpeq_epi8((X), _mm_setzero_si128())) != 0xFFFF) -ACCEL_BUFFER_ZERO(buffer_zero_sse2, 64, __m128i, SSE2_NONZERO) +#endif /* CONFIG_AVX2_OPT */ + +static bool +buffer_zero_sse2(const void *buf, size_t len) +{ + __m128i t = _mm_loadu_si128(buf); + __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16); + __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16); + __m128i zero = _mm_setzero_si128(); + + /* Loop over 16-byte aligned blocks of 64. */ + while (likely(p <= e)) { + __builtin_prefetch(p); + t = _mm_cmpeq_epi8(t, zero); + if (unlikely(_mm_movemask_epi8(t) != 0xFFFF)) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } + + /* Finish the aligned tail. */ + t |= e[-3]; + t |= e[-2]; + t |= e[-1]; + + /* Finish the unaligned tail. */ + t |= _mm_loadu_si128(buf + len - 16); + + return _mm_movemask_epi8(_mm_cmpeq_epi8(t, zero)) == 0xFFFF; +} + +#ifdef CONFIG_AVX2_OPT #pragma GCC pop_options -#define CACHE_AVX2 2 -#define CACHE_AVX1 4 -#define CACHE_SSE4 8 -#define CACHE_SSE2 16 +/* These values must be most preferable alternative first. + See test_buffer_is_zero_next_accel. */ +#define CACHE_AVX2 1 +#define CACHE_AVX1 2 +#define CACHE_SSE4 4 +#define CACHE_SSE2 8 static unsigned cpuid_cache; +static accel_zero_fn buffer_accel; + +static void init_accel(unsigned cache) +{ + accel_zero_fn fn; + if (cache & CACHE_AVX2) { + fn = buffer_zero_avx2; + } else if (cache & CACHE_AVX1) { + fn = buffer_zero_avx; + } else if (cache & CACHE_SSE4) { + fn = buffer_zero_sse4; + } else if (cache & CACHE_SSE2) { + fn = buffer_zero_sse2; + } else { + fn = buffer_zero_int; + } + buffer_accel = fn; +} static void __attribute__((constructor)) init_cpuid_cache(void) {
@@ -163,8 +311,9 @@ static void __attribute__((constructor)) init_cpuid_cache(void)/* We must check that AVX is not just available, but usable. */ if ((c & bit_OSXSAVE) && (c & bit_AVX)) { - __asm("xgetbv" :"=a" (a),"=d" (d) :"c" (0 )); -if ((a &6 ) ==6 ) { +int bv; + __asm("xgetbv" :"=a" (bv),"=d" (d) :"c" (0 )); +if ((bv &6 ) ==6 ) { cache |=CACHE_AVX1 ;if (max >=7 ) {__cpuid_count (7 ,0 , a, b, c, d);
@@ -176,34 +325,34 @@ static void __attribute__((constructor)) init_cpuid_cache(void) } } cpuid_cache = cache; +init_accel (cache); } -static bool select_accel_fn (const void *buf,size_t len) +#define HAVE_NEXT_ACCEL +bool test_buffer_is_zero_next_accel (void ) { -uintptr_t ibuf = (uintptr_t )buf; -if (len %128 ==0 && ibuf %32 ==0 && (cpuid_cache &CACHE_AVX2 )) { -return buffer_zero_avx2 (buf, len); - } -if (len %64 ==0 && ibuf %16 ==0 && (cpuid_cache &CACHE_SSE2 )) { -return buffer_zero_sse2 (buf, len); +/* If no bits set, we just tested buffer_zero_int, and there +are no more acceleration options to test. */ +if (cpuid_cache ==0 ) { +return false; } -return buffer_zero_int (buf, len); +/* Disable the accelerator we used before and select a new one. */ + cpuid_cache &= cpuid_cache -1 ; +init_accel (cpuid_cache); +return true; } - -#elif defined __SSE2__ -#include <emmintrin.h> - -#define SSE2_NONZERO (X ) \ - (_mm_movemask_epi8(_mm_cmpeq_epi8((X), _mm_setzero_si128())) != 0xFFFF) -ACCEL_BUFFER_ZERO(buffer_zero_sse2, 64, __m128i, SSE2_NONZERO) +#endif /* CONFIG_AVX2_OPT */ static bool select_accel_fn (const void *buf,size_t len) { -uintptr_t ibuf = (uintptr_t )buf; -if (len %64 ==0 && ibuf %sizeof (__m128i) ==0 ) { +if (likely (len >=64 )) { +#ifdef CONFIG_AVX2_OPT +return buffer_accel (buf, len); +#else return buffer_zero_sse2 (buf, len); +#endif } -return select_accel_int (buf, len); +return buffer_zero_int (buf, len); }#elif defined(__aarch64__)
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v3 7/9] cutils: Rewrite x86 buffer zero checking
Quoted 257 lines · Richard Henderson
Handle alignment of buffers, so that the vector paths can be used more often. Add versions for AVX1 and SSE4.1, both of which have incremental improvements over SSE2.
Signed-off-by Richard Henderson <[email protected]>--- util/bufferiszero.c | 209 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 179 insertions(+), 30 deletions(-)util/bufferiszero.c +175
-29@@ -122,29 +122,177 @@ static bool select_accel_fn(const void *buf, size_t len) return buffer_zero_int(buf, len); } -#elif defined(CONFIG_AVX2_OPT ) +#elif defined(CONFIG_AVX2_OPT) || defined(__SSE2__) #include <cpuid.h> #include <x86intrin.h> +/* Note that we're going to check for LEN >= 64 for all of these. */ + +#ifdef CONFIG_AVX2_OPT #pragma GCC push_options #pragma GCC target("avx2") -#define AVX2_NONZERO (X ) !_mm256_testz_si256((X), (X)) -ACCEL_BUFFER_ZERO(buffer_zero_avx2, 128, __m256i, AVX2_NONZERO) + +static bool +buffer_zero_avx2(const void *buf, size_t len) +{ + /* Begin with an unaligned head of 32 bytes. */ + __m256i t = _mm256_loadu_si256(buf); + __m256i *p = (__m256i *)(((uintptr_t)buf + 5 * 32) & -32); + __m256i *e = (__m256i *)(((uintptr_t)buf + len) & -32); + + if (likely(p <= e)) { + /* Loop over 32-byte aligned blocks of 128. */ + do { + __builtin_prefetch(p); + if (unlikely(!_mm256_testz_si256(t, t))) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } while (p <= e); + } else { + t |= _mm256_loadu_si256(buf + 32); + if (len <= 128) { + goto last2; + } + } + + /* Finish the last block of 128 unaligned. */ + t |= _mm256_loadu_si256(buf + len - 4 * 32); + t |= _mm256_loadu_si256(buf + len - 3 * 32); + last2: + t |= _mm256_loadu_si256(buf + len - 2 * 32); + t |= _mm256_loadu_si256(buf + len - 1 * 32); + + return _mm256_testz_si256(t, t); +} + +#pragma GCC pop_options +#pragma GCC push_options +#pragma GCC target("avx") + +static bool +buffer_zero_avx(const void *buf, size_t len) +{ + __m128i t = _mm_loadu_si128(buf); + __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16); + __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16); + + /* Loop over 16-byte aligned blocks of 64. */ + while (likely(p <= e)) { + __builtin_prefetch(p); + if (unlikely(!_mm_testz_si128(t, t))) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } + + /* Finish the last block of 64 unaligned. */ + t |= _mm_loadu_si128(buf + len - 4 * 16); + t |= _mm_loadu_si128(buf + len - 3 * 16); + t |= _mm_loadu_si128(buf + len - 2 * 16); + t |= _mm_loadu_si128(buf + len - 1 * 16); + + return _mm_testz_si128(t, t); +} + #pragma GCC pop_options +#pragma GCC push_options +#pragma GCC target("sse4") + +static bool +buffer_zero_sse4(const void *buf, size_t len) +{ + __m128i t = _mm_loadu_si128(buf); + __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16); + __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16); + + /* Loop over 16-byte aligned blocks of 64. */ + while (likely(p <= e)) { + __builtin_prefetch(p); + if (unlikely(!_mm_testz_si128(t, t))) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } + /* Finish the aligned tail. */ + t |= e[-3]; + t |= e[-2]; + t |= e[-1]; + + /* Finish the unaligned tail. */ + t |= _mm_loadu_si128(buf + len - 16); + + return _mm_testz_si128(t, t); +} + +#pragma GCC pop_options #pragma GCC push_options #pragma GCC target("sse2") -#define SSE2_NONZERO (X ) \ - (_mm_movemask_epi8(_mm_cmpeq_epi8((X), _mm_setzero_si128())) != 0xFFFF) -ACCEL_BUFFER_ZERO(buffer_zero_sse2, 64, __m128i, SSE2_NONZERO) +#endif /* CONFIG_AVX2_OPT */ + +static bool +buffer_zero_sse2(const void *buf, size_t len) +{ + __m128i t = _mm_loadu_si128(buf); + __m128i *p = (__m128i *)(((uintptr_t)buf + 5 * 16) & -16); + __m128i *e = (__m128i *)(((uintptr_t)buf + len) & -16); + __m128i zero = _mm_setzero_si128(); + + /* Loop over 16-byte aligned blocks of 64. */ + while (likely(p <= e)) { + __builtin_prefetch(p); + t = _mm_cmpeq_epi8(t, zero); + if (unlikely(_mm_movemask_epi8(t) != 0xFFFF)) { + return false; + } + t = p[-4] | p[-3] | p[-2] | p[-1]; + p += 4; + } + + /* Finish the aligned tail. */ + t |= e[-3]; + t |= e[-2]; + t |= e[-1]; + + /* Finish the unaligned tail. */ + t |= _mm_loadu_si128(buf + len - 16); + + return _mm_movemask_epi8(_mm_cmpeq_epi8(t, zero)) == 0xFFFF; +} + +#ifdef CONFIG_AVX2_OPT #pragma GCC pop_options -#define CACHE_AVX2 2 -#define CACHE_AVX1 4 -#define CACHE_SSE4 8 -#define CACHE_SSE2 16 +/* These values must be most preferable alternative first. + See test_buffer_is_zero_next_accel. */ +#define CACHE_AVX2 1 +#define CACHE_AVX1 2 +#define CACHE_SSE4 4 +#define CACHE_SSE2 8 static unsigned cpuid_cache; +static accel_zero_fn buffer_accel; + +static void init_accel(unsigned cache) +{ + accel_zero_fn fn; + if (cache & CACHE_AVX2) { + fn = buffer_zero_avx2; + } else if (cache & CACHE_AVX1) { + fn = buffer_zero_avx; + } else if (cache & CACHE_SSE4) { + fn = buffer_zero_sse4; + } else if (cache & CACHE_SSE2) { + fn = buffer_zero_sse2; + } else { + fn = buffer_zero_int; + } + buffer_accel = fn; +} static void __attribute__((constructor)) init_cpuid_cache(void) {@@ -163,8 +311,9 @@ static void __attribute__((constructor)) init_cpuid_cache(void)/* We must check that AVX is not just available, but usable. */ if ((c & bit_OSXSAVE) && (c & bit_AVX)) { - __asm("xgetbv" :"=a" (a),"=d" (d) :"c" (0 )); -if ((a &6 ) ==6 ) { +int bv; + __asm("xgetbv" :"=a" (bv),"=d" (d) :"c" (0 )); +if ((bv &6 ) ==6 ) { cache |=CACHE_AVX1 ;if (max >=7 ) {__cpuid_count (7 ,0 , a, b, c, d);@@ -176,34 +325,34 @@ static void __attribute__((constructor)) init_cpuid_cache(void) } } cpuid_cache = cache; +init_accel (cache); } -static bool select_accel_fn (const void *buf,size_t len) +#define HAVE_NEXT_ACCEL +bool test_buffer_is_zero_next_accel (void ) { -uintptr_t ibuf = (uintptr_t )buf; -if (len %128 ==0 && ibuf %32 ==0 && (cpuid_cache &CACHE_AVX2 )) { -return buffer_zero_avx2 (buf, len); - } -if (len %64 ==0 && ibuf %16 ==0 && (cpuid_cache &CACHE_SSE2 )) { -return buffer_zero_sse2 (buf, len); +/* If no bits set, we just tested buffer_zero_int, and there +are no more acceleration options to test. */ +if (cpuid_cache ==0 ) { +return false; } -return buffer_zero_int (buf, len); +/* Disable the accelerator we used before and select a new one. */ + cpuid_cache &= cpuid_cache -1 ; +init_accel (cpuid_cache); +return true; } - -#elif defined __SSE2__ -#include <emmintrin.h> - -#define SSE2_NONZERO (X ) \ - (_mm_movemask_epi8(_mm_cmpeq_epi8((X), _mm_setzero_si128())) != 0xFFFF) -ACCEL_BUFFER_ZERO(buffer_zero_sse2, 64, __m128i, SSE2_NONZERO) +#endif /* CONFIG_AVX2_OPT */ static bool select_accel_fn (const void *buf,size_t len) { -uintptr_t ibuf = (uintptr_t )buf; -if (len %64 ==0 && ibuf %sizeof (__m128i) ==0 ) { +if (likely (len >=64 )) { +#ifdef CONFIG_AVX2_OPT
util/bufferiszero.c static void __attribute__((constructor)) init_cpuid_cache(void) · line 358
Quoted 10 lines
util/bufferiszero.c +4
-1+return buffer_accel (buf, len); +#else return buffer_zero_sse2 (buf, len); +#endif } -return select_accel_int (buf, len); +return buffer_zero_int (buf, len); }#elif defined(__aarch64__)
I need this on top to fix compilation with older compilers:
util/bufferiszero.c +3 -1
@@ -71,13 +71,13 @@ buffer_zero_int(const void *buf, size_t len)#if defined(CONFIG_AVX2_OPT ) || defined(__SSE2__)#include <cpuid.h> -#include <x86intrin.h> /* Note that we're going to check for LEN >= 64 for all of these. */ #ifdef CONFIG_AVX2_OPT #pragma GCC push_options#pragma GCC target("avx2") +#include <immintrin.h> static bool buffer_zero_avx2 (const void *buf,size_t len)
@@ -181,6 +181,8 @@ buffer_zero_sse4(const void *buf, size_t len)#pragma GCC target("sse2")#endif /* CONFIG_AVX2_OPT */ +#include <emmintrin.h> +static bool buffer_zero_sse2 (const void *buf,size_t len) {
Paolo
Re: [Qemu-devel] [PATCH v3 7/9] cutils: Rewrite x86 buffer zero checking
Quoted 23 lines · Paolo Bonzini
I need this on top to fix compilation with older compilers:
util/bufferiszero.c +1
-1@@ -71,13 +71,13 @@ buffer_zero_int(const void *buf, size_t len)#if defined(CONFIG_AVX2_OPT ) || defined(__SSE2__)#include <cpuid.h> -#include <x86intrin.h> /* Note that we're going to check for LEN >= 64 for all of these. */ #ifdef CONFIG_AVX2_OPT #pragma GCC push_options#pragma GCC target("avx2") +#include <immintrin.h> static bool buffer_zero_avx2 (const void *buf,size_t len)
util/bufferiszero.c buffer_zero_sse4(const void *buf, size_t len) · line 188
Quoted 8 lines
util/bufferiszero.c +2
-0@@ -181,6 +181,8 @@ buffer_zero_sse4(const void *buf, size_t len)#pragma GCC target("sse2")#endif /* CONFIG_AVX2_OPT */ +#include <emmintrin.h> +static bool buffer_zero_sse2 (const void *buf,size_t len) {
Nope, not enough on GCC 4.8:
$ nm util/bufferiszero.o
0000000000000000 b buffer_accel
0000000000000430 T buffer_is_zero
0000000000000240 t buffer_zero_avx
0000000000000330 t buffer_zero_avx2
0000000000000000 t buffer_zero_int
00000000000000b0 t buffer_zero_sse2
0000000000000190 t buffer_zero_sse4
0000000000000008 b cpuid_cache
U _GLOBAL_OFFSET_TABLE_
0000000000000000 t init_cpuid_cache
U _mm_cmpeq_epi8
U _mm_loadu_si128
U _mm_movemask_epi8
U _mm_setzero_si128
U _mm_testz_si128
00000000000003d0 T test_buffer_is_zero_next_accelSince I have no idea what's going on, I'm dropping these the last three patches.
Paolo
Re: [Qemu-devel] [PATCH v3 7/9] cutils: Rewrite x86 buffer zero checking
Quoted 55 lines · Paolo Bonzini
Quoted 23 lines · Paolo Bonzini
I need this on top to fix compilation with older compilers:
util/bufferiszero.c +1
-1@@ -71,13 +71,13 @@ buffer_zero_int(const void *buf, size_t len)#if defined(CONFIG_AVX2_OPT ) || defined(__SSE2__)#include <cpuid.h> -#include <x86intrin.h> /* Note that we're going to check for LEN >= 64 for all of these. */ #ifdef CONFIG_AVX2_OPT #pragma GCC push_options#pragma GCC target("avx2") +#include <immintrin.h> static bool buffer_zero_avx2 (const void *buf,size_t len)util/bufferiszero.c buffer_zero_sse4(const void *buf, size_t len) · line 188
Quoted 8 lines
util/bufferiszero.c +2
-0@@ -181,6 +181,8 @@ buffer_zero_sse4(const void *buf, size_t len)#pragma GCC target("sse2")#endif /* CONFIG_AVX2_OPT */ +#include <emmintrin.h> +static bool buffer_zero_sse2 (const void *buf,size_t len) {Nope, not enough on GCC 4.8:
$ nm util/bufferiszero.o 0000000000000000 b buffer_accel 0000000000000430 T buffer_is_zero 0000000000000240 t buffer_zero_avx 0000000000000330 t buffer_zero_avx2 0000000000000000 t buffer_zero_int 00000000000000b0 t buffer_zero_sse2 0000000000000190 t buffer_zero_sse4 0000000000000008 b cpuid_cache U _GLOBAL_OFFSET_TABLE_ 0000000000000000 t init_cpuid_cache U _mm_cmpeq_epi8 U _mm_loadu_si128 U _mm_movemask_epi8 U _mm_setzero_si128 U _mm_testz_si128 00000000000003d0 T test_buffer_is_zero_next_accel
Quoted 2 lines
Since I have no idea what's going on, I'm dropping these the last three patches.
I still have no idea what's going on, but dropping this patch is enough.
Paolo
Re: [Qemu-devel] [PATCH v3 7/9] cutils: Rewrite x86 buffer zero checking
Quoted 58 lines · Paolo Bonzini
Quoted 55 lines · Paolo Bonzini
Quoted 23 lines · Paolo Bonzini
I need this on top to fix compilation with older compilers:
util/bufferiszero.c +1
-1@@ -71,13 +71,13 @@ buffer_zero_int(const void *buf, size_t len)#if defined(CONFIG_AVX2_OPT ) || defined(__SSE2__)#include <cpuid.h> -#include <x86intrin.h> /* Note that we're going to check for LEN >= 64 for all of these. */ #ifdef CONFIG_AVX2_OPT #pragma GCC push_options#pragma GCC target("avx2") +#include <immintrin.h> static bool buffer_zero_avx2 (const void *buf,size_t len)util/bufferiszero.c buffer_zero_sse4(const void *buf, size_t len) · line 188
Quoted 8 lines
util/bufferiszero.c +2
-0@@ -181,6 +181,8 @@ buffer_zero_sse4(const void *buf, size_t len)#pragma GCC target("sse2")#endif /* CONFIG_AVX2_OPT */ +#include <emmintrin.h> +static bool buffer_zero_sse2 (const void *buf,size_t len) {Nope, not enough on GCC 4.8:
$ nm util/bufferiszero.o 0000000000000000 b buffer_accel 0000000000000430 T buffer_is_zero 0000000000000240 t buffer_zero_avx 0000000000000330 t buffer_zero_avx2 0000000000000000 t buffer_zero_int 00000000000000b0 t buffer_zero_sse2 0000000000000190 t buffer_zero_sse4 0000000000000008 b cpuid_cache U _GLOBAL_OFFSET_TABLE_ 0000000000000000 t init_cpuid_cache U _mm_cmpeq_epi8 U _mm_loadu_si128 U _mm_movemask_epi8 U _mm_setzero_si128 U _mm_testz_si128 00000000000003d0 T test_buffer_is_zero_next_accel
Quoted 4 lines
Quoted 2 lines
Since I have no idea what's going on, I'm dropping these the last three patches.
I still have no idea what's going on, but dropping this patch is enough.
Ok, it's the AVX version, the failure somehow is related to vptest not being in AVX1.
Paolo
PATCH v3 8/9 cutils: Remove aarch64 buffer zero checking
In reply to Richard HendersonThe revised integer version is 4 times faster than the neon version on an AppliedMicro Mustang. Even with hand scheduling and additional unrolling I cannot make any neon version run as fast as the integer.
--- util/bufferiszero.c | 15 --------------- 1 file changed, 15 deletions(-)
util/bufferiszero.c +0 -15
@@ -355,21 +355,6 @@ static bool select_accel_fn(const void *buf, size_t len) return buffer_zero_int(buf, len); } -#elif defined(__aarch64__) -#include "arm_neon.h" - -#define DO_NONZERO (X ) (vgetq_lane_u64((X), 0) | vgetq_lane_u64((X), 1)) -ACCEL_BUFFER_ZERO(buffer_zero_neon, 128, uint64x2_t, DO_NONZERO) - -static bool select_accel_fn (const void *buf,size_t len) -{ -uintptr_t ibuf = (uintptr_t )buf; -if (len %128 ==0 && ibuf %sizeof (uint64x2_t) ==0 ) { -return buffer_zero_neon (buf, len); - } -return buffer_zero_int (buf, len); -} -#else #define select_accel_fn buffer_zero_int#endif
Signature · 1 line
2.7.4
PATCH v3 9/9 cutils: Remove ppc buffer zero checking
In reply to Richard HendersonFor ppc64le, gcc6 does extremely poorly with the Altivec code. Moreover, on POWER7 and POWER8, a hand-optimized Altivec version turns out to be no faster than the revised integer version, and therefore not worth the effort.
--- util/bufferiszero.c | 55 +---------------------------------------------------- 1 file changed, 1 insertion(+), 54 deletions(-)
util/bufferiszero.c +1 -54
@@ -29,35 +29,6 @@/* vector definitions */ -extern void link_error (void ); - -#define ACCEL_BUFFER_ZERO (NAME ,SIZE ,VECTYPE ,NONZERO ) \ -static bool NAME(const void *buf, size_t len) \ -{ \ - const void *end = buf + len; \ - do { \ - const VECTYPE *p = buf; \ - VECTYPE t; \ - __builtin_prefetch(buf + SIZE); \ - barrier(); \ - if (SIZE == sizeof(VECTYPE) * 4) { \ - t = (p[0] | p[1]) | (p[2] | p[3]); \ - } else if (SIZE == sizeof(VECTYPE) * 8) { \ - t = p[0] | p[1]; \ - t |= p[2] | p[3]; \ - t |= p[4] | p[5]; \ - t |= p[6] | p[7]; \ - } else { \ - link_error(); \ - } \ - if (unlikely(NONZERO(t))) { \ - return false; \ - } \ - buf += SIZE; \ - } while (buf < end); \ - return true; \ -} -typedef bool (*accel_zero_fn )(const void *,size_t ); static bool
@@ -98,31 +69,7 @@ buffer_zero_int(const void *buf, size_t len) } } -#ifdef __ALTIVEC__ -#include <altivec.h> -/* The altivec.h header says we're allowed to undef these for -* C++ compatibility. Here we don't care about C++, but we -* undef them anyway to avoid namespace pollution. -* altivec.h may redefine the bool macro as vector type. -* Reset it to POSIX semantics. -*/ -#undef vector -#undef pixel -#undef bool -#define bool _Bool -#define DO_NONZERO (X ) vec_any_ne(X, (__vector unsigned char){ 0 }) -ACCEL_BUFFER_ZERO(buffer_zero_ppc, 128, __vector unsigned char, DO_NONZERO) - -static bool select_accel_fn (const void *buf,size_t len) -{ -uintptr_t ibuf = (uintptr_t )buf; - if (len % 128 == 0 && ibuf % sizeof(__vector unsigned char) == 0) { -return buffer_zero_ppc (buf, len); - } -return buffer_zero_int (buf, len); -} - -#elif defined(CONFIG_AVX2_OPT ) || defined(__SSE2__) +#if defined(CONFIG_AVX2_OPT ) || defined(__SSE2__)#include <cpuid.h> #include <x86intrin.h>
Signature · 1 line
2.7.4
Quoted 19 lines · Richard Henderson
Changes from v2 to v3:
* Unit testing. This includes having x86 attempt all versions of the accelerator that will run on the hardware. Thus an avx2 host will run the basic test 5 times (1.5sec on my laptop).
* Drop the ppc and aarch64 specializations. I have improved the basic integer version to the point that those vectorized versions are not a win.
In the case of my aarch64 mustang, the integer version is 4 times faster than the neon version that I delete. With effort I was able to rewrite the neon version to come to within a factor of 1.1, but it remained slower than the integer. To be fair, gcc6 makes very good use of ldp, so the integer path is *also* loading 16 bytes per insn.
I can forward my standalone aarch64 benchmark if anyone is interested.
Quoted 8 lines
Note however that at least the avx2 acceleration is still very much a win, being about 3 times faster on my laptop. Of course, it's handling 4 times as much data per loop as the integer version, so one can still see the overhead caused by using vector insns.
For grins I wrote an avx512 version, if someone has a skylake upon which to test and benchmark. That requires additional configure checks, so I didn't bother to include it here.
Thanks, queued for 2.8.
Paolo
* Richard Henderson ([email protected]) wrote:
Have you considered contributing something similar to this to glibc? I filed https://sourceware.org/bugzilla/show_bug.cgi?id=19920 a while back suggesting it would be useful to have it in libc to be used
by things other than just qemu.
Dave
Quoted 43 lines
Changes from v2 to v3:
* Unit testing. This includes having x86 attempt all versions of the accelerator that will run on the hardware. Thus an avx2 host will run the basic test 5 times (1.5sec on my laptop).
* Drop the ppc and aarch64 specializations. I have improved the basic integer version to the point that those vectorized versions are not a win.
In the case of my aarch64 mustang, the integer version is 4 times faster than the neon version that I delete. With effort I was able to rewrite the neon version to come to within a factor of 1.1, but it remained slower than the integer. To be fair, gcc6 makes very good use of ldp, so the integer path is *also* loading 16 bytes per insn.
I can forward my standalone aarch64 benchmark if anyone is interested.
Note however that at least the avx2 acceleration is still very much a win, being about 3 times faster on my laptop. Of course, it's handling 4 times as much data per loop as the integer version, so one can still see the overhead caused by using vector insns.
For grins I wrote an avx512 version, if someone has a skylake upon which to test and benchmark. That requires additional configure checks, so I didn't bother to include it here.
r~
Richard Henderson (9):
cutils: Movebuffer_is_zeroand subroutines to a new file
cutils: Remove SPLAT macro
cutils: Export onlybuffer_is_zero
cutils: Rearrangebuffer_is_zeroacceleration
cutils: Add test forbuffer_is_zero
cutils: Add generic prefetch
cutils: Rewrite x86 buffer zero checking
cutils: Remove aarch64 buffer zero checking
cutils: Remove ppc buffer zero checking
Quoted 17 lines
configure | 21 +-- include/qemu/cutils.h | 3 +- migration/ram.c | 2 +- migration/rdma.c | 5 +- tests/Makefile.include | 3 + tests/test-bufferiszero.c | 78 +++++++++++ util/Makefile.objs | 1 + util/bufferiszero.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 244 ---------------------------------- 9 files changed, 423 insertions(+), 266 deletions(-) create mode 100644 tests/test-bufferiszero.c create mode 100644 util/bufferiszero.cSignature · 1 line
2.7.4
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK