Skip to content
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

[ConvertToUnpack] Apply correct permutation when lowering unpack to DMA #794

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ LogicalResult unPackDmaInputs(IREE::LinalgExt::UnPackOp unPackOp,
SmallVector<OpFoldResult> outerSizes =
SmallVector<OpFoldResult>(sizes.begin(), sizes.begin() + numOuterDims);

// Apply permutations to the outer dims if provided.
// Apply inverse permutation to the outer dims if permutation provided (if
// permutation not provided, it is identity, and therefore so is the inverse).
if (!permutation.empty()) {
applyPermutationToVector(outerStrides, permutation);
applyPermutationToVector(outerSizes, permutation);
applyPermutationToVector(outerOffsets, permutation);
SmallVector<int64_t> inversePermutation =
invertPermutationVector(permutation);
applyPermutationToVector(outerStrides, inversePermutation);
applyPermutationToVector(outerSizes, inversePermutation);
applyPermutationToVector(outerOffsets, inversePermutation);
}
// Do the unpacking on the Outer dims.
llvm::SmallDenseMap<int64_t, int64_t> outerDimsIndexMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: iree-opt --iree-amdaie-convert-to-dma --cse --split-input-file %s | FileCheck %s
// RUN: iree-opt --iree-amdaie-convert-to-dma --split-input-file %s | FileCheck %s

// CHECK-LABEL: @basic_unitdim_pack
// CHECK: %[[ALLOC0:.*]] = memref.alloc() : memref<1x1x8x16xi32, 1>
Expand Down Expand Up @@ -36,28 +36,28 @@ func.func @multidim_pack() {

// -----
// CHECK-LABEL: @permute_pack
// CHECK: %[[ALLOC0:.*]] = memref.alloc() : memref<1x1x2x2x4x8xi32, 2>
// CHECK: %[[FROMMEMREF0:.*]] = amdaie.logicalobjectfifo.from_memref %[[ALLOC0]], {} : memref<1x1x2x2x4x8xi32, 2> -> !amdaie.logicalobjectfifo<memref<1x1x2x2x4x8xi32, 2>>
// CHECK: %[[ALLOC1:.*]] = memref.alloc() : memref<1x1x8x16xi32, 1>
// CHECK: %[[FROMMEMREF1:.*]] = amdaie.logicalobjectfifo.from_memref %[[ALLOC1]], {} : memref<1x1x8x16xi32, 1> -> !amdaie.logicalobjectfifo<memref<1x1x8x16xi32, 1>>
// CHECK: %[[ALLOC_DST:.*]] = memref.alloc() : memref<1x1x2x2x4x8xi32, 2>
// CHECK: %[[FROMDST:.*]] = amdaie.logicalobjectfifo.from_memref %[[ALLOC_DST]], {} : memref<1x1x2x2x4x8xi32, 2> -> !amdaie.logicalobjectfifo<memref<1x1x2x2x4x8xi32, 2>>
// CHECK: %[[ALLOC_SRC:.*]] = memref.alloc() : memref<1x1x8x16xi32, 1>
// CHECK: %[[FROMSRC:.*]] = amdaie.logicalobjectfifo.from_memref %[[ALLOC_SRC]], {} : memref<1x1x8x16xi32, 1> -> !amdaie.logicalobjectfifo<memref<1x1x8x16xi32, 1>>
// CHECK: %[[DMA0:.*]] = amdaie.dma_cpy_nd
// CHECK-SAME: %[[FROMMEMREF0]][0, 0, 0, 0, 0, 0] [1, 1, 2, 2, 4, 8] [128, 128, 64, 32, 8, 1]
// CHECK-SAME: %[[FROMMEMREF1]][0, 0, 0, 0, 0, 0] [1, 1, 2, 2, 4, 8] [128, 128, 8, 64, 16, 1]
// CHECK-SAME: %[[FROMDST]][0, 0, 0, 0, 0, 0] [1, 1, 2, 2, 4, 8] [128, 128, 64, 32, 8, 1]
// CHECK-SAME: %[[FROMSRC]][0, 0, 0, 0, 0, 0] [1, 1, 2, 2, 4, 8] [128, 128, 8, 64, 16, 1]
// CHECK-SAME: (!amdaie.logicalobjectfifo<memref<1x1x2x2x4x8xi32, 2>>, !amdaie.logicalobjectfifo<memref<1x1x8x16xi32, 1>>)
func.func @permute_pack() {
%alloc = memref.alloc() : memref<1x1x2x2x4x8xi32, 2>
%alloc_0 = memref.alloc() : memref<1x1x8x16xi32, 1>
iree_linalg_ext.pack %alloc_0 outer_dims_perm = [0, 1, 3, 2] inner_dims_pos = [2, 3] inner_tiles = [4, 8] into %alloc : (memref<1x1x8x16xi32, 1> memref<1x1x2x2x4x8xi32, 2>)
%dst = memref.alloc() : memref<1x1x2x2x4x8xi32, 2>
%src = memref.alloc() : memref<1x1x8x16xi32, 1>
iree_linalg_ext.pack %src outer_dims_perm = [0, 1, 3, 2] inner_dims_pos = [2, 3] inner_tiles = [4, 8] into %dst : (memref<1x1x8x16xi32, 1> memref<1x1x2x2x4x8xi32, 2>)
return
}



// -----
// CHECK-LABEL: @subview_pack
// CHECK: %[[ALLOC1:.*]] = memref.alloc() : memref<32x8x8xf32>
// CHECK: %[[FROMMEMREF1:.*]] = amdaie.logicalobjectfifo.from_memref %[[ALLOC1]], {} : memref<32x8x8xf32> -> !amdaie.logicalobjectfifo<memref<32x8x8xf32>>
// CHECK: scf.parallel (%arg0, %arg1, %arg2) = (%c0, %c0, %c0) to (%c32, %c8, %c64) step (%c1, %c8, %c64)
// CHECK-NOT: memref.subview
// CHECK: %[[ALLOC0:.*]] = memref.alloc() : memref<1x1x1x8x8xf32, 1>
// CHECK: %[[FROMMEMREF0:.*]] = amdaie.logicalobjectfifo.from_memref %[[ALLOC0]], {} : memref<1x1x1x8x8xf32, 1> -> !amdaie.logicalobjectfifo<memref<1x1x1x8x8xf32, 1>>
// CHECK: %[[DMA0:.*]] = amdaie.dma_cpy_nd
Expand Down Expand Up @@ -212,3 +212,66 @@ func.func @copy_away_from_core() {
linalg.copy ins(%src : memref<8xi32, 2>) outs(%dst : memref<8xi32, 1>)
return
}

// -----

// CHECK-LABEL: @permute_unpack_tricyle_permute_rank_preserving
// CHECK-DAG: %[[DST:.*]] = amdaie.logicalobjectfifo.from_memref {{.*}} !amdaie.logicalobjectfifo<memref<30x20x10xf32, 1>>
// CHECK-DAG: %[[SRC:.*]] = amdaie.logicalobjectfifo.from_memref {{.*}} !amdaie.logicalobjectfifo<memref<20x10x30xf32, 2>>
// CHECK: amdaie.dma_cpy_nd
// CHECK-SAME: %[[DST]][0, 0, 0] [30, 20, 10] [200, 10, 1]
// CHECK-SAME: %[[SRC]][0, 0, 0] [30, 20, 10] [1, 300, 30]

// Before this PR this was:
// %2 = amdaie.dma_cpy_nd(
// %0[0, 0, 0] [30, 20, 10] [200, 10, 1],
// %1[0, 0, 0] [10, 30, 20] [30, 1, 300]) :
// (!amdaie.logicalobjectfifo<memref<30x20x10xf32, 1>>,
// !amdaie.logicalobjectfifo<memref<20x10x30xf32, 2>>)

// which is incorrect: we always expect the sizes to be the same
// (up to shape expansion/collapse) for src and dst, which is not the case for
// [30, 20, 10] and [10, 30, 20].

func.func @permute_unpack_tricyle_permute_rank_preserving(){
%dst = memref.alloc() : memref<30x20x10xf32, 1>
%src = memref.alloc() : memref<20x10x30xf32, 2>
iree_linalg_ext.unpack %src outer_dims_perm = [1, 2, 0]
inner_dims_pos = []
inner_tiles = []
into %dst : (memref<20x10x30xf32, 2> memref<30x20x10xf32, 1>)
return
}

// -----

// CHECK-LABEL: @permute_pack_tricyle_permute
// CHECK-DAG: %[[DST:.*]] = amdaie.logicalobjectfifo.from_memref {{.*}} !amdaie.logicalobjectfifo<memref<4x2x6x5x5x5xf32, 2>>
// CHECK-DAG: %[[SRC:.*]] = amdaie.logicalobjectfifo.from_memref {{.*}} !amdaie.logicalobjectfifo<memref<30x20x10xf32, 1>>
// CHECK: amdaie.dma_cpy_nd
// CHECK-SAME: %[[DST]][0, 0, 0, 0, 0, 0] [4, 2, 6, 5, 5, 5] [1500, 750, 125, 25, 5, 1]
// CHECK-SAME: %[[SRC]][0, 0, 0, 0, 0, 0] [4, 2, 6, 5, 5, 5] [50, 5, 1000, 200, 10, 1]
func.func @permute_pack_tricyle_permute(){
%dst = memref.alloc() : memref<4x2x6x5x5x5xf32, 2>
%src = memref.alloc() : memref<30x20x10xf32, 1>
iree_linalg_ext.pack %src outer_dims_perm = [1, 2, 0] inner_dims_pos = [0, 1, 2] inner_tiles = [5, 5, 5] into %dst : (memref<30x20x10xf32, 1> memref<4x2x6x5x5x5xf32, 2>)
return
}

// -----

// CHECK-LABEL: @permute_unpack_tricyle_permute
// CHECK-DAG: %[[DST:.*]] = amdaie.logicalobjectfifo.from_memref {{.*}} !amdaie.logicalobjectfifo<memref<30x20x10xf32, 1>>
// CHECK-DAG: %[[SRC:.*]] = amdaie.logicalobjectfifo.from_memref {{.*}} !amdaie.logicalobjectfifo<memref<4x2x6x5x5x5xf32, 2>>
// CHECK: amdaie.dma_cpy_nd
// CHECK-SAME: %[[DST]][0, 0, 0] [30, 20, 10] [200, 10, 1]
// CHECK-SAME: %[[SRC]][0, 0, 0, 0, 0, 0] [6, 5, 4, 5, 2, 5] [125, 25, 1500, 5, 750, 1]
func.func @permute_unpack_tricyle_permute(){
%dst = memref.alloc() : memref<30x20x10xf32, 1>
%src = memref.alloc() : memref<4x2x6x5x5x5xf32, 2>
iree_linalg_ext.unpack %src outer_dims_perm = [1, 2, 0] inner_dims_pos = [0, 1, 2] inner_tiles = [5, 5, 5] into %dst : (memref<4x2x6x5x5x5xf32, 2> memref<30x20x10xf32, 1>)
return
}



Loading