[libc-commits] [libc] 0c64cef - [libc] Rever "Simplifies multi implementations and benchmarks".

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Mon May 10 12:22:55 PDT 2021


Author: Siva Chandra Reddy
Date: 2021-05-10T19:20:27Z
New Revision: 0c64cef8943546fce28936bb51cce2e22f88c698

URL: https://github.com/llvm/llvm-project/commit/0c64cef8943546fce28936bb51cce2e22f88c698
DIFF: https://github.com/llvm/llvm-project/commit/0c64cef8943546fce28936bb51cce2e22f88c698.diff

LOG: [libc] Rever "Simplifies multi implementations and benchmarks".

This reverts commit 541f107871bc9c020925a6e5342542a47c902d12 as the bots
are failing with unknown architecture "x86-64-v*". Will let the original
author decide on the right course of action to correct the problem and
reland.

Added: 
    libc/src/string/aarch64/CMakeLists.txt
    libc/src/string/x86_64/CMakeLists.txt

Modified: 
    libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
    libc/src/string/CMakeLists.txt
    libc/test/src/string/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
index d0289d11d4eb0..a44186ea32cbe 100644
--- a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
+++ b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -3,14 +3,10 @@
 # ------------------------------------------------------------------------------
 
 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
-  set(ALL_CPU_FEATURES SSE2 SSE4_2 AVX2 AVX512F)
-  set(LIBC_COMPILE_OPTIONS_NATIVE -march=native)
-elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
-  set(LIBC_COMPILE_OPTIONS_NATIVE -mcpu=native)
+  set(ALL_CPU_FEATURES SSE SSE2 AVX AVX2 AVX512F)
+  list(SORT ALL_CPU_FEATURES)
 endif()
 
-list(SORT ALL_CPU_FEATURES)
-
 # Function to check whether the target CPU supports the provided set of features.
 # Usage:
 # cpu_supports(
@@ -26,6 +22,49 @@ function(cpu_supports output_var features)
   endif()
 endfunction()
 
+# Function to compute the flags to pass down to the compiler.
+# Usage:
+# compute_flags(
+#   <output variable>
+#   MARCH <arch name or "native">
+#   REQUIRE <list of mandatory features to enable>
+#   REJECT <list of features to disable>
+# )
+function(compute_flags output_var)
+  cmake_parse_arguments(
+    "COMPUTE_FLAGS"
+    "" # Optional arguments
+    "MARCH" # Single value arguments
+    "REQUIRE;REJECT" # Multi value arguments
+    ${ARGN})
+  # Check that features are not required and rejected at the same time.
+  if(COMPUTE_FLAGS_REQUIRE AND COMPUTE_FLAGS_REJECT)
+    _intersection(var ${COMPUTE_FLAGS_REQUIRE} ${COMPUTE_FLAGS_REJECT})
+    if(var)
+      message(FATAL_ERROR "Cpu Features REQUIRE and REJECT ${var}")
+    endif()
+  endif()
+  # Generate the compiler flags in `current`.
+  if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang|GNU")
+    if(COMPUTE_FLAGS_MARCH)
+      list(APPEND current "-march=${COMPUTE_FLAGS_MARCH}")
+    endif()
+    foreach(feature IN LISTS COMPUTE_FLAGS_REQUIRE)
+      string(TOLOWER ${feature} lowercase_feature)
+      list(APPEND current "-m${lowercase_feature}")
+    endforeach()
+    foreach(feature IN LISTS COMPUTE_FLAGS_REJECT)
+      string(TOLOWER ${feature} lowercase_feature)
+      list(APPEND current "-mno-${lowercase_feature}")
+    endforeach()
+  else()
+    # In future, we can extend for other compilers.
+    message(FATAL_ERROR "Unkown compiler ${CMAKE_CXX_COMPILER_ID}.")
+  endif()
+  # Export the list of flags.
+  set(${output_var} "${current}" PARENT_SCOPE)
+endfunction()
+
 # ------------------------------------------------------------------------------
 # Internal helpers and utilities.
 # ------------------------------------------------------------------------------
@@ -55,27 +94,54 @@ function(_generate_check_code)
 endfunction()
 _generate_check_code()
 
