jameshu15869 | 02b57de | 2024-06-26 21:38:39 | [diff] [blame] | 1 | if(LIBC_TARGET_OS_IS_GPU) |
| 2 | add_subdirectory(gpu) |
| 3 | return() |
| 4 | endif() |
| 5 | |
| 6 | # The CPU build depends on Google benchmark. |
| 7 | if(NOT LIBC_INCLUDE_BENCHMARKS) |
| 8 | return() |
| 9 | endif() |
| 10 | |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 11 | find_package(Threads) |
| 12 | |
Archibald Elliott | f09cf34 | 2022-12-20 10:24:02 | [diff] [blame] | 13 | set(LLVM_LINK_COMPONENTS |
| 14 | Support |
| 15 | TargetParser |
| 16 | ) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 17 | |
| 18 | #============================================================================== |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 19 | # Add Unit Testing Support |
| 20 | #============================================================================== |
| 21 | |
| 22 | function(add_libc_benchmark_unittest target_name) |
| 23 | if(NOT LLVM_INCLUDE_TESTS) |
| 24 | return() |
| 25 | endif() |
| 26 | |
| 27 | cmake_parse_arguments( |
| 28 | "LIBC_BENCHMARKS_UNITTEST" |
| 29 | "" # No optional arguments |
| 30 | "SUITE" # Single value arguments |
| 31 | "SRCS;DEPENDS" # Multi-value arguments |
| 32 | ${ARGN} |
| 33 | ) |
| 34 | |
| 35 | add_executable(${target_name} |
| 36 | EXCLUDE_FROM_ALL |
| 37 | ${LIBC_BENCHMARKS_UNITTEST_SRCS} |
| 38 | ) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 39 | target_link_libraries(${target_name} |
| 40 | PRIVATE |
Stella Laurenzo | 38151a0 | 2022-03-04 20:08:04 | [diff] [blame] | 41 | llvm_gtest_main |
| 42 | llvm_gtest |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 43 | ${LIBC_BENCHMARKS_UNITTEST_DEPENDS} |
| 44 | ) |
Guillaume Chatelet | 36c36c1 | 2022-02-02 13:35:46 | [diff] [blame] | 45 | llvm_update_compile_flags(${target_name}) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 46 | |
| 47 | add_custom_command( |
| 48 | TARGET ${target_name} |
| 49 | POST_BUILD |
| 50 | COMMAND $<TARGET_FILE:${target_name}> |
| 51 | ) |
Paula Toth | d80715d | 2020-05-06 08:21:38 | [diff] [blame] | 52 | add_dependencies(libc-benchmark-util-tests ${target_name}) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 53 | endfunction() |
| 54 | |
| 55 | #============================================================================== |
| 56 | # Build Google Benchmark for libc |
| 57 | #============================================================================== |
| 58 | |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 59 | include(ExternalProject) |
| 60 | ExternalProject_Add(google-benchmark-libc |
| 61 | EXCLUDE_FROM_ALL ON |
| 62 | PREFIX google-benchmark-libc |
| 63 | SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark |
| 64 | INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-libc |
| 65 | CMAKE_CACHE_ARGS |
| 66 | -DBENCHMARK_ENABLE_EXCEPTIONS:BOOL=OFF |
| 67 | -DBENCHMARK_ENABLE_LTO:BOOL=OFF |
| 68 | -DBENCHMARK_ENABLE_TESTING:BOOL=OFF |
| 69 | -DBENCHMARK_ENABLE_WERROR:BOOL=${LLVM_ENABLE_WERROR} |
| 70 | -DBENCHMARK_FORCE_WERROR:BOOL=OFF |
| 71 | -DBENCHMARK_USE_LIBCXX:BOOL=OFF |
Guillaume Chatelet | b51e6bf | 2023-05-09 13:53:32 | [diff] [blame] | 72 | -DCMAKE_BUILD_TYPE:STRING=Release |
| 73 | |
| 74 | -DCMAKE_SYSTEM_NAME:STRING=${CMAKE_SYSTEM_NAME} |
| 75 | -DCMAKE_SYSTEM_PROCESSOR:STRING=${CMAKE_SYSTEM_PROCESSOR} |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 76 | -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} |
| 77 | -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} |
Guillaume Chatelet | b51e6bf | 2023-05-09 13:53:32 | [diff] [blame] | 78 | -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} |
| 79 | -DCMAKE_FIND_ROOT_PATH:STRING=${CMAKE_FIND_ROOT_PATH} |
| 80 | |
| 81 | -DBUILD_SHARED_LIBS:BOOL=OFF |
| 82 | -DCMAKE_EXE_LINKER_FLAGS:STRING=-static |
| 83 | |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 84 | -DCMAKE_CXX_STANDARD:STRING=14 |
| 85 | -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> |
| 86 | ) |
| 87 | |
Paula Toth | d80715d | 2020-05-06 08:21:38 | [diff] [blame] | 88 | add_custom_target(libc-benchmark-util-tests) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 89 | |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 90 | # libc-benchmark |
| 91 | add_library(libc-benchmark |
| 92 | STATIC |
| 93 | EXCLUDE_FROM_ALL |
| 94 | LibcBenchmark.cpp |
| 95 | LibcBenchmark.h |
| 96 | ) |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 97 | |
Guillaume Chatelet | 4670777 | 2023-09-20 09:21:46 | [diff] [blame] | 98 | target_include_directories(libc-benchmark |
| 99 | PUBLIC ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR} |
| 100 | ) |
Guillaume Chatelet | c2dcdf9 | 2020-01-27 09:55:32 | [diff] [blame] | 101 | target_link_libraries(libc-benchmark |
| 102 | PUBLIC |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 103 | benchmark::benchmark |
Guillaume Chatelet | c2dcdf9 | 2020-01-27 09:55:32 | [diff] [blame] | 104 | LLVMSupport |
Archibald Elliott | 2a261a7 | 2022-12-20 11:29:04 | [diff] [blame] | 105 | LLVMTargetParser |
Guillaume Chatelet | c2dcdf9 | 2020-01-27 09:55:32 | [diff] [blame] | 106 | Threads::Threads |
| 107 | ) |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 108 | add_dependencies(libc-benchmark google-benchmark-libc) |
Guillaume Chatelet | 36c36c1 | 2022-02-02 13:35:46 | [diff] [blame] | 109 | llvm_update_compile_flags(libc-benchmark) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 110 | |
| 111 | add_libc_benchmark_unittest(libc-benchmark-test |
| 112 | SRCS LibcBenchmarkTest.cpp |
| 113 | DEPENDS libc-benchmark |
| 114 | ) |
| 115 | |
| 116 | # libc-memory-benchmark |
| 117 | add_library(libc-memory-benchmark |
| 118 | STATIC |
| 119 | EXCLUDE_FROM_ALL |
| 120 | LibcMemoryBenchmark.cpp |
| 121 | LibcMemoryBenchmark.h |
Guillaume Chatelet | 00c943a | 2021-10-11 15:26:43 | [diff] [blame] | 122 | LibcFunctionPrototypes.h |
Guillaume Chatelet | c400e01 | 2020-10-15 08:01:26 | [diff] [blame] | 123 | MemorySizeDistributions.cpp |
| 124 | MemorySizeDistributions.h |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 125 | ) |
Guillaume Chatelet | 00c943a | 2021-10-11 15:26:43 | [diff] [blame] | 126 | target_include_directories(libc-memory-benchmark |
| 127 | PUBLIC |
| 128 | ${CMAKE_CURRENT_SOURCE_DIR} |
David Peixotto | 081a80f | 2024-11-14 19:10:11 | [diff] [blame] | 129 | ${LIBC_SOURCE_DIR} |
Guillaume Chatelet | 00c943a | 2021-10-11 15:26:43 | [diff] [blame] | 130 | ) |
Guillaume Chatelet | deae7e9 | 2020-12-17 13:16:14 | [diff] [blame] | 131 | target_link_libraries(libc-memory-benchmark |
| 132 | PUBLIC |
| 133 | libc-benchmark |
| 134 | ) |
Guillaume Chatelet | 36c36c1 | 2022-02-02 13:35:46 | [diff] [blame] | 135 | llvm_update_compile_flags(libc-memory-benchmark) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 136 | |
| 137 | add_libc_benchmark_unittest(libc-memory-benchmark-test |
| 138 | SRCS LibcMemoryBenchmarkTest.cpp |
| 139 | DEPENDS libc-memory-benchmark |
| 140 | ) |
| 141 | |
| 142 | # json |
| 143 | add_library(json |
| 144 | STATIC |
| 145 | EXCLUDE_FROM_ALL |
| 146 | JSON.cpp |
| 147 | JSON.h |
| 148 | ) |
| 149 | target_link_libraries(json PUBLIC libc-memory-benchmark) |
Guillaume Chatelet | 36c36c1 | 2022-02-02 13:35:46 | [diff] [blame] | 150 | llvm_update_compile_flags(json) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 151 | |
| 152 | add_libc_benchmark_unittest(json-test |
| 153 | SRCS JSONTest.cpp |
| 154 | DEPENDS json |
| 155 | ) |
| 156 | |
| 157 | #============================================================================== |
Guillaume Chatelet | deae7e9 | 2020-12-17 13:16:14 | [diff] [blame] | 158 | # Benchmarking tool |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 159 | #============================================================================== |
| 160 | |
Guillaume Chatelet | 8d64ed8 | 2021-06-10 13:04:56 | [diff] [blame] | 161 | # Benchmark all implementations that can run on the target CPU. |
| 162 | function(add_libc_multi_impl_benchmark name) |
| 163 | get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations) |
| 164 | foreach(fq_config_name IN LISTS fq_implementations) |
| 165 | get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES) |
| 166 | cpu_supports(can_run "${required_cpu_features}") |
| 167 | if(can_run) |
| 168 | set(benchmark_name ${fq_config_name}_benchmark) |
| 169 | add_executable(${benchmark_name} |
| 170 | EXCLUDE_FROM_ALL |
| 171 | LibcMemoryBenchmarkMain.cpp |
| 172 | ) |
| 173 | get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW") |
| 174 | target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file}) |
| 175 | string(TOUPPER ${name} name_upper) |
Guillaume Chatelet | b6bc9d7 | 2023-09-26 09:45:04 | [diff] [blame] | 176 | target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"") |
Guillaume Chatelet | d5bb0de | 2022-02-02 14:15:39 | [diff] [blame] | 177 | llvm_update_compile_flags(${benchmark_name}) |
Guillaume Chatelet | 8d64ed8 | 2021-06-10 13:04:56 | [diff] [blame] | 178 | else() |
| 179 | message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'") |
| 180 | endif() |
| 181 | endforeach() |
| 182 | endfunction() |
| 183 | |
Guillaume Chatelet | de21f34 | 2021-11-30 10:46:16 | [diff] [blame] | 184 | add_libc_multi_impl_benchmark(bcmp) |
Guillaume Chatelet | 87065c0 | 2021-06-23 14:19:40 | [diff] [blame] | 185 | add_libc_multi_impl_benchmark(bzero) |
| 186 | add_libc_multi_impl_benchmark(memcmp) |
Guillaume Chatelet | de21f34 | 2021-11-30 10:46:16 | [diff] [blame] | 187 | add_libc_multi_impl_benchmark(memcpy) |
| 188 | add_libc_multi_impl_benchmark(memmove) |
| 189 | add_libc_multi_impl_benchmark(memset) |
Guillaume Chatelet | 59198d0 | 2021-08-02 12:14:11 | [diff] [blame] | 190 | |
| 191 | #============================================================================== |
| 192 | # Google Benchmarking tool |
| 193 | #============================================================================== |
| 194 | |
| 195 | # This target uses the Google Benchmark facility to report throughput for llvm |
| 196 | # libc memory functions compiled for the host machine. This is useful to |
| 197 | # continuously monitor the performance of the memory functions. |
| 198 | add_executable(libc.benchmarks.memory_functions.opt_host |
| 199 | EXCLUDE_FROM_ALL |
| 200 | LibcMemoryGoogleBenchmarkMain.cpp |
Guillaume Chatelet | adc18ad | 2021-09-30 13:10:45 | [diff] [blame] | 201 | LibcDefaultImplementations.cpp |
Guillaume Chatelet | 59198d0 | 2021-08-02 12:14:11 | [diff] [blame] | 202 | ) |
Guillaume Chatelet | 59198d0 | 2021-08-02 12:14:11 | [diff] [blame] | 203 | target_link_libraries(libc.benchmarks.memory_functions.opt_host |
| 204 | PRIVATE |
| 205 | libc-memory-benchmark |
Guillaume Chatelet | b51e6bf | 2023-05-09 13:53:32 | [diff] [blame] | 206 | libc.src.string.memcmp_opt_host.__internal__ |
Guillaume Chatelet | b51e6bf | 2023-05-09 13:53:32 | [diff] [blame] | 207 | libc.src.string.memcpy_opt_host.__internal__ |
Guillaume Chatelet | b51e6bf | 2023-05-09 13:53:32 | [diff] [blame] | 208 | libc.src.string.memmove_opt_host.__internal__ |
Nick Desaulniers | 431ea2d | 2024-12-10 16:58:45 | [diff] [blame] | 209 | libc.src.string.memset_opt_host.__internal__ |
| 210 | libc.src.strings.bcmp_opt_host.__internal__ |
| 211 | libc.src.strings.bzero_opt_host.__internal__ |
Guillaume Chatelet | 59198d0 | 2021-08-02 12:14:11 | [diff] [blame] | 212 | benchmark_main |
| 213 | ) |
Guillaume Chatelet | d5bb0de | 2022-02-02 14:15:39 | [diff] [blame] | 214 | llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host) |