Skip to content

refactor(ir): use pointer identity instead of name_hint_ in IR transforms#624

Merged
lyfne123 merged 2 commits intohw-native-sys:mainfrom
Hzfengsy:issue-616-var-identity-not-name-hint
Mar 19, 2026
Merged

refactor(ir): use pointer identity instead of name_hint_ in IR transforms#624
lyfne123 merged 2 commits intohw-native-sys:mainfrom
Hzfengsy:issue-616-var-identity-not-name-hint

Conversation

@Hzfengsy
Copy link
Member

Summary

  • Migrate 7 IR transform files from using Var::name_hint_ (a cosmetic IgnoreField) as semantic variable identity to using const Var* pointer identity
  • Add CanonicalVarTracker class in the SSA converter to confine all name-based identity logic to a single class, with all other SSA logic using canonical pointer keys
  • Replace canonicalize_name() string-suffix-stripping in expand_mixed_kernel_pass with a structural pointer-based origin map that traces each Var back to its originating function parameter
  • Replace string-keyed maps with pointer-keyed maps in dependency_analyzer, scope_outline_utils, and all 3 outline pass callers

Testing

  • All 2558 unit tests pass (including 47 SSA, 19+10+22 outline, 36 expand_mixed_kernel)
  • Code review passed — no issues found
  • clang-tidy passed (all findings resolved)
  • All pre-commit hooks pass (clang-format, cpplint, etc.)

Related Issues

Fixes #616

…orms

Migrate dependency_analyzer, scope_outline_utils, expand_mixed_kernel,
and convert_to_ssa transforms from string-based Var::name_hint_ lookups
to const Var* pointer identity. This eliminates reliance on cosmetic
naming for semantic correctness.

Key changes:
- Add CanonicalVarTracker to confine name-based identity in SSA pass
- Replace string-keyed maps with pointer-keyed maps across all transforms
- Replace canonicalize_name() with pointer-based origin tracing
- Add known_names set for SSA name collision avoidance in outliner

Fixes hw-native-sys#616
Copilot AI review requested due to automatic review settings March 19, 2026 08:55
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c9035f0d-e954-49c1-8f3e-5ba1be351b0f

📥 Commits

Reviewing files that changed from the base of the PR and between cae48e6 and 0e15ee2.

📒 Files selected for processing (1)
  • include/pypto/ir/transforms/dependency_analyzer.h
✅ Files skipped from review due to trivial changes (1)
  • include/pypto/ir/transforms/dependency_analyzer.h

📝 Walkthrough

Walkthrough

Switches multiple IR transforms and outline utilities to use Var pointer identity (const Var*) instead of name-string keys; adds known_names tracking to outline utilities and updates SSA, dependency, expand-mixed-kernel, and outlining passes to operate on canonical Var pointers.

Changes

Cohort / File(s) Summary
Scope Outline Utils & Outline Passes
include/pypto/ir/transforms/utils/scope_outline_utils.h, src/ir/transforms/outline_cluster_scopes_pass.cpp, src/ir/transforms/outline_hierarchy_scopes_pass.cpp, src/ir/transforms/outline_incore_scopes_pass.cpp
Switched VarCollector and ScopeOutliner symbol tables from name-keyed maps to pointer-keyed (const Var*) maps; added known_names set and updated outline passes to populate symbol tables with var.get() and known names.
SSA Conversion Pass
src/ir/transforms/convert_to_ssa_pass.cpp
Introduced canonical Var-pointer tracking and reworked collectors, liveness, SSA versioning/state, phi/iter-arg handling, and expression substitution to use canonical const Var* keys instead of name strings; deterministic fallback uses UniqueId.
Dependency Analyzer
src/ir/transforms/dependency_analyzer.cpp, include/pypto/ir/transforms/dependency_analyzer.h
Replaced name-string keyed dependency maps with const Var* keyed maps; updated RAW/WAR/WAW edge construction and deduplication to use pointer identity; docs updated to state pointer-identity precondition.
Mixed Kernel Expansion
src/ir/transforms/expand_mixed_kernel_pass.cpp
Replaced affinity and mapping keyed by name_hint_ with pointer-keyed maps; removed name-canonicalization suffix logic and added an origin_map with recursive propagation across Assign/For/While/If/Seq to fix dangling output remaps.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • lyfne123

