oxicuda-ptx 0.1.0

OxiCUDA PTX - PTX code generation DSL and IR for GPU kernel development
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
//! NVIDIA GPU architecture definitions and capability queries.
//!
//! This module provides [`SmVersion`] to identify target architectures from
//! Turing (sm_75) through Blackwell (sm_120), and [`ArchCapabilities`] for
//! querying hardware features such as tensor core availability, async copy
//! support, and maximum thread counts.

use std::fmt;

/// NVIDIA GPU Streaming Multiprocessor version.
///
/// Each variant corresponds to a CUDA compute capability and determines
/// the PTX ISA version, available instructions, and hardware features.
///
/// # Ordering
///
/// `SmVersion` derives `Ord` so that newer architectures compare greater
/// than older ones: `Sm80 > Sm75`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SmVersion {
    /// Turing (compute capability 7.5).
    Sm75,
    /// Ampere (compute capability 8.0).
    Sm80,
    /// Ampere `GA10x` (compute capability 8.6).
    Sm86,
    /// Ada Lovelace (compute capability 8.9).
    Sm89,
    /// Hopper (compute capability 9.0).
    Sm90,
    /// Hopper with accelerated features (compute capability 9.0a).
    Sm90a,
    /// Blackwell (compute capability 10.0).
    Sm100,
    /// Blackwell B200 / next-gen (compute capability 12.0).
    Sm120,
}

impl SmVersion {
    /// Returns the PTX target string (e.g. `"sm_80"`, `"sm_90a"`).
    #[must_use]
    pub const fn as_ptx_str(self) -> &'static str {
        match self {
            Self::Sm75 => "sm_75",
            Self::Sm80 => "sm_80",
            Self::Sm86 => "sm_86",
            Self::Sm89 => "sm_89",
            Self::Sm90 => "sm_90",
            Self::Sm90a => "sm_90a",
            Self::Sm100 => "sm_100",
            Self::Sm120 => "sm_120",
        }
    }

    /// Returns the PTX ISA version string appropriate for this architecture.
    ///
    /// The PTX version determines which instructions and features are available.
    /// Later architectures require higher PTX versions.
    #[must_use]
    pub const fn ptx_version(self) -> &'static str {
        match self {
            Self::Sm75 => "6.4",
            Self::Sm80 => "7.0",
            Self::Sm86 => "7.1",
            Self::Sm89 => "7.8",
            Self::Sm90 | Self::Sm90a => "8.0",
            Self::Sm100 => "8.5",
            Self::Sm120 => "8.7",
        }
    }

    /// Returns the PTX ISA version as a `(major, minor)` pair.
    ///
    /// This is useful for programmatic version comparisons rather than
    /// string parsing.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxicuda_ptx::arch::SmVersion;
    ///
    /// assert_eq!(SmVersion::Sm80.ptx_isa_version(), (7, 0));
    /// assert_eq!(SmVersion::Sm90.ptx_isa_version(), (8, 0));
    /// assert_eq!(SmVersion::Sm120.ptx_isa_version(), (8, 7));
    /// ```
    #[must_use]
    pub const fn ptx_isa_version(self) -> (u32, u32) {
        match self {
            Self::Sm75 => (6, 4),
            Self::Sm80 => (7, 0),
            Self::Sm86 => (7, 1),
            Self::Sm89 => (7, 8),
            Self::Sm90 | Self::Sm90a => (8, 0),
            Self::Sm100 => (8, 5),
            Self::Sm120 => (8, 7),
        }
    }

    /// Returns the architecture capabilities for this SM version.
    #[must_use]
    pub const fn capabilities(self) -> ArchCapabilities {
        ArchCapabilities::for_sm(self)
    }

    /// Converts a CUDA compute capability pair to an `SmVersion`.
    ///
    /// Returns `None` if the compute capability is not recognized.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxicuda_ptx::arch::SmVersion;
    ///
    /// assert_eq!(SmVersion::from_compute_capability(8, 0), Some(SmVersion::Sm80));
    /// assert_eq!(SmVersion::from_compute_capability(7, 5), Some(SmVersion::Sm75));
    /// assert_eq!(SmVersion::from_compute_capability(6, 0), None);
    /// ```
    #[must_use]
    pub const fn from_compute_capability(major: i32, minor: i32) -> Option<Self> {
        match (major, minor) {
            (7, 5) => Some(Self::Sm75),
            (8, 0) => Some(Self::Sm80),
            (8, 6) => Some(Self::Sm86),
            (8, 9) => Some(Self::Sm89),
            (9, 0) => Some(Self::Sm90),
            (10, 0) => Some(Self::Sm100),
            (12, 0) => Some(Self::Sm120),
            _ => None,
        }
    }

    /// Returns the maximum number of threads per block for this architecture.
    #[must_use]
    pub const fn max_threads_per_block(self) -> u32 {
        1024
    }

    /// Returns the maximum number of threads per SM for this architecture.
    #[must_use]
    pub const fn max_threads_per_sm(self) -> u32 {
        match self {
            Self::Sm75 => 1024,
            Self::Sm89 => 1536,
            Self::Sm80 | Self::Sm86 | Self::Sm90 | Self::Sm90a | Self::Sm100 | Self::Sm120 => 2048,
        }
    }

    /// Returns the warp size for this architecture (always 32).
    #[must_use]
    pub const fn warp_size(self) -> u32 {
        32
    }

    /// Returns the maximum shared memory per block in bytes.
    #[must_use]
    pub const fn max_shared_mem_per_block(self) -> u32 {
        match self {
            Self::Sm75 => 65536,
            Self::Sm80 | Self::Sm86 => 163_840,
            Self::Sm89 => 101_376,
            Self::Sm90 | Self::Sm90a | Self::Sm100 | Self::Sm120 => 232_448,
        }
    }
}

impl fmt::Display for SmVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_ptx_str())
    }
}

/// Hardware capabilities for a specific GPU architecture.
///
/// Query this struct to determine whether a given feature (tensor cores,
/// async copy, TMA, etc.) is available on the target architecture before
/// emitting instructions that require it.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArchCapabilities {
    /// Whether the architecture supports `mma.sync` tensor core instructions.
    pub has_tensor_cores: bool,
    /// Whether `cp.async` (asynchronous global-to-shared copy) is supported.
    pub has_cp_async: bool,
    /// Whether `ldmatrix` (warp-cooperative shared memory load) is supported.
    pub has_ldmatrix: bool,
    /// Whether `mma.sync.aligned.m16n8k16` (Ampere MMA shapes) is supported.
    pub has_ampere_mma: bool,
    /// Whether WGMMA (warp-group MMA, Hopper) instructions are supported.
    pub has_wgmma: bool,
    /// Whether TMA (Tensor Memory Accelerator, Hopper) is supported.
    pub has_tma: bool,
    /// Whether FP8 (E4M3/E5M2) data types are supported.
    pub has_fp8: bool,
    /// Whether FP6/FP4 narrow floating-point types are supported (Blackwell).
    pub has_fp6_fp4: bool,
    /// Whether dynamic shared memory (`extern __shared__`) is supported.
    pub has_dynamic_smem: bool,
    /// Whether `bar.sync` with named barriers is supported.
    pub has_named_barriers: bool,
    /// Whether `fence.mbarrier` and related cluster barriers are supported.
    pub has_cluster_barriers: bool,
    /// Whether `stmatrix` (store matrix to shared memory) is supported (SM >= 90).
    pub has_stmatrix: bool,
    /// Whether `redux.sync` (warp-level reduction) is supported (SM >= 80).
    pub has_redux: bool,
    /// Whether `elect.sync` (warp leader election) is supported (SM >= 90).
    pub has_elect_one: bool,
    /// Whether `griddepcontrol` (grid dependency control) is supported (SM >= 90).
    pub has_griddepcontrol: bool,
    /// Whether `setmaxnreg` (set max register count) is supported (SM >= 90).
    pub has_setmaxnreg: bool,
    /// Whether bulk async copy operations are supported (SM >= 90).
    pub has_bulk_copy: bool,
    /// Whether SM 120 (Rubin) specific features are available.
    pub has_sm120_features: bool,
}

