Skip to content

[S-TIR][Schedule] Reject loop_partition on a reduction loop of a block with init - #20858

Open
cchung100m wants to merge 3 commits into
apache:mainfrom
cchung100m:issue-20823
Open

cchung100m wants to merge 3 commits into
apache:mainfrom
cchung100m:issue-20823

Conversation

@cchung100m

Copy link
Copy Markdown
Contributor

Hi Committers,

This PR fixes issue #20823. Any suggestions would be appreciated if you are available.

Root Cause

loop_partition clones the loop body once per partition. BlockMutator updates the iter-var domains and the read/write regions of each clone, but leaves block->init untouched. When the partitioned loop binds a reduction iter of a block that has an init statement, every partition keeps its own with T.init(): S[v_i] = 0. The second partition therefore resets the accumulator, and only the last partition's contribution survives:
expected [45, 145, 245, 345] get [24, 54, 84, 114]
The primitive had no check for this case, so the wrong result was produced silently.

Solution

LoopPartition now rejects the unsafe case with a ScheduleError.

  • Add PartitionReductionWithInitError in src/s_tir/schedule/primitive/loop_transformation.cc. It walks the blocks under the loop and raises when a block with an init has a kCommReduce iter whose binding expression uses the loop var. Composite bindings(for example, ko * 5 + ki or a fused i * 10 + k) are detected as well.
  • Spatial loops and blocks without an init are unaffected.
  • Document the restriction in the loop_partition docstring and point to decompose_reduction as the workaround.

I chose rejection over keeping the init only in the first partition because the first partition is guarded by extent > 0 and fused bindings make it hard to tell which partition owns the init.

@cchung100m
cchung100m marked this pull request as ready for review October 30, 2026 17:42
…tring and point to decompose_reduction as the workaround
@cchung100m

Copy link
Copy Markdown
Contributor Author

Hi @tlopex @spectrometerHBH @swjng
Could you please help review this PR? Thank you!

This branch has not been deployed

No deployments
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