bca718
commit 83d776f979342f923b5c3d2a5b43afab841c6086
bca718
Author: Andrew Senkevich <andrew.senkevich@intel.com>
bca718
Date:   Sat Dec 19 02:47:28 2015 +0300
bca718
bca718
    Added memset optimized with AVX512 for KNL hardware.
bca718
    
bca718
    It shows improvement up to 28% over AVX2 memset (performance results
bca718
    attached at <https://sourceware.org/ml/libc-alpha/2015-12/msg00052.html>).
bca718
    
bca718
        * sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S: New file.
bca718
        * sysdeps/x86_64/multiarch/Makefile (sysdep_routines): Added new file.
bca718
        * sysdeps/x86_64/multiarch/ifunc-impl-list.c: Added new tests.
bca718
        * sysdeps/x86_64/multiarch/memset.S: Added new IFUNC branch.
bca718
        * sysdeps/x86_64/multiarch/memset_chk.S: Likewise.
bca718
        * sysdeps/x86/cpu-features.h (bit_Prefer_No_VZEROUPPER,
bca718
        index_Prefer_No_VZEROUPPER): New.
bca718
        * sysdeps/x86/cpu-features.c (init_cpu_features): Set the
bca718
        Prefer_No_VZEROUPPER for Knights Landing.
bca718
bca718
Index: glibc-2.17-c758a686/sysdeps/x86/cpu-features.c
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86/cpu-features.c
bca718
+++ glibc-2.17-c758a686/sysdeps/x86/cpu-features.c
bca718
@@ -76,6 +76,8 @@ init_cpu_features (struct cpu_features *
bca718
 
bca718
 	    case 0x57:
bca718
 	      /* Knights Landing.  Enable Silvermont optimizations.  */
bca718
+	      cpu_features->feature[index_Prefer_No_VZEROUPPER]
bca718
+		|= bit_Prefer_No_VZEROUPPER;
bca718
 
bca718
 	    case 0x37:
bca718
 	    case 0x4a:
bca718
Index: glibc-2.17-c758a686/sysdeps/x86/cpu-features.h
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86/cpu-features.h
bca718
+++ glibc-2.17-c758a686/sysdeps/x86/cpu-features.h
bca718
@@ -33,6 +33,7 @@
bca718
 #define bit_AVX512F_Usable		(1 << 12)
bca718
 #define bit_AVX512DQ_Usable		(1 << 13)
bca718
 #define bit_Prefer_MAP_32BIT_EXEC	(1 << 16)
bca718
+#define bit_Prefer_No_VZEROUPPER	(1 << 17)
bca718
 
bca718
 /* CPUID Feature flags.  */
bca718
 
bca718
@@ -93,6 +94,7 @@
bca718
 # define index_AVX512F_Usable		FEATURE_INDEX_1*FEATURE_SIZE
bca718
 # define index_AVX512DQ_Usable		FEATURE_INDEX_1*FEATURE_SIZE
bca718
 # define index_Prefer_MAP_32BIT_EXEC	FEATURE_INDEX_1*FEATURE_SIZE
bca718
+# define index_Prefer_No_VZEROUPPER	FEATURE_INDEX_1*FEATURE_SIZE
bca718
 
bca718
 # if defined (_LIBC) && !IS_IN (nonlib)
bca718
 #  ifdef __x86_64__
bca718
@@ -270,6 +272,7 @@ extern const struct cpu_features *__get_
bca718
 # define index_AVX512F_Usable		FEATURE_INDEX_1
bca718
 # define index_AVX512DQ_Usable		FEATURE_INDEX_1
bca718
 # define index_Prefer_MAP_32BIT_EXEC	FEATURE_INDEX_1
bca718
+# define index_Prefer_No_VZEROUPPER     FEATURE_INDEX_1
bca718
 
bca718
 #endif	/* !__ASSEMBLER__ */
bca718
 
bca718
Index: glibc-2.17-c758a686/sysdeps/x86_64/multiarch/Makefile
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86_64/multiarch/Makefile
bca718
+++ glibc-2.17-c758a686/sysdeps/x86_64/multiarch/Makefile
bca718
@@ -16,7 +16,8 @@ sysdep_routines += strncat-c stpncpy-c s
bca718
 		   strcat-sse2-unaligned strncat-sse2-unaligned \
bca718
 		   strcat-ssse3 strncat-ssse3 strlen-sse2-pminub \
bca718
 		   strnlen-sse2-no-bsf strrchr-sse2-no-bsf strchr-sse2-no-bsf \
bca718
-		   memcmp-ssse3 strstr-sse2-unaligned
bca718
+		   memcmp-ssse3 strstr-sse2-unaligned \
bca718
+		   memset-avx512-no-vzeroupper
bca718
 ifeq (yes,$(config-cflags-sse4))
bca718
 sysdep_routines += strcspn-c strpbrk-c strspn-c strstr-c strcasestr-c varshift
bca718
 CFLAGS-varshift.c += -msse4
bca718
Index: glibc-2.17-c758a686/sysdeps/x86_64/multiarch/ifunc-impl-list.c
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86_64/multiarch/ifunc-impl-list.c
bca718
+++ glibc-2.17-c758a686/sysdeps/x86_64/multiarch/ifunc-impl-list.c
bca718
@@ -20,6 +20,7 @@
bca718
 #include <string.h>
bca718
 #include <wchar.h>
bca718
 #include <ifunc-impl-list.h>
bca718
+#include <sysdep.h>
bca718
 #include "init-arch.h"
bca718
 
bca718
 /* Maximum number of IFUNC implementations.  */
bca718
@@ -66,12 +67,24 @@ __libc_ifunc_impl_list (const char *name
bca718
   IFUNC_IMPL (i, name, __memset_chk,
bca718
 	      IFUNC_IMPL_ADD (array, i, __memset_chk, 1, __memset_chk_sse2)
bca718
 	      IFUNC_IMPL_ADD (array, i, __memset_chk, 1,
bca718
-			      __memset_chk_x86_64))
bca718
+			      __memset_chk_x86_64)
bca718
+#ifdef HAVE_AVX512_ASM_SUPPORT
bca718
+	      IFUNC_IMPL_ADD (array, i, __memset_chk,
bca718
+			      HAS_ARCH_FEATURE (AVX512F_Usable),
bca718
+			      __memset_chk_avx512_no_vzeroupper)
bca718
+#endif
bca718
+	      )
bca718
 
bca718
   /* Support sysdeps/x86_64/multiarch/memset.S.  */
bca718
   IFUNC_IMPL (i, name, memset,
bca718
 	      IFUNC_IMPL_ADD (array, i, memset, 1, __memset_sse2)
bca718
-	      IFUNC_IMPL_ADD (array, i, memset, 1, __memset_x86_64))
bca718
+	      IFUNC_IMPL_ADD (array, i, memset, 1, __memset_x86_64)
bca718
+#ifdef HAVE_AVX512_ASM_SUPPORT
bca718
+	      IFUNC_IMPL_ADD (array, i, memset,
bca718
+			      HAS_ARCH_FEATURE (AVX512F_Usable),
bca718
+			      __memset_avx512_no_vzeroupper)
bca718
+#endif
bca718
+	     )
bca718
 
