Skip to content

[TIR][FEAT] Require DeclBuffer before use in verify_well_formed#18840

Closed
tqchen wants to merge 1 commit intoapache:mainfrom
tqchen:require-declbuffer-before-use
Closed

[TIR][FEAT] Require DeclBuffer before use in verify_well_formed#18840
tqchen wants to merge 1 commit intoapache:mainfrom
tqchen:require-declbuffer-before-use

Conversation

@tqchen
Copy link
Copy Markdown
Member

@tqchen tqchen commented Feb 27, 2026

Summary

  • Add UndefinedBufferVerifier to verify_well_formed that checks buffer-scope invariants: buffers declared via buffer_map, DeclBuffer, alloc_buffers, match_buffers, or buffer_bind_scope must not be used outside their declaration scope
  • In schedule-level TIR (functions with SBlock nodes), all buffers in BufferLoad/BufferStore must have a prior declaration
  • Fix TIRVisitorWithPath to call WithDef for Buffer at two previously missing sites: SBlock::match_buffers and AttrStmt::buffer_bind_scope

Follows up on the concept from draft PR #14778 (RFC#70). The check is gated to schedule-level TIR only, since FlattenBuffer strips DeclBuffer statements.

Test plan

  • 6 new tests in test_tir_analysis_verify_well_formed.py (buffer_map, decl_buffer, alloc_buffer, match_buffer, error_out_of_scope, error_undeclared)
  • All 23 existing + new tests pass
  • CI checks

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.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 Buffer Verification: Introduced UndefinedBufferVerifier to enforce buffer-scope invariants within verify_well_formed, ensuring buffers are used only within their declared scopes.
  • Stricter Schedule-Level TIR Checks: Implemented a stricter check for schedule-level TIR, requiring all buffers used in BufferLoad or BufferStore to have a prior declaration.
  • TIRVisitorWithPath Fixes: Corrected TIRVisitorWithPath to properly track Buffer definitions in SBlock::match_buffers and AttrStmt::buffer_bind_scope.

🧠 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
  • src/tir/analysis/verify_well_formed.cc
    • Added UndefinedBufferVerifier class to check buffer declaration scopes and usage.
    • Integrated UndefinedBufferVerifier into VerifyWellFormed for both PrimFunc and IRModule verification.
  • src/tir/ir/tir_visitor_with_path.cc
    • Updated AttrStmtNode visitor to include DefContext<Buffer> for buffer_bind_scope attributes.
    • Updated SBlockNode visitor to include DefContext<Buffer> for match_buffers.
  • tests/python/tir-analysis/test_tir_analysis_verify_well_formed.py
    • Added test_buffer_in_buffer_map_is_well_formed to verify buffers from function parameters.
    • Added test_decl_buffer_is_well_formed to verify buffers declared via DeclBuffer.
    • Added test_alloc_buffer_in_block_is_well_formed to verify buffers allocated in SBlock::alloc_buffers.
    • Added test_match_buffer_in_block_is_well_formed to verify buffers matched in SBlock::match_buffers.
    • Added test_error_buffer_used_out_of_decl_scope to test out-of-scope buffer usage.
    • Added test_error_undeclared_buffer_in_schedulable_tir to test undeclared buffer usage in schedule-level TIR.
Activity
  • No human activity (comments, reviews, progress updates) was provided in the context.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@tqchen tqchen closed this Feb 27, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
previously_defined_.insert({buffer, path});
previously_defined_[buffer] = path;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant