446cf2
commit 3f4b61a0b8de67ef9f20737919c713ddfc4bd620
446cf2
Author: H.J. Lu <hjl.tools@gmail.com>
446cf2
Date:   Mon Jul 6 11:48:09 2020 -0700
446cf2
446cf2
    x86: Add thresholds for "rep movsb/stosb" to tunables
446cf2
    
446cf2
    Add x86_rep_movsb_threshold and x86_rep_stosb_threshold to tunables
446cf2
    to update thresholds for "rep movsb" and "rep stosb" at run-time.
446cf2
    
446cf2
    Note that the user specified threshold for "rep movsb" smaller than
446cf2
    the minimum threshold will be ignored.
446cf2
    
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
446cf2
Conflicts:
446cf2
	sysdeps/x86/cacheinfo.c
446cf2
	  (Previous backport of the shared cache computation fix.)
446cf2
446cf2
diff --git a/manual/tunables.texi b/manual/tunables.texi
446cf2
index ef10d2872cfc244e..55d5dfb14db4dfb8 100644
446cf2
--- a/manual/tunables.texi
446cf2
+++ b/manual/tunables.texi
446cf2
@@ -373,6 +373,22 @@ like memmove and memcpy.
446cf2
 This tunable is specific to i386 and x86-64.
446cf2
 @end deftp
446cf2
 
446cf2
+@deftp Tunable glibc.cpu.x86_rep_movsb_threshold
446cf2
+The @code{glibc.cpu.x86_rep_movsb_threshold} tunable allows the user to
446cf2
+set threshold in bytes to start using "rep movsb".  The value must be
446cf2
+greater than zero, and currently defaults to 2048 bytes.
446cf2
+
446cf2
+This tunable is specific to i386 and x86-64.
446cf2
+@end deftp
446cf2
+
446cf2
+@deftp Tunable glibc.cpu.x86_rep_stosb_threshold
446cf2
+The @code{glibc.cpu.x86_rep_stosb_threshold} tunable allows the user to
446cf2
+set threshold in bytes to start using "rep stosb".  The value must be
446cf2
+greater than zero, and currently defaults to 2048 bytes.
446cf2
+
446cf2
+This tunable is specific to i386 and x86-64.
446cf2
+@end deftp
446cf2
+
446cf2
 @deftp Tunable glibc.cpu.x86_ibt
446cf2
 The @code{glibc.cpu.x86_ibt} tunable allows the user to control how
446cf2
 indirect branch tracking (IBT) should be enabled.  Accepted values are
446cf2
diff --git a/sysdeps/x86/cacheinfo.c b/sysdeps/x86/cacheinfo.c
446cf2
index aa7cb705d546bcd0..c741a69fb19a1e95 100644
446cf2
--- a/sysdeps/x86/cacheinfo.c
446cf2
+++ b/sysdeps/x86/cacheinfo.c
446cf2
@@ -530,6 +530,12 @@ long int __x86_raw_shared_cache_size attribute_hidden = 1024 * 1024;
446cf2
 /* Threshold to use non temporal store.  */
446cf2
 long int __x86_shared_non_temporal_threshold attribute_hidden;
446cf2
 
446cf2
+/* Threshold to use Enhanced REP MOVSB.  */
446cf2
+long int __x86_rep_movsb_threshold attribute_hidden = 2048;
446cf2
+
446cf2
+/* Threshold to use Enhanced REP STOSB.  */
446cf2
+long int __x86_rep_stosb_threshold attribute_hidden = 2048;
446cf2
+
446cf2
 #ifndef DISABLE_PREFETCHW
446cf2
 /* PREFETCHW support flag for use in memory and string routines.  */
446cf2
 int __x86_prefetchw attribute_hidden;
446cf2
@@ -892,6 +898,36 @@ init_cacheinfo (void)
446cf2
     = (cpu_features->non_temporal_threshold != 0
446cf2
        ? cpu_features->non_temporal_threshold
446cf2
        : __x86_shared_cache_size * 3 / 4);
