Skip to content

Commit 4fac373

Browse files
committed
Disable exceptions when used in CUDA code
NVCC supports neither exceptions nor std::terminate in device code, but silently ignores them. When using Clang to compile CUDA code, "reference to __host__ function" errors are raised when using exceptions or std::terminate. This patch disables exceptions and uses the __trap intrinsic to deliver consistently correct behavior for both NVCC and Clang. MPARK_BUILTIN_UNREACHABLE cannot be used here as it again results in "reference to __host__ function" errors.
1 parent 23cb94f commit 4fac373

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

include/mpark/config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363
#define MPARK_CPP14_CONSTEXPR
6464
#endif
6565

66-
#if __has_feature(cxx_exceptions) || defined(__cpp_exceptions) || \
66+
#if (__has_feature(cxx_exceptions) || defined(__cpp_exceptions) || \
6767
(defined(_MSC_VER) && defined(_CPPUNWIND)) || \
68-
defined(__EXCEPTIONS)
68+
defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__)
6969
#define MPARK_EXCEPTIONS
7070
#endif
7171

include/mpark/variant.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,15 @@ namespace mpark {
244244
virtual const char *what() const noexcept override { return "bad_variant_access"; }
245245
};
246246

247-
[[noreturn]] inline void throw_bad_variant_access() {
247+
[[noreturn]]
248+
#ifdef __CUDACC__
249+
__host__ __device__
250+
#endif
251+
inline void throw_bad_variant_access() {
248252
#ifdef MPARK_EXCEPTIONS
249253
throw bad_variant_access{};
254+
#elif defined(__CUDA_ARCH__)
255+
__trap();
250256
#else
251257
std::terminate();
252258
MPARK_BUILTIN_UNREACHABLE;

0 commit comments

Comments
 (0)