Skip to content

Commit

Permalink
Suppress execution checks for expected
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Jan 29, 2025
1 parent 1c31493 commit 2c93e12
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libcudacxx/include/cuda/std/__expected/expected_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,35 @@ union __expected_union_t
struct __empty_t
{};

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Tp2 = _Tp)
_CCCL_REQUIRES(_CCCL_TRAIT(is_default_constructible, _Tp2))
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t() noexcept(_CCCL_TRAIT(is_nothrow_default_constructible, _Tp2))
: __val_()
{}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Tp2 = _Tp)
_CCCL_REQUIRES((!_CCCL_TRAIT(is_default_constructible, _Tp2)))
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t() noexcept
: __empty_()
{}

_CCCL_EXEC_CHECK_DISABLE
template <class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(in_place_t, _Args&&... __args) noexcept(
_CCCL_TRAIT(is_nothrow_constructible, _Tp, _Args...))
: __val_(_CUDA_VSTD::forward<_Args>(__args)...)
{}

_CCCL_EXEC_CHECK_DISABLE
template <class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(unexpect_t, _Args&&... __args) noexcept(
_CCCL_TRAIT(is_nothrow_constructible, _Err, _Args...))
: __unex_(_CUDA_VSTD::forward<_Args>(__args)...)
{}

_CCCL_EXEC_CHECK_DISABLE
template <class _Fun, class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(
__expected_construct_from_invoke_tag,
Expand All @@ -104,6 +109,7 @@ union __expected_union_t
: __val_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...))
{}

_CCCL_EXEC_CHECK_DISABLE
template <class _Fun, class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(
__expected_construct_from_invoke_tag,
Expand All @@ -128,18 +134,21 @@ union __expected_union_t<_Tp, _Err, true>
struct __empty_t
{};

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Tp2 = _Tp)
_CCCL_REQUIRES(_CCCL_TRAIT(is_default_constructible, _Tp2))
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t() noexcept(_CCCL_TRAIT(is_nothrow_default_constructible, _Tp2))
: __val_()
{}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Tp2 = _Tp)
_CCCL_REQUIRES((!_CCCL_TRAIT(is_default_constructible, _Tp2)))
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t() noexcept
: __empty_()
{}

_CCCL_EXEC_CHECK_DISABLE
template <class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(in_place_t, _Args&&... __args) noexcept(
_CCCL_TRAIT(is_nothrow_constructible, _Tp, _Args...))
Expand All @@ -152,6 +161,7 @@ union __expected_union_t<_Tp, _Err, true>
: __unex_(_CUDA_VSTD::forward<_Args>(__args)...)
{}

_CCCL_EXEC_CHECK_DISABLE
template <class _Fun, class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(
__expected_construct_from_invoke_tag,
Expand All @@ -161,6 +171,7 @@ union __expected_union_t<_Tp, _Err, true>
: __val_(_CUDA_VSTD::invoke(_CUDA_VSTD::forward<_Fun>(__fun), _CUDA_VSTD::forward<_Args>(__args)...))
{}

_CCCL_EXEC_CHECK_DISABLE
template <class _Fun, class... _Args>
_LIBCUDACXX_HIDE_FROM_ABI constexpr __expected_union_t(
__expected_construct_from_invoke_tag,
Expand Down Expand Up @@ -436,6 +447,7 @@ struct __expected_storage : __expected_destruct<_Tp, _Err>
{
_LIBCUDACXX_DELEGATE_CONSTRUCTORS(__expected_storage, __expected_destruct, _Tp, _Err);

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _T1, class _T2, class... _Args)
_CCCL_REQUIRES(_CCCL_TRAIT(is_nothrow_constructible, _T1, _Args...))
static _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 void
Expand All @@ -445,6 +457,7 @@ struct __expected_storage : __expected_destruct<_Tp, _Err>
_LIBCUDACXX_CONSTRUCT_AT(__newval, _CUDA_VSTD::forward<_Args>(__args)...);
}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _T1, class _T2, class... _Args)
_CCCL_REQUIRES(
(!_CCCL_TRAIT(is_nothrow_constructible, _T1, _Args...)) _CCCL_AND _CCCL_TRAIT(is_nothrow_move_constructible, _T1))
Expand All @@ -456,6 +469,7 @@ struct __expected_storage : __expected_destruct<_Tp, _Err>
_LIBCUDACXX_CONSTRUCT_AT(__newval, _CUDA_VSTD::move(__tmp));
}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _T1, class _T2, class... _Args)
_CCCL_REQUIRES(
(!_CCCL_TRAIT(is_nothrow_constructible, _T1, _Args...)) _CCCL_AND(!_CCCL_TRAIT(is_nothrow_move_constructible, _T1)))
Expand All @@ -475,6 +489,7 @@ struct __expected_storage : __expected_destruct<_Tp, _Err>
__trans.__complete();
}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Err2 = _Err)
_CCCL_REQUIRES(_CCCL_TRAIT(is_nothrow_move_constructible, _Err2))
static _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 void
Expand All @@ -493,6 +508,7 @@ struct __expected_storage : __expected_destruct<_Tp, _Err>
__with_err.__has_val_ = true;
}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Err2 = _Err)
_CCCL_REQUIRES((!_CCCL_TRAIT(is_nothrow_move_constructible, _Err2)))
static _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 void
Expand Down Expand Up @@ -653,6 +669,7 @@ struct __expected_copy_assign<_Tp, _Err, __smf_availability::__available> : __ex
_CCCL_HIDE_FROM_ABI __expected_copy_assign(const __expected_copy_assign&) = default;
_CCCL_HIDE_FROM_ABI __expected_copy_assign(__expected_copy_assign&&) = default;

