[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}) set(LIBC_TARGET_OS ${CMAKE_SYSTEM_NAME})
string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS) 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 # Check --print-resource-dir to find the compiler resource dir if this flag
# is supported by the compiler. # is supported by the compiler.
@ -73,8 +74,8 @@ include(CMakeParseArguments)
include(LLVMLibCRules) include(LLVMLibCRules)
include(LLVMLibCCheckCpuFeatures) 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_ARCHITECTURE}/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}/headers.txt")
set(TARGET_ENTRYPOINT_NAME_LIST "") set(TARGET_ENTRYPOINT_NAME_LIST "")
foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS) 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 # 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) set(ALL_CPU_FEATURES SSE SSE2 AVX AVX2 AVX512F)
list(SORT ALL_CPU_FEATURES) list(SORT ALL_CPU_FEATURES)
endif() endif()

View File

@ -3,9 +3,9 @@ add_gen_header(
DEF_FILE syscall.h.def DEF_FILE syscall.h.def
GEN_HDR syscall.h GEN_HDR syscall.h
PARAMS PARAMS
inline_syscalls=${LIBC_TARGET_MACHINE}/syscall.h.inc inline_syscalls=${LIBC_TARGET_ARCHITECTURE}/syscall.h.inc
DATA_FILES DATA_FILES
${LIBC_TARGET_MACHINE}/syscall.h.inc ${LIBC_TARGET_ARCHITECTURE}/syscall.h.inc
DEPENDS DEPENDS
libc.src.__support.common libc.src.__support.common
) )

View File

@ -56,16 +56,16 @@ function(add_loader_object name)
) )
endfunction() endfunction()
if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE})) if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}))
message(STATUS "Skipping loader for target machine ${LIBC_TARGET_MACHINE}") message(STATUS "Skipping loader for target architecture ${LIBC_TARGET_ARCHITECTURE}")
return() return()
endif() endif()
add_subdirectory(${LIBC_TARGET_MACHINE}) add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
add_loader_object( add_loader_object(
crt1 crt1
ALIAS ALIAS
DEPENDS DEPENDS
.${LIBC_TARGET_MACHINE}.crt1 .${LIBC_TARGET_ARCHITECTURE}.crt1
) )

View File

@ -1,19 +1,19 @@
add_subdirectory(generic) add_subdirectory(generic)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE}) if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
add_subdirectory(${LIBC_TARGET_MACHINE}) add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
endif() endif()
function(add_math_entrypoint_object name) function(add_math_entrypoint_object name)
# We prefer machine specific implementation if available. Hence we check # 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 # that first and retrun early if we are able to add an alias target for the
# machine specific implementation. # 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}) if(TARGET ${fq_machine_specific_target_name})
add_entrypoint_object( add_entrypoint_object(
${name} ${name}
ALIAS ALIAS
DEPENDS DEPENDS
.${LIBC_TARGET_MACHINE}.${name} .${LIBC_TARGET_ARCHITECTURE}.${name}
) )
return() return()
endif() endif()

View File

