Skip to content

Commit be7b44c

Browse files
[SYCL] Move kernel id into device kernel info struct
1 parent 29dfd03 commit be7b44c

File tree

6 files changed

+48
-59
lines changed

6 files changed

+48
-59
lines changed

sycl/source/detail/device_kernel_info.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ namespace sycl {
1212
inline namespace _V1 {
1313
namespace detail {
1414

15-
DeviceKernelInfo::DeviceKernelInfo(const CompileTimeKernelInfoTy &Info)
16-
: CompileTimeKernelInfoTy(Info) {}
15+
DeviceKernelInfo::DeviceKernelInfo(const CompileTimeKernelInfoTy &Info,
16+
std::optional<sycl::kernel_id> KernelID)
17+
: CompileTimeKernelInfoTy{Info}, MKernelID{std::move(KernelID)} {}
1718

1819
template <typename OtherTy>
1920
inline constexpr bool operator==(const CompileTimeKernelInfoTy &LHS,

sycl/source/detail/device_kernel_info.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sycl/detail/compile_time_kernel_info.hpp>
1414
#include <sycl/detail/spinlock.hpp>
1515
#include <sycl/detail/ur.hpp>
16+
#include <sycl/kernel_bundle.hpp>
1617

1718
#include <mutex>
1819
#include <optional>
@@ -84,12 +85,10 @@ struct FastKernelSubcacheT {
8485
// information that is uniform between different submissions of the same
8586
// kernel). Pointers to instances of this class are stored in header function
8687
// templates as a static variable to avoid repeated runtime lookup overhead.
87-
// TODO Currently this class duplicates information fetched from the program
88-
// manager. Instead, we should merge all of this information
89-
// into this structure and get rid of the other KernelName -> * maps.
9088
class DeviceKernelInfo : public CompileTimeKernelInfoTy {
9189
public:
92-
DeviceKernelInfo(const CompileTimeKernelInfoTy &Info);
90+
DeviceKernelInfo(const CompileTimeKernelInfoTy &Info,
91+
std::optional<sycl::kernel_id> KernelID = std::nullopt);
9392

9493
void init(std::string_view KernelName);
9594
void setCompileTimeInfoIfNeeded(const CompileTimeKernelInfoTy &Info);
@@ -100,6 +99,11 @@ class DeviceKernelInfo : public CompileTimeKernelInfoTy {
10099
return MImplicitLocalArgPos;
101100
}
102101

102+
const sycl::kernel_id &getKernelID() const {
103+
assert(MKernelID);
104+
return *MKernelID;
105+
}
106+
103107
// Implicit local argument position is used only for some backends, so this
104108
// function allows setting it as more images are added.
105109
void setImplicitLocalArgPos(int Pos);
@@ -109,6 +113,7 @@ class DeviceKernelInfo : public CompileTimeKernelInfoTy {
109113

110114
FastKernelSubcacheT MFastKernelSubcache;
111115
std::optional<int> MImplicitLocalArgPos;
116+
const std::optional<sycl::kernel_id> MKernelID;
112117
};
113118

114119
} // namespace detail

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,11 +1333,12 @@ ProgramManager::getDeviceImage(std::string_view KernelName,
13331333

13341334
const RTDeviceBinaryImage *Img = nullptr;
13351335
{
1336-
std::lock_guard<std::mutex> KernelIDsGuard(m_KernelIDsMutex);
1337-
if (auto KernelId = m_KernelName2KernelIDs.find(KernelName);
1338-
KernelId != m_KernelName2KernelIDs.end()) {
1339-
Img = getBinImageFromMultiMap(m_KernelIDs2BinImage, KernelId->second,
1340-
ContextImpl, DeviceImpl);
1336+
std::lock_guard<std::mutex> Guard(m_DeviceKernelInfoMapMutex);
1337+
if (auto It = m_DeviceKernelInfoMap.find(KernelName);
1338+
It != m_DeviceKernelInfoMap.end()) {
1339+
Img = getBinImageFromMultiMap(m_KernelIDs2BinImage,
1340+
It->second.getKernelID(), ContextImpl,
1341+
DeviceImpl);
13411342
}
13421343
}
13431344

@@ -1721,26 +1722,24 @@ void ProgramManager::addImage(sycl_device_binary RawImg,
17211722

17221723
auto name = EntriesIt->GetName();
17231724

1724-
// Skip creating unique kernel ID if it is an exported device
1725+
// Skip creating device kernel information if it is an exported device
17251726
// function. Exported device functions appear in the offload entries
17261727
// among kernels, but are identifiable by being listed in properties.
17271728
if (m_ExportedSymbolImages.find(name) != m_ExportedSymbolImages.end())
17281729
continue;
17291730

1730-
// ... and create a unique kernel ID for the entry
1731-
auto It = m_KernelName2KernelIDs.find(name);
1732-
if (It == m_KernelName2KernelIDs.end()) {
1731+
auto It = m_DeviceKernelInfoMap.find(std::string_view(name));
1732+
if (It == m_DeviceKernelInfoMap.end()) {
17331733
sycl::kernel_id KernelID = detail::createSyclObjFromImpl<sycl::kernel_id>(
17341734
std::make_shared<detail::kernel_id_impl>(name));
1735-
1736-
It = m_KernelName2KernelIDs.emplace_hint(It, name, KernelID);
1735+
CompileTimeKernelInfoTy DefaultCompileTimeInfo{std::string_view(name)};
1736+
It = m_DeviceKernelInfoMap.emplace_hint(
1737+
It, std::piecewise_construct, std::forward_as_tuple(name),
1738+
std::forward_as_tuple(DefaultCompileTimeInfo, KernelID));
17371739
}
1738-
m_KernelIDs2BinImage.insert(std::make_pair(It->second, Img.get()));
1739-
KernelIDs->push_back(It->second);
1740-
1741-
CompileTimeKernelInfoTy DefaultCompileTimeInfo{std::string_view(name)};
1742-
m_DeviceKernelInfoMap.try_emplace(std::string_view(name),
1743-
DefaultCompileTimeInfo);
1740+
m_KernelIDs2BinImage.insert(
1741+
std::make_pair(It->second.getKernelID(), Img.get()));
1742+
KernelIDs->push_back(It->second.getKernelID());
17441743

17451744
// Keep track of image to kernel name reference count for cleanup.
17461745
m_KernelNameRefCount[name]++;
@@ -1919,9 +1918,10 @@ void ProgramManager::removeImages(sycl_device_binaries DeviceBinary) {
19191918
continue;
19201919
}
19211920

1922-
auto Name2IDIt = m_KernelName2KernelIDs.find(Name);
1923-
if (Name2IDIt != m_KernelName2KernelIDs.end())
1924-
removeFromMultimapByVal(m_KernelIDs2BinImage, Name2IDIt->second, Img);
1921+
auto DKIIt = m_DeviceKernelInfoMap.find(Name);
1922+
assert(DKIIt != m_DeviceKernelInfoMap.end());
1923+
removeFromMultimapByVal(m_KernelIDs2BinImage, DKIIt->second.getKernelID(),
1924+
Img);
19251925

19261926
auto RefCountIt = m_KernelNameRefCount.find(Name);
19271927
assert(RefCountIt != m_KernelNameRefCount.end());
@@ -1933,10 +1933,8 @@ void ProgramManager::removeImages(sycl_device_binaries DeviceBinary) {
19331933
if (--RefCount == 0) {
19341934
// TODO aggregate all these maps into a single one since their entries
19351935
// share lifetime.
1936-
m_DeviceKernelInfoMap.erase(Name);
1936+
m_DeviceKernelInfoMap.erase(DKIIt);
19371937
m_KernelNameRefCount.erase(RefCountIt);
1938-
if (Name2IDIt != m_KernelName2KernelIDs.end())
1939-
m_KernelName2KernelIDs.erase(Name2IDIt);
19401938
}
19411939
}
19421940

@@ -2058,10 +2056,10 @@ std::vector<kernel_id> ProgramManager::getAllSYCLKernelIDs() {
20582056
std::lock_guard<std::mutex> KernelIDsGuard(m_KernelIDsMutex);
20592057

20602058
std::vector<sycl::kernel_id> AllKernelIDs;
2061-
AllKernelIDs.reserve(m_KernelName2KernelIDs.size());
2062-
for (std::pair<std::string_view, kernel_id> KernelID :
2063-
m_KernelName2KernelIDs) {
2064-
AllKernelIDs.push_back(KernelID.second);
2059+
AllKernelIDs.reserve(m_DeviceKernelInfoMap.size());
2060+
for (const std::pair<const std::string_view, DeviceKernelInfo> &Pair :
2061+
m_DeviceKernelInfoMap) {
2062+
AllKernelIDs.push_back(Pair.second.getKernelID());
20652063
}
20662064
return AllKernelIDs;
20672065
}

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,20 @@ class ProgramManager {
221221

222222
// The function returns the unique SYCL kernel identifier associated with a
223223
// kernel name or nullopt if there is no such ID.
224-
std::optional<kernel_id> tryGetSYCLKernelID(std::string_view KernelName) {
225-
std::lock_guard<std::mutex> KernelIDsGuard(m_KernelIDsMutex);
224+
std::optional<kernel_id>
225+
tryGetSYCLKernelID(std::string_view KernelName) const {
226+
std::lock_guard<std::mutex> Guard(m_DeviceKernelInfoMapMutex);
226227

227-
auto KernelID = m_KernelName2KernelIDs.find(KernelName);
228-
if (KernelID == m_KernelName2KernelIDs.end())
228+
auto It = m_DeviceKernelInfoMap.find(KernelName);
229+
if (It == m_DeviceKernelInfoMap.end())
229230
return std::nullopt;
230231

231-
return KernelID->second;
232+
return It->second.getKernelID();
232233
}
233234

234235
// The function returns the unique SYCL kernel identifier associated with a
235236
// kernel name or throws a sycl exception if there is no such ID.
236-
kernel_id getSYCLKernelID(std::string_view KernelName) {
237+
kernel_id getSYCLKernelID(std::string_view KernelName) const {
237238
if (std::optional<kernel_id> MaybeKernelID = tryGetSYCLKernelID(KernelName))
238239
return *MaybeKernelID;
239240
throw exception(make_error_code(errc::runtime),
@@ -423,17 +424,10 @@ class ProgramManager {
423424
const device_impl &DeviceImpl);
424425

425426
protected:
426-
/// The three maps below are used during kernel resolution. Any kernel is
427-
/// identified by its name.
428427
using RTDeviceBinaryImageUPtr = std::unique_ptr<RTDeviceBinaryImage>;
429428
using DynRTDeviceBinaryImageUPtr = std::unique_ptr<DynRTDeviceBinaryImage>;
430-
/// Maps names of kernels to their unique kernel IDs.
431-
/// TODO: Use std::unordered_set with transparent hash and equality functions
432-
/// when C++20 is enabled for the runtime library.
433-
/// Access must be guarded by the m_KernelIDsMutex mutex.
434-
//
435-
std::unordered_map<std::string_view, kernel_id> m_KernelName2KernelIDs;
436-
429+
/// The two maps below are used during kernel resolution. Any kernel is
430+
/// identified by its name, its kernel id is stored in m_DeviceKernelInfoMap.
437431
// Maps KernelIDs to device binary images. There can be more than one image
438432
// in case of SPIRV + AOT.
439433
/// Access must be guarded by the m_KernelIDsMutex mutex.
@@ -525,7 +519,7 @@ class ProgramManager {
525519
std::unordered_map<std::string_view, DeviceKernelInfo> m_DeviceKernelInfoMap;
526520

527521
// Protects m_DeviceKernelInfoMap.
528-
std::mutex m_DeviceKernelInfoMapMutex;
522+
mutable std::mutex m_DeviceKernelInfoMapMutex;
529523

530524
// Sanitizer type used in device image
531525
SanitizerType m_SanitizerFoundInImage;

sycl/source/handler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,7 @@ detail::EventImplPtr handler::finalize() {
505505
!KernelBundleImpPtr->tryGetKernel(impl->getKernelName())) {
506506
detail::device_impl &Dev = impl->get_device();
507507
kernel_id KernelID =
508-
detail::ProgramManager::getInstance().getSYCLKernelID(
509-
impl->getKernelName());
508+
impl->MKernelData.getDeviceKernelInfoPtr()->getKernelID();
510509
bool KernelInserted = KernelBundleImpPtr->add_kernel(
511510
KernelID, detail::createSyclObjFromImpl<device>(Dev));
512511
// If kernel was not inserted and the bundle is in input mode we try

sycl/unittests/program_manager/Cleanup.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ class ProgramManagerExposed : public sycl::detail::ProgramManager {
1818
return m_KernelIDs2BinImage;
1919
}
2020

21-
std::unordered_map<std::string_view, sycl::kernel_id> &
22-
getKernelName2KernelID() {
23-
return m_KernelName2KernelIDs;
24-
}
25-
2621
std::unordered_map<const sycl::detail::RTDeviceBinaryImage *,
2722
std::shared_ptr<std::vector<sycl::kernel_id>>> &
2823
getBinImage2KernelId() {
@@ -278,9 +273,6 @@ void checkAllInvolvedContainers(ProgramManagerExposed &PM,
278273
bool MultipleImgsPerEntryTestCase = false) {
279274
EXPECT_EQ(PM.getKernelID2BinImage().size(), ExpectedImgCount)
280275
<< "KernelID2BinImg " + CommentPostfix;
281-
checkContainer(PM.getKernelName2KernelID(), ExpectedEntryCount,
282-
generateRefNames(ImgIds, "Kernel"),
283-
"KernelName2KernelID " + CommentPostfix);
284276
EXPECT_EQ(PM.getBinImage2KernelId().size(), ExpectedImgCount)
285277
<< CommentPostfix;
286278
checkContainer(PM.getExportedSymbolImages(), ExpectedImgCount,

0 commit comments

Comments
 (0)