aprender-ptx-debug 0.37.0

Pure Rust PTX debugging and static analysis tool
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
//! PTX Type System definitions

/// PTX data types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PtxType {
    /// 8-bit signed integer
    S8,
    /// 16-bit signed integer
    S16,
    /// 32-bit signed integer
    S32,
    /// 64-bit signed integer
    S64,
    /// 8-bit unsigned integer
    U8,
    /// 16-bit unsigned integer
    U16,
    /// 32-bit unsigned integer
    U32,
    /// 64-bit unsigned integer
    U64,
    /// 16-bit floating point
    F16,
    /// 32-bit floating point
    F32,
    /// 64-bit floating point
    F64,
    /// 8-bit untyped
    B8,
    /// 16-bit untyped
    B16,
    /// 32-bit untyped
    B32,
    /// 64-bit untyped
    B64,
    /// Predicate (boolean)
    Pred,
}

impl PtxType {
    /// Size in bytes
    pub fn size_bytes(&self) -> usize {
        match self {
            PtxType::S8 | PtxType::U8 | PtxType::B8 => 1,
            PtxType::S16 | PtxType::U16 | PtxType::B16 | PtxType::F16 => 2,
            PtxType::S32 | PtxType::U32 | PtxType::B32 | PtxType::F32 => 4,
            PtxType::S64 | PtxType::U64 | PtxType::B64 | PtxType::F64 => 8,
            PtxType::Pred => 1,
        }
    }

    /// Is this a signed type
    pub fn is_signed(&self) -> bool {
        matches!(
            self,
            PtxType::S8 | PtxType::S16 | PtxType::S32 | PtxType::S64
        )
    }

    /// Is this a floating point type
    pub fn is_float(&self) -> bool {
        matches!(self, PtxType::F16 | PtxType::F32 | PtxType::F64)
    }

    /// Is this a 64-bit type
    pub fn is_64bit(&self) -> bool {
        matches!(
            self,
            PtxType::S64 | PtxType::U64 | PtxType::B64 | PtxType::F64
        )
    }
}

impl std::fmt::Display for PtxType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            PtxType::S8 => ".s8",
            PtxType::S16 => ".s16",
            PtxType::S32 => ".s32",
            PtxType::S64 => ".s64",
            PtxType::U8 => ".u8",
            PtxType::U16 => ".u16",
            PtxType::U32 => ".u32",
            PtxType::U64 => ".u64",
            PtxType::F16 => ".f16",
            PtxType::F32 => ".f32",
            PtxType::F64 => ".f64",
            PtxType::B8 => ".b8",
            PtxType::B16 => ".b16",
            PtxType::B32 => ".b32",
            PtxType::B64 => ".b64",
            PtxType::Pred => ".pred",
        };
        write!(f, "{}", s)
    }
}

/// Address space qualifiers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AddressSpace {
    /// Generic (unqualified) address
    Generic,
    /// Global memory
    Global,
    /// Shared memory (per-block)
    Shared,
    /// Local memory (per-thread)
    Local,
    /// Constant memory
    Const,
    /// Parameter space
    Param,
    /// Texture memory
    Texture,
    /// Surface memory
    Surface,
}

impl std::fmt::Display for AddressSpace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            AddressSpace::Generic => "",
            AddressSpace::Global => ".global",
            AddressSpace::Shared => ".shared",
            AddressSpace::Local => ".local",
            AddressSpace::Const => ".const",
            AddressSpace::Param => ".param",
            AddressSpace::Texture => ".tex",
            AddressSpace::Surface => ".surf",
        };
        write!(f, "{}", s)
    }
}

