kaio-core 0.5.0

KAIO core — PTX IR types and emission. Part of the KAIO GPU kernel authoring framework.
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! PTX module — the top-level IR container.

use std::fmt;

use super::instruction::PtxInstruction;
use super::kernel::PtxKernel;
use crate::instr::{MemoryOp, TensorCoreOp};
use crate::types::PtxType;

/// A complete PTX module containing version/target metadata and kernels.
///
/// Corresponds to a single `.ptx` file with a header and one or more
/// `.entry` kernel definitions.
#[derive(Debug, Clone)]
pub struct PtxModule {
    /// PTX ISA version (e.g. `"7.8"`).
    pub version: String,
    /// Target SM architecture (e.g. `"sm_89"`).
    pub target: String,
    /// Address size in bits (32 or 64).
    pub address_size: u32,
    /// Kernel definitions in this module.
    pub kernels: Vec<PtxKernel>,
}

impl PtxModule {
    /// Create a new module targeting the given SM architecture.
    ///
    /// Defaults: PTX version `8.7` (CUDA 12.8), address size `64`.
    pub fn new(target: &str) -> Self {
        Self {
            version: "8.7".to_string(),
            target: target.to_string(),
            address_size: 64,
            kernels: Vec::new(),
        }
    }

    /// Add a kernel to this module.
    pub fn add_kernel(&mut self, kernel: PtxKernel) {
        self.kernels.push(kernel);
    }

    /// Parse the target string (e.g. `"sm_89"`) into a numeric SM
    /// version (e.g. `89`).
    ///
    /// Returns `None` if the target string is not a recognized
    /// `sm_NN` form (e.g. future targets, virtual architectures).
    /// [`validate`](Self::validate) tolerates unparseable targets by
    /// skipping the SM check — we'd rather let unusual targets through
    /// than block a user experimenting with a custom target string.
    fn parse_sm_target(&self) -> Option<u32> {
        self.target.strip_prefix("sm_").and_then(|s| s.parse().ok())
    }

    /// Validate that this module's target SM is high enough for every
    /// feature used by its kernels.
    ///
    /// Walks all kernel bodies looking for features that carry a minimum
    /// SM requirement — currently tensor-core operations and `cp.async`
    /// variants (both Ampere+ / SM 8.0). Returns [`ValidationError::SmTooLow`]
    /// on the **first** such mismatch with a human-readable description.
    ///
    /// This is a narrow **target-capability** check, not a semantic or
    /// dataflow pass. The goal is to surface clean errors at emit-time
    /// instead of cryptic ptxas messages downstream.
    pub fn validate(&self) -> Result<(), ValidationError> {
        let target_sm = self.parse_sm_target();

        for kernel in &self.kernels {
            for instr in &kernel.body {
                // Target-agnostic shape/dtype routing checks.
                if let PtxInstruction::TensorCore(op) = instr {
                    validate_tensor_core_op(op)?;
                }

                // Target-capability check (skipped on unparseable targets).
                if let Some(target_sm) = target_sm
                    && let Some((required, feature)) = instruction_sm_requirement(instr)
                    && target_sm < required
                {
                    return Err(ValidationError::SmTooLow {
                        required,
                        actual: target_sm,
                        feature,
                    });
                }
            }
        }
        Ok(())
    }
}

