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

Conversation

nickdesaulniers
Copy link
Member

@nickdesaulniers nickdesaulniers commented Dec 5, 2024

docgen relies on the convention that we have a file foo.cpp in
libc/src/<header>/. Because the above functions weren't in libc/src/strings/
but rather libc/src/string/, docgen could not find that we had implemented
these.

Rather than add special carve outs to docgen, let's fix up our sources for
these 7 functions to stick with the existing conventions the rest of the
codebase follows.

Link: #118860
Fixes: #118875

…p to strings.h

docgen relies on the convention that we have a file foo.cpp in
libc/src/<header>/. Because the above functions weren't in libc/src/strings/
but rather libc/src/string/, docgen could not find that we had implemented
these.

Rather than add special carve outs to docgen, let's fix up our sources for
these 7 functions to stick with the existing conventions the rest of the
codebase follows.

Link: llvm#118860
Fixes: llvm#118875
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

docgen relies on the convention that we have a file foo.cpp in
libc/src/<header>/. Because the above functions weren't in libc/src/strings/
but rather libc/src/string/, docgen could not find that we had implemented
these.

Rather than add special carve outs to docgen, let's fix up our sources for
these 7 functions to stick with the existing conventions the rest of the
codebase follows.

Link: #118860
Fixes: #118875


Patch is 47.51 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/118899.diff

45 Files Affected:

  • (modified) libc/benchmarks/CMakeLists.txt (+3-3)
  • (modified) libc/cmake/modules/LLVMLibCObjectRules.cmake (+38)
  • (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+35-5)
  • (modified) libc/config/baremetal/arm/entrypoints.txt (+9-7)
  • (modified) libc/config/baremetal/riscv/entrypoints.txt (+9-7)
  • (modified) libc/config/darwin/arm/entrypoints.txt (+7-5)
  • (modified) libc/config/darwin/x86_64/entrypoints.txt (+5-3)
  • (modified) libc/config/gpu/entrypoints.txt (+9-7)
  • (modified) libc/config/linux/aarch64/entrypoints.txt (+9-7)
  • (modified) libc/config/linux/arm/entrypoints.txt (+9-7)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+8-7)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+9-7)
  • (modified) libc/config/windows/entrypoints.txt (+7-5)
  • (modified) libc/fuzzing/string/CMakeLists.txt (+1-1)
  • (modified) libc/fuzzing/string/bcmp_fuzz.cpp (+1-1)
  • (modified) libc/hdr/func/free.h (+2-1)
  • (modified) libc/hdr/func/malloc.h (+3-1)
  • (modified) libc/hdr/func/realloc.h (+3-1)
  • (modified) libc/src/CMakeLists.txt (+1)
  • (modified) libc/src/string/CMakeLists.txt (-146)
  • (added) libc/src/strings/CMakeLists.txt (+99)
  • (renamed) libc/src/strings/bcmp.cpp (+1-1)
  • (renamed) libc/src/strings/bcmp.h (+3-3)
  • (renamed) libc/src/strings/bcopy.cpp (+1-1)
  • (renamed) libc/src/strings/bcopy.h (+3-3)
  • (renamed) libc/src/strings/bzero.cpp (+1-1)
  • (renamed) libc/src/strings/bzero.h (+3-3)
  • (renamed) libc/src/strings/index.cpp (+1-1)
  • (renamed) libc/src/strings/index.h (+3-3)
  • (renamed) libc/src/strings/rindex.cpp (+1-1)
  • (renamed) libc/src/strings/rindex.h (+3-3)
  • (renamed) libc/src/strings/strcasecmp.cpp (+1-1)
  • (renamed) libc/src/strings/strcasecmp.h (+3-3)
  • (renamed) libc/src/strings/strncasecmp.cpp (+1-1)
  • (renamed) libc/src/strings/strncasecmp.h (+3-3)
  • (modified) libc/test/src/CMakeLists.txt (+1)
  • (modified) libc/test/src/string/CMakeLists.txt (+4-90)
  • (modified) libc/test/src/string/memory_utils/op_tests.cpp (+1)
  • (renamed) libc/test/src/strings/bcmp_test.cpp (+2-2)
  • (renamed) libc/test/src/strings/bcopy_test.cpp (+3-3)
  • (renamed) libc/test/src/strings/bzero_test.cpp (+2-2)
  • (renamed) libc/test/src/strings/index_test.cpp ()
  • (renamed) libc/test/src/strings/rindex_test.cpp ()
  • (renamed) libc/test/src/strings/strcasecmp_test.cpp (+1-1)
  • (renamed) libc/test/src/strings/strncasecmp_test.cpp (+1-1)