_CCCL_EXEC_CHECK_DISABLE
_LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 __expected_copy_assign&
operator=(const __expected_copy_assign& __other) noexcept(
_CCCL_TRAIT(is_nothrow_copy_assignable, _Tp) && _CCCL_TRAIT(is_nothrow_copy_constructible, _Tp)
Expand Down Expand Up @@ -917,6 +934,7 @@ struct __expected_storage<void, _Err> : __expected_destruct<void, _Err>
{
_LIBCUDACXX_DELEGATE_CONSTRUCTORS(__expected_storage, __expected_destruct, void, _Err);

_CCCL_EXEC_CHECK_DISABLE
static _LIBCUDACXX_HIDE_FROM_ABI _CCCL_CONSTEXPR_CXX20 void __swap_val_unex_impl(
__expected_storage& __with_val,
__expected_storage& __with_err) noexcept(_CCCL_TRAIT(is_nothrow_move_constructible, _Err))
Expand Down
7 changes: 7 additions & 0 deletions libcudacxx/include/cuda/std/__expected/unexpected.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class unexpected
_CCCL_HIDE_FROM_ABI unexpected(const unexpected&) = default;
_CCCL_HIDE_FROM_ABI unexpected(unexpected&&) = default;

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Error = _Err)
_CCCL_REQUIRES((!_CCCL_TRAIT(is_same, remove_cvref_t<_Error>, unexpected)
&& !_CCCL_TRAIT(is_same, remove_cvref_t<_Error>, in_place_t)
Expand All @@ -82,13 +83,15 @@ class unexpected
: __unex_(_CUDA_VSTD::forward<_Error>(__error))
{}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class... _Args)
_CCCL_REQUIRES(_CCCL_TRAIT(is_constructible, _Err, _Args...))
_LIBCUDACXX_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, _Args&&... __args) noexcept(
_CCCL_TRAIT(is_nothrow_constructible, _Err, _Args...))
: __unex_(_CUDA_VSTD::forward<_Args>(__args)...)
{}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Up, class... _Args)
_CCCL_REQUIRES(_CCCL_TRAIT(is_constructible, _Err, initializer_list<_Up>&, _Args...))
_LIBCUDACXX_HIDE_FROM_ABI constexpr explicit unexpected(
Expand Down Expand Up @@ -123,13 +126,15 @@ class unexpected
}

// [expected.un.swap]
_CCCL_EXEC_CHECK_DISABLE
_LIBCUDACXX_HIDE_FROM_ABI constexpr void swap(unexpected& __other) noexcept(_CCCL_TRAIT(is_nothrow_swappable, _Err))
{
static_assert(_CCCL_TRAIT(is_swappable, _Err), "E must be swappable");
using _CUDA_VSTD::swap;
swap(__unex_, __other.__unex_);
}

_CCCL_EXEC_CHECK_DISABLE
_CCCL_TEMPLATE(class _Err2 = _Err)
_CCCL_REQUIRES(_CCCL_TRAIT(is_swappable, _Err2))
friend _LIBCUDACXX_HIDE_FROM_ABI constexpr void
Expand All @@ -140,6 +145,7 @@ class unexpected
}

// [expected.un.eq]
_CCCL_EXEC_CHECK_DISABLE
template <class _UErr>
_CCCL_NODISCARD_FRIEND _LIBCUDACXX_HIDE_FROM_ABI constexpr bool
operator==(const unexpected& __lhs,
Expand All @@ -148,6 +154,7 @@ class unexpected
return __lhs.error() == __rhs.error();
}
# if _CCCL_STD_VER < 2020
_CCCL_EXEC_CHECK_DISABLE
template <class _UErr>
_CCCL_NODISCARD_FRIEND _LIBCUDACXX_HIDE_FROM_ABI constexpr bool
operator!=(const unexpected& __lhs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
//===----------------------------------------------------------------------===//
//
// Part of the libcu++ Project, 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) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#include <cuda/std/cassert>
#include <cuda/std/expected>

#include "test_macros.h"

struct device_only_type
{
int val_;

__device__ device_only_type(const int val = 0) noexcept
: val_(val)
{}

__device__ device_only_type(const device_only_type& other) noexcept
: val_(other.val_)
{}
__device__ device_only_type(device_only_type&& other) noexcept
: val_(cuda::std::exchange(other.val_, -1))
{}

__device__ device_only_type& operator=(const device_only_type& other) noexcept
{
val_ = other.val_;
return *this;
}

__device__ device_only_type& operator=(device_only_type&& other) noexcept

{
val_ = cuda::std::exchange(other.val_, -1);
return *this;
}

__device__ ~device_only_type() noexcept {}

__device__ bool operator==(const int other) noexcept
{
return other == val_;
}
__device__ bool operator!=(const int other) noexcept
{
return other != val_;
}
};

__device__ void test()
{
using expected = cuda::std::expected<device_only_type, device_only_type>;
{ // default construction
expected default_constructed{};
assert(default_constructed.has_value());
assert(*default_constructed == 0);
}

{ // in_place zero initialization
expected in_place_zero_initialization{cuda::std::in_place};
assert(in_place_zero_initialization.has_value());
assert(*in_place_zero_initialization == 0);
}

{ // in_place initialization
expected in_place_initialization{cuda::std::in_place, 42};
assert(in_place_initialization.has_value());
assert(*in_place_initialization == 42);
}

{ // unexpect zero initialization
expected in_place_zero_initialization{cuda::std::unexpect};
assert(!in_place_zero_initialization.has_value());
assert(in_place_zero_initialization.error() == 0);
}

{ // unexpect initialization
expected in_place_initialization{cuda::std::unexpect, 42};
assert(!in_place_initialization.has_value());
assert(in_place_initialization.error() == 42);
}

{ // value initialization
expected value_initialization{42};
assert(value_initialization.has_value());
assert(*value_initialization == 42);
}

{ // copy construction
expected input{42};
expected dest{input};
assert(dest.has_value());
assert(*dest == 42);
}

{ // move construction
expected input{42};
expected dest{cuda::std::move(input)};
assert(dest.has_value());
assert(*dest == 42);
}

{ // assignment, value to value
expected input{42};
expected dest{1337};
dest = input;
assert(dest.has_value());
assert(*dest == 42);
}

{ // assignment, value to empty
expected input{42};
expected dest{};
dest = input;
assert(dest.has_value());
assert(*dest == 42);
}

{ // assignment, empty to value
expected input{};
expected dest{1337};
dest = input;
assert(dest.has_value());
assert(*dest == 0);
}

{ // assignment, empty to empty
expected input{};
expected dest{};
dest = input;
assert(dest.has_value());
assert(*dest == 0);
}

{ // assignment, error to value
expected input{cuda::std::unexpect, 42};
expected dest{1337};
dest = input;
assert(!dest.has_value());
assert(dest.error() == 42);
}

{ // assignment, value to error
expected input{42};
expected dest{cuda::std::unexpect, 1337};
dest = input;
assert(dest.has_value());
assert(*dest == 42);
}

{ // assignment, error to error
expected input{cuda::std::unexpect, 42};
expected dest{cuda::std::unexpect, 1337};
dest = input;
assert(!dest.has_value());
assert(dest.error() == 42);
}
}

int main(int arg, char** argv)
{
NV_IF_TARGET(NV_IS_DEVICE, (test();))
return 0;
}
Loading

0 comments on commit 2c93e12

Please sign in to comment.