Skip to content

Commit ebf5bb7

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:ba89749cd249 into amd-gfx:461597c1055d
Local branch amd-gfx 461597c Merged main:0a9c6bea6b08 into amd-gfx:fad78e377956 Remote branch main ba89749 [libc++] <experimental/simd> Add implicit type conversion constructor for class simd/simd_mask (llvm#71132)
2 parents 461597c + ba89749 commit ebf5bb7

File tree

16 files changed

+242
-39
lines changed

16 files changed

+242
-39
lines changed

libcxx/docs/Status/ParallelismProjects.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Section,Description,Dependencies,Assignee,Complete
2020
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
2121
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd default constructor <https://github.com/llvm/llvm-project/pull/70424>`_", None, Yin Zhang, |Complete|
2222
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
23+
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd implicit type conversion constructor <https://github.com/llvm/llvm-project/pull/71132>`_", None, Yin Zhang, |Complete|
2324
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd generate constructor <https://reviews.llvm.org/D159442>`_", None, Yin Zhang, |Complete|
2425
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
2526
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "Class template simd implementation", None, Yin Zhang, |In Progress|
@@ -28,6 +29,7 @@ Section,Description,Dependencies,Assignee,Complete
2829
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.class] simd<>::size(), Yin Zhang, |Complete|
2930
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask default constructor <https://github.com/llvm/llvm-project/pull/70424>`_", None, Yin Zhang, |Complete|
3031
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
32+
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask implicit type conversion constructor <https://github.com/llvm/llvm-project/pull/71132>`_", None, Yin Zhang, |Complete|
3133
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
3234
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "Class template simd_mask implementation", None, Yin Zhang, |In Progress|
3335
| `[parallel.simd.mask.nonmembers] <https://wg21.link/N4808>`_, "simd_mask non-member operations", None, Yin Zhang, |In Progress|

