morok-codegen 0.1.0-alpha.2

Backend code generation for the Morok ML compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! MLIR operation builders for individual UOp operations.
//!
//! Provides helper functions for building arith + LLVM dialect operations.
//! Control flow (Range/End/If/EndIf) is handled directly in the renderer.

use melior::Context;
use melior::dialect::ods::vector;
use melior::dialect::{arith, arith::CmpfPredicate, arith::CmpiPredicate, ods};
use melior::ir::attribute::{FloatAttribute, IntegerAttribute};
use melior::ir::block::BlockLike;
use melior::ir::operation::OperationBuilder;
use melior::ir::r#type::IntegerType;
use melior::ir::{Block, Location, Type, Value};
use morok_dtype::DType;
use morok_ir::{BinaryOp, ReduceOp, TernaryOp, UnaryOp};

use super::types::mlir_type;

// =============================================================================
// Constants
// =============================================================================

pub fn const_int<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    val: i64,
    int_type: Type<'c>,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let attr = IntegerAttribute::new(int_type, val);
    block.append_operation(arith::constant(ctx, attr.into(), loc)).result(0).unwrap().into()
}

pub fn const_i64<'c>(ctx: &'c Context, block: &Block<'c>, val: i64, loc: Location<'c>) -> Value<'c, 'c> {
    const_int(ctx, block, val, IntegerType::new(ctx, 64).into(), loc)
}

pub fn const_i32<'c>(ctx: &'c Context, block: &Block<'c>, val: i64, loc: Location<'c>) -> Value<'c, 'c> {
    const_int(ctx, block, val, IntegerType::new(ctx, 32).into(), loc)
}

pub fn const_float<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    val: f64,
    float_type: Type<'c>,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let attr = FloatAttribute::new(ctx, float_type, val);
    block.append_operation(arith::constant(ctx, attr.into(), loc)).result(0).unwrap().into()
}

/// Build an MLIR constant from a ConstValue.
///
/// The ConstValue variant must match the target dtype — the IR always produces
/// correctly-typed constants (via `ConstValue::zero`, `reduce_identity`, etc.).
pub fn build_const<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    val: &morok_ir::ConstValue,
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let mlir_ty = mlir_type(ctx, dtype);

    match val {
        morok_ir::ConstValue::Int(i) => {
            if dtype.is_float() {
                // AMX accumulator init uses Int(0) for float zero
                const_float(ctx, block, *i as f64, mlir_ty, loc)
            } else {
                const_int(ctx, block, *i, mlir_ty, loc)
            }
        }
        morok_ir::ConstValue::UInt(u) => {
            if dtype.is_float() {
                const_float(ctx, block, *u as f64, mlir_ty, loc)
            } else {
                const_int(ctx, block, *u as i64, mlir_ty, loc)
            }
        }
        morok_ir::ConstValue::Float(f) => {
            debug_assert!(dtype.is_float(), "Float ConstValue with non-float dtype {dtype:?}");
            const_float(ctx, block, *f, mlir_ty, loc)
        }
        morok_ir::ConstValue::Bool(b) => {
            debug_assert!(dtype.is_bool(), "Bool ConstValue with non-bool dtype {dtype:?}");
            const_int(ctx, block, i64::from(*b), IntegerType::new(ctx, 1).into(), loc)
        }
    }
}

/// Build a vector constant.
pub fn build_vconst<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    values: &[morok_ir::ConstValue],
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let vec_type = mlir_type(ctx, dtype);
    let scalar_dtype = dtype.scalar_dtype();

    let scalars: Vec<Value> = values.iter().map(|val| build_const(ctx, block, val, &scalar_dtype, loc)).collect();
    block.append_operation(vector::from_elements(ctx, vec_type, &scalars, loc).into()).result(0).unwrap().into()
}

