diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..76aba23521 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -630,6 +630,9 @@ struct ListState { occlusion: Occlusion, /// UMA address ranges written by this list since last Reset/KickoffX written_ranges: Vec<(u64, u64)>, + /// `(va, bytes, value)` of this list's last immediate write (`write`) to each address; a later + /// write over it of unknown value (fill, copy) removes it + written_values: Vec<(u64, u64, u64)>, /// Sparse-window `(va, bytes)` ranges written by memory ops (`Write32/64`, `Fill*`, /// `CopyMemoryToMemoryX`, timestamps) to copy back to the CPU view after execution. sparse_writes: Vec<(usize, usize)>, @@ -667,6 +670,7 @@ impl ListState { return; } let end = va.saturating_add(size); + self.written_values.retain(|&(s, n, _)| !(va < s + n && s < end)); if self.written_ranges.len() < MAX_WRITTEN_RANGES { self.written_ranges.push((va, end)); } else if let Some(last) = self.written_ranges.last_mut() { @@ -704,6 +708,11 @@ impl ListState { std::mem::take(&mut self.write64_signals) } + /// What a mid-list split's next segment inherits of this list's writes: the ranges and the known values. + fn writes_for_split(&self) -> (Vec<(u64, u64)>, Vec<(u64, u64, u64)>) { + (self.written_ranges.clone(), self.written_values.clone()) + } + fn has_prior_write(&self, va: u64, size: u64) -> bool { let end = va.saturating_add(size); self.written_ranges.iter().any(|&(s, e)| va < e && s < end) @@ -874,6 +883,7 @@ pub(super) fn clear_segment_state(list: usize) { if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&list)) { s.occlusion = Occlusion::default(); s.written_ranges.clear(); + s.written_values.clear(); s.clear_sparse_writes(); s.clear_write64_signals(); s.clear_waits(); @@ -1033,6 +1043,16 @@ fn note_write(list: usize, va: u64, size: u64) { LISTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(list).or_default().note_write(va, size); } +/// `note_write` for an immediate write of `words`, keeping its value for `wait_memory`. +fn note_write_value(list: usize, va: u64, words: &[u32]) { + let mut g = LISTS.lock().unwrap(); + let s = g.get_or_insert_with(HashMap::new).entry(list).or_default(); + s.note_write(va, 4 * words.len() as u64); + if words.len() <= 2 && s.written_values.len() < MAX_WRITTEN_RANGES { + s.written_values.push((va, 4 * words.len() as u64, words.iter().rev().fold(0, |v, &w| v << 32 | w as u64))); + } +} + // --------------------------------------------------------------------------------------- // per-address GPU fence bridge for monotonic 64-bit Write64BitValue* -> Wait64BitValueX // --------------------------------------------------------------------------------------- @@ -2455,7 +2475,7 @@ unsafe fn write(list: *mut c_void, va: u64, words: &[u32], mode: D3D12_WRITEBUFF } return warn(&N, what, format_args!("{va:#x} is not in unified memory")); }; - note_write(list as usize, va, n); + note_write_value(list as usize, va, words); let params: Vec = words.iter().enumerate().map(|(i, &w)| D3D12_WRITEBUFFERIMMEDIATE_PARAMETER { Dest: at + 4 * i as u64, Value: w }).collect(); let modes = vec![mode; words.len()]; type F = unsafe extern "system" fn(*mut c_void, u32, *const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER, *const D3D12_WRITEBUFFERIMMEDIATE_MODE); @@ -2750,7 +2770,7 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ } None => None, }; - let (alloc, last_queue, bound_pso, depth_bias, index_buffer, depth_bounds, predication, pred_buf, pred_in_state, active_query, ended_early, written_ranges, bindings) = { + let (alloc, last_queue, bound_pso, depth_bias, index_buffer, depth_bounds, predication, pred_buf, pred_in_state, active_query, ended_early, (written_ranges, written_values), bindings) = { let mut g = LISTS.lock().unwrap(); let s = g.as_mut()?.get_mut(&(list as usize))?; if s.allocator == 0 { @@ -2771,7 +2791,7 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ pred_in_state, active_query, ended_early, - s.written_ranges.clone(), + s.writes_for_split(), s.bindings.clone(), ) }; @@ -2862,6 +2882,7 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ depth_bounds, occlusion: Occlusion { active: None, ended_early }, written_ranges, + written_values, pred_buf: pred_buf.clone(), pred_in_state: false, predication, @@ -3004,7 +3025,9 @@ unsafe fn wait_memory(list: *mut c_void, va: u64, width: u8, comparison: u32, va let need_split = { let mut g = LISTS.lock().unwrap(); let s = g.get_or_insert_with(HashMap::new).entry(list as usize).or_default(); - if s.has_prior_write(va, n) { + // (only when the value it wrote meets the wait: else another queue is to produce it) + let own = s.written_values.iter().find(|&&(w, len, _)| w == va && len == n); + if s.has_prior_write(va, n) && own.is_none_or(|&(_, _, v)| compare_u64(comparison, v & mask, value) == Some(true)) { return; } if s.write64_signals.is_empty() { @@ -3293,6 +3316,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let sparse_writes = s.clear_sparse_writes(); let write64_signals = s.clear_write64_signals(); s.written_ranges.clear(); + s.written_values.clear(); let cpu_writes = std::mem::take(&mut s.cpu_writes); let pred_in_state = std::mem::replace(&mut s.pred_in_state, false); let pred_buf = s.pred_buf.as_ref().map(|r| r.as_raw() as usize); @@ -3389,6 +3413,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { s.pred_in_state = false; s.occlusion = Occlusion { active: None, ended_early }; s.written_ranges.clear(); + s.written_values.clear(); s.clear_sparse_writes(); s.clear_write64_signals(); s.clear_waits(); @@ -4085,6 +4110,7 @@ pub(super) unsafe fn reset_pc(this: *mut c_void, alloc: usize, pso: usize) -> i3 s.depth_bounds = None; s.occlusion = Occlusion::default(); s.written_ranges.clear(); + s.written_values.clear(); s.clear_sparse_writes(); s.clear_write64_signals(); s.clear_waits(); @@ -4947,6 +4973,92 @@ mod tests { assert!(!s.has_prior_write(0x0FF8, 8)); } + /// A wait on an address the same list wrote is dropped only when the value that write left + /// meets it: a list that resets a counter to 0 and then waits for another queue to raise it + /// keeps its wait. + #[test] + fn a_wait_after_an_own_write_is_dropped_only_when_the_written_value_meets_it() { + let cell64 = AtomicU64::new(0); + let cell32 = AtomicU32::new(0); + let (va64, va32) = (&cell64 as *const AtomicU64 as u64, &cell32 as *const AtomicU32 as u64); + let mut token = 0usize; + let list = (&mut token as *mut usize).cast::(); + // records the writes (as `write` does) and the wait, and returns the waits left on the list + let kept = |writes: &[(u64, &[u32])], wait: &dyn Fn()| -> usize { + for &(va, words) in writes { + note_write_value(list as usize, va, words); + } + wait(); + let n = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(list as usize))).map_or(0, |s| s.pending_waits.len()); + on_release(list as usize); + n + }; + let w64 = |v: u64| [v as u32, (v >> 32) as u32]; + use D3D12_COMPARISON_FUNC as F; + // (value written, comparison, value waited for, wait kept) + for (written, op, wanted, keep) in [ + (0u64, D3D12_COMPARISON_FUNC_GREATER_EQUAL, 1u64, true), + (5, D3D12_COMPARISON_FUNC_GREATER_EQUAL, 1, false), + (0xFFFF_FFFF, D3D12_COMPARISON_FUNC_GREATER_EQUAL, 1 << 32, true), + (0, D3D12_COMPARISON_FUNC_EQUAL, 1, true), + (1, D3D12_COMPARISON_FUNC_EQUAL, 1, false), + (1, D3D12_COMPARISON_FUNC_NOT_EQUAL, 1, true), + (0, D3D12_COMPARISON_FUNC_NOT_EQUAL, 1, false), + (1, D3D12_COMPARISON_FUNC_LESS, 1, true), + (0, D3D12_COMPARISON_FUNC_LESS, 1, false), + (2, D3D12_COMPARISON_FUNC_LESS_EQUAL, 1, true), + (1, D3D12_COMPARISON_FUNC_LESS_EQUAL, 1, false), + (1, D3D12_COMPARISON_FUNC_GREATER, 1, true), + (2, D3D12_COMPARISON_FUNC_GREATER, 1, false), + (0, D3D12_COMPARISON_FUNC_ALWAYS, 1, false), + (0, D3D12_COMPARISON_FUNC_NEVER, 1, false), + ] { + let F(op) = op; + let got = kept(&[(va64, &w64(written))], &|| unsafe { wait64_x(list, va64, op as u32, wanted, 0, 0) }); + assert_eq!(got, keep as usize, "64-bit: wrote {written:#x}, wait op {op} {wanted:#x}"); + if wanted <= u32::MAX as u64 && written <= u32::MAX as u64 { + let got = kept(&[(va32, &[written as u32])], &|| unsafe { wait32_x(list, va32, op as u32, wanted as u32, u32::MAX, 0) }); + assert_eq!(got, keep as usize, "32-bit: wrote {written:#x}, wait op {op} {wanted:#x}"); + } + } + let ge = D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32; + let eq = D3D12_COMPARISON_FUNC_EQUAL.0 as u32; + // the mask applies to the written value too + assert_eq!(kept(&[(va32, &[0x1234_00FF])], &|| unsafe { wait32_x(list, va32, eq, 0xFF, 0xFF, 0) }), 0); + assert_eq!(kept(&[(va32, &[0x0000_12FE])], &|| unsafe { wait32_x(list, va32, eq, 0xFF, 0xFF, 0) }), 1); + // the last write before the wait counts + assert_eq!(kept(&[(va64, &w64(5)), (va64, &w64(0))], &|| unsafe { wait64_x(list, va64, ge, 1, 0, 0) }), 1); + assert_eq!(kept(&[(va64, &w64(0)), (va64, &w64(5))], &|| unsafe { wait64_x(list, va64, ge, 1, 0, 0) }), 0); + // a write to another address leaves the wait alone + assert_eq!(kept(&[(va32, &[5])], &|| unsafe { wait64_x(list, va64, ge, 1, 0, 0) }), 1); + // a write of unknown value over it (a fill or a copy) still drops the wait, as before + let fill = || { + note_write_value(list as usize, va64, &w64(0)); + note_write(list as usize, va64, 8); + unsafe { wait64_x(list, va64, ge, 1, 0, 0) } + }; + assert_eq!(kept(&[], &fill), 0); + } + + #[test] + fn a_split_segment_keeps_the_written_value_for_a_later_wait() { + let cell = AtomicU64::new(0); + let va = &cell as *const AtomicU64 as u64; + let (mut a, mut b) = (0usize, 0usize); + let (list, next) = ((&mut a as *mut usize).cast::(), (&mut b as *mut usize).cast::()); + let ge = D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32; + // `Write64(va, 0)`, then a split moves the recording to `next` with what `split` carries over + note_write_value(list as usize, va, &[0, 0]); + let (written_ranges, written_values) = LISTS.lock().unwrap().as_ref().unwrap()[&(list as usize)].writes_for_split(); + on_release(list as usize); + LISTS.lock().unwrap().as_mut().unwrap().insert(next as usize, ListState { written_ranges, written_values, ..Default::default() }); + // a wait the written 0 does not meet stays on the segment + unsafe { wait64_x(next, va, ge, 1, 0, 0) }; + let n = LISTS.lock().unwrap().as_ref().unwrap()[&(next as usize)].pending_waits.len(); + on_release(next as usize); + assert_eq!(n, 1, "the wait after the split was dropped"); + } + #[test] fn execute_with_waits_splits_batches_before_unsatisfied_list() { // Use synthetic non-zero list pointers distinct from real COM objects.