diff --git a/libc/benchmarks/CMakeLists.txt b/libc/benchmarks/CMakeLists.txt
index 52e3f942d16ea3..6c011ef3015bcd 100644
--- a/libc/benchmarks/CMakeLists.txt
+++ b/libc/benchmarks/CMakeLists.txt
@@ -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)
diff --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake
index 68b5ed1ed51c04..142778d9ea1cc5 100644
--- a/libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -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()
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 36f871920c3c33..84f3125d557eae 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -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)
@@ -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)
@@ -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()
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 9027717acb4dae..71b49d98942916 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -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
+
     # inttypes.h entrypoints
     libc.src.inttypes.imaxabs
     libc.src.inttypes.imaxdiv
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index afae2b3e082be3..e84d139d09dd8e 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index 2d5dbeff485747..7972d285f963a4 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 64eeed18f3819e..19230cde471989 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -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
@@ -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
diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 38e9f2e685caed..e3ebd3ca472560 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index effa5b12d87ac4..6b46f925cec288 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 31d81de06fb6b0..7c83e09792f3b3 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -21,10 +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.index
     libc.src.string.memccpy
     libc.src.string.memchr
     libc.src.string.memcmp
@@ -34,10 +30,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
@@ -48,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
@@ -61,6 +54,15 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strtok
     libc.src.string.strtok_r
 
+    # 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
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5a48baf104159f..bd035c71f91646 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -93,6 +86,14 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strtok_r
     libc.src.string.strxfrm
 
+    # strings.h entrypoints
+    libc.src.string.index
+    libc.src.string.rindex
+    libc.src.strings.bcmp
+    libc.src.strings.bcopy
+    libc.src.strings.bzero
+    libc.src.strings.strcasecmp
+
     # inttypes.h entrypoints
     libc.src.inttypes.imaxabs
     libc.src.inttypes.imaxdiv
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 1bedc25a9d0291..aa2034bad23aca 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 8f0b50bcc83ea2..36dc08ce2cbc82 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -18,9 +18,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.toupper
 
     # 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
