Skip to content

feat(ir): Accept pad_value kwarg on tile.slice#1129

Merged
lyfne123 merged 2 commits intohw-native-sys:mainfrom
Hzfengsy:worktree-fillpad
Apr 23, 2026
Merged

feat(ir): Accept pad_value kwarg on tile.slice#1129
lyfne123 merged 2 commits intohw-native-sys:mainfrom
Hzfengsy:worktree-fillpad

Conversation

@Hzfengsy
Copy link
Copy Markdown
Member

Summary

  • Extends tile.slice with an optional pad_value kwarg mirroring tile.fillpad. The value is written into TileView::pad on the output type.
  • Python DSL wrapper pl.tile.slice gains a pad_value: PadValue | None = None parameter and emits a UserWarning when pad_value is set without a valid_shape, since pad only takes effect when valid_shape < shape.
  • Python IR-layer ir.op.tile.slice forwards the new kwarg; existing callers unaffected (default PadValue.null → identical output TileType).
  • Adds 5 new unit tests in tests/ut/ir/operators/test_tile_ops.py plus a kwarg-schema check in test_op_registry.py.

This is the first of three PRs unifying slice+fillpad semantics. It is purely additive — tile.fillpad remains a first-class op, and the optimization pass that fuses slice → fillpad lives in a separate follow-up PR.

Testing

  • Targeted: tests/ut/ir/operators/test_tile_ops.py::TestTileSliceReshapeOps — 18/18 pass
  • Targeted: tests/ut/ir/operators/test_op_registry.py — 112/112 pass
  • Regression sentinels (test_legalize_pto_buffer_reuse.py, test_memory_reuse.py, test_pto_codegen_slice_fillpad_partial_dynamic_valid_shape) — all pass
  • Full tests/ut/ suite: 3909 passed, 16 skipped (unchanged baseline)
  • Pre-commit hooks: clang-format, clang-tidy line-filter clean on modified lines, cpplint, ruff check+format, pyright — all pass

Notes for reviewers

  • Validation block style in DeduceTileSliceType uses the exact template mirrored across the planned 3-PR series (typeid check + enum-range CHECK), so future tensor.slice and the fuse pass will share the pattern verbatim.
  • Backward compatibility: default pad_value = PadValue::null → existing slice(...) calls produce identical output TileType as before; no existing test required modification.
  • The bad-kwarg test (test_tile_slice_rejects_bad_pad_value) asserts TypeError from OpRegistry::ValidateKwargs, which fires before the deducer-level CHECK. The typeid CHECK in the deducer is retained as defense-in-depth and to match the shared validation template.

Extends tile.slice with an optional pad_value kwarg mirroring tile.fillpad.
The value is written into TileView::pad on the output type. DSL wrapper
warns when pad_value is set without a valid_shape, since pad only takes
effect when valid_shape < shape.

This is the first of three PRs unifying slice+fillpad semantics. It is
purely additive: default pad_value is null, so existing slice calls
produce identical output TileTypes.
Copilot AI review requested due to automatic review settings April 22, 2026 11:37
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

📝 Walkthrough

Walkthrough

This pull request introduces a new optional pad_value parameter to the tile.slice operation across Python bindings, C++ IR implementation, and tests. The parameter supports PadValue enums and numeric literals (0, infinity, negative infinity) that normalize to padding behaviors, with validation enforcing compatibility when valid_shape is specified.

Changes

Cohort / File(s) Summary
Python DSL Layer
python/pypto/language/op/tile_ops.py
Added pad_value parameter to slice() function signature with docstring documentation. Included runtime warning when pad_value is specified without valid_shape, and forwarding of pad_value to underlying IR binding via _ir_ops.slice(). Added import warnings for warning support.
Python IR Binding
python/pypto/ir/op/tile_ops.py
Extended slice() function signature with pad_value: PadValue | int | float | None parameter. Updated IR call construction to conditionally forward pad_value kwarg after normalization, treating PadValue.null as pass-through and normalizing other values.
C++ IR Implementation
src/ir/op/tile_ops/transform.cpp
Modified DeduceTileSliceType() to read optional pad_value from kwargs with validation of PadValue enum values (null, zero, max, min). Updated REGISTER_OP("tile.slice") with .set_attr<PadValue>("pad_value") attribute declaration.
Test Coverage
tests/ut/ir/operators/test_op_registry.py, tests/ut/ir/operators/test_tile_ops.py
Added schema validation test for pad_value kwarg presence. Introduced comprehensive functional tests covering pad_value propagation, numeric sugar mapping (0 → zero, infmax, -infmin), default behavior, type validation, and DSL-level warning emission.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • PR #1122: Introduces normalize_pad_value helper and PadValue literal sugar handling that this PR directly applies to tile.slice parameter normalization.
  • PR #603: Implements TilePadPadValue rename and establishes op-attribute/serialization infrastructure for pad_value in tile operations.
  • PR #591: Modifies the same tile.slice operator implementation (IR registration and type-deduction logic) for adding the valid_shape parameter, now extended with pad_value support.

Suggested reviewers

  • lyfne123

Poem

🐰 A slice with padding, oh what delight!
Zero, max, min—choose what feels right,
From Python down to C++ so deep,
The pad_value hops where valid shapes leap,
Sugar literals make the magic complete!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(ir): Accept pad_value kwarg on tile.slice' clearly summarizes the main change: adding a new optional pad_value keyword argument to the tile.slice operation.
Description check ✅ Passed The description is detailed and directly related to the changeset, explaining the pad_value kwarg addition, Python DSL changes, testing, and backward compatibility considerations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

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