impl ArchCapabilities {
    /// Returns the capabilities for the given SM version.
    #[must_use]
    #[allow(clippy::too_many_lines)]
    pub const fn for_sm(sm: SmVersion) -> Self {
        match sm {
            SmVersion::Sm75 => Self {
                has_tensor_cores: true,
                has_cp_async: false,
                has_ldmatrix: true,
                has_ampere_mma: false,
                has_wgmma: false,
                has_tma: false,
                has_fp8: false,
                has_fp6_fp4: false,
                has_dynamic_smem: true,
                has_named_barriers: true,
                has_cluster_barriers: false,
                has_stmatrix: false,
                has_redux: false,
                has_elect_one: false,
                has_griddepcontrol: false,
                has_setmaxnreg: false,
                has_bulk_copy: false,
                has_sm120_features: false,
            },
            SmVersion::Sm80 | SmVersion::Sm86 => Self {
                has_tensor_cores: true,
                has_cp_async: true,
                has_ldmatrix: true,
                has_ampere_mma: true,
                has_wgmma: false,
                has_tma: false,
                has_fp8: false,
                has_fp6_fp4: false,
                has_dynamic_smem: true,
                has_named_barriers: true,
                has_cluster_barriers: false,
                has_stmatrix: false,
                has_redux: true,
                has_elect_one: false,
                has_griddepcontrol: false,
                has_setmaxnreg: false,
                has_bulk_copy: false,
                has_sm120_features: false,
            },
            SmVersion::Sm89 => Self {
                has_tensor_cores: true,
                has_cp_async: true,
                has_ldmatrix: true,
                has_ampere_mma: true,
                has_wgmma: false,
                has_tma: false,
                has_fp8: true,
                has_fp6_fp4: false,
                has_dynamic_smem: true,
                has_named_barriers: true,
                has_cluster_barriers: false,
                has_stmatrix: false,
                has_redux: true,
                has_elect_one: false,
                has_griddepcontrol: false,
                has_setmaxnreg: false,
                has_bulk_copy: false,
                has_sm120_features: false,
            },
            SmVersion::Sm90 | SmVersion::Sm90a => Self {
                has_tensor_cores: true,
                has_cp_async: true,
                has_ldmatrix: true,
                has_ampere_mma: true,
                has_wgmma: true,
                has_tma: true,
                has_fp8: true,
                has_fp6_fp4: false,
                has_dynamic_smem: true,
                has_named_barriers: true,
                has_cluster_barriers: true,
                has_stmatrix: true,
                has_redux: true,
                has_elect_one: true,
                has_griddepcontrol: true,
                has_setmaxnreg: true,
                has_bulk_copy: true,
                has_sm120_features: false,
            },
            SmVersion::Sm100 => Self {
                has_tensor_cores: true,
                has_cp_async: true,
                has_ldmatrix: true,
                has_ampere_mma: true,
                has_wgmma: true,
                has_tma: true,
                has_fp8: true,
                has_fp6_fp4: true,
                has_dynamic_smem: true,
                has_named_barriers: true,
                has_cluster_barriers: true,
                has_stmatrix: true,
                has_redux: true,
                has_elect_one: true,
                has_griddepcontrol: true,
                has_setmaxnreg: true,
                has_bulk_copy: true,
                has_sm120_features: false,
            },
            SmVersion::Sm120 => Self {
                has_tensor_cores: true,
                has_cp_async: true,
                has_ldmatrix: true,
                has_ampere_mma: true,
                has_wgmma: true,
                has_tma: true,
                has_fp8: true,
                has_fp6_fp4: true,
                has_dynamic_smem: true,
                has_named_barriers: true,
                has_cluster_barriers: true,
                has_stmatrix: true,
                has_redux: true,
                has_elect_one: true,
                has_griddepcontrol: true,
                has_setmaxnreg: true,
                has_bulk_copy: true,
                has_sm120_features: true,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sm_version_ordering() {
        assert!(SmVersion::Sm80 > SmVersion::Sm75);
        assert!(SmVersion::Sm90a > SmVersion::Sm90);
        assert!(SmVersion::Sm120 > SmVersion::Sm100);
    }

    #[test]
    fn ptx_version_strings() {
        assert_eq!(SmVersion::Sm75.ptx_version(), "6.4");
        assert_eq!(SmVersion::Sm80.ptx_version(), "7.0");
        assert_eq!(SmVersion::Sm86.ptx_version(), "7.1");
        assert_eq!(SmVersion::Sm90.ptx_version(), "8.0");
        assert_eq!(SmVersion::Sm100.ptx_version(), "8.5");
        assert_eq!(SmVersion::Sm120.ptx_version(), "8.7");
    }

    #[test]
    fn from_compute_capability_valid() {
        assert_eq!(
            SmVersion::from_compute_capability(7, 5),
            Some(SmVersion::Sm75)
        );
        assert_eq!(
            SmVersion::from_compute_capability(8, 0),
            Some(SmVersion::Sm80)
        );
        assert_eq!(
            SmVersion::from_compute_capability(9, 0),
            Some(SmVersion::Sm90)
        );
    }

    #[test]
    fn from_compute_capability_unknown() {
        assert_eq!(SmVersion::from_compute_capability(6, 0), None);
        assert_eq!(SmVersion::from_compute_capability(5, 2), None);
    }

    #[test]
    fn capabilities_turing() {
        let caps = SmVersion::Sm75.capabilities();
        assert!(caps.has_tensor_cores);
        assert!(!caps.has_cp_async);
        assert!(!caps.has_ampere_mma);
        assert!(!caps.has_wgmma);
    }

    #[test]
    fn capabilities_ampere() {
        let caps = SmVersion::Sm80.capabilities();
        assert!(caps.has_tensor_cores);
        assert!(caps.has_cp_async);
        assert!(caps.has_ampere_mma);
        assert!(!caps.has_wgmma);
        assert!(!caps.has_fp8);
    }

    #[test]
    fn capabilities_hopper() {
        let caps = SmVersion::Sm90a.capabilities();
        assert!(caps.has_wgmma);
        assert!(caps.has_tma);
        assert!(caps.has_fp8);
        assert!(!caps.has_fp6_fp4);
        assert!(caps.has_cluster_barriers);
    }

    #[test]
    fn capabilities_blackwell() {
        let caps = SmVersion::Sm100.capabilities();
        assert!(caps.has_fp6_fp4);
        assert!(caps.has_wgmma);
        assert!(caps.has_tma);
    }

    #[test]
    fn display_sm_version() {
        assert_eq!(format!("{}", SmVersion::Sm80), "sm_80");
        assert_eq!(format!("{}", SmVersion::Sm90a), "sm_90a");
    }

    #[test]
    fn shared_memory_limits() {
        assert_eq!(SmVersion::Sm75.max_shared_mem_per_block(), 65536);
        assert_eq!(SmVersion::Sm80.max_shared_mem_per_block(), 163_840);
        assert_eq!(SmVersion::Sm90.max_shared_mem_per_block(), 232_448);
    }

    #[test]
    fn ptx_isa_version_all_sm() {
        assert_eq!(SmVersion::Sm75.ptx_isa_version(), (6, 4));
        assert_eq!(SmVersion::Sm80.ptx_isa_version(), (7, 0));
        assert_eq!(SmVersion::Sm86.ptx_isa_version(), (7, 1));
        assert_eq!(SmVersion::Sm89.ptx_isa_version(), (7, 8));
        assert_eq!(SmVersion::Sm90.ptx_isa_version(), (8, 0));
        assert_eq!(SmVersion::Sm90a.ptx_isa_version(), (8, 0));
        assert_eq!(SmVersion::Sm100.ptx_isa_version(), (8, 5));
        assert_eq!(SmVersion::Sm120.ptx_isa_version(), (8, 7));
    }

    #[test]
    fn capabilities_new_fields_turing() {
        let caps = SmVersion::Sm75.capabilities();
        assert!(!caps.has_redux);
        assert!(!caps.has_stmatrix);
        assert!(!caps.has_elect_one);
        assert!(!caps.has_griddepcontrol);
        assert!(!caps.has_setmaxnreg);
        assert!(!caps.has_bulk_copy);
        assert!(!caps.has_sm120_features);
    }

    #[test]
    fn capabilities_new_fields_ampere() {
        let caps = SmVersion::Sm80.capabilities();
        assert!(caps.has_redux);
        assert!(!caps.has_stmatrix);
        assert!(!caps.has_elect_one);
        assert!(!caps.has_griddepcontrol);
        assert!(!caps.has_sm120_features);
    }

    #[test]
    fn capabilities_new_fields_hopper() {
        let caps = SmVersion::Sm90.capabilities();
        assert!(caps.has_redux);
        assert!(caps.has_stmatrix);
        assert!(caps.has_elect_one);
        assert!(caps.has_griddepcontrol);
        assert!(caps.has_setmaxnreg);
        assert!(caps.has_bulk_copy);
        assert!(!caps.has_sm120_features);
    }

    #[test]
    fn capabilities_new_fields_sm120() {
        let caps = SmVersion::Sm120.capabilities();
        assert!(caps.has_redux);
        assert!(caps.has_stmatrix);
        assert!(caps.has_elect_one);
        assert!(caps.has_griddepcontrol);
        assert!(caps.has_setmaxnreg);
        assert!(caps.has_bulk_copy);
        assert!(caps.has_sm120_features);
    }
}