/// Build a reduce identity constant.
pub fn build_reduce_identity<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    reduce_op: ReduceOp,
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let identity = super::types::reduce_identity_value(reduce_op, dtype);

    if matches!(dtype, DType::Vector { .. }) {
        let scalar_dtype = dtype.scalar_dtype();
        let scalar_val = build_const(ctx, block, &identity, &scalar_dtype, loc);
        let vec_type = mlir_type(ctx, dtype);
        block.append_operation(vector::splat(ctx, vec_type, scalar_val, loc).into()).result(0).unwrap().into()
    } else {
        build_const(ctx, block, &identity, dtype, loc)
    }
}

// =============================================================================
// Binary operations
// =============================================================================

pub fn render_binary<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    op: BinaryOp,
    lhs: Value<'c, 'c>,
    rhs: Value<'c, 'c>,
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let result_type = mlir_type(ctx, dtype);
    let is_f = dtype.is_float();
    let is_s = dtype.is_signed();
    let emit = |op| block.append_operation(op).result(0).unwrap().into();
    let cmp = |fpred, spred, upred| {
        let op = if is_f {
            arith::cmpf(ctx, fpred, lhs, rhs, loc)
        } else if is_s {
            arith::cmpi(ctx, spred, lhs, rhs, loc)
        } else {
            arith::cmpi(ctx, upred, lhs, rhs, loc)
        };
        block.append_operation(op).result(0).unwrap().into()
    };

    match op {
        BinaryOp::Add if is_f => emit(arith::addf(lhs, rhs, loc)),
        BinaryOp::Add => emit(arith::addi(lhs, rhs, loc)),
        BinaryOp::Sub if is_f => emit(arith::subf(lhs, rhs, loc)),
        BinaryOp::Sub => emit(arith::subi(lhs, rhs, loc)),
        BinaryOp::Mul if is_f => emit(arith::mulf(lhs, rhs, loc)),
        BinaryOp::Mul => emit(arith::muli(lhs, rhs, loc)),
        BinaryOp::Fdiv => emit(arith::divf(lhs, rhs, loc)),
        BinaryOp::Idiv if is_s => emit(arith::divsi(lhs, rhs, loc)),
        BinaryOp::Idiv => emit(arith::divui(lhs, rhs, loc)),
        BinaryOp::Mod if is_f => emit(arith::remf(lhs, rhs, loc)),
        BinaryOp::Mod if is_s => emit(arith::remsi(lhs, rhs, loc)),
        BinaryOp::Mod => emit(arith::remui(lhs, rhs, loc)),
        BinaryOp::Max if is_f => emit(ods::arith::maxnumf(ctx, lhs, rhs, loc).into()),
        BinaryOp::Max => {
            let p = if is_s { CmpiPredicate::Sgt } else { CmpiPredicate::Ugt };
            let c = emit(arith::cmpi(ctx, p, lhs, rhs, loc));
            emit(arith::select(c, lhs, rhs, loc))
        }
        BinaryOp::Pow if is_f => emit(ods::math::powf(ctx, lhs, rhs, loc).into()),
        BinaryOp::Pow => {
            let f64_type = Type::float64(ctx);
            let lf = emit(arith::sitofp(lhs, f64_type, loc));
            let rf = emit(arith::sitofp(rhs, f64_type, loc));
            let pf = emit(ods::math::powf(ctx, lf, rf, loc).into());
            emit(arith::fptosi(pf, result_type, loc))
        }
        BinaryOp::Lt => cmp(CmpfPredicate::Olt, CmpiPredicate::Slt, CmpiPredicate::Ult),
        BinaryOp::Le => cmp(CmpfPredicate::Ole, CmpiPredicate::Sle, CmpiPredicate::Ule),
        BinaryOp::Gt => cmp(CmpfPredicate::Ogt, CmpiPredicate::Sgt, CmpiPredicate::Ugt),
        BinaryOp::Ge => cmp(CmpfPredicate::Oge, CmpiPredicate::Sge, CmpiPredicate::Uge),
        BinaryOp::Eq => cmp(CmpfPredicate::Oeq, CmpiPredicate::Eq, CmpiPredicate::Eq),
        BinaryOp::Ne => cmp(CmpfPredicate::Une, CmpiPredicate::Ne, CmpiPredicate::Ne),
        BinaryOp::And => emit(arith::andi(lhs, rhs, loc)),
        BinaryOp::Or => emit(arith::ori(lhs, rhs, loc)),
        BinaryOp::Xor | BinaryOp::Threefry => emit(arith::xori(lhs, rhs, loc)),
        BinaryOp::Shl => emit(arith::shli(lhs, rhs, loc)),
        BinaryOp::Shr if is_s => emit(arith::shrsi(lhs, rhs, loc)),
        BinaryOp::Shr => emit(arith::shrui(lhs, rhs, loc)),
    }
}

