|
| 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 | +} |
0 commit comments