446cf2
+
446cf2
+  /* NB: The REP MOVSB threshold must be greater than VEC_SIZE * 8.  */
446cf2
+  unsigned int minimum_rep_movsb_threshold;
446cf2
+  /* NB: The default REP MOVSB threshold is 2048 * (VEC_SIZE / 16).  */
446cf2
+  unsigned int rep_movsb_threshold;
446cf2
+  if (CPU_FEATURES_ARCH_P (cpu_features, AVX512F_Usable)
446cf2
+      && !CPU_FEATURES_ARCH_P (cpu_features, Prefer_No_AVX512))
446cf2
+    {
446cf2
+      rep_movsb_threshold = 2048 * (64 / 16);
446cf2
+      minimum_rep_movsb_threshold = 64 * 8;
446cf2
+    }
446cf2
+  else if (CPU_FEATURES_ARCH_P (cpu_features,
446cf2
+				AVX_Fast_Unaligned_Load))
446cf2
+    {
446cf2
+      rep_movsb_threshold = 2048 * (32 / 16);
446cf2
+      minimum_rep_movsb_threshold = 32 * 8;
446cf2
+    }
446cf2
+  else
446cf2
+    {
446cf2
+      rep_movsb_threshold = 2048 * (16 / 16);
446cf2
+      minimum_rep_movsb_threshold = 16 * 8;
446cf2
+    }
446cf2
+  if (cpu_features->rep_movsb_threshold > minimum_rep_movsb_threshold)
446cf2
+    __x86_rep_movsb_threshold = cpu_features->rep_movsb_threshold;
446cf2
+  else
446cf2
+    __x86_rep_movsb_threshold = rep_movsb_threshold;
446cf2
+
446cf2
+# if HAVE_TUNABLES
446cf2
+  __x86_rep_stosb_threshold = cpu_features->rep_stosb_threshold;
446cf2
+# endif
446cf2
 }
446cf2
 
446cf2
 #endif
446cf2
diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c
446cf2
index 21565474839efffc..ad470f79ef7769fc 100644
446cf2
--- a/sysdeps/x86/cpu-features.c
446cf2
+++ b/sysdeps/x86/cpu-features.c
446cf2
@@ -605,6 +605,10 @@ no_cpuid:
446cf2
   TUNABLE_GET (hwcaps, tunable_val_t *, TUNABLE_CALLBACK (set_hwcaps));
446cf2
   cpu_features->non_temporal_threshold
446cf2
     = TUNABLE_GET (x86_non_temporal_threshold, long int, NULL);
446cf2
+  cpu_features->rep_movsb_threshold
446cf2
+    = TUNABLE_GET (x86_rep_movsb_threshold, long int, NULL);
446cf2
+  cpu_features->rep_stosb_threshold
446cf2
+    = TUNABLE_GET (x86_rep_stosb_threshold, long int, NULL);
446cf2
   cpu_features->data_cache_size
446cf2
     = TUNABLE_GET (x86_data_cache_size, long int, NULL);
446cf2
   cpu_features->shared_cache_size
446cf2
diff --git a/sysdeps/x86/cpu-features.h b/sysdeps/x86/cpu-features.h
446cf2
index e7ea9e8ece3e8211..0f19c64352c4d7f1 100644
446cf2
--- a/sysdeps/x86/cpu-features.h
446cf2
+++ b/sysdeps/x86/cpu-features.h
446cf2
@@ -102,6 +102,10 @@ struct cpu_features
446cf2
   unsigned long int shared_cache_size;
446cf2
   /* Threshold to use non temporal store.  */
446cf2
   unsigned long int non_temporal_threshold;
446cf2
+  /* Threshold to use "rep movsb".  */
446cf2
+  unsigned long int rep_movsb_threshold;
446cf2
+  /* Threshold to use "rep stosb".  */
446cf2
+  unsigned long int rep_stosb_threshold;
446cf2
 };
446cf2
 
