[TIR][FEAT] Require DeclBuffer before use in verify_well_formed#18843
[TIR][FEAT] Require DeclBuffer before use in verify_well_formed#18843tqchen wants to merge 4 commits intoapache:mainfrom
Conversation
Enforce that all buffers used in BufferLoad, BufferStore, or BufferRegion must have a prior DeclBuffer or equivalent declaration (alloc_buffer, match_buffer, buffer_map, etc.) in scope. Changes: - verify_well_formed.cc: Make UndefinedBufferVerifier check unconditional (remove schedule-level-only restriction). - flatten_buffer.cc: Emit DeclBuffer nodes for flattened parameter buffer views at function body level. - tir_visitor_with_path.cc: Visit SBlock alloc_buffers definitions before reads/writes so allocated buffers are in scope for region references. - layout_transformation.cc: Fix buffer identity in TransformLayoutRewriter -- update match_buffer source references and inferred access regions to use new_buffer_ instead of stale old_buffer_ after layout transform. - renew_defs.cc: Add DeclBufferNode visitor to RenewDefMutator so buffer data vars are properly renewed through DeclBuffer nodes. - Update ~60 test instances across 21 files to use T.decl_buffer() where needed, add check_well_formed=False for expected outputs with buffer views, and adjust body traversal paths for DeclBuffer nesting.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the well-formedness checks for buffers within TVM's Tensor IR (TIR) by enforcing that all buffers are explicitly declared before their use. This change aligns with RFC#70, improving the robustness and consistency of buffer management throughout the compiler's intermediate representation. It involves updates to core analysis, transformation, and visitation passes, along with comprehensive test case adjustments to reflect the new invariant. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
- Break long line in test_s_tir_transform_loop_partition.py:478 by extracting index calculation - Rename unused B_new to _B_new in test_tir_transform_flatten_buffer.py:313
There was a problem hiding this comment.
Code Review
This pull request correctly enforces the DeclBuffer-before-use invariant, a significant step towards better TIR well-formedness as per RFC#70. The changes are comprehensive, including the new UndefinedBufferVerifier, updates to various TIR transforms like FlattenBuffer and RenewDefMutator, and extensive modifications to tests to align with the new requirement. The logic for adding DeclBuffer for flattened parameter buffers and handling buffer scopes in different nodes seems correct and robust. The test updates are thorough, covering both positive cases and expected failures, which ensures the new verification logic is working as intended. I have one minor suggestion for code simplification, but overall, this is a high-quality contribution.
| auto new_source = match_buf->source; | ||
| auto* source_n = new_source.CopyOnWrite(); | ||
| source_n->buffer = new_buffer_; | ||
| auto new_match = match_buf; | ||
| new_match.CopyOnWrite()->source = new_source; | ||
| return new_match; |
There was a problem hiding this comment.
This logic for updating the match_buffer is correct, but it can be made more concise and idiomatic by chaining the CopyOnWrite calls. This improves readability without changing the functionality.
| auto new_source = match_buf->source; | |
| auto* source_n = new_source.CopyOnWrite(); | |
| source_n->buffer = new_buffer_; | |
| auto new_match = match_buf; | |
| new_match.CopyOnWrite()->source = new_source; | |
| return new_match; | |
| auto new_match = match_buf; | |
| auto* n = new_match.CopyOnWrite(); | |
| n->source.CopyOnWrite()->buffer = new_buffer_; | |
| return new_match; |
- Fix F841 unused variable by prefixing with underscore: _A_local - Fix ruff format issues in multiple test files - Fix clang-format issues in flatten_buffer.cc
Summary
Enforce the DeclBuffer-before-use invariant from RFC#70 across all TIR.
Changes