cccl icon indicating copy to clipboard operation
cccl copied to clipboard

[BUG]: constraint recursion in the `cuda::mr::resource` concept

Open ericniebler opened this issue 1 year ago • 1 comments

Is this a duplicate?

  • [X] I confirmed there appear to be no duplicate issues for this bug and that I agree to the Code of Conduct

Type of Bug

Compile-time Error

Component

libcu++

Describe the bug

The resource concept is defined as follows:

template <class _Resource>
_LIBCUDACXX_CONCEPT_FRAGMENT(
  __resource_,
  requires(_Resource& __res, void* __ptr, size_t __bytes, size_t __alignment)(
    requires(_CUDA_VSTD::same_as<void*, decltype(__res.allocate(__bytes, __alignment))>),
    requires(_CUDA_VSTD::same_as<void, decltype(__res.deallocate(__ptr, __bytes, __alignment))>),
    requires(_CUDA_VSTD::equality_comparable<_Resource>)));

template <class _Resource>
_LIBCUDACXX_CONCEPT resource = _LIBCUDACXX_FRAGMENT(__resource_, _Resource);

A type must be equality_comparable to satisfy resource. But some resource types that are heterogeneously comparable have operator== overloads that are indirectly constrained with resource (like below), which causes constrain recursion and a hard error.

  template <class _Resource>
  _CCCL_NODISCARD_FRIEND auto operator==(cuda_memory_resource const& __lhs, _Resource const& __rhs) noexcept
    _LIBCUDACXX_TRAILING_REQUIRES(bool)(__different_resource<cuda_memory_resource, _Resource>)
  {
    return resource_ref<>{const_cast<cuda_memory_resource&>(__lhs)} == resource_ref<>{const_cast<_Resource&>(__rhs)};
  }

The problem here is the __different_resource concept, which subsumes resource.

My suggestion is to drop support for heterogeneous comparisons.

How to Reproduce

#include <cuda/memory_resource>

struct my_resource : cuda::mr::cuda_memory_resource {
  using cuda::mr::cuda_memory_resource::cuda_memory_resource;
};

static_assert(cuda::mr::resource<my_resource>);

result:

/opt/compiler-explorer/libs/cccl/trunk/libcudacxx/include/cuda/__memory_resource/resource.h(104): error: expression must have a constant value
    (!::cuda::std::__4::same_as< ::cuda::std::__4::decay_t<_Resource>, ::cuda::std::__4::decay_t<_OtherResource>>)
    ^
/opt/compiler-explorer/libs/cccl/trunk/libcudacxx/include/cuda/__memory_resource/resource.h(105): note #2689-D: the value of variable "cuda::mr::__4::resource [with _Resource=my_resource]" (declared at line 58) cannot be used as a constant
    && resource<_OtherResource>;
       ^
          detected during:
            instantiation of "const bool cuda::mr::__4::__different_resource [with _Resource=cuda::mr::__4::cuda_memory_resource, _OtherResource=my_resource]" at line 75 of /opt/compiler-explorer/libs/cccl/trunk/libcudacxx/include/cuda/std/__concepts/equality_comparable.h
            instantiation of "const bool cuda::std::__4::__weakly_equality_comparable_with [with _Tp=my_resource, _Up=my_resource]" at line 78 of /opt/compiler-explorer/libs/cccl/trunk/libcudacxx/include/cuda/std/__concepts/equality_comparable.h
            instantiation of "const bool cuda::std::__4::equality_comparable [with _Tp=my_resource]" at line 58
            instantiation of "const bool cuda::mr::__4::resource [with _Resource=my_resource]" at line 7 of <source>

<source>(7): error: static assertion failed
  static_assert(cuda::mr::resource<my_resource>);
  ^

2 errors detected in the compilation of "<source>".
Compiler returned: 2

https://godbolt.org/z/4o97PM7oz

Expected behavior

the static_assert should not cause a hard error.

Reproduction link

https://godbolt.org/z/4o97PM7oz

Operating System

No response

nvidia-smi output

No response

NVCC version

No response

ericniebler avatar Aug 09 '24 22:08 ericniebler

good analysis

cyk2018 avatar Aug 11 '24 06:08 cyk2018

I encountered this problem in one of my tests an I thought that i "fixed" it through the ``__different_from` concept, that should short circuit the concept evaluation if both resources have the same type. With that the type should be equality comparable or am I missing something.

miscco avatar Aug 12 '24 07:08 miscco