Skip to content

Commit

Permalink
[SCFToCalyx] Re-initialize IRmapping at the start of each loop iterat…
Browse files Browse the repository at this point in the history
…ion to map block arguments in region-based operations (#8075)
  • Loading branch information
jiahanxie353 authored Jan 13, 2025
1 parent fab24cb commit 504f331
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Conversion/SCFToCalyx/SCFToCalyx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,6 @@ class BuildParGroups : public calyx::FuncOpPartialLoweringPattern {
OpBuilder insideBuilder(newParOp);
Block *currBlock = nullptr;
auto &region = newParOp.getRegion();
IRMapping operandMap;

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

while (true) {
// Each iteration starts with a fresh mapping, so each new block’s
// argument of a region-based operation (such as `scf.for`) get re-mapped
// independently.
IRMapping operandMap;

// Create a new block in the region for the current combination of indices
currBlock = &region.emplaceBlock();
insideBuilder.setInsertionPointToEnd(currBlock);
Expand Down
51 changes: 51 additions & 0 deletions test/Conversion/SCFToCalyx/convert_simple.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,54 @@ module {
return %res : si32
}
}

// Test parallel op lowering when it has region-based nested ops, such as `scf.for`

// -----

// CHECK: calyx.control {
// CHECK: calyx.seq {
// CHECK: calyx.par {
// CHECK: calyx.seq {
// CHECK: calyx.enable @init_for_0_induction_var
// CHECK: calyx.repeat 2 {
// CHECK: calyx.seq {
// CHECK: calyx.enable @bb0_0
// CHECK: calyx.enable @bb0_1
// CHECK: calyx.enable @incr_for_0_induction_var
// CHECK: }
// CHECK: }
// CHECK: }
// CHECK: calyx.seq {
// CHECK: calyx.enable @init_for_1_induction_var
// CHECK: calyx.repeat 2 {
// CHECK: calyx.seq {
// CHECK: calyx.enable @bb0_2
// CHECK: calyx.enable @bb0_3
// CHECK: calyx.enable @incr_for_1_induction_var
// CHECK: }
// CHECK: }
// CHECK: }
// CHECK: }
// CHECK: }
// CHECK: }
// CHECK: } {toplevel}

module {
func.func @main() {
%c2 = arith.constant 2 : index
%c1 = arith.constant 1 : index
%c0 = arith.constant 0 : index
%alloc = memref.alloc() : memref<6xi32>
%alloc_1 = memref.alloc() : memref<6xi32>
scf.parallel (%arg2) = (%c0) to (%c2) step (%c1) {
scf.for %arg3 = %c0 to %c2 step %c1 {
%1 = memref.load %alloc_1[%arg3] : memref<6xi32>
%2 = arith.shli %arg2, %c1 : index
memref.store %1, %alloc[%2] : memref<6xi32>
}
scf.reduce
}
return
}
}

0 comments on commit 504f331

Please sign in to comment.