Skip to content

fix(codegen): Infer static v_row/v_col when source valid >= slice size#1303

Merged
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
zhusy54:valid-shape
May 9, 2026
Merged

fix(codegen): Infer static v_row/v_col when source valid >= slice size#1303
Hzfengsy merged 1 commit intohw-native-sys:mainfrom
zhusy54:valid-shape

Conversation

@zhusy54
Copy link
Copy Markdown
Contributor

@zhusy54 zhusy54 commented May 7, 2026

Summary

  • Adds a third inference case in InferSubviewTileTypeComponents (src/backend/common/pto_ops_common.cpp): when the source tile's valid extent in a dimension is a static constant >= slice size, the sliced valid extent is statically known as size regardless of the (possibly dynamic) offset.
  • Fixes a missing return; after the zero-offset equivalence case (Case 2), which would have fallen through into the new case.

Background

pto.subview with a dynamic loop offset (e.g. k0 = i * K) was emitting dynamic v_row/v_col even when the source tile is large enough that any valid offset leaves exactly size valid elements. This caused PTOAS to reject otherwise-valid programs. The new inference case handles this: if source_valid >= slice_size (both static constants), the subview valid extent is trivially size.

Testing

  • All 4419 unit tests pass
  • Clang-tidy clean
  • Code review completed

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 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
📝 Walkthrough

Walkthrough

This PR refines static-dimension inference in InferSubviewTileTypeComponents by adding two early-return branches that mark subview dimensions statically known when (1) offset==0 and requested size equals source valid extent, or (2) source valid constant ≥ requested size.

Changes

Subview Dimension Static Inference Enhancement

Layer / File(s) Summary
Core Logic Enhancement
src/backend/common/pto_ops_common.cpp
InferSubviewTileTypeComponents adds two explicit static-case shortcuts: (1) when offset is 0 and requested size matches source valid extent, and (2) when source valid constant ≥ requested size; both set the dimension as statically known and return early before the existing dynamic inference.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • lyfne123

Poem

🐰 I nibble at offsets, I count every size,
Static or dynamic — I open my eyes.
When zero meets equal, I stamp it as known,
When valid is plenty, the answer is shown.
Hooray for neat inference — a hop and a tone!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 describes the main change: adding static inference for v_row/v_col when source valid extent is greater than or equal to slice size.
Description check ✅ Passed The description is well-related to the changeset, explaining the summary of changes, background context, and testing results.
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.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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 updates the InferSubviewTileTypeComponents function in src/backend/common/pto_ops_common.cpp to improve the static inference of tile type components. Specifically, it adds logic to identify cases where the number of valid elements is statically known because the source's valid extent is greater than or equal to the requested size, independent of the offset. It also adds a missing return statement and clarifying comments to the existing logic. There were no review comments provided, so I have no feedback to provide.

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.

🧹 Nitpick comments (1)
src/backend/common/pto_ops_common.cpp (1)

249-255: ⚡ Quick win

Document (or assert) the bounds invariant that makes Case 3 sound.

Case 3 concludes v_row/v_col = size (static) whenever valid_const->value_ >= size, even though the offset may be a dynamic runtime value. This inference is only correct under the implicit invariant:

offset + size <= valid_const->value_

If that invariant does not hold (e.g., offset = valid - size + 1), then the true remaining valid extent is min(size, valid - offset) < size, but the emitted tile-buf type would claim size. PTOAS would then accept the program with an incorrect type annotation rather than flagging the out-of-bounds access.

The comment currently says "Any valid offset leaves exactly size valid elements" — but "valid offset" is doing all the work; the word "valid" should be unpacked explicitly.

💡 Suggested improvement: make the invariant explicit
-    // Any valid offset leaves exactly `size` valid elements in this dimension,
-    // so v_row/v_col is statically known regardless of the offset value.
-    if (valid_const && valid_const->value_ >= size) {
+    // When the source valid extent is statically >= size, any in-bounds offset
+    // (i.e., offset + size <= valid_const->value_, guaranteed by IR well-formedness)
+    // leaves exactly `size` valid elements. v_row/v_col is therefore statically known.
+    DCHECK(!offset_const)  // dynamic-offset path; static-offset already handled by Case 1
+        << "InferSubviewTileTypeComponents: Case 3 should not be reached with a constant offset";
+    if (valid_const && valid_const->value_ >= size) {

Alternatively, add a DCHECK or INTERNAL_CHECK that offset_const == nullptr here (since a constant offset with a constant valid would have been caught by Case 1), and add a brief note that the caller is responsible for ensuring the subview is in-bounds.

🤖 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 `@src/backend/common/pto_ops_common.cpp` around lines 249 - 255, The code
assumes that when valid_const->value_ >= size it is safe to mark *out_value =
size and *out_dynamic = false; make this invariant explicit or assert it: add a
comment stating the required precondition "offset + size <= valid_const->value_"
(or that the caller guarantees a valid subview) and/or add a runtime
DCHECK/INTERNAL_CHECK on offset_const (e.g., ensure offset_const == nullptr or
verify the numeric invariant when offset is available) so Case 3 in the logic
around valid_const, offset_const, size, out_value, out_dynamic cannot emit an
over-sized tile-buf type accepted by PTOAS.
🤖 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.

Nitpick comments:
In `@src/backend/common/pto_ops_common.cpp`:
- Around line 249-255: The code assumes that when valid_const->value_ >= size it
is safe to mark *out_value = size and *out_dynamic = false; make this invariant
explicit or assert it: add a comment stating the required precondition "offset +
size <= valid_const->value_" (or that the caller guarantees a valid subview)
and/or add a runtime DCHECK/INTERNAL_CHECK on offset_const (e.g., ensure
offset_const == nullptr or verify the numeric invariant when offset is
available) so Case 3 in the logic around valid_const, offset_const, size,
out_value, out_dynamic cannot emit an over-sized tile-buf type accepted by
PTOAS.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a149d8a4-39c5-4d57-9035-a59f1ff23855

📥 Commits

Reviewing files that changed from the base of the PR and between f228bf4 and fb73ef9.

📒 Files selected for processing (1)
  • src/backend/common/pto_ops_common.cpp

@zhusy54 zhusy54 marked this pull request as draft May 7, 2026 10:31
@zhusy54 zhusy54 marked this pull request as ready for review May 7, 2026 12:29
@zhusy54 zhusy54 marked this pull request as draft May 7, 2026 12:29
When the source tile's valid extent in a dimension is a static constant
that is >= the requested slice size, the sliced valid extent is always
exactly `size` regardless of the (possibly dynamic) offset. Add this
as a third inference case in InferSubviewTileTypeComponents.

Also fixes a missing return after the zero-offset equivalence case,
which would fall through into the new case without it.
@zhusy54 zhusy54 marked this pull request as ready for review May 9, 2026 08:20
@Hzfengsy Hzfengsy merged commit 92c12c1 into hw-native-sys:main May 9, 2026
9 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.

2 participants