Skip to content

Commit

Permalink
nested if control
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahanxie353 committed Oct 9, 2024
1 parent 307160c commit 9f77622
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/Conversion/SCFToCalyx/SCFToCalyx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,13 @@ class BuildControl : public calyx::FuncOpPartialLoweringPattern {
rewriter.create<calyx::SeqOp>(ifOp.getThenRegion().getLoc());
auto *thenSeqOpBlock = thenSeqOp.getBodyBlock();

rewriter.setInsertionPointToEnd(thenSeqOpBlock);
auto *thenBlock = &ifOp.getThenRegion().front();
LogicalResult res = buildCFGControl(path, rewriter, thenSeqOpBlock,
/*preBlock=*/block, thenBlock);
if (res.failed())
return res;

rewriter.setInsertionPointToEnd(thenSeqOpBlock);
calyx::GroupOp thenGroup =
getState<ComponentLoweringState>().getThenGroup(ifOp);
rewriter.create<calyx::EnableOp>(thenGroup.getLoc(),
Expand All @@ -1565,8 +1570,13 @@ class BuildControl : public calyx::FuncOpPartialLoweringPattern {
rewriter.create<calyx::SeqOp>(ifOp.getElseRegion().getLoc());
auto *elseSeqOpBlock = elseSeqOp.getBodyBlock();

rewriter.setInsertionPointToEnd(elseSeqOpBlock);
auto *elseBlock = &ifOp.getElseRegion().front();
res = buildCFGControl(path, rewriter, elseSeqOpBlock,
/*preBlock=*/block, elseBlock);
if (res.failed())
return res;

rewriter.setInsertionPointToEnd(elseSeqOpBlock);
calyx::GroupOp elseGroup =
getState<ComponentLoweringState>().getElseGroup(ifOp);
rewriter.create<calyx::EnableOp>(elseGroup.getLoc(),
Expand Down
113 changes: 113 additions & 0 deletions test/Conversion/SCFToCalyx/convert_controlflow.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,116 @@ module {
return %1 : i32
}
}

// -----

// Test nested if ops.

module {
// CHECK-LABEL: calyx.component @example(
// CHECK-SAME: %[[VAL_0:in0]]: i32,
// CHECK-SAME: %[[VAL_1:.*]]: i1 {clk},
// CHECK-SAME: %[[VAL_2:.*]]: i1 {reset},
// CHECK-SAME: %[[VAL_3:.*]]: i1 {go}) -> (
// CHECK-SAME: %[[VAL_4:out0]]: i32,
// CHECK-SAME: %[[VAL_5:.*]]: i1 {done}) {
// CHECK: %[[VAL_6:.*]] = hw.constant 30 : i32
// CHECK: %[[VAL_7:.*]] = hw.constant 20 : i32
// CHECK: %[[VAL_8:.*]] = hw.constant 10 : i32
// CHECK: %[[VAL_9:.*]] = hw.constant 1 : i32
// CHECK: %[[VAL_10:.*]] = hw.constant 2 : i32
// CHECK: %[[VAL_11:.*]] = hw.constant 5 : i32
// CHECK: %[[VAL_12:.*]] = hw.constant true
// CHECK: %[[VAL_13:.*]], %[[VAL_14:.*]], %[[VAL_15:.*]] = calyx.std_eq @std_eq_1 : i32, i32, i1
// CHECK: %[[VAL_16:.*]], %[[VAL_17:.*]], %[[VAL_18:.*]] = calyx.std_eq @std_eq_0 : i32, i32, i1
// CHECK: %[[VAL_19:.*]], %[[VAL_20:.*]], %[[VAL_21:.*]] = calyx.std_add @std_add_0 : i32, i32, i32
// CHECK: %[[VAL_22:.*]], %[[VAL_23:.*]], %[[VAL_24:.*]], %[[VAL_25:.*]], %[[VAL_26:.*]], %[[VAL_27:.*]] = calyx.register @if_res_1_reg : i32, i1, i1, i1, i32, i1
// CHECK: %[[VAL_28:.*]], %[[VAL_29:.*]], %[[VAL_30:.*]], %[[VAL_31:.*]], %[[VAL_32:.*]], %[[VAL_33:.*]] = calyx.register @if_res_0_reg : i32, i1, i1, i1, i32, i1
// CHECK: %[[VAL_34:.*]], %[[VAL_35:.*]], %[[VAL_36:.*]], %[[VAL_37:.*]], %[[VAL_38:.*]], %[[VAL_39:.*]] = calyx.register @ret_arg0_reg : i32, i1, i1, i1, i32, i1
// CHECK: calyx.wires {
// CHECK: calyx.assign %[[VAL_4]] = %[[VAL_38]] : i32
// CHECK: calyx.group @then_br_0 {
// CHECK: calyx.assign %[[VAL_28]] = %[[VAL_7]] : i32
// CHECK: calyx.assign %[[VAL_29]] = %[[VAL_12]] : i1
// CHECK: calyx.group_done %[[VAL_33]] : i1
// CHECK: }
// CHECK: calyx.group @else_br_0 {
// CHECK: calyx.assign %[[VAL_28]] = %[[VAL_6]] : i32
// CHECK: calyx.assign %[[VAL_29]] = %[[VAL_12]] : i1
// CHECK: calyx.group_done %[[VAL_33]] : i1
// CHECK: }
// CHECK: calyx.group @then_br_1 {
// CHECK: calyx.assign %[[VAL_22]] = %[[VAL_8]] : i32
// CHECK: calyx.assign %[[VAL_23]] = %[[VAL_12]] : i1
// CHECK: calyx.group_done %[[VAL_27]] : i1
// CHECK: }
// CHECK: calyx.group @else_br_1 {
// CHECK: calyx.assign %[[VAL_22]] = %[[VAL_32]] : i32
// CHECK: calyx.assign %[[VAL_23]] = %[[VAL_12]] : i1
// CHECK: calyx.group_done %[[VAL_27]] : i1
// CHECK: }
// CHECK: calyx.comb_group @bb0_1 {
// CHECK: calyx.assign %[[VAL_16]] = %[[VAL_21]] : i32
// CHECK: calyx.assign %[[VAL_17]] = %[[VAL_10]] : i32
// CHECK: calyx.assign %[[VAL_19]] = %[[VAL_0]] : i32
// CHECK: calyx.assign %[[VAL_20]] = %[[VAL_9]] : i32
// CHECK: }
// CHECK: calyx.comb_group @bb0_2 {
// CHECK: calyx.assign %[[VAL_13]] = %[[VAL_21]] : i32
// CHECK: calyx.assign %[[VAL_14]] = %[[VAL_11]] : i32
// CHECK: calyx.assign %[[VAL_19]] = %[[VAL_0]] : i32
// CHECK: calyx.assign %[[VAL_20]] = %[[VAL_9]] : i32
// CHECK: }
// CHECK: calyx.group @ret_assign_0 {
// CHECK: calyx.assign %[[VAL_34]] = %[[VAL_26]] : i32
// CHECK: calyx.assign %[[VAL_35]] = %[[VAL_12]] : i1
// CHECK: calyx.group_done %[[VAL_39]] : i1
// CHECK: }
// CHECK: }
// CHECK: calyx.control {
// CHECK: calyx.seq {
// CHECK: calyx.if %[[VAL_18]] with @bb0_1 {
// CHECK: calyx.seq {
// CHECK: calyx.enable @then_br_1
// CHECK: }
// CHECK: } else {
// CHECK: calyx.seq {
// CHECK: calyx.if %[[VAL_15]] with @bb0_2 {
// CHECK: calyx.seq {
// CHECK: calyx.enable @then_br_0
// CHECK: }
// CHECK: } else {
// CHECK: calyx.seq {
// CHECK: calyx.enable @else_br_0
// CHECK: }
// CHECK: }
// CHECK: calyx.enable @else_br_1
// CHECK: }
// CHECK: }
// CHECK: calyx.enable @ret_assign_0
// CHECK: }
// CHECK: }
// CHECK: } {toplevel}
func.func @example(%arg0 : index) -> i32 {
%one = arith.constant 1 : index
%two = arith.constant 2: index
%five = arith.constant 5 : index
%sum = arith.addi %one, %arg0 : index
%cond0 = arith.cmpi eq, %sum, %two : index
%0 = scf.if %cond0 -> i32 {
%1 = arith.constant 10 : i32
scf.yield %1 : i32
} else {
%cond1 = arith.cmpi eq, %sum, %five : index
%4 = scf.if %cond1 -> i32 {
%2 = arith.constant 20 : i32
scf.yield %2 : i32
} else {
%3 = arith.constant 30 : i32
scf.yield %3 : i32
}
scf.yield %4 : i32
}
return %0 : i32
}
}

0 comments on commit 9f77622

Please sign in to comment.