@@ -42,6 +42,29 @@ function (run_python OUT EXPR ERR_MSG)
4242 set (${OUT} ${PYTHON_OUT} PARENT_SCOPE )
4343endfunction ()
4444
45+ #
46+ # Run `EXPR` in python. The standard output of python is stored in `OUT` and
47+ # has trailing whitespace stripped. If an error is encountered when running
48+ # python, `SUCCESS` is set to FALSE. If successful, `SUCCESS` is set to TRUE.
49+ #
50+ function (try_run_python OUT SUCCESS EXPR )
51+ execute_process (
52+ COMMAND
53+ "${Python3_EXECUTABLE} " "-c" "${EXPR} "
54+ OUTPUT_VARIABLE PYTHON_OUT
55+ RESULT_VARIABLE PYTHON_ERROR_CODE
56+ ERROR_QUIET
57+ OUTPUT_STRIP_TRAILING_WHITESPACE )
58+
59+ if (NOT PYTHON_ERROR_CODE EQUAL 0)
60+ set (${SUCCESS} FALSE PARENT_SCOPE )
61+ set (${OUT} "" PARENT_SCOPE )
62+ else ()
63+ set (${SUCCESS} TRUE PARENT_SCOPE )
64+ set (${OUT} ${PYTHON_OUT} PARENT_SCOPE )
65+ endif ()
66+ endfunction ()
67+
4568# Run `EXPR` in python after importing `PKG`. Use the result of this to extend
4669# `CMAKE_PREFIX_PATH` so the torch cmake configuration can be imported.
4770macro (append_cmake_prefix_path PKG EXPR )
@@ -152,34 +175,28 @@ macro(string_to_ver OUT_VER IN_STR)
152175endmacro ()
153176
154177#
155- # Clear all `-gencode` flags from `CMAKE_CUDA_FLAGS` and store them in
156- # `CUDA_ARCH_FLAGS`.
178+ # Clear all `-gencode` flags from `CMAKE_CUDA_FLAGS`.
157179#
158180# Example:
159181# CMAKE_CUDA_FLAGS="-Wall -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75"
160- # clear_cuda_arches(CUDA_ARCH_FLAGS)
161- # CUDA_ARCH_FLAGS="-gencode arch=compute_70,code=sm_70;-gencode arch=compute_75,code=sm_75"
182+ # clear_gencode_flags()
162183# CMAKE_CUDA_FLAGS="-Wall"
163184#
164- macro (clear_cuda_arches CUDA_ARCH_FLAGS )
165- # Extract all `-gencode` flags from `CMAKE_CUDA_FLAGS`
166- string (REGEX MATCHALL "-gencode arch=[^ ]+" CUDA_ARCH_FLAGS
167- ${CMAKE_CUDA_FLAGS} )
168-
185+ macro (clear_gencode_flags )
169186 # Remove all `-gencode` flags from `CMAKE_CUDA_FLAGS` since they will be modified
170187 # and passed back via the `CUDA_ARCHITECTURES` property.
171188 string (REGEX REPLACE "-gencode arch=[^ ]+ *" "" CMAKE_CUDA_FLAGS
172189 ${CMAKE_CUDA_FLAGS} )
173190endmacro ()
174191
175192#
176- # Extract unique CUDA architectures from a list of compute capabilities codes in
177- # the form `<major><minor>[<letter>]`, convert them to the form sort
178- # `<major>.<minor>`, dedupes them and then sorts them in ascending order and
193+ # Extract unique CUDA architectures from a list of compute capabilities codes in
194+ # the form `<major><minor>[<letter>]`, convert them to the form sort
195+ # `<major>.<minor>`, dedupes them and then sorts them in ascending order and
179196# stores them in `OUT_ARCHES`.
180197#
181198# Example:
182- # CUDA_ARCH_FLAGS="-gencode arch=compute_75,code=sm_75;...;-gencode arch=compute_90a,code=sm_90a"
199+ # CUDA_ARCH_FLAGS="-gencode arch=compute_75,code=sm_75;...;-gencode arch=compute_90a,code=sm_90a"
183200# extract_unique_cuda_archs_ascending(OUT_ARCHES CUDA_ARCH_FLAGS)
184201# OUT_ARCHES="7.5;...;9.0"
185202function (extract_unique_cuda_archs_ascending OUT_ARCHES CUDA_ARCH_FLAGS )
@@ -200,15 +217,15 @@ function(extract_unique_cuda_archs_ascending OUT_ARCHES CUDA_ARCH_FLAGS)
200217endfunction ()
201218
202219#
203- # For a specific file set the `-gencode` flag in compile options conditionally
204- # for the CUDA language.
220+ # For a specific file set the `-gencode` flag in compile options conditionally
221+ # for the CUDA language.
205222#
206223# Example:
207224# set_gencode_flag_for_srcs(
208225# SRCS "foo.cu"
209226# ARCH "compute_75"
210227# CODE "sm_75")
211- # adds: "-gencode arch=compute_75,code=sm_75" to the compile options for
228+ # adds: "-gencode arch=compute_75,code=sm_75" to the compile options for
212229# `foo.cu` (only for the CUDA language).
213230#
214231macro (set_gencode_flag_for_srcs )
@@ -228,14 +245,14 @@ macro(set_gencode_flag_for_srcs)
228245endmacro (set_gencode_flag_for_srcs )
229246
230247#
231- # For a list of source files set the `-gencode` flags in the files specific
248+ # For a list of source files set the `-gencode` flags in the files specific
232249# compile options (specifically for the CUDA language).
233250#
234251# arguments are:
235252# SRCS: list of source files
236253# CUDA_ARCHS: list of CUDA architectures in the form `<major>.<minor>[letter]`
237254# BUILD_PTX_FOR_ARCH: if set to true, then the PTX code will be built
238- # for architecture `BUILD_PTX_FOR_ARCH` if there is a CUDA_ARCH in CUDA_ARCHS
255+ # for architecture `BUILD_PTX_FOR_ARCH` if there is a CUDA_ARCH in CUDA_ARCHS
239256# that is larger than BUILD_PTX_FOR_ARCH.
240257#
241258macro (set_gencode_flags_for_srcs )
@@ -383,12 +400,14 @@ function(cuda_archs_loose_intersection OUT_CUDA_ARCHS SRC_CUDA_ARCHS TGT_CUDA_AR
383400 endforeach ()
384401 set (_CUDA_ARCHS ${_FINAL_ARCHS} )
385402
403+ list (SORT _CUDA_ARCHS COMPARE NATURAL ORDER ASCENDING )
404+
386405 set (${OUT_CUDA_ARCHS} ${_CUDA_ARCHS} PARENT_SCOPE )
387406endfunction ()
388407
389408#
390- # For the given `SRC_ROCM_ARCHS` list of architecture versions in the form
391- # `<name>` compute the "loose intersection" with the `TGT_ROCM_ARCHS` list.
409+ # For the given `SRC_ROCM_ARCHS` list of architecture versions in the form
410+ # `<name>` compute the "loose intersection" with the `TGT_ROCM_ARCHS` list.
392411# The loose intersection is defined as:
393412# { max{ x \in tgt | x <= y } | y \in src, { x \in tgt | x <= y } != {} }
394413# where `<=` is the version comparison operator.
@@ -404,28 +423,48 @@ endfunction()
404423#
405424function (hip_archs_loose_intersection OUT_ROCM_ARCHS SRC_ROCM_ARCHS TGT_ROCM_ARCHS )
406425 list (REMOVE_DUPLICATES SRC_ROCM_ARCHS)
407-
426+
408427 # ROCm architectures are typically in format gfxNNN or gfxNNNx where N is a digit
409428 # and x is a letter. We can sort them by string comparison which works for this format.
410429 list (SORT SRC_ROCM_ARCHS COMPARE STRING ORDER ASCENDING )
411-
430+
412431 set (_ROCM_ARCHS)
413-
432+
414433 # Find the intersection of supported architectures
415434 foreach (_SRC_ARCH ${SRC_ROCM_ARCHS} )
416435 if (_SRC_ARCH IN_LIST TGT_ROCM_ARCHS)
417436 list (APPEND _ROCM_ARCHS ${_SRC_ARCH} )
418437 endif ()
419438 endforeach ()
420-
439+
421440 list (REMOVE_DUPLICATES _ROCM_ARCHS)
422441 set (${OUT_ROCM_ARCHS} ${_ROCM_ARCHS} PARENT_SCOPE )
423442endfunction ()
424443
444+ function (cuda_remove_ptx_suffixes OUT_CUDA_ARCHS CUDA_ARCHS )
445+ set (_CUDA_ARCHS "${CUDA_ARCHS} " )
446+
447+ # handle +PTX suffix: separate base arch for matching, record PTX requests
448+ foreach (_arch ${CUDA_ARCHS} )
449+ if (_arch MATCHES "\\ +PTX$" )
450+ string (REPLACE "+PTX" "" _base "${_arch} " )
451+ list (REMOVE_ITEM _CUDA_ARCHS "${_arch} " )
452+ list (APPEND _CUDA_ARCHS "${_base} " )
453+ endif ()
454+ endforeach ()
455+
456+ list (REMOVE_DUPLICATES _CUDA_ARCHS)
457+ list (SORT _CUDA_ARCHS COMPARE NATURAL ORDER ASCENDING )
458+
459+ set (${OUT_CUDA_ARCHS} ${_CUDA_ARCHS} PARENT_SCOPE )
460+ endfunction ()
461+
462+
463+
425464#
426465# Override the GPU architectures detected by cmake/torch and filter them by
427466# `GPU_SUPPORTED_ARCHES`. Sets the final set of architectures in
428- # `GPU_ARCHES`. This only applies to the HIP language since for CUDA we set
467+ # `GPU_ARCHES`. This only applies to the HIP language since for CUDA we set
429468# the architectures on a per file basis.
430469#
431470# Note: this is defined as a macro since it updates `CMAKE_CUDA_FLAGS`.
0 commit comments