tensor-wasm-jit 0.3.8

JIT pipeline: Cranelift detector, IR normalisation, PTX codegen, kernel cache, deopt.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Craton Software Company

//! Float-family Cranelift→[`LoweredOp`] lowering (wave 1, task L2).
//!
//! This module covers the float rows of the [Cranelift → `dialect-mir`
//! mapping table](crate::pliron_dialect#mapping-table):
//!
//! | Cranelift op | Produces                                       |
//! |--------------|------------------------------------------------|
//! | `fadd`       | [`LoweredOp::AddF`]                            |
//! | `fsub`       | [`LoweredOp::SubF`]                            |
//! | `fmul`       | [`LoweredOp::MulF`]                            |
//! | `fdiv`       | [`LoweredOp::DivF`]                            |
//! | `fma`        | [`LoweredOp::Fma`] (`a*b + c`, single rounding)|
//! | `fneg`       | [`LoweredOp::FNeg`]                            |
//! | `fabs`       | [`LoweredOp::FAbs`]                            |
//!
//! Only `f32` / `f64` scalars are accepted — `f16`, `v128`, or any other
//! Cranelift type returns [`None`] so the caller can fall back to a
//! different lowering family (`lower_vector` handles vector float ops in a
//! sibling module). Per-row Pliron equivalents and PTX semantics are
//! captured in the mapping table; this module purposely does not repeat
//! those notes here so the single source of truth stays in
//! [`crate::pliron_dialect`].
//!
//! # Result-id allocation
//!
//! Each successful lowering allocates a fresh [`LoweredValueId`] for the
//! op's SSA result by reading `*next_value_id` and post-incrementing it.
//! Operand ids come from the caller-owned `value_map` which maps each
//! Cranelift [`cranelift_codegen::ir::Value`] to the already-assigned
//! [`LoweredValueId`]. Missing operands are a caller bug and panic — the
//! lowering walker must populate the map for every block parameter and
//! every prior instruction's result before recursing into this family.
//!
//! # Scope
//!
//! - Pure: no side effects beyond `*next_value_id` and reading
//!   `func.dfg`. Safe to call in any order; the caller controls
//!   block-walk ordering.
//! - Stateless: no module-level statics; multiple lowerings can run
//!   concurrently (one `&mut next_value_id` per pass, as is the
//!   wave-1 convention shared with `lower_arith` etc.).

#![cfg(feature = "cuda-oxide-backend")]

use std::collections::HashMap;

use cranelift_codegen::ir::{self, Function, Inst, Opcode, Value};

use crate::lowered_ir::{LoweredOp, LoweredType, LoweredValueId};

/// Try to lower one Cranelift instruction as a float-family op.
///
/// Returns `Some(LoweredOp)` if `inst`'s opcode is one of the seven this
/// module handles (`fadd`/`fsub`/`fmul`/`fdiv`/`fma`/`fneg`/`fabs`) **and**
/// its result type is `f32` or `f64`. Returns `None` otherwise — the
/// caller is expected to consult sibling `lower_*` modules in that case
/// (vector float ops live in `lower_vector`; non-float ops live in
/// `lower_arith`, `lower_memory`, etc.).
///
/// # Parameters
///
/// - `inst`: the Cranelift instruction being lowered.
/// - `func`: the enclosing function. Used read-only for opcode lookup,
///   operand fetch (`dfg.inst_args`), and result-type inspection
///   (`dfg.value_type` on the instruction's first result).
/// - `value_map`: caller-owned map from already-lowered Cranelift
///   `Value`s to their assigned [`LoweredValueId`]s. Every operand of
///   `inst` must be present.
/// - `next_value_id`: caller-owned monotonic counter for fresh result
///   ids. On a successful lowering, the current value is consumed as
///   the new op's `result` field and then incremented by one.
///
/// # Panics
///
/// Panics if an operand `Value` is missing from `value_map`. This
/// signals a walker bug (a use-before-def in the caller); the wave-1
/// design treats it as unrecoverable rather than threading a `Result`
/// through every per-family lowering.
pub fn lower_float_inst(
    inst: Inst,
    func: &Function,
    value_map: &HashMap<Value, LoweredValueId>,
    next_value_id: &mut LoweredValueId,
) -> Option<LoweredOp> {
    let opcode = func.dfg.insts[inst].opcode();

    // Quick reject: anything not in our seven-op set returns None
    // without touching `next_value_id`. Keeps the function side-effect
    // free on the rejection path so the caller can fall through to the
    // next lowering family cleanly.
    match opcode {
        Opcode::Fadd
        | Opcode::Fsub
        | Opcode::Fmul
        | Opcode::Fdiv
        | Opcode::Fma
        | Opcode::Fneg
        | Opcode::Fabs => {}
        _ => return None,
    }

    // Determine the result type. All seven ops produce exactly one
    // result whose type matches their operands. Reject anything outside
    // the f32/f64 scalar set — vector float ops (e.g. fadd on f32x4)
    // are handled by `lower_vector`, and f16 is not in the wave-1
    // mapping table.
    let result_value = func.dfg.first_result(inst);
    let ty = match func.dfg.value_type(result_value) {
        t if t == ir::types::F32 => LoweredType::F32,
        t if t == ir::types::F64 => LoweredType::F64,
        _ => return None,
    };

    let args = func.dfg.inst_args(inst);
    let result = *next_value_id;
    // jit LOW fix (finding 7): standardize on `checked_add(1)?` (the
    // `lower_arith` idiom) instead of `.expect(...)`. A function exceeding
    // `u32::MAX` SSA values now surfaces as a structured lowering miss
    // (`None`, mapped to a `LoweringError` by the driver) rather than
    // panicking the process.
    *next_value_id = next_value_id.checked_add(1)?;

    let lowered = match opcode {
        Opcode::Fadd => LoweredOp::AddF {
            ty,
            lhs: lookup(value_map, args[0])?,
            rhs: lookup(value_map, args[1])?,
            result,
        },
        Opcode::Fsub => LoweredOp::SubF {
            ty,
            lhs: lookup(value_map, args[0])?,
            rhs: lookup(value_map, args[1])?,
            result,
        },
        Opcode::Fmul => LoweredOp::MulF {
            ty,
            lhs: lookup(value_map, args[0])?,
            rhs: lookup(value_map, args[1])?,
            result,
        },
        Opcode::Fdiv => LoweredOp::DivF {
            ty,
            lhs: lookup(value_map, args[0])?,
            rhs: lookup(value_map, args[1])?,
            result,
        },
        Opcode::Fma => LoweredOp::Fma {
            ty,
            a: lookup(value_map, args[0])?,
            b: lookup(value_map, args[1])?,
            c: lookup(value_map, args[2])?,
            result,
        },
        Opcode::Fneg => LoweredOp::FNeg {
            ty,
            src: lookup(value_map, args[0])?,
            result,
        },
        Opcode::Fabs => LoweredOp::FAbs {
            ty,
            src: lookup(value_map, args[0])?,
            result,
        },
        // Unreachable: the earlier `match` already filtered the opcode
        // set down to the seven listed above.
        _ => unreachable!("opcode already validated by the dispatch match"),
    };

    Some(lowered)
}

