Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / benchmarks / CMakeLists.txt
blob4978da65850ccba9ac601eb671c6c30794e75185
1 find_package(Threads)
3 set(LLVM_LINK_COMPONENTS
4   Support
5   TargetParser
6   )
8 #==============================================================================
9 # Add Unit Testing Support
10 #==============================================================================
12 function(add_libc_benchmark_unittest target_name)
13   if(NOT LLVM_INCLUDE_TESTS)
14     return()
15   endif()
17   cmake_parse_arguments(
18     "LIBC_BENCHMARKS_UNITTEST"
19     "" # No optional arguments
20     "SUITE" # Single value arguments
21     "SRCS;DEPENDS" # Multi-value arguments
22     ${ARGN}
23   )
25   add_executable(${target_name}
26     EXCLUDE_FROM_ALL
27     ${LIBC_BENCHMARKS_UNITTEST_SRCS}
28   )
29   target_link_libraries(${target_name}
30     PRIVATE
31     llvm_gtest_main
32     llvm_gtest
33     ${LIBC_BENCHMARKS_UNITTEST_DEPENDS}
34   )
35   llvm_update_compile_flags(${target_name})
37   add_custom_command(
38     TARGET ${target_name}
39     POST_BUILD
40     COMMAND $<TARGET_FILE:${target_name}>
41   )
42   add_dependencies(libc-benchmark-util-tests ${target_name})
43 endfunction()
45 #==============================================================================
46 # Build Google Benchmark for libc
47 #==============================================================================
49 include(ExternalProject)
50 ExternalProject_Add(google-benchmark-libc
51         EXCLUDE_FROM_ALL ON
52         PREFIX google-benchmark-libc
53         SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
54         INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-libc
55         CMAKE_CACHE_ARGS
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>
76         )
78 add_custom_target(libc-benchmark-util-tests)
80 # libc-benchmark
81 add_library(libc-benchmark
82     STATIC
83     EXCLUDE_FROM_ALL
84     LibcBenchmark.cpp
85     LibcBenchmark.h
88 target_include_directories(libc-benchmark
89     PUBLIC ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR}
91 target_link_libraries(libc-benchmark
92     PUBLIC
93     benchmark::benchmark
94     LLVMSupport
95     LLVMTargetParser
96     Threads::Threads
98 add_dependencies(libc-benchmark google-benchmark-libc)
99 llvm_update_compile_flags(libc-benchmark)
101 add_libc_benchmark_unittest(libc-benchmark-test
102     SRCS LibcBenchmarkTest.cpp
103     DEPENDS libc-benchmark
106 # libc-memory-benchmark
107 add_library(libc-memory-benchmark
108     STATIC
109     EXCLUDE_FROM_ALL
110     LibcMemoryBenchmark.cpp
111     LibcMemoryBenchmark.h
112     LibcFunctionPrototypes.h
113     MemorySizeDistributions.cpp
114     MemorySizeDistributions.h
116 target_include_directories(libc-memory-benchmark
117     PUBLIC
118     ${CMAKE_CURRENT_SOURCE_DIR}
120 target_link_libraries(libc-memory-benchmark
121     PUBLIC
122     libc-benchmark
124 llvm_update_compile_flags(libc-memory-benchmark)
126 add_libc_benchmark_unittest(libc-memory-benchmark-test
127     SRCS LibcMemoryBenchmarkTest.cpp
128     DEPENDS libc-memory-benchmark
131 # json
132 add_library(json
133     STATIC
134     EXCLUDE_FROM_ALL
135     JSON.cpp
136     JSON.h
138 target_link_libraries(json PUBLIC libc-memory-benchmark)
139 llvm_update_compile_flags(json)
141 add_libc_benchmark_unittest(json-test
142     SRCS JSONTest.cpp
143     DEPENDS json
146 #==============================================================================
147 # Benchmarking tool
148 #==============================================================================
150 # Benchmark all implementations that can run on the target CPU.
151 function(add_libc_multi_impl_benchmark name)
152   get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
153   foreach(fq_config_name IN LISTS fq_implementations)
154     get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
155     cpu_supports(can_run "${required_cpu_features}")
156     if(can_run)
157         set(benchmark_name ${fq_config_name}_benchmark)
158         add_executable(${benchmark_name}
159             EXCLUDE_FROM_ALL
160             LibcMemoryBenchmarkMain.cpp
161         )
162         get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")
163         target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})
164         string(TOUPPER ${name} name_upper)
165         target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
166         llvm_update_compile_flags(${benchmark_name})
167     else()
168       message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
169     endif()
170   endforeach()
171 endfunction()
173 add_libc_multi_impl_benchmark(bcmp)
174 add_libc_multi_impl_benchmark(bzero)
175 add_libc_multi_impl_benchmark(memcmp)
176 add_libc_multi_impl_benchmark(memcpy)
177 add_libc_multi_impl_benchmark(memmove)
178 add_libc_multi_impl_benchmark(memset)
180 #==============================================================================
181 # Google Benchmarking tool
182 #==============================================================================
184 # This target uses the Google Benchmark facility to report throughput for llvm
185 # libc memory functions compiled for the host machine. This is useful to
186 # continuously monitor the performance of the memory functions.
187 add_executable(libc.benchmarks.memory_functions.opt_host
188   EXCLUDE_FROM_ALL
189   LibcMemoryGoogleBenchmarkMain.cpp
190   LibcDefaultImplementations.cpp
192 target_link_libraries(libc.benchmarks.memory_functions.opt_host
193   PRIVATE
194   libc-memory-benchmark
195   libc.src.string.memcmp_opt_host.__internal__
196   libc.src.string.bcmp_opt_host.__internal__
197   libc.src.string.memcpy_opt_host.__internal__
198   libc.src.string.memset_opt_host.__internal__
199   libc.src.string.bzero_opt_host.__internal__
200   libc.src.string.memmove_opt_host.__internal__
201   benchmark_main
203 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
205 add_subdirectory(automemcpy)