Skip to content

[SYCL] Delete symbol based info with the last image referencing it #19659

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

Open
wants to merge 13 commits into
base: sycl
Choose a base branch
from

Conversation

sergey-semenov
Copy link
Contributor

@sergey-semenov sergey-semenov commented Jul 31, 2025

Prior to this patch, symbol based info (e.g. kernel id, kernel assert usage or images containing an exported symbol) was deleted whenever an image referencing it was removed. This is incorrect since multiple images can contain the same symbol.

While unlikely to cause any problems now (since those images usually all get removed with one call to removeImages after another), this will cause issues once kernel name based kernel caches start getting cleaned up in the same manner.

Prior to this patch, kernel name based info (e.g. kernel id or assert usage)
was deleted whenever an image referencing it was removed. This is technically
incorrect since multiple images can contain the same kernel name.
@sergey-semenov sergey-semenov changed the title [SYCL] Delete kernel name based info with the last image referencing it [SYCL] Delete entry name based info with the last image referencing it Aug 4, 2025
@sergey-semenov sergey-semenov changed the title [SYCL] Delete entry name based info with the last image referencing it [SYCL] Delete symbol based info with the last image referencing it Aug 8, 2025
@sergey-semenov sergey-semenov marked this pull request as ready for review August 8, 2025 16:25
@sergey-semenov sergey-semenov requested a review from a team as a code owner August 8, 2025 16:25
Copy link
Contributor

@KseniyaTikhomirova KseniyaTikhomirova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -2115,6 +2118,18 @@ void ProgramManager::addImages(sycl_device_binaries DeviceBinary) {
addImage(&(DeviceBinary->DeviceBinaries[I]));
}

template <typename MultimapT, typename KeyT, typename ValT>
void removeFromMultimap(MultimapT &Map, const KeyT &Key, const ValT &Val,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would change the name to removeFromMultimapByVal.

m_VFSet2BinImage.erase(SetName);
for (const auto &SetName : detail::split_string(StrValue, ',')) {
auto It = m_VFSet2BinImage.find(SetName);
assert(It != m_VFSet2BinImage.end());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to create a tiny separate method for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about outlining, but some inline comment would help...

@@ -2140,44 +2155,67 @@ void ProgramManager::removeImages(sycl_device_binaries DeviceBinary) {
// Unmap the unique kernel IDs for the offload entries
for (sycl_offload_entry EntriesIt = EntriesB; EntriesIt != EntriesE;
EntriesIt = EntriesIt->Increment()) {

const char *Name = EntriesIt->GetName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be std::string_view instead?

Comment on lines +2170 to +2191
// Remove everything associated with this KernelName if this is the last
// image referencing it, otherwise remove just the ID -> Img mapping.
auto RefCountIt = m_KernelNameRefCount.find(Name);
assert(RefCountIt != m_KernelNameRefCount.end());
int &RefCount = RefCountIt->second;
assert(RefCount > 0);
--RefCount;

if (auto It = m_KernelName2KernelIDs.find(EntriesIt->GetName());
if (auto It = m_KernelName2KernelIDs.find(Name);
It != m_KernelName2KernelIDs.end()) {
m_KernelIDs2BinImage.erase(It->second);
m_KernelName2KernelIDs.erase(It);
if (RefCount == 0) {
m_KernelIDs2BinImage.erase(It->second);
m_KernelName2KernelIDs.erase(It);
} else {
removeFromMultimap(m_KernelIDs2BinImage, It->second, Img);
}
}

if (RefCount == 0) {
m_KernelUsesAssert.erase(Name);
m_KernelImplicitLocalArgPos.erase(Name);
m_KernelNameRefCount.erase(RefCountIt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ugly enough to justify an extended comment. I'm not familiar with the code but it's very unexpected to have several branches for if (RefCount == 0), it looks like some ownership is spread between several places which is never good. Can you explain what is going on here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants