-
Notifications
You must be signed in to change notification settings - Fork 873
[Codegen][MXFP4] Add folding patterns for tensor.empty op that can bypass SwizzleHintOps #23084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Muzammiluddin-Syed-ECE
merged 11 commits into
iree-org:main
from
Muzammiluddin-Syed-ECE:muzasyed/fold
Jan 16, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
af63572
add new pass to flatten allocs right before resolution
Muzammiluddin-Syed-ECE 6c14083
adding folding patterns
Muzammiluddin-Syed-ECE d129274
rebasing off main
Muzammiluddin-Syed-ECE f00e034
lint fix
Muzammiluddin-Syed-ECE e941e7d
code quality fixes
Muzammiluddin-Syed-ECE cabf6ef
PR review comments
Muzammiluddin-Syed-ECE a1cdcc5
Pr review comments pt 2
Muzammiluddin-Syed-ECE 89031ed
adding new line at the end of test
Muzammiluddin-Syed-ECE ae5aef7
adding gpu apply tiling test
Muzammiluddin-Syed-ECE 44fa70a
updating assert message
Muzammiluddin-Syed-ECE 4eecb5d
address pr comments pt 3
Muzammiluddin-Syed-ECE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
compiler/src/iree/compiler/Codegen/Common/GPU/test/gpu_fold_swizzle_hint_ops.mlir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // RUN: iree-opt --mlir-print-local-scope --pass-pipeline="builtin.module(func.func(iree-codegen-gpu-apply-tiling-level, canonicalize, cse))" %s | FileCheck %s | ||
|
|
||
| // Test: tensor.extract_slice of swizzle_hint(tensor.empty) should fold | ||
| // to swizzle_hint(tensor.empty) with the sliced shape. | ||
| func.func @fold_extract_slice_of_swizzle_hint() -> tensor<16x32xf32> { | ||
| %empty = tensor.empty() : tensor<64x64xf32> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| %slice = tensor.extract_slice %swizzle[0, 0] [16, 32] [1, 1] : tensor<64x64xf32> to tensor<16x32xf32> | ||
| return %slice : tensor<16x32xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @fold_extract_slice_of_swizzle_hint | ||
| // CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<16x32xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[EMPTY]][#iree_codegen.rotate_rows<64, 4>] : tensor<16x32xf32> | ||
| // CHECK: return %[[SWIZZLE]] | ||
|
|
||
| // Test: tensor.extract_slice with dynamic sizes should fold correctly. | ||
| func.func @fold_extract_slice_dynamic(%size0: index, %size1: index) -> tensor<?x?xf32> { | ||
| %empty = tensor.empty() : tensor<64x64xf32> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.xor_shuffle<128, 16>] : tensor<64x64xf32> | ||
| %slice = tensor.extract_slice %swizzle[0, 0] [%size0, %size1] [1, 1] : tensor<64x64xf32> to tensor<?x?xf32> | ||
| return %slice : tensor<?x?xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @fold_extract_slice_dynamic | ||
| // CHECK-SAME: %[[SIZE0:[A-Za-z0-9]+]]: index | ||
| // CHECK-SAME: %[[SIZE1:[A-Za-z0-9]+]]: index | ||
| // CHECK: %[[EMPTY:.+]] = tensor.empty(%[[SIZE0]], %[[SIZE1]]) : tensor<?x?xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[EMPTY]][#iree_codegen.xor_shuffle<128, 16>] : tensor<?x?xf32> | ||
| // CHECK: return %[[SWIZZLE]] | ||
|
|
||
| // Test: tensor.expand_shape of swizzle_hint(tensor.empty) should fold | ||
| // to swizzle_hint(tensor.empty) with the expanded shape. | ||
| func.func @fold_expand_shape_of_swizzle_hint() -> tensor<4x16x64xf32> { | ||
| %empty = tensor.empty() : tensor<64x64xf32> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| %expanded = tensor.expand_shape %swizzle [[0, 1], [2]] output_shape [4, 16, 64] : tensor<64x64xf32> into tensor<4x16x64xf32> | ||
| return %expanded : tensor<4x16x64xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @fold_expand_shape_of_swizzle_hint | ||
| // CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<4x16x64xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[EMPTY]][#iree_codegen.rotate_rows<64, 4>] : tensor<4x16x64xf32> | ||
| // CHECK: return %[[SWIZZLE]] | ||
|
|
||
| // Test: tensor.collapse_shape of swizzle_hint(tensor.empty) should fold | ||
| // to swizzle_hint(tensor.empty) with the collapsed shape. | ||
| func.func @fold_collapse_shape_of_swizzle_hint() -> tensor<64x64xf32> { | ||
| %empty = tensor.empty() : tensor<4x16x4x16xf32> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.rotate_rows<64, 4>] : tensor<4x16x4x16xf32> | ||
| %collapsed = tensor.collapse_shape %swizzle [[0, 1], [2, 3]] : tensor<4x16x4x16xf32> into tensor<64x64xf32> | ||
| return %collapsed : tensor<64x64xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @fold_collapse_shape_of_swizzle_hint | ||
| // CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<64x64xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[EMPTY]][#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| // CHECK: return %[[SWIZZLE]] | ||
|
|
||
| // Negative test: extract_slice of swizzle_hint without tensor.empty source | ||
| // should NOT fold. | ||
| func.func @no_fold_extract_slice_non_empty(%arg0: tensor<64x64xf32>) -> tensor<16x32xf32> { | ||
| %swizzle = iree_codegen.swizzle_hint %arg0[#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| %slice = tensor.extract_slice %swizzle[0, 0] [16, 32] [1, 1] : tensor<64x64xf32> to tensor<16x32xf32> | ||
| return %slice : tensor<16x32xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @no_fold_extract_slice_non_empty | ||
| // CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<64x64xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[ARG0]][#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| // CHECK: %[[SLICE:.+]] = tensor.extract_slice %[[SWIZZLE]] | ||
| // CHECK: return %[[SLICE]] | ||
|
|
||
| // Negative test: expand_shape of swizzle_hint without tensor.empty source | ||
| // should NOT fold. | ||
| func.func @no_fold_expand_shape_non_empty(%arg0: tensor<64x64xf32>) -> tensor<4x16x64xf32> { | ||
| %swizzle = iree_codegen.swizzle_hint %arg0[#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| %expanded = tensor.expand_shape %swizzle [[0, 1], [2]] output_shape [4, 16, 64] : tensor<64x64xf32> into tensor<4x16x64xf32> | ||
| return %expanded : tensor<4x16x64xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @no_fold_expand_shape_non_empty | ||
| // CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<64x64xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[ARG0]][#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| // CHECK: %[[EXPANDED:.+]] = tensor.expand_shape %[[SWIZZLE]] | ||
| // CHECK: return %[[EXPANDED]] | ||
|
|
||
| // Test: XOR shuffle swizzle attribute is preserved through folding. | ||
| func.func @fold_xor_shuffle_swizzle() -> tensor<8x64xf32> { | ||
| %empty = tensor.empty() : tensor<16x128xf32> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.xor_shuffle<128, 16>] : tensor<16x128xf32> | ||
| %slice = tensor.extract_slice %swizzle[0, 0] [8, 64] [1, 1] : tensor<16x128xf32> to tensor<8x64xf32> | ||
| return %slice : tensor<8x64xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @fold_xor_shuffle_swizzle | ||
| // CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<8x64xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[EMPTY]][#iree_codegen.xor_shuffle<128, 16>] : tensor<8x64xf32> | ||
| // CHECK: return %[[SWIZZLE]] | ||
|
|
||
| // Test: Rank-reducing extract_slice should work correctly. | ||
| func.func @fold_rank_reducing_extract_slice() -> tensor<32xf32> { | ||
| %empty = tensor.empty() : tensor<64x64xf32> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.rotate_rows<64, 4>] : tensor<64x64xf32> | ||
| %slice = tensor.extract_slice %swizzle[0, 0] [1, 32] [1, 1] : tensor<64x64xf32> to tensor<32xf32> | ||
| return %slice : tensor<32xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @fold_rank_reducing_extract_slice | ||
| // CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<32xf32> | ||
| // CHECK: %[[SWIZZLE:.+]] = iree_codegen.swizzle_hint %[[EMPTY]][#iree_codegen.rotate_rows<64, 4>] : tensor<32xf32> | ||
| // CHECK: return %[[SWIZZLE]] | ||
|
|
||
| #encoding = #iree_encoding.encoding<operand_index = 0, op_type = matmul, element_types = [bf16, bf16, bf16], user_indexing_maps = [affine_map<(m, n, k) -> (m, k)>, affine_map<(m, n, k) -> (k, n)>, affine_map<(m, n, k) -> (m, n)>], iteration_sizes = [?, ?, ?]> | ||
| func.func @fold_swizzle_hint_of_encoding() -> tensor<16xbf16,#encoding> { | ||
| %empty = tensor.empty() : tensor<8x16xbf16, #encoding> | ||
| %swizzle = iree_codegen.swizzle_hint %empty[#iree_codegen.rotate_rows<8, 4>] : tensor<8x16xbf16, #encoding> | ||
| %slice = tensor.extract_slice %swizzle[0, 0] [1, 16] [1, 1] : tensor<8x16xbf16, #encoding> to tensor<16xbf16,#encoding> | ||
| return %slice : tensor<16xbf16,#encoding> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.