Skip to content

Commit e110abc

Browse files
[mlir][affine] Use iter argument replace init when delete loop in the coalesceLoops function (#169514)
Fix #169483 by using iter argument replace init when delete loop in the coalesceLoops function.
1 parent 75aa01b commit e110abc

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,12 @@ LogicalResult mlir::affine::coalesceLoops(MutableArrayRef<AffineForOp> loops) {
17111711
outermost.getBody()->getOperations().splice(
17121712
Block::iterator(secondOutermostLoop.getOperation()),
17131713
innermost.getBody()->getOperations());
1714+
for (auto [iter, init] :
1715+
llvm::zip_equal(secondOutermostLoop.getRegionIterArgs(),
1716+
secondOutermostLoop.getInits())) {
1717+
iter.replaceAllUsesWith(init);
1718+
iter.dropAllUses();
1719+
}
17141720
secondOutermostLoop.erase();
17151721
return success();
17161722
}

mlir/test/Dialect/Affine/loop-coalescing.mlir

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,31 @@ func.func @test_loops_do_not_get_coalesced() {
416416
// CHECK-NEXT: }
417417
// CHECK-NEXT: }
418418
// CHECK-NEXT: return
419+
420+
// -----
421+
422+
// CHECK-LABEL: func @inner_loop_has_iter_args
423+
// CHECK-SAME: %[[ALLOC:.*]]: memref<?xi64>)
424+
func.func @inner_loop_has_iter_args(%alloc : memref<?xi64>) {
425+
%c17 = arith.constant 17 : index
426+
affine.for %arg0 = 0 to 79 {
427+
%0 = affine.for %arg1 = 0 to 64 iter_args(%arg2 = %alloc) -> (memref<?xi64>) {
428+
%1 = arith.remui %arg1, %c17 : index
429+
%2 = arith.index_cast %arg1 : index to i64
430+
memref.store %2, %arg2[%1] : memref<?xi64>
431+
affine.yield %arg2 : memref<?xi64>
432+
}
433+
}
434+
return
435+
}
436+
437+
// CHECK: %[[CONSTANT_0:.*]] = arith.constant 17 : index
438+
// CHECK: %[[APPLY_0:.*]] = affine.apply affine_map<() -> (79)>()
439+
// CHECK: %[[APPLY_1:.*]] = affine.apply affine_map<() -> (64)>()
440+
// CHECK: %[[APPLY_2:.*]] = affine.apply affine_map<(d0)[s0] -> (d0 * s0)>(%[[APPLY_0]]){{\[}}%[[APPLY_1]]]
441+
// CHECK: affine.for %[[IV:.*]] = 0 to %[[APPLY_2]] {
442+
// CHECK: %[[APPLY_3:.*]] = affine.apply affine_map<(d0)[s0] -> (d0 mod s0)>(%[[IV]]){{\[}}%[[APPLY_1]]]
443+
// CHECK: %[[REMUI_0:.*]] = arith.remui %[[APPLY_3]], %[[CONSTANT_0]] : index
444+
// CHECK: %[[INDEX_CAST_0:.*]] = arith.index_cast %[[APPLY_3]] : index to i64
445+
// CHECK: memref.store %[[INDEX_CAST_0]], %[[ALLOC]]{{\[}}%[[REMUI_0]]] : memref<?xi64>
446+
// CHECK: }

0 commit comments

Comments
 (0)