Skip to content

Commit d056c75

Browse files
authored
[mlir][scf] Fix unrolling when the yielded value is defined above the loop. (#122177)
1 parent fb03ce7 commit d056c75

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

mlir/lib/Dialect/SCF/Utils/Utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static void generateUnrolledLoop(
361361

362362
// Update yielded values.
363363
for (unsigned i = 0, e = lastYielded.size(); i < e; i++)
364-
lastYielded[i] = operandMap.lookup(yieldedValues[i]);
364+
lastYielded[i] = operandMap.lookupOrDefault(yieldedValues[i]);
365365
}
366366

367367
// Make sure we annotate the Ops in the original body. We do this last so that

mlir/test/Dialect/SCF/loop-unroll.mlir

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,32 @@ func.func @static_loop_unroll_with_integer_iv() -> (f32, f32) {
489489
// UNROLL-BY-3-NEXT: scf.yield %[[EADD]], %[[EMUL]] : f32, f32
490490
// UNROLL-BY-3-NEXT: }
491491
// UNROLL-BY-3-NEXT: return %[[EFOR]]#0, %[[EFOR]]#1 : f32, f32
492+
493+
// -----
494+
495+
// Test loop unrolling when the yielded value is defined above the loop.
496+
func.func @loop_unroll_static_yield_value_defined_above(%init: i32) {
497+
%c42 = arith.constant 42 : i32
498+
%c0 = arith.constant 0 : index
499+
%c1 = arith.constant 1 : index
500+
%c4 = arith.constant 4 : index
501+
%103:2 = scf.for %i = %c0 to %c4 step %c1
502+
iter_args(%iter1 = %c42, %iter2 = %init) -> (i32, i32) {
503+
%0 = arith.andi %iter2, %iter1 : i32
504+
scf.yield %0, %init : i32, i32
505+
}
506+
return
507+
}
508+
// UNROLL-OUTER-BY-2-LABEL: @loop_unroll_static_yield_value_defined_above(
509+
// UNROLL-OUTER-BY-2-SAME: %[[INIT:.*]]: i32) {
510+
// UNROLL-OUTER-BY-2-DAG: %[[C42:.*]] = arith.constant 42 : i32
511+
// UNROLL-OUTER-BY-2-DAG: %[[C0:.*]] = arith.constant 0 : index
512+
// UNROLL-OUTER-BY-2-DAG: %[[C4:.*]] = arith.constant 4 : index
513+
// UNROLL-OUTER-BY-2-DAG: %[[C2:.*]] = arith.constant 2 : index
514+
// UNROLL-OUTER-BY-2: scf.for %{{.*}} = %[[C0]] to %[[C4]] step %[[C2]]
515+
// UNROLL-OUTER-BY-2-SAME: iter_args(%[[ITER1:.*]] = %[[C42]],
516+
// UNROLL-OUTER-BY-2-SAME: %[[ITER2:.*]] = %[[INIT]])
517+
// UNROLL-OUTER-BY-2: %[[SUM:.*]] = arith.andi %[[ITER2]], %[[ITER1]]
518+
// UNROLL-OUTER-BY-2: %[[SUM1:.*]] = arith.andi %[[INIT]], %[[SUM]]
519+
// UNROLL-OUTER-BY-2: scf.yield %[[SUM1]], %[[INIT]] : i32, i32
520+

0 commit comments

Comments
 (0)