[vulkan] Set BlockRegion initial usage_count to 1 on allocation#9237
Open
johningve wants to merge 1 commit into
Open
[vulkan] Set BlockRegion initial usage_count to 1 on allocation#9237johningve wants to merge 1 commit into
johningve wants to merge 1 commit into
Conversation
BlockRegion has a usage_count that is incremented by RegionAllocator::retain and decremented by RegionAllocator::release. These methods are used in the runtime when a buffer is cropped, with the intent to ensure that the underlying memory region does not get deallocated until both the cropped buffer and original buffer are released. The order of operations as seen by the RegionAllocator when allocating a buffer, then allocating a crop of that first buffer, then releasing both, is this: 1. region = reserve(...) 2. retain(region) 3. release(region) 4. release(region) Note that after 3, but before 4, the region should still be allocated and the region allocator should not allocate any new region that overlaps it. Testing this pattern found that the region allocator considers the region to be available after step 3. In practice, this causes Halide to give the same underlying memory to two live buffers simultaneously. The cause of this issue is that RegionAllocator::alloc_block_region does not update the usage_count of the allocated block region from its default value of 0. This commit fixes the issue by setting the usage count of the newly allocated block region to 1 in the same place where its status is updated, indicating that the newly allocated region will have one usage, which must be released before the memory can be reallocated to a new region. This commit includes a regression test of retain/release with the exact same order of operations as was mentioned above. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
johningve
force-pushed
the
region-allocator-usage-count
branch
from
July 24, 2026 13:05
3c38b94 to
0f1046c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9237 +/- ##
==========================================
- Coverage 70.24% 70.10% -0.15%
==========================================
Files 255 255
Lines 78929 78904 -25
Branches 18879 18866 -13
==========================================
- Hits 55444 55313 -131
- Misses 17872 17898 +26
- Partials 5613 5693 +80 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
BlockRegion has a usage_count that is incremented by RegionAllocator::retain and decremented by RegionAllocator::release. These methods are used in the runtime when a buffer is cropped, with the intent to ensure that the underlying memory region does not get deallocated until both the cropped buffer and original buffer are released.
The order of operations as seen by the RegionAllocator when allocating a buffer, then allocating a crop of that first buffer, then releasing both, is this:
Note that after 3, but before 4, the region should still be allocated and the region allocator should not allocate any new region that overlaps it.
Testing this pattern found that the region allocator considers the region to be available after step 3. In practice, this causes Halide to give the same underlying memory to two live buffers simultaneously.
The cause of this issue is that RegionAllocator::alloc_block_region does not update the usage_count of the allocated block region from its default value of 0.
This commit fixes the issue by setting the usage count of the newly allocated block region to 1 in the same place where its status is updated, indicating that the newly allocated region will have one usage, which must be released before the memory can be reallocated to a new region.
This commit includes a regression test of retain/release with the exact same order of operations as was mentioned above.
Checklist