Skip to content

Commit 38ad6b1

Browse files
authored
[mlir][Affine] Fix crash in affine-loop-fusion pass by guarding against an empty op list (#144841)
Related: #139231 This patch fixes a crash in the affine-loop-fusion pass when `getInnermostCommonLoop` is called with an empty list of operations. The function expects at least one op to analyze, and passing an empty array of ops causes an assertion failure. This change ensures the pass checks for an empty op array before calling `getInnermostCommonLoop`. @bondhugula @matthias-springer
1 parent 5491576 commit 38ad6b1

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,8 @@ struct GreedyFusion {
10021002
if (producerConsumerMemrefs.count(
10031003
cast<AffineWriteOpInterface>(op).getMemRef()))
10041004
dstMemrefOps.push_back(op);
1005+
if (dstMemrefOps.empty())
1006+
continue;
10051007
unsigned dstLoopDepthTest =
10061008
getInnermostCommonLoopDepth(dstMemrefOps) - numSurroundingLoops;
10071009

mlir/test/Dialect/Affine/loop-fusion-4.mlir

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,80 @@ func.func @unrolled(%arg0: memref<2x4xf32>, %arg1: memref<1x2x4xf32>) {
666666
// PRODUCER-CONSUMER-MAXIMAL: affine.load %{{.*}}[0, %{{.*}}, %{{.*}}]
667667
return
668668
}
669+
670+
// -----
671+
672+
// Exercises fix for crash reported at https://github.com/llvm/llvm-project/issues/139231
673+
674+
#map = affine_map<(d0, d1) -> (d0 + d1)>
675+
#map1 = affine_map<(d0, d1) -> (d0 * 2 + d1 * 2)>
676+
module {
677+
func.func @zero_candidates() {
678+
%cst = arith.constant 2.221140e+03 : f32
679+
%cst_0 = arith.constant 2.606200e+03 : f32
680+
%cst_1 = arith.constant 3.224000e+03 : f32
681+
%cst_2 = arith.constant 0.000000e+00 : f32
682+
%alloc = memref.alloc() {alignment = 64 : i64} : memref<3x7x5x6xf32>
683+
affine.for %arg0 = 0 to 3 {
684+
affine.for %arg1 = 0 to 7 {
685+
affine.for %arg2 = 0 to 5 {
686+
affine.for %arg3 = 0 to 6 {
687+
affine.store %cst_1, %alloc[%arg0, %arg1, %arg2, %arg3] : memref<3x7x5x6xf32>
688+
}
689+
}
690+
}
691+
}
692+
%alloc_3 = memref.alloc() {alignment = 64 : i64} : memref<3x10x7x6xf32>
693+
%subview = memref.subview %alloc_3[0, 2, 1, 0] [3, 7, 5, 6] [1, 1, 1, 1] : memref<3x10x7x6xf32> to memref<3x7x5x6xf32, strided<[420, 42, 6, 1], offset: 90>>
694+
memref.copy %alloc, %subview : memref<3x7x5x6xf32> to memref<3x7x5x6xf32, strided<[420, 42, 6, 1], offset: 90>>
695+
%alloc_4 = memref.alloc() {alignment = 64 : i64} : memref<3x10x3x6x1xf32>
696+
affine.for %arg0 = 0 to 3 {
697+
affine.for %arg1 = 0 to 10 {
698+
affine.for %arg2 = 0 to 3 {
699+
affine.for %arg3 = 0 to 6 {
700+
affine.for %arg4 = 0 to 1 {
701+
affine.store %cst_2, %alloc_4[%arg0, %arg1, %arg2, %arg3, %arg4] : memref<3x10x3x6x1xf32>
702+
}
703+
}
704+
}
705+
}
706+
}
707+
affine.for %arg0 = 0 to 3 {
708+
affine.for %arg1 = 0 to 10 {
709+
affine.for %arg2 = 0 to 3 {
710+
affine.for %arg3 = 0 to 6 {
711+
affine.for %arg4 = 0 to 1 {
712+
affine.for %arg5 = 0 to 1 {
713+
affine.for %arg6 = 0 to 2 {
714+
%0 = affine.apply #map(%arg1, %arg5)
715+
%1 = affine.apply #map1(%arg2, %arg6)
716+
%2 = affine.load %alloc_3[%arg0, %0, %1, %arg3] : memref<3x10x7x6xf32>
717+
%3 = affine.load %alloc_4[%arg0, %arg1, %arg2, %arg3, %arg4] : memref<3x10x3x6x1xf32>
718+
%4 = arith.mulf %2, %cst_0 : f32
719+
%5 = arith.addf %3, %4 : f32
720+
affine.store %5, %alloc_4[%arg0, %arg1, %arg2, %arg3, %arg4] : memref<3x10x3x6x1xf32>
721+
}
722+
}
723+
}
724+
}
725+
}
726+
}
727+
}
728+
%alloc_5 = memref.alloc() {alignment = 64 : i64} : memref<3x10x3x6xf32>
729+
%expand_shape = memref.expand_shape %alloc_5 [[0], [1], [2], [3, 4]] output_shape [3, 10, 3, 6, 1] : memref<3x10x3x6xf32> into memref<3x10x3x6x1xf32>
730+
affine.for %arg0 = 0 to 3 {
731+
affine.for %arg1 = 0 to 10 {
732+
affine.for %arg2 = 0 to 3 {
733+
affine.for %arg3 = 0 to 6 {
734+
affine.for %arg4 = 0 to 1 {
735+
%0 = affine.load %alloc_4[%arg0, %arg1, %arg2, %arg3, %arg4] : memref<3x10x3x6x1xf32>
736+
%1 = arith.addf %0, %cst : f32
737+
affine.store %1, %expand_shape[%arg0, %arg1, %arg2, %arg3, %arg4] : memref<3x10x3x6x1xf32>
738+
}
739+
}
740+
}
741+
}
742+
}
743+
return
744+
}
745+
}

0 commit comments

Comments
 (0)