Poem

🐰 I hopped from names to pointers bright,
Each Var now holds its truest light.
Through SSA, scopes, and dependency tracks,
No string can trick my little snacks—
Hooray for identity, hop-hop, delight! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 32.79% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'refactor(ir): use pointer identity instead of name_hint_ in IR transforms' accurately captures the main change: migrating from name-based to pointer-based variable identity in IR transforms.
Description check ✅ Passed The description clearly summarizes the migration from name_hint_ to const Var* pointer identity, mentions testing (2558 unit tests pass), and links to issue #616.
Linked Issues check ✅ Passed All code changes align with issue #616 objectives: replace name_hint_-keyed state with pointer-keyed maps across ConvertToSSA [SSA], dependency_analyzer, scope_outline_utils, and expand_mixed_kernel_pass.
Out of Scope Changes check ✅ Passed All changes are in-scope: they address issue #616 by replacing string-keyed maps with pointer-keyed structures and refactoring IR transforms to use const Var* identity.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@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 implements a significant refactoring of how variables are identified and tracked within the Intermediate Representation (IR) transformation passes. The core change shifts from using potentially ambiguous string-based name hints to unique pointer identity for Var objects. This enhances the correctness and reliability of IR analyses and transformations, particularly in the context of Single Static Assignment (SSA) conversion, by eliminating issues arising from name collisions or transformations that alter variable names. The introduction of a CanonicalVarTracker and widespread updates to data structures ensure a consistent and robust approach to variable management throughout the IR pipeline.

Highlights

  • Variable Identity Refactoring: Migrated the IR transformation passes from relying on Var::name_hint_ (a cosmetic field) for variable identity to using const Var* pointer identity, ensuring more robust and unambiguous variable tracking, especially after SSA conversion.
  • CanonicalVarTracker Introduction: Introduced a new CanonicalVarTracker class within the SSA converter to centralize and confine all name-based identity logic. This class maps variable names to a canonical Var* representative, allowing all other SSA logic to operate using pointer identity.
  • Map Type Updates Across Transforms: Refactored numerous std::unordered_map and std::set instances across various IR transform files (e.g., scope_outline_utils, dependency_analyzer, convert_to_ssa_pass, expand_mixed_kernel_pass) to use const Var* as keys instead of std::string (name hints), aligning with the new pointer-based identity approach.
  • Structural Origin Mapping in Mixed Kernel Expansion: Replaced the string-suffix-stripping canonicalize_name() logic in expand_mixed_kernel_pass with a structural origin_map. This map traces each Var back to its originating function parameter using pointer identity, improving the correctness of remapping undefined SSA versions of output parameters.
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

The pull request refactors the IR transformation logic to primarily use const Var* pointer identity instead of Var::name_hint_ for variable identification. This significantly enhances the robustness and correctness of the transformations by moving away from fragile string-based comparisons. Key improvements include the introduction of CanonicalVarTracker to centralize canonical variable identity, widespread adoption of const Var* as map keys, and a more robust origin_map for tracing variable derivations in expand_mixed_kernel_pass. The changes are well-aligned with the goal of improving IR stability and correctness.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/ir/transforms/expand_mixed_kernel_pass.cpp (1)

1170-1182: Consider propagating YieldStmt values to return_vars for completeness.

The current implementation propagates return_var[i] → origin of iter_arg[i] (lines 1177-1182), which is correct for loop-carried state. However, the actual carried value comes from the YieldStmt at the end of the loop body, not directly from the iter_arg.

This is likely fine in practice because:

  1. The iter_arg and its yielded value typically share the same origin parameter.
  2. The goal is just to find an Out/InOut parameter to remap dangling references.

