Skip to content

Commit

Permalink
* Add support and codepaths for C++20's [[no_unique_address]].
Browse files Browse the repository at this point in the history
* Add many more tests for `detail::__compressed_pair`'s layout and ABI.
  • Loading branch information
brycelelbach committed Jun 9, 2021
1 parent 96f23f0 commit 972a4bc
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 267 deletions.
1 change: 1 addition & 0 deletions compilation_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_compilation_test(ctest_mdspan_convertible)
add_compilation_test(ctest_standard_layout)
add_compilation_test(ctest_trivially_copyable)
add_compilation_test(ctest_no_unique_address)
add_compilation_test(ctest_compressed_pair_layout)
add_compilation_test(ctest_constexpr_dereference)
add_compilation_test(ctest_constexpr_submdspan)
add_compilation_test(ctest_constexpr_layouts)
145 changes: 145 additions & 0 deletions compilation_tests/ctest_compressed_pair_layout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 2.0
// Copyright (2020) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
//
// ************************************************************************
//@HEADER
*/

#include "ctest_common.hpp"

#include <experimental/mdspan>

#include <type_traits>

namespace stdex = std::experimental;

enum emptyness {
non_empty = false,
empty = true
};

enum standard_layoutness {
non_standard_layout = false,
standard_layout = true
};

enum trivially_copyableness {
non_trivially_copyable = false,
trivially_copyable = true
};

template <class T, size_t Size,
emptyness Empty = non_empty,
standard_layoutness StandardLayout = standard_layout,
trivially_copyableness TriviallyCopyable = trivially_copyable>
void test() {
MDSPAN_STATIC_TEST(sizeof(T) == Size);
MDSPAN_STATIC_TEST(std::is_empty_v<T> == Empty);
MDSPAN_STATIC_TEST(std::is_standard_layout_v<T> == StandardLayout);
MDSPAN_STATIC_TEST(std::is_trivially_copyable_v<T> == TriviallyCopyable);
}

template <class T, class U>
using CP = stdex::detail::__compressed_pair<T, U>;

struct E0 {};
struct E1 {};
struct E2 {};
struct E3 {};

void instantiate_tests() {
//==============================================================================
// <editor-fold desc="compressed pair layout: 2 leaf elements"> {{{1
test<CP<E0, E0>, 2, empty>();
test<CP<E0, E1>, 1, empty>();
test<CP<int*, E1>, sizeof(int*), non_empty>();
test<CP<E0, int*>, sizeof(int*), non_empty>();
test<CP<int*, int*>, 2 * sizeof(int*), non_empty>();
// </editor-fold> end compressed pair layout: 2 leaf elements }}}1
//==============================================================================

//==============================================================================
// <editor-fold desc="compressed pair layout: 1 nested pair, 3 leaf element"> {{{1
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS)
test<CP<E0, CP<E0, E0>>, 3, empty>(); // Emulation can't handle this correctly.
#endif
test<CP<E0, CP<E1, E2>>, 1, empty>();
test<CP<E0, CP<E1, int*>>, sizeof(int*), non_empty>();
test<CP<E0, CP<int*, E2>>, sizeof(int*), non_empty>();
test<CP<E0, CP<int*, int*>>, 2 * sizeof(int*), non_empty>();
test<CP<int*, CP<E1, E2>>, sizeof(int*), non_empty>();
test<CP<int*, CP<E1, int*>>, 2 * sizeof(int*), non_empty>();
test<CP<int*, CP<int*, E2>>, 2 * sizeof(int*), non_empty>();
test<CP<int*, CP<int*, int*>>, 3 * sizeof(int*), non_empty>();
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS)
test<CP<CP<E0, E0>, E0>, 3, empty>(); // Emulation can't handle this correctly.
#endif
test<CP<CP<E0, E1>, E2>, 1, empty>();
test<CP<CP<E0, E1>, int*>, sizeof(int*), non_empty>();
test<CP<CP<E0, int*>, E2>, sizeof(int*), non_empty>();
test<CP<CP<E0, int*>, int*>, 2 * sizeof(int*), non_empty>();
test<CP<CP<int*, E1>, E2>, sizeof(int*), non_empty>();
test<CP<CP<int*, E1>, int*>, 2 * sizeof(int*), non_empty>();
test<CP<CP<int*, int*>, E2>, 2 * sizeof(int*), non_empty>();
test<CP<CP<int*, int*>, int*>, 3 * sizeof(int*), non_empty>();
// </editor-fold> end compressed pair layout: 1 nested pair, 3 leaf element }}}1
//==============================================================================

