From 950fea22435a03c17d75343d115f80d274b9f2ae Mon Sep 17 00:00:00 2001 From: Geovanni <82838501+Geovannisz@users.noreply.github.com> Date: Mon, 30 Nov 2026 02:13:25 -0300 Subject: [PATCH 1/3] fix(d3d12): Isolate AMD buffer size query handles Add an opt-in DXIL workaround for an AMD shader compiler crash during pipeline creation. Keep the size query's heap handle separate when reusing values and allow the writer to discard unused queries. Adapt the approach from imjustprism's Marvisak/VIctory#2122 to Mandy's descriptor and raw-view paths. Refs #2570 Co-authored-by: OpenAI Codex --- d3d12/README.md | 7 +++++++ .../crates/leonida-shader/src/emit/bitcode.rs | 10 +++++---- .../leonida-shader/src/emit/bitcode/census.rs | 2 +- .../leonida-shader/src/emit/bitcode/opt.rs | 20 ++++++++++++++++++ d3d12/crates/leonida-shader/src/emit/lower.rs | 21 +++++++++++++++++-- .../crates/leonida-shader/src/emit/shader.rs | 13 +++++++----- docs/design.md | 6 ++++++ 7 files changed, 67 insertions(+), 12 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index ed0c4bd1f6..6db906ab6e 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -70,6 +70,13 @@ Hookshot's release builds do; `-NoPrecompile` skips it. down for it (`LEONIDA_PIPELINE_BOOK=1`, set by the launcher), so the first session in a place still makes that place's pipelines as it meets them; from the next start on they are made ahead. +On a Radeon RX 9070 XT with driver 32.0.32029.2010, some compute pipelines fault in +`amdxc64.dll` while the driver compiles DXIL. Starting Leonida with +`LEONIDA_SEPARATE_SIZE_HANDLES=1` gives buffer size queries their own heap handles. +The shader cache distinguishes this setting and rebuilds translations on the next run. +A prior warm run can leave failed keys in `pipelines/warm-bad.txt` under the cache directory. +With Leonida closed, move that file aside to retry those pipelines with the new setting. + --- > **Before you play** diff --git a/d3d12/crates/leonida-shader/src/emit/bitcode.rs b/d3d12/crates/leonida-shader/src/emit/bitcode.rs index d47cc7d9eb..0eafef98ee 100644 --- a/d3d12/crates/leonida-shader/src/emit/bitcode.rs +++ b/d3d12/crates/leonida-shader/src/emit/bitcode.rs @@ -14,7 +14,7 @@ pub mod opt; pub(crate) struct Ty(u32); /// What the lowering made an instruction for, where it says so (counted by -/// [`census`], nothing else reads it). +/// [`census`] and used by the optimiser). #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum Tag { /// The select that keeps a register's old value in lanes a guard masks. @@ -23,6 +23,8 @@ pub(crate) enum Tag { Clamp, /// The iteration counter and limit test the translation adds to a loop. Counter, + /// A buffer size query's heap handle must stay distinct from memory accesses. + SizeHandle, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -390,11 +392,11 @@ impl Module { g } - /// Notes what instruction `v` is for (see [`Tag`]); only when modules - /// are counted. + /// Notes what instruction `v` is for (see [`Tag`]). + /// Keep optimisation tags even when instruction counts are disabled. pub fn tag(&mut self, v: V, tag: Tag) { if let V::Inst(i) = v - && census::enabled() + && (tag == Tag::SizeHandle || census::enabled()) { self.tags.insert(i, tag); } diff --git a/d3d12/crates/leonida-shader/src/emit/bitcode/census.rs b/d3d12/crates/leonida-shader/src/emit/bitcode/census.rs index ee54eea70a..5f5178e97e 100644 --- a/d3d12/crates/leonida-shader/src/emit/bitcode/census.rs +++ b/d3d12/crates/leonida-shader/src/emit/bitcode/census.rs @@ -164,7 +164,7 @@ impl Module { Some(Tag::Guard) => c.counts[k("guard_select")] += 1, Some(Tag::Clamp) => c.counts[k("clamp")] += 1, Some(Tag::Counter) => c.counts[k("loop_counter")] += 1, - None => {} + Some(Tag::SizeHandle) | None => {} } let _ = V::Inst(i); } diff --git a/d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs b/d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs index d0e205663c..50e882c01a 100644 --- a/d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs +++ b/d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs @@ -916,6 +916,7 @@ impl Module { let class = self.class(&effects, i); if passes & REUSE != 0 && class == Effect::Pure + && self.tags.get(&i) != Some(&super::Tag::SizeHandle) && let Some(key) = self.key(i) { if let Some(&first) = known.get(&key) { @@ -1915,6 +1916,25 @@ mod tests { } } + #[test] + fn buffer_size_handles_are_kept_separate_when_reusing_values() { + let Kit { mut m, b, pure, writes, .. } = kit(); + let (index, sampler) = (m.i32(4), m.i32(0)); + let size = m.call(b, pure, vec![index, sampler]); + m.tag(size, super::super::Tag::SizeHandle); + let access = m.call(b, pure, vec![index, sampler]); + let duplicate = m.call(b, pure, vec![index, sampler]); + let unread_query = m.call(b, pure, vec![index, sampler]); + m.tag(unread_query, super::super::Tag::SizeHandle); + m.call(b, writes, vec![size, duplicate]); + m.terminate(b, Term::Ret); + + m.optimise(REUSE | DEAD); + + assert_eq!(stores(&m, b), [[size, access]]); + assert_eq!(body(&m, b), 3); + } + #[test] fn a_value_is_reused_below_its_block_and_not_beside_it() { let Diamond { diff --git a/d3d12/crates/leonida-shader/src/emit/lower.rs b/d3d12/crates/leonida-shader/src/emit/lower.rs index 52fb6fb431..fbf5c4cf85 100644 --- a/d3d12/crates/leonida-shader/src/emit/lower.rs +++ b/d3d12/crates/leonida-shader/src/emit/lower.rs @@ -8278,7 +8278,24 @@ impl<'a> Lower<'a> { (first, props, image, uav, used) }; let src = Source::Loaded { load: site, first }; - let h = self.handle(site, &src, false, props)?; + // On the affected AMD driver, sharing a buffer handle between + // getDimensions and a memory access can crash the DXIL compiler. + static SEPARATE_SIZE_HANDLES: std::sync::OnceLock = std::sync::OnceLock::new(); + let h = if image.is_none() + && *SEPARATE_SIZE_HANDLES.get_or_init(|| { + std::env::var_os("LEONIDA_SEPARATE_SIZE_HANDLES").is_some_and(|v| v == "1") + }) + { + let index = self.heap_index(site, first, false)?; + let (index, bias) = self.raw_twin(&src, index, false, props); + let h = self.s.heap_handle_at(self.cur, (index, bias.is_some()), false, true, props, true); + if let Some(bias) = bias { + self.twin_bias.insert(h, bias); + } + h + } else { + self.handle(site, &src, false, props)? + }; let c = self.cur; let i32t = self.s.t.i32; let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]); @@ -9193,7 +9210,7 @@ impl<'a> Lower<'a> { /// for [`Lower::buffer_op`]. fn twin_handle(&mut self, src: &Source, index: V, sampler: bool, nonuniform: bool, props: (u32, u32)) -> V { let (index, bias) = self.raw_twin(src, index, sampler, props); - let h = self.s.heap_handle_at(self.cur, index, sampler, nonuniform, props, bias.is_some()); + let h = self.s.heap_handle_at(self.cur, (index, bias.is_some()), sampler, nonuniform, props, false); if let Some(bias) = bias { self.twin_bias.insert(h, bias); } diff --git a/d3d12/crates/leonida-shader/src/emit/shader.rs b/d3d12/crates/leonida-shader/src/emit/shader.rs index f1ed73fc47..1efcac0675 100644 --- a/d3d12/crates/leonida-shader/src/emit/shader.rs +++ b/d3d12/crates/leonida-shader/src/emit/shader.rs @@ -617,19 +617,19 @@ impl Shader { nonuniform: bool, props: (u32, u32), ) -> V { - self.heap_handle_at(block, index, sampler, nonuniform, props, false) + self.heap_handle_at(block, (index, false), sampler, nonuniform, props, false) } - /// [`Shader::heap_handle`]; `bounded`: the index is inside the host's heap already - /// ([`raw_views`]). + /// [`Shader::heap_handle`]; `bounded` means the index is already inside the host's heap + /// ([`raw_views`]). `separate` protects a buffer size query from handle reuse. pub fn heap_handle_at( &mut self, block: usize, - index: V, + (index, bounded): (V, bool), sampler: bool, nonuniform: bool, props: (u32, u32), - bounded: bool, + separate: bool, ) -> V { // a view index past the host heap (`set_view_heap_limit`) reads its null slot instead // (the raw twins of buffer views lie past it: such an index comes bounded) @@ -670,6 +670,9 @@ impl Shader { 218, &[index, s, n], ); + if separate { + self.m.tag(h, super::bitcode::Tag::SizeHandle); + } self.annotate(block, h, props.0, props.1) } diff --git a/docs/design.md b/docs/design.md index fbc5ade1b1..ab94e0ba2d 100644 --- a/docs/design.md +++ b/docs/design.md @@ -252,6 +252,12 @@ passing `spirv-val --target-env vulkan1.2`: `ps_ops` 76→33 instructions and 7, 60→22 and 4,560→1,676, `ps_sdwa` 61→19 and 5,720→2,192, `ps_vcc_x2` 63→26 and 5,260→2,552, and `ps_fnan_med3` 76→26 and 8,060→4,264. +### Native D3D12 translator contract + +| Piece | Contract | +|---|---| +| AMD buffer size queries | With `LEONIDA_SEPARATE_SIZE_HANDLES=1`, a buffer's `getDimensions` gets a separate heap handle to the same view. The reuse pass keeps the handle separate from loads and stores, while dead queries can still be removed. Descriptor binding, raw-twin bias and record-count arithmetic retain their existing rules. Confirmed by the synthetic `buffer_size_handles_are_kept_separate_when_reusing_values` regression and offline native D3D12 pipeline compilation on Radeon RX 9070 XT (driver 32.0.32029.2010). See [VIctory #2122](https://github.com/Marvisak/VIctory/pull/2122) for the related finding. | + ### GPU tests (RTX 4060 Ti) `runtime\test\vk_harness.cpp` (CMake target `vk_harness`, built by `scripts\build_runtime.ps1` or From d93d4d888f4c3801b0fd9307c0a0ffd253d53710 Mon Sep 17 00:00:00 2001 From: Geovanni <82838501+Geovannisz@users.noreply.github.com> Date: Mon, 30 Nov 2026 02:37:18 -0300 Subject: [PATCH 2/3] test(d3d12): account for dedicated size handles in waterfall checks Keep the opt-in size-handle setting in one resolver and include its conservatively non-uniform temporary handle in pre-cleanup counters. GPU readback and wave-operation assertions remain in place. Co-authored-by: OpenAI Codex --- d3d12/crates/leonida-shader/src/emit/lower.rs | 20 +++++++++++-------- .../src/emit/waterfall_tests.rs | 5 +++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/d3d12/crates/leonida-shader/src/emit/lower.rs b/d3d12/crates/leonida-shader/src/emit/lower.rs index fbf5c4cf85..849d0c5176 100644 --- a/d3d12/crates/leonida-shader/src/emit/lower.rs +++ b/d3d12/crates/leonida-shader/src/emit/lower.rs @@ -1655,6 +1655,14 @@ pub fn host_settings() -> [u32; 4] { [host_max_wave(), super::shader::view_heap_limit(), super::shader::raw_views(), host_rdna()] } +/// Whether buffer size queries use dedicated, conservatively non-uniform handles. +pub(super) fn separate_size_handles() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| { + std::env::var_os("LEONIDA_SEPARATE_SIZE_HANDLES").is_some_and(|v| v == "1") + }) +} + static HOST_RDNA: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); /// Tells the translator that the host's shaders run on AMD RDNA hardware @@ -8280,12 +8288,7 @@ impl<'a> Lower<'a> { let src = Source::Loaded { load: site, first }; // On the affected AMD driver, sharing a buffer handle between // getDimensions and a memory access can crash the DXIL compiler. - static SEPARATE_SIZE_HANDLES: std::sync::OnceLock = std::sync::OnceLock::new(); - let h = if image.is_none() - && *SEPARATE_SIZE_HANDLES.get_or_init(|| { - std::env::var_os("LEONIDA_SEPARATE_SIZE_HANDLES").is_some_and(|v| v == "1") - }) - { + let h = if image.is_none() && separate_size_handles() { let index = self.heap_index(site, first, false)?; let (index, bias) = self.raw_twin(&src, index, false, props); let h = self.s.heap_handle_at(self.cur, (index, bias.is_some()), false, true, props, true); @@ -15612,8 +15615,9 @@ mod tests { for words in [WATERFALL_JOINED_WAVE32, WATERFALL_JOINED_MIXED_WAVE32] { let (layout, used) = texture_and_uav_tables(); let l = lower_wave(words, layout, used, false).unwrap(); - // One image handle, from the join's per-lane heap index. - assert_eq!(l.nonuniform_handles, 1); + // One per-lane image handle, plus the optional size-query handle. + // This counter is collected before unused queries are removed. + assert_eq!(l.nonuniform_handles, 1 + u32::from(super::separate_size_handles())); assert!(!l.wave_ops); } } diff --git a/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs b/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs index d7d73c709d..e1f23a4fe1 100644 --- a/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs +++ b/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs @@ -71,8 +71,9 @@ fn build(words: &[u32], buffers: bool) -> HostShader { mesh_stride: 0, }) .expect("lower"); - // One access with each lane's own heap index, no wave operation. - assert_eq!(lowered.nonuniform_handles, 1); + // One per-lane access, plus the optional conservative size-query handle. + // The counter includes queries that are removed later as unused. + assert_eq!(lowered.nonuniform_handles, 1 + u32::from(lower::separate_size_handles())); assert!(!lowered.wave_ops); let shader = HostShader { stage: HwStage::Cs, From 1f188c4f7abd72d02ef500bf32f86eb562072769 Mon Sep 17 00:00:00 2001 From: Geovanni <82838501+Geovannisz@users.noreply.github.com> Date: Mon, 30 Nov 2026 02:41:23 -0300 Subject: [PATCH 3/3] test(d3d12): track temporary size-query handles explicitly Count dedicated size-query handles separately in test metadata so waterfall assertions continue checking the per-lane resource handles. This covers programs that emit several unused size queries before dead-code cleanup. Co-authored-by: OpenAI Codex --- d3d12/crates/leonida-shader/src/emit/lower.rs | 7 ++++++- d3d12/crates/leonida-shader/src/emit/shader.rs | 9 +++++++++ d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/d3d12/crates/leonida-shader/src/emit/lower.rs b/d3d12/crates/leonida-shader/src/emit/lower.rs index 849d0c5176..38d1535f65 100644 --- a/d3d12/crates/leonida-shader/src/emit/lower.rs +++ b/d3d12/crates/leonida-shader/src/emit/lower.rs @@ -76,6 +76,9 @@ pub(crate) struct Lowered { /// Heap handles made with a non-uniform index. #[cfg(test)] pub nonuniform_handles: u32, + /// Dedicated size-query handles counted before dead-code removal. + #[cfg(test)] + pub size_query_handles: u32, /// Whether the program uses wave operations. #[cfg(test)] pub wave_ops: bool, @@ -3497,6 +3500,8 @@ impl<'a> Lower<'a> { #[cfg(test)] nonuniform_handles: self.s.nonuniform_handles, #[cfg(test)] + size_query_handles: self.s.size_query_handles, + #[cfg(test)] wave_ops: self.s.flags & flags::WAVE_OPS != 0, #[cfg(test)] early_depth: self.s.flags & flags::EARLY_DEPTH_STENCIL != 0, @@ -15617,7 +15622,7 @@ mod tests { let l = lower_wave(words, layout, used, false).unwrap(); // One per-lane image handle, plus the optional size-query handle. // This counter is collected before unused queries are removed. - assert_eq!(l.nonuniform_handles, 1 + u32::from(super::separate_size_handles())); + assert_eq!(l.nonuniform_handles - l.size_query_handles, 1); assert!(!l.wave_ops); } } diff --git a/d3d12/crates/leonida-shader/src/emit/shader.rs b/d3d12/crates/leonida-shader/src/emit/shader.rs index 1efcac0675..288e329dd0 100644 --- a/d3d12/crates/leonida-shader/src/emit/shader.rs +++ b/d3d12/crates/leonida-shader/src/emit/shader.rs @@ -362,6 +362,9 @@ pub(crate) struct Shader { pub flags: u64, /// Heap handles made with a non-uniform index. pub nonuniform_handles: u32, + /// Dedicated size-query handles counted before dead-code removal. + #[cfg(test)] + pub size_query_handles: u32, /// A compute shader with this thread-group size. pub threads: Option<[u32; 3]>, /// The wave size a compute shader requires (`WaveSize`). @@ -422,6 +425,8 @@ impl Shader { root_uavs: Vec::new(), flags: 0, nonuniform_handles: 0, + #[cfg(test)] + size_query_handles: 0, threads: None, wave_size: None, lds_dwords: 0, @@ -661,6 +666,10 @@ impl Shader { flags::RESOURCE_HEAP }; self.nonuniform_handles += u32::from(nonuniform); + #[cfg(test)] + { + self.size_query_handles += u32::from(separate); + } let (s, n) = (self.m.bool(sampler), self.m.bool(nonuniform)); let h = self.op( block, diff --git a/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs b/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs index e1f23a4fe1..eb09889e24 100644 --- a/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs +++ b/d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs @@ -73,7 +73,7 @@ fn build(words: &[u32], buffers: bool) -> HostShader { .expect("lower"); // One per-lane access, plus the optional conservative size-query handle. // The counter includes queries that are removed later as unused. - assert_eq!(lowered.nonuniform_handles, 1 + u32::from(lower::separate_size_handles())); + assert_eq!(lowered.nonuniform_handles - lowered.size_query_handles, 1); assert!(!lowered.wave_ops); let shader = HostShader { stage: HwStage::Cs,