[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
This commit is contained in:
Guillaume Chatelet 2021-05-05 15:52:42 +00:00
parent efc31be7f8
commit 7c2ece523d
17 changed files with 72 additions and 52 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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
)

View File

@ -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
)

View File

@ -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()

View File

@ -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()

View File

@ -1 +1 @@
add_memcpy("memcpy_${LIBC_TARGET_MACHINE}")
add_memcpy("memcpy_${LIBC_TARGET_ARCHITECTURE}")

View File

@ -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")

View File

@ -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")

View File

@ -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(

View File

@ -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()

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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()