// =============================================================================
// Unary operations
// =============================================================================

pub fn render_unary<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    op: UnaryOp,
    src: Value<'c, 'c>,
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let result_type = mlir_type(ctx, dtype);
    let is_f = dtype.is_float();
    let emit = |op| block.append_operation(op).result(0).unwrap().into();

    match op {
        UnaryOp::Neg if is_f => emit(arith::negf(src, loc)),
        UnaryOp::Neg => {
            let zero = const_int(ctx, block, 0, result_type, loc);
            emit(arith::subi(zero, src, loc))
        }
        UnaryOp::Not => {
            let mask = if dtype.is_bool() {
                const_int(ctx, block, 1, IntegerType::new(ctx, 1).into(), loc)
            } else {
                const_int(ctx, block, -1, result_type, loc)
            };
            emit(arith::xori(src, mask, loc))
        }
        UnaryOp::Sqrt => emit(ods::math::sqrt(ctx, src, loc).into()),
        UnaryOp::Exp => emit(ods::math::exp(ctx, src, loc).into()),
        UnaryOp::Exp2 => emit(ods::math::exp_2(ctx, src, loc).into()),
        UnaryOp::Log => emit(ods::math::log(ctx, src, loc).into()),
        UnaryOp::Log2 => emit(ods::math::log_2(ctx, src, loc).into()),
        UnaryOp::Sin => emit(ods::math::sin(ctx, src, loc).into()),
        UnaryOp::Cos => emit(ods::math::cos(ctx, src, loc).into()),
        UnaryOp::Floor => emit(ods::math::floor(ctx, src, loc).into()),
        UnaryOp::Ceil => emit(ods::math::ceil(ctx, src, loc).into()),
        UnaryOp::Trunc => emit(ods::math::trunc(ctx, src, loc).into()),
        UnaryOp::Round => emit(ods::math::round(ctx, src, loc).into()),
        UnaryOp::Abs if is_f => emit(ods::math::absf(ctx, src, loc).into()),
        UnaryOp::Abs => emit(ods::math::absi(ctx, src, loc).into()),
        UnaryOp::Rsqrt => emit(ods::math::rsqrt(ctx, src, loc).into()),
        UnaryOp::Reciprocal => {
            let one = const_float(ctx, block, 1.0, result_type, loc);
            emit(arith::divf(one, src, loc))
        }
        UnaryOp::Tan => emit(ods::math::tan(ctx, src, loc).into()),
        UnaryOp::Sign if is_f => {
            let zero = const_float(ctx, block, 0.0, result_type, loc);
            let gt = emit(arith::cmpf(ctx, CmpfPredicate::Ogt, src, zero, loc));
            let lt = emit(arith::cmpf(ctx, CmpfPredicate::Olt, src, zero, loc));
            let gt_f = emit(arith::uitofp(gt, result_type, loc));
            let lt_f = emit(arith::uitofp(lt, result_type, loc));
            emit(arith::subf(gt_f, lt_f, loc))
        }
        UnaryOp::Sign => {
            let zero = const_int(ctx, block, 0, result_type, loc);
            let (gt_pred, lt_pred) = if dtype.is_signed() {
                (CmpiPredicate::Sgt, CmpiPredicate::Slt)
            } else {
                (CmpiPredicate::Ugt, CmpiPredicate::Ult)
            };
            let gt = emit(arith::cmpi(ctx, gt_pred, src, zero, loc));
            let lt = emit(arith::cmpi(ctx, lt_pred, src, zero, loc));
            let gt_ext = emit(arith::extui(gt, result_type, loc));
            let lt_ext = emit(arith::extui(lt, result_type, loc));
            emit(arith::subi(gt_ext, lt_ext, loc))
        }
        UnaryOp::Erf => emit(ods::math::erf(ctx, src, loc).into()),
        UnaryOp::Square if is_f => emit(arith::mulf(src, src, loc)),
        UnaryOp::Square => emit(arith::muli(src, src, loc)),
    }
}

