Skip to content

Commit d058186

Browse files
authored
[SYCL] Disable HostPtr reuse when the pointer is read-only (#10334)
Mutable SYCL buffers can be initialized using a `const T* hostData`. This change ensures that these buffers allocate new memory so that their contents can be modified without changing the original host data. Fixes #10091. Signed-off-by: Michael Aziz <[email protected]>
1 parent 2f2560f commit d058186

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

sycl/source/detail/sycl_mem_obj_t.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class __SYCL_EXPORT SYCLMemObjT : public SYCLMemObjI {
175175
bool canReuseHostPtr(void *HostPtr, const size_t RequiredAlign) {
176176
bool Aligned =
177177
(reinterpret_cast<std::uintptr_t>(HostPtr) % RequiredAlign) == 0;
178-
return Aligned || useHostPtr();
178+
return !MHostPtrReadOnly && (Aligned || useHostPtr());
179179
}
180180

181181
void handleHostData(void *HostPtr, const size_t RequiredAlign) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
#include <iostream>
5+
#include <sycl/sycl.hpp>
6+
7+
using namespace sycl;
8+
9+
static constexpr int N = 32;
10+
11+
void testAccessor() {
12+
std::vector<int> vec(N, 1);
13+
{
14+
buffer<int, 1> buf(static_cast<const int *>(vec.data()), range<1>{N});
15+
queue q;
16+
q.submit([&](handler &cgh) {
17+
auto acc = buf.get_access<access_mode::read_write>(cgh);
18+
cgh.parallel_for<class Kernel>({N}, [=](id<1> i) { acc[i] += 5; });
19+
});
20+
}
21+
assert(vec[0] == 1);
22+
}
23+
24+
void testHostAcessor() {
25+
std::vector<int> vec(N, 1);
26+
{
27+
buffer<int, 1> buf(static_cast<const int *>(vec.data()), range<1>{N});
28+
auto acc = buf.get_host_access();
29+
acc[0] += 5;
30+
}
31+
assert(vec[0] == 1);
32+
}
33+
34+
int main() {
35+
testAccessor();
36+
testHostAcessor();
37+
}

0 commit comments

Comments
 (0)