Skip to content
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

[METAL] Fix flaky memory issue due to racing #14671

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/runtime/metal/metal_device_api.mm
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ int GetWarpSize(id<MTLDevice> dev) {

void MetalWorkspace::FreeDataSpace(Device dev, void* ptr) {
AUTORELEASEPOOL {
// need to make sure buffer is not in use in command buffer
// before set the purgeable state to empty
// otherwise can cause issues sometimes
this->StreamSync(dev, nullptr);
// MTLBuffer PurgeableState should be set to empty before manual
// release in order to prevent memory leak
[(id<MTLBuffer>)ptr setPurgeableState:MTLPurgeableStateEmpty];
Expand Down Expand Up @@ -336,6 +340,10 @@ int GetWarpSize(id<MTLDevice> dev) {
if (temp_buffer_[dev.device_id] == nil || temp_buffer_[dev.device_id].length < size) {
id<MTLDevice> mtl_dev = MetalWorkspace::Global()->GetDevice(dev);
if (temp_buffer_[dev.device_id] != nil) {
// need to make sure buffer is not in use in command buffer
// before set the purgeable state to empty
// otherwise can cause issues sometimes
MetalWorkspace::Global()->StreamSync(dev, nullptr);
[temp_buffer_[dev.device_id] setPurgeableState:MTLPurgeableStateEmpty];
[temp_buffer_[dev.device_id] release];
}
Expand Down