Adds an optional pad_value kwarg to tile.slice across the C++ IR op registry/type-deducer and both Python IR + DSL wrappers, enabling slice to carry padding semantics via TileView::pad (aligned with tile.fillpad behavior).

Changes:

  • Extend C++ tile.slice op registration to accept pad_value and plumb it into DeduceTileSliceType by setting tile_view.pad.
  • Add pad_value to Python DSL pl.tile.slice with a UserWarning when used without valid_shape.
  • Add Python IR-layer forwarding for ir.op.tile.slice(..., pad_value=...) and add unit tests + kwarg-schema coverage.

Reviewed changes

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

Show a summary per file
File Description
src/ir/op/tile_ops/transform.cpp Adds pad_value kwarg schema + sets TileView::pad during type deduction for tile.slice.
python/pypto/language/op/tile_ops.py Exposes pad_value on pl.tile.slice and emits a warning when it won’t take effect.
python/pypto/ir/op/tile_ops.py Forwards pad_value into create_op_call kwargs when provided.
tests/ut/ir/operators/test_tile_ops.py Adds unit tests covering pad_value propagation, defaulting, bad-type rejection, and DSL warning behavior.
tests/ut/ir/operators/test_op_registry.py Adds a schema test ensuring tile.slice declares pad_value as an allowed kwarg.

Comment thread python/pypto/language/op/tile_ops.py Outdated
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 introduces a pad_value parameter to the tile.slice operation across the IR, language, and C++ layers, allowing for padding of out-of-bounds elements. It also includes corresponding unit tests and attribute registration. The review feedback suggests enhancing the implementation by supporting numeric literals for pad_value and using normalize_pad_value for consistency with other operations like tile.fillpad.

Comment thread python/pypto/ir/op/tile_ops.py Outdated
Comment thread python/pypto/ir/op/tile_ops.py Outdated
Comment thread python/pypto/ir/op/tile_ops.py Outdated
Comment thread python/pypto/language/op/tile_ops.py Outdated
…e-sys#1129

Mirror the numeric-literal sugar that main's newly-landed tile.fillpad
accepts, by routing non-null pad_value through normalize_pad_value:

- Both the DSL wrapper (pl.tile.slice) and the IR-layer wrapper
  (ir.op.tile.slice) now accept PadValue | int | float | None.
- Non-null values are validated at the Python boundary via
  normalize_pad_value; PadValue.null is passed through unchanged
  (slice treats it as "no padding", unlike fillpad which rejects it).
- Docstrings now document None / PadValue.null as the no-op default
  and the 0 / math.inf / -math.inf literal sugar.
- New tests: numeric-sugar round-trip, Python-boundary rejection of
  non-sugar numeric values.

Addresses gemini-code-assist and copilot review comments on hw-native-sys#1129.
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.

Caution

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

⚠️ Outside diff range comments (1)
python/pypto/ir/op/tile_ops.py (1)

1917-1923: ⚠️ Potential issue | 🟠 Major

Preserve positional span compatibility.

Line 1922 inserts pad_value before the existing span parameter, so old calls like slice(tile, shape, offset, valid_shape, span) now treat span as pad_value and fail normalization. Since this is a kwarg feature, keep span in its old positional slot and make pad_value keyword-only.

🐛 Proposed fix
 def slice(
     tile: Expr,
     shape: Sequence[int | Expr] | _ir_core.MakeTuple,
     offset: Sequence[int | Expr] | _ir_core.MakeTuple,
     valid_shape: Sequence[int | Expr] | _ir_core.MakeTuple | None = None,
-    pad_value: PadValue | int | float | None = None,
     span: Span | None = None,
+    *,
+    pad_value: PadValue | int | float | None = None,
 ) -> Call:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@python/pypto/ir/op/tile_ops.py` around lines 1917 - 1923, The new parameter
ordering broke positional compatibility for slice(...) by inserting pad_value
before span; to restore compatibility, keep span as the last positional
parameter in the slice function signature and make pad_value keyword-only by
inserting a bare * before pad_value (i.e., leave parameters up through span
unchanged and place a * so pad_value can only be passed by name). Update the
slice function declaration and any related docstrings/annotations to reflect
pad_value as keyword-only while leaving span in its original positional slot.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@python/pypto/ir/op/tile_ops.py`:
- Around line 1917-1923: The new parameter ordering broke positional
compatibility for slice(...) by inserting pad_value before span; to restore
compatibility, keep span as the last positional parameter in the slice function
signature and make pad_value keyword-only by inserting a bare * before pad_value
(i.e., leave parameters up through span unchanged and place a * so pad_value can
only be passed by name). Update the slice function declaration and any related
docstrings/annotations to reflect pad_value as keyword-only while leaving span
in its original positional slot.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 67ad3d24-01ce-4be0-b981-a1be036edcf7

📥 Commits

Reviewing files that changed from the base of the PR and between b7e6a82 and c96407b.

📒 Files selected for processing (5)
  • python/pypto/ir/op/tile_ops.py
  • python/pypto/language/op/tile_ops.py
  • src/ir/op/tile_ops/transform.cpp
  • tests/ut/ir/operators/test_op_registry.py
  • tests/ut/ir/operators/test_tile_ops.py

@lyfne123 lyfne123 merged commit 4cbc096 into hw-native-sys:main Apr 23, 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.

3 participants