ptxlint 0.0.4

Static analysis and lints for NVIDIA PTX, no GPU needed. 不需要显卡的 NVIDIA PTX 静态分析工具
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
//! Per-kernel resource metrics and an occupancy model.

use crate::parse::{type_bits, Inst, Kernel};
use std::collections::BTreeMap;

/// Hardware limits per SM. Numbers from the CUDA C Programming Guide's
/// "Compute Capabilities" table.
#[derive(Debug, Clone, Copy)]
pub struct Arch {
    pub name: &'static str,
    pub max_warps_per_sm: u32,
    pub max_blocks_per_sm: u32,
    pub regs_per_sm: u32,
    pub max_regs_per_thread: u32,
    pub shared_per_sm: u32,
    pub max_shared_per_block: u32,
    /// True for GeForce parts, where FP64 runs at 1/64 of FP32.
    pub weak_fp64: bool,
}

const ARCHS: &[Arch] = &[
    Arch {
        name: "sm_70",
        max_warps_per_sm: 64,
        max_blocks_per_sm: 32,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 98304,
        max_shared_per_block: 98304,
        weak_fp64: false,
    },
    Arch {
        name: "sm_72",
        max_warps_per_sm: 64,
        max_blocks_per_sm: 32,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 98304,
        max_shared_per_block: 98304,
        weak_fp64: true,
    },
    Arch {
        name: "sm_75",
        max_warps_per_sm: 32,
        max_blocks_per_sm: 16,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 65536,
        max_shared_per_block: 65536,
        weak_fp64: true,
    },
    Arch {
        name: "sm_80",
        max_warps_per_sm: 64,
        max_blocks_per_sm: 32,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 167936,
        max_shared_per_block: 166912,
        weak_fp64: false,
    },
    Arch {
        name: "sm_86",
        max_warps_per_sm: 48,
        max_blocks_per_sm: 16,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 102400,
        max_shared_per_block: 101376,
        weak_fp64: true,
    },
    Arch {
        name: "sm_87",
        max_warps_per_sm: 48,
        max_blocks_per_sm: 16,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 167936,
        max_shared_per_block: 166912,
        weak_fp64: true,
    },
    Arch {
        name: "sm_89",
        max_warps_per_sm: 48,
        max_blocks_per_sm: 24,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 102400,
        max_shared_per_block: 101376,
        weak_fp64: true,
    },
    Arch {
        name: "sm_90",
        max_warps_per_sm: 64,
        max_blocks_per_sm: 32,
        regs_per_sm: 65536,
        max_regs_per_thread: 255,
        shared_per_sm: 233472,
        max_shared_per_block: 232448,
        weak_fp64: false,
    },
];

pub const DEFAULT_ARCH: &str = "sm_80";

impl Arch {
    /// Look up by `sm_XX`, tolerating suffixes like `sm_90a`.
    pub fn lookup(target: &str) -> Option<Arch> {
        // `.target sm_52, debug` — the architecture is the first entry.
        let t = target.split(',').next().unwrap_or(target).trim();
        let t = t.strip_suffix(['a', 'f']).unwrap_or(t);
        ARCHS.iter().copied().find(|a| a.name == t)
    }

    pub fn lookup_or_default(target: Option<&str>) -> (Arch, bool) {
        match target.and_then(Arch::lookup) {
            Some(a) => (a, true),
            None => (Arch::lookup(DEFAULT_ARCH).unwrap(), false),
        }
    }
}

/// Where the register count came from — this changes how much you should trust it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegSource {
    /// Exact, from `ptxas -v`.
    Ptxas,
    /// Estimated from virtual register declarations in the PTX. Virtual
    /// registers are in SSA-ish form and get coalesced by ptxas, so this is an
    /// upper bound, often a loose one.
    VirtualUpperBound,
}

#[derive(Debug, Clone, Default)]
pub struct InstMix {
    pub total: u32,
    pub fp32: u32,
    pub fp64: u32,
    pub fp16: u32,
    pub int: u32,
    pub int_divrem: u32,
    pub branch: u32,
    pub predicated: u32,
    pub sync: u32,
    pub atomic: u32,
    pub tensor: u32,
    pub calls: u32,
    /// Loads/stores per state space.
    pub mem: BTreeMap<String, u32>,
    /// Global accesses that move <= 4 bytes per thread per instruction.
    pub narrow_global: u32,
    pub global_access: u32,
}

