-
Notifications
You must be signed in to change notification settings - Fork 333
[Bugfix] Do not force inline let stmt #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5d6e821
remove debug print
LeiWang1999 81f1cae
Remove inline let expressions from the LowerAndLegalize function in p…
LeiWang1999 abc2f8c
add test
LeiWang1999 f1aa27e
Update sparse MLA examples to support SKV adjustment and correctness …
LeiWang1999 6efbef8
reduce test shape
LeiWang1999 9354899
Update documentation structure and refactor main function parameters …
LeiWang1999 f372812
Update buffer access checks in merge_shared_memory_allocations.cc
LeiWang1999 ede05d3
lint fix
LeiWang1999 cc3138a
Support pipeline with LetStmt
LeiWang1999 597d8b1
lint fix
LeiWang1999 36736f3
• Fix LowerTileOp let handling to avoid LetInline dependency
LeiWang1999 0da83a8
Merge branch 'main' of https://github.com/tile-ai/tilelang into issue…
LeiWang1999 d0648e5
fix for wgmma pipeline with let binding
LeiWang1999 6f115d3
lint fix
LeiWang1999 04d66d6
test fix
LeiWang1999 afc668d
reduce smem usage.
LeiWang1999 c637767
let binding enhancement
LeiWang1999 531a3ae
fix for dpgm
LeiWang1999 4fa28c9
fix simplify
LeiWang1999 22ae8c5
lint fix
LeiWang1999 9ee3540
use tilelang.Simplify instead of tir.Simplify
LeiWang1999 2311fc7
• Add TL_FORCE_LET_INLINE pass config and gate eager LetInline usage
LeiWang1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| # LetStmt Inlining in TileLang | ||
|
|
||
| This document explains how `LetStmt` inlining works in TileLang's simplification pipeline, which is an important optimization that affects code generation and performance. | ||
|
|
||
| ## Overview | ||
|
|
||
| A `LetStmt` (Let Statement) is a temporary variable binding in the IR (Intermediate Representation). During compilation, TileLang's simplifier may choose to inline these temporary variables to simplify the code. TileLang also provides a standalone `LetInline` pass that performs eager substitution before the main legalization pipeline. However, not all `LetStmt` nodes can be safely inlined. | ||
|
|
||
| ## When Does LetStmt Get Inlined? | ||
|
|
||
| The inlining logic is implemented in `src/transform/simplify.cc`. A `LetStmt` will be inlined if **both** of the following conditions are met: | ||
|
|
||
| ### 1. The value satisfies `CanInlineLetStmt` | ||
|
|
||
| The `CanInlineLetStmt` helper returns `true` when: | ||
|
|
||
| - **The value is a constant** (`is_const_number(op->value)` returns true) | ||
| - **The value is a variable** (`op->value.as<VarNode>()` returns a node) | ||
| - **The value is an integer expression without side effects**: | ||
| - The value has `int` dtype | ||
| - The side effect level is `kPure` or lower (no observable side effects) | ||
|
|
||
| ```cpp | ||
| bool CanInlineLetStmt(const LetStmtNode *op) { | ||
| if (is_const_number(op->value)) | ||
| return true; | ||
| if (op->value.as<VarNode>()) | ||
| return true; | ||
| // Won't face the deep expression explosion problem as in Let expression. | ||
| // attempt to inline as much as possible if the value integer type(can be | ||
| // index). | ||
| if (!op->value.dtype().is_int()) | ||
| return false; | ||
| return SideEffect(op->value) <= CallEffectKind::kPure; | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. The variable is NOT used in buffer definitions | ||
|
|
||
| Even if `CanInlineLetStmt` returns true, the variable will **not** be inlined if it's used in a buffer's definition (shape, strides, elem_offset, or data fields). | ||
|
|
||
| This protection exists because: | ||
| - Buffer definitions are not updated during the simplification pass | ||
| - If a variable used in a buffer definition is inlined, later references to that buffer would fail to find the variable definition | ||
| - This would cause compilation errors or incorrect behavior | ||
|
|
||
| The mutator checks this before dropping the binding: | ||
|
|
||
| ```cpp | ||
| bool used_in_buffer_def = used_in_buffer_def_.count(op->var.get()); | ||
|
|
||
| if (can_inline && !used_in_buffer_def) { | ||
| return body; // Inline: remove LetStmt and return body directly | ||
| } | ||
| ``` | ||
|
|
||
| ## Example: Why Buffer Definition Variables Are Protected | ||
|
|
||
| Consider this code: | ||
|
|
||
| ```python | ||
| let stride = M * 16 | ||
| let buffer_a = Buffer(data, shape=[M, N], strides=[stride, 1]) | ||
| buffer_a[i, j] = ... | ||
| ``` | ||
|
|
||
| - `stride` satisfies `CanInlineLetStmt` (it's an int expression with no side effects) | ||
| - However, `stride` is used in `buffer_a`'s `strides` field | ||
| - If we inline it, the buffer definition becomes `strides=[M*16, 1]` | ||
| - But the Buffer object's fields are not updated during simplification | ||
| - Later code accessing `buffer_a` would fail to find the `stride` variable | ||
|
|
||
| Therefore, `stride` is added to `used_in_buffer_def_` and will **not** be inlined. | ||
|
|
||
| ## How Variables Are Collected | ||
|
|
||
| The `CollectVarsUsedInBufferDefinition` helper traverses all `BufferLoad` and `BufferStore` nodes and collects variables used in their buffer definitions: | ||
|
|
||
| ```cpp | ||
| void VisitBuffer(const Buffer &buf) { | ||
| // Collect variables that should remain defined | ||
| VarUseDefAnalyzer usage(Array<Var>{}); | ||
| usage(buf->data); | ||
| for (const auto &dim : buf->shape) { | ||
| usage(dim); | ||
| } | ||
| for (const auto &dim : buf->strides) { | ||
| usage(dim); | ||
| } | ||
| usage(buf->elem_offset); | ||
|
|
||
| // Track for use in LetStmtNode mutator | ||
| for (const auto &var : usage.undefined_) { | ||
| used_in_buffer_def_.insert(var.get()); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Practical Example: Temporary Variable Issue | ||
|
|
||
| Consider this TileLang code: | ||
|
|
||
| ```python | ||
| for i in T.Parallel(block_N): | ||
| idx = bx * block_N + i | ||
| tmp = T.max(A[idx], 1) | ||
| B[idx] = tmp / 2 | ||
| A[idx] = tmp * 2 | ||
| ``` | ||
|
|
||
| In this case: | ||
| - `tmp` is an integer-like temporary variable | ||
| - It satisfies `CanInlineLetStmt` (pure int expression) | ||
| - It's **not** used in any buffer definition | ||
| - Therefore, `tmp` **will be inlined** | ||
|
|
||
| This means the IR becomes: | ||
|
|
||
| ```python | ||
| for i in T.Parallel(block_N): | ||
| idx = bx * block_N + i | ||
| B[idx] = T.max(A[idx], 1) / 2 | ||
| A[idx] = T.max(A[idx], 1) * 2 | ||
| ``` | ||
|
|
||
| If this causes issues (e.g., `A[idx]` being read twice with different values due to the first write), it indicates a potential problem with the inlining heuristic or the code pattern. | ||
|
|
||
| ## Controlling Let Inlining via Pass Config | ||
|
|
||
| TileLang exposes an explicit pass configuration key, `tilelang.PassConfigKey.TL_FORCE_LET_INLINE` (`"tl.force_let_inline"`), that allows users to force the eager `LetInline` pass to run before the legalization pipeline begins. When enabled, the pipeline invokes `tilelang.transform.LetInline()` at the start of `LowerAndLegalize` (see `tilelang/engine/phase.py`). This knob is useful when debugging LetStmt-related issues or when deterministic inlining behavior is desired across different environments. | ||
|
|
||
| ```python | ||
| from tilelang import transform | ||
| from tilelang.engine.phase import LowerAndLegalize | ||
|
|
||
| with transform.PassContext( | ||
| config={transform.PassConfigKey.TL_FORCE_LET_INLINE: True} | ||
| ): | ||
| lowered_mod = LowerAndLegalize(input_mod, target) | ||
| ``` | ||
|
|
||
| If the flag is left unset (the default), the eager pass is only applied when downstream transforms opt in (for example, by calling `_Simplify(..., inline_let=True)` inside Tile operators). The guard in `tilelang/engine/phase.py` ensures the eager pass is only triggered when the user explicitly requests it. | ||
|
|
||
| ## Summary | ||
|
|
||
| The LetStmt inlining mechanism is a **conservative optimization** that: | ||
| 1. Aggressively inlines simple, pure integer expressions to simplify the IR | ||
| 2. Protects variables used in buffer definitions to avoid breaking buffer access | ||
| 3. Helps reduce IR complexity and improve code generation | ||
| 4. Can be forced through `TL_FORCE_LET_INLINE` when deterministic eager inlining is required | ||
|
|
||
| Understanding when inlining happens is crucial for: | ||
| - Debugging compilation issues | ||
| - Understanding generated code | ||
| - Writing efficient TileLang programs | ||
| - Identifying potential optimization opportunities or bugs | ||
|
|
||
| ## Related Files | ||
|
|
||
| - `src/transform/simplify.cc`: Main Simplify implementation | ||
| - `src/transform/frontend_legalize.cc`: Standalone LetInline pass | ||
| - `tilelang/engine/phase.py`: Pipeline integration for eager LetInlining | ||
| - `testing/python/transform/test_tilelang_transform_let_inline.py`: Regression coverage for the pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revise the hazard explanation; current sequence doesn’t demonstrate a read-after-write issue.
In the example, both reads occur before the write to A[idx], so “due to the first write” isn’t accurate. Consider a case where A[idx] is written between two reads to illustrate why duplicated loads from inlining can change behavior, or rephrase to note duplicate loads impact performance but not semantics here.
📝 Committable suggestion
🤖 Prompt for AI Agents