[TIR][FEAT] Require DeclBuffer before use in verify_well_formed#18840
[TIR][FEAT] Require DeclBuffer before use in verify_well_formed#18840tqchen wants to merge 1 commit intoapache:mainfrom
Conversation
This PR adds an UndefinedBufferVerifier to verify_well_formed that checks two buffer-scope invariants: (1) any buffer that was declared via buffer_map, DeclBuffer, alloc_buffers, match_buffers, or buffer_bind_scope must not be used outside its declaration scope; (2) in schedule-level TIR (functions with SBlock nodes), all buffers accessed in BufferLoad/BufferStore must have a prior declaration. Also fixes TIRVisitorWithPath to call WithDef for Buffer at two previously missing sites: SBlock::match_buffers and AttrStmt::buffer_bind_scope.
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 TensoIR (TIR) verification process by introducing robust checks for buffer declaration and usage. It ensures that buffers are always used within their defined scopes and, for schedule-level TIR, that all buffers are explicitly declared before use. This improves the correctness and reliability of TIR programs by catching common buffer-related errors early in the compilation pipeline. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new UndefinedBufferVerifier to enforce buffer declaration and scope rules in TIR, which is a valuable addition for ensuring TIR well-formedness. The changes correctly identify out-of-scope and undeclared buffer usages, particularly for schedule-level TIR. The fixes in TIRVisitorWithPath to properly handle buffer definitions from match_buffers and buffer_bind_scope are also correct and necessary. The overall implementation is solid and well-tested. I've found one issue in the logic for tracking out-of-scope buffers that could lead to misleading error messages, for which I've provided a suggestion.
| if (active_def != currently_defined_.end()) { | ||
| currently_defined_.erase(active_def); | ||
| } | ||
| previously_defined_.insert({buffer, path}); |
There was a problem hiding this comment.
std::unordered_map::insert does not update the value if an element with the same key already exists. If a buffer is defined, goes out of scope, and is then redefined in a later non-overlapping scope, previously_defined_ will retain the AccessPath from the end of the first scope. This can lead to confusing error messages that point to an older, incorrect declaration site. Using operator[] or insert_or_assign will ensure the path to the most recent out-of-scope definition is always stored.
| previously_defined_.insert({buffer, path}); | |
| previously_defined_[buffer] = path; |
Summary
UndefinedBufferVerifiertoverify_well_formedthat checks buffer-scope invariants: buffers declared viabuffer_map,DeclBuffer,alloc_buffers,match_buffers, orbuffer_bind_scopemust not be used outside their declaration scopeSBlocknodes), all buffers inBufferLoad/BufferStoremust have a prior declarationTIRVisitorWithPathto callWithDefforBufferat two previously missing sites:SBlock::match_buffersandAttrStmt::buffer_bind_scopeFollows up on the concept from draft PR #14778 (RFC#70). The check is gated to schedule-level TIR only, since
FlattenBufferstripsDeclBufferstatements.Test plan
test_tir_analysis_verify_well_formed.py(buffer_map, decl_buffer, alloc_buffer, match_buffer, error_out_of_scope, error_undeclared)