Skip to content

Commit 5d001f5

Browse files
committed
[mlir] Fix a bug in Affine LICM.
Currently Affine LICM checks iterOperands and does not hoist out any instruction containing iterOperands. We should check iterArgs instead. Reviewed By: bondhugula Differential Revision: https://reviews.llvm.org/D111090
1 parent b225c5f commit 5d001f5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ bool checkInvarianceOfNestedIfOps(Operation *op, Value indVar,
195195
void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
196196
auto *loopBody = forOp.getBody();
197197
auto indVar = forOp.getInductionVar();
198-
ValueRange iterArgs = forOp.getIterOperands();
198+
ValueRange iterArgs = forOp.getRegionIterArgs();
199199

200200
// This is the place where hoisted instructions would reside.
201201
OpBuilder b(forOp.getOperation());

mlir/test/Dialect/Affine/affine-loop-invariant-code-motion.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,25 @@ func @affine_for_not_invariant(%in : memref<30x512xf32, 1>,
719719

720720
// -----
721721

722+
// CHECK-LABEL: func @use_of_iter_operands_invariant
723+
func @use_of_iter_operands_invariant(%m : memref<10xindex>) {
724+
%sum_1 = constant 0 : index
725+
%v0 = affine.for %arg1 = 0 to 11 iter_args (%prevAccum = %sum_1) -> index {
726+
%prod = muli %sum_1, %sum_1 : index
727+
%newAccum = addi %prevAccum, %prod : index
728+
affine.yield %newAccum : index
729+
}
730+
return
731+
}
732+
733+
// CHECK: constant
734+
// CHECK-NEXT: muli
735+
// CHECK-NEXT: affine.for
736+
// CHECK-NEXT: addi
737+
// CHECK-NEXT: affine.yield
738+
739+
// -----
740+
722741
// CHECK-LABEL: func @use_of_iter_args_not_invariant
723742
func @use_of_iter_args_not_invariant(%m : memref<10xindex>) {
724743
%sum_1 = constant 0 : index

0 commit comments

Comments
 (0)