217 lines · plain
1if(LIBC_TARGET_OS_IS_GPU)2 add_subdirectory(gpu)3 return()4endif()5 6# The CPU build depends on Google benchmark.7if(NOT LIBC_INCLUDE_BENCHMARKS)8 return()9endif()10 11find_package(Threads)12 13set(LLVM_LINK_COMPONENTS14 Support15 TargetParser16 )17 18#==============================================================================19# Add Unit Testing Support20#==============================================================================21 22make_gtest_target()23 24function(add_libc_benchmark_unittest target_name)25 if(NOT LLVM_INCLUDE_TESTS)26 return()27 endif()28 29 cmake_parse_arguments(30 "LIBC_BENCHMARKS_UNITTEST"31 "" # No optional arguments32 "SUITE" # Single value arguments33 "SRCS;DEPENDS" # Multi-value arguments34 ${ARGN}35 )36 37 add_executable(${target_name}38 EXCLUDE_FROM_ALL39 ${LIBC_BENCHMARKS_UNITTEST_SRCS}40 )41 target_link_libraries(${target_name}42 PRIVATE43 default_gtest_main44 default_gtest45 ${LIBC_BENCHMARKS_UNITTEST_DEPENDS}46 )47 llvm_update_compile_flags(${target_name})48 49 add_custom_command(50 TARGET ${target_name}51 POST_BUILD52 COMMAND $<TARGET_FILE:${target_name}>53 )54 add_dependencies(libc-benchmark-util-tests ${target_name})55endfunction()56 57#==============================================================================58# Build Google Benchmark for libc59#==============================================================================60 61include(ExternalProject)62ExternalProject_Add(google-benchmark-libc63 EXCLUDE_FROM_ALL ON64 PREFIX google-benchmark-libc65 SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark66 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-libc67 CMAKE_CACHE_ARGS68 -DBENCHMARK_ENABLE_EXCEPTIONS:BOOL=OFF69 -DBENCHMARK_ENABLE_LTO:BOOL=OFF70 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF71 -DBENCHMARK_ENABLE_WERROR:BOOL=${LLVM_ENABLE_WERROR}72 -DBENCHMARK_FORCE_WERROR:BOOL=OFF73 -DBENCHMARK_USE_LIBCXX:BOOL=OFF74 -DCMAKE_BUILD_TYPE:STRING=Release75 76 -DCMAKE_SYSTEM_NAME:STRING=${CMAKE_SYSTEM_NAME}77 -DCMAKE_SYSTEM_PROCESSOR:STRING=${CMAKE_SYSTEM_PROCESSOR}78 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}79 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}80 -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}81 -DCMAKE_FIND_ROOT_PATH:STRING=${CMAKE_FIND_ROOT_PATH}82 83 -DBUILD_SHARED_LIBS:BOOL=OFF84 -DCMAKE_EXE_LINKER_FLAGS:STRING=-static85 86 -DCMAKE_CXX_STANDARD:STRING=1487 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>88 )89 90add_custom_target(libc-benchmark-util-tests)91 92# libc-benchmark93add_library(libc-benchmark94 STATIC95 EXCLUDE_FROM_ALL96 LibcBenchmark.cpp97 LibcBenchmark.h98)99 100target_include_directories(libc-benchmark101 PUBLIC ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR}102)103target_link_libraries(libc-benchmark104 PUBLIC105 benchmark::benchmark106 LLVMSupport107 LLVMTargetParser108 Threads::Threads109)110add_dependencies(libc-benchmark google-benchmark-libc)111llvm_update_compile_flags(libc-benchmark)112 113add_libc_benchmark_unittest(libc-benchmark-test114 SRCS LibcBenchmarkTest.cpp115 DEPENDS libc-benchmark116)117 118# libc-memory-benchmark119add_library(libc-memory-benchmark120 STATIC121 EXCLUDE_FROM_ALL122 LibcMemoryBenchmark.cpp123 LibcMemoryBenchmark.h124 LibcFunctionPrototypes.h125 MemorySizeDistributions.cpp126 MemorySizeDistributions.h127)128target_include_directories(libc-memory-benchmark129 PUBLIC130 ${CMAKE_CURRENT_SOURCE_DIR}131 ${LIBC_SOURCE_DIR}132)133target_link_libraries(libc-memory-benchmark134 PUBLIC135 libc-benchmark136)137llvm_update_compile_flags(libc-memory-benchmark)138 139add_libc_benchmark_unittest(libc-memory-benchmark-test140 SRCS LibcMemoryBenchmarkTest.cpp141 DEPENDS libc-memory-benchmark142)143 144# json145add_library(json146 STATIC147 EXCLUDE_FROM_ALL148 JSON.cpp149 JSON.h150)151target_link_libraries(json PUBLIC libc-memory-benchmark)152llvm_update_compile_flags(json)153 154add_libc_benchmark_unittest(json-test155 SRCS JSONTest.cpp156 DEPENDS json157)158 159#==============================================================================160# Benchmarking tool161#==============================================================================162 163# Benchmark all implementations that can run on the target CPU.164function(add_libc_multi_impl_benchmark name)165 get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)166 foreach(fq_config_name IN LISTS fq_implementations)167 get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)168 cpu_supports(can_run "${required_cpu_features}")169 if(can_run)170 set(benchmark_name ${fq_config_name}_benchmark)171 add_executable(${benchmark_name}172 EXCLUDE_FROM_ALL173 LibcMemoryBenchmarkMain.cpp174 )175 get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")176 target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})177 string(TOUPPER ${name} name_upper)178 target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")179 llvm_update_compile_flags(${benchmark_name})180 else()181 message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")182 endif()183 endforeach()184endfunction()185 186add_libc_multi_impl_benchmark(bcmp)187add_libc_multi_impl_benchmark(bzero)188add_libc_multi_impl_benchmark(memcmp)189add_libc_multi_impl_benchmark(memcpy)190add_libc_multi_impl_benchmark(memmove)191add_libc_multi_impl_benchmark(memset)192 193#==============================================================================194# Google Benchmarking tool195#==============================================================================196 197# This target uses the Google Benchmark facility to report throughput for llvm198# libc memory functions compiled for the host machine. This is useful to199# continuously monitor the performance of the memory functions.200add_executable(libc.benchmarks.memory_functions.opt_host201 EXCLUDE_FROM_ALL202 LibcMemoryGoogleBenchmarkMain.cpp203 LibcDefaultImplementations.cpp204)205target_link_libraries(libc.benchmarks.memory_functions.opt_host206 PRIVATE207 libc-memory-benchmark208 libc.src.string.memcmp_opt_host.__internal__209 libc.src.string.memcpy_opt_host.__internal__210 libc.src.string.memmove_opt_host.__internal__211 libc.src.string.memset_opt_host.__internal__212 libc.src.strings.bcmp_opt_host.__internal__213 libc.src.strings.bzero_opt_host.__internal__214 benchmark_main215)216llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)217