blob: 60f522d7d8c65db8957357d8c8336bc076dc4e29 [file] [log] [blame]
jameshu1586902b57de2024-06-26 21:38:391if(LIBC_TARGET_OS_IS_GPU)
2 add_subdirectory(gpu)
3 return()
4endif()
5
6# The CPU build depends on Google benchmark.
7if(NOT LIBC_INCLUDE_BENCHMARKS)
8 return()
9endif()
10
Guillaume Chateletaba80d02020-01-06 12:17:0411find_package(Threads)
12
Archibald Elliottf09cf342022-12-20 10:24:0213set(LLVM_LINK_COMPONENTS
14 Support
15 TargetParser
16 )
Guillaume Chateletaba80d02020-01-06 12:17:0417
18#==============================================================================
Guillaume Chateletaba80d02020-01-06 12:17:0419# Add Unit Testing Support
20#==============================================================================
21
22function(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 Chateletaba80d02020-01-06 12:17:0439 target_link_libraries(${target_name}
40 PRIVATE
Stella Laurenzo38151a02022-03-04 20:08:0441 llvm_gtest_main
42 llvm_gtest
Guillaume Chateletaba80d02020-01-06 12:17:0443 ${LIBC_BENCHMARKS_UNITTEST_DEPENDS}
44 )
Guillaume Chatelet36c36c12022-02-02 13:35:4645 llvm_update_compile_flags(${target_name})
Guillaume Chateletaba80d02020-01-06 12:17:0446
47 add_custom_command(
48 TARGET ${target_name}
49 POST_BUILD
50 COMMAND $<TARGET_FILE:${target_name}>
51 )
Paula Tothd80715d2020-05-06 08:21:3852 add_dependencies(libc-benchmark-util-tests ${target_name})
Guillaume Chateletaba80d02020-01-06 12:17:0453endfunction()
54
55#==============================================================================
56# Build Google Benchmark for libc
57#==============================================================================
58
Guillaume Chateletdf838db2022-03-25 13:21:2059include(ExternalProject)
60ExternalProject_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 Chateletb51e6bf2023-05-09 13:53:3272 -DCMAKE_BUILD_TYPE:STRING=Release
73
74 -DCMAKE_SYSTEM_NAME:STRING=${CMAKE_SYSTEM_NAME}
75 -DCMAKE_SYSTEM_PROCESSOR:STRING=${CMAKE_SYSTEM_PROCESSOR}
Guillaume Chateletdf838db2022-03-25 13:21:2076 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
77 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
Guillaume Chateletb51e6bf2023-05-09 13:53:3278 -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 Chateletdf838db2022-03-25 13:21:2084 -DCMAKE_CXX_STANDARD:STRING=14
85 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
86 )
87
Paula Tothd80715d2020-05-06 08:21:3888add_custom_target(libc-benchmark-util-tests)
Guillaume Chateletaba80d02020-01-06 12:17:0489
Guillaume Chateletaba80d02020-01-06 12:17:0490# libc-benchmark
91add_library(libc-benchmark
92 STATIC
93 EXCLUDE_FROM_ALL
94 LibcBenchmark.cpp
95 LibcBenchmark.h
96)
Guillaume Chateletdf838db2022-03-25 13:21:2097
Guillaume Chatelet46707772023-09-20 09:21:4698target_include_directories(libc-benchmark
99 PUBLIC ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR}
100)
Guillaume Chateletc2dcdf92020-01-27 09:55:32101target_link_libraries(libc-benchmark
102 PUBLIC
Guillaume Chateletdf838db2022-03-25 13:21:20103 benchmark::benchmark
Guillaume Chateletc2dcdf92020-01-27 09:55:32104 LLVMSupport
Archibald Elliott2a261a72022-12-20 11:29:04105 LLVMTargetParser
Guillaume Chateletc2dcdf92020-01-27 09:55:32106 Threads::Threads
107)
Guillaume Chateletdf838db2022-03-25 13:21:20108add_dependencies(libc-benchmark google-benchmark-libc)
Guillaume Chatelet36c36c12022-02-02 13:35:46109llvm_update_compile_flags(libc-benchmark)
Guillaume Chateletaba80d02020-01-06 12:17:04110
111add_libc_benchmark_unittest(libc-benchmark-test
112 SRCS LibcBenchmarkTest.cpp
113 DEPENDS libc-benchmark
114)
115
116# libc-memory-benchmark
117add_library(libc-memory-benchmark
118 STATIC
119 EXCLUDE_FROM_ALL
120 LibcMemoryBenchmark.cpp
121 LibcMemoryBenchmark.h
Guillaume Chatelet00c943a2021-10-11 15:26:43122 LibcFunctionPrototypes.h
Guillaume Chateletc400e012020-10-15 08:01:26123 MemorySizeDistributions.cpp
124 MemorySizeDistributions.h
Guillaume Chateletaba80d02020-01-06 12:17:04125)
Guillaume Chatelet00c943a2021-10-11 15:26:43126target_include_directories(libc-memory-benchmark
127 PUBLIC
128 ${CMAKE_CURRENT_SOURCE_DIR}
David Peixotto081a80f2024-11-14 19:10:11129 ${LIBC_SOURCE_DIR}
Guillaume Chatelet00c943a2021-10-11 15:26:43130)
Guillaume Chateletdeae7e92020-12-17 13:16:14131target_link_libraries(libc-memory-benchmark
132 PUBLIC
133 libc-benchmark
134)
Guillaume Chatelet36c36c12022-02-02 13:35:46135llvm_update_compile_flags(libc-memory-benchmark)
Guillaume Chateletaba80d02020-01-06 12:17:04136
137add_libc_benchmark_unittest(libc-memory-benchmark-test
138 SRCS LibcMemoryBenchmarkTest.cpp
139 DEPENDS libc-memory-benchmark
140)
141
142# json
143add_library(json
144 STATIC
145 EXCLUDE_FROM_ALL
146 JSON.cpp
147 JSON.h
148)
149target_link_libraries(json PUBLIC libc-memory-benchmark)
Guillaume Chatelet36c36c12022-02-02 13:35:46150llvm_update_compile_flags(json)
Guillaume Chateletaba80d02020-01-06 12:17:04151
152add_libc_benchmark_unittest(json-test
153 SRCS JSONTest.cpp
154 DEPENDS json
155)
156
157#==============================================================================
Guillaume Chateletdeae7e92020-12-17 13:16:14158# Benchmarking tool
Guillaume Chateletaba80d02020-01-06 12:17:04159#==============================================================================
160
Guillaume Chatelet8d64ed82021-06-10 13:04:56161# Benchmark all implementations that can run on the target CPU.
162function(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 Chateletb6bc9d72023-09-26 09:45:04176 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
Guillaume Chateletd5bb0de2022-02-02 14:15:39177 llvm_update_compile_flags(${benchmark_name})
Guillaume Chatelet8d64ed82021-06-10 13:04:56178 else()
179 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
180 endif()
181 endforeach()
182endfunction()
183
Guillaume Chateletde21f342021-11-30 10:46:16184add_libc_multi_impl_benchmark(bcmp)
Guillaume Chatelet87065c02021-06-23 14:19:40185add_libc_multi_impl_benchmark(bzero)
186add_libc_multi_impl_benchmark(memcmp)
Guillaume Chateletde21f342021-11-30 10:46:16187add_libc_multi_impl_benchmark(memcpy)
188add_libc_multi_impl_benchmark(memmove)
189add_libc_multi_impl_benchmark(memset)
Guillaume Chatelet59198d02021-08-02 12:14:11190
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.
198add_executable(libc.benchmarks.memory_functions.opt_host
199 EXCLUDE_FROM_ALL
200 LibcMemoryGoogleBenchmarkMain.cpp
Guillaume Chateletadc18ad2021-09-30 13:10:45201 LibcDefaultImplementations.cpp
Guillaume Chatelet59198d02021-08-02 12:14:11202)
Guillaume Chatelet59198d02021-08-02 12:14:11203target_link_libraries(libc.benchmarks.memory_functions.opt_host
204 PRIVATE
205 libc-memory-benchmark
Guillaume Chateletb51e6bf2023-05-09 13:53:32206 libc.src.string.memcmp_opt_host.__internal__
Guillaume Chateletb51e6bf2023-05-09 13:53:32207 libc.src.string.memcpy_opt_host.__internal__
Guillaume Chateletb51e6bf2023-05-09 13:53:32208 libc.src.string.memmove_opt_host.__internal__
Nick Desaulniers431ea2d2024-12-10 16:58:45209 libc.src.string.memset_opt_host.__internal__
210 libc.src.strings.bcmp_opt_host.__internal__
211 libc.src.strings.bzero_opt_host.__internal__
Guillaume Chatelet59198d02021-08-02 12:14:11212 benchmark_main
213)
Guillaume Chateletd5bb0de2022-02-02 14:15:39214llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)