446cf2
 /* Used from outside of glibc to get access to the CPU features
446cf2
diff --git a/sysdeps/x86/dl-tunables.list b/sysdeps/x86/dl-tunables.list
446cf2
index 2a457d0eec9c3122..e066313a1d1dd009 100644
446cf2
--- a/sysdeps/x86/dl-tunables.list
446cf2
+++ b/sysdeps/x86/dl-tunables.list
446cf2
@@ -30,6 +30,30 @@ glibc {
446cf2
     x86_non_temporal_threshold {
446cf2
       type: SIZE_T
446cf2
     }
446cf2
+    x86_rep_movsb_threshold {
446cf2
+      type: SIZE_T
446cf2
+      # Since there is overhead to set up REP MOVSB operation, REP MOVSB
446cf2
+      # isn't faster on short data.  The memcpy micro benchmark in glibc
446cf2
+      # shows that 2KB is the approximate value above which REP MOVSB
446cf2
+      # becomes faster than SSE2 optimization on processors with Enhanced
446cf2
+      # REP MOVSB.  Since larger register size can move more data with a
446cf2
+      # single load and store, the threshold is higher with larger register
446cf2
+      # size.  Note: Since the REP MOVSB threshold must be greater than 8
446cf2
+      # times of vector size, the minium value must be updated at run-time.
446cf2
+      minval: 1
446cf2
+      default: 2048
446cf2
+    }
446cf2
+    x86_rep_stosb_threshold {
446cf2
+      type: SIZE_T
446cf2
+      # Since there is overhead to set up REP STOSB operation, REP STOSB
446cf2
+      # isn't faster on short data.  The memset micro benchmark in glibc
446cf2
+      # shows that 2KB is the approximate value above which REP STOSB
446cf2
+      # becomes faster on processors with Enhanced REP STOSB.  Since the
446cf2
+      # stored value is fixed, larger register size has minimal impact
446cf2
+      # on threshold.
446cf2
+      minval: 1
446cf2
+      default: 2048
446cf2
+    }
446cf2
     x86_data_cache_size {
446cf2
       type: SIZE_T
446cf2
     }
446cf2
diff --git a/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S b/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S
446cf2
index e2ede45e9f68791b..c952576cfdf6e3e6 100644
446cf2
--- a/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S
446cf2
+++ b/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S
446cf2
@@ -56,17 +56,6 @@
446cf2
 # endif
446cf2
 #endif
446cf2
 
446cf2
-/* Threshold to use Enhanced REP MOVSB.  Since there is overhead to set
446cf2
-   up REP MOVSB operation, REP MOVSB isn't faster on short data.  The
446cf2
-   memcpy micro benchmark in glibc shows that 2KB is the approximate
446cf2
-   value above which REP MOVSB becomes faster than SSE2 optimization
446cf2
-   on processors with Enhanced REP MOVSB.  Since larger register size
446cf2
-   can move more data with a single load and store, the threshold is
446cf2
-   higher with larger register size.  */
446cf2
-#ifndef REP_MOVSB_THRESHOLD
446cf2
-# define REP_MOVSB_THRESHOLD	(2048 * (VEC_SIZE / 16))
446cf2
-#endif
446cf2
-
446cf2
 #ifndef PREFETCH
446cf2
 # define PREFETCH(addr) prefetcht0 addr
446cf2
 #endif
446cf2
@@ -245,9 +234,6 @@ L(movsb):
446cf2
 	leaq	(%rsi,%rdx), %r9
446cf2
 	cmpq	%r9, %rdi
446cf2
 	/* Avoid slow backward REP MOVSB.  */
446cf2
-# if REP_MOVSB_THRESHOLD <= (VEC_SIZE * 8)
446cf2
-#  error Unsupported REP_MOVSB_THRESHOLD and VEC_SIZE!
446cf2
-# endif
446cf2
 	jb	L(more_8x_vec_backward)
446cf2
 1:
446cf2
 	movq	%rdx, %rcx
446cf2
@@ -323,7 +309,7 @@ L(between_2_3):
446cf2
 
446cf2
 #if defined USE_MULTIARCH && IS_IN (libc)
446cf2
 L(movsb_more_2x_vec):
446cf2
-	cmpq	$REP_MOVSB_THRESHOLD, %rdx
446cf2
+	cmp	__x86_rep_movsb_threshold(%rip), %RDX_LP
446cf2
 	ja	L(movsb)
446cf2
 #endif
446cf2
 L(more_2x_vec):
446cf2
diff --git a/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S b/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S
446cf2
index dc9cb88b37a5477a..270a1d49b34be9f5 100644
446cf2
--- a/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S
446cf2
+++ b/sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S
446cf2
@@ -58,16 +58,6 @@
446cf2
 # endif
446cf2
 #endif
446cf2
 
446cf2
-/* Threshold to use Enhanced REP STOSB.  Since there is overhead to set
446cf2
-   up REP STOSB operation, REP STOSB isn't faster on short data.  The
446cf2
-   memset micro benchmark in glibc shows that 2KB is the approximate
446cf2
-   value above which REP STOSB becomes faster on processors with
446cf2
-   Enhanced REP STOSB.  Since the stored value is fixed, larger register
446cf2
-   size has minimal impact on threshold.  */
446cf2
-#ifndef REP_STOSB_THRESHOLD
446cf2
-# define REP_STOSB_THRESHOLD		2048
446cf2
-#endif
446cf2
-
446cf2
 #ifndef SECTION
446cf2
 # error SECTION is not defined!
446cf2
 #endif
446cf2
@@ -173,7 +163,7 @@ ENTRY (MEMSET_SYMBOL (__memset, unaligned_erms))
446cf2
 	ret
446cf2
 
446cf2
 L(stosb_more_2x_vec):
446cf2
-	cmpq	$REP_STOSB_THRESHOLD, %rdx
446cf2
+	cmp	__x86_rep_stosb_threshold(%rip), %RDX_LP
446cf2
 	ja	L(stosb)
446cf2
 #endif
446cf2
 L(more_2x_vec):