/// Resolve a Cranelift `Value` to its already-assigned
/// [`LoweredValueId`]. Returns `None` if the operand was not
/// pre-mapped (jit S-4: a malformed Cranelift function with a
/// backward branch whose block-param references a not-yet-seen
/// value used to panic the worker; now it gracefully degrades to
/// `lower_float_inst` returning `None`, which the walker treats
/// as "skip this instruction" — same as for unsupported ops).
fn lookup(map: &HashMap<Value, LoweredValueId>, v: Value) -> Option<LoweredValueId> {
    let id = map.get(&v).copied();
    if id.is_none() {
        tracing::debug!(
            target: "tensor_wasm_jit::lower_float",
            value = ?v,
            "operand not pre-mapped; skipping float instruction"
        );
    }
    id
}

#[cfg(test)]
mod tests {
    use super::*;
    use cranelift_codegen::ir::{
        types, AbiParam, Block, Function, InstructionData, Signature, UserFuncName, Value,
    };
    use cranelift_codegen::isa::CallConv;

    /// Fixture: a one-instruction function and the operand `Value`s
    /// used to build it. The test asserts against `inst`; operand ids
    /// are pre-populated into a `value_map` via [`map_operands`] so
    /// the lowering can resolve them.
    struct InstFixture {
        func: Function,
        inst: Inst,
        operands: Vec<Value>,
    }

    /// Build a one-instruction function whose only block has parameters
    /// of `param_ty`, then append a single instruction described by
    /// `make_data` (operands pulled from the block params, one per
    /// `arg_count`). The result type is set by `make_inst_results`
    /// using `param_ty` as the controlling typevar.
    fn build_func_with_inst(
        param_ty: cranelift_codegen::ir::Type,
        arg_count: usize,
        make_data: impl FnOnce(&[Value]) -> InstructionData,
    ) -> InstFixture {
        let mut sig = Signature::new(CallConv::Fast);
        for _ in 0..arg_count {
            sig.params.push(AbiParam::new(param_ty));
        }
        sig.returns.push(AbiParam::new(param_ty));

        let mut func = Function::with_name_signature(UserFuncName::testcase("t"), sig);
        let block: Block = func.dfg.make_block();
        let mut operands: Vec<Value> = Vec::with_capacity(arg_count);
        for _ in 0..arg_count {
            operands.push(func.dfg.append_block_param(block, param_ty));
        }
        let data = make_data(&operands);
        let inst = func.dfg.make_inst(data);
        func.dfg.make_inst_results(inst, param_ty);
        InstFixture {
            func,
            inst,
            operands,
        }
    }

    /// Map the fixture's operand `Value`s to fresh
    /// [`LoweredValueId`]s starting at 0. Returns the map and the
    /// first id not yet used (i.e. the id the lowering should assign
    /// to the instruction's result).
    fn map_operands(operands: &[Value]) -> (HashMap<Value, LoweredValueId>, LoweredValueId) {
        let mut map = HashMap::new();
        let mut next: LoweredValueId = 0;
        for v in operands {
            map.insert(*v, next);
            next += 1;
        }
        (map, next)
    }

