From 0fce2f10a788d03fdc7d3a29474c3d45f4bb4d3c Mon Sep 17 00:00:00 2001 From: Amit Pawar Date: Fri, 5 Feb 2016 22:35:32 +0530 Subject: [PATCH] RFC x86_64 patch to improve selection order for string and memory routines. Code refactoring is done to improve the selection logic for multi-versioning functions through currently implemented IFUNC's. Structure array is defined which contains details of multi-versioning functions along with their CPU and ARCH flags. These flags are mapped to their implementations. Two functions are defined one for intel and AMD. Each function is responsible for defining currect selection logic based on family and model values. This order is verified based on given choices and selected if supported. sysdep/x86_64 * sysdeps/x86_64/selectionorder.h: New file. (feature_type): enum added. (cpu_selection): structure added. * sysdeps/x86_64/stringselection.h: New file. (index_): new macros defined. (index__): new macros defined. * sysdeps/x86_64/stringinfo.c: New file. (string_implementations): new structure variable defined. (__get_string_func_address): new function to return function address. (test_bit): new function varifies CPU or ARCH bit supported or not. (verify_update_global_array): new function verfies passed input array. (amd_cpus): new function that defines selection order for AMD CPU's. (intel_cpus): new function that defines selection order for INTEL CPU's. (register_string_functions): new function to do basic initialization. --- ChangeLog | 18 ++ sysdeps/x86_64/Makefile | 2 +- sysdeps/x86_64/multiarch/memcmp.S | 19 +- sysdeps/x86_64/multiarch/memset.S | 21 +-- sysdeps/x86_64/selectionorder.h | 42 +++++ sysdeps/x86_64/stringinfo.c | 355 ++++++++++++++++++++++++++++++++++++++ sysdeps/x86_64/stringselection.h | 87 ++++++++++ 7 files changed, 518 insertions(+), 26 deletions(-) create mode 100644 sysdeps/x86_64/selectionorder.h create mode 100644 sysdeps/x86_64/stringinfo.c create mode 100644 sysdeps/x86_64/stringselection.h diff --git a/ChangeLog b/ChangeLog index 11c3156..a2c8760 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2016-02-05 Amit Pawar + + * sysdeps/x86_64/selectionorder.h: New file. + (feature_type): enum added. + (cpu_selection): structure added. + * sysdeps/x86_64/stringselection.h: New file. + (index_): new macros defined. + (index__): new macros + defined. + * sysdeps/x86_64/stringinfo.c: New file. + (string_implementations): new structure variable defined + (__get_string_func_address): new function to return function address. + (test_bit): new function varifies CPU or ARCH bit supported or not. + (verify_update_global_array): new function verfies passed input array. + (amd_cpus): new function that defines selection order for AMD CPU's. + (intel_cpus): new function that defines selection order for INTEL CPU's. + (register_string_functions): new function to do basic initialization + 2016-02-04 Rajalakshmi Srinivasaraghavan * sysdeps/powerpc/fpu/libm-test-ulps: Regenerated. diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile index 67ed5ba..2313892 100644 --- a/sysdeps/x86_64/Makefile +++ b/sysdeps/x86_64/Makefile @@ -14,7 +14,7 @@ tests += tst-mallocalign1 endif ifeq ($(subdir),string) -sysdep_routines += cacheinfo strcasecmp_l-nonascii strncase_l-nonascii +sysdep_routines += cacheinfo strcasecmp_l-nonascii strncase_l-nonascii stringinfo gen-as-const-headers += locale-defines.sym endif diff --git a/sysdeps/x86_64/multiarch/memcmp.S b/sysdeps/x86_64/multiarch/memcmp.S index b5a1cc2..76fbccc 100644 --- a/sysdeps/x86_64/multiarch/memcmp.S +++ b/sysdeps/x86_64/multiarch/memcmp.S @@ -20,24 +20,19 @@ #include #include +#include /* Define multiple versions only for the definition in libc. */ #if IS_IN (libc) .text ENTRY(memcmp) .type memcmp, @gnu_indirect_function - LOAD_RTLD_GLOBAL_RO_RDX - HAS_CPU_FEATURE (SSSE3) - jnz 2f - leaq __memcmp_sse2(%rip), %rax - ret - -2: HAS_CPU_FEATURE (SSE4_1) - jz 3f - leaq __memcmp_sse4_1(%rip), %rax - ret - -3: leaq __memcmp_ssse3(%rip), %rax + pushq %rbp + movq %rsp, %rbp + subq $32, %rsp + mov $index_MEMCMP, %edi + callq __get_string_func_address + leave ret END(memcmp) diff --git a/sysdeps/x86_64/multiarch/memset.S b/sysdeps/x86_64/multiarch/memset.S index 8e3b9b9..566c662 100644 --- a/sysdeps/x86_64/multiarch/memset.S +++ b/sysdeps/x86_64/multiarch/memset.S @@ -20,24 +20,19 @@ #include #include #include +#include "stringselection.h" /* Define multiple versions only for the definition in lib. */ #if IS_IN (libc) ENTRY(memset) .type memset, @gnu_indirect_function - LOAD_RTLD_GLOBAL_RO_RDX - leaq __memset_sse2(%rip), %rax - HAS_ARCH_FEATURE (AVX2_Usable) - jz 2f - leaq __memset_avx2(%rip), %rax -#ifdef HAVE_AVX512_ASM_SUPPORT - HAS_ARCH_FEATURE (AVX512F_Usable) - jz 2f - HAS_ARCH_FEATURE (Prefer_No_VZEROUPPER) - jz 2f - leaq __memset_avx512_no_vzeroupper(%rip), %rax -#endif -2: ret + pushq %rbp + movq %rsp, %rbp + subq $32, %rsp + mov $index_MEMSET, %edi + callq __get_string_func_address + leave + ret END(memset) #endif diff --git a/sysdeps/x86_64/selectionorder.h b/sysdeps/x86_64/selectionorder.h new file mode 100644 index 0000000..e3c9395 --- /dev/null +++ b/sysdeps/x86_64/selectionorder.h @@ -0,0 +1,42 @@ +/* x86_64 selection order details. + Copyright (C) 2003-2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef cpu_selection_order_h +#define cpu_selection_order_h + +/* CPU selection order structure.*/ +typedef enum +{ + OTHER_TYPE, + CPU_TYPE, + ARCH_TYPE, + MULTI_ARCH_TYPE, +}feature_type; + +typedef struct +{ + unsigned int n_implementation; + struct implementation_details + { + feature_type feature_flag; + unsigned int bit_flag; + void *funcptrs; + }impl_list[TOTAL_IMPLEMENTATION]; +}cpu_selection; + +#endif //cpu_selection_order_h diff --git a/sysdeps/x86_64/stringinfo.c b/sysdeps/x86_64/stringinfo.c new file mode 100644 index 0000000..3d6a976 --- /dev/null +++ b/sysdeps/x86_64/stringinfo.c @@ -0,0 +1,355 @@ +/* x86_64 string/memory selection order details. + Copyright (C) 2003-2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include "stringselection.h" +#include + +#undef __get_cpu_features +#include + + +extern void* __memset_sse2(void *, int ,ssize_t) attribute_hidden; +extern void* __memset_avx2(void *, int ,ssize_t) attribute_hidden; + +extern void* __memcmp_sse2(void *, void * ,ssize_t) attribute_hidden; +extern void* __memcmp_sse2_unaligned(void *, void * ,ssize_t) attribute_hidden; +extern void* __memcmp_ssse3(void *, void * ,ssize_t) attribute_hidden; +extern void* __memcmp_sse4_1(void *, void * ,ssize_t) attribute_hidden; + +extern void* __memcpy_sse2(void *, void * ,ssize_t) attribute_hidden; +extern void* __memcpy_sse2_unaligned(void *, void * ,ssize_t) attribute_hidden; +extern void* __memcpy_ssse3(void *, void * ,ssize_t) attribute_hidden; +extern void* __memcpy_avx_unaligned(void *, void * ,ssize_t) attribute_hidden; + +#ifdef HAVE_AVX512_ASM_SUPPORT +extern void* __memset_avx512_no_vzeroupper(void *, int ,ssize_t) attribute_hidden; +#endif + +static void register_string_functions(void); + + +#define B(x) bit_##x +#define B_M(x,y) ( B(x) | B(y) ) + +unsigned int string_functions[TOTAL_STRING_ROUTINES]; + +const cpu_selection string_implementations[TOTAL_STRING_ROUTINES] = + { + /* Memset */ + [index_MEMSET]= + { + .n_implementation= 2, + { + [MEMSET_I(SSE2)] = {CPU_TYPE, B(SSE2), __memset_sse2}, + [MEMSET_I(AVX2_Usable)] = {ARCH_TYPE, B(AVX2_Usable), __memset_avx2}, +#ifdef HAVE_AVX512_ASM_SUPPORT + [MEMSET_I(AVX512_no_vzeroupper)] = + {MULTI_ARCH_TYPE, B_M(AVX512F_Usable,Prefer_No_VZEROUPPER), + __memset_avx512_no_vzeroupper}, +#endif + } + }, + + /* Memcmp */ + [index_MEMCMP]= + { + .n_implementation= 3, + { + [index_MEMCMP_SSE2] = {CPU_TYPE, B(SSE2), __memcmp_sse2}, + [index_MEMCMP_SSSE3] = {CPU_TYPE, B(AVX2), __memcmp_ssse3}, + [index_MEMCMP_SSE4_1] = {CPU_TYPE, B(SSE4_1), __memcmp_sse4_1}, + } + }, + + [index_MEMCPY]= + { +#ifdef SHARED + .n_implementation= 4, + { + [index_MEMCPY_SSE2] = {CPU_TYPE, B(SSE2), __memcpy_sse2}, + [index_MEMCPY_Slow_BSF] = + {ARCH_TYPE, B(Slow_BSF), __memcpy_sse2_unaligned}, + [index_MEMCPY_SSSE3] = {CPU_TYPE, B(SSSE3), __memcpy_ssse3}, + [index_MEMCPY_AVX_Unaligned] = + {ARCH_TYPE, B(AVX_Fast_Unaligned_Load), __memcpy_avx_unaligned}, + } +#else + .n_implementation= 1, + { + [index_MEMCPY_SSE2] = {CPU_TYPE, B(SSE2), memcpy}, + } +#endif + }, + }; + + +#ifdef SHARED +extern void *__get_string_func_address (unsigned int ); +libc_hidden_proto(__get_string_func_address) +#endif + +void * +__get_string_func_address (unsigned int string_routine_index) +{ + unsigned int index=-1; + +#ifndef SHARED + static int initialized = 1; + if (initialized) + { + register_string_functions(); + initialized = 0; + } +#endif + + if (string_routine_index < TOTAL_STRING_ROUTINES) + index = string_functions[string_routine_index]; + else + index = 0; + + return string_implementations[string_routine_index].impl_list[index].funcptrs; + +} + +#ifdef SHARED +libc_hidden_def (__get_string_func_address) +#endif + +static unsigned int +test_bit (unsigned int bit_flag, feature_type feature_flag) +{ + unsigned int flag= 0; + switch (feature_flag) + { + case CPU_TYPE: + + switch (bit_flag) + { + +#define TEST_CPU_FLAG(x) \ + case bit_##x: \ + \ + if (HAS_CPU_FEATURE(x)) \ + flag=1; \ + \ + break; + + TEST_CPU_FLAG(CX8) + TEST_CPU_FLAG(CMOV) + TEST_CPU_FLAG(SSE2) + TEST_CPU_FLAG(SSSE3) + TEST_CPU_FLAG(SSE4_1) + TEST_CPU_FLAG(SSE4_2) + TEST_CPU_FLAG(AVX) + TEST_CPU_FLAG(AVX2) + + default: + flag=0; + } + + + break; + + case ARCH_TYPE: + case MULTI_ARCH_TYPE: + +#define TEST_ARCH_FLAG(x) \ + case bit_##x: \ + \ + if (HAS_ARCH_FEATURE(x)) \ + flag=1; \ + \ + break; + + switch (bit_flag) + { + + TEST_ARCH_FLAG(Fast_Rep_String) + TEST_ARCH_FLAG(Fast_Copy_Backward) + TEST_ARCH_FLAG(Slow_BSF) + TEST_ARCH_FLAG(Fast_Unaligned_Load) + TEST_ARCH_FLAG(Prefer_PMINUB_for_stringop) + TEST_ARCH_FLAG(AVX_Usable) + TEST_ARCH_FLAG(FMA_Usable) + TEST_ARCH_FLAG(FMA4_Usable) + TEST_ARCH_FLAG(Slow_SSE4_2) + TEST_ARCH_FLAG(AVX2_Usable) + TEST_ARCH_FLAG(AVX_Fast_Unaligned_Load) + TEST_ARCH_FLAG(AVX512F_Usable) + TEST_ARCH_FLAG(AVX512DQ_Usable) + TEST_ARCH_FLAG(I586) + TEST_ARCH_FLAG(I686) + TEST_ARCH_FLAG(Prefer_MAP_32BIT_EXEC) + TEST_ARCH_FLAG(Prefer_No_VZEROUPPER) + default: + flag = 0; + } + + break; + + default: + flag = 0; + } + + return flag; +} + +static void +verify_update_global_array(unsigned int array[][TOTAL_IMPLEMENTATION]) +{ + unsigned int i,j,k; + + for ( i=0 ; i < TOTAL_STRING_ROUTINES; i++) + { + k = 0; + + for (j=0; j < string_implementations[i].n_implementation ; j++) + { + feature_type feature_flag; + unsigned int bit_flag; + + k = array[i][j]; + bit_flag = string_implementations[i].impl_list[k].bit_flag; + feature_flag = string_implementations[i].impl_list[k].feature_flag; + + if ((feature_flag == CPU_TYPE || feature_flag == ARCH_TYPE) + && test_bit (bit_flag, feature_flag)) + { + string_functions[i] = k; + break; + } + + if (feature_flag == MULTI_ARCH_TYPE) + { + // Handle it + string_functions[i] = k; + break; + } + } + } +} + +static void +amd_cpus(const struct cpu_features *cpu_features) +{ + + // Contains array of no used for indexing in main string_implementation array. + unsigned int s_order[TOTAL_STRING_ROUTINES][TOTAL_IMPLEMENTATION]; + unsigned int i,j; + + for (i=0;i < TOTAL_STRING_ROUTINES ; i++) + for (j=0;jfamily) + { + case 21: + + /* Excavator */ + if (cpu_features->model >= 0x60 && cpu_features->model <= 0x7f) + { + /* SSE2 is first choice for memset*/ + s_order [index_MEMSET][FIRST_CHOICE] = MEMSET_I(SSE2); + s_order [index_MEMSET][SECOND_CHOICE] = MEMSET_I(AVX2_Usable); + + /* MEMCMP does not need changes */ +/* + s_order [index_MEMCPY][FIRST_CHOICE] = index_MEMCPY_AVX_Unaligned; + s_order [index_MEMCPY][SECOND_CHOICE] = index_MEMCPY_SSSE3; + s_order [index_MEMCPY][THIRD_CHOICE] = index_MEMCPY_Slow_BSF; + s_order [index_MEMCPY][FOURTH_CHOICE] = index_MEMCPY_SSE2; +*/ + } + + break; + + default: + ; + + /* Set default selection order for AMD CPU's */ + } + + verify_update_global_array (s_order); + +} + +static void +intel_cpus(const struct cpu_features *cpu_features) +{ + unsigned int s_order[TOTAL_STRING_ROUTINES][TOTAL_IMPLEMENTATION]; + unsigned int i,j; + + for (i=0;i < TOTAL_STRING_ROUTINES ; i++) + for (j=0;jfamily) + { + default: + ; + break; + /* Set default selection order for AMD CPU's */ + } + + verify_update_global_array (s_order); + +} + + +static void +#ifdef SHARED + __attribute__((constructor)) +#endif +register_string_functions(void) +{ + const struct cpu_features *cpu_features; + cpu_features = __get_cpu_features(); + + switch (cpu_features->kind) + { + case arch_kind_amd: + amd_cpus(cpu_features); + break; + + case arch_kind_intel: + intel_cpus(cpu_features); + break; + + default: + break; + } +} diff --git a/sysdeps/x86_64/stringselection.h b/sysdeps/x86_64/stringselection.h new file mode 100644 index 0000000..237da4b --- /dev/null +++ b/sysdeps/x86_64/stringselection.h @@ -0,0 +1,87 @@ +/* x86_64 string/memory selection order details. + Copyright (C) 2003-2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef string_selection_order_h +#define string_selection_order_h + +#define TOTAL_STRING_ROUTINES 5 +#define MAX_STRING_IMPLEMENTED 7 + +#define TOTAL_IMPLEMENTATION MAX_STRING_IMPLEMENTED + +#define FIRST_CHOICE 0 +#define SECOND_CHOICE 1 +#define THIRD_CHOICE 2 +#define FOURTH_CHOICE 3 +#define FIFTH_CHOICE 4 +#define SIX_CHOICE 5 +#define SEVENTH_CHOICE 6 + + +#define STRING_START_INDEX 0 + +/* String routine mapping*/ +#define index_MEMSET (STRING_START_INDEX) +#define index_MEMSET_CHK (STRING_START_INDEX+1) +#define index_MEMCMP (STRING_START_INDEX+2) +#define index_MEMCPY (STRING_START_INDEX+3) +#define index_MEMCPY_CHK (STRING_START_INDEX+4) +#define index_MEMPCPY (STRING_START_INDEX+5) +#define index_MEMPCPY_CHK (STRING_START_INDEX+6) +#define index_MEMMOVE (STRING_START_INDEX+7) + +#define index_STRLEN (STRING_START_INDEX+8) +#define index_STRNLEN (STRING_START_INDEX+9) +/* +#define index_STRCMP (STRING_START_INDEX+4) +#define index_STRNCMP (STRING_START_INDEX+4) +#define index_STRCPY (STRING_START_INDEX+4) +#define index_STRNCPY (STRING_START_INDEX+4) +#define index_STRSTR (STRING_START_INDEX+4) +#define index_STRCMP (STRING_START_INDEX+4) +#define index_STRCMP (STRING_START_INDEX+4) +#define index_STRCMP (STRING_START_INDEX+4) +#define index_STRCMP (STRING_START_INDEX+4) +*/ + +/* Memset implementation mapping*/ +#define index_MEMSET_SSE2 0 +#define index_MEMSET_AVX2_Usable 1 +#define index_MEMSET_AVX512_no_vzeroupper 2 + +#define MEMSET_I(x) index_MEMSET_##x + + +#define index_MEMCMP_SSE2 0 +#define index_MEMCMP_SSSE3 1 +#define index_MEMCMP_SSE4_1 2 + +#define MEMCMP_I(x) index_MEMCMP_##x + +#define index_MEMCPY_SSE2 0 +#define index_MEMCPY_Slow_BSF 1 +#define index_MEMCPY_SSSE3 2 +#define index_MEMCPY_AVX_Unaligned 3 + + +#endif //string_selection_order + + +#ifndef ASSEMBLER +#include "selectionorder.h" +#endif -- 2.1.4