Skip to content

[SYCL][RTC] Define custom destructor for kernel_bundle_impl #16702

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

Merged
merged 12 commits into from
Feb 19, 2025

Conversation

jopperm
Copy link
Contributor

@jopperm jopperm commented Jan 21, 2025

Adds a custom destructor to kernel_bundle_impl to properly clean-up runtime information and device binaries for bundles that are runtime-compiled from source:

  • Removal of kernels from program manager
    The new ProgramManager::removeImages method takes addImages as a blueprint and reverses the effects of registering the given device binaries. Currently, only a subset of the data structures in the program manager is handled, and asserts are in place for the remaining members. Device globals will be addressed after [SYCL][RTC] Initial support for device globals #16565 lands. AFAICT I could clean-up most of the other members mechanically as well, but decided against that because I can't test these features (such as virtual functions) right now due to lack of SYCL-RTC support.

  • Free device binaries in JIT library
    Store the UR-specific data structure that backs a device binary in a map of unique ptrs instead of a vector to make it possible to free it again without invalidating other compilation results. Also introduce a mutex to protect concurrent access to this new map.

  • Free raw SPIR-V binaries in JIT context
    Again replace a vector with a map of unique ptr to make it possible to free KernelBinary objects in the JITContext. KernelBinary owns the actual SPIR-V binaries; all other data structures mentioned earlier only store the pointer return by KernelBinary::address(). A new function destroyBinary is added to the sycl-jit interface.

My understanding is that ~device_image_impl and ~kernel_impl already take care of releasing the underlying UR resources, hence no changes required there.

Signed-off-by: Julian Oppermann <[email protected]>
@jopperm jopperm marked this pull request as ready for review January 21, 2025 07:00
@jopperm jopperm requested review from a team as code owners January 21, 2025 07:00
Copy link
Contributor

@sommerlukas sommerlukas left a comment

Choose a reason for hiding this comment

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

A few small nits/questions.

RTDeviceBinaryImage *Img = *RawImages.begin();

// Drop the kernel argument mask map
// TODO: Why is this not protected by a mutex?
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need to address this TODO as part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What the TODO is asking is why m_EliminatedKernelArgMasks not protected by a mutex in the existing code, in particular in addImages which my method here tries to mirror. So this is potentially a separate issue, and I was hoping reviewers could explain the rationale here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, maybe @sergey-semenov can provide some insight here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why it shouldn't be. seems like a bug to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I filed #16836, and dropped the TODO from the code for now.

@@ -927,6 +930,13 @@ class kernel_bundle_impl {
return true;
}

~kernel_bundle_impl() {
if (DeviceBinaries) {
Copy link
Contributor

Choose a reason for hiding this comment

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

removeImages() might throw an exception (because getSYCLKernelID and several other sub-calls can)

But ~kernel_bundle_impl() itself is implictly noexcept. So you'll need a try/catch here. You can use that __SYCL_REPORT_EXCEPTION_TO_STREAM macro if appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks!

@jopperm
Copy link
Contributor Author

jopperm commented Jan 29, 2025

@sommerlukas could you re-review after 907f7f8, please?

Signed-off-by: Julian Oppermann <[email protected]>
Signed-off-by: Julian Oppermann <[email protected]>
Signed-off-by: Julian Oppermann <[email protected]>
@jopperm
Copy link
Contributor Author

jopperm commented Feb 19, 2025

@intel/llvm-gatekeepers Please merge, thanks!

@sommerlukas sommerlukas merged commit ba6cc2c into intel:sycl Feb 19, 2025
16 checks passed
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.

4 participants