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}
131 target_link_libraries(libc-memory-benchmark
135 llvm_update_compile_flags(libc-memory-benchmark)
137 add_libc_benchmark_unittest(libc-memory-benchmark-test
138 SRCS LibcMemoryBenchmarkTest.cpp
139 DEPENDS libc-memory-benchmark
149 target_link_libraries(json PUBLIC libc-memory-benchmark)
150 llvm_update_compile_flags(json)
152 add_libc_benchmark_unittest(json-test
157 #==============================================================================
159 #==============================================================================
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}")
168 set(benchmark_name ${fq_config_name}_benchmark)
169 add_executable(${benchmark_name}
171 LibcMemoryBenchmarkMain.cpp
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)
176 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
177 llvm_update_compile_flags(${benchmark_name})
179 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
184 add_libc_multi_impl_benchmark(bcmp)
185 add_libc_multi_impl_benchmark(bzero)
186 add_libc_multi_impl_benchmark(memcmp)
187 add_libc_multi_impl_benchmark(memcpy)
188 add_libc_multi_impl_benchmark(memmove)
189 add_libc_multi_impl_benchmark(memset)
191 #==============================================================================
192 # Google Benchmarking tool
193 #==============================================================================
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
200 LibcMemoryGoogleBenchmarkMain.cpp
201 LibcDefaultImplementations.cpp
203 target_link_libraries(libc.benchmarks.memory_functions.opt_host
205 libc-memory-benchmark
206 libc.src.string.memcmp_opt_host.__internal__
207 libc.src.string.bcmp_opt_host.__internal__
208 libc.src.string.memcpy_opt_host.__internal__
209 libc.src.string.memset_opt_host.__internal__
210 libc.src.string.bzero_opt_host.__internal__
211 libc.src.string.memmove_opt_host.__internal__
214 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
216 add_subdirectory(automemcpy)