Skip to content

[libc] move bcmp, bzero, bcopy, index, rindex, strcasecmp, strncasecmp to strings.h #118899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libc/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ target_link_libraries(libc.benchmarks.memory_functions.opt_host
PRIVATE
libc-memory-benchmark
libc.src.string.memcmp_opt_host.__internal__
libc.src.string.bcmp_opt_host.__internal__
libc.src.string.memcpy_opt_host.__internal__
libc.src.string.memset_opt_host.__internal__
libc.src.string.bzero_opt_host.__internal__
libc.src.string.memmove_opt_host.__internal__
libc.src.string.memset_opt_host.__internal__
libc.src.strings.bcmp_opt_host.__internal__
libc.src.strings.bzero_opt_host.__internal__
benchmark_main
)
llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
Expand Down
38 changes: 38 additions & 0 deletions libc/cmake/modules/LLVMLibCObjectRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,41 @@ function(add_redirector_object target_name)
BEFORE PRIVATE -fPIC ${LIBC_COMPILE_OPTIONS_DEFAULT}
)
endfunction(add_redirector_object)

# Helper to define a function with multiple implementations
# - Computes flags to satisfy required/rejected features and arch,
# - Declares an entry point,
# - Attach the REQUIRE_CPU_FEATURES property to the target,
# - Add the fully qualified target to `${name}_implementations` global property for tests.
function(add_implementation name impl_name)
cmake_parse_arguments(
"ADD_IMPL"
"" # Optional arguments
"" # Single value arguments
"REQUIRE;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;MLLVM_COMPILE_OPTIONS" # Multi value arguments
${ARGN})

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# Note that '-mllvm' needs to be prefixed with 'SHELL:' to prevent CMake flag deduplication.
foreach(opt IN LISTS ADD_IMPL_MLLVM_COMPILE_OPTIONS)
list(APPEND ADD_IMPL_COMPILE_OPTIONS "SHELL:-mllvm ${opt}")
endforeach()
endif()

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
# Prevent warning when passing x86 SIMD types as template arguments.
# e.g. "warning: ignoring attributes on template argument ‘__m128i’ [-Wignored-attributes]"
list(APPEND ADD_IMPL_COMPILE_OPTIONS "-Wno-ignored-attributes")
endif()

add_entrypoint_object(${impl_name}
NAME ${name}
SRCS ${ADD_IMPL_SRCS}
HDRS ${ADD_IMPL_HDRS}
DEPENDS ${ADD_IMPL_DEPENDS}
COMPILE_OPTIONS ${libc_opt_high_flag} ${ADD_IMPL_COMPILE_OPTIONS}
)
get_fq_target_name(${impl_name} fq_target_name)
set_target_properties(${fq_target_name} PROPERTIES REQUIRE_CPU_FEATURES "${ADD_IMPL_REQUIRE}")
set_property(GLOBAL APPEND PROPERTY "${name}_implementations" "${fq_target_name}")
endfunction()
40 changes: 35 additions & 5 deletions libc/cmake/modules/LLVMLibCTestRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ function(add_integration_test test_name)
libc.test.IntegrationTest.test
# We always add the memory functions objects. This is because the
# compiler's codegen can emit calls to the C memory functions.
libc.src.string.bcmp
libc.src.string.bzero
libc.src.string.memcmp
libc.src.string.memcpy
libc.src.string.memmove
libc.src.string.memset
libc.src.strings.bcmp
libc.src.strings.bzero
)

if(libc.src.compiler.__stack_chk_fail IN_LIST TARGET_LLVMLIBC_ENTRYPOINTS)
Expand Down Expand Up @@ -583,13 +583,13 @@ function(add_libc_hermetic test_name)
libc.startup.${LIBC_TARGET_OS}.crt1
# We always add the memory functions objects. This is because the
# compiler's codegen can emit calls to the C memory functions.
libc.src.string.bcmp
libc.src.string.bzero
libc.src.__support.StringUtil.error_to_string
libc.src.string.memcmp
libc.src.string.memcpy
libc.src.string.memmove
libc.src.string.memset
libc.src.__support.StringUtil.error_to_string
libc.src.strings.bcmp
libc.src.strings.bzero
)