libcxx/include/experimental/__simd/simd.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ class simd {
4646
template <class _Up, enable_if_t<__can_broadcast_v<value_type, __remove_cvref_t<_Up>>, int> = 0>
4747
_LIBCPP_HIDE_FROM_ABI simd(_Up&& __v) noexcept : __s_(_Impl::__broadcast(static_cast<value_type>(__v))) {}
4848

49+
// implicit type conversion constructor
50+
template <class _Up,
51+
enable_if_t<!is_same_v<_Up, _Tp> && is_same_v<abi_type, simd_abi::fixed_size<size()>> &&
52+
__is_non_narrowing_convertible_v<_Up, value_type>,
53+
int> = 0>
54+
_LIBCPP_HIDE_FROM_ABI simd(const simd<_Up, simd_abi::fixed_size<size()>>& __v) noexcept {
55+
for (size_t __i = 0; __i < size(); __i++) {
56+
(*this)[__i] = static_cast<value_type>(__v[__i]);
57+
}
58+
}
59+
4960
// generator constructor
5061
template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
5162
explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept

libcxx/include/experimental/__simd/simd_mask.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class simd_mask {
4343
// broadcast constructor
4444
_LIBCPP_HIDE_FROM_ABI explicit simd_mask(value_type __v) noexcept : __s_(_Impl::__broadcast(__v)) {}
4545

46+
// implicit type conversion constructor
47+
template <class _Up, enable_if_t<!is_same_v<_Up, _Tp> && is_same_v<abi_type, simd_abi::fixed_size<size()>>, int> = 0>
48+
_LIBCPP_HIDE_FROM_ABI simd_mask(const simd_mask<_Up, simd_abi::fixed_size<size()>>& __v) noexcept {
49+
for (size_t __i = 0; __i < size(); __i++) {
50+
(*this)[__i] = __v[__i];
51+
}
52+
}
53+
4654
// scalar access [simd.mask.subscr]
4755
_LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
4856
_LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// <experimental/simd>
12+
//
13+
// [simd.class]
14+
// template<class U> simd(const simd<U, simd_abi::fixed_size<size()>>&) noexcept;
15+
16+
#include "../test_utils.h"
17+
#include <experimental/simd>
18+
19+
namespace ex = std::experimental::parallelism_v2;
20+
21+
template <class T, class SimdAbi, std::size_t array_size>
22+
struct ConversionHelper {
23+
const std::array<T, array_size>& expected_value;
24+
25+
ConversionHelper(const std::array<T, array_size>& value) : expected_value(value) {}
26+
27+
template <class U>
28+
void operator()() const {
29+
if constexpr (!std::is_same_v<U, T> && std::is_same_v<SimdAbi, ex::simd_abi::fixed_size<array_size>> &&
30+
is_non_narrowing_convertible_v<U, T>) {
31+
static_assert(noexcept(ex::simd<T, SimdAbi>(ex::simd<U, SimdAbi>{})));
32+
ex::simd<U, SimdAbi> origin_simd([](U i) { return i; });
33+
ex::simd<T, SimdAbi> simd_from_implicit_conversion(origin_simd);
34+
assert_simd_values_equal<array_size>(simd_from_implicit_conversion, expected_value);
35+
}
36+
}
37+
};
38+
39+
template <class T, std::size_t>
40+
struct CheckConversionSimdCtor {
41+
template <class SimdAbi>
42+
void operator()() {
43+
constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
44+
std::array<T, array_size> expected_value;
45+
for (size_t i = 0; i < array_size; ++i)
46+
expected_value[i] = static_cast<T>(i);
47+
48+
types::for_each(arithmetic_no_bool_types(), ConversionHelper<T, SimdAbi, array_size>(expected_value));
49+
}
50+
};
51+
52+
template <class T, class SimdAbi, std::size_t array_size>
53+
struct CheckConversionSimdCtorTraitsHelper {
54+
template <class U>
55+
void operator()() {
56+
if constexpr (!std::is_same_v<U, T>) {
57+
if constexpr (std::is_same_v<SimdAbi, ex::simd_abi::fixed_size<array_size>> &&
58+
is_non_narrowing_convertible_v<U, T>)
59+
static_assert(std::is_convertible_v<ex::simd<U, SimdAbi>, ex::simd<T, SimdAbi>>);
60+
else
61+
static_assert(!std::is_convertible_v<ex::simd<U, SimdAbi>, ex::simd<T, SimdAbi>>);
62+
}
63+
}
64+
};
65+
66+
template <class T, std::size_t>
67+
struct CheckConversionSimdCtorTraits {
68+
template <class SimdAbi>
69+
void operator()() {
70+
constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
71+
72+
types::for_each(arithmetic_no_bool_types(), CheckConversionSimdCtorTraitsHelper<T, SimdAbi, array_size>());
73+
}
74+
};
75+
76+
int main(int, char**) {
77+
test_all_simd_abi<CheckConversionSimdCtor>();
78+
test_all_simd_abi<CheckConversionSimdCtorTraits>();
79+
return 0;
80+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// <experimental/simd>
12+
//
13+
// [simd.class]
14+
// template<class U> simd_mask(const simd_mask<U, simd_abi::fixed_size<size()>>&) noexcept;
15+
16+
#include "../test_utils.h"
17+
#include <experimental/simd>
18+
19+
namespace ex = std::experimental::parallelism_v2;
20+
21+
template <class T, class SimdAbi, std::size_t array_size>
22+
struct ConversionHelper {
23+
const std::array<bool, array_size>& expected_value;
24+
25+
ConversionHelper(const std::array<bool, array_size>& value) : expected_value(value) {}
26+
27+
template <class U>
28+
void operator()() const {
29+
if constexpr (!std::is_same_v<U, T> && std::is_same_v<SimdAbi, ex::simd_abi::fixed_size<array_size>>) {
30+
static_assert(noexcept(ex::simd_mask<T, SimdAbi>(ex::simd_mask<U, SimdAbi>{})));
31+
ex::simd_mask<U, SimdAbi> origin_mask(false);
32+
ex::simd_mask<T, SimdAbi> mask_from_implicit_conversion(origin_mask);
33+
assert_simd_mask_values_equal<array_size>(mask_from_implicit_conversion, expected_value);
34+
}
35+
}
36+
};
37+
38+
template <class T, std::size_t>
39+
struct CheckConversionMaskCtor {
40+
template <class SimdAbi>
41+
void operator()() {
42+
constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
43+
std::array<bool, array_size> expected_value{};
44+
45+
types::for_each(arithmetic_no_bool_types(), ConversionHelper<T, SimdAbi, array_size>(expected_value));
46+
}
47+
};
48+
49+
template <class T, class SimdAbi, std::size_t array_size>
50+
struct CheckConversionMaskCtorTraitsHelper {
51+
template <class U>
52+
void operator()() {
53+
if constexpr (!std::is_same_v<U, T>) {
54+
if constexpr (std::is_same_v<SimdAbi, ex::simd_abi::fixed_size<array_size>>)
55+
static_assert(std::is_convertible_v<ex::simd_mask<U, SimdAbi>, ex::simd_mask<T, SimdAbi>>);
56+
else
57+
static_assert(!std::is_convertible_v<ex::simd_mask<U, SimdAbi>, ex::simd_mask<T, SimdAbi>>);
58+
}
59+
}
60+
};
61+
62+
template <class T, std::size_t>
63+
struct CheckConversionMaskCtorTraits {
64+
template <class SimdAbi>
65+
void operator()() {
66+
constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
67+
68+
types::for_each(arithmetic_no_bool_types(), CheckConversionMaskCtorTraitsHelper<T, SimdAbi, array_size>());
69+
}
70+
};
71+
72+
int main(int, char**) {
73+
test_all_simd_abi<CheckConversionMaskCtor>();
74+
test_all_simd_abi<CheckConversionMaskCtorTraits>();
75+
return 0;
76+
}

libcxxabi/src/abort_message.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,15 @@
1414
extern "C" _LIBCXXABI_HIDDEN _LIBCXXABI_NORETURN void
1515
abort_message(const char *format, ...) __attribute__((format(printf, 1, 2)));
1616

17+
#ifndef _LIBCXXABI_ASSERT
18+
# define _LIBCXXABI_ASSERT(expr, msg) \
19+
do { \
20+
if (!(expr)) { \
21+
char const* __msg = (msg); \
22+
::abort_message("%s:%d: %s", __FILE__, __LINE__, __msg); \
23+
} \
24+
} while (false)
25+
1726
#endif
27+
28+
#endif // __ABORT_MESSAGE_H_

libcxxabi/src/cxa_demangle.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
// file does not yet support:
1111
// - C++ modules TS
1212

13+
#include "abort_message.h"
14+
#define DEMANGLE_ASSERT(expr, msg) _LIBCXXABI_ASSERT(expr, msg)
15+
1316
#include "demangle/DemangleConfig.h"
1417
#include "demangle/ItaniumDemangle.h"
1518
#include "__cxxabi_config.h"
16-
#include <cassert>
1719
#include <cctype>
1820
#include <cstdio>
1921
#include <cstdlib>
@@ -395,7 +397,7 @@ __cxa_demangle(const char *MangledName, char *Buf, size_t *N, int *Status) {
395397
InternalStatus = demangle_invalid_mangled_name;
396398
else {
397399
OutputBuffer O(Buf, N);
398-
assert(Parser.ForwardTemplateRefs.empty());
400+
DEMANGLE_ASSERT(Parser.ForwardTemplateRefs.empty(), "");
399401
AST->print(O);
400402
O += '\0';
401403
if (N != nullptr)

libcxxabi/src/demangle/DemangleConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
#define DEMANGLE_FALLTHROUGH
100100
#endif
101101

102+
#ifndef DEMANGLE_ASSERT
103+
#include <cassert>
104+
#define DEMANGLE_ASSERT(__expr, __msg) assert((__expr) && (__msg))
105+
#endif
106+
102107
#define DEMANGLE_NAMESPACE_BEGIN namespace { namespace itanium_demangle {
103108
#define DEMANGLE_NAMESPACE_END } }
104109

libcxxabi/src/demangle/ItaniumDemangle.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Utility.h"
2222
#include <__cxxabi_config.h>
2323
#include <algorithm>
24-
#include <cassert>
2524
#include <cctype>
2625
#include <cstdio>
2726
#include <cstdlib>
@@ -129,12 +128,12 @@ template <class T, size_t N> class PODSmallVector {
129128

130129
// NOLINTNEXTLINE(readability-identifier-naming)
131130
void pop_back() {
132-
assert(Last != First && "Popping empty vector!");
131+
DEMANGLE_ASSERT(Last != First, "Popping empty vector!");
133132
--Last;
134133
}
135134

136135
void shrinkToSize(size_t Index) {
137-
assert(Index <= size() && "shrinkToSize() can't expand!");
136+
DEMANGLE_ASSERT(Index <= size(), "shrinkToSize() can't expand!");
138137
Last = First + Index;
139138
}
140139

@@ -144,11 +143,11 @@ template <class T, size_t N> class PODSmallVector {
144143
bool empty() const { return First == Last; }
145144
size_t size() const { return static_cast<size_t>(Last - First); }
146145
T &back() {
147-
assert(Last != First && "Calling back() on empty vector!");
146+
DEMANGLE_ASSERT(Last != First, "Calling back() on empty vector!");
148147
return *(Last - 1);
149148
}
150149
T &operator[](size_t Index) {
151-
assert(Index < size() && "Invalid access!");
150+
DEMANGLE_ASSERT(Index < size(), "Invalid access!");
152151
return *(begin() + Index);
153152
}
154153
void clear() { Last = First; }
@@ -1678,7 +1677,7 @@ class SpecialSubstitution final : public ExpandedSpecialSubstitution {
16781677
std::string_view SV = ExpandedSpecialSubstitution::getBaseName();
16791678
if (isInstantiation()) {
16801679
// The instantiations are typedefs that drop the "basic_" prefix.
1681-
assert(starts_with(SV, "basic_"));
1680+
DEMANGLE_ASSERT(starts_with(SV, "basic_"), "");
16821681
SV.remove_prefix(sizeof("basic_") - 1);
16831682
}
16841683
return SV;
@@ -2569,7 +2568,7 @@ void Node::visit(Fn F) const {
25692568
return F(static_cast<const X *>(this));
25702569
#include "ItaniumNodes.def"
25712570
}
2572-
assert(0 && "unknown mangling node kind");
2571+
DEMANGLE_ASSERT(0, "unknown mangling node kind");
25732572
}
25742573

25752574
/// Determine the kind of a node from its type.
@@ -2611,7 +2610,8 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
26112610
Parser->TemplateParams.push_back(&Params);
26122611
}
26132612
~ScopedTemplateParamList() {
2614-
assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2613+
DEMANGLE_ASSERT(Parser->TemplateParams.size() >= OldNumTemplateParamLists,
2614+
"");
26152615
Parser->TemplateParams.shrinkToSize(OldNumTemplateParamLists);
26162616
}
26172617
TemplateParamList *params() { return &Params; }
@@ -2692,7 +2692,7 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
26922692
}
26932693

26942694
NodeArray popTrailingNodeArray(size_t FromPosition) {
2695-
assert(FromPosition <= Names.size());
2695+
DEMANGLE_ASSERT(FromPosition <= Names.size(), "");
26962696
NodeArray res =
26972697
makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
26982698
Names.shrinkToSize(FromPosition);
@@ -2859,8 +2859,8 @@ template <typename Derived, typename Alloc> struct AbstractManglingParser {
28592859
std::string_view getSymbol() const {
28602860
std::string_view Res = Name;
28612861
if (Kind < Unnameable) {
2862-
assert(starts_with(Res, "operator") &&
2863-
"operator name does not start with 'operator'");
2862+
DEMANGLE_ASSERT(starts_with(Res, "operator"),
2863+
"operator name does not start with 'operator'");
28642864
Res.remove_prefix(sizeof("operator") - 1);
28652865
if (starts_with(Res, ' '))
28662866
Res.remove_prefix(1);
@@ -3694,7 +3694,7 @@ Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
36943694
}
36953695
}
36963696

3697-
assert(SoFar != nullptr);
3697+
DEMANGLE_ASSERT(SoFar != nullptr, "");
36983698

36993699
Node *Base = getDerived().parseBaseUnresolvedName();
37003700
if (Base == nullptr)
@@ -5635,7 +5635,8 @@ Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
56355635
Node *ForwardRef = make<ForwardTemplateReference>(Index);
56365636
if (!ForwardRef)
56375637
return nullptr;
5638-
assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5638+
DEMANGLE_ASSERT(ForwardRef->getKind() == Node::KForwardTemplateReference,
5639+
"");
56395640
ForwardTemplateRefs.push_back(
56405641
static_cast<ForwardTemplateReference *>(ForwardRef));
56415642
return ForwardRef;

libcxxabi/src/demangle/Utility.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "DemangleConfig.h"
2020

2121
#include <array>
22-
#include <cassert>
2322
#include <cstdint>
2423
#include <cstdlib>
2524
#include <cstring>
@@ -159,7 +158,7 @@ class OutputBuffer {
159158
}
160159

161160
void insert(size_t Pos, const char *S, size_t N) {
162-
assert(Pos <= CurrentPosition);
161+
DEMANGLE_ASSERT(Pos <= CurrentPosition, "");
163162
if (N == 0)
164163
return;
165164
grow(N);
@@ -172,7 +171,7 @@ class OutputBuffer {
172171
void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; }
173172

174173
char back() const {
175-
assert(CurrentPosition);
174+
DEMANGLE_ASSERT(CurrentPosition, "");
176175
return Buffer[CurrentPosition - 1];
177176
}
178177

0 commit comments

Comments
 (0)