Skip to content

fix(codegen): propagate tensor view across plain tensor aliases#1606

Merged
lyfne123 merged 2 commits into
hw-native-sys:mainfrom
Hzfengsy:fix/pto-codegen-tensor-alias-view
May 31, 2026
Merged

fix(codegen): propagate tensor view across plain tensor aliases#1606
lyfne123 merged 2 commits into
hw-native-sys:mainfrom
Hzfengsy:fix/pto-codegen-tensor-alias-view

Conversation

@Hzfengsy
Copy link
Copy Markdown
Member

Summary

PTO codegen registers loop-result tensor views only inside the ForStmt visitor. When a constant-trip stage-2 pl.pipeline's statically-empty main loop is folded by Simplify into a plain alias t__rv = t__iter, that registration is bypassed and the AssignStmt visitor propagated nothing for tensor aliases. A later tile.store into the alias then tripped GetOrCreateTensorView's INTERNAL_CHECK with an opaque Tensor view not found for parameter: <synthetic-var>, giving no hint at the real cause.

This adds a branch to PTOCodegen::VisitStmt_(AssignStmtPtr) that, for a plain lhs_tensor = rhs_var alias, propagates the tensor view / SSA name / base ptr from RHS to LHS — mirroring the ForStmt loop-result registration the fold bypasses. Resolution uses the non-fatal TryGetTensorView and falls through on a view miss, so the change is strictly additive (never introduces a new throw).

Testing

  • Two regression tests in tests/ut/codegen/test_pto_codegen.py covering the plain-Var and IterArg RHS legs (the latter is the exact post-fold t__rv = t__iter shape). Both fail without the fix with the documented "Tensor view not found" error and pass with it.
  • Full tests/ut/ suite: 5382 passed, 28 skipped.
  • clang-tidy / clang-format / cpplint / pyright clean.

Notes

  • Internal C++ codegen only — no public API, bindings, or type-stub changes.
  • The non-fatal TryGetTensorView helper already exists upstream; this change only adds a new caller.

Copilot AI review requested due to automatic review settings May 30, 2026 09:22
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d76d47a7-b609-4b69-9ecd-fbe63ade9742

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR extends PTOCodegen's assignment visitor to propagate tensor views through plain alias assignments, allowing downstream tile.store operations to resolve and lower without explicit tensor-view creation. Tests validate view propagation for variable and loop iter_arg sources.

Changes

Tensor Alias View Propagation

Layer / File(s) Summary
Tensor alias view propagation and documentation
include/pypto/codegen/pto/pto_codegen.h, src/codegen/pto/pto_codegen.cpp
TryGetTensorView docstring clarified; VisitStmt_(AssignStmtPtr) adds a fast-path that reuses an existing RHS tensor view by binding it to the LHS tensor, enabling alias resolution without forcing a new tensor-view creation or failing.
Alias view propagation test cases
tests/ut/codegen/test_pto_codegen.py
Two regression tests verify that plain tensor aliases from variables and loop iter_args correctly propagate their underlying tensor views through the assignment, enabling tile.store to lower to pto.tstore with partition views generated from the resolved view.

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • hw-native-sys/pypto#347: Both PRs modify PTOCodegen's MLIR codegen in VisitStmt_(AssignStmtPtr) to handle tensor/view aliasing by remapping/reusing the underlying tile buffers and views, so the changes overlap at the alias-to-buffer propagation layer.

Suggested labels

bug

Poem

🐰 A tensor takes a simple alias form,
no make_view call to keep it warm,
yet through assignment we now bind the sight,
so tile.store finds the view just right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% 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 clearly and specifically summarizes the main change: propagating tensor views across plain tensor aliases in the PTO codegen fix.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem, solution, testing, and scope of changes across all three modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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 implements support for plain tensor aliases in the PTO codegen. When a loop-result ForStmt is folded into a plain AssignStmt (e.g., lhs_tensor = rhs_var), the AssignStmt visitor now propagates the tensor view, SSA name, and base pointer from the RHS to the LHS. This prevents downstream failures during subsequent tile.store operations. Two regression tests have been added to verify this behavior for both plain variable and loop IterArg RHS aliases. There are no review comments, so no feedback is provided.

Copy link
Copy Markdown
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 fixes a PTO codegen gap where AssignStmt did not propagate tensor-view metadata across plain tensor aliases (lhs_tensor = rhs_var). That gap shows up when an empty loop-result ForStmt is simplified into a plain alias (notably after certain constant-trip pl.pipeline folds), causing later tile.store codegen to fail because the alias has no registered tensor view.

Changes:

  • Extend PTOCodegen::VisitStmt_(AssignStmtPtr) to detect plain tensor aliases and bind tensor view / SSA name / base ptr from RHS to LHS (using non-fatal TryGetTensorView).
  • Add two regression tests covering the plain-Var RHS case and the IterArg RHS case (loop-carried tensor alias).
  • Update the TryGetTensorView header comment to include the new alias-propagation use case.

Reviewed changes

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

File Description
src/codegen/pto/pto_codegen.cpp Adds alias-detection logic in AssignStmt visitor to propagate tensor-view bindings for later consumers (e.g., tile.store).
include/pypto/codegen/pto/pto_codegen.h Expands TryGetTensorView doc comment to cover alias propagation as a non-fatal lookup use case.
tests/ut/codegen/test_pto_codegen.py Adds regression tests ensuring tile.store resolves tensor views when its output tensor is a plain alias (Var RHS and IterArg RHS).

Comment thread src/codegen/pto/pto_codegen.cpp
PTO codegen registers loop-result tensor views only inside the ForStmt
visitor. When a constant-trip stage-2 pl.pipeline's statically-empty
main loop is folded by Simplify into a plain alias `t__rv = t__iter`,
that registration is bypassed and the AssignStmt visitor propagated
nothing for tensor aliases. A later tile.store into the alias then
tripped GetOrCreateTensorView's INTERNAL_CHECK ("Tensor view not found
for parameter: <name>"), an opaque failure pointing at a synthetic var.

Propagate the tensor view / SSA name / base ptr from RHS to LHS for a
plain `lhs_tensor = rhs_var` alias, mirroring the ForStmt loop-result
registration. Resolution uses the non-fatal TryGetTensorView and falls
through on a view miss, so the change is strictly additive (never
introduces a new throw).

Add two regression tests covering the plain-Var and IterArg RHS legs;
both fail without the fix with the exact "Tensor view not found" error.
@Hzfengsy Hzfengsy force-pushed the fix/pto-codegen-tensor-alias-view branch from a7583f3 to e7cf6fe Compare May 31, 2026 01:13
VisitStmt_(ForStmtPtr) registers the loop-result tensor view / SSA name
for its return var but does not call RegisterBasePtr; the prior comment
overstated it. Reword to match, and note that this branch additionally
propagates the base-ptr mapping for element-wise alias consumers
(pl.read / pl.write / store_scalar), as VisitStmt_(IfStmtPtr) does for
merged tensors. Comment-only; no behavior change.

Addresses review feedback on hw-native-sys#1606.
@lyfne123 lyfne123 merged commit d5b3536 into hw-native-sys:main May 31, 2026
10 checks passed
@Hzfengsy Hzfengsy deleted the fix/pto-codegen-tensor-alias-view branch June 1, 2026 07:55
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.

3 participants