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
64 -DCMAKE_SYSTEM_NAME:STRING=${CMAKE_SYSTEM_NAME}
65 -DCMAKE_SYSTEM_PROCESSOR:STRING=${CMAKE_SYSTEM_PROCESSOR}
66 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
67 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
68 -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
69 -DCMAKE_FIND_ROOT_PATH:STRING=${CMAKE_FIND_ROOT_PATH}
71 -DBUILD_SHARED_LIBS:BOOL=OFF
72 -DCMAKE_EXE_LINKER_FLAGS:STRING=-static
74 -DCMAKE_CXX_STANDARD:STRING=14
75 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
78 add_custom_target(libc-benchmark-util-tests)
81 add_library(libc-benchmark
88 target_link_libraries(libc-benchmark
95 add_dependencies(libc-benchmark google-benchmark-libc)
96 llvm_update_compile_flags(libc-benchmark)
98 add_libc_benchmark_unittest(libc-benchmark-test
99 SRCS LibcBenchmarkTest.cpp
100 DEPENDS libc-benchmark
103 # libc-memory-benchmark
104 add_library(libc-memory-benchmark
107 LibcMemoryBenchmark.cpp
108 LibcMemoryBenchmark.h
109 LibcFunctionPrototypes.h
110 MemorySizeDistributions.cpp
111 MemorySizeDistributions.h
113 target_include_directories(libc-memory-benchmark
115 ${CMAKE_CURRENT_SOURCE_DIR}
117 target_link_libraries(libc-memory-benchmark
121 llvm_update_compile_flags(libc-memory-benchmark)
123 add_libc_benchmark_unittest(libc-memory-benchmark-test
124 SRCS LibcMemoryBenchmarkTest.cpp
125 DEPENDS libc-memory-benchmark
135 target_link_libraries(json PUBLIC libc-memory-benchmark)
136 llvm_update_compile_flags(json)
138 add_libc_benchmark_unittest(json-test
143 #==============================================================================
145 #==============================================================================
147 # Benchmark all implementations that can run on the target CPU.
148 function(add_libc_multi_impl_benchmark name)
149 get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
150 foreach(fq_config_name IN LISTS fq_implementations)
151 get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
152 cpu_supports(can_run "${required_cpu_features}")
154 set(benchmark_name ${fq_config_name}_benchmark)
155 add_executable(${benchmark_name}
157 LibcMemoryBenchmarkMain.cpp
159 get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")
160 target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})
161 string(TOUPPER ${name} name_upper)
162 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=__llvm_libc::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
163 llvm_update_compile_flags(${benchmark_name})
165 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
170 add_libc_multi_impl_benchmark(bcmp)
171 add_libc_multi_impl_benchmark(bzero)
172 add_libc_multi_impl_benchmark(memcmp)
173 add_libc_multi_impl_benchmark(memcpy)
174 add_libc_multi_impl_benchmark(memmove)
175 add_libc_multi_impl_benchmark(memset)
177 #==============================================================================
178 # Google Benchmarking tool
179 #==============================================================================
181 # This target uses the Google Benchmark facility to report throughput for llvm
182 # libc memory functions compiled for the host machine. This is useful to
183 # continuously monitor the performance of the memory functions.
184 add_executable(libc.benchmarks.memory_functions.opt_host
186 LibcMemoryGoogleBenchmarkMain.cpp
187 LibcDefaultImplementations.cpp
189 target_link_libraries(libc.benchmarks.memory_functions.opt_host
191 libc-memory-benchmark
192 libc.src.string.memcmp_opt_host.__internal__
193 libc.src.string.bcmp_opt_host.__internal__
194 libc.src.string.memcpy_opt_host.__internal__
195 libc.src.string.memset_opt_host.__internal__
196 libc.src.string.bzero_opt_host.__internal__
197 libc.src.string.memmove_opt_host.__internal__
200 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
202 add_subdirectory(automemcpy)