ptx-90-parser 0.4.3

Parse NVIDIA PTX 9.0 assembly into a structured AST and explore modules via a CLI.
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
use super::common::{Instruction, Label};
use super::variable::{ParameterDirective, VariableDirective};
use crate::Spanned;
use crate::parser::Span;
use crate::r#type::{AttributeDirective, DataType, FunctionSymbol, VariableSymbol};

/// Alias directive relating one function symbol to another.
///
/// Syntax:
/// .alias fAlias, fAliasee;
///
/// Example:
/// .alias foo, bar;
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct AliasFunctionDirective {
    pub alias: FunctionSymbol,
    pub target: FunctionSymbol,
    pub span: Span,
}

/// A PTX kernel declared with the `.entry` directive.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct FuncFunctionDirective {
    /// Example:
    /// .func .attribute(.unified(0xAB, 0xCD)) bar() { ... }
    pub attributes: Vec<AttributeDirective>,
    /// Optional return param.
    ///
    /// Example:
    /// .func (.param .u32 rval) bar(.param .u32 N, .param .align 4 .b8 numbers[]) { ... }
    pub return_param: Option<ParameterDirective>,
    /// Function name.
    pub name: FunctionSymbol,
    /// Function parameters.
    ///
    /// Example:
    /// .func (.param .u32 rval) bar(.param .u32 N, .param .align 4 .b8 numbers[])
    pub params: Vec<ParameterDirective>,
    /// Optional directives.
    ///
    /// Example:
    /// .func foo (.reg .b32 N, .reg .f64 dbl) .noreturn { ... }
    pub directives: Vec<FuncFunctionHeaderDirective>,
    /// Optional function body. Without body represents a function prototype.
    pub body: Option<FunctionBody>,
    pub span: Span,
}

/// A PTX device function declared with the `.func` directive.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct EntryFunctionDirective {
    /// Name of the entry function.
    pub name: FunctionSymbol,
    /// Function parameters.
    pub params: Vec<ParameterDirective>,
    /// Optional directives.
    pub directives: Vec<EntryFunctionHeaderDirective>,
    /// Optional function body. Without body represents a function prototype.
    pub body: Option<FunctionBody>,
    pub span: Span,
}

/// Directive tokens that may decorate a PTX function header.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub enum FuncFunctionHeaderDirective {
    /// Syntax:
    /// .noreturn
    ///
    /// Example:
    /// .func foo .noreturn { ... }
    NoReturn { span: Span },
    /// Syntax:
    /// .pragma list-of-strings ;
    ///
    /// Example:
    ///.entry foo .pragma "nounroll"; { ... } // disable unrolling for current kernel
    Pragma { args: Vec<String>, span: Span },
    /// Syntax:
    /// .abi_preserve N
    ///
    /// Example:
    /// .entry foo .abi_preserve 8 { ... }
    AbiPreserve { value: u32, span: Span },
    /// Syntax:
    /// .abi_preserve_control N
    ///
    /// Example:
    /// .entry foo .abi_preserve_control 16 { ... }
    AbiPreserveControl { value: u32, span: Span },
}

/// Directive tokens that may decorate a PTX function header.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub enum EntryFunctionHeaderDirective {
    /// Syntax:
    /// .maxnreg n
    ///
    /// Example:
    /// .entry foo .maxnreg 16 { ... }  // max regs per thread = 16
    MaxNReg { value: u32, span: Span },
    /// Syntax:
    /// .maxntid nx
    /// .maxntid nx, ny
    /// .maxntid nx, ny, nz
    ///
    /// Example:
    /// .entry foo .maxntid 256       { ... }  // max threads = 256
    /// .entry bar .maxntid 16,16,4   { ... }  // max threads = 1024
    MaxNTid { dim: FunctionDim, span: Span },
    /// Syntax:
    /// .reqntid nx
    /// .reqntid nx, ny
    /// .reqntid nx, ny, nz
    ///
    /// Example:
    /// .entry foo .reqntid 256       { ... }  // num threads = 256
    /// .entry bar .reqntid 16,16,4   { ... }  // num threads = 1024
    ReqNTid { dim: FunctionDim, span: Span },
    /// Syntax:
    /// .minnctapersm ncta
    ///
    /// Example:
    /// .entry foo .maxntid 256 .minnctapersm 4 { ... }
    MinNCtaPerSm { value: u32, span: Span },
    /// Syntax:
    /// .maxnctapersm ncta
    ///
    /// Example:
    /// .entry foo .maxntid 256 .maxnctapersm 4 { ... }
    MaxNCtaPerSm { value: u32, span: Span },
    /// Syntax:
    /// .pragma list-of-strings ;
    ///
    /// Example:
    ///.entry foo .pragma "nounroll"; { ... } // disable unrolling for current kernel
    Pragma { args: Vec<String>, span: Span },
    /// Syntax:
    /// .reqnctapercluster nx
    /// .reqnctapercluster nx, ny
    /// .reqnctapercluster nx, ny, nz
    ///
    /// Example:
    /// .entry foo .reqnctapercluster 2         { . . . }
    /// .entry bar .reqnctapercluster 2, 2, 1   { . . . }
    /// .entry ker .reqnctapercluster 3, 2      { . . . }
    ReqNctaPerCluster { dim: FunctionDim, span: Span },
    /// Syntax:
    /// .explicitcluster
    ///
    /// Example:
    /// .entry foo .explicitcluster         { . . . }
    ExplicitCluster { span: Span },
    /// Syntax:
    /// .maxclusterrank n
    ///
    /// Example:
    /// .entry foo ..maxclusterrank 8         { . . . }
    MaxClusterRank { value: u32, span: Span },
    /// Syntax:
    ///.blocksareclusters
    ///
    /// Example:
    /// .entry foo .reqntid 32, 32, 1 .reqnctapercluster 32, 32, 1 .blocksareclusters { ... } // only allowed when with .reqnctapercluster and .reqntid
    BlocksAreClusters { span: Span },
}