If edge cases arise where a yield value has a different origin than its iter_arg, you may need to also trace through YieldStmt values. For now, this appears sufficient.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/ir/transforms/expand_mixed_kernel_pass.cpp` around lines 1170 - 1182, The
loop currently maps return_vars_ to the origin of iter_args_ (in the ForStmt
handling) but doesn't account for YieldStmt values which are the actual carried
results; update the ForStmt branch in expand_mixed_kernel_pass.cpp so that after
walking origins for the loop body (walk_origins(FlattenBody(for_stmt->body_)))
you also scan the body for any YieldStmt nodes, find their yielded expressions
and propagate their origins into origin_map for the corresponding return_vars_
(fall back to iter_args_ origin only if a YieldStmt value origin is missing);
use the existing helper propagate_from_expr / origin_map lookup logic and
reference ForStmt::iter_args_, ForStmt::return_vars_, YieldStmt and FlattenBody
to locate and map the correct origins.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/ir/transforms/expand_mixed_kernel_pass.cpp`:
- Around line 1170-1182: The loop currently maps return_vars_ to the origin of
iter_args_ (in the ForStmt handling) but doesn't account for YieldStmt values
which are the actual carried results; update the ForStmt branch in
expand_mixed_kernel_pass.cpp so that after walking origins for the loop body
(walk_origins(FlattenBody(for_stmt->body_))) you also scan the body for any
YieldStmt nodes, find their yielded expressions and propagate their origins into
origin_map for the corresponding return_vars_ (fall back to iter_args_ origin
only if a YieldStmt value origin is missing); use the existing helper
propagate_from_expr / origin_map lookup logic and reference ForStmt::iter_args_,
ForStmt::return_vars_, YieldStmt and FlattenBody to locate and map the correct
origins.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 241922cc-6ced-4abe-9723-81e78f924a2e

📥 Commits

Reviewing files that changed from the base of the PR and between 95925ed and cae48e6.

📒 Files selected for processing (7)
  • include/pypto/ir/transforms/utils/scope_outline_utils.h
  • src/ir/transforms/convert_to_ssa_pass.cpp
  • src/ir/transforms/dependency_analyzer.cpp
  • src/ir/transforms/expand_mixed_kernel_pass.cpp
  • src/ir/transforms/outline_cluster_scopes_pass.cpp
  • src/ir/transforms/outline_hierarchy_scopes_pass.cpp
  • src/ir/transforms/outline_incore_scopes_pass.cpp

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors several IR transforms to stop using Var::name_hint_ (a cosmetic IgnoreField) as semantic variable identity, and instead rely on stable pointer identity (const Var*) for maps, substitution, and dependency tracking.

Changes:

  • Refactor outlining passes/utilities to build symbol tables keyed by const Var* and track used names separately for fresh-name generation.
  • Update ConvertToSSA to centralize all name-based “same variable” logic in a CanonicalVarTracker, with the rest of the SSA conversion using canonical pointer keys.
  • Replace expand_mixed_kernel_pass name-canonicalization with a pointer-based origin map to remap dangling output references.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ir/transforms/outline_incore_scopes_pass.cpp Switch outlining symbol table seeding to pointer-keyed tables and pass known-name set into outliner.
src/ir/transforms/outline_hierarchy_scopes_pass.cpp Same as above for hierarchy scope outlining.
src/ir/transforms/outline_cluster_scopes_pass.cpp Same as above for cluster scope outlining.
src/ir/transforms/expand_mixed_kernel_pass.cpp Key affinity tracking by const Var* and replace name-suffix canonicalization with a pointer-based origin propagation map.
src/ir/transforms/dependency_analyzer.cpp Switch dependency tracking maps from string keys to const Var* pointer identity.
include/pypto/ir/transforms/utils/scope_outline_utils.h Refactor symbol table collection/outlining helpers to be pointer-keyed and introduce known_names_ for collision-free renaming.

Add precondition comment to DependencyAnalyzer header noting that
input IR must have unique Var pointers per definition (SSA form),
since variable identity is now determined by pointer identity.

Addresses review comment on hw-native-sys#624
@lyfne123 lyfne123 merged commit 8bb464a into hw-native-sys:main Mar 19, 2026
7 checks passed
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.

[Pass Bug] IR transforms should not use name_hint_ as variable identity

3 participants