Skip to content

[TIR][CSE] Fix crash with thread_extent values#18912

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

[TIR][CSE] Fix crash with thread_extent values#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

Summary

CSE previously inserted Bind statements between nested thread_extent AttrStmts when the extent value (e.g. 511 // B + 1) also appeared in the body. LowerDeviceKernelLaunch extracts thread_extent values from the device function and transplants them as kernel launch arguments in the host function, assuming they are expressible in terms of the function's parameters. CSE broke this assumption by replacing thread_extent values with locally-scoped Bind variables, producing dangling references in the host function and crashing LLVM codegen with cannot find variable cse_v3.

Fix: skip recording and rewriting expressions in thread_extent values. Body expressions are still CSE'd normally.

Test plan

  • Existing 21 CSE tests pass
  • New test_thread_extent_no_cse_between_launch regression test (before/after with assert_structural_equal)
  • Original reproducer builds successfully with CUDA target

@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

CSE previously inserted Bind statements between nested thread_extent
AttrStmts when the extent value (e.g. 511 // B + 1) also appeared in
the body. LowerDeviceKernelLaunch later dropped these Binds while the
thread_extent value still referenced CSE variables, causing LLVM host
codegen to fail with "cannot find variable cse_v3".

Fix: skip recording and rewriting expressions in thread_extent values.
Body expressions are still CSE'd normally.
@MasterJH5574 MasterJH5574 force-pushed the tvm-dev/2026-03-18-cse branch from 22efd83 to 74cd126 Compare March 18, 2026 21:46
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