From 78e99b92c4d0d23be224bcb1de42a57a1f53478b Mon Sep 17 00:00:00 2001 From: Sinan Mutlu Date: Fri, 27 Nov 2026 23:03:17 +0300 Subject: [PATCH] GPU: a parallel compare's first cut saturates at its span's start The per-thread cut was `at - (start + at) % PIECE`. When the span does not begin on a piece boundary the subtraction runs below the span's start and wraps in a release build; the first thread then scanned nothing (or a huge range), so differences in the first piece were never found and those bytes were never copied back into title memory. `scan_boundary` subtracts the offset into the piece from the span and saturates at `from`; the test walks unaligned starts and asserts the cuts stay ordered and on piece boundaries. From kelvaroq native/patches/0009-shadow-compare.patch (GPL-3.0-or-later); the same fix as fxceless/victory-fork 81d54b0 (fork pull request #1407). (cherry picked from commit 30279c13dd81686d8a8cce7d329c10098fe043bd) --- crates/victory-gpu/src/shadow.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 0020ed5..7c9b773 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -1412,9 +1412,36 @@ fn last_difference(a: &[u8], b: &[u8]) -> Option { .map(|i| chunk * DIFF_CHUNK + i) } +/// Cut backwards to a piece boundary inside the span being scanned. The +/// first cut of a span that does not start on a piece is the span's start: +/// subtracting its offset into the piece would run below it (and wrap). +fn scan_boundary(start: usize, from: usize, at: usize) -> usize { + at.saturating_sub((start + at) % PIECE).max(from) +} + #[cfg(test)] mod difference_tests { - use super::{first_difference, last_difference}; + use super::{PIECE, first_difference, last_difference, scan_boundary}; + + #[test] + fn unaligned_parallel_scan_covers_the_first_piece() { + let span = 48 << 20; + for start in [0, 1, 31, PIECE - 1] { + for from in [0, 1, 31, PIECE - 1, PIECE + 3] { + let end = from + span; + let cuts = [ + scan_boundary(start, from, from), + scan_boundary(start, from, from + span / 3), + scan_boundary(start, from, from + 2 * span / 3), + end, + ]; + assert_eq!(cuts[0], from); + assert!(cuts.windows(2).all(|pair| pair[0] < pair[1])); + assert_eq!((start + cuts[1]) % PIECE, 0); + assert_eq!((start + cuts[2]) % PIECE, 0); + } + } + } /// The chunk searches agree with a byte-wise scan across chunk boundaries. #[test] @@ -3766,7 +3793,7 @@ impl Shadow { return to; } let at = from + (to - from) / threads * i; - (at - (start + at) % PIECE).max(from) + scan_boundary(start, from, at) }; let scan = &scan; std::thread::scope(|s| {