Skip to content

Commit ad52b62

Browse files
alexreinkingardier
authored andcommitted
Make use of CMake 3.22 features (halide#6919)
* Remove AddCudaToTarget.cmake * Remove MakeShellPath.cmake * Use CheckLinkerFlag in TargetExportScript * Use DEPFILE for all generators * Use REQUIRED with find_program, where applicable * Use REQUIRED with find_library, where applicable * Use CMake 3.21 cache behavior in HalideTargetHelpers.cmake * Replace uses of get_filename_component with cmake_path * Rework BLAS detection in linear_algebra app * Drive-by: fix autotune_loop.sh install rule. * Fix CBLAS header in linear_algebra test_halide_blas
1 parent f7ab5d7 commit ad52b62

21 files changed

Lines changed: 177 additions & 282 deletions

File tree

CMakeLists.txt

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ enable_testing()
1313
# Make our custom helpers available throughout the project via include().
1414
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
1515
include(HalideGeneratorHelpers)
16-
include(MakeShellPath)
1716
include(CMakeDependentOption)
1817

1918
# Build Halide as a shared lib by default, but still honor command-line settings.
@@ -50,22 +49,22 @@ endif()
5049
# Build Halide with ccache if the package is present
5150
option(Halide_CCACHE_BUILD "Set to ON for a ccache enabled build" OFF)
5251
mark_as_advanced(Halide_CCACHE_BUILD)
52+
5353
if (Halide_CCACHE_BUILD)
54-
find_program(CCACHE_PROGRAM ccache)
55-
if (CCACHE_PROGRAM)
56-
# TODO: ccache recommends setting CCACHE_SLOPPINESS=pch_defines,time_macros to
57-
# enable precompiled header caching. Our timing found it slightly faster with
58-
# just CCACHE_SLOPPINESS=pch_defines, so that's what we're using. Maybe revisit
59-
# if issues occur (but we don't use any of the time macros so should be irrelevant).
60-
set(Halide_CCACHE_PARAMS CCACHE_CPP2=yes CCACHE_HASHDIR=yes CCACHE_SLOPPINESS=pch_defines
61-
CACHE STRING "Parameters to pass through to ccache")
62-
mark_as_advanced(Halide_CCACHE_PARAMS)
63-
set(CMAKE_C_COMPILER_LAUNCHER ${CMAKE_COMMAND} -E env ${Halide_CCACHE_PARAMS} ${CCACHE_PROGRAM})
64-
set(CMAKE_CXX_COMPILER_LAUNCHER ${CMAKE_COMMAND} -E env ${Halide_CCACHE_PARAMS} ${CCACHE_PROGRAM})
65-
message(STATUS "Enabling ccache usage for building.")
66-
else ()
67-
message(FATAL_ERROR "Unable to find the program ccache. Set Halide_CCACHE_BUILD to OFF")
68-
endif ()
54+
find_program(CCACHE_PROGRAM ccache REQUIRED)
55+
56+
# TODO: ccache recommends setting CCACHE_SLOPPINESS=pch_defines,time_macros to
57+
# enable precompiled header caching. Our timing found it slightly faster with
58+
# just CCACHE_SLOPPINESS=pch_defines, so that's what we're using. Maybe revisit
59+
# if issues occur (but we don't use any of the time macros so should be irrelevant).
60+
set(Halide_CCACHE_PARAMS CCACHE_CPP2=yes CCACHE_HASHDIR=yes CCACHE_SLOPPINESS=pch_defines
61+
CACHE STRING "Parameters to pass through to ccache")
62+
mark_as_advanced(Halide_CCACHE_PARAMS)
63+
64+
set(CMAKE_C_COMPILER_LAUNCHER ${CMAKE_COMMAND} -E env ${Halide_CCACHE_PARAMS} ${CCACHE_PROGRAM})
65+
set(CMAKE_CXX_COMPILER_LAUNCHER ${CMAKE_COMMAND} -E env ${Halide_CCACHE_PARAMS} ${CCACHE_PROGRAM})
66+
67+
message(STATUS "Enabling ccache usage for building.")
6968
endif ()
7069

7170
# Enable the SPIR-V target if requested (must declare before processing dependencies)

apps/HelloWasm/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ set(CMAKE_CXX_EXTENSIONS NO)
1010
find_package(Halide REQUIRED)
1111
set(halide_includes "$<TARGET_PROPERTY:Halide::Runtime,INTERFACE_INCLUDE_DIRECTORIES>")
1212

13-
find_program(EMCC emcc HINTS "$ENV{EMSDK}/upstream/emscripten")
14-
if (NOT EMCC)
15-
message(FATAL_ERROR "Could not find emscripten/emcc!")
16-
endif ()
13+
find_program(EMCC emcc REQUIRED HINTS "$ENV{EMSDK}/upstream/emscripten")
1714

1815
configure_file(index.html index.html COPYONLY)
1916

apps/linear_algebra/CMakeLists.txt

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,47 @@ set(CMAKE_CXX_EXTENSIONS NO)
1212
find_package(Halide REQUIRED)
1313

1414
# Find BLAS-es
15-
set(DEFAULT_BLAS "")
16-
set(BLAS_TARGETS "")
17-
set(BLAS_VENDORS OpenBLAS ATLAS Apple Generic)
18-
19-
# ATLAS is weird and has extra requirements
20-
find_library(CBLAS_LIBRARY cblas)
21-
set(ATLAS_EXTRA_LIBS ${CBLAS_LIBRARY})
15+
set(found_blases "")
16+
set(known_vendors OpenBLAS ATLAS Apple Intel10_64_dyn Generic)
2217

2318
message(STATUS "Checking for available CBLAS implementations")
24-
foreach (BLA_VENDOR IN LISTS BLAS_VENDORS)
19+
foreach (BLA_VENDOR IN LISTS known_vendors)
2520
find_package(BLAS QUIET)
26-
if (NOT BLAS_FOUND
27-
OR ("${BLA_VENDOR}" STREQUAL "ATLAS" AND NOT CBLAS_LIBRARY)
28-
OR ("${BLA_VENDOR}" STREQUAL "Generic" AND BLAS_TARGETS))
29-
message(STATUS "${BLA_VENDOR}: Missing")
30-
else ()
31-
list(APPEND BLAS_LIBRARIES ${${BLA_VENDOR}_EXTRA_LIBS})
32-
33-
message(STATUS "${BLA_VENDOR}: Found ${BLAS_LIBRARIES}")
34-
add_library(BLAS_${BLA_VENDOR} INTERFACE)
35-
add_library(${BLA_VENDOR}::${BLA_VENDOR} ALIAS BLAS_${BLA_VENDOR})
3621

37-
target_link_libraries(BLAS_${BLA_VENDOR} INTERFACE ${BLAS_LIBRARIES})
38-
target_link_options(BLAS_${BLA_VENDOR} INTERFACE ${BLAS_LINKER_FLAGS})
39-
target_include_directories(BLAS_${BLA_VENDOR} SYSTEM INTERFACE include) # Use CBlas header in our own tree.
22+
# Fail early if not found
23+
if (NOT BLAS_FOUND)
24+
message(STATUS "${BLA_VENDOR}: Missing")
25+
continue()
26+
endif ()
4027

41-
if (NOT DEFAULT_BLAS)
42-
set(DEFAULT_BLAS ${BLA_VENDOR}::${BLA_VENDOR})
28+
# ATLAS is weird and has extra requirements
29+
if (BLA_VENDOR STREQUAL "ATLAS")
30+
find_library(CBLAS_LIBRARY cblas)
31+
if (NOT CBLAS_LIBRARY)
32+
message(STATUS "${BLA_VENDOR}: Missing dependency on CBLAS (hint: set CBLAS_LIBRARY)")
33+
continue()
4334
endif ()
35+
list(APPEND BLAS_LIBRARIES "${CBLAS_LIBRARY}")
36+
endif ()
4437

45-
list(APPEND BLAS_TARGETS ${BLA_VENDOR})
38+
# Don't use "Generic" BLAS if any good BLAS is available.
39+
if (BLA_VENDOR STREQUAL "Generic" AND found_blases)
40+
message(STATUS "${BLA_VENDOR}: Not considered")
41+
continue()
4642
endif ()
43+
44+
message(STATUS "${BLA_VENDOR}: Found ${BLAS_LIBRARIES}")
45+
46+
add_library(BLAS::${BLA_VENDOR} INTERFACE IMPORTED)
47+
target_link_libraries(BLAS::${BLA_VENDOR} INTERFACE ${BLAS_LIBRARIES})
48+
target_link_options(BLAS::${BLA_VENDOR} INTERFACE ${BLAS_LINKER_FLAGS})
49+
target_include_directories(BLAS::${BLA_VENDOR} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") # Use CBlas header in our own tree.
50+
51+
list(APPEND found_blases ${BLA_VENDOR})
4752
endforeach ()
4853

49-
if (NOT BLAS_TARGETS)
50-
message(FATAL_ERROR "Could not find any BLAS libraries! Searched among ${BLAS_VENDORS}")
54+
if (NOT found_blases)
55+
message(FATAL_ERROR "Could not find any BLAS libraries! Searched among ${known_vendors}")
5156
endif ()
5257

5358
# Load in the rest of the project.

apps/linear_algebra/benchmarks/CMakeLists.txt

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_executable(halide_benchmarks halide_benchmarks.cpp)
22
target_compile_definitions(halide_benchmarks PRIVATE ENABLE_FTZ_DAZ)
33
target_link_libraries(halide_benchmarks PRIVATE halide_blas Halide::Tools)
4-
set(BENCHMARK_TARGETS halide_benchmarks)
4+
set(benchmark_targets halide_benchmarks)
55

66
find_package(Eigen3 QUIET)
77
set(Eigen3 Eigen3::Eigen)
@@ -18,39 +18,41 @@ if (TARGET ${Eigen3})
1818
add_executable(eigen_benchmarks eigen_benchmarks.cpp)
1919
target_compile_definitions(eigen_benchmarks PRIVATE EIGEN_DONT_PARALLELIZE ENABLE_FTZ_DAZ)
2020
target_link_libraries(eigen_benchmarks PRIVATE ${Eigen3} Halide::Tools)
21-
list(APPEND BENCHMARK_TARGETS eigen_benchmarks)
21+
list(APPEND benchmark_targets eigen_benchmarks)
2222
message(STATUS "Eigen3: Found")
2323
else ()
2424
message(STATUS "Eigen3: Missing")
2525
endif ()
2626

27-
foreach (BLAS_TARGET IN LISTS BLAS_TARGETS)
28-
set(TARGET ${BLAS_TARGET}_benchmarks)
29-
add_executable(${TARGET} cblas_benchmarks.cpp)
30-
target_compile_definitions(${TARGET} PRIVATE "BLAS_NAME=\"${BLAS_TARGET}\"")
31-
target_link_libraries(${TARGET} PRIVATE ${BLAS_TARGET}::${BLAS_TARGET} Halide::Tools)
32-
list(APPEND BENCHMARK_TARGETS ${TARGET})
27+
foreach (blas IN LISTS FOUND_BLASES)
28+
set(blas_benchmarks "${blas}_benchmarks")
29+
add_executable(${blas_benchmarks} cblas_benchmarks.cpp)
30+
target_compile_definitions(${blas_benchmarks} PRIVATE "BLAS_NAME=\"${blas}\"")
31+
target_link_libraries(${blas_benchmarks} PRIVATE BLAS::${blas} Halide::Tools)
32+
list(APPEND benchmark_targets ${blas_benchmarks})
3333
endforeach ()
3434

3535
# Large powers of two are a pathological case for the cache, so avoid
3636
# them for the benchmarks.
37-
set(BLAS_LEVELS L1 L2 L3)
38-
list(APPEND BENCHMARK_SIZES 64 128 256 512 1280 2560)
39-
list(APPEND L1_BENCHMARKS scopy dcopy sscal dscal saxpy daxpy sdot ddot sasum dasum)
40-
list(APPEND L2_BENCHMARKS sgemv_notrans dgemv_notrans sgemv_trans dgemv_trans sger dger)
41-
list(APPEND L3_BENCHMARKS sgemm_notrans dgemm_notrans sgemm_transA dgemm_transA sgemm_transB dgemm_transB sgemm_transAB dgemm_transAB)
42-
43-
foreach (TARGET IN LISTS BENCHMARK_TARGETS)
44-
string(REPLACE "_benchmarks" "" BLA_VENDOR "${TARGET}")
45-
foreach (LEVEL IN LISTS BLAS_LEVELS)
46-
foreach (FUNC IN LISTS ${LEVEL}_BENCHMARKS)
47-
foreach (SIZE IN LISTS BENCHMARK_SIZES)
48-
set(TEST_NAME ${BLA_VENDOR}_${FUNC}_${SIZE})
49-
add_test(NAME ${TEST_NAME}
50-
COMMAND ${TARGET} ${FUNC} ${SIZE})
51-
set_tests_properties("${TEST_NAME}" PROPERTIES
52-
LABELS "linear_algebra;${BLA_VENDOR};${LEVEL};slow_tests"
53-
PASS_REGULAR_EXPRESSION "${FUNC}[ \t]+${SIZE}"
37+
set(blas_levels L1 L2 L3)
38+
list(APPEND benchmark_sizes 64 128 256 512 1280 2560)
39+
list(APPEND L1_functions scopy dcopy sscal dscal saxpy daxpy sdot ddot sasum dasum)
40+
list(APPEND L2_functions sgemv_notrans dgemv_notrans sgemv_trans dgemv_trans sger dger)
41+
list(APPEND L3_functions sgemm_notrans dgemm_notrans sgemm_transA dgemm_transA sgemm_transB dgemm_transB sgemm_transAB dgemm_transAB)
42+
43+
foreach (benchmark IN LISTS benchmark_targets)
44+
string(REPLACE "_benchmarks" "" vendor "${benchmark}")
45+
foreach (level IN LISTS blas_levels)
46+
foreach (func IN LISTS ${level}_functions)
47+
foreach (size IN LISTS benchmark_sizes)
48+
set(test_name ${vendor}_${func}_${size})
49+
50+
add_test(NAME ${test_name}
51+
COMMAND ${benchmark} ${func} ${size})
52+
53+
set_tests_properties("${test_name}" PROPERTIES
54+
LABELS "linear_algebra;${vendor};${level};slow_tests"
55+
PASS_REGULAR_EXPRESSION "${func}[ \t]+${size}"
5456
SKIP_REGULAR_EXPRESSION "\\[SKIP\\]")
5557
endforeach ()
5658
endforeach ()

apps/linear_algebra/tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
add_executable(test_halide_blas test_halide_blas.cpp)
2-
target_link_libraries(test_halide_blas PRIVATE ${DEFAULT_BLAS} halide_blas)
2+
target_link_libraries(test_halide_blas PRIVATE BLAS::BLAS halide_blas)
3+
target_include_directories(test_halide_blas PRIVATE "${linear_algebra_SOURCE_DIR}/include")
4+
35
add_test(NAME test_halide_blas COMMAND test_halide_blas)
46
set_tests_properties(test_halide_blas PROPERTIES
57
LABELS linear_algebra

cmake/AddCudaToTarget.cmake

Lines changed: 0 additions & 41 deletions
This file was deleted.

cmake/BundleStatic.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function(transfer_locations)
149149

150150
get_property(lib TARGET ${ARG_FROM} PROPERTY "IMPORTED_LOCATION${cfg}")
151151
if (lib)
152-
get_filename_component(stage "${lib}" NAME_WE)
152+
cmake_path(GET lib STEM stage)
153153
set(stage "${CMAKE_CURRENT_BINARY_DIR}/${stage}.obj")
154154

155155
if (NOT EXISTS "${stage}")

cmake/HalideGeneratorHelpers.cmake

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -488,19 +488,9 @@ function(_Halide_target_link_gpu_libs TARGET VISIBILITY)
488488
endif ()
489489

490490
if ("${ARGN}" MATCHES "metal")
491-
find_library(METAL_LIBRARY Metal)
492-
if (NOT METAL_LIBRARY)
493-
message(AUTHOR_WARNING "Metal framework dependency not found on system.")
494-
else ()
495-
target_link_libraries(${TARGET} ${VISIBILITY} "${METAL_LIBRARY}")
496-
endif ()
497-
498-
find_library(FOUNDATION_LIBRARY Foundation)
499-
if (NOT FOUNDATION_LIBRARY)
500-
message(AUTHOR_WARNING "Foundation framework dependency not found on system.")
501-
else ()
502-
target_link_libraries(${TARGET} ${VISIBILITY} "${FOUNDATION_LIBRARY}")
503-
endif ()
491+
find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
492+
find_library(METAL_LIBRARY Metal REQUIRED)
493+
target_link_libraries(${TARGET} ${VISIBILITY} "${FOUNDATION_LIBRARY}" "${METAL_LIBRARY}")
504494
endif ()
505495
endfunction()
506496

cmake/HalideTargetHelpers.cmake

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
13
##
24
# Utilities for manipulating Halide target triples
35
##
@@ -27,22 +29,16 @@ function(_Halide_cmake_target OUTVAR)
2729
set(${OUTVAR} "${arch}-${bits}-${os}" PARENT_SCOPE)
2830
endfunction()
2931

30-
function(_Halide_cache var val doc)
31-
if (DEFINED ${var})
32-
set(${var} "${${var}}" CACHE STRING "${doc}")
33-
else ()
34-
set(${var} "${val}" CACHE STRING "${doc}")
35-
endif ()
36-
endfunction()
37-
3832
##
3933
# Set Halide `host` and `cmake` meta-target values
4034
##
4135

4236
_Halide_cmake_target(_active_triple)
4337

44-
_Halide_cache(Halide_HOST_TARGET "${_active_triple}" "Halide target triple matching the Halide library")
45-
_Halide_cache(Halide_CMAKE_TARGET "${_active_triple}" "Halide target triple matching the CMake target")
38+
set(Halide_HOST_TARGET "${_active_triple}"
39+
CACHE STRING "Halide target triple matching the Halide library")
40+
set(Halide_CMAKE_TARGET "${_active_triple}"
41+
CACHE STRING "Halide target triple matching the CMake target")
4642

4743
unset(_active_triple)
4844

@@ -58,7 +54,8 @@ else ()
5854
set(_default_target "${Halide_CMAKE_TARGET}")
5955
endif ()
6056

61-
_Halide_cache(Halide_TARGET "${_default_target}" "The default target to use when AOT compiling")
57+
set(Halide_TARGET "${_default_target}"
58+
CACHE STRING "The default target to use when AOT compiling")
6259

6360
unset(_default_target)
6461

cmake/HalideTestHelpers.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function(tests)
8181

8282
set(TEST_NAMES "")
8383
foreach (file IN LISTS args_SOURCES)
84-
get_filename_component(name "${file}" NAME_WE)
84+
cmake_path(GET file STEM name)
8585
set(TARGET "${PRIMARY_GROUP}_${name}")
8686

8787
list(APPEND TEST_NAMES "${TARGET}")

0 commit comments

Comments
 (0)