bca718
   /* Support sysdeps/x86_64/multiarch/rawmemchr.S.  */
bca718
   IFUNC_IMPL (i, name, rawmemchr,
bca718
Index: glibc-2.17-c758a686/sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S
bca718
===================================================================
bca718
--- /dev/null
bca718
+++ glibc-2.17-c758a686/sysdeps/x86_64/multiarch/memset-avx512-no-vzeroupper.S
bca718
@@ -0,0 +1,194 @@
bca718
+/* memset optimized with AVX512 for KNL hardware.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#include <sysdep.h>
bca718
+
bca718
+#if defined HAVE_AVX512_ASM_SUPPORT && IS_IN (libc)
bca718
+
bca718
+#include "asm-syntax.h"
bca718
+#ifndef MEMSET
bca718
+# define MEMSET __memset_avx512_no_vzeroupper
bca718
+# define MEMSET_CHK __memset_chk_avx512_no_vzeroupper
bca718
+#endif
bca718
+
bca718
+	.section .text,"ax",@progbits
bca718
+#if defined PIC
bca718
+ENTRY (MEMSET_CHK)
bca718
+	cmpq	%rdx, %rcx
bca718
+	jb	HIDDEN_JUMPTARGET (__chk_fail)
bca718
+END (MEMSET_CHK)
bca718
+#endif
bca718
+
bca718
+ENTRY (MEMSET)
bca718
+	vpxor	%xmm0, %xmm0, %xmm0
bca718
+	vmovd	%esi, %xmm1
bca718
+	lea	(%rdi, %rdx), %rsi
bca718
+	mov	%rdi, %rax
bca718
+	vpshufb	%xmm0, %xmm1, %xmm0
bca718
+	cmp	$16, %rdx
bca718
+	jb	L(less_16bytes)
bca718
+	cmp	$512, %rdx
bca718
+	vbroadcastss	%xmm0, %zmm2
bca718
+	ja	L(512bytesormore)
bca718
+	cmp	$256, %rdx
bca718
+	jb	L(less_256bytes)
bca718
+	vmovups	%zmm2, (%rdi)
bca718
+	vmovups %zmm2, 0x40(%rdi)
bca718
+	vmovups %zmm2, 0x80(%rdi)
bca718
+	vmovups %zmm2, 0xC0(%rdi)
bca718
+	vmovups %zmm2, -0x100(%rsi)
bca718
+	vmovups %zmm2, -0xC0(%rsi)
bca718
+	vmovups %zmm2, -0x80(%rsi)
bca718
+	vmovups %zmm2, -0x40(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_256bytes):
bca718
+	cmp	$128, %dl
bca718
+	jb	L(less_128bytes)
bca718
+	vmovups	%zmm2, (%rdi)
bca718
+	vmovups %zmm2, 0x40(%rdi)
bca718
+	vmovups %zmm2, -0x80(%rsi)
bca718
+	vmovups %zmm2, -0x40(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_128bytes):
bca718
+	cmp	$64, %dl
bca718
+	jb	L(less_64bytes)
bca718
+	vmovups	%zmm2, (%rdi)
bca718
+	vmovups	%zmm2, -0x40(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_64bytes):
bca718
+	cmp	$32, %dl
bca718
+	jb	L(less_32bytes)
bca718
+	vmovdqu	%ymm2, (%rdi)
bca718
+	vmovdqu %ymm2, -0x20(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_32bytes):
bca718
+	vmovdqu %xmm0, (%rdi)
bca718
+	vmovdqu %xmm0, -0x10(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_16bytes):
bca718
+	cmp	$8, %dl
bca718
+	jb	L(less_8bytes)
bca718
+	vmovq	%xmm0, (%rdi)
bca718
+	vmovq	%xmm0, -0x08(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_8bytes):
bca718
+	vmovd	%xmm0, %ecx
bca718
+	cmp	$4, %dl
bca718
+	jb	L(less_4bytes)
bca718
+	mov	%ecx, (%rdi)
bca718
+	mov	%ecx, -0x04(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_4bytes):
bca718
+	cmp	$2, %dl
bca718
+	jb	L(less_2bytes)
bca718
+	mov	%cx, (%rdi)
bca718
+	mov	%cx, -0x02(%rsi)
bca718
+	ret
bca718
+
bca718
+L(less_2bytes):
bca718
+	cmp	$1, %dl
bca718
+	jb	L(less_1bytes)
bca718
+	mov	%cl, (%rdi)
bca718
+L(less_1bytes):
bca718
+	ret
bca718
+
bca718
+L(512bytesormore):
bca718
+	mov	__x86_64_shared_cache_size_half(%rip), %rcx
bca718
+	cmp	%rcx, %rdx
bca718
+	ja	L(preloop_large)
bca718
+	cmp	$1024, %rdx
bca718
+	ja	L(1024bytesormore)
bca718
+
bca718
+	vmovups	%zmm2, (%rdi)
bca718
+	vmovups	%zmm2, 0x40(%rdi)
bca718
+	vmovups	%zmm2, 0x80(%rdi)
bca718
+	vmovups	%zmm2, 0xC0(%rdi)
bca718
+	vmovups	%zmm2, 0x100(%rdi)
bca718
+	vmovups	%zmm2, 0x140(%rdi)
bca718
+	vmovups	%zmm2, 0x180(%rdi)
bca718
+	vmovups	%zmm2, 0x1C0(%rdi)
bca718
+	vmovups %zmm2, -0x200(%rsi)
bca718
+	vmovups %zmm2, -0x1C0(%rsi)
bca718
+	vmovups %zmm2, -0x180(%rsi)
bca718
+	vmovups %zmm2, -0x140(%rsi)
bca718
+	vmovups %zmm2, -0x100(%rsi)
bca718
+	vmovups %zmm2, -0xC0(%rsi)
bca718
+	vmovups %zmm2, -0x80(%rsi)
bca718
+	vmovups %zmm2, -0x40(%rsi)
bca718
+	ret
bca718
+
bca718
+/* Align on 64 and loop with aligned stores.  */
bca718
+L(1024bytesormore):
bca718
+	sub	$0x100, %rsi
bca718
+	vmovups	%zmm2, (%rax)
bca718
+	and	$-0x40, %rdi
bca718
+	add	$0x40, %rdi
bca718
+
bca718
+L(gobble_256bytes_loop):
bca718
+	vmovaps	%zmm2, (%rdi)
bca718
+	vmovaps	%zmm2, 0x40(%rdi)
bca718
+	vmovaps	%zmm2, 0x80(%rdi)
bca718
+	vmovaps	%zmm2, 0xC0(%rdi)
bca718
+	add	$0x100, %rdi
bca718
+	cmp	%rsi, %rdi
bca718
+	jb	L(gobble_256bytes_loop)
bca718
+	vmovups %zmm2, (%rsi)
bca718
+	vmovups %zmm2, 0x40(%rsi)
bca718
+	vmovups %zmm2, 0x80(%rsi)
bca718
+	vmovups %zmm2, 0xC0(%rsi)
bca718
+	ret
bca718
+
bca718
+/* Align on 128 and loop with non-temporal stores.  */
bca718
+L(preloop_large):
bca718
+	and	$-0x80, %rdi
bca718
+	add	$0x80, %rdi
bca718
+	vmovups	%zmm2, (%rax)
bca718
+	vmovups	%zmm2, 0x40(%rax)
bca718
+	sub	$0x200, %rsi
bca718
+
bca718
+L(gobble_512bytes_nt_loop):
bca718
+	vmovntdq %zmm2, (%rdi)
bca718
+	vmovntdq %zmm2, 0x40(%rdi)
bca718
+	vmovntdq %zmm2, 0x80(%rdi)
bca718
+	vmovntdq %zmm2, 0xC0(%rdi)
bca718
+	vmovntdq %zmm2, 0x100(%rdi)
bca718
+	vmovntdq %zmm2, 0x140(%rdi)
bca718
+	vmovntdq %zmm2, 0x180(%rdi)
bca718
+	vmovntdq %zmm2, 0x1C0(%rdi)
bca718
+	add	$0x200, %rdi
bca718
+	cmp	%rsi, %rdi
bca718
+	jb	L(gobble_512bytes_nt_loop)
bca718
+	sfence
bca718
+	vmovups %zmm2, (%rsi)
bca718
+	vmovups %zmm2, 0x40(%rsi)
bca718
+	vmovups %zmm2, 0x80(%rsi)
bca718
+	vmovups %zmm2, 0xC0(%rsi)
bca718
+	vmovups	%zmm2, 0x100(%rsi)
bca718
+	vmovups	%zmm2, 0x140(%rsi)
bca718
+	vmovups	%zmm2, 0x180(%rsi)
bca718
+	vmovups	%zmm2, 0x1C0(%rsi)
bca718
+	ret
bca718
+END (MEMSET)
bca718
+#endif
bca718
Index: glibc-2.17-c758a686/sysdeps/x86_64/multiarch/memset.S
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86_64/multiarch/memset.S
bca718
+++ glibc-2.17-c758a686/sysdeps/x86_64/multiarch/memset.S
bca718
@@ -29,6 +29,13 @@ ENTRY(memset)
bca718
 	HAS_ARCH_FEATURE (Prefer_SSE_for_memop)
bca718
 	jz	2f
bca718
 	leaq	__memset_sse2(%rip), %rax
bca718
+#ifdef HAVE_AVX512_ASM_SUPPORT
bca718
+	HAS_ARCH_FEATURE (AVX512F_Usable)
bca718
+	jz	2f
bca718
+	HAS_ARCH_FEATURE (Prefer_No_VZEROUPPER)
bca718
+	jz	2f
bca718
+	leaq	__memset_avx512_no_vzeroupper(%rip), %rax
bca718
+#endif
bca718
 2:	ret
bca718
 END(memset)
bca718
 
bca718
Index: glibc-2.17-c758a686/sysdeps/x86_64/multiarch/memset_chk.S
bca718
===================================================================
bca718
--- glibc-2.17-c758a686.orig/sysdeps/x86_64/multiarch/memset_chk.S
bca718
+++ glibc-2.17-c758a686/sysdeps/x86_64/multiarch/memset_chk.S
bca718
@@ -30,6 +30,13 @@ ENTRY(__memset_chk)
bca718
 	HAS_ARCH_FEATURE (Prefer_SSE_for_memop)
bca718
 	jz	2f
bca718
 	leaq	__memset_chk_sse2(%rip), %rax
bca718
+#ifdef HAVE_AVX512_ASM_SUPPORT
bca718
+	HAS_ARCH_FEATURE (AVX512F_Usable)
bca718
+	jz	2f
bca718
+	HAS_ARCH_FEATURE (Prefer_No_VZEROUPPER)
bca718
+	jz	2f
bca718
+	leaq	__memset_chk_avx512_no_vzeroupper(%rip), %rax
bca718
+#endif
bca718
 2:	ret
bca718
 END(__memset_chk)
bca718