소스 검색

Merge pull request #43 from 9hvn9yt2vk-netizen/shader-mask-cycle

shader: a lane mask that needs its own value fails instead of overflo…
Marvis 3 일 전
부모
커밋
a6888a6831
2개의 변경된 파일과 48개의 추가작업 그리고 1개의 파일을 삭제
  1. 11 1
      crates/victory-shader/src/emit/lower.rs
  2. 37 0
      crates/victory-shader/src/emit/mod.rs

+ 11 - 1
crates/victory-shader/src/emit/lower.rs

@@ -183,6 +183,10 @@ struct Lower<'a> {
     mask_ok: bool,
     /// Ballots of masks read as data, by low dword: (low, high).
     ballots: HashMap<Value, (V, V)>,
+    /// Masks whose ballot is being built: a mask whose lane bit needs its
+    /// own value as a number (an opaque mask keyed by a dword it shares
+    /// with another) would otherwise recurse until the stack overflows.
+    balloting: HashSet<Value>,
     /// Block writing each value.
     def_block: HashMap<Value, usize>,
     /// IR block being lowered.
@@ -472,6 +476,7 @@ impl<'a> Lower<'a> {
             },
             mask_ok: false,
             ballots: HashMap::new(),
+            balloting: HashSet::new(),
             def_block: p
                 .blocks
                 .iter()
@@ -1161,7 +1166,12 @@ impl<'a> Lower<'a> {
                     Some(&b) if b == self.cur_ir => self.cur,
                     Some(&b) => self.tail[b].ok_or("a lane mask written after its use")?,
                 };
-                let bit = self.boolean(m, at)?;
+                if !self.balloting.insert(lo) {
+                    return Err("a lane mask whose lanes depend on its own value");
+                }
+                let bit = self.boolean(m, at);
+                self.balloting.remove(&lo);
+                let bit = bit?;
                 let i32t = self.s.t.i32;
                 let four = self.s.m.named_struct("dx.types.fouri32", vec![i32t; 4]);
                 let r = self

+ 37 - 0
crates/victory-shader/src/emit/mod.rs

@@ -355,6 +355,43 @@ mod tests {
     use super::bitcode::Attr;
     use super::container::SigElement;
     use super::shader::Shader;
+    use super::*;
+    use crate::native::{ComputeIo, StageIo};
+
+    /// A wave64 compute program that writes only EXEC's high dword
+    /// (`s_mov_b32 exec_hi, 0`) before a vector instruction. The new mask is
+    /// opaque and keyed by the unchanged low dword, so building its lane bit
+    /// needed the mask's own value: lowering recursed until the stack
+    /// overflowed, which takes the whole process down (found by
+    /// random-input testing). Now the instruction fails like any other that
+    /// cannot be expressed: its result is missing, and only a side effect
+    /// that uses it would stop the translation.
+    #[test]
+    fn an_exec_high_write_does_not_overflow_the_stack() {
+        // s_mov_b32 exec_hi, 0; v_mov_b32 v1, v0; s_endpgm
+        let words = [0xBEFF_0380u32, 0x7E02_0300, 0xBF81_0000];
+        let native = NativeProgram {
+            stage: HwStage::Cs,
+            wave64: true,
+            vgprs: 2,
+            layout: vec![],
+            used: vec![],
+            io: StageIo::Cs(ComputeIo {
+                rsrc2: 0,
+                threads: [64, 1, 1],
+            }),
+            code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
+        };
+        let p = ir::build(&native.patched_code(), native.wave64, native.vgprs).unwrap();
+        let res = resource::track(&p, &native.layout, native.stage).unwrap();
+        let io = io::raise(&p, &native).unwrap();
+        let st = structure::structure(&p).unwrap();
+        let container = crate::dxbc::build(&[]);
+        let c = Container::parse(&container).unwrap();
+        if let Err(e) = emit(&c, &native, &p, &st, &io, &res) {
+            assert!(e.to_string().contains("its own value"), "{e}");
+        }
+    }
 
     /// A pixel shader writing a constant colour.
     fn constant_color() -> Vec<u8> {