Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Codegen] Use safer hoisting in OptimizeTensorInsertExtractSlices#23280

Merged
Max191 merged 1 commit into
iree-org:mainfrom
Max191-agents:safer-hoisting-in-optimize-slices
Jan 27, 2026
Merged

[Codegen] Use safer hoisting in OptimizeTensorInsertExtractSlices#23280
Max191 merged 1 commit into
iree-org:mainfrom
Max191-agents:safer-hoisting-in-optimize-slices

Conversation

@Max191
Copy link
Copy Markdown
Contributor

@Max191 Max191 commented Jan 26, 2026

Use the moveLoopInvariantCodeFromGuaranteedLoops transform instead of the moveLoopInvariantCode transform in the OptimizeTensorInsertExtractSlices pass. This transform is safer, because it validates that loops will be executed at least once before hoisting loop invariant code. Hoisting from loops that may not execute is not an optimization, so this is a better version of the transformation.

The new safer transform also hoists from linalg.generic ops, so the moveLoopInvariantCodeFromGenericOps is removed, since it is no longer used.

This PR also removes the _batch_matmul_narrow_n_2_dispatch_4_unpack_i32 test, which was doing nothing but checking that a tensor.empty op gets hoisted from an scf.for loop (which cannot be guaranteed to execute). Hoisting empty tensors is not the job of this pass, and the test is verbose, so the test is simply removed.

@Max191
Copy link
Copy Markdown
Contributor Author

Max191 commented Jan 26, 2026

I'm going to convert to draft until I have the pipeline tests under control. There is an annoying issue with broadcast in the VectorDistribute pipeline.

@Max191
Copy link
Copy Markdown
Contributor Author

Max191 commented Jan 26, 2026

Here's the issue with broadcast in VectorDistribute: #23283

@Max191
Copy link
Copy Markdown
Contributor Author

Max191 commented Jan 26, 2026

The hack we could do is to prevent hoisting from scf.forall ops in OptimizeTensorInsertExtractSlices, since that is what is causing the bug, but it is just masking the real issue IMO. Otherwise, maybe this deserves a longer form discussion.

@Groverkss
Copy link
Copy Markdown
Contributor

The hack we could do is to prevent hoisting from scf.forall ops in OptimizeTensorInsertExtractSlices, since that is what is causing the bug, but it is just masking the real issue IMO. Otherwise, maybe this deserves a longer form discussion.

I commented on the issue a potential fix that should fix the dps chain. This is fixing a real issue on vector distribute where we aren't hoisting out the redundant computation so we should try to fix it. If it doesn't work, maybe add an option to not fuse out of scf.forall and enable it in VectorDistribute and i'll have a look at it.

@Max191
Copy link
Copy Markdown
Contributor Author

Max191 commented Jan 26, 2026

The suggestion from @Groverkss worked, so once #23285 is merged, I'll rebase this one and it should be good to go. Thanks for the quick suggestion Kunwar!

@Max191 Max191 force-pushed the safer-hoisting-in-optimize-slices branch from 81c9ff4 to ac8b09d Compare January 26, 2026 22:16
@Max191 Max191 force-pushed the safer-hoisting-in-optimize-slices branch from ac8b09d to f503a98 Compare January 27, 2026 17:55
@Max191 Max191 marked this pull request as ready for review January 27, 2026 17:55
@Max191 Max191 merged commit 789859e into iree-org:main Jan 27, 2026
55 of 58 checks passed
@Max191 Max191 deleted the safer-hoisting-in-optimize-slices branch January 27, 2026 20:46
hanhanW pushed a commit that referenced this pull request Feb 6, 2026
…3280)

Use the `moveLoopInvariantCodeFromGuaranteedLoops` transform instead of
the `moveLoopInvariantCode` transform in the
OptimizeTensorInsertExtractSlices pass. This transform is safer, because
it validates that loops will be executed at least once before hoisting
loop invariant code. Hoisting from loops that may not execute is not an
optimization, so this is a better version of the transformation.

The new safer transform also hoists from linalg.generic ops, so the
`moveLoopInvariantCodeFromGenericOps` is removed, since it is no longer
used.

This PR also removes the
`_batch_matmul_narrow_n_2_dispatch_4_unpack_i32` test, which was doing
nothing but checking that a tensor.empty op gets hoisted from an scf.for
loop (which cannot be guaranteed to execute). Hoisting empty tensors is
not the job of this pass, and the test is verbose, so the test is simply
removed.

Signed-off-by: Max Dawkins <[email protected]>
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Feb 24, 2026
…ee-org#23280)

Use the `moveLoopInvariantCodeFromGuaranteedLoops` transform instead of
the `moveLoopInvariantCode` transform in the
OptimizeTensorInsertExtractSlices pass. This transform is safer, because
it validates that loops will be executed at least once before hoisting
loop invariant code. Hoisting from loops that may not execute is not an
optimization, so this is a better version of the transformation.

The new safer transform also hoists from linalg.generic ops, so the
`moveLoopInvariantCodeFromGenericOps` is removed, since it is no longer
used.

This PR also removes the
`_batch_matmul_narrow_n_2_dispatch_4_unpack_i32` test, which was doing
nothing but checking that a tensor.empty op gets hoisted from an scf.for
loop (which cannot be guaranteed to execute). Hoisting empty tensors is
not the job of this pass, and the test is verbose, so the test is simply
removed.

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.

4 participants