Prechádzať zdrojové kódy

Shaders: reuse lane guards for unchanged EXEC within a block

An instruction guard depends on its EXEC and the reachability of its block. Keep the most recent result, including an elided guard, rather than repeating the same BDD queries for consecutive vector instructions. Scalar instructions still bypass guard selection.

Cover EXEC changes across and within a branch with a synthetic shader. Add a CPU-only guard_runs benchmark for unmasked, short, and long masked runs.

(cherry picked from commit 81b81df351437c50f4321286c725de51664c7b1b)
Devin 2 dní pred
rodič
commit
ea357d4210

+ 4 - 0
crates/victory-shader/Cargo.toml

@@ -17,6 +17,10 @@ host = ["dep:windows"]
 # Debugging switches (emitted-module dumps and the like).
 diagnostics = []
 
+[[bench]]
+name = "guard_runs"
+harness = false
+
 [target.'cfg(windows)'.dependencies]
 windows-sys = { workspace = true, features = ["Win32_System_LibraryLoader"] }
 windows = { workspace = true, optional = true, features = [

+ 48 - 0
crates/victory-shader/benches/guard_runs.rs

@@ -0,0 +1,48 @@
+//! Time structurisation only; the synthetic programs need no title files or GPU.
+
+use std::hint::black_box;
+use std::time::Instant;
+
+use victory_shader::{ir, structure::structure};
+
+fn program(masks: u32, run: usize) -> ir::Program {
+    let mut words = Vec::new();
+    for i in 0..masks.max(1) {
+        if masks != 0 {
+            // v_cmpx_eq_u32 exec, 0, v[i]: each comparison narrows EXEC.
+            words.push(0x7DA4_0080 | (i << 9));
+        }
+        words.extend(std::iter::repeat_n(0x4A00_0081, run)); // v_add_nc_u32 v0, 1, v0
+    }
+    words.push(0xBF81_0000); // s_endpgm
+    let code = words
+        .into_iter()
+        .flat_map(u32::to_le_bytes)
+        .collect::<Vec<_>>();
+    ir::build(&code, true, 256).unwrap()
+}
+
+fn main() {
+    const BATCH: u32 = 64;
+    for (label, masks, run) in [
+        ("unmasked", 0, 512),
+        ("short runs", 8, 1),
+        ("long runs", 8, 64),
+    ] {
+        let p = program(masks, run);
+        black_box(structure(&p).unwrap());
+        let mut samples = [std::time::Duration::ZERO; 9];
+        for sample in &mut samples {
+            let start = Instant::now();
+            for _ in 0..BATCH {
+                black_box(structure(black_box(&p)).unwrap());
+            }
+            *sample = start.elapsed() / BATCH;
+        }
+        samples.sort_unstable();
+        println!(
+            "{label}: {:?} per program (median)",
+            samples[samples.len() / 2]
+        );
+    }
+}

+ 44 - 2
crates/victory-shader/src/structure.rs

@@ -792,6 +792,9 @@ impl<'a> Lanes<'a> {
             .iter()
             .enumerate()
             .map(|(n, block)| {
+                // Reachability is fixed within the block. Vector instructions
+                // with the same EXEC therefore need the same guard.
+                let mut last_guard = None;
                 block
                     .insts
                     .iter()
@@ -801,8 +804,16 @@ impl<'a> Lanes<'a> {
                         | (_, Fields::Sop { .. } | Fields::Sopp { .. } | Fields::Smem { .. }) => {
                             None
                         }
-                        (&Some(e), _) if b.implies(cond[n], e) => None,
-                        (&Some(e), _) => Some(b.given(e, cond[n])),
+                        (&Some(e), _) => {
+                            if let Some((prev, guard)) = last_guard
+                                && prev == e
+                            {
+                                return guard;
+                            }
+                            let guard = (!b.implies(cond[n], e)).then(|| b.given(e, cond[n]));
+                            last_guard = Some((e, guard));
+                            guard
+                        }
                     })
                     .collect()
             })
@@ -1577,6 +1588,37 @@ mod tests {
         assert!(matches!(s.bools.support(g)[..], [Atom::Lane(_)]));
     }
 
+    #[test]
+    fn guards_follow_exec_and_block_reachability() {
+        let p = build(
+            &[
+                0x7DA4_0080, // v_cmpx_eq_u32 exec, 0, v0
+                0x7E02_0280, // v_mov_b32 v1, 0
+                0x7E04_0280, // v_mov_b32 v2, 0
+                0xBF88_0005, // s_cbranch_execz -> end
+                0x7E06_0280, // v_mov_b32 v3, 0
+                0x7DA4_0880, // v_cmpx_eq_u32 exec, 0, v4
+                0x7E06_0280, // v_mov_b32 v3, 0
+                0xBE82_0480, // s_mov_b64 s[2:3], 0
+                0x7E0A_0280, // v_mov_b32 v5, 0
+                S_ENDPGM,
+            ],
+            true,
+        );
+        let s = structure(&p).unwrap();
+        let lane = |block: usize, i: usize| {
+            Atom::Lane(p.blocks[block].insts[i].write(Reg::S(EXEC)).unwrap())
+        };
+        let a = s.guards[0][1].unwrap();
+        let b = s.guards[1][2].unwrap();
+        assert_eq!(s.bools.node(a), Some((lane(0, 0), Bool::FALSE, Bool::TRUE)));
+        assert_eq!(s.guards[0], [None, Some(a), Some(a), None]);
+        // The fallthrough already implies the first compare. Only the second
+        // compare needs testing, and the scalar move never needs a guard.
+        assert_eq!(s.bools.node(b), Some((lane(1, 1), Bool::FALSE, Bool::TRUE)));
+        assert_eq!(s.guards[1], [None, None, Some(b), None, Some(b)]);
+    }
+
     #[test]
     fn divergent_loop_exits_per_lane() {
         let p = build(