@@ -32,7 +29,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
@@ -43,7 +39,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
@@ -59,6 +54,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
diff --git a/libc/fuzzing/string/CMakeLists.txt b/libc/fuzzing/string/CMakeLists.txt
index 9dd4fceee3b596..efda80b59c951f 100644
--- a/libc/fuzzing/string/CMakeLists.txt
+++ b/libc/fuzzing/string/CMakeLists.txt
@@ -38,5 +38,5 @@ add_libc_fuzzer(
   SRCS
     bcmp_fuzz.cpp
   DEPENDS
-    libc.src.string.bcmp
+    libc.src.strings.bcm...
[truncated]

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.

@@ -246,6 +246,7 @@ int CmpBlockAdaptor(cpp::span<char> p1, cpp::span<char> p2, size_t size) {
return (int)FnImpl(as_byte(p1), as_byte(p2));
}

// TODO: should testing of bcmp be moved to libc/test/src/strings/ dir?
Copy link
Member Author

Choose a reason for hiding this comment

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

@gchatelet thoughts on this question?

Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense for unit tests and fuzz tests to move to a strings folder but op_tests is about testing the low level constructs and I think it's fine to keep them here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

This looks like a safe NFC to me, but I'd definitely prefer to wait for Guillame to approve.

Copy link
Contributor

@gchatelet gchatelet left a comment

Choose a reason for hiding this comment

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

I missed the direct mention, please make sure to ping me directly if you need a review from me.

libc.src.strings.index
libc.src.strings.rindex
libc.src.strings.strcasecmp
libc.src.strings.strncasecmp
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.

SRCS ${LIBC_SOURCE_DIR}/src/strings/bcmp.cpp
HDRS ${LIBC_SOURCE_DIR}/src/strings/bcmp.h
DEPENDS
libc.include.string
Copy link
Contributor

Choose a reason for hiding this comment

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

should we also add libc.include.strings?
Here and below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should I just remove having a dependency on our includes? Other functions like bcopy, index, etc, below don't have such dependencies. bcmp and bzero don't explicitly include anything from libc/include/.

30655b4

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure let's remove them then.

@@ -246,6 +246,7 @@ int CmpBlockAdaptor(cpp::span<char> p1, cpp::span<char> p2, size_t size) {
return (int)FnImpl(as_byte(p1), as_byte(p2));
}

// TODO: should testing of bcmp be moved to libc/test/src/strings/ dir?
Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense for unit tests and fuzz tests to move to a strings folder but op_tests is about testing the low level constructs and I think it's fine to keep them here.

@nickdesaulniers nickdesaulniers merged commit 431ea2d into llvm:main Dec 10, 2024
7 checks passed
@nickdesaulniers nickdesaulniers deleted the strings branch December 10, 2024 16:58
@nickdesaulniers
Copy link
Member Author

Looks like this broke the riscv32 buildbot. Will fix forward.

nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Dec 10, 2024
I missed riscv, and the newly added baremetal aarch64 entrypoints had a mid air
collision.

Link: llvm#118691
Link: llvm#118899
nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Dec 10, 2024
…b.h, and inttypes.h

We should add support to docgen for mentioning glibc extensions (mempcpy) or
extensions from other libcs.

Link: llvm#118875
Link: llvm#118899
nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Dec 10, 2024
nickdesaulniers added a commit that referenced this pull request Dec 10, 2024
I typo'd riscv, and the newly added baremetal aarch64 entrypoints had a mid air
collision.

Link: #118691
Link: #118899
nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Dec 10, 2024
nickdesaulniers added a commit that referenced this pull request Dec 10, 2024
Due to moving strings out of string.

Link: #118899
Caslyn added a commit that referenced this pull request Dec 11, 2024
Since `index_test.cpp` and `rindex_test.cpp` now reside in /strings,
adjust some header paths accordingly.

Link: #118899
bherrera pushed a commit to misttech/fuchsia that referenced this pull request Dec 19, 2024
llvm/llvm-project#118899 began the process of
moving files from /string to /strings. This CL likewise introduces the
/strings directory to Fuchsia's build machinery and performs the first
step in migrating the needed targets to /strings.

Since it's expected that we'll need to repeat this process as more
targets are migrated to strings/ in the future, this CL introduces a
(temporary) exec_script that can be used as an example for future CL
migrations, to make it easier and faster to detect file changes (but
note the exec_script gets cleaned up after each migration, as is done
in Ia8de544a2695125f888544071b08f37906fe381d).

Bug: 383347626
Change-Id: I81b66e1f5ec01f240ed8cf1f601764eb89ed6cff
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1172248
Reviewed-by: David Fang <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Fuchsia-Auto-Submit: Caslyn Tonelli <[email protected]>
bherrera pushed a commit to misttech/integration that referenced this pull request Dec 21, 2024
llvm/llvm-project#118899 began the process of
moving files from /string to /strings. This CL likewise introduces the
/strings directory to Fuchsia's build machinery and performs the first
step in migrating the needed targets to /strings.

Since it's expected that we'll need to repeat this process as more
targets are migrated to strings/ in the future, this CL introduces a
(temporary) exec_script that can be used as an example for future CL
migrations, to make it easier and faster to detect file changes (but
note the exec_script gets cleaned up after each migration, as is done
in Ia8de544a2695125f888544071b08f37906fe381d).

Original-Bug: 383347626
Original-Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1172248
Original-Revision: 575cd6a3d6a2829f5c91f5a8d1cf199d42ea8808
GitOrigin-RevId: a2a59a5095415d8eacdfb6a1a8c4268102471e4d
Change-Id: Ie7d1fbc863db617cac3d7119994b0f9ba7103545
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc] split strings.h from string.h
4 participants