-
Notifications
You must be signed in to change notification settings - Fork 787
[SYCL][L0] Fix the Pi Platform Cache memory leak #2881
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read the comment above why we used "new". To avoid the leak we need to implement proper tear-down process, similar to how SYCL RT does it now: #2516
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I think I misunderstood you.
This PR is only fixing the memory leak for the PiPlatformCache and PiPlatformCacheMutex objects.
PiPlatformCache is a vector of pi_platform pointers which are allocated on the heap.
This PR is not intended to fix the leak of pi_platforms that are allocated somewhere else.
If there is an API called piPlatformRelease, I can easily implement a deallocation.
But, there is no such PI API, so SYCL RT never informs to plugins that platform_impl objects are deallocated so the underlying pi_platform can be deallocated, too.
Anyway, I am still working on the memory leak of pi_platform objects, which is not intended to fix in this PR.
This PR is intended only to fix the memory leak of vector itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please read lines 809-813 to see why it is not OK to use static globals for the cache (non-trivial data object): the order of static objects destruction is undefined.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. then I did not misunderstand you.
Let me explain what is going on with this static variable.
First of all PiPlatformsCache is already a "static" variable.
static auto PiPlatformsCache = new std::vector<pi_platform>;
Secondly, this is not a global variable. It is a local static variable. It seems like misunderstanding.
This "static" pointer variable points to the vector that is allocated on the heap.
As you may know, the heap allocated variable is not automatically deallocated unless we call "delete" for this pointer variable, right?
My PR changes this static pointer variable to a static local variable.
The static local variable persists in the static memory region (which is not on stack or heap) even after the function returns, but it will be automatically deallocated when the whole program process terminates.
This way the static local variable is deallocated when the program terminates.
As I said, this PR is not intended to deallocate the memory allocated for the pi_platform vector itself.
Hope this makes sense.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me help you understand how my PR fixes the leak in PiPlatformsCache with an example.
Here is a simple code that shows the memory leaks for the local static variable.
This code allocates 3 integers, a, b, c on the heap, and then create a new vector, vec, on the heap.
Since there was no “delete” for these heap variables, all those heap memory are leaked.
Now I changed the variable ‘PiPlatformsCache’ to a local static vector.
Now, valgrind only detects memory leak for a, b, c.
Does it makes sense to you now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the problem. The order of destruction of such static object (at program termination) is undefined. There might be other globals that depend on this one, but are deallocated later than this one. Allocating with
new
"resolves" this ordering problem by introducing an informed leak.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you tell me where you got this info from?
As far as I know, the static variables have a well-defined construction and destruction order.
The destruction order of static variables is the reverse of the construction order of them.
And, all global variables are constructed before main() starts, whereas static variables are constructed only when the specific scope is reached (i.e., the defining function is called).
I know "global" objects have undefined destruction order, but static variables are not global variables.