Skip to content

[SYCL] Fix compare_exchange_strong to properly update Expected inout param #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sycl/include/CL/sycl/atomic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ class atomic {
T Value = __spirv_AtomicCompareExchange(
Ptr, SpirvScope, detail::getSPIRVMemorySemanticsMask(SuccessOrder),
detail::getSPIRVMemorySemanticsMask(FailOrder), Desired, Expected);
return (Value == Expected);

if (Value == Expected)
return true;

Expected = Value;
return false;
#else
return Ptr->compare_exchange_strong(Expected, Desired,
detail::getStdMemoryOrder(SuccessOrder),
Expand Down
40 changes: 40 additions & 0 deletions sycl/test/basic_tests/compare_exchange_strong.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out

#include <CL/sycl.hpp>
using namespace cl::sycl;

int main() {
queue testQueue;

char testResult;
{
buffer<char, 1> resultBuf(&testResult, range<1>(1));

int32_t data = 42;
buffer<int32_t, 1> buf(&data, range<1>(1));

testQueue.submit([&](handler &cgh) {
auto globAcc = buf.template get_access<access::mode::atomic>(cgh);
auto resultAcc = resultBuf.template get_access<access::mode::write>(cgh);
cgh.single_task<class foo>([=]() {
auto a = globAcc[0];
char result = 0;
int32_t expected = 0; // Set to wrong value.
char updated = a.compare_exchange_strong(expected, 1);
if (updated)
// Update shouldn't happen, value in memory is different from
// expected!
result = 1;
if (expected != 42)
// "Expected" inout parameter wasn't updated to the value read!
result = 1;
resultAcc[0] = result;
});
});
}
assert(testResult == 0 && "Test failed!");
return testResult;
}