/// Statements contained within a PTX function body.
#[derive(Debug, Clone, Default, PartialEq, Spanned)]
pub struct FunctionBody {
    pub statements: Vec<FunctionStatement>,
    pub span: Span,
}

/// Nested statement block enclosed in braces.
/// Executable items that appear within a function body.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub enum FunctionStatement {
    Label {
        label: Label,
        span: Span,
    },
    Directive {
        directive: StatementDirective,
        span: Span,
    },
    Instruction {
        instruction: Instruction,
        span: Span,
    },
    Block {
        statements: Vec<FunctionStatement>,
        span: Span,
    },
}

/// Directive that declares a register variable inside a function body.
///
/// Syntax:
/// .reg .ty name<range>
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct RegisterDirective {
    pub ty: DataType,
    pub registers: Vec<RegisterTarget>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct RegisterTarget {
    pub name: VariableSymbol,
    pub range: Option<u32>,
    pub span: Span,
}

/// Directive that applies to individual statements.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub enum StatementDirective {
    Loc {
        directive: LocationDirective,
        span: Span,
    },
    Pragma {
        directive: PragmaDirective,
        span: Span,
    },
    Section {
        directive: SectionDirective,
        span: Span,
    },
    Reg {
        directive: RegisterDirective,
        span: Span,
    },
    Local {
        directive: VariableDirective,
        span: Span,
    },
    Param {
        directive: VariableDirective,
        span: Span,
    },
    Shared {
        directive: VariableDirective,
        span: Span,
    },
    Dwarf {
        directive: DwarfDirective,
        span: Span,
    },
    BranchTargets {
        directive: BranchTargetsDirective,
        span: Span,
    },
    CallTargets {
        directive: CallTargetsDirective,
        span: Span,
    },
    CallPrototype {
        directive: CallPrototypeDirective,
        span: Span,
    },
}

/// Raw dwarf directive emitted by the compiler (e.g. `@@dwarf`).
///
/// Syntax:
/// ```text
/// @@DWARF dwarf-string
///
/// dwarf-string may have one of the
/// .byte   byte-list   // comma-separated hexadecimal byte values
/// .4byte  int32-list  // comma-separated hexadecimal integers in range [0..2^32-1]
/// .quad   int64-list  // comma-separated hexadecimal integers in range [0..2^64-1]
/// .4byte  label
/// .quad   label
/// ```
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct DwarfDirective {
    pub kind: DwarfDirectiveKind,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DwarfDirectiveKind {
    ByteValues(Vec<u8>),
    FourByteValues(Vec<u32>),
    QuadValues(Vec<u64>),
    FourByteLabel(Label),
    QuadLabel(Label),
}

/// Structured representation of a `.section` directive inside a function body.
///
/// Syntax:
/// ```text
/// .section section_name { dwarf-lines }
///
/// dwarf-lines have the following formats:
///   .b8    byte-list       // integers in [-128..255]
///   .b16   int16-list      // integers in [-2^15..2^16-1]
///   .b32   int32-list      // integers in [-2^31..2^32-1]
///   label:                 // define label inside the debug section
///   .b64   int64-list      // integers in [-2^63..2^64-1]
///   .b32   label
///   .b64   label
///   .b32   label+imm       // label plus constant integer byte offset (32-bit)
///   .b64   label+imm       // label plus constant integer byte offset (64-bit)
///   .b32   label1-label2   // difference between labels in same section (32-bit)
///   .b64   label3-label4   // difference between labels in same section (64-bit)
/// ```
///
/// Example:
/// ```text
///     .section .debug_str {
///    info_string0:
///     .b8 95  // _
///     .b8 90  // z
///     .b8 51  // 3
///     .b8 102 // f
///     .b8 111 // o
///     .b8 111 // o
///     .b8 118 // v
///     .b8 0
///    info_string1:
///     .b8 95  // _
///     .b8 90  // z
///     .b8 51  // 3
///     .b8 98  // b
///     .b8 97  // a
///     .b8 114 // r
///     .b8 118 // v
///     .b8 0
///     .b8 95  // _
///     .b8 90  // z
///     .b8 51  // 3
///     .b8 99  // c
///     .b8 97  // a
///     .b8 114 // r
///     .b8 118 // v
///     .b8 0
///    }
/// ```
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct SectionDirective {
    pub name: String,
    pub entries: Vec<SectionEntry>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq)]