-set(LIBC_CPU_FEATURES "" CACHE PATH "Host supported CPU features")
-
-if(CMAKE_CROSSCOMPILING)
-  _intersection(cpu_features "${ALL_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
-  if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
-    message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
-  endif()
-  set(LIBC_CPU_FEATURES "${cpu_features}")
-else()
-  # Populates the LIBC_CPU_FEATURES list from host.
+# Compiles and runs the code generated above with the specified requirements.
+# This is helpful to infer which features a particular target supports or if
+# a specific features implies other features (e.g. BMI2 implies SSE2 and SSE).
+function(_check_defined_cpu_feature output_var)
+  cmake_parse_arguments(
+    "CHECK_DEFINED"
+    "" # Optional arguments
+    "MARCH" # Single value arguments
+    "REQUIRE;REJECT" # Multi value arguments
+    ${ARGN})
+  compute_flags(
+    flags
+    MARCH  ${CHECK_DEFINED_MARCH}
+    REQUIRE ${CHECK_DEFINED_REQUIRE}
+    REJECT  ${CHECK_DEFINED_REJECT})
   try_run(
     run_result compile_result "${CMAKE_CURRENT_BINARY_DIR}/check_${feature}"
     "${CMAKE_CURRENT_BINARY_DIR}/cpu_features/check_cpu_features.cpp"
-    COMPILE_DEFINITIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
+    COMPILE_DEFINITIONS ${flags}
     COMPILE_OUTPUT_VARIABLE compile_output
     RUN_OUTPUT_VARIABLE run_output)
   if("${run_result}" EQUAL 0)
-    set(LIBC_CPU_FEATURES "${run_output}")
+    set(${output_var}
+        "${run_output}"
+        PARENT_SCOPE)
   elseif(NOT ${compile_result})
     message(FATAL_ERROR "Failed to compile: ${compile_output}")
   else()
     message(FATAL_ERROR "Failed to run: ${run_output}")
   endif()
+endfunction()
+
+set(LIBC_CPU_FEATURES "" CACHE PATH "supported CPU features")
+
+if(CMAKE_CROSSCOMPILING)
+  _intersection(cpu_features "${ALL_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
+  if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
+    message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
+  endif()
+  set(LIBC_CPU_FEATURES "${cpu_features}")
+else()
+  # Populates the LIBC_CPU_FEATURES list.
+  # Use -march=native only when the compiler supports it.
+  include(CheckCXXCompilerFlag)
+  CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
+  if(COMPILER_SUPPORTS_MARCH_NATIVE)
+    _check_defined_cpu_feature(LIBC_CPU_FEATURES MARCH native)
+  else()
+    _check_defined_cpu_feature(LIBC_CPU_FEATURES)
+  endif()
 endif()

diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 0d38e06f1d2f1..351e942e9b98b 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -186,15 +186,20 @@ function(add_implementation name impl_name)
   cmake_parse_arguments(
     "ADD_IMPL"
     "" # Optional arguments
-    "" # Single value arguments
-    "REQUIRE;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
+    "MARCH" # Single value arguments
+    "REQUIRE;REJECT;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
     ${ARGN})
+  compute_flags(flags
+    MARCH ${ADD_IMPL_MARCH}
+    REQUIRE ${ADD_IMPL_REQUIRE}
+    REJECT ${ADD_IMPL_REJECT}
+  )
   add_entrypoint_object(${impl_name}
     NAME ${name}
     SRCS ${ADD_IMPL_SRCS}
     HDRS ${ADD_IMPL_HDRS}
     DEPENDS ${ADD_IMPL_DEPENDS}
-    COMPILE_OPTIONS ${ADD_IMPL_COMPILE_OPTIONS}
+    COMPILE_OPTIONS ${ADD_IMPL_COMPILE_OPTIONS} ${flags} -O2
   )
   get_fq_target_name(${impl_name} fq_target_name)
   set_target_properties(${fq_target_name} PROPERTIES REQUIRE_CPU_FEATURES "${ADD_IMPL_REQUIRE}")
@@ -205,6 +210,17 @@ endfunction()
 # memcpy
 # ------------------------------------------------------------------------------
 
+# include the relevant architecture specific implementations
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
+  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/${LIBC_TARGET_ARCHITECTURE}/memcpy.cpp)
+elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
+  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/${LIBC_TARGET_ARCHITECTURE}/memcpy.cpp)
+#Disable tail merging as it leads to lower performance
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm --tail-merge-threshold=0")
+else()
+  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp)
+endif()
+
 function(add_memcpy memcpy_name)
   add_implementation(memcpy ${memcpy_name}
     SRCS ${MEMCPY_SRC}
@@ -219,23 +235,8 @@ function(add_memcpy memcpy_name)
 endfunction()
 
 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
-  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/x86_64/memcpy.cpp)
-  add_memcpy(memcpy_x86_64_opt_sse2   COMPILE_OPTIONS -march=x86-64    REQUIRE SSE2)
-  add_memcpy(memcpy_x86_64_opt_sse4   COMPILE_OPTIONS -march=x86-64-v2 REQUIRE SSE4_2)
-  add_memcpy(memcpy_x86_64_opt_avx2   COMPILE_OPTIONS -march=x86-64-v3 REQUIRE AVX2)
-  add_memcpy(memcpy_x86_64_opt_avx512 COMPILE_OPTIONS -march=x86-64-v4 REQUIRE AVX512F)
-  add_memcpy(memcpy_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
-  add_memcpy(memcpy)
-elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
-  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/aarch64/memcpy.cpp)
-  # Disable tail merging as it leads to lower performance.
-  # Note that '-mllvm' needs to be prefixed with 'SHELL:' to prevent CMake flag deduplication.
-  add_memcpy(memcpy_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
-                                      COMPILE_OPTIONS "SHELL:-mllvm --tail-merge-threshold=0")
-  add_memcpy(memcpy                   COMPILE_OPTIONS "SHELL:-mllvm --tail-merge-threshold=0")
+  add_memcpy(memcpy MARCH native)
 else()
