Skip to content

Commit 504f331

Browse files
authored
[SCFToCalyx] Re-initialize IRmapping at the start of each loop iteration to map block arguments in region-based operations (#8075)
1 parent fab24cb commit 504f331

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/Conversion/SCFToCalyx/SCFToCalyx.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,6 @@ class BuildParGroups : public calyx::FuncOpPartialLoweringPattern {
18941894
OpBuilder insideBuilder(newParOp);
18951895
Block *currBlock = nullptr;
18961896
auto &region = newParOp.getRegion();
1897-
IRMapping operandMap;
18981897

18991898
// extract lower bounds, upper bounds, and steps as integer index values
19001899
SmallVector<int64_t> lbVals, ubVals, stepVals;
@@ -1920,6 +1919,11 @@ class BuildParGroups : public calyx::FuncOpPartialLoweringPattern {
19201919
SmallVector<int64_t> indices = lbVals;
19211920

19221921
while (true) {
1922+
// Each iteration starts with a fresh mapping, so each new block’s
1923+
// argument of a region-based operation (such as `scf.for`) get re-mapped
1924+
// independently.
1925+
IRMapping operandMap;
1926+
19231927
// Create a new block in the region for the current combination of indices
19241928
currBlock = &region.emplaceBlock();
19251929
insideBuilder.setInsertionPointToEnd(currBlock);

test/Conversion/SCFToCalyx/convert_simple.mlir

+51
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,54 @@ module {
530530
return %res : si32
531531
}
532532
}
533+
534+
// Test parallel op lowering when it has region-based nested ops, such as `scf.for`
535+
536+
// -----
537+
538+
// CHECK: calyx.control {
539+
// CHECK: calyx.seq {
540+
// CHECK: calyx.par {
541+
// CHECK: calyx.seq {
542+
// CHECK: calyx.enable @init_for_0_induction_var
543+
// CHECK: calyx.repeat 2 {
544+
// CHECK: calyx.seq {
545+
// CHECK: calyx.enable @bb0_0
546+
// CHECK: calyx.enable @bb0_1
547+
// CHECK: calyx.enable @incr_for_0_induction_var
548+
// CHECK: }
549+
// CHECK: }
550+
// CHECK: }
551+
// CHECK: calyx.seq {
552+
// CHECK: calyx.enable @init_for_1_induction_var
553+
// CHECK: calyx.repeat 2 {
554+
// CHECK: calyx.seq {
555+
// CHECK: calyx.enable @bb0_2
556+
// CHECK: calyx.enable @bb0_3
557+
// CHECK: calyx.enable @incr_for_1_induction_var
558+
// CHECK: }
559+
// CHECK: }
560+
// CHECK: }
561+
// CHECK: }
562+
// CHECK: }
563+
// CHECK: }
564+
// CHECK: } {toplevel}
565+
566+
module {
567+
func.func @main() {
568+
%c2 = arith.constant 2 : index
569+
%c1 = arith.constant 1 : index
570+
%c0 = arith.constant 0 : index
571+
%alloc = memref.alloc() : memref<6xi32>
572+
%alloc_1 = memref.alloc() : memref<6xi32>
573+
scf.parallel (%arg2) = (%c0) to (%c2) step (%c1) {
574+
scf.for %arg3 = %c0 to %c2 step %c1 {
575+
%1 = memref.load %alloc_1[%arg3] : memref<6xi32>
576+
%2 = arith.shli %arg2, %c1 : index
577+
memref.store %1, %alloc[%2] : memref<6xi32>
578+
}
579+
scf.reduce
580+
}
581+
return
582+
}
583+
}

0 commit comments

Comments
 (0)