google / google/heir

Adding support for for-loops with secret length

Open
#1,929 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
MLIR
Stars
906
Forks
171
Avg merge
4d 12h
Merged PRs (30d)
32

Description

In general, it is not possible to compile for-loops, where the length of the loop is secret. To avoid this issue one can annotate an upper and lower bound for for-loops. Here is an example to compute the power function by applying multiplications in a for-loop.

```
module {
func.func @power(%arg0: i32, %arg1: i32) -> i32 {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c1_i32 = arith.constant 1 : i32
%0 = arith.index_cast %arg1 : i32 to index
%1 = scf.for %arg2 = %c0 to %0 step %c1 iter_args(%arg3 = %c1_i32) -> (i32) {
%2 = arith.muli %arg3, %arg0 : i32
scf.yield %2 : i32
} {lower = 0, upper = 6}
return %1 : i32
}
}
```

Currently, when the program is compiled with:
`bazel run @heir//tools:heir-opt -- --secretize --mlir-to-bgv --mlir-print-ir-after-all --mlir-print-ir-tree-dir=/tmp/mlir $PWD/test_ks_test/power.mlir`

Unrolling the loop into multiple if statements does work fine, but there are two other issues:
- arith.index_cast is not supported during lowering
- arith.cmpi/arith.select have no proper implementation

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.