[TIR] Fix reduce buffer allocation position#17799
Merged
Hzfengsy merged 2 commits intoApr 3, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR addresses a bug in the TIR transformation where the reduction write buffer allocation position is incorrectly placed under a reduction tiling loop. The changes in this PR add a new test case to verify that the reduction buffer allocation now correctly dominates all reduce loops.
- Added a new test function (test_reduce_buffer_dominate_reduce_loops) to compare the IR before and after applying the buffer allocation transformation.
- Enhanced the test by including explicit TIR annotations (T.reads/T.writes) to ensure the transformation preserves correct buffer reuse behavior.
Files not reviewed (1)
- src/tir/analysis/buffer_access_lca_detector.cc: Language not supported
Comments suppressed due to low confidence (2)
tests/python/tir-transform/test_tir_transform_plan_update_buffer_allocation_location.py:432
- [nitpick] The allocated shape for x_red_ is (256, 256), but based on the intended reduction buffer compaction (as noted in the description), a more compact shape (e.g., (64, 256) or (64, 64)) might be expected. Consider verifying that the allocation shape aligns with the intended transformation.
x_red_ = T.alloc_buffer((256, 256))
tests/python/tir-transform/test_tir_transform_plan_update_buffer_allocation_location.py:443
- [nitpick] The index expression 'ax0_0 * 64 + ax0_1 - ax0_0 * 64' simplifies to 'ax0_1', and similarly for the second index, which might improve readability if simplified. Consider simplifying these expressions to make the intent clearer.
x_red_[ax0_0 * 64 + ax0_1 - ax0_0 * 64, ax1_0 * 64 + ax1_1 - ax1_0 * 64] = T.float32(0.0)
ShiboXing
pushed a commit
to ShiboXing/tvm
that referenced
this pull request
Aug 10, 2025
* fix reduce buffer allocation position * fix test_tir_analysis_detect_buffer_access_lca.py::test_buffer_load_store
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.
The change modify lca detector to ensure the reduction buffer allocation position dominates all reduce related loops. This behavior is also compatible with same logic in
DecomposeReduction.Below is a working case with wrong cpu compile results. We just build a simple reduce-sum function, with outer tiling loops in order "SRS".
The error is due to transformation
tir.PlanAndUpdateBufferAllocationLocationandtir.CompactBufferAllocation: