diff --git a/sycl/unittests/context_device/DeviceRefCounter.cpp b/sycl/unittests/context_device/DeviceRefCounter.cpp index 93b565cc73198..ea8c38b6fdbb7 100644 --- a/sycl/unittests/context_device/DeviceRefCounter.cpp +++ b/sycl/unittests/context_device/DeviceRefCounter.cpp @@ -5,6 +5,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// +#include #include #include #include @@ -31,6 +32,7 @@ static ur_result_t redefinedDeviceReleaseAfter(void *) { TEST(DevRefCounter, DevRefCounter) { { sycl::unittest::UrMock<> Mock; + EXPECT_EQ(DevRefCounter, 0); mock::getCallbacks().set_after_callback("urDeviceGet", &redefinedDevicesGetAfter); @@ -41,6 +43,12 @@ TEST(DevRefCounter, DevRefCounter) { sycl::platform Plt = sycl::platform(); Plt.get_devices(); + EXPECT_NE(DevRefCounter, 0); + // This is the behavior that SYCL performs at shutdown, but there + // are timing differences Lin/Win and shared/static that make + // it not map correctly into our mock. + // So for this test, we just do it. + sycl::detail::GlobalHandler::instance().getPlatformCache().clear(); } EXPECT_EQ(DevRefCounter, 0); } diff --git a/sycl/unittests/helpers/UrMock.hpp b/sycl/unittests/helpers/UrMock.hpp index 697df02bceb53..642ed8b401782 100644 --- a/sycl/unittests/helpers/UrMock.hpp +++ b/sycl/unittests/helpers/UrMock.hpp @@ -596,6 +596,16 @@ template class UrMock { sycl::detail::ur::initializeUr(UrLoaderConfig); urLoaderConfigRelease(UrLoaderConfig); + + // We clear platform cache for each test run, so that tests can have a + // different backend. This forces platforms to be reconstructed (and thus + // queries about UR backend info to be called again) This also erases each + // platform's devices (normally done in the library shutdown) so that + // platforms/devices' lifetimes could work in unittests scenario. In SYCL, + // this is normally done at shutdown, but between Win/Lin and static/shared + // differences, there is no correct parallel in the ~UrMock destructor. + // Instead we do it here. Simple and clean. + detail::GlobalHandler::instance().getPlatformCache().clear(); } UrMock(UrMock &&Other) = delete; @@ -606,14 +616,8 @@ template class UrMock { // these between tests detail::GlobalHandler::instance().prepareSchedulerToRelease(true); detail::GlobalHandler::instance().releaseDefaultContexts(); - // clear platform cache in case subsequent tests want a different backend, - // this forces platforms to be reconstructed (and thus queries about UR - // backend info to be called again) - // - // This also erases each platform's devices (normally done in the library - // shutdown) so that platforms/devices' lifetimes could work in unittests - // scenario. - detail::GlobalHandler::instance().getPlatformCache().clear(); + // the platform cache is cleared at the BEGINING of the mock. + mock::getCallbacks().resetCallbacks(); }