Skip to content

Commit 90cd2de

Browse files
committed
Move all kernel component handling to CMake functions
Our end goal is to generate a CMake file that works with all backends. This is a first step, handle the kernel components using functions, so that we can later generate the calls for every backend. The calls are still in backend-specific code for now.
1 parent 9d22eff commit 90cd2de

14 files changed

Lines changed: 357 additions & 213 deletions

File tree

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
set({{kernel_name}}_SRC
2-
{{ sources }}
1+
cpu_kernel_component(SRC
2+
SOURCES {{ sources }}
3+
{% if includes %}INCLUDES "{{ includes }}"{% endif %}
4+
{% if cxx_flags %}CXX_FLAGS "{{ cxx_flags }}"{% endif %}
35
)
4-
5-
{% if includes %}
6-
# TODO: check if CLion support this:
7-
# https://youtrack.jetbrains.com/issue/CPP-16510/CLion-does-not-handle-per-file-include-directories
8-
set_source_files_properties(
9-
{{'${' + kernel_name + '_SRC}'}}
10-
PROPERTIES INCLUDE_DIRECTORIES "{{ includes }}")
11-
{% endif %}
12-
13-
{% if cxx_flags %}
14-
foreach(_KERNEL_SRC {{'${' + kernel_name + '_SRC}'}})
15-
set_property(
16-
SOURCE ${_KERNEL_SRC}
17-
APPEND PROPERTY
18-
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:{{ cxx_flags }}>"
19-
)
20-
endforeach()
21-
{% endif %}
22-
23-
# Add C++ sources to main source list
24-
list(APPEND SRC {{'"${' + kernel_name + '_SRC}"'}})

build2cmake/src/templates/cpu/preamble.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ file(MAKE_DIRECTORY ${FETCHCONTENT_BASE_DIR}) # Ensure the directory exists
1010
message(STATUS "FetchContent base directory: ${FETCHCONTENT_BASE_DIR}")
1111

1212
include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
13+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/kernel.cmake)
1314

1415
if(DEFINED Python3_EXECUTABLE)
1516
# Allow passing through the interpreter (e.g. from setup.py).
@@ -42,3 +43,6 @@ endif()
4243
{% endif %}
4344

