[libc-commits] [libc] 7c2ece5 - [libc] Normalize LIBC_TARGET_MACHINE

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Wed May 5 08:52:54 PDT 2021


Author: Guillaume Chatelet
Date: 2021-05-05T15:52:42Z
New Revision: 7c2ece523d7ff74f3eeabce1b9685f3eaae8cff4

URL: https://github.com/llvm/llvm-project/commit/7c2ece523d7ff74f3eeabce1b9685f3eaae8cff4
DIFF: https://github.com/llvm/llvm-project/commit/7c2ece523d7ff74f3eeabce1b9685f3eaae8cff4.diff

LOG: [libc] Normalize LIBC_TARGET_MACHINE

Current implementation defines LIBC_TARGET_MACHINE with the use of CMAKE_SYSTEM_PROCESSOR.
Unfortunately CMAKE_SYSTEM_PROCESSOR is OS dependent and can produce different results.
An evidence of this is the various matchers used to detect whether the architecture is x86.

This patch normalizes LIBC_TARGET_MACHINE and renames it LIBC_TARGET_ARCHITECTURE.
I've added many architectures but we may want to limit ourselves to x86 and ARM.

Differential Revision: https://reviews.llvm.org/D101524

Added: 
    libc/cmake/modules/LLVMLibCArchitectures.cmake
    libc/src/string/x86_64/CMakeLists.txt
    libc/src/string/x86_64/memcpy.cpp

Modified: 
    libc/CMakeLists.txt
    libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
    libc/config/linux/CMakeLists.txt
    libc/loader/linux/CMakeLists.txt
    libc/src/math/CMakeLists.txt
    libc/src/string/CMakeLists.txt
    libc/src/string/aarch64/CMakeLists.txt
    libc/src/threads/linux/CMakeLists.txt
    libc/test/config/linux/CMakeLists.txt
    libc/test/loader/linux/CMakeLists.txt
    libc/test/src/math/CMakeLists.txt
    libc/test/utils/FPUtil/CMakeLists.txt
    libc/utils/FPUtil/CMakeLists.txt

Removed: 
    libc/src/string/x86/CMakeLists.txt
    libc/src/string/x86/memcpy.cpp


################################################################################
diff  --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index ff1a0c6fe69a2..15a0b35bac655 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -19,7 +19,8 @@ set(LIBC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Define libc destinat
 set(LIBC_TARGET_OS ${CMAKE_SYSTEM_NAME})
 string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
 
-set(LIBC_TARGET_MACHINE ${CMAKE_SYSTEM_PROCESSOR})
+# Defines LIBC_TARGET_ARCHITECTURE and associated macros.
+include(LLVMLibCArchitectures)
 
 # Check --print-resource-dir to find the compiler resource dir if this flag
 # is supported by the compiler.
@@ -73,8 +74,8 @@ include(CMakeParseArguments)
 include(LLVMLibCRules)
 include(LLVMLibCCheckCpuFeatures)
 
-include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_MACHINE}/entrypoints.txt")
-include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_MACHINE}/headers.txt")
+include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
+include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
 
 set(TARGET_ENTRYPOINT_NAME_LIST "")
 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)

diff  --git a/libc/cmake/modules/LLVMLibCArchitectures.cmake b/libc/cmake/modules/LLVMLibCArchitectures.cmake
new file mode 100644
index 0000000000000..8d49054ed048b
--- /dev/null
+++ b/libc/cmake/modules/LLVMLibCArchitectures.cmake
@@ -0,0 +1,22 @@
+# ------------------------------------------------------------------------------
+# Architecture definitions
+# ------------------------------------------------------------------------------
+
+if(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
+  set(LIBC_TARGET_ARCHITECTURE_IS_MIPS TRUE)
+  set(LIBC_TARGET_ARCHITECTURE "mips")
+elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
+  set(LIBC_TARGET_ARCHITECTURE_IS_ARM TRUE)
+  set(LIBC_TARGET_ARCHITECTURE "arm")
+elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
+  set(LIBC_TARGET_ARCHITECTURE_IS_AARCH64 TRUE)
+  set(LIBC_TARGET_ARCHITECTURE "aarch64")
+elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
+  set(LIBC_TARGET_ARCHITECTURE_IS_X86 TRUE)
+  set(LIBC_TARGET_ARCHITECTURE "x86_64")
+elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
+  set(LIBC_TARGET_ARCHITECTURE_IS_POWER TRUE)
+  set(LIBC_TARGET_ARCHITECTURE "power")
+else()
+  message(FATAL_ERROR "Unsupported processor ${CMAKE_SYSTEM_PROCESSOR}")
+endif()

diff  --git a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
index 1b92b481d5406..a44186ea32cbe 100644
--- a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
+++ b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -2,7 +2,7 @@
 # Cpu features definition and flags
 # ------------------------------------------------------------------------------
 
-if(${LIBC_TARGET_MACHINE} MATCHES "x86|x86_64")
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   set(ALL_CPU_FEATURES SSE SSE2 AVX AVX2 AVX512F)
   list(SORT ALL_CPU_FEATURES)
 endif()

diff  --git a/libc/config/linux/CMakeLists.txt b/libc/config/linux/CMakeLists.txt
index 50e98379cf532..e3a00189a2ad8 100644
--- a/libc/config/linux/CMakeLists.txt
+++ b/libc/config/linux/CMakeLists.txt
@@ -3,9 +3,9 @@ add_gen_header(
   DEF_FILE syscall.h.def
   GEN_HDR syscall.h
   PARAMS
-    inline_syscalls=${LIBC_TARGET_MACHINE}/syscall.h.inc
+    inline_syscalls=${LIBC_TARGET_ARCHITECTURE}/syscall.h.inc
   DATA_FILES
-    ${LIBC_TARGET_MACHINE}/syscall.h.inc
+    ${LIBC_TARGET_ARCHITECTURE}/syscall.h.inc
   DEPENDS
     libc.src.__support.common
 )

diff  --git a/libc/loader/linux/CMakeLists.txt b/libc/loader/linux/CMakeLists.txt
index 5cb184a56aac5..4b8b0d9e98fa3 100644
--- a/libc/loader/linux/CMakeLists.txt
+++ b/libc/loader/linux/CMakeLists.txt
@@ -56,16 +56,16 @@ function(add_loader_object name)
   )
 endfunction()
 
