//===----------------------------------------------------------------------===// // // Part of libcu++, the C++ Standard Library for your entire system, // under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===// // MIT License // // Modifications Copyright (C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #ifndef __CCCL_ASSERT_H #define __CCCL_ASSERT_H #include #include #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) # pragma GCC system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) # pragma clang system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) # pragma system_header #endif // no system header #include #include #include #include #if !_CCCL_COMPILER(NVRTC) && !defined(_CCCL_COMPILER_HIPRTC) # include #endif // !_CCCL_COMPILER(NVRTC) #include #if defined(_DEBUG) || defined(DEBUG) # ifndef _CCCL_ENABLE_DEBUG_MODE # define _CCCL_ENABLE_DEBUG_MODE # endif // !_CCCL_ENABLE_DEBUG_MODE #endif // _DEBUG || DEBUG // Automatically enable assertions when debug mode is enabled #ifdef _CCCL_ENABLE_DEBUG_MODE # ifndef CCCL_ENABLE_ASSERTIONS # define CCCL_ENABLE_ASSERTIONS # endif // !CCCL_ENABLE_ASSERTIONS #endif // _CCCL_ENABLE_DEBUG_MODE //! Ensure that we switch on host assertions when all assertions are enabled #ifndef CCCL_ENABLE_HOST_ASSERTIONS # ifdef CCCL_ENABLE_ASSERTIONS # define CCCL_ENABLE_HOST_ASSERTIONS # endif // CCCL_ENABLE_ASSERTIONS #endif // !CCCL_ENABLE_HOST_ASSERTIONS //! Ensure that we switch on device assertions when all assertions are enabled #ifndef CCCL_ENABLE_DEVICE_ASSERTIONS # ifdef CCCL_ENABLE_ASSERTIONS # define CCCL_ENABLE_DEVICE_ASSERTIONS # endif // CCCL_ENABLE_ASSERTIONS #endif // !CCCL_ENABLE_DEVICE_ASSERTIONS //! Use the different standard library implementations to implement host side asserts //! _CCCL_ASSERT_IMPL_HOST should never be used directly #if _CCCL_OS(QNX) # define _CCCL_ASSERT_IMPL_HOST(expression, message) ((void) 0) #elif _CCCL_COMPILER(NVRTC) || defined(_CCCL_COMPILER_HIPRTC) // There is no host standard library in nvrtc # define _CCCL_ASSERT_IMPL_HOST(expression, message) ((void) 0) #elif _CCCL_HAS_INCLUDE() && (_CCCL_COMPILER(MSVC) || (defined(__HIP_PLATFORM_AMD__) && defined(_WIN32))) // MSVC uses _STL_VERIFY from # include # define _CCCL_ASSERT_IMPL_HOST(expression, message) _STL_VERIFY(expression, message) #else // ^^^ MSVC, HIPCC, WIN32 ^^^ / vvv !MSVC STL, HIPCC (Linux/glibc) vvv # ifdef NDEBUG // Reintroduce the __assert_fail declaration extern "C" { # if !_CCCL_CUDA_COMPILER(CLANG) && !defined(_CCCL_COMPILER_HIPCC) && !defined(_CCCL_COMPILER_HIPRTC) _CCCL_HOST_DEVICE # endif // !_CCCL_CUDA_COMPILER(CLANG) void __assert_fail(const char* __assertion, const char* __file, unsigned int __line, const char* __function) noexcept __attribute__((__noreturn__)); } # endif // NDEBUG # define _CCCL_ASSERT_IMPL_HOST(expression, message) \ _CCCL_BUILTIN_EXPECT(static_cast(expression), 1) \ ? (void) 0 : __assert_fail(message, __FILE__, __LINE__, __func__) #endif // !MSVC STL, HIPCC (Linux/glibc) //! Use custom implementations with nvcc on device and the host ones with clang-cuda and nvhpc //! _CCCL_ASSERT_IMPL_DEVICE should never be used directly #if _CCCL_OS(QNX) # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) ((void) 0) #elif _CCCL_COMPILER(NVRTC) || defined(_CCCL_COMPILER_HIPRTC) // NOTE(HIP/AMD): Use _wassert on Windows, __assertfail on Linux # if defined(_WIN32) # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) \ _CCCL_BUILTIN_EXPECT(static_cast(expression), 1) \ ? (void) 0 : _wassert(_CRT_WIDE(message), __FILEW__, __LINE__) # else // NOTE(HIP/AMD): We need to use __assertfail which is defined in hiprtc_runtime, otherwise we get error: // note: candidate function not viable: requires 0 arguments, but 5 were provided # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) \ _CCCL_BUILTIN_EXPECT(static_cast(expression), 1) \ ? (void) 0 : __assertfail() // ? (void) 0 : __assertfail(message, __FILE__, __LINE__, __func__, sizeof(char)) # endif #elif _CCCL_CUDA_COMPILER(NVCC) || (defined(__HIP_PLATFORM_AMD__) && defined(_WIN32)) //! Use __assert_fail to implement device side asserts # if _CCCL_COMPILER(MSVC) || (defined(__HIP_PLATFORM_AMD__) && defined(_WIN32)) # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) \ _CCCL_BUILTIN_EXPECT(static_cast(expression), 1) \ ? (void) 0 : _wassert(_CRT_WIDE(#message), __FILEW__, __LINE__) # else // ^^^ _CCCL_COMPILER(MSVC) ^^^ / vvv !_CCCL_COMPILER(MSVC) vvv # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) \ _CCCL_BUILTIN_EXPECT(static_cast(expression), 1) \ ? (void) 0 : __assert_fail(message, __FILE__, __LINE__, __func__) # endif // !_CCCL_COMPILER(MSVC) #elif _CCCL_HAS_CUDA_COMPILER() # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message) #else // ^^^ _CCCL_HAS_CUDA_COMPILER() ^^^ / vvv !_CCCL_HAS_CUDA_COMPILER() vvv # define _CCCL_ASSERT_IMPL_DEVICE(expression, message) ((void) 0) #endif // !_CCCL_HAS_CUDA_COMPILER() //! _CCCL_ASSERT_HOST is enabled conditionally depending on CCCL_ENABLE_HOST_ASSERTIONS #ifdef CCCL_ENABLE_HOST_ASSERTIONS # define _CCCL_ASSERT_HOST(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message) #else // ^^^ CCCL_ENABLE_HOST_ASSERTIONS ^^^ / vvv !CCCL_ENABLE_HOST_ASSERTIONS vvv # define _CCCL_ASSERT_HOST(expression, message) ((void) 0) #endif // !CCCL_ENABLE_HOST_ASSERTIONS //! _CCCL_ASSERT_DEVICE is enabled conditionally depending on CCCL_ENABLE_DEVICE_ASSERTIONS #ifdef CCCL_ENABLE_DEVICE_ASSERTIONS # define _CCCL_ASSERT_DEVICE(expression, message) _CCCL_ASSERT_IMPL_DEVICE(expression, message) #else // ^^^ CCCL_ENABLE_DEVICE_ASSERTIONS ^^^ / vvv !CCCL_ENABLE_DEVICE_ASSERTIONS vvv # define _CCCL_ASSERT_DEVICE(expression, message) ((void) 0) #endif // !CCCL_ENABLE_DEVICE_ASSERTIONS //! _CCCL_VERIFY is enabled unconditionally and reserved for critical checks that are required to always be on //! _CCCL_ASSERT is enabled conditionally depending on CCCL_ENABLE_HOST_ASSERTIONS and CCCL_ENABLE_DEVICE_ASSERTIONS #if _CCCL_CUDA_COMPILER(NVHPC) // NVHPC can't have different behavior for host and device. // The host version of the assert will also work in device code. # define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message) # if defined(CCCL_ENABLE_HOST_ASSERTIONS) || defined(CCCL_ENABLE_DEVICE_ASSERTIONS) # define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_HOST(expression, message) # else # define _CCCL_ASSERT(expression, message) ((void) 0) # endif #elif _CCCL_HAS_CUDA_COMPILER() # if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) # define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_DEVICE(expression, message) # define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_DEVICE(expression, message) # else // ^^^ __CUDA_ARCH__ ^^^ / vvv !__CUDA_ARCH__ vvv # define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message) # define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_HOST(expression, message) # endif // !__CUDA_ARCH__ #else // ^^^ _CCCL_HAS_CUDA_COMPILER() ^^^ / vvv !_CCCL_HAS_CUDA_COMPILER() vvv # define _CCCL_VERIFY(expression, message) _CCCL_ASSERT_IMPL_HOST(expression, message) # define _CCCL_ASSERT(expression, message) _CCCL_ASSERT_HOST(expression, message) #endif // !_CCCL_HAS_CUDA_COMPILER() #endif // __CCCL_ASSERT_H