Skip to content

[Codegen] Don't fold workgroup loops during workgroup tiling - #21137

Merged
Max191 merged 3 commits into
iree-org:mainfrom
Max191-agents:tile-dispatch-with-predistributed-ops
Jul 8, 2025
Merged

Max191 merged 3 commits into
iree-org:mainfrom
Max191-agents:tile-dispatch-with-predistributed-ops

Conversation

@Max191

@Max191 Max191 commented Jun 19, 2025 •

Copy link
Copy Markdown
Contributor

There are optimizations in TileAndDistributeToWorkgroupsUsingForallOpPass that try to fold away unit trip distribution loops, but this is not valid if there are ever any other workgroup forall loops in the IR, because the other forall may ask for multiple workers. This PR prevents the optimizations from fully folding the distribution loop.

@Max191
Max191 requested a review from hanhanW as a code owner June 19, 2025 14:09
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

You maybe can move that canonicalization outside of the main tiling methods, but more than that I think this is kind of hinting at issues with the loose/implicit link between different workgroup distributions.

Make the workgroup distribution distribute only along x-dimension, i.e. fold all the logical dimensions into a single physical dimension, then you can use delinearize to get back the different dimensions (AFAIK this should be very easy to change cause this is what happens to map a 4D logical distribution onto a 3D distribution. Then you wont care about the unit dims. The x/y/z dimensions have no basis actually in hardware. its all just a list.

@Max191

Max191 commented Jun 23, 2025

Copy link
Copy Markdown
Contributor Author

I think this is kind of hinting at issues with the loose/implicit link between different workgroup distributions.

Yeah, we need to be careful about folding away workgroup loops. Folding away any workgroup loop loses some information about the fact that the contained computation is distributed to a single workgroup. In this case, it is easy to tell that folding the loop will be invalid, but if the order of transformations were reversed (do workgroup tiling and fold the forall loop, and then introduce a separate forall later), then it would be very hard to tell that any of the dispatch is already distributed.

I didn't want to interfere with the current default path too much in this PR, but now that we aren't assuming only a single scf.forall when we resolve workgroup distribution, I think we really shouldn't ever be fully folding away workgroup loops.

Make the workgroup distribution distribute only along x-dimension, i.e. fold all the logical dimensions into a single physical dimension, then you can use delinearize to get back the different dimensions (AFAIK this should be very easy to change cause this is what happens to map a 4D logical distribution onto a 3D distribution. Then you wont care about the unit dims. The x/y/z dimensions have no basis actually in hardware. its all just a list.

This seems like the job of ReconcileTranslationInfo, no? I think the point of folding away the unit dims was to open op additional optimizations by having more static information in the IR. Linearizing and delinearizing like this seems unnecessary at this phase of codegen. We just need to be careful about folding away forall loops in general IMO.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, based on offline conversation. I can stamp this PR, but there is a lot of "if checking" to walk a very specific path. I think it would be much better if can prefetch the "linearize all to x" first and then this PR will become redundant. I guess it depends on how badly you need this PR to unblock. If you need it, I can stamp it, but the lot of checking here is very specific and threading a very fine needle to effectively just pass tests we have right now.

@Max191
Max191 force-pushed the tile-dispatch-with-predistributed-ops branch 2 times, most recently from 4f1a060 to 24a4bd3 Compare June 30, 2025 19:27
@Max191
Max191 requested review from Groverkss and kuhar as code owners June 30, 2025 20:05
@Max191
Max191 force-pushed the tile-dispatch-with-predistributed-ops branch from 7b385e4 to 306c104 Compare July 7, 2025 13:35
@Max191 Max191 changed the title [Codegen] Account for pre-distributed computation in workgroup tiling [Codegen] Don't fold workgroup loops during workgroup tiling Jul 7, 2025
Signed-off-by: Max Dawkins <[email protected]>
@Max191

Max191 commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

@MaheshRavishankar after rebasing, the new test you added was failing, which I fixed by adjusting the lowering config that had tile sizes larger than the problem size (see latest commit). I assume this was a fine change to make, but I'll wait for your confirmation.

@Max191
Max191 merged commit 887d75a into iree-org:main Jul 8, 2025
@Max191
Max191 deleted the tile-dispatch-with-predistributed-ops branch July 8, 2025 16:19
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Jul 9, 2025
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Jul 9, 2025
Max191 added a commit to Max191-agents/iree that referenced this pull request Jul 9, 2025
Max191 added a commit to Max191-agents/iree that referenced this pull request Jul 9, 2025
Max191 added a commit that referenced this pull request Jul 9, 2025
…21137)" (#21318)

This reverts the commit
887d75a
which caused a failure compiling LLaMa 8b downstream.

Also reverts dependent commit:
7f35ecf

---------

Signed-off-by: Max Dawkins <[email protected]>
Max191 added a commit to Max191-agents/iree that referenced this pull request Jul 15, 2025
Max191 added a commit to Max191-agents/iree that referenced this pull request Jul 15, 2025
Max191 added a commit that referenced this pull request Jul 16, 2025
…#21137)" (#21382)

Undo the revert of
887d75a
and
7f35ecf
now that the failure is fixed by
#21334

Signed-off-by: Max Dawkins <[email protected]>
IanWood1 pushed a commit to IanWood1/iree that referenced this pull request Jul 18, 2025
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
There are optimizations in
`TileAndDistributeToWorkgroupsUsingForallOpPass` that try to fold away
unit trip distribution loops, but this is not valid if there are ever
any other workgroup forall loops in the IR, because the other forall may
ask for multiple workers. This PR prevents the optimizations from fully
folding the distribution loop.

---------

Signed-off-by: Max Dawkins <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…21137)" (#21318)

This reverts the commit
887d75a
which caused a failure compiling LLaMa 8b downstream.

Also reverts dependent commit:
7f35ecf

---------

Signed-off-by: Max Dawkins <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…#21137)" (#21382)

Undo the revert of
887d75a
and
7f35ecf
now that the failure is fixed by
#21334

Signed-off-by: Max Dawkins <[email protected]>
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.

2 participants