[SAOC 2026] MLIR support Weekly Update #9
Asadbek
aosindarov at gmail.com
Thu Nov 19 17:59:07 UTC 2026
Hello everyone,
Since the last update I finished the remaining Milestone 2 work
and started on Milestone 3.
I have implemented lowerings for local variables, they will be
spilled to stack memory slots. I have used `memref` MLIR dialect
to implement it. I had to introduce the concept of
`lvalue`/`rvalue` for that. I have just followed how existing LDC
LLVM backend implemented and tried to resemble the
implementation. Also it was interesting to see the D frontend
rewrites because I noticed that it canonicalizes certain patterns
in the code. The easiest way to see these rewrites is through the
online D playground [0] with `vcg-ast` flag.
Also I have implemented the lowering pipeline inside LDC, from
MLIR to LLVM IR then on to object file. Until now I was verifying
the generated MLIR by hand with `mlir-opt` and `mlir-translate`.
That got really tedious. So I implemented the lowering pipeline
as modeled for `LDC_inline_mlir` pragma feature. This means a
BetterC program can now be compiled through the MLIR backend and
run:
ldc2 -betterC --output-mlir --output-o -of=test test.d &&
./test
Here is a small sample:
```d
byte wrap(byte b)
{
b += 127;
return b;
}
int sign(int x)
{
int r = 0;
if (x < 0)
r = -1;
else
r = 1;
return r;
}
float clampPositive(float f)
{
float r = f;
if (f < 0)
r = 0;
return r;
}
```
and the emitted MLIR for it:
```mlir
func.func @_D4test4wrapFgZg(%arg0: i8) -> i8 {
%alloca = memref.alloca() : memref<i8>
memref.store %arg0, %alloca[] : memref<i8>
%c127_i32 = arith.constant 127 : i32
%0 = memref.load %alloca[] : memref<i8>
%1 = arith.extsi %0 : i8 to i32
%2 = arith.addi %1, %c127_i32 : i32
%3 = arith.trunci %2 : i32 to i8
memref.store %3, %alloca[] : memref<i8>
%4 = memref.load %alloca[] : memref<i8>
return %4 : i8
}
func.func @_D4test4signFiZi(%arg0: i32) -> i32 {
%alloca = memref.alloca() : memref<i32>
%alloca_0 = memref.alloca() : memref<i32>
memref.store %arg0, %alloca[] : memref<i32>
%c0_i32 = arith.constant 0 : i32
memref.store %c0_i32, %alloca_0[] : memref<i32>
%0 = memref.load %alloca[] : memref<i32>
%c0_i32_1 = arith.constant 0 : i32
%1 = arith.cmpi slt, %0, %c0_i32_1 : i32
scf.if %1 {
%c-1_i32 = arith.constant -1 : i32
memref.store %c-1_i32, %alloca_0[] : memref<i32>
} else {
%c1_i32 = arith.constant 1 : i32
memref.store %c1_i32, %alloca_0[] : memref<i32>
}
%2 = memref.load %alloca_0[] : memref<i32>
return %2 : i32
}
func.func @_D4test13clampPositiveFfZf(%arg0: f32) -> f32 {
%alloca = memref.alloca() : memref<f32>
%alloca_0 = memref.alloca() : memref<f32>
memref.store %arg0, %alloca[] : memref<f32>
%0 = memref.load %alloca[] : memref<f32>
memref.store %0, %alloca_0[] : memref<f32>
%1 = memref.load %alloca[] : memref<f32>
%cst = arith.constant 0.000000e+00 : f32
%2 = arith.cmpf olt, %1, %cst : f32
scf.if %2 {
%cst_1 = arith.constant 0.000000e+00 : f32
memref.store %cst_1, %alloca_0[] : memref<f32>
}
%3 = memref.load %alloca_0[] : memref<f32>
return %3 : f32
}
```
The memref ops look noisy, but after the in-tree lowering and
`-O2` LLVM folds all of it away:
```llvmir
; [#uses = 0]
; Function Attrs: mustprogress nofree norecurse nosync nounwind
willreturn memory(none)
define i8 @_D4test4wrapFgZg(i8 %0) local_unnamed_addr #0 {
%2 = add i8 %0, 127 ; [#uses = 1]
ret i8 %2
}
; [#uses = 0]
; Function Attrs: mustprogress nofree norecurse nosync nounwind
willreturn memory(none)
define range(i32 -1, 2) i32 @_D4test4signFiZi(i32 %0)
local_unnamed_addr #0 {
%.inv = icmp sgt i32 %0, -1 ; [#uses = 1]
%. = select i1 %.inv, i32 1, i32 -1 ; [#uses = 1]
ret i32 %.
}
; [#uses = 0]
; Function Attrs: mustprogress nofree norecurse nosync nounwind
willreturn memory(none)
define float @_D4test13clampPositiveFfZf(float %0)
local_unnamed_addr #0 {
%2 = fcmp olt float %0, 0.000000e+00 ; [#uses = 1]
%storemerge = select i1 %2, float 0.000000e+00, float %0 ;
[#uses = 1]
ret float %storemerge
}
```
With that, Milestone 2 is complete, I still need to add lit tests
for code gen path. I have setup local tests but they are not yet
public.
In Milestone 3, I will mostly work on lowering structured control
flow using `scf` and `cf` dialects. Also try to experiment with
things that MLIR is good at such as using `scf.parallel`[1] and
`scf.reduce`[2].
The work is on the same public branch [3].
Feedback and questions are welcome!
Best,
Asadbek
[0] https://run.dlang.io/
[1]
https://mlir.llvm.org/docs/Dialects/SCFDialect/#scfparallel-scfparallelop
[2]
https://mlir.llvm.org/docs/Dialects/SCFDialect/#scfreduce-scfreduceop
[3]
https://github.com/asindarov/ldc/tree/mlir-func-and-arith-dialect-support
More information about the Digitalmars-d
mailing list