Skip to content

Commit 05740cc

Browse files
[SYCL] Fix a bug with sycl::sample on marray types (#12694)
1 parent cdd3b9c commit 05740cc

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

sycl/include/sycl/builtins_preview.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,22 @@ auto builtin_default_host_impl(FuncTy F, const Ts &...x) {
170170
template <typename FuncTy, typename... Ts>
171171
auto builtin_delegate_to_scalar(FuncTy F, const Ts &...x) {
172172
using T = typename first_type<Ts...>::type;
173-
if constexpr (is_vec_or_swizzle_v<T>) {
174-
using ret_elem_type = decltype(F(x[0]...));
175-
// TODO: using r{} to avoid Werror. Not sure if ok.
176-
vec<ret_elem_type, T::size()> r{};
177-
loop<T::size()>([&](auto idx) { r[idx] = F(x[idx]...); });
178-
return r;
173+
static_assert(is_vec_or_swizzle_v<T> || is_marray_v<T>);
174+
175+
constexpr auto Size = T::size();
176+
using ret_elem_type = decltype(F(x[0]...));
177+
std::conditional_t<is_marray_v<T>, marray<ret_elem_type, Size>,
178+
vec<ret_elem_type, Size>>
179+
r{};
180+
181+
if constexpr (is_marray_v<T>) {
182+
for (size_t i = 0; i < Size; ++i)
183+
r[i] = F(x[i]...);
179184
} else {
180-
static_assert(is_marray_v<T>);
181-
return builtin_marray_impl(F, x...);
185+
loop<Size>([&](auto idx) { r[idx] = F(x[idx]...); });
182186
}
187+
188+
return r;
183189
}
184190

185191
template <typename T>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
// RUN: %if preview-breaking-changes-supported %{ %{build} -fpreview-breaking-changes -o %t_preview.out %}
4+
// RUN: %if preview-breaking-changes-supported %{ %{run} %t_preview.out%}
5+
6+
#include <sycl/sycl.hpp>
7+
8+
int main() {
9+
using namespace sycl;
10+
queue q;
11+
12+
auto Test = [&](auto F, auto Expected, auto... Args) {
13+
std::tuple ArgsTuple{Args...};
14+
auto Result = std::apply(F, ArgsTuple);
15+
static_assert(std::is_same_v<decltype(Expected), decltype(Result)>);
16+
17+
auto Equal = [](auto x, auto y) {
18+
for (size_t i = 0; i < x.size(); ++i)
19+
if (x[i] != y[i])
20+
return false;
21+
22+
return true;
23+
};
24+
25+
assert(Equal(Result, Expected));
26+
27+
buffer<bool, 1> ResultBuf{1};
28+
q.submit([&](handler &cgh) {
29+
accessor Result{ResultBuf, cgh};
30+
cgh.single_task([=]() {
31+
auto R = std::apply(F, ArgsTuple);
32+
static_assert(std::is_same_v<decltype(Expected), decltype(R)>);
33+
Result[0] = Equal(R, Expected);
34+
});
35+
});
36+
assert(host_accessor{ResultBuf}[0]);
37+
};
38+
39+
{
40+
// Test upsample:
41+
auto Upsample = [](auto... xs) { return upsample(xs...); };
42+
Test(Upsample, marray<int16_t, 2>{0x203, 0x302}, marray<int8_t, 2>{2, 3},
43+
marray<uint8_t, 2>{3, 2});
44+
Test(Upsample, marray<uint16_t, 2>{0x203, 0x302}, marray<uint8_t, 2>{2, 3},
45+
marray<uint8_t, 2>{3, 2});
46+
}
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)