Skip to content

Commit 04928f9

Browse files
[SYCL] Implement accessor default constructor (#6940)
This patch implements SYCL 2020 accessor default constructor
1 parent f3d245d commit 04928f9

File tree

7 files changed

+111
-39
lines changed

7 files changed

+111
-39
lines changed

sycl/include/sycl/accessor.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,15 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
11821182
return reinterpret_cast<PtrType>(AccessorBaseHost::getPtr());
11831183
}
11841184

1185+
public:
1186+
accessor()
1187+
: AccessorBaseHost(
1188+
/*Offset=*/{0, 0, 0}, /*AccessRange=*/{0, 0, 0},
1189+
/*MemoryRange=*/{0, 0, 0},
1190+
/*AccessMode=*/getAdjustedMode({}),
1191+
/*SYCLMemObject=*/nullptr, /*Dims=*/0, /*ElemSize=*/0,
1192+
/*OffsetInBytes=*/0, /*IsSubBuffer=*/false, /*PropertyList=*/{}){};
1193+
11851194
#endif // __SYCL_DEVICE_ONLY__
11861195

11871196
private:
@@ -1937,7 +1946,7 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
19371946
size_t byte_size() const noexcept { return size() * sizeof(DataT); }
19381947

19391948
size_t max_size() const noexcept {
1940-
return (std::numeric_limits<difference_type>::max)();
1949+
return empty() ? 0 : (std::numeric_limits<difference_type>::max)();
19411950
}
19421951

19431952
bool empty() const noexcept { return size() == 0; }

sycl/source/detail/scheduler/commands.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,8 @@ static pi_result SetKernelParamsAndLaunch(
20012001
break;
20022002
case kernel_param_kind_t::kind_accessor: {
20032003
Requirement *Req = (Requirement *)(Arg.MPtr);
2004+
if (Req->MAccessRange == range<3>({0, 0, 0}))
2005+
break;
20042006
if (getMemAllocationFunc == nullptr)
20052007
throw sycl::exception(make_error_code(errc::kernel_argument),
20062008
"placeholder accessor must be bound by calling "
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
3+
#include <sycl/sycl.hpp>
4+
5+
int main() {
6+
sycl::accessor<int, 0, sycl::access::mode::read_write, sycl::target::device>
7+
B;
8+
assert(B.empty());
9+
assert(B.size() == 0);
10+
assert(B.max_size() == 0);
11+
assert(B.byte_size() == 0);
12+
// The return values of get_pointer() and get_multi_ptr() are unspecified.
13+
assert(B.get_pointer() == nullptr);
14+
// TODO: uncomment check with get_multi_ptr() when SYCL 2020 mupti_ptr feature
15+
// will be merged
16+
// assert(B.get_multi_ptr() == nullptr);
17+
18+
return 0;
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "SchedulerTest.hpp"
2+
#include "SchedulerTestUtils.hpp"
3+
#include <detail/handler_impl.hpp>
4+
5+
#include <helpers/PiMock.hpp>
6+
#include <helpers/ScopedEnvVar.hpp>
7+
#include <helpers/TestKernel.hpp>
8+
9+
#include <vector>
10+
11+
using namespace sycl;
12+
13+
TEST_F(SchedulerTest, AccDefaultCtorDoesntAffectDepGraph) {
14+
unittest::PiMock Mock;
15+
platform Plt = Mock.getPlatform();
16+
17+
queue QueueDev(context(Plt), default_selector_v);
18+
MockScheduler MS;
19+
20+
detail::QueueImplPtr QueueDevImpl = detail::getSyclObjImpl(QueueDev);
21+
22+
std::vector<detail::Command *> ToEnqueue;
23+
24+
MockHandlerCustomFinalize MockCGH(QueueDevImpl, false);
25+
26+
sycl::accessor<int, 0, sycl::access::mode::read_write, sycl::target::device>
27+
B;
28+
29+
MockCGH.single_task<class acc_with_zero_dim>([=]() {
30+
int size = B.size();
31+
(void)size;
32+
});
33+
34+
std::unique_ptr<sycl::detail::CG> CmdGroup = MockCGH.finalize();
35+
36+
detail::Command *NewCmd =
37+
MS.addCG(std::move(CmdGroup), QueueDevImpl, ToEnqueue);
38+
39+
// if MDeps is empty, accessor built from default ctor does not affect
40+
// dependency graph in accordance with SYCL 2020
41+
EXPECT_TRUE(NewCmd->MDeps.empty());
42+
}

sycl/unittests/scheduler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ add_sycl_unittest(SchedulerTests OBJECT
2323
InOrderQueueSyncCheck.cpp
2424
RunOnHostIntelCG.cpp
2525
EnqueueWithDependsOnDeps.cpp
26+
AccessorDefaultCtor.cpp
2627
)

sycl/unittests/scheduler/EnqueueWithDependsOnDeps.cpp

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "SchedulerTest.hpp"
1010
#include "SchedulerTestUtils.hpp"
11-
#include <detail/handler_impl.hpp>
1211

1312
#include <helpers/PiMock.hpp>
1413
#include <helpers/ScopedEnvVar.hpp>
@@ -19,48 +18,13 @@
1918
using namespace sycl;
2019
using EventImplPtr = std::shared_ptr<detail::event_impl>;
2120

22-
namespace DependsOnTest {
23-
class MockHandlerCustom : public MockHandler {
24-
public:
25-
MockHandlerCustom(std::shared_ptr<sycl::detail::queue_impl> Queue,
26-
bool IsHost)
27-
: MockHandler(Queue, IsHost) {}
28-
29-
std::unique_ptr<sycl::detail::CG> finalize() {
30-
std::unique_ptr<sycl::detail::CG> CommandGroup;
31-
switch (getType()) {
32-
case sycl::detail::CG::Kernel: {
33-
CommandGroup.reset(new sycl::detail::CGExecKernel(
34-
getNDRDesc(), std::move(getHostKernel()), getKernel(),
35-
std::move(MImpl->MKernelBundle), getArgsStorage(), getAccStorage(),
36-
getSharedPtrStorage(), getRequirements(), getEvents(), getArgs(),
37-
getKernelName(), getOSModuleHandle(), getStreamStorage(),
38-
MImpl->MAuxiliaryResources, getCGType(), getCodeLoc()));
39-
break;
40-
}
41-
case sycl::detail::CG::CodeplayHostTask: {
42-
CommandGroup.reset(new detail::CGHostTask(
43-
std::move(getHostTask()), getQueue(), getQueue()->getContextImplPtr(),
44-
getArgs(), getArgsStorage(), getAccStorage(), getSharedPtrStorage(),
45-
getRequirements(), getEvents(), getCGType(), getCodeLoc()));
46-
break;
47-
}
48-
default:
49-
throw sycl::runtime_error("Unhandled type of command group",
50-
PI_ERROR_INVALID_OPERATION);
51-
}
52-
53-
return CommandGroup;
54-
}
55-
};
56-
} // namespace DependsOnTest
5721
detail::Command *AddTaskCG(bool IsHost, MockScheduler &MS,
5822
detail::QueueImplPtr DevQueue,
5923
const std::vector<EventImplPtr> &Events) {
6024
std::vector<detail::Command *> ToEnqueue;
6125

6226
// Emulating processing of command group function
63-
DependsOnTest::MockHandlerCustom MockCGH(DevQueue, false);
27+
MockHandlerCustomFinalize MockCGH(DevQueue, false);
6428

6529
for (auto EventImpl : Events)
6630
MockCGH.depends_on(detail::createSyclObjFromImpl<event>(EventImpl));

sycl/unittests/scheduler/SchedulerTestUtils.hpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <detail/queue_impl.hpp>
1111
#include <detail/scheduler/commands.hpp>
1212
#include <detail/scheduler/scheduler.hpp>
13+
#include <detail/handler_impl.hpp>
1314
#include <detail/stream_impl.hpp>
1415
#include <sycl/detail/cl.h>
1516

@@ -270,4 +271,38 @@ class MockHandler : public sycl::handler {
270271

271272
return nullptr;
272273
}
273-
};
274+
};
275+
276+
class MockHandlerCustomFinalize : public MockHandler {
277+
public:
278+
MockHandlerCustomFinalize(std::shared_ptr<sycl::detail::queue_impl> Queue,
279+
bool IsHost)
280+
: MockHandler(Queue, IsHost) {}
281+
282+
std::unique_ptr<sycl::detail::CG> finalize() {
283+
std::unique_ptr<sycl::detail::CG> CommandGroup;
284+
switch (getType()) {
285+
case sycl::detail::CG::Kernel: {
286+
CommandGroup.reset(new sycl::detail::CGExecKernel(
287+
getNDRDesc(), std::move(getHostKernel()), getKernel(),
288+
std::move(MImpl->MKernelBundle), getArgsStorage(), getAccStorage(),
289+
getSharedPtrStorage(), getRequirements(), getEvents(), getArgs(),
290+
getKernelName(), getOSModuleHandle(), getStreamStorage(),
291+
MImpl->MAuxiliaryResources, getCGType(), getCodeLoc()));
292+
break;
293+
}
294+
case sycl::detail::CG::CodeplayHostTask: {
295+
CommandGroup.reset(new sycl::detail::CGHostTask(
296+
std::move(getHostTask()), getQueue(), getQueue()->getContextImplPtr(),
297+
getArgs(), getArgsStorage(), getAccStorage(), getSharedPtrStorage(),
298+
getRequirements(), getEvents(), getCGType(), getCodeLoc()));
299+
break;
300+
}
301+
default:
302+
throw sycl::runtime_error("Unhandled type of command group",
303+
PI_ERROR_INVALID_OPERATION);
304+
}
305+
306+
return CommandGroup;
307+
}
308+
};

0 commit comments

Comments
 (0)