// =============================================================================
// Cast operations
// =============================================================================

pub fn render_cast<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    src: Value<'c, 'c>,
    from_dtype: &DType,
    to_dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let to_type = mlir_type(ctx, to_dtype);
    let from_scalar = from_dtype.base();
    let to_scalar = to_dtype.base();

    // Ptr <-> Ptr: no-op, pointers are opaque in LLVM
    if matches!(from_dtype, DType::Ptr { .. }) && matches!(to_dtype, DType::Ptr { .. }) {
        return src;
    }
    // Ptr -> scalar or scalar -> Ptr: invalid IR — ensure_scalar produces Cast(Ptr<T> → T)
    // which should be lowered as a load, not a ptrtoint/inttoptr reinterpretation.
    if matches!(from_dtype, DType::Ptr { .. }) || matches!(to_dtype, DType::Ptr { .. }) {
        unreachable!("Cast between Ptr and scalar types is invalid IR: {from_dtype:?} → {to_dtype:?}");
    }

    // Float -> Float
    if from_scalar.is_float() && to_scalar.is_float() {
        return if to_scalar.bytes() > from_scalar.bytes() {
            block.append_operation(arith::extf(src, to_type, loc)).result(0).unwrap().into()
        } else if to_scalar.bytes() < from_scalar.bytes() {
            block.append_operation(arith::truncf(src, loc)).result(0).unwrap().into()
        } else {
            block.append_operation(arith::bitcast(src, to_type, loc)).result(0).unwrap().into()
        };
    }

    // Int/Bool -> Float
    if !from_scalar.is_float() && to_scalar.is_float() {
        return if from_scalar.is_unsigned() || from_scalar.is_bool() {
            block.append_operation(arith::uitofp(src, to_type, loc)).result(0).unwrap().into()
        } else {
            block.append_operation(arith::sitofp(src, to_type, loc)).result(0).unwrap().into()
        };
    }

    // Float -> Int
    if from_scalar.is_float() && !to_scalar.is_float() {
        return if to_scalar.is_unsigned() {
            block.append_operation(arith::fptoui(src, to_type, loc)).result(0).unwrap().into()
        } else {
            block.append_operation(arith::fptosi(src, to_type, loc)).result(0).unwrap().into()
        };
    }

    // Int -> Int
    let from_bits = if from_scalar.is_bool() { 1 } else { from_scalar.bytes() as u32 * 8 };
    let to_bits = if to_scalar.is_bool() { 1 } else { to_scalar.bytes() as u32 * 8 };

    if from_bits == to_bits {
        block.append_operation(arith::bitcast(src, to_type, loc)).result(0).unwrap().into()
    } else if to_bits < from_bits {
        block.append_operation(arith::trunci(src, to_type, loc)).result(0).unwrap().into()
    } else if from_scalar.is_unsigned() || from_scalar.is_bool() {
        block.append_operation(arith::extui(src, to_type, loc)).result(0).unwrap().into()
    } else {
        block.append_operation(arith::extsi(src, to_type, loc)).result(0).unwrap().into()
    }
}