if(libc.src.compiler.__stack_chk_fail IN_LIST TARGET_LLVMLIBC_ENTRYPOINTS)
Expand Down Expand Up @@ -766,3 +766,33 @@ function(add_libc_test test_name)
endif()
endif()
endfunction(add_libc_test)

# Tests all implementations that can run on the target CPU.
function(add_libc_multi_impl_test name suite)
get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
foreach(fq_config_name IN LISTS fq_implementations)
get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
cpu_supports(can_run "${required_cpu_features}")
if(can_run)
string(FIND ${fq_config_name} "." last_dot_loc REVERSE)
math(EXPR name_loc "${last_dot_loc} + 1")
string(SUBSTRING ${fq_config_name} ${name_loc} -1 target_name)
add_libc_test(
${target_name}_test
SUITE
${suite}
COMPILE_OPTIONS
${LIBC_COMPILE_OPTIONS_NATIVE}
LINK_LIBRARIES
LibcMemoryHelpers
${ARGN}
DEPENDS
${fq_config_name}
libc.src.__support.macros.sanitizer
)
get_fq_target_name(${fq_config_name}_test fq_target_name)
else()
message(STATUS "Skipping test for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
endif()
endforeach()
endfunction()
16 changes: 9 additions & 7 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.setjmp.setjmp

# string.h entrypoints
libc.src.string.bcmp
libc.src.string.bcopy
libc.src.string.bzero
libc.src.string.index
libc.src.string.memccpy
libc.src.string.memchr
libc.src.string.memcmp
Expand All @@ -45,10 +41,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.memrchr
libc.src.string.memset
libc.src.string.memset_explicit
libc.src.string.rindex
libc.src.string.stpcpy
libc.src.string.stpncpy
libc.src.string.strcasecmp
libc.src.string.strcasestr
libc.src.string.strcat
libc.src.string.strchr
Expand All @@ -62,7 +56,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strlcat
libc.src.string.strlcpy
libc.src.string.strlen
libc.src.string.strncasecmp
libc.src.string.strncat
libc.src.string.strncmp
libc.src.string.strncpy
Expand All @@ -76,6 +69,15 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strtok_r
libc.src.string.strxfrm

# strings.h entrypoints
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
libc.src.strings.strncasecmp
Copy link
Member Author

@nickdesaulniers nickdesaulniers Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrhosek so if your goal is to avoid POSIX in baremetal, then we can make llvm-libc slightly smaller for baremetal by excluding bcopy, index, rindex, strcasecmp, and strncasecmp which are POSIX and not ISO C. LLVM may emit libcalls to bcmp (IME) and bzero (I suspect) (unsure about bcopy, @gchatelet may know; otherwise I know where to look in llvm but am lazy).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clang needs to know that these functions are present. And they are when using glibc or musl libc in the target triple.


# inttypes.h entrypoints
libc.src.inttypes.imaxabs
libc.src.inttypes.imaxdiv
Expand Down
16 changes: 9 additions & 7 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.errno.errno

# string.h entrypoints
libc.src.string.bcmp
libc.src.string.bcopy
libc.src.string.bzero
libc.src.string.index
libc.src.string.memccpy
libc.src.string.memchr
libc.src.string.memcmp
Expand All @@ -41,10 +37,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.memrchr
libc.src.string.memset
libc.src.string.memset_explicit
libc.src.string.rindex
libc.src.string.stpcpy
libc.src.string.stpncpy
libc.src.string.strcasecmp
libc.src.string.strcasestr
libc.src.string.strcat
libc.src.string.strchr
Expand All @@ -58,7 +52,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strlcat
libc.src.string.strlcpy
libc.src.string.strlen
libc.src.string.strncasecmp
libc.src.string.strncat
libc.src.string.strncmp
libc.src.string.strncpy
Expand All @@ -72,6 +65,15 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strtok_r
libc.src.string.strxfrm

# strings.h entrypoints
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
libc.src.strings.strncasecmp

# inttypes.h entrypoints
libc.src.inttypes.imaxabs
libc.src.inttypes.imaxdiv
Expand Down
12 changes: 7 additions & 5 deletions libc/config/darwin/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.errno.errno

# string.h entrypoints
libc.src.string.bcmp
libc.src.string.bcopy
libc.src.string.bzero
libc.src.string.memccpy
libc.src.string.memchr
libc.src.string.memcmp
Expand All @@ -35,7 +32,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.memset
libc.src.string.stpcpy
libc.src.string.stpncpy
libc.src.string.strcasecmp
libc.src.string.strcasestr
libc.src.string.strcat
libc.src.string.strchr
Expand All @@ -46,7 +42,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strlcat
libc.src.string.strlcpy
libc.src.string.strlen
libc.src.string.strncasecmp
libc.src.string.strncat
libc.src.string.strncmp
libc.src.string.strncpy
Expand All @@ -62,6 +57,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strdup
libc.src.string.strndup

# strings.h entrypoints
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.strcasecmp
libc.src.strings.strncasecmp

# inttypes.h entrypoints
libc.src.inttypes.imaxabs
libc.src.inttypes.imaxdiv
Expand Down
8 changes: 5 additions & 3 deletions libc/config/darwin/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.toupper

# search.h entrypoints
libc.src.search.lfind
libc.src.search.lfind

# string.h entrypoints
libc.src.string.bcmp
libc.src.string.bzero
libc.src.string.memccpy
libc.src.string.memchr
libc.src.string.memcmp
Expand Down Expand Up @@ -58,6 +56,10 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strdup
libc.src.string.strndup

# strings.h entrypoints
libc.src.strings.bcmp
libc.src.strings.bzero

# inttypes.h entrypoints
libc.src.inttypes.imaxabs
libc.src.inttypes.imaxdiv
Expand Down
16 changes: 9 additions & 7 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.toupper_l

# string.h entrypoints
libc.src.string.bcmp
libc.src.string.bcopy
libc.src.string.bzero
libc.src.string.index
libc.src.string.memccpy
libc.src.string.memchr
libc.src.string.memcmp
Expand All @@ -48,10 +44,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.mempcpy
libc.src.string.memrchr
libc.src.string.memset
libc.src.string.rindex
libc.src.string.stpcpy
libc.src.string.stpncpy
libc.src.string.strcasecmp
libc.src.string.strcasestr
libc.src.string.strcat
libc.src.string.strchr
Expand All @@ -66,7 +60,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strlcat
libc.src.string.strlcpy
libc.src.string.strlen
libc.src.string.strncasecmp
libc.src.string.strncat
libc.src.string.strncmp
libc.src.string.strncpy
Expand All @@ -82,6 +75,15 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strxfrm
libc.src.string.strxfrm_l

# strings.h entrypoints
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
libc.src.strings.strncasecmp

# stdbit.h entrypoints
libc.src.stdbit.stdc_bit_ceil_uc
libc.src.stdbit.stdc_bit_ceil_ui
Expand Down
16 changes: 9 additions & 7 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sched.sched_yield

# string.h entrypoints
libc.src.string.bcmp
libc.src.string.bcopy
libc.src.string.bzero
libc.src.string.index
libc.src.string.memccpy
libc.src.string.memchr
libc.src.string.memcmp
Expand All @@ -59,10 +55,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.memrchr
libc.src.string.memset
libc.src.string.memset_explicit
libc.src.string.rindex
libc.src.string.stpcpy
libc.src.string.stpncpy
libc.src.string.strcasecmp
libc.src.string.strcasestr
libc.src.string.strcat
libc.src.string.strchr
Expand All @@ -77,7 +71,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strlcat
libc.src.string.strlcpy
libc.src.string.strlen
libc.src.string.strncasecmp
libc.src.string.strncat
libc.src.string.strncmp
libc.src.string.strncpy
Expand All @@ -93,6 +86,15 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strtok_r
libc.src.string.strxfrm

# strings.h entrypoints
libc.src.strings.bcmp
libc.src.strings.bcopy
libc.src.strings.bzero
libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
libc.src.strings.strncasecmp

# inttypes.h entrypoints
libc.src.inttypes.imaxabs
libc.src.inttypes.imaxdiv
Expand Down
Loading
Loading