/// SM (Streaming Multiprocessor) target architecture
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SmTarget {
    /// Unknown/unspecified
    #[default]
    Unknown,
    /// SM 5.0 (Maxwell)
    Sm50,
    /// SM 5.2 (Maxwell)
    Sm52,
    /// SM 6.0 (Pascal)
    Sm60,
    /// SM 6.1 (Pascal)
    Sm61,
    /// SM 7.0 (Volta)
    Sm70,
    /// SM 7.5 (Turing)
    Sm75,
    /// SM 8.0 (Ampere)
    Sm80,
    /// SM 8.6 (Ampere)
    Sm86,
    /// SM 8.9 (Ada Lovelace)
    Sm89,
    /// SM 9.0 (Hopper)
    Sm90,
}

impl SmTarget {
    /// Minimum PTX version for this target
    pub fn min_ptx_version(&self) -> (u8, u8) {
        match self {
            SmTarget::Unknown => (1, 0),
            SmTarget::Sm50 | SmTarget::Sm52 => (4, 0),
            SmTarget::Sm60 | SmTarget::Sm61 => (5, 0),
            SmTarget::Sm70 => (6, 0),
            SmTarget::Sm75 => (6, 3),
            SmTarget::Sm80 | SmTarget::Sm86 => (7, 0),
            SmTarget::Sm89 => (7, 8),
            SmTarget::Sm90 => (8, 0),
        }
    }

    /// Does this target support Tensor Cores
    pub fn has_tensor_cores(&self) -> bool {
        matches!(
            self,
            SmTarget::Sm70
                | SmTarget::Sm75
                | SmTarget::Sm80
                | SmTarget::Sm86
                | SmTarget::Sm89
                | SmTarget::Sm90
        )
    }
}

/// PTX Opcodes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Opcode {
    // Data Movement
    /// Load
    Ld,
    /// Store
    St,
    /// Move
    Mov,
    /// Convert address space
    Cvta,
    /// Convert type
    Cvt,

    // Arithmetic
    /// Add
    Add,
    /// Subtract
    Sub,
    /// Multiply
    Mul,
    /// Divide
    Div,
    /// Remainder
    Rem,
    /// Multiply-add
    Mad,
    /// Fused multiply-add
    Fma,
    /// Negate
    Neg,
    /// Absolute value
    Abs,
    /// Minimum
    Min,
    /// Maximum
    Max,

    // Logic
    /// Bitwise AND
    And,
    /// Bitwise OR
    Or,
    /// Bitwise XOR
    Xor,
    /// Bitwise NOT
    Not,
    /// Shift left
    Shl,
    /// Shift right
    Shr,

    // Comparison
    /// Set predicate
    Setp,
    /// Select
    Selp,

    // Control Flow
    /// Branch
    Bra,
    /// Call function
    Call,
    /// Return
    Ret,
    /// Exit kernel
    Exit,

    // Synchronization
    /// Barrier
    Bar,
    /// Memory barrier
    MemBar,
    /// Atomic operation
    Atom,
    /// Reduction operation
    Red,

    // Special
    /// Texture load
    Tex,
    /// Texture load 4
    Tld4,
    /// Surface load
    Suld,
    /// Surface store
    Sust,
    /// Warp shuffle
    Shfl,
    /// Warp vote
    Vote,
    /// Matrix multiply-accumulate
    Mma,
    /// Warp MMA
    Wmma,
    /// Load matrix
    LdMatrix,
    /// Copy (async)
    Cp,
    /// Prefetch
    Prefetch,

    /// Unknown opcode
    Unknown,
}

impl Opcode {
    /// Is this a load instruction
    pub fn is_load(&self) -> bool {
        matches!(
            self,
            Opcode::Ld | Opcode::Tex | Opcode::Tld4 | Opcode::Suld | Opcode::LdMatrix
        )
    }

    /// Is this a store instruction
    pub fn is_store(&self) -> bool {
        matches!(self, Opcode::St | Opcode::Sust)
    }

    /// Is this a memory operation
    pub fn is_memory_op(&self) -> bool {
        self.is_load() || self.is_store() || matches!(self, Opcode::Atom | Opcode::Red)
    }

