trueno-gpu 0.4.15

Pure Rust PTX generation for NVIDIA CUDA - no LLVM, no nvcc
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
454
//! Memory operation emission
//!
//! Handles: Ld, LdVolatile, LdParam, St, Cvt, Cvta, Atom* variants

use crate::ptx::instructions::{Operand, PtxInstruction, PtxOp};
use crate::ptx::types::PtxType;
use std::fmt::Write;

/// Emit memory opcode to the output string
pub(crate) fn emit_memory_opcode(instr: &PtxInstruction, s: &mut String) {
    match instr.op {
        PtxOp::Ld => {
            if let Some(ss) = instr.state_space {
                s.push_str("ld");
                s.push_str(ss.to_ptx_string());
            } else {
                s.push_str("ld");
            }
        }
        PtxOp::LdVolatile => {
            if let Some(ss) = instr.state_space {
                s.push_str("ld.volatile");
                s.push_str(ss.to_ptx_string());
            } else {
                s.push_str("ld.volatile");
            }
        }
        PtxOp::LdParam => s.push_str("ld.param"),
        PtxOp::St => {
            if let Some(ss) = instr.state_space {
                s.push_str("st");
                s.push_str(ss.to_ptx_string());
            } else {
                s.push_str("st");
            }
        }
        PtxOp::Cvt => emit_cvt_opcode(instr, s),
        PtxOp::Cvta => {
            let space = instr
                .state_space
                .map(|ss| ss.to_ptx_string())
                .unwrap_or(".shared");
            let ty = instr.ty.to_ptx_string();
            s.push_str("cvta");
            s.push_str(space);
            s.push_str(ty);
        }
        PtxOp::AtomAdd => emit_atomic_opcode(instr, s, "add"),
        PtxOp::AtomMin => emit_atomic_opcode(instr, s, "min"),
        PtxOp::AtomMax => emit_atomic_opcode(instr, s, "max"),
        PtxOp::AtomExch => emit_atomic_opcode(instr, s, "exch"),
        PtxOp::AtomCas => emit_atomic_opcode(instr, s, "cas"),
        _ => {}
    }
}

/// Emit cvt opcode with proper type conversion handling
fn emit_cvt_opcode(instr: &PtxInstruction, s: &mut String) {
    let dst_ty = instr.ty.to_ptx_string();
    let src_ty = if let Some(st) = instr.src_type {
        st.to_ptx_string()
    } else if let Some(Operand::Reg(vreg)) = instr.srcs.first() {
        vreg.ty().to_ptx_string()
    } else {
        ".u32"
    };

    let actual_src_type = instr.src_type.unwrap_or_else(|| {
        instr
            .srcs
            .first()
            .and_then(|src| {
                if let Operand::Reg(vreg) = src {
                    Some(vreg.ty())
                } else {
                    None
                }
            })
            .unwrap_or(PtxType::U32)
    });

    let src_is_f16 = actual_src_type == PtxType::F16;
    let dst_is_f32 = instr.ty == PtxType::F32;
    let is_f16_to_f32 = src_is_f16 && dst_is_f32;

    let needs_rounding = !is_f16_to_f32
        && (instr.ty.is_float()
            || instr
                .srcs
                .first()
                .is_some_and(|src| matches!(src, Operand::Reg(vreg) if vreg.ty().is_float())));

    let round = if needs_rounding {
        instr.rounding.as_ref().map_or(".rn", |r| r.to_ptx_string())
    } else {
        ""
    };

    s.push_str("cvt");
    s.push_str(round);
    s.push_str(dst_ty);
    s.push_str(src_ty);
}

/// Emit atomic operation opcode
fn emit_atomic_opcode(instr: &PtxInstruction, s: &mut String, op: &str) {
    let space = instr
        .state_space
        .map(|ss| ss.to_ptx_string())
        .unwrap_or(".global");
    let _ = write!(s, "atom{}.{}", space, op);
}

/// Check if this is a memory operation
pub(crate) fn is_memory_op(op: &PtxOp) -> bool {
    matches!(
        op,
        PtxOp::Ld
            | PtxOp::LdVolatile
            | PtxOp::LdParam
            | PtxOp::St
            | PtxOp::Cvt
            | PtxOp::Cvta
            | PtxOp::AtomAdd
            | PtxOp::AtomMin
            | PtxOp::AtomMax
            | PtxOp::AtomExch
            | PtxOp::AtomCas
    )
}

