Skip to content

Commit 6ba05b7

Browse files
[SYCL][E2E] Add more tests for virtual functions (#15067)
This commit still doesn't bring an exhaustive coverage for the feature, but still improves the situation by checking the following scenarios: - using math built-ins from virtual functions - using group barriers from virtual functions - using virtual functions in nd-range kernels where every work-item calls a different virtual function - using virtual functions when the code is scattered across several translation units Some tests are disabled, because we do not support those scenarios yet and more changes are required to make them work.
1 parent 885b14e commit 6ba05b7

File tree

12 files changed

+796
-0
lines changed

12 files changed

+796
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// REQUIRES: aspect-usm_shared_allocations
2+
//
3+
// On CPU it segfaults within the kernel that performs virtual function call.
4+
// XFAIL: cpu
5+
// XFAIL-TRACKER: https://github.com/intel/llvm/issues/15080
6+
// UNSUPPORTED: gpu
7+
// On GPU this test (its older version which used nd_item instead of group)
8+
// used to fail with UR_RESULT_ERROR_PROGRAM_LINK_FAILURE.
9+
// SPIR-V files produced by SYCL_DUMP_IMAGES could be linked just fine (using
10+
// both llvm-spirv -r + llvm-link and ocloc).
11+
// Current version hangs and therefore it is marked as unsupported to avoid
12+
// wasting time in CI and potentially blocking a machine.
13+
// Reported in https://github.com/intel/llvm/issues/15068
14+
//
15+
// This test checks that group operations (barrier in this case) work correctly
16+
// inside virtual functions.
17+
//
18+
// RUN: %{build} -o %t.out %helper-includes
19+
// RUN: %{run} %t.out
20+
21+
#include <sycl/detail/core.hpp>
22+
#include <sycl/group_algorithm.hpp>
23+
#include <sycl/group_barrier.hpp>
24+
#include <sycl/usm.hpp>
25+
26+
#include "helpers.hpp"
27+
28+
#include <iostream>
29+
#include <numeric>
30+
31+
namespace oneapi = sycl::ext::oneapi::experimental;
32+
33+
class BaseOp {
34+
public:
35+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
36+
virtual int apply(int *, sycl::group<1>) = 0;
37+
38+
virtual int computeReference(sycl::range<1> LocalRange, int Init) = 0;
39+
};
40+
41+
class SumOp : public BaseOp {
42+
public:
43+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
44+
int apply(int *LocalData, sycl::group<1> WG) override {
45+
LocalData[WG.get_local_id()] = WG.get_local_id() + WG.get_group_id();
46+
sycl::group_barrier(WG);
47+
if (WG.leader()) {
48+
int Res = 0;
49+
for (size_t I = 0; I < WG.get_local_range().size(); ++I) {
50+
Res += LocalData[I];
51+
}
52+
LocalData[0] = Res;
53+
}
54+
sycl::group_barrier(WG);
55+
56+
return LocalData[0];
57+
}
58+
59+
int computeReference(sycl::range<1> LocalRange, int WGID) override {
60+
std::vector<int> LocalData(LocalRange.size());
61+
for (size_t LID = 0; LID < LocalRange.size(); ++LID)
62+
LocalData[LID] = LID + WGID;
63+
64+
int Res = 0;
65+
for (size_t LID = 0; LID < LocalRange.size(); ++LID)
66+
Res += LocalData[LID];
67+
68+
return Res;
69+
}
70+
};
71+
72+
class MultiplyOp : public BaseOp {
73+
public:
74+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
75+
int apply(int *LocalData, sycl::group<1> WG) override {
76+
// +1 to avoid multiplying by 0 below
77+
LocalData[WG.get_local_id()] = WG.get_local_id() + WG.get_group_id() + 1;
78+
sycl::group_barrier(WG);
79+
if (WG.leader()) {
80+
int Res = 1;
81+
for (size_t I = 0; I < WG.get_local_range().size(); ++I) {
82+
Res *= LocalData[I];
83+
}
84+
LocalData[0] = Res;
85+
}
86+
sycl::group_barrier(WG);
87+
88+
return LocalData[0];
89+
}
90+
91+
int computeReference(sycl::range<1> LocalRange, int WGID) override {
92+
std::vector<int> LocalData(LocalRange.size());
93+
for (size_t LID = 0; LID < LocalRange.size(); ++LID)
94+
LocalData[LID] = LID + WGID + 1;
95+
96+
int Res = 1;
97+
for (size_t LID = 0; LID < LocalRange.size(); ++LID)
98+
Res *= LocalData[LID];
99+
100+
return Res;
101+
}
102+
};
103+
104+
int main() try {
105+
using storage_t = obj_storage_t<SumOp, MultiplyOp>;
106+
107+
sycl::queue q;
108+
109+
storage_t HostStorage;
110+
auto *DeviceStorage = sycl::malloc_shared<storage_t>(1, q);
111+
// Let's keep ranges small, or otherwise we will encounter integer overflow
112+
// (which is a UB) in MultiplyOp::apply.
113+
sycl::range G{16};
114+
sycl::range L{4};
115+
116+
constexpr oneapi::properties props{oneapi::assume_indirect_calls};
117+
for (unsigned TestCase = 0; TestCase < 2; ++TestCase) {
118+
sycl::buffer<int> DataStorage(G);
119+
120+
q.submit([&](sycl::handler &CGH) {
121+
CGH.single_task([=]() {
122+
DeviceStorage->construct</* ret type = */ BaseOp>(TestCase);
123+
});
124+
}).wait_and_throw();
125+
126+
q.submit([&](sycl::handler &CGH) {
127+
sycl::accessor DataAcc(DataStorage, CGH, sycl::read_write);
128+
sycl::local_accessor<int> LocalAcc(L, CGH);
129+
CGH.parallel_for(sycl::nd_range{G, L}, props, [=](auto It) {
130+
auto *Ptr = DeviceStorage->getAs<BaseOp>();
131+
DataAcc[It.get_global_id()] = Ptr->apply(
132+
LocalAcc.get_multi_ptr<sycl::access::decorated::no>().get(),
133+
It.get_group());
134+
});
135+
}).wait_and_throw();
136+
137+
auto *Ptr = HostStorage.construct</* ret type = */ BaseOp>(TestCase);
138+
sycl::host_accessor HostAcc(DataStorage);
139+
140+
// All work-items in a group produce the same result, so we do verification
141+
// per work-group.
142+
for (size_t WorkGroupID = 0; WorkGroupID < G.size() / L.size();
143+
++WorkGroupID) {
144+
int Reference = Ptr->computeReference(L, WorkGroupID);
145+
for (size_t I = 0; I < L.size(); ++I) {
146+
size_t GID = WorkGroupID * L.size() + I;
147+
if (HostAcc[GID] != Reference) {
148+
std::cout << "Mismatch at index " << I << ": " << HostAcc[I]
149+
<< " != " << Reference << std::endl;
150+
assert(HostAcc[I] == Reference);
151+
}
152+
}
153+
}
154+
}
155+
156+
sycl::free(DeviceStorage, q);
157+
158+
return 0;
159+
} catch (sycl::exception &e) {
160+
std::cout << "Unexpected exception was thrown: " << e.what() << std::endl;
161+
return 1;
162+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// REQUIRES: aspect-usm_shared_allocations
2+
//
3+
// This test checks that SYCL math built-in functions work correctly
4+
// inside virtual functions.
5+
//
6+
// RUN: %{build} -o %t.out %helper-includes
7+
// RUN: %{run} %t.out
8+
9+
#include <sycl/builtins.hpp>
10+
#include <sycl/detail/core.hpp>
11+
#include <sycl/usm.hpp>
12+
13+
#include "helpers.hpp"
14+
15+
#include <iostream>
16+
17+
namespace oneapi = sycl::ext::oneapi::experimental;
18+
19+
class BaseOp {
20+
public:
21+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
22+
virtual float apply(float) = 0;
23+
};
24+
25+
class FloorOp : public BaseOp {
26+
public:
27+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
28+
virtual float apply(float V) { return sycl::floor(V); }
29+
};
30+
31+
class CeilOp : public BaseOp {
32+
public:
33+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
34+
virtual float apply(float V) { return sycl::ceil(V); }
35+
};
36+
37+
class RoundOp : public BaseOp {
38+
public:
39+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
40+
virtual float apply(float V) { return sycl::round(V); }
41+
};
42+
43+
int main() try {
44+
using storage_t = obj_storage_t<FloorOp, CeilOp, RoundOp>;
45+
46+
storage_t HostStorage;
47+
48+
sycl::queue q;
49+
50+
auto *DeviceStorage = sycl::malloc_shared<storage_t>(1, q);
51+
52+
constexpr oneapi::properties props{oneapi::assume_indirect_calls};
53+
for (unsigned TestCase = 0; TestCase < 3; ++TestCase) {
54+
float HostData = 3.56;
55+
float Data = HostData;
56+
sycl::buffer<float> DataStorage(&Data, sycl::range{1});
57+
58+
q.submit([&](sycl::handler &CGH) {
59+
CGH.single_task([=]() {
60+
DeviceStorage->construct</* ret type = */ BaseOp>(TestCase);
61+
});
62+
}).wait_and_throw();
63+
64+
q.submit([&](sycl::handler &CGH) {
65+
sycl::accessor DataAcc(DataStorage, CGH, sycl::read_write);
66+
CGH.single_task(props, [=]() {
67+
auto *Ptr = DeviceStorage->getAs<BaseOp>();
68+
DataAcc[0] = Ptr->apply(DataAcc[0]);
69+
});
70+
});
71+
72+
auto *Ptr = HostStorage.construct</* ret type = */ BaseOp>(TestCase);
73+
HostData = Ptr->apply(HostData);
74+
75+
sycl::host_accessor HostAcc(DataStorage);
76+
assert(HostAcc[0] == HostData);
77+
}
78+
79+
sycl::free(DeviceStorage, q);
80+
81+
return 0;
82+
} catch (sycl::exception &e) {
83+
std::cout << "Unexpected exception was thrown: " << e.what() << std::endl;
84+
return 1;
85+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// REQUIRES: aspect-usm_shared_allocations
2+
//
3+
// This test checks that virtual functions work correctly in simple range
4+
// kernels when different work-items perform calls to different virtual
5+
// functions using the same object.
6+
//
7+
// RUN: %{build} -o %t.out %helper-includes
8+
// RUN: %{run} %t.out
9+
10+
#include <sycl/detail/core.hpp>
11+
#include <sycl/usm.hpp>
12+
13+
#include "helpers.hpp"
14+
15+
#include <iostream>
16+
#include <numeric>
17+
18+
namespace oneapi = sycl::ext::oneapi::experimental;
19+
20+
class BaseOp {
21+
public:
22+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
23+
virtual int foo(int) = 0;
24+
25+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
26+
virtual int bar(int) = 0;
27+
};
28+
29+
class OpA : public BaseOp {
30+
public:
31+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
32+
virtual int foo(int V) { return V + 2; }
33+
34+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
35+
virtual int bar(int V) { return V - 2; }
36+
};
37+
38+
class OpB : public BaseOp {
39+
public:
40+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
41+
virtual int foo(int V) { return V * 2; }
42+
43+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY(oneapi::indirectly_callable)
44+
virtual int bar(int V) { return V / 2; }
45+
};
46+
47+
int main() try {
48+
using storage_t = obj_storage_t<OpA, OpB>;
49+
50+
storage_t HostStorage;
51+
52+
sycl::queue q;
53+
54+
auto *DeviceStorage = sycl::malloc_shared<storage_t>(1, q);
55+
sycl::range R{1024};
56+
57+
constexpr oneapi::properties props{oneapi::assume_indirect_calls};
58+
for (size_t TestCase = 0; TestCase < 2; ++TestCase) {
59+
std::vector<int> HostData(R.size());
60+
std::iota(HostData.begin(), HostData.end(), 0);
61+
std::vector<int> DeviceData = HostData;
62+
sycl::buffer<int> DataStorage(DeviceData.data(), R);
63+
64+
q.submit([&](sycl::handler &CGH) {
65+
CGH.single_task([=]() {
66+
DeviceStorage->construct</* ret type = */ BaseOp>(TestCase);
67+
});
68+
}).wait_and_throw();
69+
70+
q.submit([&](sycl::handler &CGH) {
71+
sycl::accessor DataAcc(DataStorage, CGH, sycl::read_write);
72+
CGH.parallel_for(R, props, [=](auto It) {
73+
// Select method that corresponds to this work-item
74+
auto *Ptr = DeviceStorage->template getAs<BaseOp>();
75+
if (It % 2)
76+
DataAcc[It] = Ptr->foo(DataAcc[It]);
77+
else
78+
DataAcc[It] = Ptr->bar(DataAcc[It]);
79+
});
80+
});
81+
82+
BaseOp *Ptr = HostStorage.construct</* ret type = */ BaseOp>(TestCase);
83+
84+
for (size_t I = 0; I < HostData.size(); ++I) {
85+
if (I % 2)
86+
HostData[I] = Ptr->foo(HostData[I]);
87+
else
88+
HostData[I] = Ptr->bar(HostData[I]);
89+
}
90+
91+
sycl::host_accessor HostAcc(DataStorage);
92+
for (size_t I = 0; I < HostData.size(); ++I)
93+
assert(HostAcc[I] == HostData[I]);
94+
}
95+
96+
sycl::free(DeviceStorage, q);
97+
98+
return 0;
99+
} catch (sycl::exception &e) {
100+
std::cout << "Unexpected exception was thrown: " << e.what() << std::endl;
101+
return 1;
102+
}

0 commit comments

Comments
 (0)