    /// Is this a synchronization instruction
    pub fn is_sync(&self) -> bool {
        matches!(self, Opcode::Bar | Opcode::MemBar)
    }

    /// Is this a branch instruction
    pub fn is_branch(&self) -> bool {
        matches!(
            self,
            Opcode::Bra | Opcode::Call | Opcode::Ret | Opcode::Exit
        )
    }
}

/// Instruction modifiers
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Modifier {
    // Address space
    /// .shared
    Shared,
    /// .global
    Global,
    /// .local
    Local,
    /// .const
    Const,
    /// .param
    Param,

    // Types
    /// .u32
    U32,
    /// .u64
    U64,
    /// .s32
    S32,
    /// .s64
    S64,
    /// .f32
    F32,
    /// .f64
    F64,
    /// .b32
    B32,
    /// .b64
    B64,

    // Synchronization
    /// .sync
    Sync,
    /// .cta
    Cta,
    /// .gl
    Gl,
    /// .sys
    Sys,

    // Atomic
    /// .add (atomic add)
    AtomicAdd,
    /// .cas (compare and swap)
    AtomicCas,
    /// .exch (exchange)
    AtomicExch,
    /// .min
    AtomicMin,
    /// .max
    AtomicMax,

    // Other
    /// Other modifier
    Other(String),
}

impl Modifier {
    /// Get the address space if this is an address space modifier
    pub fn as_address_space(&self) -> Option<AddressSpace> {
        match self {
            Modifier::Shared => Some(AddressSpace::Shared),
            Modifier::Global => Some(AddressSpace::Global),
            Modifier::Local => Some(AddressSpace::Local),
            Modifier::Const => Some(AddressSpace::Const),
            Modifier::Param => Some(AddressSpace::Param),
            _ => None,
        }
    }

    /// Get the type if this is a type modifier
    pub fn as_type(&self) -> Option<PtxType> {
        match self {
            Modifier::U32 => Some(PtxType::U32),
            Modifier::U64 => Some(PtxType::U64),
            Modifier::S32 => Some(PtxType::S32),
            Modifier::S64 => Some(PtxType::S64),
            Modifier::F32 => Some(PtxType::F32),
            Modifier::F64 => Some(PtxType::F64),
            Modifier::B32 => Some(PtxType::B32),
            Modifier::B64 => Some(PtxType::B64),
            _ => None,
        }
    }
}

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

    #[test]
    fn test_ptx_type_size() {
        assert_eq!(PtxType::U8.size_bytes(), 1);
        assert_eq!(PtxType::U16.size_bytes(), 2);
        assert_eq!(PtxType::U32.size_bytes(), 4);
        assert_eq!(PtxType::U64.size_bytes(), 8);
        assert_eq!(PtxType::F32.size_bytes(), 4);
        assert_eq!(PtxType::F64.size_bytes(), 8);
    }

    #[test]
    fn test_ptx_type_properties() {
        assert!(PtxType::S32.is_signed());
        assert!(!PtxType::U32.is_signed());
        assert!(PtxType::F32.is_float());
        assert!(!PtxType::U32.is_float());
        assert!(PtxType::U64.is_64bit());
        assert!(!PtxType::U32.is_64bit());
    }

    #[test]
    fn test_sm_target_ptx_version() {
        assert!(SmTarget::Sm90.min_ptx_version() >= (8, 0));
        assert!(SmTarget::Sm70.min_ptx_version() >= (6, 0));
    }

    #[test]
    fn test_opcode_categories() {
        assert!(Opcode::Ld.is_load());
        assert!(Opcode::St.is_store());
        assert!(Opcode::Bar.is_sync());
        assert!(Opcode::Bra.is_branch());
    }

    #[test]
    fn test_modifier_conversion() {
        assert_eq!(
            Modifier::Shared.as_address_space(),
            Some(AddressSpace::Shared)
        );
        assert_eq!(Modifier::U32.as_type(), Some(PtxType::U32));
    }
}