refactor(ir): switch to pointer identity for variable management in IR transforms#625
refactor(ir): switch to pointer identity for variable management in IR transforms#625lyfne123 wants to merge 1 commit intohw-native-sys:mainfrom
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR switches multiple IR transform passes from name-based (string) variable maps to pointer-identity ( Changes
Sequence Diagram(s)(omitted — changes are internal refactors and targeted lookup/fix behavior that do not introduce a new multi-component runtime control flow requiring a sequence diagram) Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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 introduces a fundamental change in how variables are identified and managed within the Intermediate Representation (IR) layer. By transitioning from string-based naming to pointer identity, the system gains improved robustness against naming collisions and enhances type safety. This refactoring impacts several core IR transformation passes, ensuring a more consistent and reliable approach to variable handling throughout the compilation pipeline. Highlights
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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors several IR transformation passes to use pointer identity (const Var*) instead of string-based name_hint_ for variable identification and substitution. This is a significant improvement for the robustness and maintainability of the IR layer. The changes are consistently applied across convert_tensor_to_tile_ops_pass.cpp, flatten_tile_nd_to_2d_pass.cpp, and resolve_transpose_layout_pass.cpp. A new centralized SubstituteVars utility has been introduced, replacing local mutator implementations. The handling of complex cross-scope variable matching in flatten_tile_nd_to_2d_pass.cpp with a hybrid pointer/name approach is well-considered. The overall refactoring is thorough and correct.
…R transforms - Updated multiple IR transformation files to utilize pointer identity (`const Var*`) instead of string-based `name_hint_` for variable identification. - Enhanced variable substitution and mapping methods to improve type safety and reduce reliance on string comparisons. - Refactored relevant functions to ensure consistent handling of variable identities across transformations. - Improved overall code clarity and maintainability by centralizing variable identity logic. This refactor aligns with recent changes aimed at enhancing the robustness of variable management in the IR layer.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/ir/transforms/flatten_tile_nd_to_2d_pass.cpp`:
- Around line 337-345: The current code in flatten_tile_nd_to_2d_pass.cpp
substitutes an IfStmt/ForStmt/WhileStmt return var by directly reusing a
body-local Var (see if_stmt->return_vars_, then_ctx.name_to_new_var and
ctx.Insert(rv, it->second)), which breaks the invariant that control-flow nodes
have fresh defs; instead, for each matched rv create a fresh Var with the same
type/name-hint as the original (e.g., new_rv), push new_rv into new_return_vars,
insert the mapping from the original rv to new_rv into ctx (ctx.Insert(rv,
new_rv)), and then map the body-local var to that new_rv so uses after the
branch/loop refer to the fresh control-flow def (ensure the same fix is applied
in the analogous blocks referenced at lines 383-387 and 424-428).
In `@tests/ut/ir/transforms/test_basic_memory_reuse.py`:
- Around line 1054-1076: The helper _find_first_for_stmt currently skips
ir.ScopeStmt, causing false negatives when loops are wrapped in scopes; update
_find_first_for_stmt to detect isinstance(stmt, ir.ScopeStmt) and recurse into
the inner scope (e.g., call _find_first_for_stmt on stmt.body or the ScopeStmt's
contained statement sequence) just like it does for ir.WhileStmt and ir.IfStmt
so the search descends into scope wrappers.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 946fa49b-bc49-48a4-b218-31c7479afdbb
📒 Files selected for processing (6)
src/codegen/pto/pto_codegen.cppsrc/ir/transforms/basic_memory_reuse_pass.cppsrc/ir/transforms/convert_tensor_to_tile_ops_pass.cppsrc/ir/transforms/flatten_tile_nd_to_2d_pass.cppsrc/ir/transforms/resolve_transpose_layout_pass.cpptests/ut/ir/transforms/test_basic_memory_reuse.py
🚧 Files skipped from review as they are similar to previous changes (1)
- src/codegen/pto/pto_codegen.cpp
| // Substitute return_vars using the branch contexts (name-based cross-scope matching) | ||
| std::vector<VarPtr> new_return_vars; | ||
| new_return_vars.reserve(if_stmt->return_vars_.size()); | ||
| for (const auto& rv : if_stmt->return_vars_) { | ||
| auto it = then_ctx.var_map.find(rv->name_hint_); | ||
| if (it != then_ctx.var_map.end()) { | ||
| auto it = then_ctx.name_to_new_var.find(rv->name_hint_); | ||
| if (it != then_ctx.name_to_new_var.end()) { | ||
| new_return_vars.push_back(it->second); | ||
| ctx.var_map[rv->name_hint_] = it->second; | ||
| ctx.Insert(rv, it->second); | ||
| } else { |
There was a problem hiding this comment.
Keep return_vars_ as fresh control-flow defs.
Here it->second is a var defined inside the branch or loop body. Reusing that pointer as the IfStmt/ForStmt/WhileStmt return var breaks the invariant that return_vars_ are separate defs for the control-flow node, and later passes can substitute post-branch/post-loop uses to a body-local var instead of the statement output. Please rebuild a fresh Var for the rewritten type and map rv -> new_rv, rather than wiring rv directly to the body var.
Minimal fix pattern
- if (it != body_ctx.name_to_new_var.end()) {
- new_return_vars.push_back(it->second);
- ctx.Insert(rv, it->second);
+ if (it != body_ctx.name_to_new_var.end()) {
+ auto new_rv = std::make_shared<Var>(rv->name_hint_, it->second->GetType(), rv->span_);
+ new_return_vars.push_back(new_rv);
+ ctx.Insert(rv, new_rv);
} else {Also applies to: 383-387, 424-428
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/ir/transforms/flatten_tile_nd_to_2d_pass.cpp` around lines 337 - 345, The
current code in flatten_tile_nd_to_2d_pass.cpp substitutes an
IfStmt/ForStmt/WhileStmt return var by directly reusing a body-local Var (see
if_stmt->return_vars_, then_ctx.name_to_new_var and ctx.Insert(rv, it->second)),
which breaks the invariant that control-flow nodes have fresh defs; instead, for
each matched rv create a fresh Var with the same type/name-hint as the original
(e.g., new_rv), push new_rv into new_return_vars, insert the mapping from the
original rv to new_rv into ctx (ctx.Insert(rv, new_rv)), and then map the
body-local var to that new_rv so uses after the branch/loop refer to the fresh
control-flow def (ensure the same fix is applied in the analogous blocks
referenced at lines 383-387 and 424-428).
| def _find_first_for_stmt(stmt): | ||
| """Return the first ForStmt found in a statement tree.""" | ||
| if isinstance(stmt, ir.ForStmt): | ||
| return stmt | ||
| if isinstance(stmt, ir.SeqStmts): | ||
| for child in stmt.stmts: | ||
| found = _find_first_for_stmt(child) | ||
| if found is not None: | ||
| return found | ||
| if isinstance(stmt, ir.OpStmts): | ||
| for child in stmt.stmts: | ||
| found = _find_first_for_stmt(child) | ||
| if found is not None: | ||
| return found | ||
| if isinstance(stmt, ir.IfStmt): | ||
| found = _find_first_for_stmt(stmt.then_body) | ||
| if found is not None: | ||
| return found | ||
| if stmt.else_body is not None: | ||
| return _find_first_for_stmt(stmt.else_body) | ||
| if isinstance(stmt, ir.WhileStmt): | ||
| return _find_first_for_stmt(stmt.body) | ||
| return None |
There was a problem hiding this comment.
Descend into scope wrappers here.
This helper skips ir.ScopeStmt, so the new regression test will false-negative as soon as a normalization pass wraps the loop body in a scope.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/ut/ir/transforms/test_basic_memory_reuse.py` around lines 1054 - 1076,
The helper _find_first_for_stmt currently skips ir.ScopeStmt, causing false
negatives when loops are wrapped in scopes; update _find_first_for_stmt to
detect isinstance(stmt, ir.ScopeStmt) and recurse into the inner scope (e.g.,
call _find_first_for_stmt on stmt.body or the ScopeStmt's contained statement
sequence) just like it does for ir.WhileStmt and ir.IfStmt so the search
descends into scope wrappers.
const Var*) instead of string-basedname_hint_for variable identification.This refactor aligns with recent changes aimed at enhancing the robustness of variable management in the IR layer.