diff options
| author | Uday Bondhugula <uday@polymagelabs.com> | 2021-07-22 07:09:53 +0530 |
|---|---|---|
| committer | Uday Bondhugula <uday@polymagelabs.com> | 2021-07-22 08:11:40 +0530 |
| commit | 795e726f5f15a6f07d146255721fe3d392c6a618 (patch) | |
| tree | ac9a41fc2e077a601714b727c586b5bfc44887b1 | |
| parent | 9e5c5afc7ee24ace168418138c99dcede357cd50 (diff) | |
| download | llvm-795e726f5f15a6f07d146255721fe3d392c6a618.zip llvm-795e726f5f15a6f07d146255721fe3d392c6a618.tar.gz llvm-795e726f5f15a6f07d146255721fe3d392c6a618.tar.bz2 | |
[MLIR] Fix affine.for empty loop body folder
Fix affine.for empty loop body folder in the presence of yield values.
The existing pattern ignored iter_args/yield values and thus crashed
when yield values had uses.
Reviewed By: mehdi_amini
Differential Revision: https://reviews.llvm.org/D106121
| -rw-r--r-- | mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 6 | ||||
| -rw-r--r-- | mlir/test/Dialect/Affine/canonicalize.mlir | 14 |
2 files changed, 13 insertions, 7 deletions
diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp index b1721cd..370084d 100644 --- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp +++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp @@ -1628,7 +1628,8 @@ static LogicalResult canonicalizeLoopBounds(AffineForOp forOp) { } namespace { -/// This is a pattern to fold trivially empty loops. +/// This is a pattern to fold trivially empty loop bodies. +/// TODO: This should be moved into the folding hook. struct AffineForEmptyLoopFolder : public OpRewritePattern<AffineForOp> { using OpRewritePattern<AffineForOp>::OpRewritePattern; @@ -1637,7 +1638,8 @@ struct AffineForEmptyLoopFolder : public OpRewritePattern<AffineForOp> { // Check that the body only contains a yield. if (!llvm::hasSingleElement(*forOp.getBody())) return failure(); - rewriter.eraseOp(forOp); + // The initial values of the iteration arguments would be the op's results. + rewriter.replaceOp(forOp, forOp.getIterOperands()); return success(); } }; diff --git a/mlir/test/Dialect/Affine/canonicalize.mlir b/mlir/test/Dialect/Affine/canonicalize.mlir index f12a0ca..3c55431 100644 --- a/mlir/test/Dialect/Affine/canonicalize.mlir +++ b/mlir/test/Dialect/Affine/canonicalize.mlir @@ -460,14 +460,18 @@ func @constant_fold_bounds(%N : index) { // ----- -// CHECK-LABEL: func @fold_empty_loop() { -func @fold_empty_loop() { - // CHECK-NOT: affine.for +// CHECK-LABEL: func @fold_empty_loops() +func @fold_empty_loops() -> index { + %c0 = constant 0 : index affine.for %i = 0 to 10 { } - return + %res = affine.for %i = 0 to 10 iter_args(%arg = %c0) -> index { + affine.yield %arg : index + } + // CHECK-NEXT: %[[zero:.*]] = constant 0 + // CHECK-NEXT: return %[[zero]] + return %res : index } -// CHECK: return // ----- |
