Skip to content

Conversation

@LeiWang1999
Copy link
Member

@LeiWang1999 LeiWang1999 commented Nov 26, 2025

This commit refines the MakeIterVars method in CopyNode to select base ranges based on memory scope levels, ensuring that the chosen ranges are not smaller than the original source ranges. Additionally, it updates the Python copy function to clarify range handling, including broadcasting logic and extent alignment. These changes improve the robustness and clarity of the copy operation's implementation.

Summary by CodeRabbit

  • New Features

    • Direct-buffer lowering path for simple copy cases to improve generated code.
  • Improvements

    • More robust range and extent handling with right-to-left extent legalization and broadcasting support.
    • Per-dimension safety checks and two-pointer alignment to prevent invalid copy bounds.
    • Improved diagnostics and clearer error messages for reshape/copy mismatches.

✏️ Tip: You can customize this high-level summary in your review settings.

This commit refines the `MakeIterVars` method in `CopyNode` to select base ranges based on memory scope levels, ensuring that the chosen ranges are not smaller than the original source ranges. Additionally, it updates the Python `copy` function to clarify range handling, including broadcasting logic and extent alignment. These changes improve the robustness and clarity of the copy operation's implementation.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 26, 2025

Walkthrough

Refactors C++ copy lowering to choose loop ranges from the lower-level memory scope and adds per-dimension safety checks; updates Python copy lowering to handle missing extents as ones, legalize extents from the tail, add an early BufferLoad→BufferStore path, and lower copies via tl.region + tl.copy.

Changes

Cohort / File(s) Summary
C++ copy range & validation
src/op/copy.cc
Selects base loop ranges using lower-level memory scope via a new scope_level helper; introduces per-dimension safety checks using arith::Analyzer to ensure chosen base_ranges aren’t provably smaller than src_range; implements two-pointer skipping of unit extents; updates IterVar/loop generation to use base_ranges and logs diagnostics on failure.
Python copy lowering & extents
tilelang/language/copy.py
Treats missing extents as all-ones and legalizes pairwise extents from the tail; adds early lowering for BufferLoad operands without extents directly to BufferStore; converts operands to tl.region with coalesced_width/eviction_policy and emits tl.copy intrinsic; docstring/comments updated to reflect behavior.
Reshape error message
tilelang/language/customize.py
Expands and reformats the reshape assertion error message to include source/target shapes and dtypes; no control-flow or behavior change.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as CopyOp
    participant ScopeEval as Scope Evaluator
    participant RangeSelect as Range Selector
    participant Analyzer as arith::Analyzer
    participant LoopGen as Loop Generator

    Caller->>ScopeEval: Query scope_level(src), scope_level(dst)
    ScopeEval->>RangeSelect: Choose base_ranges from lower-level scope
    RangeSelect->>Analyzer: For each non-singleton dim, check base_range ≥ src_range
    alt Analyzer proves smaller
        Analyzer->>Caller: Emit fatal diagnostic / fail
    else OK
        RangeSelect->>LoopGen: Provide base_ranges
        LoopGen->>LoopGen: Create IterVars from base_ranges, skip unit extents, generate loops
    end
Loading
sequenceDiagram
    participant PyCaller as py.copy
    participant Extents as Extent Handler
    participant EarlyLower as BufferStore Lowerer
    participant RegionLower as Region Lowerer

    PyCaller->>Extents: Read src/dst extents (missing → all-ones)
    Extents->>Extents: Legalize pairwise extents from tail
    alt both operands are BufferLoad without extents
        Extents->>EarlyLower: Lower to direct BufferStore (dst[...] = src[...])
    else default
        Extents->>RegionLower: Convert src/dst → tl.region, set coalesced_width & eviction_policy
        RegionLower->>RegionLower: Emit tl.copy intrinsic
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

  • Pay special attention to:
    • Correctness of scope_level computation and selection condition between src/dst.
    • arith::Analyzer checks and the provably-smaller detection logic (edge cases with symbolic dims).
    • Two-pointer skipping logic for unit extents and alignment assumptions.
    • Early-lowering path in Python to ensure it doesn't bypass necessary extent/legalization for other cases.

