Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 1 | find_package(Threads) |
| 2 | |
| 3 | include(ExternalProject) |
| 4 | |
| 5 | set(LLVM_LINK_COMPONENTS Support) |
| 6 | |
| 7 | #============================================================================== |
| 8 | # Build Google Benchmark |
| 9 | #============================================================================== |
| 10 | set(GOOGLE_BENCHMARK_TARGET_FLAGS ${BENCHMARK_DIALECT_FLAG}) |
| 11 | if (LIBCXX_BENCHMARK_GCC_TOOLCHAIN) |
| 12 | set(GOOGLE_BENCHMARK_TARGET_FLAGS |
Fangrui Song | 40aab04 | 2021-08-20 22:24:58 | [diff] [blame^] | 13 | --gcc-toolchain=${LIBCXX_BENCHMARK_GCC_TOOLCHAIN}) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 14 | endif() |
| 15 | string(REPLACE ";" " " GOOGLE_BENCHMARK_TARGET_FLAGS "${GOOGLE_BENCHMARK_TARGET_FLAGS}") |
| 16 | |
| 17 | ExternalProject_Add(google-benchmark |
| 18 | EXCLUDE_FROM_ALL ON |
| 19 | PREFIX google-benchmark |
| 20 | SOURCE_DIR ${LIBC_SOURCE_DIR}/../llvm/utils/benchmark |
| 21 | INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark |
| 22 | CMAKE_CACHE_ARGS |
Guillaume Chatelet | deae7e9 | 2020-12-17 13:16:14 | [diff] [blame] | 23 | -DBUILD_SHARED_LIBS:BOOL=OFF |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 24 | -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON |
| 25 | -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} |
| 26 | -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} |
| 27 | -DCMAKE_CXX_FLAGS:STRING=${GOOGLE_BENCHMARK_TARGET_FLAGS} |
Siva Chandra Reddy | 1852af1 | 2020-04-28 21:00:12 | [diff] [blame] | 28 | -DCMAKE_CXX_STANDARD:STRING=14 |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 29 | -DCMAKE_BUILD_TYPE:STRING=RELEASE |
| 30 | -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> |
| 31 | -DBENCHMARK_ENABLE_TESTING:BOOL=OFF) |
| 32 | |
| 33 | set(GOOGLE_BENCHMARK_LIBC_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark) |
| 34 | set(GOOGLE_BENCHMARK_LINK_FLAGS -L${GOOGLE_BENCHMARK_LIBC_INSTALL}/lib/) |
| 35 | |
| 36 | #============================================================================== |
| 37 | # Add Unit Testing Support |
| 38 | #============================================================================== |
| 39 | |
| 40 | function(add_libc_benchmark_unittest target_name) |
| 41 | if(NOT LLVM_INCLUDE_TESTS) |
| 42 | return() |
| 43 | endif() |
| 44 | |
| 45 | cmake_parse_arguments( |
| 46 | "LIBC_BENCHMARKS_UNITTEST" |
| 47 | "" # No optional arguments |
| 48 | "SUITE" # Single value arguments |
| 49 | "SRCS;DEPENDS" # Multi-value arguments |
| 50 | ${ARGN} |
| 51 | ) |
| 52 | |
| 53 | add_executable(${target_name} |
| 54 | EXCLUDE_FROM_ALL |
| 55 | ${LIBC_BENCHMARKS_UNITTEST_SRCS} |
| 56 | ) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 57 | target_link_libraries(${target_name} |
| 58 | PRIVATE |
| 59 | gtest_main |
| 60 | gtest |
| 61 | ${LIBC_BENCHMARKS_UNITTEST_DEPENDS} |
| 62 | ) |
| 63 | |
| 64 | add_custom_command( |
| 65 | TARGET ${target_name} |
| 66 | POST_BUILD |
| 67 | COMMAND $<TARGET_FILE:${target_name}> |
| 68 | ) |
Paula Toth | d80715d | 2020-05-06 08:21:38 | [diff] [blame] | 69 | add_dependencies(libc-benchmark-util-tests ${target_name}) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 70 | endfunction() |
| 71 | |
| 72 | #============================================================================== |
| 73 | # Build Google Benchmark for libc |
| 74 | #============================================================================== |
| 75 | |
Paula Toth | d80715d | 2020-05-06 08:21:38 | [diff] [blame] | 76 | add_custom_target(libc-benchmark-util-tests) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 77 | |
| 78 | function(fix_rtti target) |
| 79 | # TODO: Make this portable and inline with rtti mode from llvm/ |
| 80 | target_compile_options(${target} PUBLIC -fno-rtti) |
| 81 | endfunction() |
| 82 | |
| 83 | # libc-benchmark |
| 84 | add_library(libc-benchmark |
| 85 | STATIC |
| 86 | EXCLUDE_FROM_ALL |
| 87 | LibcBenchmark.cpp |
| 88 | LibcBenchmark.h |
| 89 | ) |
| 90 | add_dependencies(libc-benchmark google-benchmark) |
Guillaume Chatelet | c2dcdf9 | 2020-01-27 09:55:32 | [diff] [blame] | 91 | target_include_directories(libc-benchmark |
Guillaume Chatelet | cba533f | 2020-10-14 15:26:17 | [diff] [blame] | 92 | SYSTEM PUBLIC |
Guillaume Chatelet | c2dcdf9 | 2020-01-27 09:55:32 | [diff] [blame] | 93 | "${GOOGLE_BENCHMARK_LIBC_INSTALL}/include" |
| 94 | ) |
| 95 | target_link_libraries(libc-benchmark |
| 96 | PUBLIC |
| 97 | "${GOOGLE_BENCHMARK_LINK_FLAGS}" # FIXME: Move to `target_link_options` |
| 98 | -lbenchmark # FIXME: Move to `target_link_options` |
| 99 | LLVMSupport |
| 100 | Threads::Threads |
| 101 | ) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 102 | fix_rtti(libc-benchmark) |
| 103 | |
| 104 | add_libc_benchmark_unittest(libc-benchmark-test |
| 105 | SRCS LibcBenchmarkTest.cpp |
| 106 | DEPENDS libc-benchmark |
| 107 | ) |
| 108 | |
| 109 | # libc-memory-benchmark |
| 110 | add_library(libc-memory-benchmark |
| 111 | STATIC |
| 112 | EXCLUDE_FROM_ALL |
| 113 | LibcMemoryBenchmark.cpp |
| 114 | LibcMemoryBenchmark.h |
Guillaume Chatelet | c400e01 | 2020-10-15 08:01:26 | [diff] [blame] | 115 | MemorySizeDistributions.cpp |
| 116 | MemorySizeDistributions.h |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 117 | ) |
Guillaume Chatelet | deae7e9 | 2020-12-17 13:16:14 | [diff] [blame] | 118 | target_link_libraries(libc-memory-benchmark |
| 119 | PUBLIC |
| 120 | libc-benchmark |
| 121 | ) |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 122 | fix_rtti(libc-memory-benchmark) |
| 123 | |
| 124 | add_libc_benchmark_unittest(libc-memory-benchmark-test |
| 125 | SRCS LibcMemoryBenchmarkTest.cpp |
| 126 | DEPENDS libc-memory-benchmark |
| 127 | ) |
| 128 | |
| 129 | # json |
| 130 | add_library(json |
| 131 | STATIC |
| 132 | EXCLUDE_FROM_ALL |
| 133 | JSON.cpp |
| 134 | JSON.h |
| 135 | ) |
| 136 | target_link_libraries(json PUBLIC libc-memory-benchmark) |
| 137 | fix_rtti(json) |
| 138 | |
| 139 | add_libc_benchmark_unittest(json-test |
| 140 | SRCS JSONTest.cpp |
| 141 | DEPENDS json |
| 142 | ) |
| 143 | |
| 144 | #============================================================================== |
Guillaume Chatelet | deae7e9 | 2020-12-17 13:16:14 | [diff] [blame] | 145 | # Benchmarking tool |
Guillaume Chatelet | aba80d0 | 2020-01-06 12:17:04 | [diff] [blame] | 146 | #============================================================================== |
| 147 | |
Guillaume Chatelet | 8d64ed8 | 2021-06-10 13:04:56 | [diff] [blame] | 148 | # Benchmark all implementations that can run on the target CPU. |
| 149 | function(add_libc_multi_impl_benchmark name) |
| 150 | get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations) |
| 151 | foreach(fq_config_name IN LISTS fq_implementations) |
| 152 | get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES) |
| 153 | cpu_supports(can_run "${required_cpu_features}") |
| 154 | if(can_run) |
| 155 | set(benchmark_name ${fq_config_name}_benchmark) |
| 156 | add_executable(${benchmark_name} |
| 157 | EXCLUDE_FROM_ALL |
| 158 | LibcMemoryBenchmarkMain.cpp |
| 159 | ) |
| 160 | get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW") |
| 161 | target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file}) |
| 162 | string(TOUPPER ${name} name_upper) |
Guillaume Chatelet | d3c70d9 | 2021-07-28 14:52:29 | [diff] [blame] | 163 | target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=__llvm_libc::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"") |
Guillaume Chatelet | 8d64ed8 | 2021-06-10 13:04:56 | [diff] [blame] | 164 | else() |
| 165 | message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'") |
| 166 | endif() |
| 167 | endforeach() |
| 168 | endfunction() |
| 169 | |
| 170 | add_libc_multi_impl_benchmark(memcpy) |
| 171 | add_libc_multi_impl_benchmark(memset) |
Guillaume Chatelet | 87065c0 | 2021-06-23 14:19:40 | [diff] [blame] | 172 | add_libc_multi_impl_benchmark(bzero) |
| 173 | add_libc_multi_impl_benchmark(memcmp) |
Guillaume Chatelet | c8f7989 | 2021-08-19 17:55:16 | [diff] [blame] | 174 | add_libc_multi_impl_benchmark(bcmp) |
Guillaume Chatelet | 59198d0 | 2021-08-02 12:14:11 | [diff] [blame] | 175 | |
| 176 | #============================================================================== |
| 177 | # Google Benchmarking tool |
| 178 | #============================================================================== |
| 179 | |
| 180 | # This target uses the Google Benchmark facility to report throughput for llvm |
| 181 | # libc memory functions compiled for the host machine. This is useful to |
| 182 | # continuously monitor the performance of the memory functions. |
| 183 | add_executable(libc.benchmarks.memory_functions.opt_host |
| 184 | EXCLUDE_FROM_ALL |
| 185 | LibcMemoryGoogleBenchmarkMain.cpp |
| 186 | ) |
| 187 | |
| 188 | target_link_libraries(libc.benchmarks.memory_functions.opt_host |
| 189 | PRIVATE |
| 190 | libc-memory-benchmark |
| 191 | libc.src.string.memcmp_opt_host |
Guillaume Chatelet | c8f7989 | 2021-08-19 17:55:16 | [diff] [blame] | 192 | libc.src.string.bcmp_opt_host |
Guillaume Chatelet | 59198d0 | 2021-08-02 12:14:11 | [diff] [blame] | 193 | libc.src.string.memcpy_opt_host |
| 194 | libc.src.string.memset_opt_host |
| 195 | libc.src.string.bzero_opt_host |
| 196 | benchmark_main |
| 197 | ) |