    fn binary(opcode: Opcode, args: [Value; 2]) -> InstructionData {
        InstructionData::Binary { opcode, args }
    }

    fn ternary(opcode: Opcode, args: [Value; 3]) -> InstructionData {
        InstructionData::Ternary { opcode, args }
    }

    fn unary(opcode: Opcode, arg: Value) -> InstructionData {
        InstructionData::Unary { opcode, arg }
    }

    #[test]
    fn lower_fadd_f32() {
        let fx = build_func_with_inst(types::F32, 2, |params| {
            binary(Opcode::Fadd, [params[0], params[1]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fadd lowers");
        match op {
            LoweredOp::AddF {
                ty,
                lhs,
                rhs,
                result,
            } => {
                assert_eq!(ty, LoweredType::F32);
                assert_eq!(lhs, 0);
                assert_eq!(rhs, 1);
                assert_eq!(result, 2);
            }
            other => panic!("expected AddF, got {other:?}"),
        }
        assert_eq!(next, 3);
    }

    #[test]
    fn lower_fsub_f64() {
        let fx = build_func_with_inst(types::F64, 2, |params| {
            binary(Opcode::Fsub, [params[0], params[1]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fsub lowers");
        match op {
            LoweredOp::SubF { ty, .. } => assert_eq!(ty, LoweredType::F64),
            other => panic!("expected SubF, got {other:?}"),
        }
    }

    #[test]
    fn lower_fmul_f32() {
        let fx = build_func_with_inst(types::F32, 2, |params| {
            binary(Opcode::Fmul, [params[0], params[1]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fmul lowers");
        match op {
            LoweredOp::MulF { ty, .. } => assert_eq!(ty, LoweredType::F32),
            other => panic!("expected MulF, got {other:?}"),
        }
    }

    #[test]
    fn lower_fdiv_f64() {
        let fx = build_func_with_inst(types::F64, 2, |params| {
            binary(Opcode::Fdiv, [params[0], params[1]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fdiv lowers");
        match op {
            LoweredOp::DivF { ty, .. } => assert_eq!(ty, LoweredType::F64),
            other => panic!("expected DivF, got {other:?}"),
        }
    }

    #[test]
    fn lower_fma_f32() {
        let fx = build_func_with_inst(types::F32, 3, |params| {
            ternary(Opcode::Fma, [params[0], params[1], params[2]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fma lowers");
        match op {
            LoweredOp::Fma {
                ty,
                a,
                b,
                c,
                result,
            } => {
                assert_eq!(ty, LoweredType::F32);
                assert_eq!(a, 0);
                assert_eq!(b, 1);
                assert_eq!(c, 2);
                assert_eq!(result, 3);
            }
            other => panic!("expected Fma, got {other:?}"),
        }
    }

    #[test]
    fn lower_fneg_f64() {
        let fx = build_func_with_inst(types::F64, 1, |params| unary(Opcode::Fneg, params[0]));
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fneg lowers");
        match op {
            LoweredOp::FNeg { ty, src, result } => {
                assert_eq!(ty, LoweredType::F64);
                assert_eq!(src, 0);
                assert_eq!(result, 1);
            }
            other => panic!("expected FNeg, got {other:?}"),
        }
    }

    #[test]
    fn lower_fabs_f32() {
        let fx = build_func_with_inst(types::F32, 1, |params| unary(Opcode::Fabs, params[0]));
        let (map, mut next) = map_operands(&fx.operands);
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next).expect("fabs lowers");
        match op {
            LoweredOp::FAbs { ty, src, result } => {
                assert_eq!(ty, LoweredType::F32);
                assert_eq!(src, 0);
                assert_eq!(result, 1);
            }
            other => panic!("expected FAbs, got {other:?}"),
        }
    }

    /// Non-float opcodes (here `iadd`) return `None` and leave the
    /// id-counter untouched so the caller can fall through to the next
    /// lowering family.
    #[test]
    fn non_float_opcode_returns_none() {
        let fx = build_func_with_inst(types::I32, 2, |params| {
            binary(Opcode::Iadd, [params[0], params[1]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let before = next;
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next);
        assert!(op.is_none(), "iadd must not be claimed by lower_float");
        assert_eq!(next, before, "counter must not advance on rejection");
    }

    /// A float opcode on an unsupported type (here a v128 vector
    /// fadd) is rejected — those rows live in `lower_vector`. The
    /// caller falls through cleanly.
    #[test]
    fn vector_fadd_returns_none() {
        let fx = build_func_with_inst(types::F32X4, 2, |params| {
            binary(Opcode::Fadd, [params[0], params[1]])
        });
        let (map, mut next) = map_operands(&fx.operands);
        let before = next;
        let op = lower_float_inst(fx.inst, &fx.func, &map, &mut next);
        assert!(
            op.is_none(),
            "vector fadd must not be claimed by lower_float"
        );
        assert_eq!(next, before);
    }
}