From 868dd97145253113e0a903058df1bb3846f64890 Mon Sep 17 00:00:00 2001 From: KDenKC <125705260+KDenKC@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:52:09 -0700 Subject: [PATCH] Recompiler: on NVIDIA, programs whose compact table needs more than 12,288 words take the plain form #3261 raised the most table words a program may need to take the compact form from 12,288 to 32,768. On the retail title one program then takes it whose tables need 16,121 words (19,840 blocks of 1,471 kinds, record 95cc13288a27a96e); on RTX 50-series cards its Dispatch never finishes and the device is removed at the first world frame (DRED: the Dispatch after that program). With the limit at 12,288 the program takes the plain form and runs (BBareth, #505). The limit goes back to 12,288 on NVIDIA adapters only. On AMD the plain form of that program is the costly one (an RX 7900 XT report on #505: a driver compile of minutes refused with E_OUTOFMEMORY), so other adapters keep 32,768. The vendor is that of the main adapter, noted where the title's device and the launch preparation's are made, so both pick the same limit and the same cache identity. XCL_COMPACT_TABLE_WORDS sets the limit on any adapter (1 to 32,768); a limit other than 32,768 goes into the translated cache identity with the nesting tag. Co-Authored-By: Claude Opus 5.5 Checked: #3516, including Foniass merge-check 7305483219 (cache identity is requested before device creation). --- README.md | 3 +- crates/xcl/src/gpu/adapter.rs | 16 +++++++ crates/xcl/src/gpu/dxil/compact.rs | 42 +++++++++++++++-- crates/xcl/src/gpu/dxil/compact_tests.rs | 58 ++++++++++++++++++++++++ crates/xcl/src/gpu/dxil/kernel.rs | 7 ++- crates/xcl/src/hle/d3d12x/real.rs | 3 ++ crates/xcl/src/shaders.rs | 1 + 7 files changed, 125 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 91c019d824..7276a47e1a 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,8 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. after its dispatch's share of passes for each entry (between 1024 and 65 536 a thread, fewer the more threads the dispatch has; the direct DXIL translator ends the thread's program there), so one that runs away does not hold the GPU past the time Windows gives it. The direct - translator's compact form (programs of 20 000 instructions and more) and `XCL_PER_LANE` keep + translator's compact form (programs of 20 000 instructions and more, whose tables need at most + 12 288 words on NVIDIA and 32 768 elsewhere: `XCL_COMPACT_TABLE_WORDS` sets it, up to 32 768) and `XCL_PER_LANE` keep only the thread's 65 536 passes in all. `XCL_DISPATCH_LOOP_BUDGET=0` leaves every loop the thread's 65 536 passes, as before (to compare). - With `XCL_DRED=1`, a D3D12 device removal logs GPU breadcrumbs in `xcl.log`. diff --git a/crates/xcl/src/gpu/adapter.rs b/crates/xcl/src/gpu/adapter.rs index cd0b61fd85..ee9443f590 100644 --- a/crates/xcl/src/gpu/adapter.rs +++ b/crates/xcl/src/gpu/adapter.rs @@ -115,6 +115,22 @@ pub(crate) fn select_indices>(names: &[S], sel: &Selector) -> Vec< Vec::new() } +/// PCI vendor id of NVIDIA. +pub const NVIDIA: u32 = 0x10DE; + +/// The vendor of the adapter the title runs on (0: not chosen yet), noted where that device is +/// made: the title's (`hle::d3d12x::real`) and the launch preparation's (`shaders`). +static MAIN_VENDOR: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + +pub fn note_main_vendor(vendor: u32) { + MAIN_VENDOR.store(vendor, std::sync::atomic::Ordering::Release); +} + +/// The main adapter's vendor, None before its device is made. +pub fn main_vendor() -> Option { + Some(MAIN_VENDOR.load(std::sync::atomic::Ordering::Acquire)).filter(|&v| v != 0) +} + /// The adapter's name from its description. pub fn name(desc: &DXGI_ADAPTER_DESC1) -> String { let n = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len()); diff --git a/crates/xcl/src/gpu/dxil/compact.rs b/crates/xcl/src/gpu/dxil/compact.rs index 65c1a51b00..75f9cc8675 100644 --- a/crates/xcl/src/gpu/dxil/compact.rs +++ b/crates/xcl/src/gpu/dxil/compact.rs @@ -223,8 +223,39 @@ pub struct Plan { pub class_live_out: Vec, } -/// Most 32-bit words of tables a program may need to take the form by itself. -const MAX_TABLE_WORDS: usize = 32768; +/// Most 32-bit words of tables a program may need to take the form by itself on an NVIDIA +/// adapter, unless `XCL_COMPACT_TABLE_WORDS` says otherwise. A retail program whose tables need +/// 16,121 words (19,840 blocks of 1,471 kinds) never finished its dispatch in this form on an RTX +/// 5070 Ti and an RTX 5090 and removed the device at the first world frame; in the plain form it +/// runs (#505). On AMD the plain form of that program is the risk instead (a driver compile of +/// minutes, refused with E_OUTOFMEMORY), so other adapters keep `TABLE_WORDS_CEILING`. +const NVIDIA_TABLE_WORDS: usize = 12288; +/// The most the form can take (`XCL_COMPACT_TABLE_WORDS` up to this), and the limit on an +/// adapter that is not NVIDIA's. +const TABLE_WORDS_CEILING: usize = 32768; + +/// The table limit on the adapter of `vendor` (None: not known yet). +pub(super) fn default_table_words(vendor: Option) -> usize { + if vendor == Some(crate::gpu::adapter::NVIDIA) { NVIDIA_TABLE_WORDS } else { TABLE_WORDS_CEILING } +} + +/// `XCL_COMPACT_TABLE_WORDS=` (1 to `TABLE_WORDS_CEILING`), else the main adapter's limit +/// (`default_table_words`); a limit other than `TABLE_WORDS_CEILING` is in the translated cache +/// identity (`table_words_tag`). +fn max_table_words() -> usize { + static ENV: std::sync::OnceLock> = std::sync::OnceLock::new(); + table_words(ENV.get_or_init(|| std::env::var("XCL_COMPACT_TABLE_WORDS").ok()).as_deref(), crate::gpu::adapter::main_vendor()) +} + +pub(super) fn table_words(v: Option<&str>, vendor: Option) -> usize { + v.and_then(|v| v.trim().parse::().ok()).filter(|n| (1..=TABLE_WORDS_CEILING).contains(n)).unwrap_or(default_table_words(vendor)) +} + +/// The translated cache tag of a table limit other than `TABLE_WORDS_CEILING`, None at it. +pub fn table_words_tag() -> Option { + let n = max_table_words(); + (n != TABLE_WORDS_CEILING).then(|| format!("xcl-compact-table-{n}")) +} impl Plan { /// Bits of a table entry: the class, and the taken successor or the index of the pair of offsets. @@ -259,10 +290,15 @@ pub(super) fn atomic_candidate(cfg: &Cfg) -> bool { } pub fn automatic_plan(cfg: &Cfg, lw: &Lw, split_compute: bool) -> Option { + automatic_plan_within(cfg, lw, split_compute, max_table_words()) +} + +/// `automatic_plan` with the table limit `limit`. +pub(super) fn automatic_plan_within(cfg: &Cfg, lw: &Lw, split_compute: bool, limit: usize) -> Option { let large = large(cfg); if !large && !(split_compute && atomic_candidate(cfg)) { return None; } let p = plan(cfg, lw)?; - if p.words() > MAX_TABLE_WORDS { return None; } + if p.words() > limit { return None; } let insts = |n: usize| p.mblocks[n].insts.len(); let all: usize = (0..p.mblocks.len()).map(insts).sum(); let once: usize = p.rep.iter().map(|&n| insts(n)).sum(); diff --git a/crates/xcl/src/gpu/dxil/compact_tests.rs b/crates/xcl/src/gpu/dxil/compact_tests.rs index cf56c97c85..3b834b7556 100644 --- a/crates/xcl/src/gpu/dxil/compact_tests.rs +++ b/crates/xcl/src/gpu/dxil/compact_tests.rs @@ -299,6 +299,64 @@ fn gpu_compact_inline_table_above_old_cap_checks_late_entries() { } } +/// A program whose compact tables need more than 12,288 words takes the plain form (a retail +/// one of 16,121 words never finished its dispatch in the compact form, #505); its plan alone +/// would still fit the most the form can take. +#[test] +fn a_program_whose_tables_pass_the_limit_takes_the_plain_form() { + if compact_mode().is_some() || std::env::var_os("XCL_COMPACT_TABLE_WORDS").is_some() { + return; + } + const COPIES: usize = 8192; + let i = |n: &str, e: &str, f: &[(&str, u64)]| encode(n, e, f, None).unwrap_or_else(|x| panic!("{n}: {x}")); + // runs of the copies of `gpu_compact_inline_table_above_old_cap_checks_late_entries`, as many + // as it takes to pass 12,288 table words (each run adds about 2,300: 6 runs need 10,761) + let program = |runs: usize| { + let mut code = Vec::new(); + code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 2)])); + for marker in 0..runs { + for k in 0..COPIES { + code.extend(i("S_ADD_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 0), ("SSRC1", 1)])); + code.extend(i("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 256), ("VSRC1", 1)])); + code.extend(i("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", ((COPIES - 1 - k) * 3 + 1) as u64)])); + } + code.extend(i("S_MOV_B32", "ENC_SOP1", &[("SDST", 3 + marker as u64), ("SSRC0", 129)])); + } + code.extend(i("S_ENDPGM", "ENC_SOPP", &[])); + code + }; + let o = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; + for runs in 6..=12 { + let code = program(runs); + let cfg = super::cfg::Cfg::build(&code).unwrap(); + let leaders = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect(); + let lw = super::lower::Lw::new(&cfg.insts, &leaders, o, &super::kernel::Entry::Kernel); + let words = super::compact::plan(&cfg, &lw).expect("a plan").words(); + if words <= 12288 { + continue; + } + assert!(words <= 32768, "{runs} runs: {words} table words, past what the form can take"); + use super::compact::{automatic_plan_within, default_table_words}; + let (nvidia, amd) = (default_table_words(Some(0x10DE)), default_table_words(Some(0x1002))); + assert!(automatic_plan_within(&cfg, &lw, false, nvidia).is_none(), "{runs} runs: {words} table words take the compact form on NVIDIA"); + assert!(automatic_plan_within(&cfg, &lw, false, amd).is_some(), "{runs} runs: {words} table words: AMD keeps the compact form"); + return; + } + panic!("no program of 6 to 12 runs passes 12,288 table words"); +} + +#[test] +fn compact_table_limit_switch_parses() { + use super::compact::table_words; + const NVIDIA: Option = Some(0x10DE); + // by vendor: NVIDIA 12,288; AMD, Intel, WARP and not known yet 32,768 + assert_eq!(table_words(None, NVIDIA), 12288); + assert_eq!([Some(0x1002), Some(0x8086), Some(0x1414), None].map(|v| table_words(None, v)), [32768; 4]); + // the switch, on any vendor + assert_eq!((table_words(Some("32768"), NVIDIA), table_words(Some(" 20000 "), Some(0x1002))), (32768, 20000)); + assert_eq!((table_words(Some("0"), NVIDIA), table_words(Some("40000"), NVIDIA), table_words(Some("x"), Some(0x1002))), (12288, 12288, 32768)); +} + /// Builds a straight-line + loop program with `extra_classes` distinct blocks (each adding a /// distinct immediate to `s0` / `v1`, separated by a trivial conditional branch so every block /// becomes its own compact class), plus a bounded loop at the end. diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index f57cf8d7c0..168d43499f 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -105,7 +105,12 @@ pub fn compact_mode() -> Option { /// Non-default `XCL_DIRECT_COMPACT_NEST` cache tag for the translated cache identity. pub fn compact_nest_tag() -> Option { - super::compact::nest_mode().cache_tag() + // (with a table limit other than the default too, `compact::table_words_tag`) + match (super::compact::nest_mode().cache_tag(), super::compact::table_words_tag()) { + (n, None) => n, + (None, t) => t, + (Some(n), Some(t)) => Some(format!("{n}-{t}")), + } } /// `translate_full` in the compact form (`compact = true`) or the plain one. diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index e70e3d9e85..b3f18c8486 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1012,6 +1012,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result Optio } else { crate::gpu::adapter::candidates(&factory, &selector).into_iter().next()?.0.cast().ok()? }; + // Cache identity can be read during launch preparation before a device exists. + crate::gpu::adapter::note_main_vendor(adapter.GetDesc().ok()?.VendorId); preparation_adapter_identity(&adapter) } } diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 9945b2849d..b9e8700bc2 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -2501,6 +2501,7 @@ impl Pipelines { (crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?, desc.VendorId) }; crate::hle::d3d12x::real::driver_cache::note_vendor(vendor); + crate::gpu::adapter::note_main_vendor(vendor); let most = pipeline_threads(); let max_dxil = pipeline_cap_kb(std::env::var("XCL_PIPELINE_MAX_KB").ok().as_deref()).saturating_mul(1024); Ok(Pipelines {