Skip to content

fix(codegen): preserve static dims in !pto.tensor_view type#1541

Draft
zhangqi-chen wants to merge 2 commits into
hw-native-sys:mainfrom
zhangqi-chen:issue-1533-tensor-view-static-dims
Draft

fix(codegen): preserve static dims in !pto.tensor_view type#1541
zhangqi-chen wants to merge 2 commits into
hw-native-sys:mainfrom
zhangqi-chen:issue-1533-tensor-view-static-dims

Conversation

@zhangqi-chen
Copy link
Copy Markdown
Collaborator

Summary

Emit ConstInt shape values directly in the !pto.tensor_view<...> type
string instead of always writing ? for every dim.

ptoas needs the static shape on the type signature when the SSA producer
is an scf.if / scf.for phi — those phis carry no shape operands to
trace back to, so a fully dynamic type <?x?xf32> flows into ptoas as
memref<?x?xf32, strided<[?, ?], offset: ?>> and pto.partition_view is
rejected.

The four emission sites for !pto.tensor_view<...> are kept in lockstep
so scf.yield operand types stay equal to scf.if result types:

  • PTOCodegen::GetTensorViewTypeString (helper used by scf.if/scf.for results)
  • PTOCodegen::EmitMakeTensorViews (function-parameter views)
  • EmitCommRemoteView (distributed remote-load peer views)
  • tensor.as_layout (slice-assign rebind)

A pre-existing reference pattern in the pld.system.wait path already
handled this correctly and was used as the template.

Behavior

  • Static shape [64, 128]!pto.tensor_view<64x128xf32>
  • Symbolic shape [M, N]!pto.tensor_view<?x?xf32> (unchanged)
  • Mixed [64, M]!pto.tensor_view<64x?xf32>

Testing

  • python -m pytest tests/ut/codegen/ — 344 passed
  • python -m pytest tests/ut/ --ignore=tests/ut/codegen — 4892 passed
  • New regression test test_pto_codegen_tensor_view_type_preserves_static_dims covers static and mixed shape cases
  • Updated existing test assertions and English + Chinese doc snippets to match the new convention

Related Issues

Fixes #1533

Emit `ConstInt` shape values directly in the `!pto.tensor_view<...>` type
string instead of always writing `?` for every dim. ptoas needs the static
shape on the type signature when the SSA producer is an `scf.if` / `scf.for`
phi — those phis carry no shape operands to trace back to, so a fully
dynamic type `<?x?xf32>` flows into ptoas as
`memref<?x?xf32, strided<[?, ?], offset: ?>>` and `pto.partition_view` is
rejected. The four emission sites for `!pto.tensor_view<...>`
(`GetTensorViewTypeString`, `EmitMakeTensorViews`, `EmitCommRemoteView`,
`tensor.as_layout`) are kept in lockstep so `scf.yield` operand types stay
equal to `scf.if` result types.

Fixes hw-native-sys#1533
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 26, 2026

Review Change Stack

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: 465c3704-f89e-4f31-955e-b1f7d29816be

📥 Commits

Reviewing files that changed from the base of the PR and between cfca5da and 1b7a5ef.

📒 Files selected for processing (3)
  • docs/en/dev/codegen/00-pto_codegen.md
  • docs/zh-cn/dev/codegen/00-pto_codegen.md
  • src/backend/common/pto_ops_common.cpp
✅ Files skipped from review due to trivial changes (1)
  • docs/en/dev/codegen/00-pto_codegen.md

📝 Walkthrough

Walkthrough

PTOCodegen now emits !pto.tensor_view<...> types that include literal ConstInt extents when available, propagating concrete static dimensions through make_tensor_view, remote-view emission, and tensor.as_layout lowering; docs and tests updated to match.

Changes

Static Tensor Dimension Preservation

Layer / File(s) Summary
Tensor-view type string generation
src/codegen/pto/pto_codegen.cpp
GetTensorViewTypeString emits literal ConstInt extents and ? for dynamic dims; EmitMakeTensorViews uses it to produce consistent !pto.tensor_view<...> annotations.
Backend codegen integration
src/backend/common/pto_ops_common.cpp
EmitCommRemoteView and tensor.as_layout delegate to codegen.GetTensorViewTypeString(...) so emitted !pto.tensor_view<...> results preserve static dimensions instead of using all ? placeholders.
Documentation updates for tensor-view typing
docs/en/dev/codegen/00-pto_codegen.md, docs/zh-cn/dev/codegen/00-pto_codegen.md
Docs updated: "Tensor View Generation" text and examples (column vector, tile.load, tile.store, complete mul-kernel) now show concrete tensor-view typings (e.g., !pto.tensor_view<32x32xf32>) for ConstInt dims.
Test validation of static dimension preservation
tests/ut/codegen/test_dynamic_shape.py, tests/ut/codegen/test_pto_codegen.py
Updated expectation in test_add_kernel_valid_shape_pto_codegen to !pto.tensor_view<128x128xf32>; added test_pto_codegen_tensor_view_type_preserves_static_dims() to assert static dims are emitted as literals and symbolic dims remain ?.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • hw-native-sys/pypto#267: Both PRs modify PTOCodegen’s tensor view type-string generation (GetTensorViewTypeString / EmitMakeTensorViews).
  • hw-native-sys/pypto#1339: Related changes touching tensor.as_layout → pto.make_tensor_view codegen paths.
  • hw-native-sys/pypto#1217: Both touch pto.make_tensor_view emission, though #1217 focuses on index_cast emission for certain dynamic expressions.

Suggested labels

enhancement

Suggested reviewers

  • lyfne123
  • Hzfengsy

Poem

🐰 I hop through tensors, keen and spry,
I stamp the sizes that I spy.
No more questions where shapes should be,
ConstInts stand tall, wild dims stay free.
Hooray — the views remember me!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 36.36% 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 describes the main fix: preserving static dimensions in tensor view types instead of emitting wildcards.
Description check ✅ Passed The description explains the problem, solution, behavior changes, and testing approach with clear examples and references to the linked issue.
Linked Issues check ✅ Passed All code changes directly address issue #1533 by preserving static tensor dimensions in !pto.tensor_view types across four emission sites to prevent partition_view failures.
Out of Scope Changes check ✅ Passed All changes are directly related to the PR objective: updating codegen logic, documentation examples, and tests to preserve static dims in tensor view types.

✏️ 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 issue #1533 by preserving static dimension values (from ConstInt) in the !pto.tensor_view type signature, falling back to ? only for symbolic dimensions. This ensures downstream consumers can resolve shapes even when the SSA producer is an scf.if or scf.for phi. The documentation and unit tests have been updated to reflect this change. The reviewer feedback identifies incorrect dimensions in the documentation examples (using 32x32 instead of 16x1 for a [16, 1] column vector) and suggests refactoring the C++ backend code to reuse the centralized GetTensorViewTypeString helper method to avoid code duplication.

Comment thread docs/en/dev/codegen/00-pto_codegen.md Outdated
Comment thread docs/zh-cn/dev/codegen/00-pto_codegen.md Outdated
Comment thread src/backend/common/pto_ops_common.cpp Outdated
Comment thread src/backend/common/pto_ops_common.cpp Outdated
Copy link
Copy Markdown

@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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
docs/zh-cn/dev/codegen/00-pto_codegen.md (1)

243-250: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

修正列向量示例中的类型与 shape 不一致问题。

Line 249 写的是 !pto.tensor_view<32x32xf32>,但示例 shape 是 [16, 1];这里应改为 !pto.tensor_view<16x1xf32>(或与示例一致的符号形式)。