-if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE}))
-  message(STATUS "Skipping loader for target machine ${LIBC_TARGET_MACHINE}")
+if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}))
+  message(STATUS "Skipping loader for target architecture ${LIBC_TARGET_ARCHITECTURE}")
   return()
 endif()
 
-add_subdirectory(${LIBC_TARGET_MACHINE})
+add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
 
 add_loader_object(
   crt1
   ALIAS
   DEPENDS
-    .${LIBC_TARGET_MACHINE}.crt1
+    .${LIBC_TARGET_ARCHITECTURE}.crt1
 )

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 1774139141f87..50fc4a4325e9f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -1,19 +1,19 @@
 add_subdirectory(generic)
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE})
-  add_subdirectory(${LIBC_TARGET_MACHINE})
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
+  add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
 endif()
 
 function(add_math_entrypoint_object name)
   # We prefer machine specific implementation if available. Hence we check
   # that first and retrun early if we are able to add an alias target for the
   # machine specific implementation.
-  get_fq_target_name("${LIBC_TARGET_MACHINE}.${name}" fq_machine_specific_target_name)
+  get_fq_target_name("${LIBC_TARGET_ARCHITECTURE}.${name}" fq_machine_specific_target_name)
   if(TARGET ${fq_machine_specific_target_name})
     add_entrypoint_object(
       ${name}
       ALIAS
       DEPENDS
-        .${LIBC_TARGET_MACHINE}.${name}
+        .${LIBC_TARGET_ARCHITECTURE}.${name}
     )
     return()
   endif()

diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index c31525ad62088..351e942e9b98b 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -211,16 +211,13 @@ endfunction()
 # ------------------------------------------------------------------------------
 
 # include the relevant architecture specific implementations
-if(${LIBC_TARGET_MACHINE} STREQUAL "x86_64")
-  set(LIBC_STRING_TARGET_ARCH "x86")
-  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/x86/memcpy.cpp)
-elseif(${LIBC_TARGET_MACHINE} STREQUAL "aarch64")
-  set(LIBC_STRING_TARGET_ARCH "aarch64")
-  set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/aarch64/memcpy.cpp)
+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(LIBC_STRING_TARGET_ARCH ${LIBC_TARGET_MACHINE})
   set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp)
 endif()
 
@@ -237,7 +234,7 @@ function(add_memcpy memcpy_name)
   )
 endfunction()
 
-if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86")
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   add_memcpy(memcpy MARCH native)
 else()
   add_memcpy(memcpy)
@@ -260,7 +257,7 @@ function(add_memset memset_name)
   )
 endfunction()
 
-if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86")
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   add_memset(memset MARCH native)
 else()
   add_memset(memset)
@@ -284,7 +281,7 @@ function(add_bzero bzero_name)
   )
 endfunction()
 
-if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86")
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   add_bzero(bzero MARCH native)
 else()
   add_bzero(bzero)
@@ -294,6 +291,6 @@ endif()
 # Add all other relevant implementations for the native target.
 # ------------------------------------------------------------------------------
 
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_STRING_TARGET_ARCH})
-  include(${LIBC_STRING_TARGET_ARCH}/CMakeLists.txt)
+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
index 8430891d8e9b8..c673f5b3b25fc 100644
--- a/libc/src/string/aarch64/CMakeLists.txt
+++ b/libc/src/string/aarch64/CMakeLists.txt
@@ -1 +1 @@
-add_memcpy("memcpy_${LIBC_TARGET_MACHINE}")
+add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}")

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

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/src/string/x86/memcpy.cpp b/libc/src/string/x86_64/memcpy.cpp
similarity index 100%
rename from libc/src/string/x86/memcpy.cpp
rename to libc/src/string/x86_64/memcpy.cpp