4445
add_compile_definitions(CPU_KERNEL)
46+
47+
# Initialize SRC list for kernel and binding sources
48+
set(SRC "")
Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,11 @@
1-
{% if cuda_minver %}
2-
if (CUDA_VERSION VERSION_GREATER_EQUAL {{ cuda_minver }})
3-
{% endif %}
4-
5-
set({{kernel_name}}_SRC
6-
{{ sources }}
1+
cuda_kernel_component(SRC
2+
SOURCES {{ sources }}
3+
{% if cuda_minver %}CUDA_MINVER {{ cuda_minver }}{% endif %}
4+
{% if includes %}INCLUDES "{{ includes }}"{% endif %}
5+
{% if cuda_capabilities %}CUDA_CAPABILITIES {{ cuda_capabilities|join(" ") }}{% endif %}
6+
{% if cuda_flags %}CUDA_FLAGS "{{ cuda_flags }}"{% endif %}
7+
{% if cxx_flags %}CXX_FLAGS "{{ cxx_flags }}"{% endif %}
8+
{% if supports_hipify %}SUPPORTS_HIPIFY{% endif %}
9+
{% if hip_flags %}HIP_FLAGS "{{ hip_flags }}"{% endif %}
10+
{% if rocm_archs %}ROCM_ARCHS {{ rocm_archs|join(" ") }}{% endif %}
711
)
8-
9-
{% if includes %}
10-
# TODO: check if CLion support this:
11-
# https://youtrack.jetbrains.com/issue/CPP-16510/CLion-does-not-handle-per-file-include-directories
12-
set_source_files_properties(
13-
{{'${' + kernel_name + '_SRC}'}}
14-
PROPERTIES INCLUDE_DIRECTORIES "{{ includes }}")
15-
{% endif %}
16-
17-
if(GPU_LANG STREQUAL "CUDA")
18-
{% if cuda_capabilities %}
19-
cuda_archs_loose_intersection({{kernel_name}}_ARCHS "{{ cuda_capabilities|join(";") }}" "${CUDA_ARCHS}")
20-
{% else %}
21-
set({{kernel_name}}_ARCHS "${CUDA_KERNEL_ARCHS}")
22-
{% endif %}
23-
message(STATUS "Capabilities for kernel {{kernel_name}}: {{ '${' + kernel_name + '_ARCHS}'}}")
24-
set_gencode_flags_for_srcs(SRCS {{'"${' + kernel_name + '_SRC}"'}} CUDA_ARCHS "{{ '${' + kernel_name + '_ARCHS}'}}")
25-
26-
{% if cuda_flags %}
27-
28-
set(_CUDA_FLAGS "{{ cuda_flags }}")
29-
# -static-global-template-stub is not supported on CUDA < 12.8. Remove this
30-
# once we don't support CUDA 12.6 anymore.
31-
if(CUDA_VERSION VERSION_LESS 12.8)
32-
string(REGEX REPLACE "-static-global-template-stub=(true|false)" "" _CUDA_FLAGS "${_CUDA_FLAGS}")
33-
endif()
34-
35-
foreach(_KERNEL_SRC {{'${' + kernel_name + '_SRC}'}})
36-
if(_KERNEL_SRC MATCHES ".*\\.cu$")
37-
set_property(
38-
SOURCE ${_KERNEL_SRC}
39-
APPEND PROPERTY
40-
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CUDA>:${_CUDA_FLAGS}>"
41-
)
42-
endif()
43-
endforeach()
44-
{% endif %}
45-
46-
{% if cxx_flags %}
47-
foreach(_KERNEL_SRC {{'${' + kernel_name + '_SRC}'}})
48-
set_property(
49-
SOURCE ${_KERNEL_SRC}
50-
APPEND PROPERTY
51-
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:{{ cxx_flags }}>"
52-
)
53-
endforeach()
54-
{% endif %}
55-
56-
list(APPEND SRC {{'"${' + kernel_name + '_SRC}"'}})
57-
{% if supports_hipify %}
58-
elseif(GPU_LANG STREQUAL "HIP")
59-
{% if hip_flags %}
60-
61-
foreach(_KERNEL_SRC {{'${' + kernel_name + '_SRC}'}})
62-
if(_KERNEL_SRC MATCHES ".*\\.(cu|hip)$")
63-
set_property(
64-
SOURCE ${_KERNEL_SRC}
65-
APPEND PROPERTY
66-
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:HIP>:{{ hip_flags }}>"
67-
)
68-
endif()
69-
endforeach()
70-
{% endif %}
71-
72-
hip_archs_loose_intersection({{kernel_name}}_ARCHS "{{ rocm_archs|join(";") }}" "${ROCM_ARCHS}")
73-
message(STATUS "Archs for kernel {{kernel_name}}: {{ '${' + kernel_name + '_ARCHS}'}}")
74-
75-
foreach(_KERNEL_SRC {{'${' + kernel_name + '_SRC}'}})
76-
if(_KERNEL_SRC MATCHES ".*\\.(cu|hip)$")
77-
foreach(_ROCM_ARCH {{ '${' + kernel_name + '_ARCHS}'}})
78-
set_property(
79-
SOURCE ${_KERNEL_SRC}
80-
APPEND PROPERTY
81-
COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:HIP>:--offload-arch=${_ROCM_ARCH}>"
82-
)
83-
endforeach()
84-
endif()
85-
endforeach()
86-
87-
list(APPEND SRC {{'"${' + kernel_name + '_SRC}"'}})
88-
{% endif %}
89-
endif()
90-
91-
{% if cuda_minver %}
92-
endif()
93-
{% endif %}

build2cmake/src/templates/cuda/preamble.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ message(STATUS "FetchContent base directory: ${FETCHCONTENT_BASE_DIR}")
1212
set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx942;gfx950;gfx1030;gfx1100;gfx1101;gfx1200;gfx1201")
1313

1414
include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
15+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/kernel.cmake)
1516

1617
if(DEFINED Python3_EXECUTABLE)
1718
# Allow passing through the interpreter (e.g. from setup.py).
@@ -128,6 +129,8 @@ else()
128129
"${${GPU_LANG}_SUPPORTED_ARCHS}")
129130
endif()
130131

132+
# Initialize SRC list for kernel and binding sources
133+
set(SRC "")
131134

132135
message(STATUS "Rendered for platform {{ platform }}")
133136
{% if platform == 'windows' %}

build2cmake/src/templates/cuda/torch-binding.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ set_source_files_properties(
1414
{% endif %}
1515

1616
list(APPEND SRC {{'"${TORCH_' + name + '_SRC}"'}})
17-

0 commit comments

Comments
 (0)