[AA] Rename CaptureInfo -> CaptureAnalysis (NFC) (#116842)
[llvm-project.git] / libc / benchmarks / CMakeLists.txt
blob52e3f942d16ea34b772e592736e3d9630384a8fb
1 if(LIBC_TARGET_OS_IS_GPU)
2   add_subdirectory(gpu)
3   return()
4 endif()
6 # The CPU build depends on Google benchmark.
7 if(NOT LIBC_INCLUDE_BENCHMARKS)
8   return()
9 endif()
11 find_package(Threads)
13 set(LLVM_LINK_COMPONENTS
14   Support
15   TargetParser
16   )
18 #==============================================================================
19 # Add Unit Testing Support
20 #==============================================================================
22 function(add_libc_benchmark_unittest target_name)
23   if(NOT LLVM_INCLUDE_TESTS)
24     return()
25   endif()
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   )
35   add_executable(${target_name}
36     EXCLUDE_FROM_ALL
37     ${LIBC_BENCHMARKS_UNITTEST_SRCS}
38   )
39   target_link_libraries(${target_name}
40     PRIVATE
41     llvm_gtest_main
42     llvm_gtest
43     ${LIBC_BENCHMARKS_UNITTEST_DEPENDS}
44   )
45   llvm_update_compile_flags(${target_name})
47   add_custom_command(
48     TARGET ${target_name}
49     POST_BUILD
50     COMMAND $<TARGET_FILE:${target_name}>
51   )
52   add_dependencies(libc-benchmark-util-tests ${target_name})
53 endfunction()
55 #==============================================================================
56 # Build Google Benchmark for libc
57 #==============================================================================
59 include(ExternalProject)
60 ExternalProject_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
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>
86         )
88 add_custom_target(libc-benchmark-util-tests)
90 # libc-benchmark
91 add_library(libc-benchmark
92     STATIC
93     EXCLUDE_FROM_ALL
94     LibcBenchmark.cpp
95     LibcBenchmark.h
98 target_include_directories(libc-benchmark
99     PUBLIC ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR}
101 target_link_libraries(libc-benchmark
102     PUBLIC
103     benchmark::benchmark
104     LLVMSupport
105     LLVMTargetParser
106     Threads::Threads
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
118     STATIC
119     EXCLUDE_FROM_ALL
120     LibcMemoryBenchmark.cpp
121     LibcMemoryBenchmark.h
122     LibcFunctionPrototypes.h
123     MemorySizeDistributions.cpp
124     MemorySizeDistributions.h
126 target_include_directories(libc-memory-benchmark
127     PUBLIC
128     ${CMAKE_CURRENT_SOURCE_DIR}
129     ${LIBC_SOURCE_DIR}
131 target_link_libraries(libc-memory-benchmark
132     PUBLIC
133     libc-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
142 # json
143 add_library(json
144     STATIC
145     EXCLUDE_FROM_ALL
146     JSON.cpp
147     JSON.h
149 target_link_libraries(json PUBLIC libc-memory-benchmark)
150 llvm_update_compile_flags(json)
152 add_libc_benchmark_unittest(json-test
153     SRCS JSONTest.cpp
154     DEPENDS json
157 #==============================================================================
158 # Benchmarking tool
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}")
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)
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})
178     else()
179       message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
180     endif()
181   endforeach()
182 endfunction()
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
199   EXCLUDE_FROM_ALL
200   LibcMemoryGoogleBenchmarkMain.cpp
201   LibcDefaultImplementations.cpp
203 target_link_libraries(libc.benchmarks.memory_functions.opt_host
204   PRIVATE
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__
212   benchmark_main
214 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
216 add_subdirectory(automemcpy)