Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / benchmarks / CMakeLists.txt
blob7591f34d938bf863f19b0b8f2743a82d40531b41
1 include(ExternalProject)
2 include(CheckCXXCompilerFlag)
4 #==============================================================================
5 # Build Google Benchmark for libc++
6 #==============================================================================
8 set(CMAKE_FOLDER "${CMAKE_FOLDER}/Benchmarks")
10 set(BENCHMARK_LIBCXX_COMPILE_FLAGS
11     -Wno-unused-command-line-argument
12     -nostdinc++
13     -isystem "${LIBCXX_GENERATED_INCLUDE_DIR}"
14     -L${LIBCXX_LIBRARY_DIR}
15     -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
16     ${SANITIZER_FLAGS}
17     )
18 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
19   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
20     -isystem "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}")
21 endif()
22 if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH)
23   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
24           -L${LIBCXX_CXX_ABI_LIBRARY_PATH}
25           -Wl,-rpath,${LIBCXX_CXX_ABI_LIBRARY_PATH})
26 endif()
27 split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
29 ExternalProject_Add(google-benchmark-libcxx
30         EXCLUDE_FROM_ALL ON
31         DEPENDS cxx cxx-headers
32         PREFIX benchmark-libcxx
33         SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
34         INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx
35         CMAKE_CACHE_ARGS
36           -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
37           -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
38           -DCMAKE_BUILD_TYPE:STRING=RELEASE
39           -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
40           -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBCXX_COMPILE_FLAGS}
41           -DBENCHMARK_USE_LIBCXX:BOOL=ON
42           -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
44 #==============================================================================
45 # Build Google Benchmark for the native stdlib
46 #==============================================================================
47 set(BENCHMARK_NATIVE_TARGET_FLAGS)
48 if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN)
49   set(BENCHMARK_NATIVE_TARGET_FLAGS
50       --gcc-toolchain=${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN})
51 endif()
52 split_list(BENCHMARK_NATIVE_TARGET_FLAGS)
54 if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
55   ExternalProject_Add(google-benchmark-native
56         EXCLUDE_FROM_ALL ON
57         PREFIX benchmark-native
58         SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
59         INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native
60         CMAKE_CACHE_ARGS
61           -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
62           -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
63           -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_NATIVE_TARGET_FLAGS}
64           -DCMAKE_BUILD_TYPE:STRING=RELEASE
65           -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
66           -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
67 endif()
70 #==============================================================================
71 # Benchmark tests configuration
72 #==============================================================================
73 add_custom_target(cxx-benchmarks)
74 set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
75 set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx)
76 set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native)
78 add_library(               cxx-benchmarks-flags INTERFACE)
80 # TODO(cmake): remove. This is a workaround to prevent older versions of GCC
81 # from failing the configure step because they don't support C++23.
82 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "13.0")
83   return()
84 endif()
85 #TODO(cmake): remove the `add_compile_options`. Currently we have to explicitly
86 # pass the `std:c++latest` flag on Windows to work around an issue where
87 # requesting `cxx_std_23` results in an error -- somehow CMake fails to
88 # translate the `c++23` flag into `c++latest`, and the highest numbered C++
89 # version that MSVC flags support is C++20.
90 if (MSVC)
91   add_compile_options(/std:c++latest)
92 # ibm-clang does not recognize the cxx_std_23 flag, so use this as a temporary
93 # workaround on AIX as well.
94 elseif (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
95   add_compile_options(-std=c++23)
96 else()
97   target_compile_features( cxx-benchmarks-flags INTERFACE cxx_std_23)
98 endif()
100 target_compile_options(    cxx-benchmarks-flags INTERFACE -fsized-deallocation -nostdinc++)
101 target_include_directories(cxx-benchmarks-flags INTERFACE "${LIBCXX_GENERATED_INCLUDE_DIR}"
102                                                 INTERFACE "${BENCHMARK_LIBCXX_INSTALL}/include"
103                                                 INTERFACE "${LIBCXX_SOURCE_DIR}/test/support")
105 add_library(           cxx-benchmarks-flags-native INTERFACE)
106 target_link_libraries( cxx-benchmarks-flags-native INTERFACE cxx-benchmarks-flags)
107 target_compile_options(cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS})
108 target_link_options(   cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS} "-L${BENCHMARK_NATIVE_INSTALL}/lib")
109 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
110   find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs
111         PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}
112         PATH_SUFFIXES lib lib64
113         DOC "The libstdc++ filesystem library used by the benchmarks"
114     )
115   if (LIBSTDCXX_FILESYSTEM_TEST)
116     target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lstdc++fs)
117   endif()
118 else()
119   target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lc++fs -lc++experimental)
120 endif()
122 add_library(           cxx-benchmarks-flags-libcxx INTERFACE)
123 target_link_libraries( cxx-benchmarks-flags-libcxx INTERFACE cxx-benchmarks-flags)
124 target_compile_options(cxx-benchmarks-flags-libcxx INTERFACE ${SANITIZER_FLAGS} -Wno-user-defined-literals -Wno-suggest-override)
125 target_link_options(   cxx-benchmarks-flags-libcxx INTERFACE -nostdlib++ "-L${BENCHMARK_LIBCXX_INSTALL}/lib" "-L${BENCHMARK_LIBCXX_INSTALL}/lib64" ${SANITIZER_FLAGS})
127 set(libcxx_benchmark_targets)
129 function(add_benchmark_test name source_file)
130   set(libcxx_target ${name}_libcxx)
131   list(APPEND libcxx_benchmark_targets ${libcxx_target})
132   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
133   target_link_libraries(${libcxx_target} PRIVATE cxx-benchmarks-flags-libcxx)
134   add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
135   add_dependencies(cxx-benchmarks ${libcxx_target})
136   if (LIBCXX_ENABLE_SHARED)
137     target_link_libraries(${libcxx_target} PRIVATE cxx_shared)
138   else()
139     target_link_libraries(${libcxx_target} PRIVATE cxx_static)
140   endif()
141   target_link_libraries(${libcxx_target} PRIVATE cxx_experimental benchmark)
142   if (LLVM_USE_SANITIZER)
143     target_link_libraries(${libcxx_target} PRIVATE -ldl)
144   endif()
145   set_target_properties(${libcxx_target}
146     PROPERTIES
147           OUTPUT_NAME "${name}.libcxx.out"
148           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
149           CXX_EXTENSIONS NO)
150   cxx_link_system_libraries(${libcxx_target})
151   if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
152     set(native_target ${name}_native)
153     add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
154     target_link_libraries(${native_target} PRIVATE cxx-benchmarks-flags-native)
155     add_dependencies(${native_target} google-benchmark-native
156                                       google-benchmark-libcxx)
157     target_link_libraries(${native_target} PRIVATE -lbenchmark)
158     if (LIBCXX_HAS_PTHREAD_LIB)
159       target_link_libraries(${native_target} PRIVATE -pthread)
160     endif()
161     add_dependencies(cxx-benchmarks ${native_target})
162     set_target_properties(${native_target}
163       PROPERTIES
164           OUTPUT_NAME "${name}.native.out"
165           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
166           CXX_EXTENSIONS NO)
167   endif()
168 endfunction()
171 #==============================================================================
172 # Register Benchmark tests
173 #==============================================================================
174 set(BENCHMARK_TESTS
175     algorithms.partition_point.bench.cpp
176     algorithms/count.bench.cpp
177     algorithms/equal.bench.cpp
178     algorithms/find.bench.cpp
179     algorithms/lower_bound.bench.cpp
180     algorithms/make_heap.bench.cpp
181     algorithms/make_heap_then_sort_heap.bench.cpp
182     algorithms/min.bench.cpp
183     algorithms/min_max_element.bench.cpp
184     algorithms/pop_heap.bench.cpp
185     algorithms/pstl.stable_sort.bench.cpp
186     algorithms/push_heap.bench.cpp
187     algorithms/ranges_ends_with.bench.cpp
188     algorithms/ranges_make_heap.bench.cpp
189     algorithms/ranges_make_heap_then_sort_heap.bench.cpp
190     algorithms/ranges_pop_heap.bench.cpp
191     algorithms/ranges_push_heap.bench.cpp
192     algorithms/ranges_sort.bench.cpp
193     algorithms/ranges_sort_heap.bench.cpp
194     algorithms/ranges_stable_sort.bench.cpp
195     algorithms/sort.bench.cpp
196     algorithms/sort_heap.bench.cpp
197     algorithms/stable_sort.bench.cpp
198     libcxxabi/dynamic_cast.bench.cpp
199     libcxxabi/dynamic_cast_old_stress.bench.cpp
200     allocation.bench.cpp
201     deque.bench.cpp
202     deque_iterator.bench.cpp
203     filesystem.bench.cpp
204     format_to_n.bench.cpp
205     format_to.bench.cpp
206     format.bench.cpp
207     formatted_size.bench.cpp
208     formatter_float.bench.cpp
209     formatter_int.bench.cpp
210     function.bench.cpp
211     join_view.bench.cpp
212     lexicographical_compare_three_way.bench.cpp
213     map.bench.cpp
214     monotonic_buffer.bench.cpp
215     ordered_set.bench.cpp
216     stop_token.bench.cpp
217     std_format_spec_string_unicode.bench.cpp
218     string.bench.cpp
219     stringstream.bench.cpp
220     system_error.bench.cpp
221     to_chars.bench.cpp
222     unordered_set_operations.bench.cpp
223     util_smartptr.bench.cpp
224     variant_visit_1.bench.cpp
225     variant_visit_2.bench.cpp
226     variant_visit_3.bench.cpp
227     vector_operations.bench.cpp
228     )
230 foreach(test_path ${BENCHMARK_TESTS})
231   get_filename_component(test_file "${test_path}" NAME)
232   string(REPLACE ".bench.cpp" "" test_name "${test_file}")
233   if (NOT DEFINED ${test_name}_REPORTED)
234     message(STATUS "Adding Benchmark: ${test_file}")
235     # Only report the adding of the benchmark once.
236     set(${test_name}_REPORTED ON CACHE INTERNAL "")
237   endif()
238   add_benchmark_test(${test_name} ${test_path})
239 endforeach()
241 if (LIBCXX_INCLUDE_TESTS)
242   include(AddLLVM)
244   if (NOT DEFINED LIBCXX_TEST_DEPS)
245     message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined")
246   endif()
248   configure_lit_site_cfg(
249           ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
250           ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
252   set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}")
254   add_lit_target(check-cxx-benchmarks
255           "Running libcxx benchmarks tests"
256           ${CMAKE_CURRENT_BINARY_DIR}
257           DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS}
258           ARGS ${BENCHMARK_LIT_ARGS})
259 endif()