Skip to content

Commit c4f1cee

Browse files
authored
[SYCL] Fix issues caught by static analyzer (#12253)
* Don't throw exceptions in destructors (they are explicitely marked as noexcept) * Make sure that MPlatform data member of context_impl is always initialized. * Assign default value to MCacheMutex* in kernel_impl.
1 parent c1ce159 commit c4f1cee

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

sycl/source/detail/buffer_impl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ buffer_impl::getNativeVector(backend BackendName) const {
7474
auto Platform = Ctx->getPlatformImpl();
7575
// If Host Shared Memory is not supported then there is alloca for host that
7676
// doesn't have platform
77-
if (!Platform)
77+
if (!Platform || (Platform->getBackend() != BackendName))
7878
continue;
79+
7980
auto Plugin = Platform->getPlugin();
8081

81-
if (Platform->getBackend() != BackendName)
82-
continue;
8382
if (Platform->getBackend() == backend::opencl) {
8483
Plugin->call<PiApiKind::piMemRetain>(NativeMem);
8584
}

sycl/source/detail/context_impl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ namespace detail {
3232
context_impl::context_impl(const device &Device, async_handler AsyncHandler,
3333
const property_list &PropList)
3434
: MOwnedByRuntime(true), MAsyncHandler(AsyncHandler), MDevices(1, Device),
35-
MContext(nullptr), MPlatform(), MPropList(PropList),
35+
MContext(nullptr),
36+
MPlatform(detail::getSyclObjImpl(Device.get_platform())),
37+
MPropList(PropList),
3638
MHostContext(detail::getSyclObjImpl(Device)->is_host()),
3739
MSupportBufferLocationByDevices(NotChecked) {
3840
MKernelProgramCache.setContextPtr(this);
@@ -100,6 +102,10 @@ context_impl::context_impl(sycl::detail::pi::PiContext PiContext,
100102
Platform->getOrMakeDeviceImpl(Dev, Platform)));
101103
}
102104
MPlatform = Platform;
105+
} else {
106+
throw invalid_parameter_error(
107+
"No devices in the provided device list and native context.",
108+
PI_ERROR_INVALID_VALUE);
103109
}
104110
}
105111
// TODO catch an exception and put it to list of asynchronous exceptions

sycl/source/detail/context_impl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ class context_impl {
191191
}
192192

193193
// Returns the backend of this context
194-
backend getBackend() const { return MPlatform->getBackend(); }
194+
backend getBackend() const {
195+
assert(MPlatform && "MPlatorm must be not null");
196+
return MPlatform->getBackend();
197+
}
195198

196199
/// Given a PiDevice, returns the matching shared_ptr<device_impl>
197200
/// within this context. May return nullptr if no match discovered.

sycl/source/detail/kernel_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class kernel_impl {
195195
bool MIsInterop = false;
196196
std::mutex MNoncacheableEnqueueMutex;
197197
const KernelArgMask *MKernelArgMaskPtr;
198-
std::mutex *MCacheMutex;
198+
std::mutex *MCacheMutex = nullptr;
199199

200200
bool isBuiltInKernel(const device &Device) const;
201201
void checkIfValidForNumArgsInfoQuery() const;

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ class KernelProgramCache {
9494
Val = nullptr;
9595
}
9696
~ProgramBuildResult() {
97-
if (Val)
98-
Plugin->call<PiApiKind::piProgramRelease>(Val);
97+
if (Val) {
98+
sycl::detail::pi::PiResult Err =
99+
Plugin->call_nocheck<PiApiKind::piProgramRelease>(Val);
100+
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
101+
}
99102
}
100103
};
101104
using ProgramBuildResultPtr = std::shared_ptr<ProgramBuildResult>;
@@ -126,8 +129,11 @@ class KernelProgramCache {
126129
Val.first = nullptr;
127130
}
128131
~KernelBuildResult() {
129-
if (Val.first)
130-
Plugin->call<PiApiKind::piKernelRelease>(Val.first);
132+
if (Val.first) {
133+
sycl::detail::pi::PiResult Err =
134+
Plugin->call_nocheck<PiApiKind::piKernelRelease>(Val.first);
135+
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
136+
}
131137
}
132138
};
133139
using KernelBuildResultPtr = std::shared_ptr<KernelBuildResult>;

0 commit comments

Comments
 (0)