[Lower SCF/Standard to Calyx] Add support for `scf.if`
- Dominant language
- C++
- Stars
- 2.2k
- Forks
- 524
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 46
Description
I was wondering if it would be a good idea to add lowering support of `scf::IfOp` in `createSCFToCalyxPass` .
`createSCFToCalyxPass` claims to handle `BranchOpInterface` but when it encounters a `scf::YieldOp` it asserts if it's not `scf::WhileOp`.
I've also tried using `createConvertSCFToCFPass` to lower `scf::IfOp` into `cf` and it works well for `scf::IfOp`, but the problem is that it also converts `scf::WhileOp` into a `cf::BranchOp` with back-edges, so `createSCFToCalyxPass` fails with:
```
error: CFG backedge detected. Loops must be raised to 'scf.while' or 'scf.for' operations.
```
Therefore I think it's better to support `scf::IfOp` directly.
Here's `simple_arith_if.mlir`, a port of [simple_arith.mlir](https://github.com/llvm/circt/blob/main/integration_test/Dialect/Calyx/simple_arith.mlir ) to use `scf::IfOp` instead of `cf::CondBranchOp` , which can help test the lowering of `scf::IfOp` when implemented:
```llvm
func.func @main(%arg0 : i32, %arg1 : i32) -> i32 {
%0 = arith.cmpi slt, %arg0, %arg1 : i32
%1 = scf.if %0 -> i32 {
%3 = arith.addi %arg0, %arg1 : i32
scf.yield %3 : i32
} else {
scf.yield %arg1 : i32
}
return %1 : i32
}
```
(Thanks to @rachitnigam for the updated code.)
EDIT: You need to run the command:
```
./build/bin/circt-opt --lower-scf-to-calyx test.mlir
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with createSCFToCalyxPass and the provided simple_arith_if.mlir example, then run ./build/bin/circt-opt --lower-scf-to-calyx on the input. Implement direct lowering support for scf::IfOp while retaining scf::WhileOp handling, and verify that the example lowers without the scf::YieldOp assertion or CFG back-edge error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100