/// Per-instruction target-agnostic IR validation for tensor-core ops.
///
/// Rejects bf16 dtype tags on the generic [`TensorCoreOp::MmaSync`]
/// variant — bf16 emission must go through [`TensorCoreOp::MmaSyncBf16`] so
/// the fragment types and instruction dtype stay aligned at the IR boundary.
///
/// Also rejects mis-typed registers on [`TensorCoreOp::LdMatrix`]: unlike
/// the mma variants, whose operands are typed fragment wrappers (allocated
/// with the correct register class by construction), `LdMatrix` carries raw
/// [`Register`](crate::ir::Register)s — so the `.b32`-class requirement
/// (`PtxType::U32`, the `alloc_packed_half2` packed-pair convention) is
/// enforced here, surfacing a named error at module load instead of a
/// cryptic ptxas failure at JIT time.
fn validate_tensor_core_op(op: &TensorCoreOp) -> Result<(), ValidationError> {
    match op {
        TensorCoreOp::MmaSync { a_ty, b_ty, .. } => {
            if *a_ty == PtxType::BF16 {
                return Err(ValidationError::MmaSyncBf16Rejected { operand: "a_ty" });
            }
            if *b_ty == PtxType::BF16 {
                return Err(ValidationError::MmaSyncBf16Rejected { operand: "b_ty" });
            }
        }
        TensorCoreOp::LdMatrix { dst, addr, .. } => {
            for reg in dst.regs() {
                if reg.ptx_type != PtxType::U32 {
                    return Err(ValidationError::LdMatrixBadRegType {
                        operand: "dst",
                        found: reg.ptx_type,
                    });
                }
            }
            if addr.ptx_type != PtxType::U32 {
                return Err(ValidationError::LdMatrixBadRegType {
                    operand: "addr",
                    found: addr.ptx_type,
                });
            }
        }
        _ => {}
    }
    Ok(())
}

/// Return `Some((min_sm, feature_label))` if this instruction carries an SM
/// requirement, or `None` if it is SM-agnostic.
fn instruction_sm_requirement(instr: &PtxInstruction) -> Option<(u32, String)> {
    match instr {
        PtxInstruction::TensorCore(op) => Some((op.min_sm(), op.feature_label())),
        PtxInstruction::Memory(
            MemoryOp::CpAsyncCaSharedGlobal { .. }
            | MemoryOp::CpAsyncCommitGroup
            | MemoryOp::CpAsyncWaitGroup { .. },
        ) => Some((80, "cp.async".to_string())),
        _ => None,
    }
}

/// Errors returned by [`PtxModule::validate`].
///
/// Scope is intentionally narrow — target-capability checks only, no
/// semantic analysis.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationError {
    /// A feature used by the module requires a higher SM target than
    /// the module declares.
    ///
    /// Example: a kernel containing `mma.sync.m16n8k16` in a module
    /// with `.target sm_70` would yield
    /// `required: 80, actual: 70, feature: "mma.sync.m16n8k16"`.
    SmTooLow {
        /// Minimum SM version required by the offending feature.
        required: u32,
        /// SM version parsed from the module's target string.
        actual: u32,
        /// Human-readable name of the offending feature.
        feature: String,
    },
    /// A [`TensorCoreOp::MmaSync`] instruction was constructed with
    /// `PtxType::BF16` on `a_ty` or `b_ty`. Bf16 emission must use the
    /// dedicated [`TensorCoreOp::MmaSyncBf16`] variant so fragment types
    /// and instruction dtype stay aligned at the IR boundary.
    ///
    /// Introduced in Sprint 9.1 cleanup; closes the legacy hole where the
    /// generic `MmaSync` path silently emitted a bf16 instruction from
    /// `FragmentA_F16` / `FragmentB_F16` operands.
    MmaSyncBf16Rejected {
        /// Which operand carried the rejected dtype tag (`"a_ty"` or
        /// `"b_ty"`).
        operand: &'static str,
    },
    /// A [`TensorCoreOp::LdMatrix`] instruction carries a register whose
    /// declared type is not `PtxType::U32` (`.b32` class). The mma
    /// variants get this for free from their typed fragment wrappers;
    /// `LdMatrix` takes raw registers, so the check lives here.
    ///
    /// Destination registers hold packed 16-bit pairs
    /// ([`alloc_packed_half2`](crate::ir::RegisterAllocator::alloc_packed_half2)
    /// convention) and the address register is a shared-space `.u32`
    /// byte address. Introduced in Sprint 9.3.
    LdMatrixBadRegType {
        /// Which operand carried the rejected register (`"dst"` or
        /// `"addr"`).
        operand: &'static str,
        /// The register's declared PTX type.
        found: PtxType,
    },
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SmTooLow {
                required,
                actual,
                feature,
            } => {
                write!(
                    f,
                    "{feature} requires sm_{required}+, target is sm_{actual}"
                )
            }
            Self::MmaSyncBf16Rejected { operand } => {
                write!(
                    f,
                    "TensorCoreOp::MmaSync with PtxType::BF16 on {operand} is rejected; use TensorCoreOp::MmaSyncBf16 for bf16 emission"
                )
            }
            Self::LdMatrixBadRegType { operand, found } => {
                write!(
                    f,
                    "TensorCoreOp::LdMatrix {operand} register must be PtxType::U32 (.b32 packed-pair convention, see alloc_packed_half2), found {found:?}"
                )
            }
        }
    }
}

