[SAOC 2026] MLIR support Weekly Update #8

Asadbek aosindarov at gmail.com
Fri Nov 13 14:38:21 UTC 2026


Hello everyone,

This week, I focused on implementing codegen paths for integer 
and floating-point comparisons, as well as making function 
lowering more mature by adding support for call expressions 
(including forward references and implicit argument conversions).

Since one of our main goals is to showcase what we can do with ML 
frameworks, Nicholas suggested focusing more on floating-point 
support. While implementing float comparisons, I ran into some 
interesting edge cases around NaN semantics and had to carefully 
select between ordered (`olt`, `oeq`) and unordered (`une`) 
predicates in `arith.cmpf`.

With the recent changes, LDC can now compile the following D code:

```d
int   add1(int x)              { return x + 1; }
int   twice(int x)             { return add1(x) + add1(x); }
int   callLater(float a)       { return cast(int) half(a); }
float half(float a)            { return a / 2; }
double convArgs(byte a, int b) { return mixed(a, b); }
double mixed(long a, double b) { return a + b; }
void sideEffect(int a)         {}
void callVoid(int a)           { sideEffect(a); return 
sideEffect(a); }
int   external(int a);
int   callExternal(int a)      { return external(a) * 2; }

bool lt(int a, int b)          { return a < b; }
bool ult(uint a, uint b)       { return a < b; }
bool ge(long a, long b)        { return a >= b; }
bool eq(int a, int b)          { return a == b; }
bool ne(ubyte a, ubyte b)      { return a != b; }

bool flt(float a, float b)     { return a < b; }
bool feq(float a, float b)     { return a == b; }
bool fne(double a, double b)   { return a != b; }
```

into this MLIR output:

```mlir
module {
   func.func @_D4test4add1FiZi(%arg0: i32) -> i32 {
     %c1_i32 = arith.constant 1 : i32
     %0 = arith.addi %arg0, %c1_i32 : i32
     return %0 : i32
   }
   func.func @_D4test5twiceFiZi(%arg0: i32) -> i32 {
     %0 = call @_D4test4add1FiZi(%arg0) : (i32) -> i32
     %1 = call @_D4test4add1FiZi(%arg0) : (i32) -> i32
     %2 = arith.addi %0, %1 : i32
     return %2 : i32
   }
   func.func @_D4test9callLaterFfZi(%arg0: f32) -> i32 {
     %0 = call @_D4test4halfFfZf(%arg0) : (f32) -> f32
     %1 = arith.fptosi %0 : f32 to i32
     return %1 : i32
   }
   func.func @_D4test4halfFfZf(%arg0: f32) -> f32 {
     %cst = arith.constant 2.000000e+00 : f32
     %0 = arith.divf %arg0, %cst : f32
     return %0 : f32
   }
   func.func @_D4test8convArgsFgiZd(%arg0: i8, %arg1: i32) -> f64 {
     %0 = arith.extsi %arg0 : i8 to i64
     %1 = arith.sitofp %arg1 : i32 to f64
     %2 = call @_D4test5mixedFldZd(%0, %1) : (i64, f64) -> f64
     return %2 : f64
   }
   func.func @_D4test5mixedFldZd(%arg0: i64, %arg1: f64) -> f64 {
     %0 = arith.sitofp %arg0 : i64 to f64
     %1 = arith.addf %0, %arg1 : f64
     return %1 : f64
   }
   func.func @_D4test10sideEffectFiZv(%arg0: i32) {
     return
   }
   func.func @_D4test8callVoidFiZv(%arg0: i32) {
     call @_D4test10sideEffectFiZv(%arg0) : (i32) -> ()
     call @_D4test10sideEffectFiZv(%arg0) : (i32) -> ()
     return
   }
   func.func @_D4test12callExternalFiZi(%arg0: i32) -> i32 {
     %0 = call @_D4test8externalFiZi(%arg0) : (i32) -> i32
     %c2_i32 = arith.constant 2 : i32
     %1 = arith.muli %0, %c2_i32 : i32
     return %1 : i32
   }
   func.func private @_D4test8externalFiZi(i32) -> i32
   func.func @_D4test2ltFiiZb(%arg0: i32, %arg1: i32) -> i1 {
     %0 = arith.cmpi slt, %arg0, %arg1 : i32
     return %0 : i1
   }
   func.func @_D4test3ultFkkZb(%arg0: i32, %arg1: i32) -> i1 {
     %0 = arith.cmpi ult, %arg0, %arg1 : i32
     return %0 : i1
   }
   func.func @_D4test2geFllZb(%arg0: i64, %arg1: i64) -> i1 {
     %0 = arith.cmpi sge, %arg0, %arg1 : i64
     return %0 : i1
   }
   func.func @_D4test2eqFiiZb(%arg0: i32, %arg1: i32) -> i1 {
     %0 = arith.cmpi eq, %arg0, %arg1 : i32
     return %0 : i1
   }
   func.func @_D4test2neFhhZb(%arg0: i8, %arg1: i8) -> i1 {
     %0 = arith.extui %arg0 : i8 to i32
     %1 = arith.extui %arg1 : i8 to i32
     %2 = arith.cmpi ne, %0, %1 : i32
     return %2 : i1
   }
   func.func @_D4test3fltFffZb(%arg0: f32, %arg1: f32) -> i1 {
     %0 = arith.cmpf olt, %arg0, %arg1 : f32
     return %0 : i1
   }
   func.func @_D4test3feqFffZb(%arg0: f32, %arg1: f32) -> i1 {
     %0 = arith.cmpf oeq, %arg0, %arg1 : f32
     return %0 : i1
   }
   func.func @_D4test3fneFddZb(%arg0: f64, %arg1: f64) -> i1 {
     %0 = arith.cmpf une, %arg0, %arg1 : f64
     return %0 : i1
   }
}
```

Again, this is just a small sample of the codegen test cases I've 
been adding.

This week was originally meant to conclude Milestone 2, but there 
is still a bit of work left, specifically handling local 
variables and scopes. Right now, only function parameters can be 
referenced in a function body as SSA values. I have been reading 
up on the `memref` dialect and plan to handle local variables 
over the weekend by emitting `memref.alloca`/`memref.store` 
operations and relying on MLIR's `mem2reg` pass to promote them 
into SSA (Static Single Assignment) form.

Once local variables are working, I will move straight into 
emitting control flow constructs, which starts off Milestone 3. 
After basic control flow is in place, Nicholas suggested 
experimenting with the `scf` dialect's `ParallelOp` [1] and 
`ReduceOp` [2].

I have pushed my current work to a public branch [3]. It still 
needs some cleanup, which I plan to do during Milestone 4.

Feedback and questions are welcome!

Best,
Asadbek

[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