/// Check if this op requires skipping the type suffix
pub(crate) fn skip_type_for_memory_op(op: &PtxOp) -> bool {
    matches!(op, PtxOp::Cvt | PtxOp::Cvta)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ptx::instructions::{Operand, RoundingMode};
    use crate::ptx::registers::VirtualReg;
    use crate::ptx::types::{PtxStateSpace, PtxType};

    fn make_instr(op: PtxOp, ty: PtxType) -> PtxInstruction {
        PtxInstruction {
            op,
            ty,
            src_type: None,
            dst: None,
            dsts: vec![],
            srcs: vec![],
            label: None,
            predicate: None,
            state_space: None,
            rounding: None,
        }
    }

    // === emit_memory_opcode tests (exhaustive string matching) ===

    #[test]
    fn test_emit_ld_global() {
        let mut instr = make_instr(PtxOp::Ld, PtxType::F32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "ld.global");
    }

    #[test]
    fn test_emit_ld_shared() {
        let mut instr = make_instr(PtxOp::Ld, PtxType::F32);
        instr.state_space = Some(PtxStateSpace::Shared);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "ld.shared");
    }

    #[test]
    fn test_emit_ld_no_space() {
        let instr = make_instr(PtxOp::Ld, PtxType::F32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "ld");
    }

    #[test]
    fn test_emit_ld_volatile_global() {
        let mut instr = make_instr(PtxOp::LdVolatile, PtxType::F32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "ld.volatile.global");
    }

    #[test]
    fn test_emit_ld_volatile_no_space() {
        let instr = make_instr(PtxOp::LdVolatile, PtxType::F32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "ld.volatile");
    }

    #[test]
    fn test_emit_ld_param() {
        let instr = make_instr(PtxOp::LdParam, PtxType::U64);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "ld.param");
    }

    #[test]
    fn test_emit_st_global() {
        let mut instr = make_instr(PtxOp::St, PtxType::F32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "st.global");
    }

    #[test]
    fn test_emit_st_shared() {
        let mut instr = make_instr(PtxOp::St, PtxType::F32);
        instr.state_space = Some(PtxStateSpace::Shared);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "st.shared");
    }

    #[test]
    fn test_emit_st_no_space() {
        let instr = make_instr(PtxOp::St, PtxType::F32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "st");
    }

    #[test]
    fn test_emit_cvta_shared() {
        let mut instr = make_instr(PtxOp::Cvta, PtxType::U64);
        instr.state_space = Some(PtxStateSpace::Shared);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvta.shared.u64");
    }

    #[test]
    fn test_emit_cvta_global() {
        let mut instr = make_instr(PtxOp::Cvta, PtxType::U64);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvta.global.u64");
    }

    #[test]
    fn test_emit_cvta_default() {
        let instr = make_instr(PtxOp::Cvta, PtxType::U64);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvta.shared.u64"); // Default to .shared
    }

    #[test]
    fn test_emit_atom_add_global() {
        let mut instr = make_instr(PtxOp::AtomAdd, PtxType::U32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.global.add");
    }

    #[test]
    fn test_emit_atom_add_shared() {
        let mut instr = make_instr(PtxOp::AtomAdd, PtxType::U32);
        instr.state_space = Some(PtxStateSpace::Shared);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.shared.add");
    }

    #[test]
    fn test_emit_atom_add_default() {
        let instr = make_instr(PtxOp::AtomAdd, PtxType::U32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.global.add"); // Default to .global
    }

    #[test]
    fn test_emit_atom_min() {
        let mut instr = make_instr(PtxOp::AtomMin, PtxType::S32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.global.min");
    }

    #[test]
    fn test_emit_atom_max() {
        let mut instr = make_instr(PtxOp::AtomMax, PtxType::S32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.global.max");
    }

    #[test]
    fn test_emit_atom_exch() {
        let mut instr = make_instr(PtxOp::AtomExch, PtxType::U32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.global.exch");
    }

    #[test]
    fn test_emit_atom_cas() {
        let mut instr = make_instr(PtxOp::AtomCas, PtxType::U32);
        instr.state_space = Some(PtxStateSpace::Global);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "atom.global.cas");
    }

    #[test]
    fn test_emit_non_memory_op() {
        let instr = make_instr(PtxOp::Add, PtxType::F32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert!(s.is_empty()); // No output for non-memory
    }

    // === emit_cvt_opcode tests (all branches) ===

    #[test]
    fn test_emit_cvt_f32_to_u32() {
        let mut instr = make_instr(PtxOp::Cvt, PtxType::U32);
        instr.srcs = vec![Operand::Reg(VirtualReg::new(0, PtxType::F32))];
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.rn.u32.f32");
    }

    #[test]
    fn test_emit_cvt_u32_to_f32() {
        let mut instr = make_instr(PtxOp::Cvt, PtxType::F32);
        instr.srcs = vec![Operand::Reg(VirtualReg::new(0, PtxType::U32))];
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.rn.f32.u32");
    }

    #[test]
    fn test_emit_cvt_explicit_rounding() {
        let mut instr = make_instr(PtxOp::Cvt, PtxType::F32);
        instr.rounding = Some(RoundingMode::Rz);
        instr.srcs = vec![Operand::Reg(VirtualReg::new(0, PtxType::F64))];
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.rz.f32.f64");
    }

    #[test]
    fn test_emit_cvt_explicit_src_type() {
        let mut instr = make_instr(PtxOp::Cvt, PtxType::F32);
        instr.src_type = Some(PtxType::S32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.rn.f32.s32");
    }

    #[test]
    fn test_emit_cvt_f16_to_f32_no_rounding() {
        // f16 -> f32 conversion doesn't need rounding
        let mut instr = make_instr(PtxOp::Cvt, PtxType::F32);
        instr.src_type = Some(PtxType::F16);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.f32.f16");
    }

    #[test]
    fn test_emit_cvt_no_sources() {
        // Edge case: no sources, should use default
        let instr = make_instr(PtxOp::Cvt, PtxType::F32);
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.rn.f32.u32"); // Default to .u32
    }

    #[test]
    fn test_emit_cvt_imm_source() {
        // Non-register source (immediate)
        let mut instr = make_instr(PtxOp::Cvt, PtxType::F32);
        instr.srcs = vec![Operand::ImmU64(42)];
        let mut s = String::new();
        emit_memory_opcode(&instr, &mut s);
        assert_eq!(s, "cvt.rn.f32.u32"); // Default source type
    }

    // === is_memory_op tests ===

    #[test]
    fn test_is_memory_op_all_variants() {
        assert!(is_memory_op(&PtxOp::Ld));
        assert!(is_memory_op(&PtxOp::LdVolatile));
        assert!(is_memory_op(&PtxOp::LdParam));
        assert!(is_memory_op(&PtxOp::St));
        assert!(is_memory_op(&PtxOp::Cvt));
        assert!(is_memory_op(&PtxOp::Cvta));
        assert!(is_memory_op(&PtxOp::AtomAdd));
        assert!(is_memory_op(&PtxOp::AtomMin));
        assert!(is_memory_op(&PtxOp::AtomMax));
        assert!(is_memory_op(&PtxOp::AtomExch));
        assert!(is_memory_op(&PtxOp::AtomCas));
    }

    #[test]
    fn test_is_memory_op_non_memory() {
        assert!(!is_memory_op(&PtxOp::Add));
        assert!(!is_memory_op(&PtxOp::Mul));
        assert!(!is_memory_op(&PtxOp::Bra));
        assert!(!is_memory_op(&PtxOp::Ret));
        assert!(!is_memory_op(&PtxOp::ShflDown));
    }

    // === skip_type_for_memory_op tests ===

    #[test]
    fn test_skip_type_for_cvt() {
        assert!(skip_type_for_memory_op(&PtxOp::Cvt));
    }

    #[test]
    fn test_skip_type_for_cvta() {
        assert!(skip_type_for_memory_op(&PtxOp::Cvta));
    }

    #[test]
    fn test_no_skip_type_for_ld() {
        assert!(!skip_type_for_memory_op(&PtxOp::Ld));
    }

    #[test]
    fn test_no_skip_type_for_st() {
        assert!(!skip_type_for_memory_op(&PtxOp::St));
    }

    #[test]
    fn test_no_skip_type_for_atom() {
        assert!(!skip_type_for_memory_op(&PtxOp::AtomAdd));
    }
}