[SAOC 2026] MLIR support Weekly Update #10
Asadbek
aosindarov at gmail.com
Thu Nov 26 17:39:12 UTC 2026
Hello everyone,
This week I have been working on Milestone 3, lowering loops to
the scf dialect.
Before starting on loops I restructured gen/mlir folder to mirror
how the LLVM backend is organized. Previously everything was in
irgen.cpp and toir.cpp. Now declarations, functions, statements
and expressions each have their own file. It is much easier to
find and navigate things around now.
For loops I decided to use two different ops. `scf.for` is the
counted loop with a lower bound, upper bound and a positive step
and `scf.while` is the general loop and can express anything. So
the backend first tries to prove that a D `for` loop is a
counting loop and emits `scf.for`, otherwise it falls back to
`scf.while`.
I have implemented small loop analysis [0] on the D AST. A `for`
loop is a counting loop if its condition is `var < bound` with a
signed integer `var`, `bound` is an integer literal or a local
variable of the same type, the increment is `var++` or `var += c`
with a constant `c > 0`, and the loop body never writes to `var`
or `bound` and has no `break`, `continue` or `return` because scf
dialect's `scf.for` does not support early exits. The last part
is checked by a small visitor over the body that rejects anything
it does not explicitly know, so the analysis can only be wrong by
rejecting a loop, never by accepting a bad one. Rejected loops
just take the `scf.while` path, which is always correct.
Interestingly, this covers the most common D loop, `foreach (i; 0
.. n)`, because the frontend rewrites it to `for (; __key <
__limit; __key += 1)`.
Here is a small sample:
```d
int sum(int n)
{
int s = 0;
foreach (i; 0 .. n)
s += i;
return s;
}
```
and the emitted MLIR for it:
```mlir
func.func @_D4test3sumFiZi(%arg0: i32) -> i32 {
%alloca = memref.alloca() : memref<i32>
%alloca_0 = memref.alloca() : memref<i32>
%alloca_1 = memref.alloca() : memref<i32>
%alloca_2 = memref.alloca() : memref<i32>
%alloca_3 = memref.alloca() : memref<i32>
memref.store %arg0, %alloca[] : memref<i32>
%c0_i32 = arith.constant 0 : i32
memref.store %c0_i32, %alloca_0[] : memref<i32>
%c0_i32_4 = arith.constant 0 : i32
memref.store %c0_i32_4, %alloca_1[] : memref<i32>
%0 = memref.load %alloca[] : memref<i32>
memref.store %0, %alloca_2[] : memref<i32>
%1 = memref.load %alloca_1[] : memref<i32>
%2 = memref.load %alloca_2[] : memref<i32>
%c1_i32 = arith.constant 1 : i32
%3 = scf.for %arg1 = %1 to %2 step %c1_i32 iter_args(%arg2 =
%1) -> (i32) : i32 {
memref.store %arg1, %alloca_1[] : memref<i32>
%5 = memref.load %alloca_1[] : memref<i32>
memref.store %5, %alloca_3[] : memref<i32>
%6 = memref.load %alloca_3[] : memref<i32>
%7 = memref.load %alloca_0[] : memref<i32>
%8 = arith.addi %7, %6 : i32
memref.store %8, %alloca_0[] : memref<i32>
%9 = arith.addi %arg1, %c1_i32 : i32
scf.yield %9 : i32
}
memref.store %3, %alloca_1[] : memref<i32>
%4 = memref.load %alloca_0[] : memref<i32>
return %4 : i32
}
```
The five allocas in sum are the parameter `n`, `s`, `__key`,
`__limit` and the per-iteration `i` that the `foreach` rewrite
introduces. As before, after the lowering and -O2 LLVM cleans all
of it up. For sum it even replaces the loop with the closed form
`n*(n-1)/2`:
```llvm
define i32 @_D4test3sumFiZi(i32 %0) local_unnamed_addr #0 {
%2 = icmp sgt i32 %0, 0 ; [#uses =
1]
br i1 %2, label %.lr.ph.preheader, label %._crit_edge
.lr.ph.preheader: ; preds = %1
%3 = add nsw i32 %0, -1 ; n-1
%4 = zext nneg i32 %3 to i33 ; (n-1) widened to 33 bits
%5 = add nsw i32 %0, -2 ; n-2
%6 = zext i32 %5 to i33 ; (n-2) widened
%7 = mul i33 %4, %6 ; (n-1)*(n-2)
%8 = lshr i33 %7, 1 ; /2
%9 = trunc nuw i33 %8 to i32
%10 = add i32 %0, %9 ; n + (n-1)(n-2)/2
%11 = add i32 %10, -1 ; ... - 1
br label %._crit_edge
._crit_edge: ; preds =
%.lr.ph.preheader, %1
%.0.lcssa = phi i32 [ 0, %1 ], [ %11, %.lr.ph.preheader ] ;
[#uses = 1, type = i32]
ret i32 %.0.lcssa
}
```
I am still working on finishing lowering to `scf.while` fallback
for the remaining loop shapes. After that I want to try the
scf.parallel [1] experiment on a counting loop.
One design question I am thinking about is whether loop structure
should be detected on the AST like now, or recovered later on the
IR with MLIR's `scf.while` to `scf.for` uplifting [2] (although I
think uplifting goes against the promise of MLIR's progressive
lowering). I would be interested to hear opinions on this.
The work is on the same public branch [3].
Feedback and questions are welcome!
Best,
Asadbek
[0]
https://github.com/asindarov/ldc/blob/mlir-func-and-arith-dialect-support/gen/mlir/loopanalysis.cpp
[1]
https://mlir.llvm.org/docs/Dialects/SCFDialect/#scfparallel-scfparallelop
[2]
https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/SCF/Transforms/UpliftWhileToFor.cpp
[3]
https://github.com/asindarov/ldc/tree/mlir-func-and-arith-dialect-support
More information about the Digitalmars-d
mailing list