Skip to content

[Codegen][GPU] Canonicalize to remove the empty extract slice in combineLayoutTransformation pass - #21395

Merged
jerryyin merged 1 commit into
mainfrom
users/zyin/canonicalize-extract-slice-from-empty-after-combinelayout
Jul 17, 2025
Merged

jerryyin merged 1 commit into
mainfrom
users/zyin/canonicalize-extract-slice-from-empty-after-combinelayout

Conversation

@jerryyin

@jerryyin jerryyin commented Jul 16, 2025 •

Copy link
Copy Markdown
Member

Credit to @Max191 for pointing out the this canonicalization patterns that fix performance degrade in convolution.

%11 = tensor.empty() : tensor<2x2x16x4x16xbf16>
%12 = scf.forall (%arg3, %arg4, %arg5, %arg6) in (1, 1, 4, 64) shared_outs(%arg7 = %11) -> (tensor<2x2x16x4x16xbf16>) {
...
    %extracted_slice_2 = tensor.extract_slice %arg7[0, 0, %15, %arg5, %14#2] [2, 2, 4, 1, 1] [1, 1, 1, 1, 1] : tensor<2x2x16x4x16xbf16> to tensor<2x2x4x1x1xbf16>
    %transposed = linalg.transpose ins(%20 : tensor<2x2x1x4x1xbf16>) outs(%extracted_slice_2 : tensor<2x2x4x1x1xbf16>) permutation = [0, 1, 3, 2, 4] 

Here %11 is an empty tensor, so that extract_slice can be folded into a new empty tensor. Now without the canonicalization patterns, this will results in a huge private allocation %alloca = memref.alloca() : memref<2x2x16x4x16xbf16, #gpu.address_space<private>> that eventually results in register spills that kills the performance.

Note that this will only happen when input IR has two level nested scf.forall, this make it challenging to cleanly put it in as a combine layout transformation pass test. Therefore I'm putting it as a pipeline test instead.

@jerryyin jerryyin changed the title Canonicalize to remove the empty extract slice in combineLayoutTransformation pass [Codegen][GPU] Canonicalize to remove the empty extract slice in combineLayoutTransformation pass Jul 16, 2025

@nirvedhmeshram nirvedhmeshram left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!

@jerryyin
jerryyin force-pushed the users/zyin/canonicalize-extract-slice-from-empty-after-combinelayout branch from d58c69c to b83003c Compare July 16, 2025 21:28
@jerryyin
jerryyin force-pushed the users/zyin/canonicalize-extract-slice-from-empty-after-combinelayout branch 5 times, most recently from 6901b13 to 914d8ac Compare July 17, 2025 13:00
@jerryyin
jerryyin merged commit f506d00 into main Jul 17, 2025
@jerryyin
jerryyin deleted the users/zyin/canonicalize-extract-slice-from-empty-after-combinelayout branch July 17, 2025 13:23
IanWood1 pushed a commit to IanWood1/iree that referenced this pull request Jul 18, 2025
…ineLayoutTransformation pass (iree-org#21395)

Credit to @Max191 for pointing out the this canonicalization patterns
that fix performance degrade in convolution.

```mlir
%12 = tensor.empty() : tensor<2x21x64xbf16>
%13:2 = scf.forall (%arg3, %arg4, %arg5, %arg6) in (1, 1, 4, 64) shared_outs(%arg7 = %11, %arg8 = %12) -> (tensor<2x2x16x4x16xbf16>, tensor<2x21x64xbf16>) {
...
  %extracted_slice = tensor.extract_slice %arg8[0, 0, %15, %arg5, %14#2] [2, 2, 4, 1, 1] [1, 1, 1, 1, 1] : tensor<2x2x16x4x16xbf16> to tensor<2x2x4x1x1xbf16>
  %transposed = linalg.transpose ins(%20 : tensor<2x2x1x4x1xbf16>) outs(%extracted_slice : tensor<2x2x4x1x1xbf16>) permutation = [0, 1, 3, 2, 4] 
```

Here `%12` is an empty tensor, so that extract_slice can be folded into
a new empty tensor. Now without the canonicalization patterns, this will
results in a huge private allocation ` %alloca = memref.alloca() :
memref<2x2x16x4x16xbf16, #gpu.address_space<private>>` that eventually
results in register spills that kills the performance.

Note that this will only happen when input IR has two level nested
`scf.forall`, this make it challenging to cleanly put it in as a combine
layout transformation pass test. Therefore I'm putting it as a pipeline
test instead.

Signed-off-by: jerryyin <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…ineLayoutTransformation pass (#21395)

Credit to @Max191 for pointing out the this canonicalization patterns
that fix performance degrade in convolution.

```mlir
%12 = tensor.empty() : tensor<2x21x64xbf16>
%13:2 = scf.forall (%arg3, %arg4, %arg5, %arg6) in (1, 1, 4, 64) shared_outs(%arg7 = %11, %arg8 = %12) -> (tensor<2x2x16x4x16xbf16>, tensor<2x21x64xbf16>) {
...
  %extracted_slice = tensor.extract_slice %arg8[0, 0, %15, %arg5, %14#2] [2, 2, 4, 1, 1] [1, 1, 1, 1, 1] : tensor<2x2x16x4x16xbf16> to tensor<2x2x4x1x1xbf16>
  %transposed = linalg.transpose ins(%20 : tensor<2x2x1x4x1xbf16>) outs(%extracted_slice : tensor<2x2x4x1x1xbf16>) permutation = [0, 1, 3, 2, 4] 
```

Here `%12` is an empty tensor, so that extract_slice can be folded into
a new empty tensor. Now without the canonicalization patterns, this will
results in a huge private allocation ` %alloca = memref.alloca() :
memref<2x2x16x4x16xbf16, #gpu.address_space<private>>` that eventually
results in register spills that kills the performance.

Note that this will only happen when input IR has two level nested
`scf.forall`, this make it challenging to cleanly put it in as a combine
layout transformation pass test. Therefore I'm putting it as a pipeline
test instead.

Signed-off-by: jerryyin <[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.

3 participants