Possibly related PRs

Poem

🐇 I hopped through scopes both near and far,
I counted ranges twixt buffer and bar.
With analyzers checking each tiny span,
I copy-safe hopped — the loops now ran.
Hooray — a bunny-approved, careful plan! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: enhancing CopyNode's IterVar creation with improved range handling based on memory scope levels and safety checks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b18b6f6 and 4867842.

📒 Files selected for processing (2)
  • src/op/copy.cc (1 hunks)
  • tilelang/language/customize.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tilelang/language/customize.py (1)
tilelang/utils/language.py (2)
  • prim_expr_equal (390-405)
  • bits_product (379-387)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Test for Python 3.12 with ROCm-6.3 (on self-hosted-amd)
  • GitHub Check: Test for Python 3.12 with Metal (on macos-latest)
  • GitHub Check: Test for Python 3.12 with CUDA-12.8 (on self-hosted-nvidia)
🔇 Additional comments (2)
tilelang/language/customize.py (1)

49-51: Richer reshape assertion diagnostics look good

The condition is unchanged and the expanded message (including src, shapes, and dtypes) will make debugging reshape/view mismatches much easier without affecting behavior.

src/op/copy.cc (1)

181-271: Scope-aware base range selection and per-dimension checks look correct

Picking base_ranges from the more local scope and then using the analyzer + two-pointer walk to (a) forbid base extents that are provably smaller than the corresponding non-1 src_range extents and (b) require any unmatched trailing dims to be pure singleton-broadcast (extent == 1) keeps MakeIterVars consistent with the assumptions in MakeIndices/MakePredicate while avoiding undersized loop domains. This should make loop construction more robust without changing semantics for well-formed copies.


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.

@github-actions
Copy link

👋 Hi! Thank you for contributing to the TileLang project.

Please remember to run pre-commit run --all-files in the root directory of the project to ensure your changes are properly linted and formatted. This will help ensure your contribution passes the format check.

We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
src/op/copy.cc (1)

854-856: Consider using DLOG or VLOG for this diagnostic message.

LOG(INFO) will print on every normal copy lowering, which could be noisy in production. If this is primarily for debugging, consider using DLOG(INFO) (debug-only) or VLOG(1) (verbose logging with level control) to reduce log noise.

  } else if (copy_inst == CopyInst::kNormal) {
-    LOG(INFO) << "Lowering normal copy";
+    DLOG(INFO) << "Lowering normal copy";
     return LowerNormalCopy(T, analyzer);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f0c721a and b18b6f6.

📒 Files selected for processing (2)
  • src/op/copy.cc (2 hunks)
  • tilelang/language/copy.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Quick Lint
🔇 Additional comments (4)
src/op/copy.cc (2)

182-221: Scope-based range selection logic looks correct.

The hierarchical scope levels (fragment=2 > shared=1 > global=0) correctly prioritize the more local memory scope for loop bounds, and the per-dimension safety check guards against undersized iteration domains.

One observation: the safety check at lines 209-221 only validates that base_ext >= src_ext, but doesn't check against dst_range. If base_is_src is true and dst_range[i]->extent > src_range[i]->extent, the loop may not cover all destination elements. Is this intentional for broadcasting semantics (as described in the Python-side docstring)?


223-233: LGTM!

The loop variable creation correctly uses base_ranges throughout, maintaining consistency with the scope-based selection logic above.

tilelang/language/copy.py (2)

30-45: Excellent documentation of range handling semantics.

The added docstring clearly explains the extent derivation logic, broadcasting behavior, and tail-side legalization strategy. This will help users understand how different input types are handled.


76-81: LGTM!

The updated comments clearly describe the broadcasting and alignment logic.

@LeiWang1999 LeiWang1999 merged commit 17718be into tile-ai:main Nov 26, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant