-
Notifications
You must be signed in to change notification settings - Fork 871
[Codegen][MXFP4] Adding SwizzleHintOp alloc flattening pass #23083
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 7 commits into
iree-org:main
from
Muzammiluddin-Syed-ECE:muzasyed/flattening
Jan 16, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c5c5eea
Changing swizzle hint op to not require flat allocs at tensor level
Muzammiluddin-Syed-ECE 6e0cbc5
adding folding patterns
Muzammiluddin-Syed-ECE 06a407c
adding tests
Muzammiluddin-Syed-ECE 0399455
adding another test
Muzammiluddin-Syed-ECE ce08838
addressing PR comments
Muzammiluddin-Syed-ECE 511c8b8
PR comments pt 2
Muzammiluddin-Syed-ECE 00c1c39
adjust errors
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
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
87 changes: 87 additions & 0 deletions
87
compiler/src/iree/compiler/Codegen/Common/FlattenSwizzleHintAllocs.cpp
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,87 @@ | ||
| // Copyright 2026 The IREE Authors | ||
| // | ||
| // Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include "iree/compiler/Codegen/Common/Passes.h" | ||
| #include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenOps.h" | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
| #include "mlir/Dialect/MemRef/Utils/MemRefUtils.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
|
|
||
| namespace mlir::iree_compiler { | ||
|
|
||
| #define GEN_PASS_DEF_FLATTENSWIZZLEHINTALLOCSPASS | ||
| #include "iree/compiler/Codegen/Common/Passes.h.inc" | ||
|
|
||
| namespace { | ||
| struct FlattenSwizzleHintAllocsPass final | ||
| : impl::FlattenSwizzleHintAllocsPassBase<FlattenSwizzleHintAllocsPass> { | ||
| using Base::Base; | ||
| void runOnOperation() override; | ||
| }; | ||
| } // namespace | ||
|
|
||
| /// This pass flattens swizzle hint ops that operate on allocations of rank > 1. | ||
| /// This is required since swizzle hint op indices require flat memrefs. | ||
| /// | ||
| /// Example: | ||
| /// ``` | ||
| /// %0 = iree.alloc() : tensor<512x32xf4E2M1FN> | ||
| /// %1 = iree.swizzle_hint %0 : tensor<512x32xf4E2M1FN> -> | ||
| /// tensor<512x32xf4E2M1FN> | ||
| /// ``` | ||
| /// | ||
| /// is flattened to: | ||
| /// ``` | ||
| /// %0 = iree.alloc() : tensor<16384xf4E2M1FN> | ||
| /// %1 = iree.swizzle_hint %0 : tensor<16384xf4E2M1FN> -> tensor<16384xf4E2M1FN> | ||
| /// %2 = iree.expand_shape %1 : tensor<16384xf4E2M1FN> -> | ||
| /// tensor<512x32xf4E2M1FN> | ||
| /// ``` | ||
| static void flattenSwizzleHintAllocs(RewriterBase &rewriter, | ||
| IREE::Codegen::SwizzleHintOp hintOp) { | ||
| auto allocOp = hintOp.getOperand().getDefiningOp<memref::AllocOp>(); | ||
| if (!allocOp || !allocOp->hasOneUse()) { | ||
| return; | ||
| } | ||
| MemRefType resultType = allocOp.getType(); | ||
| if (resultType.getRank() == 1 || !resultType.getLayout().isIdentity() || | ||
| !memref::isStaticShapeAndContiguousRowMajor(resultType)) { | ||
Muzammiluddin-Syed-ECE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| SmallVector<int64_t> newResultShape = {resultType.getNumElements()}; | ||
| auto newResultType = | ||
| MemRefType::get(newResultShape, resultType.getElementType(), AffineMap(), | ||
Muzammiluddin-Syed-ECE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| resultType.getMemorySpace()); | ||
| rewriter.setInsertionPoint(hintOp); | ||
| ReassociationIndices reassoc = | ||
| llvm::to_vector(llvm::seq(resultType.getRank())); | ||
| auto newAllocOp = | ||
| memref::AllocOp::create(rewriter, hintOp.getLoc(), newResultType); | ||
| auto newSwizzleHintOp = IREE::Codegen::SwizzleHintOp::create( | ||
| rewriter, hintOp.getLoc(), newAllocOp.getResult(), hintOp.getSwizzle()); | ||
| auto expandShape = memref::ExpandShapeOp::create(rewriter, hintOp.getLoc(), | ||
| resultType.getShape(), | ||
| newSwizzleHintOp, {reassoc}); | ||
| rewriter.replaceOp(hintOp, expandShape); | ||
| } | ||
|
|
||
| void FlattenSwizzleHintAllocsPass::runOnOperation() { | ||
| FunctionOpInterface funcOp = getOperation(); | ||
| // Collect all swizzle hint ops that operate on allocations. | ||
| // Flatten all allocs of rank > 1. | ||
| SmallVector<IREE::Codegen::SwizzleHintOp> hintOps; | ||
| funcOp.walk( | ||
| [&](IREE::Codegen::SwizzleHintOp hint) { hintOps.push_back(hint); }); | ||
|
|
||
| IRRewriter rewriter(funcOp->getContext()); | ||
| for (IREE::Codegen::SwizzleHintOp hintOp : hintOps) { | ||
| flattenSwizzleHintAllocs(rewriter, hintOp); | ||
| } | ||
| } | ||
|
|
||
| } // namespace mlir::iree_compiler | ||
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
96 changes: 96 additions & 0 deletions
96
compiler/src/iree/compiler/Codegen/Common/GPU/test/flatten_swizzle_hint_allocs.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,96 @@ | ||
| // RUN: iree-opt --allow-unregistered-dialect --pass-pipeline="builtin.module(func.func(iree-codegen-flatten-swizzle-hint-allocs))" \ | ||
| // RUN: --mlir-print-local-scope %s | FileCheck %s | ||
|
|
||
| // Test: 1D alloc should NOT be flattened (already 1D). | ||
| func.func @skip_1d_alloc() { | ||
| %alloc = memref.alloc() : memref<2048xf32, #gpu.address_space<workgroup>> | ||
| %0 = iree_codegen.swizzle_hint %alloc[#iree_codegen.rotate_rows<64, 4>] : memref<2048xf32, #gpu.address_space<workgroup>> | ||
| "test.use"(%0) : (memref<2048xf32, #gpu.address_space<workgroup>>) -> () | ||
| return | ||
| } | ||
|
|
||
| // CHECK-LABEL: func @skip_1d_alloc | ||
| // CHECK: %[[ALLOC:.+]] = memref.alloc() : memref<2048xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[HINT:.+]] = iree_codegen.swizzle_hint %[[ALLOC]][#iree_codegen.rotate_rows<64, 4>] : memref<2048xf32, #gpu.address_space<workgroup>> | ||
| // CHECK-NOT: memref.expand_shape | ||
| // CHECK: "test.use"(%[[HINT]]) | ||
|
|
||
| // Test: 2D alloc with swizzle hint should be flattened to 1D. | ||
| func.func @flatten_2d_alloc() { | ||
| %alloc = memref.alloc() : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| %0 = iree_codegen.swizzle_hint %alloc[#iree_codegen.rotate_rows<64, 4>] : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| "test.use"(%0) : (memref<32x64xf32, #gpu.address_space<workgroup>>) -> () | ||
| return | ||
| } | ||
|
|
||
| // CHECK-LABEL: func @flatten_2d_alloc | ||
| // CHECK: %[[ALLOC1D:.+]] = memref.alloc() : memref<2048xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[HINT:.+]] = iree_codegen.swizzle_hint %[[ALLOC1D]][#iree_codegen.rotate_rows<64, 4>] : memref<2048xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[EXPAND:.+]] = memref.expand_shape %[[HINT]] {{\[\[}}0, 1{{\]\]}} output_shape [32, 64] : memref<2048xf32, #gpu.address_space<workgroup>> into memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: "test.use"(%[[EXPAND]]) | ||
| // CHECK-NOT: memref.alloc() : memref<32x64xf32 | ||
| // CHECK-NOT: iree_codegen.swizzle_hint {{.*}} : memref<32x64xf32 | ||
| // CHECK: return | ||
|
|
||
| // Test: 3D alloc with swizzle hint should be flattened to 1D. | ||
| func.func @flatten_3d_alloc() { | ||
| %alloc = memref.alloc() : memref<4x8x16xf32, #gpu.address_space<workgroup>> | ||
| %0 = iree_codegen.swizzle_hint %alloc[#iree_codegen.rotate_rows<64, 4>] : memref<4x8x16xf32, #gpu.address_space<workgroup>> | ||
| "test.use"(%0) : (memref<4x8x16xf32, #gpu.address_space<workgroup>>) -> () | ||
| return | ||
| } | ||
|
|
||
| // CHECK-LABEL: func @flatten_3d_alloc | ||
| // CHECK: %[[ALLOC1D:.+]] = memref.alloc() : memref<512xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[HINT:.+]] = iree_codegen.swizzle_hint %[[ALLOC1D]][#iree_codegen.rotate_rows<64, 4>] : memref<512xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[EXPAND:.+]] = memref.expand_shape %[[HINT]] {{\[\[}}0, 1, 2{{\]\]}} output_shape [4, 8, 16] : memref<512xf32, #gpu.address_space<workgroup>> into memref<4x8x16xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: "test.use"(%[[EXPAND]]) | ||
| // CHECK-NOT: memref.alloc() : memref<4x8x16xf32 | ||
| // CHECK-NOT: iree_codegen.swizzle_hint {{.*}} : memref<4x8x16xf32 | ||
| // CHECK: return | ||
|
|
||
| // Test: Non-alloc operand should NOT be affected. | ||
| func.func @skip_non_alloc(%arg0: memref<32x64xf32, #gpu.address_space<workgroup>>) { | ||
| %0 = iree_codegen.swizzle_hint %arg0[#iree_codegen.rotate_rows<64, 4>] : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| "test.use"(%0) : (memref<32x64xf32, #gpu.address_space<workgroup>>) -> () | ||
| return | ||
| } | ||
|
|
||
| // CHECK-LABEL: func @skip_non_alloc | ||
| // CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[HINT:.+]] = iree_codegen.swizzle_hint %[[ARG0]][#iree_codegen.rotate_rows<64, 4>] : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| // CHECK-NOT: memref.expand_shape | ||
| // CHECK: "test.use"(%[[HINT]]) | ||
|
|
||
| // Test: Alloc with multiple uses should NOT be flattened. | ||
| func.func @skip_multi_use_alloc() { | ||
| %alloc = memref.alloc() : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| %0 = iree_codegen.swizzle_hint %alloc[#iree_codegen.rotate_rows<64, 4>] : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| "test.use"(%alloc) : (memref<32x64xf32, #gpu.address_space<workgroup>>) -> () | ||
| "test.use"(%0) : (memref<32x64xf32, #gpu.address_space<workgroup>>) -> () | ||
| return | ||
| } | ||
|
|
||
| // CHECK-LABEL: func @skip_multi_use_alloc | ||
| // CHECK: %[[ALLOC:.+]] = memref.alloc() : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[HINT:.+]] = iree_codegen.swizzle_hint %[[ALLOC]][#iree_codegen.rotate_rows<64, 4>] : memref<32x64xf32, #gpu.address_space<workgroup>> | ||
| // CHECK-NOT: memref.expand_shape | ||
| // CHECK: "test.use"(%[[ALLOC]]) | ||
| // CHECK: "test.use"(%[[HINT]]) | ||
|
|
||
| // Test: XOR shuffle swizzle attribute. | ||
| func.func @flatten_xor_shuffle() { | ||
| %alloc = memref.alloc() : memref<16x128xi8, #gpu.address_space<workgroup>> | ||
| %0 = iree_codegen.swizzle_hint %alloc[#iree_codegen.xor_shuffle<128, 16>] : memref<16x128xi8, #gpu.address_space<workgroup>> | ||
| "test.use"(%0) : (memref<16x128xi8, #gpu.address_space<workgroup>>) -> () | ||
| return | ||
| } | ||
|
|
||
| // CHECK-LABEL: func @flatten_xor_shuffle | ||
| // CHECK: %[[ALLOC1D:.+]] = memref.alloc() : memref<2048xi8, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[HINT:.+]] = iree_codegen.swizzle_hint %[[ALLOC1D]][#iree_codegen.xor_shuffle<128, 16>] : memref<2048xi8, #gpu.address_space<workgroup>> | ||
| // CHECK: %[[EXPAND:.+]] = memref.expand_shape %[[HINT]] {{\[\[}}0, 1{{\]\]}} output_shape [16, 128] : memref<2048xi8, #gpu.address_space<workgroup>> into memref<16x128xi8, #gpu.address_space<workgroup>> | ||
| // CHECK: "test.use"(%[[EXPAND]]) | ||
| // CHECK-NOT: memref.alloc() : memref<16x128xi8 | ||
| // CHECK-NOT: iree_codegen.swizzle_hint {{.*}} : memref<16x128xi8 | ||
| // CHECK: return |
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
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.