impl std::error::Error for ValidationError {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fragment::{alloc_a_f16, alloc_b_f16, alloc_c};
    use crate::instr::{MemoryOp, MmaShape, TensorCoreOp};
    use crate::ir::{PtxInstruction, PtxKernel, Register, RegisterAllocator};
    use crate::types::{PtxType, RegKind};

    fn reg(kind: RegKind, index: u32, ptx_type: PtxType) -> Register {
        Register {
            kind,
            index,
            ptx_type,
        }
    }

    fn tc_kernel() -> PtxKernel {
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("has_mma");
        k.push(PtxInstruction::TensorCore(TensorCoreOp::MmaSync {
            d: alloc_c(&mut alloc),
            a: alloc_a_f16(&mut alloc),
            b: alloc_b_f16(&mut alloc),
            c: alloc_c(&mut alloc),
            shape: MmaShape::M16N8K16,
            d_ty: PtxType::F32,
            a_ty: PtxType::F16,
            b_ty: PtxType::F16,
            c_ty: PtxType::F32,
        }));
        k
    }

    #[test]
    fn validate_rejects_mma_on_sm_70() {
        let mut module = PtxModule::new("sm_70");
        module.add_kernel(tc_kernel());
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::SmTooLow {
                required: 80,
                actual: 70,
                feature: "mma.sync.m16n8k16".to_string(),
            }
        );
        assert_eq!(
            err.to_string(),
            "mma.sync.m16n8k16 requires sm_80+, target is sm_70"
        );
    }

    #[test]
    fn validate_accepts_mma_on_sm_80() {
        let mut module = PtxModule::new("sm_80");
        module.add_kernel(tc_kernel());
        assert!(module.validate().is_ok());
    }

    #[test]
    fn validate_accepts_mma_on_sm_89() {
        let mut module = PtxModule::new("sm_89");
        module.add_kernel(tc_kernel());
        assert!(module.validate().is_ok());
    }

    fn tc_int8_kernel() -> PtxKernel {
        use crate::fragment::{alloc_a_M16N8K32, alloc_b_M16N8K32, alloc_c_M16N8K32};
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("has_mma_int8");
        k.push(PtxInstruction::TensorCore(TensorCoreOp::MmaSyncInt8 {
            d: alloc_c_M16N8K32(&mut alloc),
            a: alloc_a_M16N8K32(&mut alloc),
            b: alloc_b_M16N8K32(&mut alloc),
            c: alloc_c_M16N8K32(&mut alloc),
        }));
        k
    }

    #[test]
    fn validate_rejects_mma_int8_on_sm_70() {
        let mut module = PtxModule::new("sm_70");
        module.add_kernel(tc_int8_kernel());
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::SmTooLow {
                required: 80,
                actual: 70,
                feature: "mma.sync.m16n8k32.s8.s8.s32".to_string(),
            }
        );
        assert_eq!(
            err.to_string(),
            "mma.sync.m16n8k32.s8.s8.s32 requires sm_80+, target is sm_70"
        );
    }

    #[test]
    fn validate_accepts_mma_int8_on_sm_80() {
        let mut module = PtxModule::new("sm_80");
        module.add_kernel(tc_int8_kernel());
        assert!(module.validate().is_ok());
    }

    #[test]
    fn validate_accepts_mma_int8_on_sm_89() {
        let mut module = PtxModule::new("sm_89");
        module.add_kernel(tc_int8_kernel());
        assert!(module.validate().is_ok());
    }

    fn ldmatrix_kernel() -> PtxKernel {
        use crate::instr::LdMatrixDst;
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("has_ldmatrix");
        k.push(PtxInstruction::TensorCore(TensorCoreOp::LdMatrix {
            dst: LdMatrixDst::X4([
                alloc.alloc_packed_half2(),
                alloc.alloc_packed_half2(),
                alloc.alloc_packed_half2(),
                alloc.alloc_packed_half2(),
            ]),
            addr: alloc.alloc(PtxType::U32),
            trans: false,
        }));
        k
    }

    // ldmatrix is the first sub-80 TensorCore instruction; these tests
    // protect the shared validation path for the new 75 tier
    // (Sprint 9.3): sm_75 accepts ldmatrix, sm_70 still rejects it, and
    // mma stays gated at 80 even in a module whose ldmatrix is fine.
    #[test]
    fn validate_accepts_ldmatrix_on_sm_75() {
        let mut module = PtxModule::new("sm_75");
        module.add_kernel(ldmatrix_kernel());
        assert!(module.validate().is_ok());
    }

    #[test]
    fn validate_rejects_ldmatrix_on_sm_70() {
        let mut module = PtxModule::new("sm_70");
        module.add_kernel(ldmatrix_kernel());
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::SmTooLow {
                required: 75,
                actual: 70,
                feature: "ldmatrix.m8n8.x4".to_string(),
            }
        );
        assert_eq!(
            err.to_string(),
            "ldmatrix.m8n8.x4 requires sm_75+, target is sm_70"
        );
    }

    #[test]
    fn validate_rejects_mma_at_sm_75_even_with_ldmatrix_present() {
        let mut module = PtxModule::new("sm_75");
        module.add_kernel(ldmatrix_kernel());
        module.add_kernel(tc_kernel());
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::SmTooLow {
                required: 80,
                actual: 75,
                feature: "mma.sync.m16n8k16".to_string(),
            }
        );
    }

    #[test]
    fn validate_rejects_ldmatrix_bad_dst_reg_type() {
        use crate::instr::LdMatrixDst;
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("bad_ldmatrix_dst");
        // Third dst register is an .f32 — not the .b32 packed-pair class.
        k.push(PtxInstruction::TensorCore(TensorCoreOp::LdMatrix {
            dst: LdMatrixDst::X4([
                alloc.alloc_packed_half2(),
                alloc.alloc_packed_half2(),
                alloc.alloc(PtxType::F32),
                alloc.alloc_packed_half2(),
            ]),
            addr: alloc.alloc(PtxType::U32),
            trans: false,
        }));
        let mut module = PtxModule::new("sm_80");
        module.add_kernel(k);
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::LdMatrixBadRegType {
                operand: "dst",
                found: PtxType::F32,
            }
        );
    }

    #[test]
    fn validate_rejects_ldmatrix_bad_addr_reg_type() {
        use crate::instr::LdMatrixDst;
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("bad_ldmatrix_addr");
        // Shared addresses are 32-bit byte offsets in this IR — a .u64
        // address register is a wiring bug.
        k.push(PtxInstruction::TensorCore(TensorCoreOp::LdMatrix {
            dst: LdMatrixDst::X2([alloc.alloc_packed_half2(), alloc.alloc_packed_half2()]),
            addr: alloc.alloc(PtxType::U64),
            trans: true,
        }));
        let mut module = PtxModule::new("sm_80");
        module.add_kernel(k);
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::LdMatrixBadRegType {
                operand: "addr",
                found: PtxType::U64,
            }
        );
        assert!(err.to_string().contains("alloc_packed_half2"));
    }

    #[test]
    fn validate_rejects_cp_async_on_sm_75() {
        let mut module = PtxModule::new("sm_75");
        let mut k = PtxKernel::new("has_cp_async");
        k.push(PtxInstruction::Memory(MemoryOp::new_cp_async_ca(
            reg(RegKind::R, 0, PtxType::U32),
            reg(RegKind::Rd, 0, PtxType::U64),
            16,
        )));
        module.add_kernel(k);
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::SmTooLow {
                required: 80,
                actual: 75,
                feature: "cp.async".to_string(),
            }
        );
    }

    #[test]
    fn validate_accepts_scalar_kernel_on_sm_70() {
        // A module with no tensor-core or cp.async features should pass
        // validation even on sm_70.
        let mut module = PtxModule::new("sm_70");
        let k = PtxKernel::new("scalar_only");
        module.add_kernel(k);
        assert!(module.validate().is_ok());
    }

    #[test]
    fn validate_skips_unparseable_target() {
        // Don't block weird custom targets.
        let mut module = PtxModule::new("compute_90a");
        module.add_kernel(tc_kernel());
        assert!(module.validate().is_ok());
    }

    #[test]
    fn parse_sm_target() {
        let m = PtxModule::new("sm_89");
        assert_eq!(m.parse_sm_target(), Some(89));
        let m2 = PtxModule::new("sm_80");
        assert_eq!(m2.parse_sm_target(), Some(80));
        let m3 = PtxModule::new("compute_90a");
        assert_eq!(m3.parse_sm_target(), None);
    }

    fn mma_sync_with_bf16_tags() -> PtxKernel {
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("legacy_bf16_on_mma_sync");
        k.push(PtxInstruction::TensorCore(TensorCoreOp::MmaSync {
            d: alloc_c(&mut alloc),
            a: alloc_a_f16(&mut alloc),
            b: alloc_b_f16(&mut alloc),
            c: alloc_c(&mut alloc),
            shape: MmaShape::M16N8K16,
            d_ty: PtxType::F32,
            a_ty: PtxType::BF16,
            b_ty: PtxType::BF16,
            c_ty: PtxType::F32,
        }));
        k
    }

    #[test]
    fn validate_rejects_mma_sync_bf16_a_ty() {
        let mut module = PtxModule::new("sm_89");
        module.add_kernel(mma_sync_with_bf16_tags());
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::MmaSyncBf16Rejected { operand: "a_ty" }
        );
        assert_eq!(
            err.to_string(),
            "TensorCoreOp::MmaSync with PtxType::BF16 on a_ty is rejected; \
             use TensorCoreOp::MmaSyncBf16 for bf16 emission"
        );
    }

    #[test]
    fn validate_rejects_mma_sync_bf16_b_ty_only() {
        // Mixed: a_ty F16 + b_ty BF16 still rejected.
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("mixed_bf16_b_only");
        k.push(PtxInstruction::TensorCore(TensorCoreOp::MmaSync {
            d: alloc_c(&mut alloc),
            a: alloc_a_f16(&mut alloc),
            b: alloc_b_f16(&mut alloc),
            c: alloc_c(&mut alloc),
            shape: MmaShape::M16N8K16,
            d_ty: PtxType::F32,
            a_ty: PtxType::F16,
            b_ty: PtxType::BF16,
            c_ty: PtxType::F32,
        }));
        let mut module = PtxModule::new("sm_89");
        module.add_kernel(k);
        let err = module.validate().unwrap_err();
        assert_eq!(
            err,
            ValidationError::MmaSyncBf16Rejected { operand: "b_ty" }
        );
    }

    #[test]
    fn validate_rejects_mma_sync_bf16_even_on_unparseable_target() {
        // The dtype-routing check is target-agnostic — it fires regardless
        // of whether the target string is `sm_NN`.
        let mut module = PtxModule::new("compute_90a");
        module.add_kernel(mma_sync_with_bf16_tags());
        assert_eq!(
            module.validate().unwrap_err(),
            ValidationError::MmaSyncBf16Rejected { operand: "a_ty" }
        );
    }

    fn mma_sync_bf16_kernel() -> PtxKernel {
        use crate::fragment::{alloc_a_bf16, alloc_b_bf16};
        let mut alloc = RegisterAllocator::new();
        let mut k = PtxKernel::new("native_bf16");
        k.push(PtxInstruction::TensorCore(TensorCoreOp::MmaSyncBf16 {
            d: alloc_c(&mut alloc),
            a: alloc_a_bf16(&mut alloc),
            b: alloc_b_bf16(&mut alloc),
            c: alloc_c(&mut alloc),
        }));
        k
    }

    #[test]
    fn validate_accepts_mma_sync_bf16_dedicated_variant() {
        let mut module = PtxModule::new("sm_89");
        module.add_kernel(mma_sync_bf16_kernel());
        assert!(module.validate().is_ok());
    }
}