Skip to content

Commit e86fe36

Browse files
committed
[MLIR] Allow Affine scalar replacement to handle inner operations
Affine scalar replacement (and other affine passes, though not fixed here) don't properly handle operations with nested regions. This patch fixes the pass and two affine utilities to function properly given a non-affine internal region This patch prevents the pass from throwing an internal compiler error when running on the added test case. Differential Revision: https://reviews.llvm.org/D105058
1 parent 78e70ce commit e86fe36

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

mlir/lib/Analysis/Utils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ void mlir::getLoopIVs(Operation &op, SmallVectorImpl<AffineForOp> *loops) {
3535
AffineForOp currAffineForOp;
3636
// Traverse up the hierarchy collecting all 'affine.for' operation while
3737
// skipping over 'affine.if' operations.
38-
while (currOp && ((currAffineForOp = dyn_cast<AffineForOp>(currOp)) ||
39-
isa<AffineIfOp>(currOp))) {
40-
if (currAffineForOp)
38+
while (currOp) {
39+
if (AffineForOp currAffineForOp = dyn_cast<AffineForOp>(currOp))
4140
loops->push_back(currAffineForOp);
4241
currOp = currOp->getParentOp();
4342
}
@@ -54,8 +53,9 @@ void mlir::getEnclosingAffineForAndIfOps(Operation &op,
5453

5554
// Traverse up the hierarchy collecting all `affine.for` and `affine.if`
5655
// operations.
57-
while (currOp && (isa<AffineIfOp, AffineForOp>(currOp))) {
58-
ops->push_back(currOp);
56+
while (currOp) {
57+
if (isa<AffineIfOp, AffineForOp>(currOp))
58+
ops->push_back(currOp);
5959
currOp = currOp->getParentOp();
6060
}
6161
std::reverse(ops->begin(), ops->end());

mlir/test/Dialect/Affine/scalrep.mlir

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,34 @@ func @redundant_store_elim_fail(%out : memref<512xf32>) {
670670
}
671671
return
672672
}
673-
674673
// CHECK: affine.for
675674
// CHECK-NEXT: affine.store
676675
// CHECK-NEXT: "test.use"
677676
// CHECK-NEXT: affine.store
678677
// CHECK-NEXT: }
679678

679+
// CHECK-LABEL: @with_inner_ops
680+
func @with_inner_ops(%arg0: memref<?xf64>, %arg1: memref<?xf64>, %arg2: i1) {
681+
%cst = constant 0.000000e+00 : f64
682+
%cst_0 = constant 3.140000e+00 : f64
683+
%cst_1 = constant 1.000000e+00 : f64
684+
affine.for %arg3 = 0 to 28 {
685+
affine.store %cst, %arg1[%arg3] : memref<?xf64>
686+
affine.store %cst_0, %arg1[%arg3] : memref<?xf64>
687+
%0 = scf.if %arg2 -> (f64) {
688+
scf.yield %cst_1 : f64
689+
} else {
690+
%1 = affine.load %arg1[%arg3] : memref<?xf64>
691+
scf.yield %1 : f64
692+
}
693+
affine.store %0, %arg0[%arg3] : memref<?xf64>
694+
}
695+
return
696+
}
697+
698+
// CHECK: %[[pi:.+]] = constant 3.140000e+00 : f64
699+
// CHECK: %{{.*}} = scf.if %arg2 -> (f64) {
700+
// CHECK: scf.yield %{{.*}} : f64
701+
// CHECK: } else {
702+
// CHECK: scf.yield %[[pi]] : f64
703+
// CHECK: }

0 commit comments

Comments
 (0)