[llvm] [libsycl] Take MDataCollectionMutex in getDeviceKernelInfo() (PR #228099)
Kseniya Tikhomirova via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 1 07:55:48 PDT 2026
https://github.com/KseniyaTikhomirova created https://github.com/llvm/llvm-project/pull/228099
[libsycl] Take MDataCollectionMutex in getDeviceKernelInfo()
getDeviceKernelInfo() searched MDeviceKernelInfoMap without holding
MDataCollectionMutex, so a kernel launch could race with the
registerFatBin() or unregisterFatBin().
Report an unknown kernel name with sycl::errc::runtime instead of
asserting. Unlike registration, which runs from a module's static
initialization where nothing can catch, this runs from a user kernel
submission, so the exception reaches the user's handler.
Signed-off-by: Tikhomirova, Kseniya <kseniya.tikhomirova at intel.com>
>From 21c7d4f2cbcc7ba478d52788e37624c8430d3ece Mon Sep 17 00:00:00 2001
From: "Tikhomirova, Kseniya" <kseniya.tikhomirova at intel.com>
Date: Thu, 1 Oct 2026 06:50:44 -0700
Subject: [PATCH] [libsycl] Take MDataCollectionMutex in getDeviceKernelInfo()
Signed-off-by: Tikhomirova, Kseniya <kseniya.tikhomirova at intel.com>
---
libsycl/src/detail/program_manager.cpp | 8 +++++++-
libsycl/src/detail/program_manager.hpp | 3 +++
.../register_and_unregister.cpp | 20 +++++++++++++++++++
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/libsycl/src/detail/program_manager.cpp b/libsycl/src/detail/program_manager.cpp
index 12a38a8b75053..7d5d2fb9eecb7 100644
--- a/libsycl/src/detail/program_manager.cpp
+++ b/libsycl/src/detail/program_manager.cpp
@@ -19,6 +19,8 @@ _LIBSYCL_SUPPRESS_EXTRA_WARNINGS_BEGIN
#include <llvm/Frontend/Offloading/Utility.h>
_LIBSYCL_SUPPRESS_EXTRA_WARNINGS_END
+#include <string>
+
_LIBSYCL_BEGIN_NAMESPACE_SYCL
namespace detail {
@@ -29,8 +31,12 @@ getDeviceKernelInfo(std::string_view KernelName) {
DeviceKernelInfo &
ProgramAndKernelManager::getDeviceKernelInfo(std::string_view KernelName) {
+ std::lock_guard<std::mutex> Guard(MDataCollectionMutex);
auto It = MDeviceKernelInfoMap.find(KernelName);
- assert(It != MDeviceKernelInfoMap.end());
+ if (It == MDeviceKernelInfoMap.end())
+ throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
+ "No registered device image provides kernel " +
+ std::string(KernelName));
return It->second;
}
diff --git a/libsycl/src/detail/program_manager.hpp b/libsycl/src/detail/program_manager.hpp
index afe660add63f7..2b54ba5cfd46c 100644
--- a/libsycl/src/detail/program_manager.hpp
+++ b/libsycl/src/detail/program_manager.hpp
@@ -95,7 +95,10 @@ class ProgramAndKernelManager {
const std::shared_ptr<ContextImpl> &Context,
DeviceImpl &Device);
+ /// This method is thread-safe.
/// \return kernel info for the kernel with the specified name.
+ /// \throw sycl::exception with sycl::errc::runtime if no registered device
+ /// image provides a kernel with the specified name.
DeviceKernelInfo &getDeviceKernelInfo(std::string_view KernelName);
/// Release device image managers and corresponding resources.
diff --git a/libsycl/unittests/program_manager/register_and_unregister.cpp b/libsycl/unittests/program_manager/register_and_unregister.cpp
index 648e1ef6cfc7f..00601276e391a 100644
--- a/libsycl/unittests/program_manager/register_and_unregister.cpp
+++ b/libsycl/unittests/program_manager/register_and_unregister.cpp
@@ -127,3 +127,23 @@ TEST(ProgramAndKernelManager, CheckRegisterAndUnregister) {
EXPECT_THAT(Manager.MDeviceImageManagers, IsEmpty());
EXPECT_THAT(Manager.MDeviceKernelInfoMap, IsEmpty());
}
+
+TEST(ProgramAndKernelManager, CheckUnknownKernelName) {
+ std::array<llvm::StringRef, 1> KernelNames = {"kernel"};
+ llvm::SmallString<0> Binary =
+ sycl::unittests::createSYCLDeviceBinary(KernelNames);
+
+ MockProgramAndKernelManager Manager;
+ ASSERT_NO_THROW(Manager.registerFatBin(Binary.data(), Binary.size()));
+
+ EXPECT_NO_THROW(Manager.getDeviceKernelInfo("kernel"));
+ EXPECT_THAT(
+ [&]() { Manager.getDeviceKernelInfo("missing_kernel"); },
+ Throws<sycl::exception>(
+ AllOf(Property(&sycl::exception::what,
+ HasSubstr("No registered device image provides kernel "
+ "missing_kernel")),
+ Property(&sycl::exception::code, Eq(sycl::errc::runtime)))));
+
+ EXPECT_NO_THROW(Manager.unregisterFatBin(Binary.data(), Binary.size()));
+}
More information about the llvm-commits
mailing list