pmat 3.14.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod cb060_falsification {
    use super::*;

    // ========================================================================
    // CB-060-A: BARRIER DIVERGENCE (tests 031-040)
    // ========================================================================

    #[test]
    fn tp_031_ptx_bra_before_barrier_simple() {
        // From PARITY-114: Simple case
        let ptx = r#"
            setp.ge.u32 %p0, %r5, %r7;
            @%p0 bra exit;
            bar.sync 0;
        "#;
        let violations = detect_ptx_barrier_divergence_in_str(ptx);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed simple barrier divergence"
        );
    }

    #[test]
    fn tp_032_ptx_bra_before_barrier_multiline() {
        // Adversarial: Many lines between branch and barrier
        let ptx = r#"
            @%p0 bra skip_section;
            mov.u32 %r1, 0;
            mov.u32 %r2, 0;
            mov.u32 %r3, 0;
            mov.u32 %r4, 0;
            bar.sync 0;
            skip_section:
        "#;
        let violations = detect_ptx_barrier_divergence_in_str(ptx);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed barrier divergence with intervening code"
        );
    }

    #[test]
    fn tp_033_wgsl_barrier_in_if() {
        let wgsl = r#"
            if (local_id.x < 16u) {
                workgroupBarrier();
            }
        "#;
        let violations = detect_wgsl_barrier_divergence_in_str(wgsl);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed WGSL barrier in if"
        );
    }

    #[test]
    fn tp_034_wgsl_barrier_in_else() {
        // Adversarial: Barrier in else branch only
        let wgsl = r#"
            if (condition) {
                // no barrier
            } else {
                workgroupBarrier();
            }
        "#;
        let violations = detect_wgsl_barrier_divergence_in_str(wgsl);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed WGSL barrier in else"
        );
    }

    #[test]
    fn tn_035_ptx_barrier_before_branch() {
        // Barrier BEFORE branch is safe
        let ptx = r#"
            bar.sync 0;
            setp.ge.u32 %p0, %r5, %r7;
            @%p0 bra exit;
        "#;
        let violations = detect_ptx_barrier_divergence_in_str(ptx);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Barrier before branch flagged"
        );
    }

    #[test]
    fn tn_036_wgsl_barrier_outside_control_flow() {
        // Barrier not in divergent control flow
        let wgsl = r#"
            workgroupBarrier();
            if (condition) {
                // no barrier here
            }
            workgroupBarrier();
        "#;
        let violations = detect_wgsl_barrier_divergence_in_str(wgsl);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Non-divergent barrier flagged"
        );
    }

    #[test]
    fn edge_037_ptx_barrier_in_comment() {
        // Adversarial: bar.sync in comment should NOT trigger
        let ptx = r#"
            // bar.sync 0; -- this is a comment
            @%p0 bra exit;
        "#;
        let violations = detect_ptx_barrier_divergence_in_str(ptx);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Barrier in comment flagged"
        );
    }

    #[test]
    fn edge_038_ptx_nested_predicates() {
        // Adversarial: Complex nested predicate structure
        let ptx = r#"
            @%p0 bra check1;
            @%p1 bra check2;
            bar.sync 0;
            check1:
            check2:
        "#;
        let violations = detect_ptx_barrier_divergence_in_str(ptx);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed nested predicate divergence"
        );
    }

    #[test]
    fn edge_039_wgsl_barrier_in_loop() {
        // Barrier in loop that all threads execute is OK
        let wgsl = r#"
            for (var i = 0u; i < 4u; i++) {
                workgroupBarrier();
            }
        "#;
        let violations = detect_wgsl_barrier_divergence_in_str(wgsl);
        // Uniform loop - all threads execute same iterations
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Barrier in uniform loop flagged"
        );
    }

    #[test]
    fn edge_040_wgsl_barrier_in_divergent_loop() {
        // Barrier in loop with thread-dependent bounds
        let wgsl = r#"
            for (var i = 0u; i < local_id.x; i++) {
                workgroupBarrier();
            }
        "#;
        let violations = detect_wgsl_barrier_divergence_in_str(wgsl);
        // Divergent loop - threads execute different iterations
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed barrier in divergent loop"
        );
    }

    // ========================================================================
    // CB-060-B: SHARED MEMORY BOUNDS (tests 041-047)
    // ========================================================================

    #[test]
    fn tp_041_unbounded_shared_load() {
        // From issue #32: Direct shared memory access without bounds check
        let ptx = r#"
            mul.u32 %r10, %r5, 64;
            ld.shared.f32 %f1, [%r10];
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed unbounded shared memory load"
        );
    }

    #[test]
    fn tp_042_unbounded_shared_store() {
        let ptx = r#"
            mul.u32 %r10, %r5, 64;
            st.shared.f32 [%r10], %f1;
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed unbounded shared memory store"
        );
    }

    #[test]
    fn tn_043_bounded_shared_load() {
        // Predicated load with bounds check
        let ptx = r#"
            setp.lt.u32 %p1, %r5, 256;
            @%p1 ld.shared.f32 %f1, [%r10];
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Bounded shared load flagged"
        );
    }

    #[test]
    fn tn_044_shared_with_constant_offset() {
        // Constant offset is always bounded
        let ptx = r#"
            ld.shared.f32 %f1, [shared_mem + 128];
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Constant offset flagged"
        );
    }

    #[test]
    fn edge_045_shared_in_comment() {
        let ptx = r#"
            // ld.shared.f32 %f1, [%r10]; -- commented out
            mov.f32 %f1, 0.0;
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Commented shared access flagged"
        );
    }

    #[test]
    fn edge_046_shared_complex_index() {
        // Adversarial: Complex index expression
        let ptx = r#"
            mad.lo.u32 %r10, %r5, 64, %r6;
            add.u32 %r10, %r10, %r7;
            ld.shared.f32 %f1, [%r10];
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        // Complex index without bounds check should be flagged
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed complex index without bounds"
        );
    }

    #[test]
    fn edge_047_shared_bounds_far_apart() {
        // Adversarial: Bounds check far from actual load
        let ptx = r#"
            setp.lt.u32 %p1, %r5, 256;
            mov.u32 %r10, 0;
            mov.u32 %r11, 0;
            mov.u32 %r12, 0;
            mul.u32 %r13, %r5, 64;
            @%p1 ld.shared.f32 %f1, [%r13];
        "#;
        let violations = detect_shared_memory_unbounded_in_str(ptx);
        // Bounds check exists, load is predicated - should be OK
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Distant bounds check not recognized"
        );
    }

    // ========================================================================
    // CB-060-C: TILED KERNEL BOUNDS (tests 048-055)
    // ========================================================================

    #[test]
    fn tp_048_tiled_no_boundary_check() {
        // From issue #37: Tiled GEMM without boundary check
        let rust_code = r#"
            // Tiled GEMM kernel
            for tile in 0..k_tiles {
                // Load A tile - no bounds check for m < tile_size
                let a_elem = a_smem[local_row * TILE_K + k];
                let b_elem = b_smem[k * TILE_N + local_col];
                acc += a_elem * b_elem;
            }
            // Store without checking row < m && col < n
            c[row * n + col] = acc;
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(rust_code);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed tiled kernel without bounds"
        );
    }

    #[test]
    fn tn_049_tiled_with_boundary_check() {
        let rust_code = r#"
            // Tiled GEMM with proper bounds
            for tile in 0..k_tiles {
                let a_elem = a_smem[local_row * TILE_K + k];
                let b_elem = b_smem[k * TILE_N + local_col];
                acc += a_elem * b_elem;
            }
            // Proper bounds check before store
            if row < m && col < n {
                c[row * n + col] = acc;
            }
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(rust_code);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Properly bounded tiled kernel flagged"
        );
    }

    #[test]
    fn tp_050_ptx_tiled_early_exit() {
        // From PARITY-114: Early exit breaks tile loading
        let ptx = r#"
            setp.ge.u32 %p0, %r_row, %r_m;
            @%p0 bra exit;
            setp.ge.u32 %p1, %r_col, %r_n;
            @%p1 bra exit;
            // Tile loop starts here
            tile_loop:
            ld.shared.f32 %f1, [smem_a];
            bar.sync 0;
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(ptx);
        // Early exit before tile loop = some threads don't load
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed early exit before tile loop"
        );
    }

    #[test]
    fn edge_051_wgsl_tiled_workgroup_size() {
        // WGSL tiled kernel pattern
        let wgsl = r#"
            @workgroup_size(32, 32)
            fn tiled_matmul() {
                // No bounds check
                let a_tile = a[global_id.y * K + local_id.x];
            }
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(wgsl);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed WGSL tiled without bounds"
        );
    }

    #[test]
    fn edge_052_partial_bounds_check() {
        // Only row bounds checked, not column
        let rust_code = r#"
            if row < m {
                // Missing: && col < n
                c[row * n + col] = acc;
            }
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(rust_code);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Missed partial bounds check"
        );
    }

    #[test]
    fn edge_053_bounds_in_wrong_place() {
        // Bounds check after store (useless)
        let rust_code = r#"
            c[row * n + col] = acc;
            if row < m && col < n {
                // Too late!
            }
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(rust_code);
        assert!(
            !violations.is_empty(),
            "FALSIFIED: Accepted bounds check after store"
        );
    }

    #[test]
    fn edge_054_tiled_in_string() {
        // Adversarial: Kernel code in string literal
        let rust_code = r#"
            let kernel_src = "c[row * n + col] = acc;";
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(rust_code);
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Kernel code in string flagged"
        );
    }

    #[test]
    fn edge_055_complex_bounds_expression() {
        // Bounds check with complex expression
        let rust_code = r#"
            if (row * stride + offset) < (m * stride) && col < n {
                c[row * n + col] = acc;
            }
        "#;
        let violations = detect_tiled_kernel_no_bounds_in_str(rust_code);
        // Complex but valid bounds check
        assert!(
            violations.is_empty(),
            "FALSIFIED (FP): Complex bounds expression not recognized"
        );
    }
}