[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / benchmarks / CMakeLists.txt
blob3d665ce1c6e71984e57bf298606aa27b52303415
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_link_libraries(libc-benchmark
89     PUBLIC
90     benchmark::benchmark
91     LLVMSupport
92     LLVMTargetParser
93     Threads::Threads
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
105     STATIC
106     EXCLUDE_FROM_ALL
107     LibcMemoryBenchmark.cpp
108     LibcMemoryBenchmark.h
109     LibcFunctionPrototypes.h
110     MemorySizeDistributions.cpp
111     MemorySizeDistributions.h
113 target_include_directories(libc-memory-benchmark
114     PUBLIC
115     ${CMAKE_CURRENT_SOURCE_DIR}
117 target_link_libraries(libc-memory-benchmark
118     PUBLIC
119     libc-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
128 # json
129 add_library(json
130     STATIC
131     EXCLUDE_FROM_ALL
132     JSON.cpp
133     JSON.h
135 target_link_libraries(json PUBLIC libc-memory-benchmark)
136 llvm_update_compile_flags(json)
138 add_libc_benchmark_unittest(json-test
139     SRCS JSONTest.cpp
140     DEPENDS json
143 #==============================================================================
144 # Benchmarking tool
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}")
153     if(can_run)
154         set(benchmark_name ${fq_config_name}_benchmark)
155         add_executable(${benchmark_name}
156             EXCLUDE_FROM_ALL
157             LibcMemoryBenchmarkMain.cpp
158         )
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})
164     else()
165       message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
166     endif()
167   endforeach()
168 endfunction()
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
185   EXCLUDE_FROM_ALL
186   LibcMemoryGoogleBenchmarkMain.cpp
187   LibcDefaultImplementations.cpp
189 target_link_libraries(libc.benchmarks.memory_functions.opt_host
190   PRIVATE
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__
198   benchmark_main
200 llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
202 add_subdirectory(automemcpy)