diff --git a/src/cmake/cuda_compat/crt/math_functions.hpp b/src/cmake/cuda_compat/crt/math_functions.hpp new file mode 100644 index 000000000..965721fcc --- /dev/null +++ b/src/cmake/cuda_compat/crt/math_functions.hpp @@ -0,0 +1,21 @@ +// Copyright Contributors to the Open Shading Language project. +// SPDX-License-Identifier: BSD-3-Clause + +// CUDA 13.2 added _NV_RSQRT_SPECIFIER to math_functions.hpp, but older Clang +// CUDA wrappers include that file without defining the macro first. Match the +// definition used by newer Clang wrappers, including glibc 2.42's exception +// specifier. +// Upstream fix: https://github.com/llvm/llvm-project/pull/185701 +#ifndef _NV_RSQRT_SPECIFIER +# if defined(__GNUC__) && defined(__GLIBC_PREREQ) +# if __GLIBC_PREREQ(2, 42) +# define _NV_RSQRT_SPECIFIER noexcept(true) +# endif +# endif +# ifndef _NV_RSQRT_SPECIFIER +# define _NV_RSQRT_SPECIFIER +# endif +#endif + +// Clang's CUDA wrapper intentionally includes this header more than once. +#include_next diff --git a/src/cmake/cuda_compat/cuda_stdlib.h b/src/cmake/cuda_compat/cuda_stdlib.h new file mode 100644 index 000000000..aa8cd1f7a --- /dev/null +++ b/src/cmake/cuda_compat/cuda_stdlib.h @@ -0,0 +1,14 @@ +// Copyright Contributors to the Open Shading Language project. +// SPDX-License-Identifier: BSD-3-Clause + +#pragma once + +// Preload these headers before compiling OSL device code. CUDA's __noinline__ +// macro conflicts with attribute names in libstdc++; hide it during the includes +// and restore it for device code. The headers' include guards prevent reprocessing. +// Related upstream fix: https://github.com/llvm/llvm-project/pull/66138 +#pragma push_macro("__noinline__") +#undef __noinline__ +#include +#include +#pragma pop_macro("__noinline__") diff --git a/src/cmake/cuda_compat/texture_fetch_functions.h b/src/cmake/cuda_compat/texture_fetch_functions.h new file mode 100644 index 000000000..065cf77f0 --- /dev/null +++ b/src/cmake/cuda_compat/texture_fetch_functions.h @@ -0,0 +1,7 @@ +// Copyright Contributors to the Open Shading Language project. +// SPDX-License-Identifier: BSD-3-Clause + +// CUDA 13 removed this header, but older Clang CUDA wrappers still include it. +// OSL does not use the legacy texture-reference API, so an empty stub is +// sufficient for CUDA bitcode generation. +#pragma once diff --git a/src/cmake/cuda_macros.cmake b/src/cmake/cuda_macros.cmake index e87b359f1..1a9f77a2e 100644 --- a/src/cmake/cuda_macros.cmake +++ b/src/cmake/cuda_macros.cmake @@ -2,7 +2,7 @@ # SPDX-License-Identifier: BSD-3-Clause # https://github.com/AcademySoftwareFoundation/OpenShadingLanguage -set ($OSL_EXTRA_NVCC_ARGS "" CACHE STRING "Custom args passed to nvcc when compiling CUDA code") +set (OSL_EXTRA_NVCC_ARGS "" CACHE STRING "Custom args passed to nvcc when compiling CUDA code") set (CUDA_OPT_FLAG_NVCC "-O3" CACHE STRING "The optimization level to use when compiling CUDA/C++ files with nvcc") set (CUDA_OPT_FLAG_CLANG "-O3" CACHE STRING "The optimization level to use when compiling CUDA/C++ files with clang") @@ -148,6 +148,22 @@ function ( MAKE_CUDA_BITCODE src suffix generated_bc extra_clang_args ) set (CUDA_TEXREF_FIX "-D__CLANG_CUDA_TEXTURE_INTRINSICS_H__") endif() + if ("${CUDA_VERSION}" VERSION_GREATER_EQUAL "13.0") + # CUDA 13 removed texture_fetch_functions.h, but older Clang CUDA + # wrappers still include it. Search OSL's compatibility headers first. + set (CUDA_COMPAT_INCLUDE + "-I${PROJECT_SOURCE_DIR}/src/cmake/cuda_compat") + list (APPEND exec_headers + "${PROJECT_SOURCE_DIR}/src/cmake/cuda_compat/crt/math_functions.hpp") + endif () + + # Load libstdc++ before OSL headers while CUDA's __noinline__ macro is hidden. + if (NOT WIN32 AND LLVM_VERSION VERSION_LESS 18.0) + set (CUDA_STDLIB_HEADER "${PROJECT_SOURCE_DIR}/src/cmake/cuda_compat/cuda_stdlib.h") + set (CLANG_CUDA_COMPAT_FLAGS -include "${CUDA_STDLIB_HEADER}") + list (APPEND exec_headers "${CUDA_STDLIB_HEADER}") + endif () + list (TRANSFORM IMATH_INCLUDES PREPEND -I OUTPUT_VARIABLE ALL_IMATH_INCLUDES) list (TRANSFORM OPENEXR_INCLUDES PREPEND -I @@ -157,6 +173,8 @@ function ( MAKE_CUDA_BITCODE src suffix generated_bc extra_clang_args ) add_custom_command (OUTPUT ${bc_cuda} COMMAND ${LLVM_BC_GENERATOR} + ${CUDA_COMPAT_INCLUDE} + ${CLANG_CUDA_COMPAT_FLAGS} "-I${OPTIX_INCLUDES}" "-I${CUDA_INCLUDES}" "-I${CMAKE_CURRENT_SOURCE_DIR}" diff --git a/src/cmake/externalpackages.cmake b/src/cmake/externalpackages.cmake index 9cbbee1d6..11343a022 100644 --- a/src/cmake/externalpackages.cmake +++ b/src/cmake/externalpackages.cmake @@ -141,16 +141,6 @@ if (OSL_USE_OPTIX) message (FATAL_ERROR "NVPTX target is not available in the provided LLVM build") endif() - # CUDA 13.2+ requires LLVM 22.1.2+ for the _NV_RSQRT_SPECIFIER fix. - # See: https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/issues/2129 - if (CUDA_VERSION VERSION_GREATER_EQUAL "13.2" AND LLVM_VERSION VERSION_LESS "22.1.2") - message (FATAL_ERROR - "${ColorRed}CUDA ${CUDA_VERSION} requires LLVM 22.1.2 or newer " - "(you have LLVM ${LLVM_VERSION}). CUDA 13.2+ needs the " - "_NV_RSQRT_SPECIFIER fix from llvm-project commit c1c833734cf0, " - "first shipped in LLVM 22.1.2. Either upgrade LLVM or downgrade CUDA.${ColorReset}") - endif () - set (CUDA_LIB_FLAGS "--cuda-path=${CUDA_TOOLKIT_ROOT_DIR}") # If the user wants, try to use static libs here to putting static lib