1 if(LIBC_TARGET_OS_IS_GPU)
6 # The CPU build depends on Google benchmark.
7 if(NOT LIBC_INCLUDE_BENCHMARKS)
13 set(LLVM_LINK_COMPONENTS
18 #==============================================================================
19 # Add Unit Testing Support
20 #==============================================================================
22 function(add_libc_benchmark_unittest target_name)
23 if(NOT LLVM_INCLUDE_TESTS)
27 cmake_parse_arguments(
28 "LIBC_BENCHMARKS_UNITTEST"
29 "" # No optional arguments
30 "SUITE" # Single value arguments
31 "SRCS;DEPENDS" # Multi-value arguments
35 add_executable(${target_name}
37 ${LIBC_BENCHMARKS_UNITTEST_SRCS}
39 target_link_libraries(${target_name}
43 ${LIBC_BENCHMARKS_UNITTEST_DEPENDS}
45 llvm_update_compile_flags(${target_name})
50 COMMAND $<TARGET_FILE:${target_name}>
52 add_dependencies(libc-benchmark-util-tests ${target_name})
55 #==============================================================================
56 # Build Google Benchmark for libc
57 #==============================================================================
59 include(ExternalProject)
60 ExternalProject_Add(google-benchmark-libc
62 PREFIX google-benchmark-libc
63 SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
64 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-libc
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
72 -DCMAKE_BUILD_TYPE:STRING=Release
74 -DCMAKE_SYSTEM_NAME:STRING=${CMAKE_SYSTEM_NAME}
75 -DCMAKE_SYSTEM_PROCESSOR:STRING=${CMAKE_SYSTEM_PROCESSOR}
76 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
77 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
78 -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
79 -DCMAKE_FIND_ROOT_PATH:STRING=${CMAKE_FIND_ROOT_PATH}
81 -DBUILD_SHARED_LIBS:BOOL=OFF
82 -DCMAKE_EXE_LINKER_FLAGS:STRING=-static
84 -DCMAKE_CXX_STANDARD:STRING=14
85 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
88 add_custom_target(libc-benchmark-util-tests)
91 add_library(libc-benchmark
98 target_include_directories(libc-benchmark
99 PUBLIC ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR}
101 target_link_libraries(libc-benchmark
108 add_dependencies(libc-benchmark google-benchmark-libc)
109 llvm_update_compile_flags(libc-benchmark)
111 add_libc_benchmark_unittest(libc-benchmark-test
112 SRCS LibcBenchmarkTest.cpp
113 DEPENDS libc-benchmark
116 # libc-memory-benchmark
117 add_library(libc-memory-benchmark
120 LibcMemoryBenchmark.cpp
121 LibcMemoryBenchmark.h
122 LibcFunctionPrototypes.h
123 MemorySizeDistributions.cpp
124 MemorySizeDistributions.h
126 target_include_directories(libc-memory-benchmark
128 ${CMAKE_CURRENT_SOURCE_DIR}
130 target_link_libraries(libc-memory-benchmark
134 llvm_update_compile_flags(libc-memory-benchmark)
136 add_libc_benchmark_unittest(libc-memory-benchmark-test
137 SRCS LibcMemoryBenchmarkTest.cpp
138 DEPENDS libc-memory-benchmark
148 target_link_libraries(json PUBLIC libc-memory-benchmark)
149 llvm_update_compile_flags(json)
151 add_libc_benchmark_unittest(json-test
156 #==============================================================================
158 #==============================================================================
160 # Benchmark all implementations that can run on the target CPU.
161 function(add_libc_multi_impl_benchmark name)
162 get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
163 foreach(fq_config_name IN LISTS fq_implementations)
164 get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
165 cpu_supports(can_run "${required_cpu_features}")
167 set(benchmark_name ${fq_config_name}_benchmark)
168 add_executable(${benchmark_name}
170 LibcMemoryBenchmarkMain.cpp
172 get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")
173 target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})
174 string(TOUPPER ${name} name_upper)
175 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
176 llvm_update_compile_flags(${benchmark_name})
178 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
183 add_libc_multi_impl_benchmark(bcmp)
184 add_libc_multi_impl_benchmark(bzero)
185 add_libc_multi_impl_benchmark(memcmp)
186 add_libc_multi_impl_benchmark(memcpy)
187 add_libc_multi_impl_benchmark(memmove)
188 add_libc_multi_impl_benchmark(memset)
190 #==============================================================================
191 # Google Benchmarking tool
192 #==============================================================================
194 # This target uses the Google Benchmark facility to report throughput for llvm
195 # libc memory functions compiled for the host machine. This is useful to
196 # continuously monitor the performance of the memory functions.
197 add_executable(libc.benchmarks.memory_functions.opt_host
199 LibcMemoryGoogleBenchmarkMain.cpp
200 LibcDefaultImplementations.cpp
202 target_link_libraries(libc.benchmarks.memory_functions.opt_host
204 libc-memory-benchmark
205 libc.src.string.memcmp_opt_host.__internal__
206 libc.src.string.bcmp_opt_host.__internal__
207 libc.src.string.memcpy_opt_host.__internal__
208 libc.src.string.memset_opt_host.__internal__
209 libc.src.string.bzero_opt_host.__internal__
210 libc.src.string.memmove_opt_host.__internal__
213 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
215 add_subdirectory(automemcpy)