//==============================================================================
// <editor-fold desc="compressed pair layout: 2 nested pairs, 4 leaf element"> {{{1
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS)
test<CP<CP<E0, E0>, CP<E0, E0>>, 4, empty>(); // Emulation can't handle this correctly.
#endif
test<CP<CP<E0, E1>, CP<E2, E3>>, 1, empty>();
test<CP<CP<E0, E1>, CP<E2, int*>>, sizeof(int*), non_empty>();
test<CP<CP<E0, E1>, CP<int*, E3>>, sizeof(int*), non_empty>();
test<CP<CP<E0, E1>, CP<int*, int*>>, 2 * sizeof(int*), non_empty>();
test<CP<CP<E0, int*>, CP<E2, int*>>, 2 * sizeof(int*), non_empty>();
test<CP<CP<E0, int*>, CP<int*, E3>>, 2 * sizeof(int*), non_empty>();
test<CP<CP<E0, int*>, CP<int*, int*>>, 3 * sizeof(int*), non_empty>();
test<CP<CP<int*, E1>, CP<E2, int*>>, 2 * sizeof(int*), non_empty>();
test<CP<CP<int*, E1>, CP<int*, E3>>, 2 * sizeof(int*), non_empty>();
test<CP<CP<int*, E1>, CP<int*, int*>>, 3 * sizeof(int*), non_empty>();
test<CP<CP<int*, int*>, CP<E2, int*>>, 3 * sizeof(int*), non_empty>();
test<CP<CP<int*, int*>, CP<int*, E3>>, 3 * sizeof(int*), non_empty>();
test<CP<CP<int*, int*>, CP<int*, int*>>, 4 * sizeof(int*), non_empty>();
// </editor-fold> end compressed pair layout: 2 nested pairs, 4 leaf elements }}}1
//==============================================================================
}

21 changes: 0 additions & 21 deletions compilation_tests/ctest_standard_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,6 @@ namespace stdex = std::experimental;
//==============================================================================
// <editor-fold desc="helper utilities"> {{{1

struct empty1 { };
struct empty2 { };

MDSPAN_STATIC_TEST(
std::is_standard_layout<
stdex::detail::__compressed_pair<empty1, empty2>
>::value
);

MDSPAN_STATIC_TEST(
std::is_standard_layout<
stdex::detail::__compressed_pair<int*, empty2>
>::value
);

MDSPAN_STATIC_TEST(
std::is_standard_layout<
stdex::detail::__compressed_pair<int*, stdex::detail::__compressed_pair<empty1, empty2>>
>::value
);

MDSPAN_STATIC_TEST(
!std::is_base_of<stdex::extents<1, 2, 3>, stdex::detail::__partially_static_sizes<1, 2, 3>>::value
);
Expand Down
21 changes: 0 additions & 21 deletions compilation_tests/ctest_trivially_copyable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,6 @@ namespace stdex = std::experimental;
//==============================================================================
// <editor-fold desc="helper utilities"> {{{1

struct empty1 { };
struct empty2 { };

MDSPAN_STATIC_TEST(
std::is_trivially_copyable<
stdex::detail::__compressed_pair<empty1, empty2>
>::value
);

MDSPAN_STATIC_TEST(
std::is_trivially_copyable<
stdex::detail::__compressed_pair<int*, empty2>
>::value
);

MDSPAN_STATIC_TEST(
std::is_trivially_copyable<
stdex::detail::__compressed_pair<int*, stdex::detail::__compressed_pair<empty1, empty2>>
>::value
);

MDSPAN_STATIC_TEST(
!std::is_base_of<stdex::extents<1, 2, 3>, stdex::detail::__partially_static_sizes<1, 2, 3>>::value
);
Expand Down
5 changes: 1 addition & 4 deletions include/experimental/__p0009_bits/basic_mdspan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "layout_right.hpp"
#include "extents.hpp"
#include "trait_backports.hpp"
#include "compressed_pair.hpp"

namespace std {
namespace experimental {
Expand Down Expand Up @@ -77,10 +78,6 @@ class basic_mdspan<
{
private:

using __mapping_base_t = detail::__no_unique_address_emulation<
typename LayoutPolicy::template mapping<experimental::extents<Exts...>>, 0>;
using __accessor_base_t = detail::__no_unique_address_emulation<AccessorPolicy, 1>;

// Workaround for non-deducibility of the index sequence template parameter if it's given at the top level
template <class>
struct __impl_impl;
Expand Down
Loading

0 comments on commit 972a4bc

Please sign in to comment.