-  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp)
-  add_memcpy(memcpy_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
   add_memcpy(memcpy)
 endif()
 
@@ -257,14 +258,8 @@ function(add_memset memset_name)
 endfunction()
 
 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
-  add_memset(memset_x86_64_opt_sse2   COMPILE_OPTIONS -march=x86-64    REQUIRE SSE2)
-  add_memset(memset_x86_64_opt_sse4   COMPILE_OPTIONS -march=x86-64-v2 REQUIRE SSE4_2)
-  add_memset(memset_x86_64_opt_avx2   COMPILE_OPTIONS -march=x86-64-v3 REQUIRE AVX2)
-  add_memset(memset_x86_64_opt_avx512 COMPILE_OPTIONS -march=x86-64-v4 REQUIRE AVX512F)
-  add_memset(memset_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
-  add_memset(memset)
+  add_memset(memset MARCH native)
 else()
-  add_memset(memset_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
   add_memset(memset)
 endif()
 
@@ -287,13 +282,15 @@ function(add_bzero bzero_name)
 endfunction()
 
 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
-  add_bzero(bzero_x86_64_opt_sse2   COMPILE_OPTIONS -march=x86-64    REQUIRE SSE2)
-  add_bzero(bzero_x86_64_opt_sse4   COMPILE_OPTIONS -march=x86-64-v2 REQUIRE SSE4_2)
-  add_bzero(bzero_x86_64_opt_avx2   COMPILE_OPTIONS -march=x86-64-v3 REQUIRE AVX2)
-  add_bzero(bzero_x86_64_opt_avx512 COMPILE_OPTIONS -march=x86-64-v4 REQUIRE AVX512F)
-  add_bzero(bzero_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
-  add_bzero(bzero)
+  add_bzero(bzero MARCH native)
 else()
-  add_bzero(bzero_opt_host          COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
   add_bzero(bzero)
 endif()
+
+# ------------------------------------------------------------------------------
+# Add all other relevant implementations for the native target.
+# ------------------------------------------------------------------------------
+
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
+  include(${LIBC_TARGET_ARCHITECTURE}/CMakeLists.txt)
+endif()

diff  --git a/libc/src/string/aarch64/CMakeLists.txt b/libc/src/string/aarch64/CMakeLists.txt
new file mode 100644
index 0000000000000..c673f5b3b25fc
--- /dev/null
+++ b/libc/src/string/aarch64/CMakeLists.txt
@@ -0,0 +1 @@
+add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}")

diff  --git a/libc/src/string/x86_64/CMakeLists.txt b/libc/src/string/x86_64/CMakeLists.txt
new file mode 100644
index 0000000000000..9d0dffa1c6370
--- /dev/null
+++ b/libc/src/string/x86_64/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}_opt_none" REJECT "${ALL_CPU_FEATURES}")
+add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}_opt_sse" REQUIRE "SSE" REJECT "SSE2")
+add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}_opt_avx" REQUIRE "AVX" REJECT "AVX2")
+add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}_opt_avx512f" REQUIRE "AVX512F")
+
+add_memset("memset_${LIBC_TARGET_ARCHITECTURE}_opt_none" REJECT "${ALL_CPU_FEATURES}")
+add_memset("memset_${LIBC_TARGET_ARCHITECTURE}_opt_sse" REQUIRE "SSE" REJECT "SSE2")
+add_memset("memset_${LIBC_TARGET_ARCHITECTURE}_opt_avx" REQUIRE "AVX" REJECT "AVX2")
+add_memset("memset_${LIBC_TARGET_ARCHITECTURE}_opt_avx512f" REQUIRE "AVX512F")
+
+add_bzero("bzero_${LIBC_TARGET_ARCHITECTURE}_opt_none" REJECT "${ALL_CPU_FEATURES}")
+add_bzero("bzero_${LIBC_TARGET_ARCHITECTURE}_opt_sse" REQUIRE "SSE" REJECT "SSE2")
+add_bzero("bzero_${LIBC_TARGET_ARCHITECTURE}_opt_avx" REQUIRE "AVX" REJECT "AVX2")
+add_bzero("bzero_${LIBC_TARGET_ARCHITECTURE}_opt_avx512f" REQUIRE "AVX512F")

diff  --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 722d85a631336..511575f9a7f46 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -196,8 +196,6 @@ function(add_libc_multi_impl_test name)
           libc_string_unittests
         DEPENDS
           ${fq_config_name}
-        COMPILE_OPTIONS
-          ${LIBC_COMPILE_OPTIONS_NATIVE}
         ${ARGN}
       )
     else()


        


More information about the libc-commits mailing list