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)
This commit is contained in:
Sinan Mutlu authored and trustno1-coder committed 2026-11-27 17:29:36 -08:00
1 parent ee69dc0e1d
commit 78e99b92c4
1 file changed
+29 -2
+29 -2
View File
@@ -1412,9 +1412,36 @@ fn last_difference(a: &[u8], b: &[u8]) -> Option<usize> {
.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| {