blob: 2f06836b0fadd3f5c1e2c8d878eeeb00153e2f96 [file] [log] [blame]
Guillaume Chateletaba80d02020-01-06 12:17:041find_package(Threads)
2
3include(ExternalProject)
4
5set(LLVM_LINK_COMPONENTS Support)
6
7#==============================================================================
8# Build Google Benchmark
9#==============================================================================
10set(GOOGLE_BENCHMARK_TARGET_FLAGS ${BENCHMARK_DIALECT_FLAG})
11if (LIBCXX_BENCHMARK_GCC_TOOLCHAIN)
12 set(GOOGLE_BENCHMARK_TARGET_FLAGS
Fangrui Song40aab042021-08-20 22:24:5813 --gcc-toolchain=${LIBCXX_BENCHMARK_GCC_TOOLCHAIN})
Guillaume Chateletaba80d02020-01-06 12:17:0414endif()
15string(REPLACE ";" " " GOOGLE_BENCHMARK_TARGET_FLAGS "${GOOGLE_BENCHMARK_TARGET_FLAGS}")
16
17ExternalProject_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 Chateletdeae7e92020-12-17 13:16:1423 -DBUILD_SHARED_LIBS:BOOL=OFF
Guillaume Chateletaba80d02020-01-06 12:17:0424 -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 Reddy1852af12020-04-28 21:00:1228 -DCMAKE_CXX_STANDARD:STRING=14
Guillaume Chateletaba80d02020-01-06 12:17:0429 -DCMAKE_BUILD_TYPE:STRING=RELEASE
30 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
31 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
32
33set(GOOGLE_BENCHMARK_LIBC_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark)
34set(GOOGLE_BENCHMARK_LINK_FLAGS -L${GOOGLE_BENCHMARK_LIBC_INSTALL}/lib/)
35
36#==============================================================================
37# Add Unit Testing Support
38#==============================================================================
39
40function(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 Chateletaba80d02020-01-06 12:17:0457 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 Tothd80715d2020-05-06 08:21:3869 add_dependencies(libc-benchmark-util-tests ${target_name})
Guillaume Chateletaba80d02020-01-06 12:17:0470endfunction()
71
72#==============================================================================
73# Build Google Benchmark for libc
74#==============================================================================
75
Paula Tothd80715d2020-05-06 08:21:3876add_custom_target(libc-benchmark-util-tests)
Guillaume Chateletaba80d02020-01-06 12:17:0477
78function(fix_rtti target)
79 # TODO: Make this portable and inline with rtti mode from llvm/
80 target_compile_options(${target} PUBLIC -fno-rtti)
81endfunction()
82
83# libc-benchmark
84add_library(libc-benchmark
85 STATIC
86 EXCLUDE_FROM_ALL
87 LibcBenchmark.cpp
88 LibcBenchmark.h
89)
90add_dependencies(libc-benchmark google-benchmark)
Guillaume Chateletc2dcdf92020-01-27 09:55:3291target_include_directories(libc-benchmark
Guillaume Chateletcba533f2020-10-14 15:26:1792 SYSTEM PUBLIC
Guillaume Chateletc2dcdf92020-01-27 09:55:3293 "${GOOGLE_BENCHMARK_LIBC_INSTALL}/include"
94)
95target_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 Chateletaba80d02020-01-06 12:17:04102fix_rtti(libc-benchmark)
103
104add_libc_benchmark_unittest(libc-benchmark-test
105 SRCS LibcBenchmarkTest.cpp
106 DEPENDS libc-benchmark
107)
108
109# libc-memory-benchmark
110add_library(libc-memory-benchmark
111 STATIC
112 EXCLUDE_FROM_ALL
113 LibcMemoryBenchmark.cpp
114 LibcMemoryBenchmark.h
Guillaume Chateletc400e012020-10-15 08:01:26115 MemorySizeDistributions.cpp
116 MemorySizeDistributions.h
Guillaume Chateletaba80d02020-01-06 12:17:04117)
Guillaume Chateletdeae7e92020-12-17 13:16:14118target_link_libraries(libc-memory-benchmark
119 PUBLIC
120 libc-benchmark
121)
Guillaume Chateletaba80d02020-01-06 12:17:04122fix_rtti(libc-memory-benchmark)
123
124add_libc_benchmark_unittest(libc-memory-benchmark-test
125 SRCS LibcMemoryBenchmarkTest.cpp
126 DEPENDS libc-memory-benchmark
127)
128
129# json
130add_library(json
131 STATIC
132 EXCLUDE_FROM_ALL
133 JSON.cpp
134 JSON.h
135)
136target_link_libraries(json PUBLIC libc-memory-benchmark)
137fix_rtti(json)
138
139add_libc_benchmark_unittest(json-test
140 SRCS JSONTest.cpp
141 DEPENDS json
142)
143
144#==============================================================================
Guillaume Chateletdeae7e92020-12-17 13:16:14145# Benchmarking tool
Guillaume Chateletaba80d02020-01-06 12:17:04146#==============================================================================
147
Guillaume Chatelet8d64ed82021-06-10 13:04:56148# Benchmark all implementations that can run on the target CPU.
149function(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 Chateletd3c70d92021-07-28 14:52:29163 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=__llvm_libc::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
Guillaume Chatelet8d64ed82021-06-10 13:04:56164 else()
165 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
166 endif()
167 endforeach()
168endfunction()
169
170add_libc_multi_impl_benchmark(memcpy)
171add_libc_multi_impl_benchmark(memset)
Guillaume Chatelet87065c02021-06-23 14:19:40172add_libc_multi_impl_benchmark(bzero)
173add_libc_multi_impl_benchmark(memcmp)
Guillaume Chateletc8f79892021-08-19 17:55:16174add_libc_multi_impl_benchmark(bcmp)
Guillaume Chatelet59198d02021-08-02 12:14:11175
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.
183add_executable(libc.benchmarks.memory_functions.opt_host
184 EXCLUDE_FROM_ALL
185 LibcMemoryGoogleBenchmarkMain.cpp
186)
187
188target_link_libraries(libc.benchmarks.memory_functions.opt_host
189 PRIVATE
190 libc-memory-benchmark
191 libc.src.string.memcmp_opt_host
Guillaume Chateletc8f79892021-08-19 17:55:16192 libc.src.string.bcmp_opt_host
Guillaume Chatelet59198d02021-08-02 12:14:11193 libc.src.string.memcpy_opt_host
194 libc.src.string.memset_opt_host
195 libc.src.string.bzero_opt_host
196 benchmark_main
197)