diff  --git a/libc/src/threads/linux/CMakeLists.txt b/libc/src/threads/linux/CMakeLists.txt
index 08a04c604a36a..767bf34dc36b3 100644
--- a/libc/src/threads/linux/CMakeLists.txt
+++ b/libc/src/threads/linux/CMakeLists.txt
@@ -3,9 +3,9 @@ add_gen_header(
   DEF_FILE thread_start_args.h.def
   GEN_HDR thread_start_args.h
   PARAMS
-    thread_start_args=${LIBC_TARGET_MACHINE}/thread_start_args.h.in
+    thread_start_args=${LIBC_TARGET_ARCHITECTURE}/thread_start_args.h.in
   DATA_FILES
-    ${LIBC_TARGET_MACHINE}/thread_start_args.h.in
+    ${LIBC_TARGET_ARCHITECTURE}/thread_start_args.h.in
 )
 
 add_entrypoint_object(

diff  --git a/libc/test/config/linux/CMakeLists.txt b/libc/test/config/linux/CMakeLists.txt
index fea3d0b2dcbe9..b449a33ec94c1 100644
--- a/libc/test/config/linux/CMakeLists.txt
+++ b/libc/test/config/linux/CMakeLists.txt
@@ -1,5 +1,5 @@
 add_libc_testsuite(libc_linux_tests)
 
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE})
-  add_subdirectory(${LIBC_TARGET_MACHINE})
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
+  add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
 endif()

diff  --git a/libc/test/loader/linux/CMakeLists.txt b/libc/test/loader/linux/CMakeLists.txt
index 15d67a128194b..4284ea435f1ec 100644
--- a/libc/test/loader/linux/CMakeLists.txt
+++ b/libc/test/loader/linux/CMakeLists.txt
@@ -1,5 +1,5 @@
-if(NOT (EXISTS ${LIBC_SOURCE_DIR}/loader/linux/${LIBC_TARGET_MACHINE}))
-  message("Skipping loader tests for target machine ${LIBC_TARGET_MACHINE}.")
+if(NOT (EXISTS ${LIBC_SOURCE_DIR}/loader/linux/${LIBC_TARGET_ARCHITECTURE}))
+  message("Skipping loader tests for target architecture ${LIBC_TARGET_ARCHITECTURE}.")
   return()
 endif()
 

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index be7518f53a5a8..22ff6d612c9ed 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -976,7 +976,7 @@ add_fp_unittest(
 # from insufficient precision in MPFR calculations leading to
 # https://hal.archives-ouvertes.fr/hal-01091186/document. We will
 # renable after fixing the precision issue.
-if(${LIBC_TARGET_MACHINE} MATCHES "x86_64")
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   add_fp_unittest(
     sqrtl_test
     NEED_MPFR

diff  --git a/libc/test/utils/FPUtil/CMakeLists.txt b/libc/test/utils/FPUtil/CMakeLists.txt
index 65cb677e22d3c..c5315d3b66bae 100644
--- a/libc/test/utils/FPUtil/CMakeLists.txt
+++ b/libc/test/utils/FPUtil/CMakeLists.txt
@@ -1,4 +1,4 @@
-if((${LIBC_TARGET_OS} STREQUAL "linux") AND (${LIBC_TARGET_MACHINE} MATCHES "i386|x86_64"))
+if((${LIBC_TARGET_OS} STREQUAL "linux") AND (${LIBC_TARGET_ARCHITECTURE_IS_X86}))
   add_libc_unittest(
     x86_long_double_test
     SRCS

diff  --git a/libc/utils/FPUtil/CMakeLists.txt b/libc/utils/FPUtil/CMakeLists.txt
index 43ee6433559e6..8156464a04398 100644
--- a/libc/utils/FPUtil/CMakeLists.txt
+++ b/libc/utils/FPUtil/CMakeLists.txt
@@ -1,11 +1,11 @@
-if(${LIBC_TARGET_MACHINE} MATCHES "^x86.*")
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
   set(LONG_DOUBLE_HDR LongDoubleBitsX86.h)
 else()
   set(LONG_DOUBLE_HDR)
 endif()
 
-if(EXISTS ${LIBC_TARGET_MACHINE})
-  set(FENV_IMPL ${LIBC_TARGET_MACHINE}/FEnv.h)
+if(EXISTS ${LIBC_TARGET_ARCHITECTURE})
+  set(FENV_IMPL ${LIBC_TARGET_ARCHITECTURE}/FEnv.h)
 else()
   set(FENV_IMPL DummyFEnv.h)
 endif()


        


More information about the libc-commits mailing list