Skip to content

Commit 6e095fd

Browse files
authored
[SYCL][E2E] Test recursion in device code (#15605)
Recently we allowed any C++ features to be used in manifestly constant-evaluated expressions in device code. This is an E2E test case testing that recursion can be safely used in this way.
1 parent ea95271 commit 6e095fd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
//==---------- recursion-in-constexpr.cpp - test recursion in constexpr ----==//
5+
//
6+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7+
// See https://llvm.org/LICENSE.txt for license information.
8+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9+
//
10+
//===----------------------------------------------------------------------===//
11+
#include <iostream>
12+
#include <sycl/detail/core.hpp>
13+
14+
15+
unsigned long long constexpr factorial(int n) {
16+
if (n == 0)
17+
return 1;
18+
return n * factorial(n - 1);
19+
}
20+
21+
constexpr int X = 5;
22+
constexpr int DataLen = 5;
23+
24+
template <int A> struct GetNTTP {
25+
static const int N = A;
26+
};
27+
28+
int main() {
29+
sycl::queue q;
30+
int res[DataLen] = {0};
31+
{
32+
sycl::buffer<int> buf{res, {DataLen}};
33+
34+
q.submit([&](sycl::handler &cgh) {
35+
sycl::accessor acc{buf, cgh};
36+
cgh.single_task([=] {
37+
constexpr int C = factorial(X);
38+
for (int i = 0; i < DataLen; ++i)
39+
acc[i] = C;
40+
acc[DataLen - 1] = GetNTTP<factorial(X)>::N;
41+
});
42+
});
43+
}
44+
45+
for (int i = 0; i < DataLen; ++i) {
46+
if (res[i] != factorial(X)) {
47+
std::cout << "FAIL " << std::endl;
48+
return -1;
49+
}
50+
}
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)