Skip to content

[TIR] Handle Bind in LowerDeviceKernelLaunch#18912

Open
MasterJH5574 wants to merge 1 commit intoapache:mainfrom
MasterJH5574:tvm-dev/2026-03-18-cse
Open

[TIR] Handle Bind in LowerDeviceKernelLaunch#18912
MasterJH5574 wants to merge 1 commit intoapache:mainfrom
MasterJH5574:tvm-dev/2026-03-18-cse

Conversation

@MasterJH5574
Copy link
Contributor

@MasterJH5574 MasterJH5574 commented Mar 18, 2026

DeviceInfoCollector did not track Bind statements, so when CSE (or
any other pass) inserted a Bind before a thread_extent AttrStmt, the
collected extent referenced a locally-bound variable instead of
function parameters. LowerDeviceKernelLaunch then produced dangling
references in the host function.

Fix: record Bind definitions in DeviceInfoCollector and inline them
when extracting thread_extent values and dynamic shared memory sizes.

@gemini-code-assist
Copy link
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 addresses a critical issue where the Common Subexpression Elimination (CSE) pass inadvertently introduced Bind statements within thread_extent attributes, which were later removed by the LowerDeviceKernelLaunch pass. This removal caused a crash in the LLVM host codegen due to references to non-existent CSE variables. The fix prevents CSE from processing thread_extent values, ensuring the stability of the compilation pipeline without affecting CSE optimization for other parts of the code.

Highlights

  • Fix for LLVM Host Codegen Crash: Resolved a crash in LLVM host codegen that occurred when LowerDeviceKernelLaunch dropped Bind statements inserted by Common Subexpression Elimination (CSE) between nested thread_extent AttrStmts, leading to unresolved CSE variables.
  • CSE Behavior Modification for thread_extent: Modified the CSE pass to explicitly skip recording and rewriting expressions within thread_extent values. This ensures that thread_extent values remain inline and are not subject to CSE, while expressions in the body of the AttrStmt are still CSE'd normally.
  • New Test Case Added: Introduced a new test case, test_thread_extent_no_cse_between_launch, to verify that thread_extent values are correctly excluded from CSE while expressions within the statement body are still optimized.

🧠 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.

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.

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.

Copy link
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 addresses a crash in the Common Subexpression Elimination (CSE) pass when dealing with thread_extent attributes. The fix correctly prevents CSE from processing expressions within thread_extent values in both the CSEPlanner and CSERewriter, which resolves the issue with a later LowerDeviceKernelLaunch pass. The changes are well-targeted, and the new test case effectively validates the fix. I have one minor suggestion to improve code consistency.

if (body.same_as(op->body)) {
return ffi::GetRef<Stmt>(op);
}
return AttrStmt(op->node, op->attr_key, op->value, body);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this is functionally correct, it's more idiomatic in TVM's mutators to use the CopyOnWrite pattern. This also ensures that you can benefit from in-place updates when possible.

Consider changing this to:

auto n = CopyOnWrite(op);
n->body = std::move(body);
return Stmt(n);

@tqchen
Copy link
Member

tqchen commented Mar 18, 2026

would be good to cross check and deep deeper to see if lowe LowerDeviceKernelLaunch should be made aware of the Binds

@MasterJH5574 MasterJH5574 force-pushed the tvm-dev/2026-03-18-cse branch from 22efd83 to 74cd126 Compare March 18, 2026 21:46
@MasterJH5574 MasterJH5574 force-pushed the tvm-dev/2026-03-18-cse branch from 74cd126 to 4bd380d Compare March 27, 2026 02:35
DeviceInfoCollector did not track Bind statements, so when CSE (or
any other pass) inserted a Bind before a thread_extent AttrStmt, the
collected extent referenced a locally-bound variable instead of
function parameters.  LowerDeviceKernelLaunch then produced dangling
references in the host function.

Fix: record Bind definitions in DeviceInfoCollector and inline them
when extracting thread_extent values and dynamic shared memory sizes.
@MasterJH5574 MasterJH5574 force-pushed the tvm-dev/2026-03-18-cse branch from 4bd380d to 30c6f62 Compare March 27, 2026 02:36
@MasterJH5574 MasterJH5574 changed the title [TIR][CSE] Fix crash with thread_extent values [TIR] Handle Bind in LowerDeviceKernelLaunch Mar 27, 2026
@MasterJH5574
Copy link
Contributor Author

@tqchen @tlopex I just updated the PR. It turns out that the LowerDeviceKernelLaunch pass is the right place to fix the issue, and the CSE changes have been reverted. The PR description and title have already been updated as well.

@MasterJH5574 MasterJH5574 requested a review from tqchen March 27, 2026 02:45
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.

3 participants