1 add_subdirectory(memory_utils)
3 if(LIBC_CONF_STRING_UNSAFE_WIDE_READ)
4 list(APPEND string_config_options "-DLIBC_COPT_STRING_UNSAFE_WIDE_READ")
6 if(LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING)
7 list(APPEND string_config_options "-DLIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING")
9 if(string_config_options)
10 list(PREPEND string_config_options "COMPILE_OPTIONS")
18 .memory_utils.inline_bzero
19 .memory_utils.inline_memcpy
21 libc.src.__support.common
22 libc.src.__support.CPP.bitset
23 ${string_config_options}
27 allocating_string_utils
29 allocating_string_utils.h
31 .memory_utils.inline_memcpy
33 libc.src.__support.CPP.optional
34 libc.src.__support.macros.config
37 add_entrypoint_object(
45 add_entrypoint_object(
55 add_entrypoint_object(
63 add_entrypoint_object(
70 .memory_utils.inline_memcpy
73 add_entrypoint_object(
80 .memory_utils.inline_memmem
83 add_entrypoint_object(
93 add_entrypoint_object(
101 add_entrypoint_object(
111 add_entrypoint_object(
122 add_entrypoint_object(
129 .memory_utils.inline_bzero
132 add_entrypoint_object(
141 libc.include.llvm-libc-types.size_t
144 add_entrypoint_object(
154 add_entrypoint_object(
164 add_entrypoint_object(
171 .memory_utils.inline_strcmp
174 add_entrypoint_object(
181 .memory_utils.inline_strcmp
182 libc.src.__support.ctype_utils
185 add_entrypoint_object(
192 .memory_utils.inline_strstr
193 libc.src.__support.ctype_utils
196 add_entrypoint_object(
204 add_entrypoint_object(
212 add_entrypoint_object(
219 .memory_utils.inline_memcpy
223 add_entrypoint_object(
233 add_entrypoint_object(
240 .memory_utils.inline_memcpy
242 libc.hdr.stdlib_macros
244 libc.include.llvm-libc-types.size_t
247 add_entrypoint_object(
254 libc.src.__support.StringUtil.error_to_string
257 add_entrypoint_object(
264 libc.src.__support.StringUtil.error_to_string
267 add_entrypoint_object(
275 libc.include.llvm-libc-types.size_t
278 add_entrypoint_object(
286 libc.include.llvm-libc-types.size_t
289 add_entrypoint_object(
297 libc.include.llvm-libc-types.size_t
300 add_entrypoint_object(
309 libc.include.llvm-libc-types.size_t
312 add_entrypoint_object(
319 .memory_utils.inline_strcmp
322 add_entrypoint_object(
329 .memory_utils.inline_strcmp
330 libc.src.__support.ctype_utils
333 add_entrypoint_object(
341 add_entrypoint_object(
348 .memory_utils.inline_memcpy
351 libc.src.__support.CPP.new
352 libc.include.llvm-libc-types.size_t
355 add_entrypoint_object(
365 add_entrypoint_object(
375 add_entrypoint_object(
385 add_entrypoint_object(
395 add_entrypoint_object(
402 libc.src.__support.StringUtil.signal_to_string
405 add_entrypoint_object(
412 libc.src.__support.CPP.bitset
415 add_entrypoint_object(
422 .memory_utils.inline_strstr
425 add_entrypoint_object(
435 add_entrypoint_object(
445 add_entrypoint_object(
453 .memory_utils.inline_memcpy
456 add_entrypoint_object(
464 .memory_utils.inline_memcpy
467 add_entrypoint_object(
475 .memory_utils.inline_memset
478 # Helper to define a function with multiple implementations
479 # - Computes flags to satisfy required/rejected features and arch,
480 # - Declares an entry point,
481 # - Attach the REQUIRE_CPU_FEATURES property to the target,
482 # - Add the fully qualified target to `${name}_implementations` global property for tests.
483 function(add_implementation name impl_name)
484 cmake_parse_arguments(
486 "" # Optional arguments
487 "" # Single value arguments
488 "REQUIRE;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;MLLVM_COMPILE_OPTIONS" # Multi value arguments
491 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
492 # Note that '-mllvm' needs to be prefixed with 'SHELL:' to prevent CMake flag deduplication.
493 foreach(opt IN LISTS ADD_IMPL_MLLVM_COMPILE_OPTIONS)
494 list(APPEND ADD_IMPL_COMPILE_OPTIONS "SHELL:-mllvm ${opt}")
498 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
499 # Prevent warning when passing x86 SIMD types as template arguments.
500 # e.g. "warning: ignoring attributes on template argument ā__m128iā [-Wignored-attributes]"
501 list(APPEND ADD_IMPL_COMPILE_OPTIONS "-Wno-ignored-attributes")
504 add_entrypoint_object(${impl_name}
506 SRCS ${ADD_IMPL_SRCS}
507 HDRS ${ADD_IMPL_HDRS}
508 DEPENDS ${ADD_IMPL_DEPENDS}
509 COMPILE_OPTIONS ${libc_opt_high_flag} ${ADD_IMPL_COMPILE_OPTIONS}
511 get_fq_target_name(${impl_name} fq_target_name)
512 set_target_properties(${fq_target_name} PROPERTIES REQUIRE_CPU_FEATURES "${ADD_IMPL_REQUIRE}")
513 set_property(GLOBAL APPEND PROPERTY "${name}_implementations" "${fq_target_name}")
516 # ------------------------------------------------------------------------------
518 # ------------------------------------------------------------------------------
520 function(add_bcmp bcmp_name)
521 add_implementation(bcmp ${bcmp_name}
522 SRCS ${LIBC_SOURCE_DIR}/src/string/bcmp.cpp
523 HDRS ${LIBC_SOURCE_DIR}/src/string/bcmp.h
525 .memory_utils.memory_utils
531 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
532 add_bcmp(bcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
533 add_bcmp(bcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
534 add_bcmp(bcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
535 add_bcmp(bcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
536 add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
538 elseif(LIBC_TARGET_OS_IS_GPU)
541 add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
545 # ------------------------------------------------------------------------------
547 # ------------------------------------------------------------------------------
549 function(add_bzero bzero_name)
550 add_implementation(bzero ${bzero_name}
551 SRCS ${LIBC_SOURCE_DIR}/src/string/bzero.cpp
552 HDRS ${LIBC_SOURCE_DIR}/src/string/bzero.h
554 .memory_utils.inline_memset
560 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
561 add_bzero(bzero_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
562 add_bzero(bzero_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
563 add_bzero(bzero_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
564 add_bzero(bzero_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
565 add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
567 elseif(LIBC_TARGET_OS_IS_GPU)
570 add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
574 # ------------------------------------------------------------------------------
576 # ------------------------------------------------------------------------------
578 function(add_memcmp memcmp_name)
579 add_implementation(memcmp ${memcmp_name}
580 SRCS ${LIBC_SOURCE_DIR}/src/string/memcmp.cpp
581 HDRS ${LIBC_SOURCE_DIR}/src/string/memcmp.h
583 .memory_utils.inline_memcmp
589 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
590 add_memcmp(memcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
591 add_memcmp(memcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
592 add_memcmp(memcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
593 add_memcmp(memcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
594 add_memcmp(memcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
596 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
597 add_memcmp(memcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
599 elseif(LIBC_TARGET_OS_IS_GPU)
602 add_memcmp(memcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
606 # ------------------------------------------------------------------------------
608 # ------------------------------------------------------------------------------
610 function(add_memcpy memcpy_name)
611 add_implementation(memcpy ${memcpy_name}
612 SRCS ${LIBC_SOURCE_DIR}/src/string/memcpy.cpp
613 HDRS ${LIBC_SOURCE_DIR}/src/string/memcpy.h
615 .memory_utils.inline_memcpy
621 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
622 add_memcpy(memcpy_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
623 add_memcpy(memcpy_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
624 add_memcpy(memcpy_x86_64_opt_avx COMPILE_OPTIONS -march=sandybridge REQUIRE AVX)
625 add_memcpy(memcpy_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
626 add_memcpy(memcpy_x86_64_opt_sw_prefetch_sse4 COMPILE_OPTIONS -DLIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING -march=nehalem REQUIRE SSE4_2)
627 add_memcpy(memcpy_x86_64_opt_sw_prefetch_avx COMPILE_OPTIONS -DLIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING -march=sandybridge REQUIRE AVX)
628 add_memcpy(memcpy_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
630 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
631 # Disable tail merging as it leads to lower performance.
632 add_memcpy(memcpy_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
633 MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
634 add_memcpy(memcpy MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
635 elseif(LIBC_TARGET_OS_IS_GPU)
638 add_memcpy(memcpy_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
642 # ------------------------------------------------------------------------------
644 # ------------------------------------------------------------------------------
646 function(add_memmove memmove_name)
647 add_implementation(memmove ${memmove_name}
648 SRCS ${LIBC_SOURCE_DIR}/src/string/memmove.cpp
649 HDRS ${LIBC_SOURCE_DIR}/src/string/memmove.h
651 .memory_utils.inline_memcpy
657 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
658 add_memmove(memmove_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
659 add_memmove(memmove_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
660 add_memmove(memmove_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
661 add_memmove(memmove_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
662 add_memmove(memmove_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
664 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
665 # Disable tail merging as it leads to lower performance.
666 add_memmove(memmove_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
667 MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
668 add_memmove(memmove MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
669 elseif(LIBC_TARGET_OS_IS_GPU)
672 add_memmove(memmove_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
676 # ------------------------------------------------------------------------------
678 # ------------------------------------------------------------------------------
680 function(add_memset memset_name)
681 add_implementation(memset ${memset_name}
682 SRCS ${LIBC_SOURCE_DIR}/src/string/memset.cpp
683 HDRS ${LIBC_SOURCE_DIR}/src/string/memset.h
685 .memory_utils.inline_memset
691 if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
692 add_memset(memset_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
693 add_memset(memset_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
694 add_memset(memset_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
695 add_memset(memset_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
696 add_memset(memset_x86_64_opt_sw_prefetch COMPILE_OPTIONS -DLIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING)
697 add_memset(memset_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
699 elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
700 # Disable tail merging as it leads to lower performance.
701 add_memset(memset_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
702 MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
703 add_memset(memset MLLVM_COMPILE_OPTIONS "-tail-merge-threshold=0")
704 elseif(LIBC_TARGET_OS_IS_GPU)
707 add_memset(memset_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})