@ -211,16 +211,13 @@ endfunction()
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# include the relevant architecture specific implementations # include the relevant architecture specific implementations
if(${LIBC_TARGET_MACHINE} STREQUAL "x86_64") if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
set(LIBC_STRING_TARGET_ARCH "x86") set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/${LIBC_TARGET_ARCHITECTURE}/memcpy.cpp)
set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/x86/memcpy.cpp) elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
elseif(${LIBC_TARGET_MACHINE} STREQUAL "aarch64") set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/${LIBC_TARGET_ARCHITECTURE}/memcpy.cpp)
set(LIBC_STRING_TARGET_ARCH "aarch64")
set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/aarch64/memcpy.cpp)
#Disable tail merging as it leads to lower performance #Disable tail merging as it leads to lower performance
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm --tail-merge-threshold=0") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm --tail-merge-threshold=0")
else() else()
set(LIBC_STRING_TARGET_ARCH ${LIBC_TARGET_MACHINE})
set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp) set(MEMCPY_SRC ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp)
endif() endif()
@ -237,7 +234,7 @@ function(add_memcpy memcpy_name)
) )
endfunction() endfunction()
if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86") if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
add_memcpy(memcpy MARCH native) add_memcpy(memcpy MARCH native)
else() else()
add_memcpy(memcpy) add_memcpy(memcpy)
@ -260,7 +257,7 @@ function(add_memset memset_name)
) )
endfunction() endfunction()
if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86") if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
add_memset(memset MARCH native) add_memset(memset MARCH native)
else() else()
add_memset(memset) add_memset(memset)
@ -284,7 +281,7 @@ function(add_bzero bzero_name)
) )
endfunction() endfunction()
if(${LIBC_STRING_TARGET_ARCH} STREQUAL "x86") if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
add_bzero(bzero MARCH native) add_bzero(bzero MARCH native)
else() else()
add_bzero(bzero) add_bzero(bzero)
@ -294,6 +291,6 @@ endif()
# Add all other relevant implementations for the native target. # Add all other relevant implementations for the native target.
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_STRING_TARGET_ARCH}) if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
include(${LIBC_STRING_TARGET_ARCH}/CMakeLists.txt) include(${LIBC_TARGET_ARCHITECTURE}/CMakeLists.txt)
endif() 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 DEF_FILE thread_start_args.h.def
GEN_HDR thread_start_args.h GEN_HDR thread_start_args.h
PARAMS 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 DATA_FILES
${LIBC_TARGET_MACHINE}/thread_start_args.h.in ${LIBC_TARGET_ARCHITECTURE}/thread_start_args.h.in
) )
add_entrypoint_object( add_entrypoint_object(

View File

@ -1,5 +1,5 @@
add_libc_testsuite(libc_linux_tests) add_libc_testsuite(libc_linux_tests)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE}) if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
add_subdirectory(${LIBC_TARGET_MACHINE}) add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
endif() endif()

View File

@ -1,5 +1,5 @@
if(NOT (EXISTS ${LIBC_SOURCE_DIR}/loader/linux/${LIBC_TARGET_MACHINE})) if(NOT (EXISTS ${LIBC_SOURCE_DIR}/loader/linux/${LIBC_TARGET_ARCHITECTURE}))
message("Skipping loader tests for target machine ${LIBC_TARGET_MACHINE}.") message("Skipping loader tests for target architecture ${LIBC_TARGET_ARCHITECTURE}.")
return() return()
endif() endif()

View File

@ -976,7 +976,7 @@ add_fp_unittest(
# from insufficient precision in MPFR calculations leading to # from insufficient precision in MPFR calculations leading to
# https://hal.archives-ouvertes.fr/hal-01091186/document. We will # https://hal.archives-ouvertes.fr/hal-01091186/document. We will
# renable after fixing the precision issue. # renable after fixing the precision issue.
if(${LIBC_TARGET_MACHINE} MATCHES "x86_64") if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
add_fp_unittest( add_fp_unittest(
sqrtl_test sqrtl_test
NEED_MPFR 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( add_libc_unittest(
x86_long_double_test x86_long_double_test
SRCS 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) set(LONG_DOUBLE_HDR LongDoubleBitsX86.h)
else() else()
set(LONG_DOUBLE_HDR) set(LONG_DOUBLE_HDR)
endif() endif()
if(EXISTS ${LIBC_TARGET_MACHINE}) if(EXISTS ${LIBC_TARGET_ARCHITECTURE})
set(FENV_IMPL ${LIBC_TARGET_MACHINE}/FEnv.h) set(FENV_IMPL ${LIBC_TARGET_ARCHITECTURE}/FEnv.h)
else() else()
set(FENV_IMPL DummyFEnv.h) set(FENV_IMPL DummyFEnv.h)
endif() endif()