Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSVC Compatibility: Implement intrinsics for P0466R5 - Layout-compatibility and Pointer-interconvertibility Traits #48204

Open
MahmoudGSaleh opened this issue Jan 23, 2021 · 6 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla c++20 clang:to-be-triaged Should not be used for new issues

Comments

@MahmoudGSaleh
Copy link

MahmoudGSaleh commented Jan 23, 2021

Bugzilla Link 48860
Version 11.0
OS Windows NT
CC @CaseyCarter,@zygoloid

Extended Description

MSVC recently implemented the following intrinsics needed for P0466R5 Layout-compatibility and Pointer-interconvertibility Traits:

  • __builtin_is_layout_compatible
  • __builtin_is_pointer_interconvertible_base_of
  • __builtin_is_pointer_interconvertible_with_class
  • __builtin_is_corresponding_member

MSVC STL is planning to use them in <type_traits> as follows (PR: microsoft/STL#1575):

// STRUCT TEMPLATE is_layout_compatible
template <class _Ty1, class _Ty2>
struct is_layout_compatible : bool_constant<__builtin_is_layout_compatible(_Ty1, _Ty2)> {};

template <class _Ty1, class _Ty2>
inline constexpr bool is_layout_compatible_v = __builtin_is_layout_compatible(_Ty1, _Ty2);

// STRUCT TEMPLATE is_pointer_interconvertible_base_of
template <class _Base, class _Derived>
struct is_pointer_interconvertible_base_of
    : bool_constant<__builtin_is_pointer_interconvertible_base_of(_Base, _Derived)> {};

template <class _Base, class _Derived>
inline constexpr bool is_pointer_interconvertible_base_of_v = __builtin_is_pointer_interconvertible_base_of(
    _Base, _Derived);

// FUNCTION TEMPLATE is_pointer_interconvertible_with_class
template <class _ClassTy, class _MemberTy>
_NODISCARD constexpr bool is_pointer_interconvertible_with_class(_MemberTy _ClassTy::*_Pm) noexcept {
    return __builtin_is_pointer_interconvertible_with_class(_ClassTy, _Pm);
}

// FUNCTION TEMPLATE is_corresponding_member
template <class _ClassTy1, class _ClassTy2, class _MemberTy1, class _MemberTy2>
_NODISCARD constexpr bool is_corresponding_member(_MemberTy1 _ClassTy1::*_Pm1, _MemberTy2 _ClassTy2::*_Pm2) noexcept {
    return __builtin_is_corresponding_member(_ClassTy1, _ClassTy2, _Pm1, _Pm2);
@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Jan 24, 2021

Do we know why MSVC has deviated from the longstanding rule that compiler intrinsic forms of type traits are named by prefixing simply __ to the name? (Eg, __is_layout_compatible, __is_pointer_interconvertible_base_of, __is_pointer_interconvertible_with_class, __is_corresponding_member.)

In particular, if MSVC is avoiding name collisions with something, will we need to avoid adding the normal name in addition to the __builtin_ name?

@MahmoudGSaleh
Copy link
Author

MahmoudGSaleh commented Jan 26, 2021

Do we know why MSVC has deviated from the longstanding rule that compiler
intrinsic forms of type traits are named by prefixing simply __ to the name?
(Eg, __is_layout_compatible, __is_pointer_interconvertible_base_of,
__is_pointer_interconvertible_with_class, __is_corresponding_member.)

In particular, if MSVC is avoiding name collisions with something, will we
need to avoid adding the normal name in addition to the _builtin name?

Agree and thanks for catching this. In the past we've used the __builtin* form; e.g. __builtin_zero_non_value_bits, however not in <type_traits>. We will rename the intrinsics to follow the __is_* form instead. The change should appear in Visual Studio 2019 version 16.10 preview 1.
I will update this bug report once those renames are shipped.

@MahmoudGSaleh
Copy link
Author

MahmoudGSaleh commented Mar 4, 2021

Update: The C++ toolset update renaming the intrinsics (removing the __builtin prefix) has shipped with VS 2019 preview 1.

MSVC STL moved to the new names as well: https://github.com/microsoft/STL/pull/1711/files

@MahmoudGSaleh
Copy link
Author

MahmoudGSaleh commented Mar 4, 2021

With the latest MSVC update, the intrinsic names are:

  • __is_layout_compatible
  • __is_pointer_interconvertible_base_of
  • __is_pointer_interconvertible_with_class
  • __is_corresponding_member

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@ldionne
Copy link
Member

ldionne commented Feb 7, 2023

GCC also does this. It would be great to implement this in Clang so that libc++ can start using the traits.

@cor3ntin
Copy link
Contributor

@MahmoudGSaleh how should msvc::no_unique_address behave for the purpose of layout compatibility, in the context of https://cplusplus.github.io/CWG/issues/2759.html ?

Endilll added a commit that referenced this issue Feb 20, 2024
This patch implements `__is_layout_compatible` intrinsic, which supports
`std::is_layout_compatible` type trait introduced in C++20
([P0466R5](https://wg21.link/p0466r5) "Layout-compatibility and
Pointer-interconvertibility Traits"). Name matches GCC and MSVC
intrinsic.

Basically, this patch exposes our existing machinery for checking for
layout compatibility and figuring out common initial sequences. Said
machinery is a bit outdated, as it doesn't implement
[CWG1719](https://cplusplus.github.io/CWG/issues/1719.html) "Layout
compatibility and cv-qualification revisited" and
[CWG2759](https://cplusplus.github.io/CWG/issues/2759.html)
"`[[no_unique_address]` and common initial sequence". Those defect
reports are considered out of scope of of this PR, but will be
implemented in subsequent PRs.

Partially addresses #48204
@Endilll Endilll added the clang:to-be-triaged Should not be used for new issues label Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++20 clang:to-be-triaged Should not be used for new issues
Projects
Status: In Progress
Development

No branches or pull requests

5 participants