// =============================================================================
// Vector element insertion/extraction
// =============================================================================

/// Build llvm.insertelement operation manually (not provided by melior dialect helpers).
pub fn insert_element<'c>(
    vec: Value<'c, 'c>,
    elem: Value<'c, 'c>,
    index: Value<'c, 'c>,
    vec_type: Type<'c>,
    loc: Location<'c>,
) -> melior::ir::operation::Operation<'c> {
    OperationBuilder::new("llvm.insertelement", loc)
        .add_operands(&[vec, elem, index])
        .add_results(&[vec_type])
        .build()
        .expect("valid llvm.insertelement")
}

// =============================================================================
// Ternary operations
// =============================================================================

#[allow(clippy::too_many_arguments)]
pub fn render_ternary<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    op: TernaryOp,
    a: Value<'c, 'c>,
    b: Value<'c, 'c>,
    c: Value<'c, 'c>,
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let emit = |op| block.append_operation(op).result(0).unwrap().into();
    match op {
        TernaryOp::Where => emit(arith::select(a, b, c, loc)),
        TernaryOp::MulAcc => {
            if dtype.is_float() {
                emit(ods::math::fma(ctx, a, b, c, loc).into())
            } else {
                let mul = emit(arith::muli(a, b, loc));
                emit(arith::addi(mul, c, loc))
            }
        }
    }
}

// =============================================================================
// Reduce accumulation
// =============================================================================

/// Compute the new accumulator value: `acc_new = reduce_op(acc, src)`. Pure SSA, no memory ops.
pub fn render_reduce_accumulate<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    reduce_op: ReduceOp,
    src: Value<'c, 'c>,
    acc: Value<'c, 'c>,
    dtype: &DType,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let emit = |op| block.append_operation(op).result(0).unwrap().into();
    match reduce_op {
        ReduceOp::Add if dtype.is_float() => emit(arith::addf(acc, src, loc)),
        ReduceOp::Add => emit(arith::addi(acc, src, loc)),
        ReduceOp::Mul if dtype.is_float() => emit(arith::mulf(acc, src, loc)),
        ReduceOp::Mul => emit(arith::muli(acc, src, loc)),
        ReduceOp::Max if dtype.is_float() => emit(ods::arith::maxnumf(ctx, acc, src, loc).into()),
        ReduceOp::Max => {
            let p = if dtype.is_signed() { CmpiPredicate::Sgt } else { CmpiPredicate::Ugt };
            let c = emit(arith::cmpi(ctx, p, acc, src, loc));
            emit(arith::select(c, acc, src, loc))
        }
        ReduceOp::Min if dtype.is_float() => emit(ods::arith::minnumf(ctx, acc, src, loc).into()),
        ReduceOp::Min => {
            let p = if dtype.is_signed() { CmpiPredicate::Slt } else { CmpiPredicate::Ult };
            let c = emit(arith::cmpi(ctx, p, acc, src, loc));
            emit(arith::select(c, acc, src, loc))
        }
    }
}

// =============================================================================
// Vector operations
// =============================================================================

pub fn render_vectorize<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    elements: &[Value<'c, 'c>],
    vec_type: Type<'c>,
    _scalar_type: Type<'c>,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    block.append_operation(vector::from_elements(ctx, vec_type, elements, loc).into()).result(0).unwrap().into()
}

pub fn render_extractelement<'c>(
    ctx: &'c Context,
    block: &Block<'c>,
    vec: Value<'c, 'c>,
    index: usize,
    rtype: Type<'c>,
    loc: Location<'c>,
) -> Value<'c, 'c> {
    let idx = const_i32(ctx, block, index as i64, loc);
    let op = vector::ExtractElementOperation::builder(ctx, loc).result(rtype).vector(vec).position(idx).build();
    block.append_operation(op.into()).result(0).unwrap().into()
}