建议修改
-    : !pto.tensor_view<32x32xf32>
+    : !pto.tensor_view<16x1xf32>
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/zh-cn/dev/codegen/00-pto_codegen.md` around lines 243 - 250, The example
for the column vector is inconsistent: the pto.make_tensor_view call that
constructs %col_view uses shape = [%c16_index, %c1_index] but the type
annotation is still !pto.tensor_view<32x32xf32>; update the type to match the
shape (e.g., change !pto.tensor_view<32x32xf32> to !pto.tensor_view<16x1xf32> or
an equivalent symbol form) so %col_view, the pto.make_tensor_view invocation,
and the tensor_view type are consistent.
docs/en/dev/codegen/00-pto_codegen.md (1)

250-257: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix the column-vector example type to match the emitted shape.

Line 256 currently shows !pto.tensor_view<32x32xf32>, but the snippet shape is [16, 1]; this should be !pto.tensor_view<16x1xf32> (or a matching symbolic form) to avoid misleading readers.

Proposed doc fix
-    : !pto.tensor_view<32x32xf32>
+    : !pto.tensor_view<16x1xf32>
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/en/dev/codegen/00-pto_codegen.md` around lines 250 - 257, The documented
column-vector example uses pto.make_tensor_view producing a shape of [16, 1] for
%col_view but incorrectly annotates the type as !pto.tensor_view<32x32xf32>;
update the type annotation for %col_view (in the pto.make_tensor_view example)
to match the emitted shape, e.g. change the type to !pto.tensor_view<16x1xf32>
(or an equivalent symbolic form) so the shape and the !pto.tensor_view<> type
agree.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/backend/common/pto_ops_common.cpp`:
- Around line 2341-2350: The duplicated manual construction of the tensor view
type string (the loop that writes "x" + either ConstInt value or "?" into
view_type) should be replaced by a single call to the existing
GetTensorViewTypeString(...) helper; locate the hand-rolled logic in
pto_ops_common.cpp (the for-loop using As<ir::ConstInt>(shape[i]) that appends
ci->value_ or "?") and remove it, calling GetTensorViewTypeString(shape, rank)
(or the appropriate signature) instead; do the same for the other occurrence
around the 2985-2995 region so both sites use the central
GetTensorViewTypeString and avoid drift.

---

Outside diff comments:
In `@docs/en/dev/codegen/00-pto_codegen.md`:
- Around line 250-257: The documented column-vector example uses
pto.make_tensor_view producing a shape of [16, 1] for %col_view but incorrectly
annotates the type as !pto.tensor_view<32x32xf32>; update the type annotation
for %col_view (in the pto.make_tensor_view example) to match the emitted shape,
e.g. change the type to !pto.tensor_view<16x1xf32> (or an equivalent symbolic
form) so the shape and the !pto.tensor_view<> type agree.

In `@docs/zh-cn/dev/codegen/00-pto_codegen.md`:
- Around line 243-250: The example for the column vector is inconsistent: the
pto.make_tensor_view call that constructs %col_view uses shape = [%c16_index,
%c1_index] but the type annotation is still !pto.tensor_view<32x32xf32>; update
the type to match the shape (e.g., change !pto.tensor_view<32x32xf32> to
!pto.tensor_view<16x1xf32> or an equivalent symbol form) so %col_view, the
pto.make_tensor_view invocation, and the tensor_view type are consistent.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 98dd039f-5812-491f-bf6b-effc6317f5d2

📥 Commits

Reviewing files that changed from the base of the PR and between a73c056 and cfca5da.

📒 Files selected for processing (6)
  • docs/en/dev/codegen/00-pto_codegen.md
  • docs/zh-cn/dev/codegen/00-pto_codegen.md
  • src/backend/common/pto_ops_common.cpp
  • src/codegen/pto/pto_codegen.cpp
  • tests/ut/codegen/test_dynamic_shape.py
  • tests/ut/codegen/test_pto_codegen.py

Comment thread src/backend/common/pto_ops_common.cpp Outdated
- docs: restore the [16, 1] column-vector example type to
  `!pto.tensor_view<16x1xf32>` (the prior sed replacement over-broadly
  pinned it to 32x32, which contradicts the surrounding text)
- backend: reuse `PTOCodegen::GetTensorViewTypeString` in
  `EmitCommRemoteView` and the `tensor.as_layout` lambda instead of
  duplicating the static-vs-symbolic dim loop — keeps every emission
  site in lockstep with the helper
@zhangqi-chen zhangqi-chen marked this pull request as draft May 26, 2026 12:46
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 Bug] pto_backend emits pto.partition_view against SCF-phi tensor_view (loses static dims; ptoas rejects)

1 participant