pub enum SectionEntry {
    Label { label: Label, span: Span },
    Directive(StatementSectionDirectiveLine),
}

#[derive(Debug, Clone, PartialEq, Spanned)]
pub enum StatementSectionDirectiveLine {
    B8 { values: Vec<i16>, span: Span },
    B16 { values: Vec<i32>, span: Span },
    B32Immediate { values: Vec<i64>, span: Span },
    B64Immediate { values: Vec<i128>, span: Span },
    B32Label { labels: Label, span: Span },
    B64Label { labels: Label, span: Span },
    B32LabelPlusImm { entries: (Label, i32), span: Span },
    B64LabelPlusImm { entries: (Label, i64), span: Span },
    B32LabelDiff { entries: (Label, Label), span: Span },
    B64LabelDiff { entries: (Label, Label), span: Span },
}

/// Structured representation of a `.loc` directive inside a PTX function.
///
/// Syntax:
///     .loc file_index line_number column_position
///     .loc file_index line_number column_position,function_name label {+ immediate }, inlined_at file_index2 line_number2 column_position2
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct LocationDirective {
    pub file_index: u32,
    pub line: u32,
    pub column: u32,
    pub inlined_at: Option<LocationInlinedAt>,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct LocationInlinedAt {
    pub file_index: u32,
    pub line: u32,
    pub column: u32,
    pub function_name: FunctionSymbol,
    pub label: Label,
    pub label_offset: Option<i64>,
    pub span: Span,
}

/// Structured representation of a `.pragma` directive.
///
/// Syntax:
///     .pragma "nounroll";
///     .pragma "used_bytes_mask mask";
///     .pragma "enable_smem_spilling";
///     .pragma "frequency n";
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct PragmaDirective {
    pub kind: PragmaDirectiveKind,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq)]
pub enum PragmaDirectiveKind {
    Nounroll,
    UsedBytesMask { mask: String },
    EnableSmemSpilling,
    Frequency { value: u32 },
    Raw(String),
}

/// Structured representation of a `.branchtargets` directive.
///
/// Syntax:
///    .branchtargets label1, label2, label3, ...;
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct BranchTargetsDirective {
    pub labels: Vec<Label>,
    pub span: Span,
}

/// Structured representation of a `.calltargets` directive.
///
/// Syntax:
///     .calltargets func1, func2, func3, ...;
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct CallTargetsDirective {
    pub targets: Vec<FunctionSymbol>,
    pub span: Span,
}

/// Structured representation of a `.callprototype` directive.
///
/// Syntax:
///     // no input or return parameters
///     label: .callprototype _ .noreturn {.abi_preserve N} {.abi_preserve_control N};
///     // input params, no return params
///     label: .callprototype _ (param-list) .noreturn {.abi_preserve N} {.abi_preserve_control N};
///     // no input params, // return params
///     label: .callprototype (ret-param) _ {.abi_preserve N} {.abi_preserve_control N};
///     // input, return parameters
///     label: .callprototype (ret-param) _ (param-list) {.abi_preserve N} {.abi_preserve_control N};
#[derive(Debug, Clone, PartialEq, Spanned)]
pub struct CallPrototypeDirective {
    pub return_param: Option<ParameterDirective>,
    pub params: Vec<ParameterDirective>,
    pub noreturn: bool,
    pub abi_preserve: Option<u32>,
    pub abi_preserve_control: Option<u32>,
    pub span: Span,
}

/// Dimension triplet used by several function header directives.
#[derive(Debug, Clone, PartialEq, Spanned)]
pub enum FunctionDim {
    X { x: u32, span: Span },
    XY { x: u32, y: u32, span: Span },
    XYZ { x: u32, y: u32, z: u32, span: Span },
}