#[derive(Debug, Clone)]
pub struct Metrics {
    pub arch: Arch,
    pub arch_known: bool,
    pub regs_per_thread: u32,
    pub reg_source: RegSource,
    pub spill_bytes: Option<u64>,
    pub local_bytes: u64,
    pub shared_bytes: u64,
    pub block_size: u32,
    pub block_size_from_ptx: bool,
    pub mix: InstMix,
}

fn classify(insts: &[Inst]) -> InstMix {
    let mut m = InstMix::default();
    for i in insts {
        m.total += 1;
        if i.predicated {
            m.predicated += 1;
        }
        let ty = i.ty().unwrap_or("");
        let is_mem = matches!(
            i.base.as_str(),
            "ld" | "st" | "atom" | "red" | "cp" | "prefetch"
        );
        if matches!(i.base.as_str(), "bra" | "brx") {
            m.branch += 1;
        }
        if i.base == "bar" || i.base == "barrier" || i.base == "membar" || i.base == "fence" {
            m.sync += 1;
        }
        if i.base == "atom" || i.base == "red" {
            m.atomic += 1;
        }
        if matches!(
            i.base.as_str(),
            "wmma" | "mma" | "ldmatrix" | "stmatrix" | "wgmma"
        ) {
            m.tensor += 1;
        }
        if i.base == "call" {
            m.calls += 1;
        }
        if matches!(i.base.as_str(), "div" | "rem") && ty.starts_with(['s', 'u']) {
            m.int_divrem += 1;
        }
        if !is_mem && i.base != "cvt" && i.base != "mov" {
            match ty {
                "f64" => m.fp64 += 1,
                "f32" => m.fp32 += 1,
                "f16" | "f16x2" | "bf16" | "bf16x2" => m.fp16 += 1,
                t if t.starts_with(['s', 'u']) => m.int += 1,
                _ => {}
            }
        }
        // cvt.f64.* and mov.f64 still occupy the FP64 pipe.
        if (i.base == "cvt" || i.base == "mov") && i.quals.iter().any(|q| q == "f64") {
            m.fp64 += 1;
        }
        if is_mem {
            if let Some(space) = i.space() {
                *m.mem.entry(space.to_string()).or_insert(0) += 1;
                if space == "global" && matches!(i.base.as_str(), "ld" | "st") {
                    m.global_access += 1;
                    let bytes = type_bits(i.ty().unwrap_or("b32")) / 8 * i.vector_width();
                    if bytes <= 4 {
                        m.narrow_global += 1;
                    }
                }
            }
        }
    }
    m
}

/// Sum of declared virtual registers, converted to 32-bit register slots.
/// Predicate registers are allocated separately on real hardware, so they are
/// excluded.
pub fn virtual_regs(k: &Kernel) -> u32 {
    k.regs
        .iter()
        .filter(|(ty, _)| *ty != "pred")
        .map(|(ty, n)| {
            let slots = type_bits(ty).div_ceil(32).max(1);
            n * slots
        })
        .sum()
}

pub struct Options {
    pub arch: Option<String>,
    pub block_size: Option<u32>,
    /// Exact per-kernel register counts and spill bytes from `ptxas -v`.
    pub ptxas: BTreeMap<String, (u32, u64)>,
}

pub fn analyse(
    k: &Kernel,
    module_target: Option<&str>,
    module_shared: u64,
    opts: &Options,
) -> Metrics {
    let (arch, arch_known) = Arch::lookup_or_default(opts.arch.as_deref().or(module_target));
    let (regs_per_thread, reg_source, spill_bytes) = match opts.ptxas.get(&k.name) {
        Some(&(regs, spill)) => (regs, RegSource::Ptxas, Some(spill)),
        None => (
            virtual_regs(k).min(arch.max_regs_per_thread),
            RegSource::VirtualUpperBound,
            None,
        ),
    };
    let ptx_block = k.reqntid.or(k.maxntid).map(|(x, y, z)| x * y * z);
    let block_size = opts.block_size.or(ptx_block).unwrap_or(256);
    Metrics {
        arch,
        arch_known,
        regs_per_thread: regs_per_thread.max(1),
        reg_source,
        spill_bytes,
        local_bytes: k.local_bytes,
        shared_bytes: k.shared_bytes + module_shared,
        block_size,
        block_size_from_ptx: opts.block_size.is_none() && ptx_block.is_some(),
        mix: classify(&k.insts),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Limiter {
    Registers,
    SharedMemory,
    BlocksPerSm,
    WarpsPerSm,
}

impl Limiter {
    pub fn label(self) -> &'static str {
        match self {
            Limiter::Registers => "registers",
            Limiter::SharedMemory => "shared memory",
            Limiter::BlocksPerSm => "blocks/SM",
            Limiter::WarpsPerSm => "warps/SM (already maxed)",
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Occupancy {
    pub active_warps: u32,
    pub max_warps: u32,
    pub blocks_per_sm: u32,
    pub limiter: Limiter,
}

impl Occupancy {
    pub fn ratio(&self) -> f64 {
        self.active_warps as f64 / self.max_warps as f64
    }
}

/// Textbook CUDA occupancy calculation.
///
/// Registers are allocated per warp in units of 256 on Volta and later, and
/// shared memory in 128-byte units.
pub fn occupancy(m: &Metrics) -> Occupancy {
    let a = m.arch;
    let warps_per_block = m.block_size.div_ceil(32).max(1);

    let regs_per_warp = (m.regs_per_thread * 32).div_ceil(256) * 256;
    let warps_by_regs = a.regs_per_sm / regs_per_warp.max(1);
    let blocks_by_regs = warps_by_regs / warps_per_block;

    let shared_per_block = m.shared_bytes.div_ceil(128) * 128;
    let blocks_by_shared = (a.shared_per_sm as u64)
        .checked_div(shared_per_block)
        .map_or(u32::MAX, |n| n as u32);

    let blocks_by_warps = a.max_warps_per_sm / warps_per_block;
    let blocks_by_limit = a.max_blocks_per_sm;

    let blocks = blocks_by_regs
        .min(blocks_by_shared)
        .min(blocks_by_warps)
        .min(blocks_by_limit);
    let limiter = if blocks == blocks_by_regs {
        Limiter::Registers
    } else if blocks == blocks_by_shared {
        Limiter::SharedMemory
    } else if blocks == blocks_by_limit {
        Limiter::BlocksPerSm
    } else {
        Limiter::WarpsPerSm
    };
    Occupancy {
        active_warps: (blocks * warps_per_block).min(a.max_warps_per_sm),
        max_warps: a.max_warps_per_sm,
        blocks_per_sm: blocks,
        limiter,
    }
}

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

    fn metrics(arch: &str, regs: u32, shared: u64, block: u32) -> Metrics {
        Metrics {
            arch: Arch::lookup(arch).unwrap(),
            arch_known: true,
            regs_per_thread: regs,
            reg_source: RegSource::Ptxas,
            spill_bytes: Some(0),
            local_bytes: 0,
            shared_bytes: shared,
            block_size: block,
            block_size_from_ptx: false,
            mix: InstMix::default(),
        }
    }

    // Cross-checked against NVIDIA's occupancy calculator.
    #[test]
    fn a100_32_regs_is_full_occupancy() {
        let o = occupancy(&metrics("sm_80", 32, 0, 256));
        assert_eq!(o.active_warps, 64);
        assert_eq!(o.blocks_per_sm, 8);
    }

    #[test]
    fn a100_64_regs_halves_occupancy() {
        let o = occupancy(&metrics("sm_80", 64, 0, 256));
        // 64 regs -> 2048 regs/warp -> 32 warps/SM -> 4 blocks of 8 warps.
        assert_eq!(o.active_warps, 32);
        assert_eq!(o.blocks_per_sm, 4);
        assert_eq!(o.limiter, Limiter::Registers);
        assert!((o.ratio() - 0.5).abs() < 1e-9);
    }

    #[test]
    fn shared_memory_can_be_the_limiter() {
        // 64 KB per block on a 100 KB SM -> a single resident block.
        let o = occupancy(&metrics("sm_86", 32, 65536, 256));
        assert_eq!(o.blocks_per_sm, 1);
        assert_eq!(o.limiter, Limiter::SharedMemory);
    }

    #[test]
    fn turing_caps_at_32_warps() {
        let o = occupancy(&metrics("sm_75", 24, 0, 256));
        assert_eq!(o.max_warps, 32);
        assert_eq!(o.active_warps, 32);
    }

    #[test]
    fn small_blocks_hit_the_block_limit() {
        // 32-thread blocks on Ampere: 16 blocks max -> only 16 warps.
        let o = occupancy(&metrics("sm_86", 16, 0, 32));
        assert_eq!(o.blocks_per_sm, 16);
        assert_eq!(o.limiter, Limiter::BlocksPerSm);
    }

    #[test]
    fn arch_suffix_is_tolerated() {
        assert_eq!(Arch::lookup("sm_90a").unwrap().name, "sm_90");
        assert!(Arch::lookup("sm_999").is_none());
    }
}