Skip to content

fix(pass): preserve dynamic-shape Var pointer identity in InitMemRef#979

Merged
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
huxinyuan1215:fix/970-parser-dynvar-reuse
Apr 12, 2026
Merged

fix(pass): preserve dynamic-shape Var pointer identity in InitMemRef#979
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
huxinyuan1215:fix/970-parser-dynvar-reuse

Conversation

@huxinyuan1215
Copy link
Copy Markdown
Contributor

Summary

Closes #970

InitMemRef::ProcessNormalVar was creating new Var objects for non-ShapedType variables (e.g. ScalarType(INDEX) for dynamic dimensions like M, N) even though they don't need MemRef initialization. This fragmented a single shared M pointer into 4 distinct pointers across param types, return types, and body — breaking structural_equal bijection during roundtrip checks.

Fix: return the original Var for non-ShapedType variables. This also removes the last Variable pointer mismatch suppression from RoundtripInstrument — all roundtrip checks now run with zero suppressions.

Test plan

  • All 3419 unit tests pass
  • Verified M pointer count stays at 1 through entire pass pipeline (was 4 after InitMemRef before fix)

…w-native-sys#970)

InitMemRef's ProcessNormalVar was creating new Var objects for non-shaped
types (e.g. ScalarType INDEX for dynamic dimensions like M, N) even
though they don't need MemRef initialization. This fragmented Var
pointers: a single M shared across param types, return types, and body
became 4 distinct pointers after the pass.

Fix by returning the original Var for non-ShapedType variables, and
remove the last Variable pointer mismatch suppression from
RoundtripInstrument — all roundtrip checks now run with zero
suppressions.

Closes hw-native-sys#970
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 12, 2026

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: a0f7cb25-175e-4596-b981-c8c6e3b66679

📥 Commits

Reviewing files that changed from the base of the PR and between 53d9f93 and 14b69d0.

📒 Files selected for processing (2)
  • python/pypto/ir/instruments.py
  • src/ir/transforms/init_memref.cpp
💤 Files with no reviewable changes (1)
  • python/pypto/ir/instruments.py

📝 Walkthrough

Walkthrough

Fixes a bug where the InitMemRef pass was fragmenting dynamic-shape Var pointers by creating new instances instead of preserving the original pointer identity. Returns the original Var for non-ShapedType variables in ProcessNormalVar() and removes the downstream suppression of "Variable pointer mismatch" errors in the RoundtripInstrument.

Changes

Cohort / File(s) Summary
InitMemRef Var Pointer Preservation
src/ir/transforms/init_memref.cpp
Added explicit handling to return the original Var pointer for non-ShapedType variables instead of creating a new instance, preserving pointer identity across multiple type annotations.
RoundtripInstrument Error Suppression Removal
python/pypto/ir/instruments.py
Removed special-case string matching that silently suppressed "Variable pointer mismatch" structural-equality errors; now all exceptions are re-raised as RuntimeError with printed IR included.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • PR #871: Addresses InitMemRef handling of Var pointer identity for dynamic/valid-shape-related types by preventing creation of fresh Vars and preserving original references.
  • PR #460: Fixes the same pointer-identity problem for IR Vars used as dynamic/dimension variables through deduplication of DynVar→Var mappings to ensure shared pointer identity.

Suggested reviewers

  • lyfne123

Poem

🐰 A pointer once fragmented, now made whole,
Dynamic shapes no longer take their toll,
No more suppression of the mismatch cry,
One Var to rule them all—no alibi! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly and concisely describes the main fix: preserving Var pointer identity in InitMemRef for dynamic-shape variables, which directly matches the changeset.
Description check ✅ Passed Description relates to the changeset, explains the bug, the fix, and references the linked issue #970.
Linked Issues check ✅ Passed Code changes fully implement the objectives from issue #970: preserving Var pointer identity in InitMemRef and removing Variable pointer mismatch suppression from RoundtripInstrument.
Out of Scope Changes check ✅ Passed All changes directly address the requirements in issue #970; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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


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.

Copy link
Copy Markdown
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 the "Variable pointer mismatch" issue by ensuring that non-shaped types, such as those used for dynamic-shape dimensions, preserve their pointer identity during IR mutations in InitMemRefMutator. By returning the original Var instead of creating a new one for these types, structural equality checks no longer fail due to pointer bijections. Consequently, the previous workarounds and documentation regarding this limitation in the RoundtripInstrument have been removed. I have no feedback to provide.

@Hzfengsy Hzfengsy merged commit 5d39515 into hw-native-sys:main Apr 12, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Pass] InitMemRef fragments dynamic-shape Var pointers, breaking roundtrip structural equality

2 participants