Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/cmake/cuda_compat/crt/math_functions.hpp
Original file line number Diff line number Diff line change
@@ -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 <crt/math_functions.hpp>
14 changes: 14 additions & 0 deletions src/cmake/cuda_compat/cuda_stdlib.h
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <string>
#pragma pop_macro("__noinline__")
7 changes: 7 additions & 0 deletions src/cmake/cuda_compat/texture_fetch_functions.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 19 additions & 1 deletion src/cmake/cuda_macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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}"
Expand Down
10 changes: 0 additions & 10 deletions src/cmake/externalpackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading