From 7aae94af6eff1b555f160fb06e5f64cb1e3dfdd2 Mon Sep 17 00:00:00 2001 From: Scarlet-Noble <135478656+Scarlet-Noble@users.noreply.github.com> Date: Sat, 28 Nov 2026 13:37:38 -0300 Subject: [PATCH 1/2] fix(shader): let lanes outside a loop's entry predicate leave it, in halves A skip-guard body that halves predication runs in every lane can hold a counted per-lane loop. Lanes outside the predicate never entered that loop on the console, but they ran its exit test, which they need never meet: they spun to the iteration limit and left the program, skipping their stores. Each counted loop now records the predicate it is entered under (one entry only), and at its exit branch a lane outside that entry predicate takes the successor that leaves. A lane that left the predicate inside the loop (a break) stays in it, as #655's lockstep exchanges need. Co-Authored-By: Claude Opus 5.5 --- crates/victory-shader/src/emit/lower.rs | 53 ++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index c65315017..cfaea15e0 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -444,6 +444,9 @@ struct Lower<'a> { /// The results of first-lane reads each host half makes by itself /// ([`first_lane::read_alone`], [`first_lane::index_waterfalls`]). first_alone: HashSet, + /// Per counted loop header, the predicate it was entered under (`None`: + /// no predicate, or entries under different ones). + loop_entry_active: Map>, /// Pixel: an ordered section (POPS), whose UAVs the host makes /// rasterizer-ordered views. pops: bool, @@ -980,6 +983,7 @@ impl<'a> Lower<'a> { HashSet::new() }, first_alone, + loop_entry_active: Map::default(), pops: matches!(x.native.io, StageIo::Ps { db_shader_control, .. } if db_shader_control & 1 << 16 != 0), depth_before_shader: matches!(x.native.io, StageIo::Ps { db_shader_control, .. } @@ -11288,6 +11292,41 @@ impl<'a> Lower<'a> { } else { cond }; + // In halves predication, a lane outside the predicate the loop + // was entered under never was in it on the console (a skip- + // guard body runs in every lane): it takes the way out instead + // of the loop's own test, which it need never meet (it would + // spin to the iteration limit and leave the program). Lanes + // that left the predicate inside the loop (a break) stay, as + // the lockstep exchanges need (#655). + let header = |l: &Self, to: usize| { + if back(l, to) { + Some(to) + } else if let LaneExit::Jump(h) = l.x.st.exits[to] + && back(l, h) + { + Some(h) + } else { + None + } + }; + let entry = |l: &Self, h: usize| l.loop_entry_active.get(&h).copied().flatten(); + let cond = match (header(self, taken), header(self, not_taken)) { + (Some(h), None) => match entry(self, h) { + Some(act) => self.s.m.bin(self.cur, Bin::And, cond, act), + None => cond, + }, + (None, Some(h)) => match entry(self, h) { + Some(act) => { + let t = self.s.m.bool(true); + let outside = self.s.m.bin(self.cur, Bin::Xor, act, t); + self.s.m.bin(self.cur, Bin::Or, cond, outside) + } + None => cond, + }, + _ => cond, + }; + // Critical edges get a block of their own, so loops have // dedicated exits and preheaders as DXC emits them. let (t, f) = (self.edge(b, taken), self.edge(b, not_taken)); @@ -11322,10 +11361,22 @@ impl<'a> Lower<'a> { /// A forward edge into loop header `to` (from DXIL block `via`) starts /// its count at 0. - fn enter_loop(&mut self, _from: usize, to: usize, via: usize) { + fn enter_loop(&mut self, from: usize, to: usize, via: usize) { if let Some((_, incoming)) = self.counters.get_mut(&to) { let z = self.s.m.i32(0); incoming.push((z, via)); + // The predicate the loop is entered under, where there is one + // entry (`exit`: lanes outside it never were in the loop). + let act = self.pred_active(from); + match self.loop_entry_active.get(&to) { + None => { + self.loop_entry_active.insert(to, act); + } + Some(&known) if known != act => { + self.loop_entry_active.insert(to, None); + } + Some(_) => {} + } } } From 29c5b2bf3a6b9ee94b1e8d83dcf08f535912acad Mon Sep 17 00:00:00 2001 From: Pragyaan Gaur Date: Sun, 29 Nov 2026 09:17:18 +0530 Subject: [PATCH 2/2] refactor(shader): move format_load into lower/format_load.rs lower.rs was 2,376 bytes over the 524,288 byte policy limit with this change. The method moves whole, as #375 did for the atomics. --- crates/victory-shader/src/emit/lower.rs | 142 +---------------- .../src/emit/lower/format_load.rs | 147 ++++++++++++++++++ 2 files changed, 148 insertions(+), 141 deletions(-) create mode 100644 crates/victory-shader/src/emit/lower/format_load.rs diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 7088c2b40..7ea83c4f5 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -28,6 +28,7 @@ mod bvh; mod descriptor_records; mod f64; mod first_lane; +mod format_load; /// Format loads that move 16-bit halves without the d16 form, a child /// module so it shares the emitter's state. #[path = "half_views.rs"] @@ -5722,147 +5723,6 @@ impl<'a> Lower<'a> { self.end_guarded(guard, &pairs) } - /// `channels` channels of the element at byte `off` of raw buffer `h`, - /// converted from the GFX10 buffer format `format` as the hardware's - /// typed loads do (channels past the format's read 0, 0, 0, 1). - // shortcut: the descriptor's channel swizzle is taken as the identity - // (it is for raw views and the descriptors code builds), elements of 4 - // bytes or more as dword-aligned. - fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result, Why> { - let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?; - let c = self.cur; - let total: u32 = bits.iter().sum(); - let (u, align) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4)); - let load = |l: &mut Self, at: V, n: u32| -> Vec { - let mask = l.s.m.i8((1u8 << n) - 1); - let r = l.raw_buffer_load(c, &[h, at, u, mask, align]); - (0..n) - .map(|k| { - let x = l.s.m.extract(c, r, k); - l.as_int(x) - }) - .collect() - }; - let words = if total < 32 { - // A byte or half inside the dword that holds it. - let (not3, three) = (self.s.m.i32(!3), self.s.m.i32(3)); - let at = self.s.m.bin(c, Bin::And, off, not3); - let w = load(self, at, 1)[0]; - let byte = self.s.m.bin(c, Bin::And, off, three); - let sh = self.s.m.bin(c, Bin::Shl, byte, three); - vec![self.s.m.bin(c, Bin::LShr, w, sh)] - } else { - load(self, off, total / 32) - }; - let float = |l: &mut Self, f: V| l.as_int(f); - let mut out = Vec::new(); - let mut at = 0; - for &w in bits { - let word = words[(at / 32) as usize]; - let field = if w == 32 { - word - } else { - let sh = self.s.m.i32(at % 32); - let t = self.s.m.bin(c, Bin::LShr, word, sh); - let m = self.s.m.i32((1 << w) - 1); - self.s.m.bin(c, Bin::And, t, m) - }; - at += w; - // Sign-extended from the field's top bit. - let signed = |l: &mut Self| { - if w == 32 { - return field; - } - let k = l.s.m.i32(32 - w); - let t = l.s.m.bin(c, Bin::Shl, field, k); - l.s.m.bin(c, Bin::AShr, t, k) - }; - let f32t = self.s.t.f32; - // A normalized format's ends are exactly 1.0 and -1.0. The host - // may divide through a reciprocal and land just below 1.0: RDNA - // 4 loaded 16_16_UNORM's 65535 as the half 0x3BFF, not 0x3C00. - let x = match numeric { - Numeric::Unorm => { - let top = (1u64 << w) - 1; - let q = if w <= 16 { - let q = self.norm_quotient(field, w); - self.as_float(q) - } else { - let f = self.s.m.cast(c, Cast::UiToFp, field, f32t); - let max = self.s.m.f32((top as f32).to_bits()); - self.s.m.bin(c, Bin::SDiv, f, max) - }; - let (high, one) = (self.s.m.i32(top as u32), self.s.m.f32(1.0f32.to_bits())); - let at_top = self.s.m.cmp(c, pred::EQ, field, high); - let q = self.s.m.select(c, at_top, one, q); - float(self, q) - } - Numeric::Snorm => { - let s = signed(self); - let top = (1u32 << (w - 1)) - 1; - let q = if w <= 17 { - // The magnitude's quotient, signed (codes at and - // below -top are -1.0 below). - let (zero, sign) = (self.s.m.i32(0), self.s.m.i32(0x8000_0000)); - let neg = self.s.m.cmp(c, pred::SLT, s, zero); - let minus = self.s.m.bin(c, Bin::Sub, zero, s); - let m = self.s.m.select(c, neg, minus, s); - let q = self.norm_quotient(m, w - 1); - let sign = self.s.m.select(c, neg, sign, zero); - let q = self.s.m.bin(c, Bin::Or, q, sign); - self.as_float(q) - } else { - let f = self.s.m.cast(c, Cast::SiToFp, s, f32t); - let max = self.s.m.f32((top as f32).to_bits()); - self.s.m.bin(c, Bin::SDiv, f, max) - }; - // The lowest value is -1.0 too, as is the one above it. - let (high, one) = (self.s.m.i32(top), self.s.m.f32(1.0f32.to_bits())); - let at_top = self.s.m.cmp(c, pred::SGE, s, high); - let q = self.s.m.select(c, at_top, one, q); - let low = self.s.m.i32(top.wrapping_neg()); - let minus_one = self.s.m.f32((-1.0f32).to_bits()); - let at_bottom = self.s.m.cmp(c, pred::SLE, s, low); - let q = self.s.m.select(c, at_bottom, minus_one, q); - float(self, q) - } - Numeric::Uscaled => { - let f = self.s.m.cast(c, Cast::UiToFp, field, f32t); - float(self, f) - } - Numeric::Sscaled => { - let s = signed(self); - let f = self.s.m.cast(c, Cast::SiToFp, s, f32t); - float(self, f) - } - Numeric::Uint => field, - Numeric::Sint => signed(self), - Numeric::Float if w == 32 => field, - Numeric::Float => { - let h = match half_shift(w) { - 0 => field, - k => { - let sh = self.s.m.i32(k); - self.s.m.bin(c, Bin::Shl, field, sh) - } - }; - let f = self.f16_to_f32(c, h); - float(self, f) - } - }; - out.push(x); - } - let one = match numeric { - Numeric::Uint | Numeric::Sint => 1, - _ => 1.0f32.to_bits(), - }; - for k in out.len() as u32..channels { - out.push(self.s.m.i32(if k == 3 { one } else { 0 })); - } - out.truncate(channels as usize); - Ok(out) - } - /// The bits of the float nearest to `a / d`, `d = 2^w - 1`, for codes /// `0 <= a < d` and `w <= 16`: what the console's buffer unit gives for /// a normalized code. The host's division is not exact (the RX 6700 XT diff --git a/crates/victory-shader/src/emit/lower/format_load.rs b/crates/victory-shader/src/emit/lower/format_load.rs new file mode 100644 index 000000000..695ebf881 --- /dev/null +++ b/crates/victory-shader/src/emit/lower/format_load.rs @@ -0,0 +1,147 @@ +//! The typed loads of a raw buffer: GFX10 buffer formats converted as the +//! hardware does. + +use super::*; + +impl Lower<'_> { + /// `channels` channels of the element at byte `off` of raw buffer `h`, + /// converted from the GFX10 buffer format `format` as the hardware's + /// typed loads do (channels past the format's read 0, 0, 0, 1). + // shortcut: the descriptor's channel swizzle is taken as the identity + // (it is for raw views and the descriptors code builds), elements of 4 + // bytes or more as dword-aligned. + pub(super) fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result, Why> { + let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?; + let c = self.cur; + let total: u32 = bits.iter().sum(); + let (u, align) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4)); + let load = |l: &mut Self, at: V, n: u32| -> Vec { + let mask = l.s.m.i8((1u8 << n) - 1); + let r = l.raw_buffer_load(c, &[h, at, u, mask, align]); + (0..n) + .map(|k| { + let x = l.s.m.extract(c, r, k); + l.as_int(x) + }) + .collect() + }; + let words = if total < 32 { + // A byte or half inside the dword that holds it. + let (not3, three) = (self.s.m.i32(!3), self.s.m.i32(3)); + let at = self.s.m.bin(c, Bin::And, off, not3); + let w = load(self, at, 1)[0]; + let byte = self.s.m.bin(c, Bin::And, off, three); + let sh = self.s.m.bin(c, Bin::Shl, byte, three); + vec![self.s.m.bin(c, Bin::LShr, w, sh)] + } else { + load(self, off, total / 32) + }; + let float = |l: &mut Self, f: V| l.as_int(f); + let mut out = Vec::new(); + let mut at = 0; + for &w in bits { + let word = words[(at / 32) as usize]; + let field = if w == 32 { + word + } else { + let sh = self.s.m.i32(at % 32); + let t = self.s.m.bin(c, Bin::LShr, word, sh); + let m = self.s.m.i32((1 << w) - 1); + self.s.m.bin(c, Bin::And, t, m) + }; + at += w; + // Sign-extended from the field's top bit. + let signed = |l: &mut Self| { + if w == 32 { + return field; + } + let k = l.s.m.i32(32 - w); + let t = l.s.m.bin(c, Bin::Shl, field, k); + l.s.m.bin(c, Bin::AShr, t, k) + }; + let f32t = self.s.t.f32; + // A normalized format's ends are exactly 1.0 and -1.0. The host + // may divide through a reciprocal and land just below 1.0: RDNA + // 4 loaded 16_16_UNORM's 65535 as the half 0x3BFF, not 0x3C00. + let x = match numeric { + Numeric::Unorm => { + let top = (1u64 << w) - 1; + let q = if w <= 16 { + let q = self.norm_quotient(field, w); + self.as_float(q) + } else { + let f = self.s.m.cast(c, Cast::UiToFp, field, f32t); + let max = self.s.m.f32((top as f32).to_bits()); + self.s.m.bin(c, Bin::SDiv, f, max) + }; + let (high, one) = (self.s.m.i32(top as u32), self.s.m.f32(1.0f32.to_bits())); + let at_top = self.s.m.cmp(c, pred::EQ, field, high); + let q = self.s.m.select(c, at_top, one, q); + float(self, q) + } + Numeric::Snorm => { + let s = signed(self); + let top = (1u32 << (w - 1)) - 1; + let q = if w <= 17 { + // The magnitude's quotient, signed (codes at and + // below -top are -1.0 below). + let (zero, sign) = (self.s.m.i32(0), self.s.m.i32(0x8000_0000)); + let neg = self.s.m.cmp(c, pred::SLT, s, zero); + let minus = self.s.m.bin(c, Bin::Sub, zero, s); + let m = self.s.m.select(c, neg, minus, s); + let q = self.norm_quotient(m, w - 1); + let sign = self.s.m.select(c, neg, sign, zero); + let q = self.s.m.bin(c, Bin::Or, q, sign); + self.as_float(q) + } else { + let f = self.s.m.cast(c, Cast::SiToFp, s, f32t); + let max = self.s.m.f32((top as f32).to_bits()); + self.s.m.bin(c, Bin::SDiv, f, max) + }; + // The lowest value is -1.0 too, as is the one above it. + let (high, one) = (self.s.m.i32(top), self.s.m.f32(1.0f32.to_bits())); + let at_top = self.s.m.cmp(c, pred::SGE, s, high); + let q = self.s.m.select(c, at_top, one, q); + let low = self.s.m.i32(top.wrapping_neg()); + let minus_one = self.s.m.f32((-1.0f32).to_bits()); + let at_bottom = self.s.m.cmp(c, pred::SLE, s, low); + let q = self.s.m.select(c, at_bottom, minus_one, q); + float(self, q) + } + Numeric::Uscaled => { + let f = self.s.m.cast(c, Cast::UiToFp, field, f32t); + float(self, f) + } + Numeric::Sscaled => { + let s = signed(self); + let f = self.s.m.cast(c, Cast::SiToFp, s, f32t); + float(self, f) + } + Numeric::Uint => field, + Numeric::Sint => signed(self), + Numeric::Float if w == 32 => field, + Numeric::Float => { + let h = match half_shift(w) { + 0 => field, + k => { + let sh = self.s.m.i32(k); + self.s.m.bin(c, Bin::Shl, field, sh) + } + }; + let f = self.f16_to_f32(c, h); + float(self, f) + } + }; + out.push(x); + } + let one = match numeric { + Numeric::Uint | Numeric::Sint => 1, + _ => 1.0f32.to_bits(), + }; + for k in out.len() as u32..channels { + out.push(self.s.m.i32(if k == 3 { one } else { 0 })); + } + out.truncate(channels as usize); + Ok(out) + } +}