3 set(LLVM_LINK_COMPONENTS
8 #==============================================================================
9 # Add Unit Testing Support
10 #==============================================================================
12 function(add_libc_benchmark_unittest target_name)
13 if(NOT LLVM_INCLUDE_TESTS)
17 cmake_parse_arguments(
18 "LIBC_BENCHMARKS_UNITTEST"
19 "" # No optional arguments
20 "SUITE" # Single value arguments
21 "SRCS;DEPENDS" # Multi-value arguments
25 add_executable(${target_name}
27 ${LIBC_BENCHMARKS_UNITTEST_SRCS}
29 target_link_libraries(${target_name}
33 ${LIBC_BENCHMARKS_UNITTEST_DEPENDS}
35 llvm_update_compile_flags(${target_name})
40 COMMAND $<TARGET_FILE:${target_name}>
42 add_dependencies(libc-benchmark-util-tests ${target_name})
45 #==============================================================================
46 # Build Google Benchmark for libc
47 #==============================================================================
49 include(ExternalProject)
50 ExternalProject_Add(google-benchmark-libc
52 PREFIX google-benchmark-libc
53 SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
54 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-libc
56 -DBENCHMARK_ENABLE_EXCEPTIONS:BOOL=OFF
57 -DBENCHMARK_ENABLE_LTO:BOOL=OFF
58 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF
59 -DBENCHMARK_ENABLE_WERROR:BOOL=${LLVM_ENABLE_WERROR}
60 -DBENCHMARK_FORCE_WERROR:BOOL=OFF
61 -DBENCHMARK_USE_LIBCXX:BOOL=OFF
62 -DCMAKE_BUILD_TYPE:STRING=RELEASE
63 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
64 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
65 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBC_COMPILE_FLAGS}
66 -DCMAKE_CXX_STANDARD:STRING=14
67 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
70 add_custom_target(libc-benchmark-util-tests)
73 add_library(libc-benchmark
80 target_link_libraries(libc-benchmark
87 add_dependencies(libc-benchmark google-benchmark-libc)
88 llvm_update_compile_flags(libc-benchmark)
90 add_libc_benchmark_unittest(libc-benchmark-test
91 SRCS LibcBenchmarkTest.cpp
92 DEPENDS libc-benchmark
95 # libc-memory-benchmark
96 add_library(libc-memory-benchmark
99 LibcMemoryBenchmark.cpp
100 LibcMemoryBenchmark.h
101 LibcFunctionPrototypes.h
102 MemorySizeDistributions.cpp
103 MemorySizeDistributions.h
105 target_include_directories(libc-memory-benchmark
107 ${CMAKE_CURRENT_SOURCE_DIR}
109 target_link_libraries(libc-memory-benchmark
113 llvm_update_compile_flags(libc-memory-benchmark)
115 add_libc_benchmark_unittest(libc-memory-benchmark-test
116 SRCS LibcMemoryBenchmarkTest.cpp
117 DEPENDS libc-memory-benchmark
127 target_link_libraries(json PUBLIC libc-memory-benchmark)
128 llvm_update_compile_flags(json)
130 add_libc_benchmark_unittest(json-test
135 #==============================================================================
137 #==============================================================================
139 # Benchmark all implementations that can run on the target CPU.
140 function(add_libc_multi_impl_benchmark name)
141 get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
142 foreach(fq_config_name IN LISTS fq_implementations)
143 get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
144 cpu_supports(can_run "${required_cpu_features}")
146 set(benchmark_name ${fq_config_name}_benchmark)
147 add_executable(${benchmark_name}
149 LibcMemoryBenchmarkMain.cpp
151 get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")
152 target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})
153 string(TOUPPER ${name} name_upper)
154 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=__llvm_libc::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
155 llvm_update_compile_flags(${benchmark_name})
157 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
162 add_libc_multi_impl_benchmark(bcmp)
163 add_libc_multi_impl_benchmark(bzero)
164 add_libc_multi_impl_benchmark(memcmp)
165 add_libc_multi_impl_benchmark(memcpy)
166 add_libc_multi_impl_benchmark(memmove)
167 add_libc_multi_impl_benchmark(memset)
169 #==============================================================================
170 # Google Benchmarking tool
171 #==============================================================================
173 # This target uses the Google Benchmark facility to report throughput for llvm
174 # libc memory functions compiled for the host machine. This is useful to
175 # continuously monitor the performance of the memory functions.
176 add_executable(libc.benchmarks.memory_functions.opt_host
178 LibcMemoryGoogleBenchmarkMain.cpp
179 LibcDefaultImplementations.cpp
181 target_link_libraries(libc.benchmarks.memory_functions.opt_host
183 libc-memory-benchmark
184 libc.src.string.memcmp_opt_host
185 libc.src.string.bcmp_opt_host
186 libc.src.string.memcpy_opt_host
187 libc.src.string.memset_opt_host
188 libc.src.string.bzero_opt_host
189 libc.src.string.memmove_opt_host
192 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
194 add_subdirectory(automemcpy)