Recompiler: unpatch Xbox FP16 placeholder exports for integer SV_Target in direct DXIL #3273

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3273 into main
Kalmarv commented 2026-11-29 03:05:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T03:05:35Z.


What is in it
Unpatches Xbox's FP16 placeholder epilogue (s_nop; s_nop; v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1; exp mrtN vTmp, ..., compr=1) for integer SV_Target outputs (system_value == 64, component_type in {1, 2} for uint/sint) in the direct DXIL pixel shader translator (crates/xcl/src/gpu/dxil/kernel.rs). Xbox retail pixel shaders ship with FP16-compressed exp mrtN placeholders that the console driver normally patches to uncompressed 32-bit integer exports when bound to integer RTVs; translating the unpatched v_cvt_pkrtz_f16_f32 bitcasted 32-bit packed integers as f32 and saturated them to FP16 (0x7bff/0x8000/0x7fff). unpatch_ps_int_exports rewrites matching integer exp mrtN ... compr=1 exports to compr=0 using the original pre-conversion VGPR sources and NOPs dead placeholder v_cvt_pkrtz_f16_f32 instructions, while leaving float SV_Target exports untouched. Adds a unit + DXC-validation + disassembly test in crates/xcl/src/gpu/dxil/core_tests.rs.

Tested

  • cargo test -p xcl --lib direct_pixel_shader_unpatches_int_mrt_exports passed (1 passed; 0 failed): verifies float MRT0 keeps legacyF32ToF16, while uint MRT1 (0xFFC0_0FFC) and uint4 MRT2 export intact 32-bit integers and pass DXC validation.
  • Audited all 648 integer SV_Target pixel shaders in the local retail shader cache (ea3b5e4d): 648/648 match the v_cvt_pkrtz_f16_f32 -> exp mrtN compr=1 placeholder pattern with 0 VGPR alias or non-VGPR source exceptions.
  • 150 s xcl-smoke regression run below on NVIDIA GeForce RTX 5070 Ti (XCL_PREPARE=0, 0 device removals, 0 crashes).

Regression run
Baseline (main ancestor f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

PR branch (a5da9e8303555186d6a8d96925ef6524e676abb2):

xcl-smoke v1
commit: a5da9e8303555186d6a8d96925ef6524e676abb2
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1646 (PresentX 1646)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32813 MB

Not tested

  • HLSL/IR fallback path (only direct DXIL was updated, as direct DXIL is now the default for all stages).
  • GPU draw readback of an integer R32_UINT / R32G32B32A32_UINT RTV on D3D12 hardware; verification is via DXC validation, DXIL disassembly of exact 32-bit integer exports, and the 150 s title smoke.
  • Titles other than ea3b5e4d.

AI use
Drafted, implemented, and tested with an AI coding agent; reviewed and verified by hand against shader cache audit, DXC validation, DXIL disassembly, and 150 s smoke logs.

<!-- greyfinch-sync:ivall/xcl:issue:5547044991 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T03:05:35Z. --- **What is in it** Unpatches Xbox's FP16 placeholder epilogue (`s_nop; s_nop; v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1; exp mrtN vTmp, ..., compr=1`) for integer `SV_Target` outputs (`system_value == 64`, `component_type` in `{1, 2}` for `uint`/`sint`) in the direct DXIL pixel shader translator (`crates/xcl/src/gpu/dxil/kernel.rs`). Xbox retail pixel shaders ship with FP16-compressed `exp mrtN` placeholders that the console driver normally patches to uncompressed 32-bit integer exports when bound to integer RTVs; translating the unpatched `v_cvt_pkrtz_f16_f32` bitcasted 32-bit packed integers as `f32` and saturated them to FP16 (`0x7bff`/`0x8000`/`0x7fff`). `unpatch_ps_int_exports` rewrites matching integer `exp mrtN ... compr=1` exports to `compr=0` using the original pre-conversion VGPR sources and NOPs dead placeholder `v_cvt_pkrtz_f16_f32` instructions, while leaving float `SV_Target` exports untouched. Adds a unit + DXC-validation + disassembly test in `crates/xcl/src/gpu/dxil/core_tests.rs`. **Tested** - `cargo test -p xcl --lib direct_pixel_shader_unpatches_int_mrt_exports` passed (`1 passed; 0 failed`): verifies float `MRT0` keeps `legacyF32ToF16`, while `uint` `MRT1` (`0xFFC0_0FFC`) and `uint4` `MRT2` export intact 32-bit integers and pass DXC validation. - Audited all 648 integer `SV_Target` pixel shaders in the local retail shader cache (`ea3b5e4d`): 648/648 match the `v_cvt_pkrtz_f16_f32` -> `exp mrtN compr=1` placeholder pattern with 0 VGPR alias or non-VGPR source exceptions. - 150 s `xcl-smoke` regression run below on NVIDIA GeForce RTX 5070 Ti (`XCL_PREPARE=0`, `0` device removals, `0` crashes). **Regression run** Baseline (`main` ancestor `f0e8fdd5c3942ddd399d5ee11384c8595c5aacff`): ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` PR branch (`a5da9e8303555186d6a8d96925ef6524e676abb2`): ``` xcl-smoke v1 commit: a5da9e8303555186d6a8d96925ef6524e676abb2 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1646 (PresentX 1646) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32813 MB ``` **Not tested** - HLSL/IR fallback path (only direct DXIL was updated, as direct DXIL is now the default for all stages). - GPU draw readback of an integer `R32_UINT` / `R32G32B32A32_UINT` RTV on D3D12 hardware; verification is via DXC validation, DXIL disassembly of exact 32-bit integer exports, and the 150 s title smoke. - Titles other than `ea3b5e4d`. **AI use** Drafted, implemented, and tested with an AI coding agent; reviewed and verified by hand against shader cache audit, DXC validation, DXIL disassembly, and 150 s smoke logs.
Foniass commented 2026-11-29 14:56:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:56:51Z.


merge-check
base: fa204595a488c60c6c5f905244f0172072c9c2a9
head: 98fc0cd13c37e3737bf1af52235ee7be65319b5f
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> the lib binary aborted twice on the known xcl-pso-ahead 0x4332 test race (a race in main's own test, fixed in #3306); third lib run: 2466 passed, 4 failed, 29 ignored; SKIPPED: 38; other crates in the first run: 139 passed, 0 failed, 20 ignored; failing: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (driver E_OUTOFMEMORY at seeds 70/545/1019/1507; they fail on base on this machine too)
review: only changes the direct DXIL path (kernel.rs); the HLSL fallback keeps the old behaviour, as the PR says. The match is conservative and the test fails without it. Not checked on a title with integer render targets.
performance: 60 s smoke on the merged tree: 1665 frames (99.0 %), peak commit 17178 MB (101.3 %), no removal, no crash
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti (driver 32.0.16.2114), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

Note: since this run the PR conflicts with main (3b6a91ed). The result above is for this head merged onto the base named above.

Merge-check run and written with Claude Code (Claude Opus 5.5): a test merge onto the base above, the full suite, and a 60 s scripts/smoke.ps1 run of a retail title against main on the same machine. The 4 fuzz_validate_and_gpu_* failures happen on main on this machine too.

<!-- greyfinch-sync:ivall/xcl:comment:7257699153 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#issuecomment-7257699153). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-29T14:56:51Z. --- ```text merge-check base: fa204595a488c60c6c5f905244f0172072c9c2a9 head: 98fc0cd13c37e3737bf1af52235ee7be65319b5f merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> the lib binary aborted twice on the known xcl-pso-ahead 0x4332 test race (a race in main's own test, fixed in #3306); third lib run: 2466 passed, 4 failed, 29 ignored; SKIPPED: 38; other crates in the first run: 139 passed, 0 failed, 20 ignored; failing: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (driver E_OUTOFMEMORY at seeds 70/545/1019/1507; they fail on base on this machine too) review: only changes the direct DXIL path (kernel.rs); the HLSL fallback keeps the old behaviour, as the PR says. The match is conservative and the test fails without it. Not checked on a title with integer render targets. performance: 60 s smoke on the merged tree: 1665 frames (99.0 %), peak commit 17178 MB (101.3 %), no removal, no crash verdict: safe to merge hardware / tool and model: RTX 5070 Ti (driver 32.0.16.2114), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 ``` Note: since this run the PR conflicts with main (3b6a91ed). The result above is for this head merged onto the base named above. Merge-check run and written with Claude Code (Claude Opus 5.5): a test merge onto the base above, the full suite, and a 60 s `scripts/smoke.ps1` run of a retail title against main on the same machine. The 4 `fuzz_validate_and_gpu_*` failures happen on main on this machine too.
Foniass commented 2026-12-01 06:24:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:24:36Z.


merge-check
base: d0832e614f
head: a5da9e8303
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2890 passed, 1 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: gpu::dxil::core_tests::direct_pixel_shader_unpatches_int_mrt_exports
review: re-review (my earlier merge-check was on 98fc0cd1). Since then the PR was only rebased, but its new base includes #3200 (recomp::patch_ps_32bit_exports), which already turns these placeholder exports into 32-bit exports for 32-bit render targets in both the HLSL and direct DXIL backends, with the mask from the pipeline's formats. On the merged tree it runs first (kernel.rs ~304), so for R32* integer targets this PR does nothing at run time. What it adds is integer targets on 8- and 16-bit formats, in direct DXIL only. Findings: (1) the description and the test (core_tests.rs ~1577, R32-style values, no format mask) still argue the R32_UINT case; they should be about the narrower case. (2) Shaders that fall back to HLSL keep the old packed-f16 output, so the two backends now disagree for those targets. (3) unpatch_ps_int_exports mostly duplicates patch_ps_32bit_exports; adding the signature's integer-target bits to ps_rt32_mask and reusing it would cover both backends. (4) Minor: the no-op written at ~769 doesn't keep the instruction length as main's helper does. (5) AI use names no tool or model. Fine: it only touches compressed exports to integer targets fed directly by the f16-pack placeholder, keeps the conversion when its result is read later, leaves float targets alone, and the test validates the DXIL. Aside, on main and not from this PR: patch_ps_32bit_exports checks a VOP3 field "CLMP" (recomp/mod.rs ~211) while the encoding table calls it "CLAMP", so a clamped VOP3 conversion is never declined there; this PR uses the right name.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (9e338067): 1516 -> 1688 frames shown (111 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17556 MB, 0 device removals, 0 crashes.
verdict: not yet: the PR's own test fails merged onto current main (core_tests.rs:1718, "MRT1 col 0 should store raw 0xFFC00FFC"), consistent with finding (1): main's #3200 now handles the R32 case the test is built around, so the PR and its test need reworking around the 8/16-bit integer-target case
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7302838849 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#issuecomment-7302838849). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:24:36Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: a5da9e8303555186d6a8d96925ef6524e676abb2 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2890 passed, 1 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: gpu::dxil::core_tests::direct_pixel_shader_unpatches_int_mrt_exports review: re-review (my earlier merge-check was on 98fc0cd1). Since then the PR was only rebased, but its new base includes #3200 (`recomp::patch_ps_32bit_exports`), which already turns these placeholder exports into 32-bit exports for 32-bit render targets in both the HLSL and direct DXIL backends, with the mask from the pipeline's formats. On the merged tree it runs first (kernel.rs ~304), so for R32* integer targets this PR does nothing at run time. What it adds is integer targets on 8- and 16-bit formats, in direct DXIL only. Findings: (1) the description and the test (core_tests.rs ~1577, R32-style values, no format mask) still argue the R32_UINT case; they should be about the narrower case. (2) Shaders that fall back to HLSL keep the old packed-f16 output, so the two backends now disagree for those targets. (3) `unpatch_ps_int_exports` mostly duplicates `patch_ps_32bit_exports`; adding the signature's integer-target bits to `ps_rt32_mask` and reusing it would cover both backends. (4) Minor: the no-op written at ~769 doesn't keep the instruction length as main's helper does. (5) AI use names no tool or model. Fine: it only touches compressed exports to integer targets fed directly by the f16-pack placeholder, keeps the conversion when its result is read later, leaves float targets alone, and the test validates the DXIL. Aside, on main and not from this PR: `patch_ps_32bit_exports` checks a VOP3 field "CLMP" (recomp/mod.rs ~211) while the encoding table calls it "CLAMP", so a clamped VOP3 conversion is never declined there; this PR uses the right name. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (9e338067): 1516 -> 1688 frames shown (111 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17556 MB, 0 device removals, 0 crashes. verdict: not yet: the PR's own test fails merged onto current main (core_tests.rs:1718, "MRT1 col 0 should store raw 0xFFC00FFC"), consistent with finding (1): main's #3200 now handles the R32 case the test is built around, so the PR and its test need reworking around the 8/16-bit integer-target case hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
bmjask commented 2026-12-01 20:14:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T20:14:46Z.

Code review location: crates/xcl/src/gpu/dxil/kernel.rs; line 327

@@ -323,6 +323,9 @@ fn translate_on_this_thread(
         return Err("split wave64 LDS access needs the HLSL path".into());
     }
     let leaders: BTreeSet<u64> = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect();
+    if let Entry::Pixel(ps) = &entry {
+        unpatch_ps_int_exports(&mut cfg.insts, &leaders, ps);

This unpatch only runs in the direct-DXIL translator. The shared patch_ps_32bit_exports (recomp/mod.rs:188), which both backends run, is keyed only on 32-bit render target formats. So for a uint SV_Target with rt32_mask 0 (a non-32-bit integer render target, or the precompile path, which passes 0), direct DXIL exports the raw value. The same shader falling back to HLSL (the whole-wave reduction check right after this, ps_debug, or direct graphics turned off) still writes f16-saturated 0x7bff/0x8000. ORing a signature-derived integer-target mask into ps_rt32_mask before the shared pass would fix both backends.

<!-- greyfinch-sync:ivall/xcl:review-comment:4760337857 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#discussion_r4760337857). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T20:14:46Z. **Code review location:** `crates/xcl/src/gpu/dxil/kernel.rs`; line 327 ~~~~diff @@ -323,6 +323,9 @@ fn translate_on_this_thread( return Err("split wave64 LDS access needs the HLSL path".into()); } let leaders: BTreeSet<u64> = cfg.blocks.iter().filter_map(|b| b.insts.first().map(|&i| cfg.insts[i].pc)).collect(); + if let Entry::Pixel(ps) = &entry { + unpatch_ps_int_exports(&mut cfg.insts, &leaders, ps); ~~~~ --- This unpatch only runs in the direct-DXIL translator. The shared `patch_ps_32bit_exports` (recomp/mod.rs:188), which both backends run, is keyed only on 32-bit render target formats. So for a uint SV_Target with rt32_mask 0 (a non-32-bit integer render target, or the precompile path, which passes 0), direct DXIL exports the raw value. The same shader falling back to HLSL (the whole-wave reduction check right after this, ps_debug, or direct graphics turned off) still writes f16-saturated 0x7bff/0x8000. ORing a signature-derived integer-target mask into ps_rt32_mask before the shared pass would fix both backends.
bmjask commented 2026-12-01 20:14:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T20:14:46Z.

Code review location: crates/xcl/src/gpu/dxil/kernel.rs; line 711

@@ -607,6 +610,169 @@ fn translate_on_this_thread(
     lw.finish_dispatch(numthreads, hw, limit, split_tid)
 }
 
+/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`)
+/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder
+/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console
+/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the
+/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into
+/// f16 saturation values while `compr=1` splits the 32-bit value across two channels.
+fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) {
+    use crate::gpu::rdna2;
+    let mut int_targets = 0u8;
+    for e in &ps.outputs {
+        if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) {
+            int_targets |= 1u8 << e.index;
+        }
+    }
+    if int_targets == 0 {
+        return;
+    }
+
+    let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> {
+        if d.inst.name != "V_CVT_PKRTZ_F16_F32" {
+            return None;
+        }
+        let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n);
+        let vdst = fv("VDST")?;
+        let s0 = fv("SRC0")?;
+        if !(256..512).contains(&s0) {
+            return None;
+        }
+        let s1 = match d.enc.name {
+            "ENC_VOP2" => fv("VSRC1")?,
+            "ENC_VOP3" => {
+                if fv("ABS").unwrap_or(0) != 0
+                    || fv("NEG").unwrap_or(0) != 0
+                    || fv("CLAMP").unwrap_or(0) != 0
+                    || fv("OMOD").unwrap_or(0) != 0
+                {
+                    return None;
+                }
+                let raw = fv("SRC1")?;
+                if !(256..512).contains(&raw) {
+                    return None;
+                }
+                raw - 256
+            }
+            _ => return None,
+        };
+        if vdst >= 256 || s1 >= 256 {
+            return None;
+        }
+        Some((vdst, s0 - 256, s1))
+    };
+
+    let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool {
+        let mut k = after + 1;
+        while k < insts.len() {
+            if leaders.contains(&insts[k].pc) {
+                return false;
+            }
+            let (reads, writes) = rdna2::vgpr_uses(&insts[k]);
+            if reads.contains(&reg) {
+                return false;
+            }
+            if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" {
+                return true;
+            }
+            if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 {
+                return false;
+            }
+            k += 1;
+        }
+        true
+    };
+
+    for i in 0..insts.len() {
+        if insts[i].enc.name != "ENC_EXP" {
+            continue;
+        }
+        let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0);
+        let tgt = fv("TGT");
+        let en = fv("EN");
+        let compr = fv("COMPR") != 0;
+        if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 {
+            continue;
+        }
+        let need_lo = (en & 0x3) != 0;
+        let need_hi = (en & 0xC) != 0;
+        let vsrc0 = fv("VSRC0");
+        let vsrc1 = fv("VSRC1");
+        let done = fv("DONE");
+        let vm = fv("VM");
+
+        let mut lo: Option<(usize, u64, u64, u64)> = None;
+        let mut hi: Option<(usize, u64, u64, u64)> = None;
+        let mut j = i;
+        while j > 0 && !leaders.contains(&insts[j].pc) {
+            j -= 1;
+            if insts[j].inst.name == "S_NOP" {
+                continue;
+            }

The backward scan from an exp stops at the first instruction that isn't S_NOP or V_CVT_PKRTZ_F16_F32. In v_cvt_pkrtz v2,..; v_cvt_pkrtz v4,v6,v6; exp mrt0 v2..; exp mrt1 v4 compr, the scan from exp mrt1 hits exp mrt0 and gives up, and so does any s_waitcnt or unrelated VALU in between. The uint target is still exported as f16-saturated garbage. find_pkrtz in recomp/mod.rs walks past instructions that don't write the register.

<!-- greyfinch-sync:ivall/xcl:review-comment:4760337868 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#discussion_r4760337868). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T20:14:46Z. **Code review location:** `crates/xcl/src/gpu/dxil/kernel.rs`; line 711 ~~~~diff @@ -607,6 +610,169 @@ fn translate_on_this_thread( lw.finish_dispatch(numthreads, hw, limit, split_tid) } +/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`) +/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder +/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console +/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the +/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into +/// f16 saturation values while `compr=1` splits the 32-bit value across two channels. +fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) { + use crate::gpu::rdna2; + let mut int_targets = 0u8; + for e in &ps.outputs { + if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) { + int_targets |= 1u8 << e.index; + } + } + if int_targets == 0 { + return; + } + + let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> { + if d.inst.name != "V_CVT_PKRTZ_F16_F32" { + return None; + } + let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n); + let vdst = fv("VDST")?; + let s0 = fv("SRC0")?; + if !(256..512).contains(&s0) { + return None; + } + let s1 = match d.enc.name { + "ENC_VOP2" => fv("VSRC1")?, + "ENC_VOP3" => { + if fv("ABS").unwrap_or(0) != 0 + || fv("NEG").unwrap_or(0) != 0 + || fv("CLAMP").unwrap_or(0) != 0 + || fv("OMOD").unwrap_or(0) != 0 + { + return None; + } + let raw = fv("SRC1")?; + if !(256..512).contains(&raw) { + return None; + } + raw - 256 + } + _ => return None, + }; + if vdst >= 256 || s1 >= 256 { + return None; + } + Some((vdst, s0 - 256, s1)) + }; + + let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool { + let mut k = after + 1; + while k < insts.len() { + if leaders.contains(&insts[k].pc) { + return false; + } + let (reads, writes) = rdna2::vgpr_uses(&insts[k]); + if reads.contains(&reg) { + return false; + } + if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" { + return true; + } + if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 { + return false; + } + k += 1; + } + true + }; + + for i in 0..insts.len() { + if insts[i].enc.name != "ENC_EXP" { + continue; + } + let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0); + let tgt = fv("TGT"); + let en = fv("EN"); + let compr = fv("COMPR") != 0; + if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 { + continue; + } + let need_lo = (en & 0x3) != 0; + let need_hi = (en & 0xC) != 0; + let vsrc0 = fv("VSRC0"); + let vsrc1 = fv("VSRC1"); + let done = fv("DONE"); + let vm = fv("VM"); + + let mut lo: Option<(usize, u64, u64, u64)> = None; + let mut hi: Option<(usize, u64, u64, u64)> = None; + let mut j = i; + while j > 0 && !leaders.contains(&insts[j].pc) { + j -= 1; + if insts[j].inst.name == "S_NOP" { + continue; + } ~~~~ --- The backward scan from an `exp` stops at the first instruction that isn't S_NOP or V_CVT_PKRTZ_F16_F32. In `v_cvt_pkrtz v2,..; v_cvt_pkrtz v4,v6,v6; exp mrt0 v2..; exp mrt1 v4 compr`, the scan from `exp mrt1` hits `exp mrt0` and gives up, and so does any s_waitcnt or unrelated VALU in between. The uint target is still exported as f16-saturated garbage. `find_pkrtz` in recomp/mod.rs walks past instructions that don't write the register.
bmjask commented 2026-12-01 20:14:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T20:14:46Z.

Code review location: crates/xcl/src/gpu/dxil/kernel.rs; line 748

@@ -607,6 +610,169 @@ fn translate_on_this_thread(
     lw.finish_dispatch(numthreads, hw, limit, split_tid)
 }
 
+/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`)
+/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder
+/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console
+/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the
+/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into
+/// f16 saturation values while `compr=1` splits the 32-bit value across two channels.
+fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) {
+    use crate::gpu::rdna2;
+    let mut int_targets = 0u8;
+    for e in &ps.outputs {
+        if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) {
+            int_targets |= 1u8 << e.index;
+        }
+    }
+    if int_targets == 0 {
+        return;
+    }
+
+    let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> {
+        if d.inst.name != "V_CVT_PKRTZ_F16_F32" {
+            return None;
+        }
+        let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n);
+        let vdst = fv("VDST")?;
+        let s0 = fv("SRC0")?;
+        if !(256..512).contains(&s0) {
+            return None;
+        }
+        let s1 = match d.enc.name {
+            "ENC_VOP2" => fv("VSRC1")?,
+            "ENC_VOP3" => {
+                if fv("ABS").unwrap_or(0) != 0
+                    || fv("NEG").unwrap_or(0) != 0
+                    || fv("CLAMP").unwrap_or(0) != 0
+                    || fv("OMOD").unwrap_or(0) != 0
+                {
+                    return None;
+                }
+                let raw = fv("SRC1")?;
+                if !(256..512).contains(&raw) {
+                    return None;
+                }
+                raw - 256
+            }
+            _ => return None,
+        };
+        if vdst >= 256 || s1 >= 256 {
+            return None;
+        }
+        Some((vdst, s0 - 256, s1))
+    };
+
+    let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool {
+        let mut k = after + 1;
+        while k < insts.len() {
+            if leaders.contains(&insts[k].pc) {
+                return false;
+            }
+            let (reads, writes) = rdna2::vgpr_uses(&insts[k]);
+            if reads.contains(&reg) {
+                return false;
+            }
+            if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" {
+                return true;
+            }
+            if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 {
+                return false;
+            }
+            k += 1;
+        }
+        true
+    };
+
+    for i in 0..insts.len() {
+        if insts[i].enc.name != "ENC_EXP" {
+            continue;
+        }
+        let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0);
+        let tgt = fv("TGT");
+        let en = fv("EN");
+        let compr = fv("COMPR") != 0;
+        if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 {
+            continue;
+        }
+        let need_lo = (en & 0x3) != 0;
+        let need_hi = (en & 0xC) != 0;
+        let vsrc0 = fv("VSRC0");
+        let vsrc1 = fv("VSRC1");
+        let done = fv("DONE");
+        let vm = fv("VM");
+
+        let mut lo: Option<(usize, u64, u64, u64)> = None;
+        let mut hi: Option<(usize, u64, u64, u64)> = None;
+        let mut j = i;
+        while j > 0 && !leaders.contains(&insts[j].pc) {
+            j -= 1;
+            if insts[j].inst.name == "S_NOP" {
+                continue;
+            }
+            let Some((vdst, s0, s1)) = parse_pkrtz(&insts[j]) else {
+                break;
+            };
+            if need_lo && lo.is_none() && vdst == vsrc0 {
+                lo = Some((j, vdst, s0, s1));
+            } else if need_hi && hi.is_none() && vdst == vsrc1 {
+                hi = Some((j, vdst, s0, s1));
+            } else {
+                break;
+            }
+            if (!need_lo || lo.is_some()) && (!need_hi || hi.is_some()) {
+                break;
+            }
+        }
+        if (need_lo && lo.is_none()) || (need_hi && hi.is_none()) {
+            continue;
+        }
+
+        let (c0, c1) = lo.map_or((0, 0), |(_, _, s0, s1)| (s0, s1));
+        let (c2, c3) = hi.map_or((0, 0), |(_, _, s0, s1)| (s0, s1));
+        let srcs = [c0, c1, c2, c3];
+        let lo_dead = lo.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd));
+        let hi_dead = hi.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd));
+        // If a live vDst aliases one of the original source VGPRs, we cannot both keep it and
+        // read the unmodified source at `exp`.
+        if (!lo_dead && lo.is_some_and(|(_, vd, _, _)| srcs.contains(&vd)))
+            || (!hi_dead && hi.is_some_and(|(_, vd, _, _)| srcs.contains(&vd)))
+        {
+            continue;
+        }
+
+        let Ok(exp_bytes) = rdna2::encode(
+            "EXP",
+            "ENC_EXP",
+            &[
+                ("TGT", tgt),
+                ("EN", en),

COMPR is cleared but EN is copied unchanged. When compressed, each EN bit pair enables a 32-bit half (lower_gfx.rs:300), and uncompressed it's one bit per channel. A compressed export with en=0x1 or 0x5 (accepted today as enabling R,G / B,A) becomes an uncompressed en=0x1/0x5, so channels 1 and 3 are never stored. EN needs widening per pair: (en&3!=0 ? 3 : 0) | (en&0xC!=0 ? 0xC : 0).

<!-- greyfinch-sync:ivall/xcl:review-comment:4760337874 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#discussion_r4760337874). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T20:14:46Z. **Code review location:** `crates/xcl/src/gpu/dxil/kernel.rs`; line 748 ~~~~diff @@ -607,6 +610,169 @@ fn translate_on_this_thread( lw.finish_dispatch(numthreads, hw, limit, split_tid) } +/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`) +/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder +/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console +/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the +/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into +/// f16 saturation values while `compr=1` splits the 32-bit value across two channels. +fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) { + use crate::gpu::rdna2; + let mut int_targets = 0u8; + for e in &ps.outputs { + if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) { + int_targets |= 1u8 << e.index; + } + } + if int_targets == 0 { + return; + } + + let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> { + if d.inst.name != "V_CVT_PKRTZ_F16_F32" { + return None; + } + let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n); + let vdst = fv("VDST")?; + let s0 = fv("SRC0")?; + if !(256..512).contains(&s0) { + return None; + } + let s1 = match d.enc.name { + "ENC_VOP2" => fv("VSRC1")?, + "ENC_VOP3" => { + if fv("ABS").unwrap_or(0) != 0 + || fv("NEG").unwrap_or(0) != 0 + || fv("CLAMP").unwrap_or(0) != 0 + || fv("OMOD").unwrap_or(0) != 0 + { + return None; + } + let raw = fv("SRC1")?; + if !(256..512).contains(&raw) { + return None; + } + raw - 256 + } + _ => return None, + }; + if vdst >= 256 || s1 >= 256 { + return None; + } + Some((vdst, s0 - 256, s1)) + }; + + let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool { + let mut k = after + 1; + while k < insts.len() { + if leaders.contains(&insts[k].pc) { + return false; + } + let (reads, writes) = rdna2::vgpr_uses(&insts[k]); + if reads.contains(&reg) { + return false; + } + if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" { + return true; + } + if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 { + return false; + } + k += 1; + } + true + }; + + for i in 0..insts.len() { + if insts[i].enc.name != "ENC_EXP" { + continue; + } + let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0); + let tgt = fv("TGT"); + let en = fv("EN"); + let compr = fv("COMPR") != 0; + if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 { + continue; + } + let need_lo = (en & 0x3) != 0; + let need_hi = (en & 0xC) != 0; + let vsrc0 = fv("VSRC0"); + let vsrc1 = fv("VSRC1"); + let done = fv("DONE"); + let vm = fv("VM"); + + let mut lo: Option<(usize, u64, u64, u64)> = None; + let mut hi: Option<(usize, u64, u64, u64)> = None; + let mut j = i; + while j > 0 && !leaders.contains(&insts[j].pc) { + j -= 1; + if insts[j].inst.name == "S_NOP" { + continue; + } + let Some((vdst, s0, s1)) = parse_pkrtz(&insts[j]) else { + break; + }; + if need_lo && lo.is_none() && vdst == vsrc0 { + lo = Some((j, vdst, s0, s1)); + } else if need_hi && hi.is_none() && vdst == vsrc1 { + hi = Some((j, vdst, s0, s1)); + } else { + break; + } + if (!need_lo || lo.is_some()) && (!need_hi || hi.is_some()) { + break; + } + } + if (need_lo && lo.is_none()) || (need_hi && hi.is_none()) { + continue; + } + + let (c0, c1) = lo.map_or((0, 0), |(_, _, s0, s1)| (s0, s1)); + let (c2, c3) = hi.map_or((0, 0), |(_, _, s0, s1)| (s0, s1)); + let srcs = [c0, c1, c2, c3]; + let lo_dead = lo.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd)); + let hi_dead = hi.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd)); + // If a live vDst aliases one of the original source VGPRs, we cannot both keep it and + // read the unmodified source at `exp`. + if (!lo_dead && lo.is_some_and(|(_, vd, _, _)| srcs.contains(&vd))) + || (!hi_dead && hi.is_some_and(|(_, vd, _, _)| srcs.contains(&vd))) + { + continue; + } + + let Ok(exp_bytes) = rdna2::encode( + "EXP", + "ENC_EXP", + &[ + ("TGT", tgt), + ("EN", en), ~~~~ --- COMPR is cleared but EN is copied unchanged. When compressed, each EN bit pair enables a 32-bit half (lower_gfx.rs:300), and uncompressed it's one bit per channel. A compressed export with en=0x1 or 0x5 (accepted today as enabling R,G / B,A) becomes an uncompressed en=0x1/0x5, so channels 1 and 3 are never stored. EN needs widening per pair: `(en&3!=0 ? 3 : 0) | (en&0xC!=0 ? 0xC : 0)`.
bmjask commented 2026-12-01 20:14:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T20:14:46Z.

Code review location: crates/xcl/src/gpu/dxil/kernel.rs; line 717

@@ -607,6 +610,169 @@ fn translate_on_this_thread(
     lw.finish_dispatch(numthreads, hw, limit, split_tid)
 }
 
+/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`)
+/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder
+/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console
+/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the
+/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into
+/// f16 saturation values while `compr=1` splits the 32-bit value across two channels.
+fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) {
+    use crate::gpu::rdna2;
+    let mut int_targets = 0u8;
+    for e in &ps.outputs {
+        if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) {
+            int_targets |= 1u8 << e.index;
+        }
+    }
+    if int_targets == 0 {
+        return;
+    }
+
+    let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> {
+        if d.inst.name != "V_CVT_PKRTZ_F16_F32" {
+            return None;
+        }
+        let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n);
+        let vdst = fv("VDST")?;
+        let s0 = fv("SRC0")?;
+        if !(256..512).contains(&s0) {
+            return None;
+        }
+        let s1 = match d.enc.name {
+            "ENC_VOP2" => fv("VSRC1")?,
+            "ENC_VOP3" => {
+                if fv("ABS").unwrap_or(0) != 0
+                    || fv("NEG").unwrap_or(0) != 0
+                    || fv("CLAMP").unwrap_or(0) != 0
+                    || fv("OMOD").unwrap_or(0) != 0
+                {
+                    return None;
+                }
+                let raw = fv("SRC1")?;
+                if !(256..512).contains(&raw) {
+                    return None;
+                }
+                raw - 256
+            }
+            _ => return None,
+        };
+        if vdst >= 256 || s1 >= 256 {
+            return None;
+        }
+        Some((vdst, s0 - 256, s1))
+    };
+
+    let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool {
+        let mut k = after + 1;
+        while k < insts.len() {
+            if leaders.contains(&insts[k].pc) {
+                return false;
+            }
+            let (reads, writes) = rdna2::vgpr_uses(&insts[k]);
+            if reads.contains(&reg) {
+                return false;
+            }
+            if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" {
+                return true;
+            }
+            if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 {
+                return false;
+            }
+            k += 1;
+        }
+        true
+    };
+
+    for i in 0..insts.len() {
+        if insts[i].enc.name != "ENC_EXP" {
+            continue;
+        }
+        let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0);
+        let tgt = fv("TGT");
+        let en = fv("EN");
+        let compr = fv("COMPR") != 0;
+        if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 {
+            continue;
+        }
+        let need_lo = (en & 0x3) != 0;
+        let need_hi = (en & 0xC) != 0;
+        let vsrc0 = fv("VSRC0");
+        let vsrc1 = fv("VSRC1");
+        let done = fv("DONE");
+        let vm = fv("VM");
+
+        let mut lo: Option<(usize, u64, u64, u64)> = None;
+        let mut hi: Option<(usize, u64, u64, u64)> = None;
+        let mut j = i;
+        while j > 0 && !leaders.contains(&insts[j].pc) {
+            j -= 1;
+            if insts[j].inst.name == "S_NOP" {
+                continue;
+            }
+            let Some((vdst, s0, s1)) = parse_pkrtz(&insts[j]) else {
+                break;
+            };
+            if need_lo && lo.is_none() && vdst == vsrc0 {
+                lo = Some((j, vdst, s0, s1));
+            } else if need_hi && hi.is_none() && vdst == vsrc1 {

When the export reads the same register twice (vsrc0 == vsrc1), lo takes the nearest conversion and hi takes an older conversion to that register, whose result was already overwritten. v_cvt_pkrtz v4,v10,v11; v_cvt_pkrtz v4,v12,v13; exp mrt1 v4,v4 en=0xF compr becomes exp v12,v13,v10,v11, where the console exports (v12,v13) in both halves, and the older conversion is NOPed.

<!-- greyfinch-sync:ivall/xcl:review-comment:4760337887 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#discussion_r4760337887). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T20:14:46Z. **Code review location:** `crates/xcl/src/gpu/dxil/kernel.rs`; line 717 ~~~~diff @@ -607,6 +610,169 @@ fn translate_on_this_thread( lw.finish_dispatch(numthreads, hw, limit, split_tid) } +/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`) +/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder +/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console +/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the +/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into +/// f16 saturation values while `compr=1` splits the 32-bit value across two channels. +fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) { + use crate::gpu::rdna2; + let mut int_targets = 0u8; + for e in &ps.outputs { + if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) { + int_targets |= 1u8 << e.index; + } + } + if int_targets == 0 { + return; + } + + let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> { + if d.inst.name != "V_CVT_PKRTZ_F16_F32" { + return None; + } + let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n); + let vdst = fv("VDST")?; + let s0 = fv("SRC0")?; + if !(256..512).contains(&s0) { + return None; + } + let s1 = match d.enc.name { + "ENC_VOP2" => fv("VSRC1")?, + "ENC_VOP3" => { + if fv("ABS").unwrap_or(0) != 0 + || fv("NEG").unwrap_or(0) != 0 + || fv("CLAMP").unwrap_or(0) != 0 + || fv("OMOD").unwrap_or(0) != 0 + { + return None; + } + let raw = fv("SRC1")?; + if !(256..512).contains(&raw) { + return None; + } + raw - 256 + } + _ => return None, + }; + if vdst >= 256 || s1 >= 256 { + return None; + } + Some((vdst, s0 - 256, s1)) + }; + + let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool { + let mut k = after + 1; + while k < insts.len() { + if leaders.contains(&insts[k].pc) { + return false; + } + let (reads, writes) = rdna2::vgpr_uses(&insts[k]); + if reads.contains(&reg) { + return false; + } + if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" { + return true; + } + if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 { + return false; + } + k += 1; + } + true + }; + + for i in 0..insts.len() { + if insts[i].enc.name != "ENC_EXP" { + continue; + } + let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0); + let tgt = fv("TGT"); + let en = fv("EN"); + let compr = fv("COMPR") != 0; + if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 { + continue; + } + let need_lo = (en & 0x3) != 0; + let need_hi = (en & 0xC) != 0; + let vsrc0 = fv("VSRC0"); + let vsrc1 = fv("VSRC1"); + let done = fv("DONE"); + let vm = fv("VM"); + + let mut lo: Option<(usize, u64, u64, u64)> = None; + let mut hi: Option<(usize, u64, u64, u64)> = None; + let mut j = i; + while j > 0 && !leaders.contains(&insts[j].pc) { + j -= 1; + if insts[j].inst.name == "S_NOP" { + continue; + } + let Some((vdst, s0, s1)) = parse_pkrtz(&insts[j]) else { + break; + }; + if need_lo && lo.is_none() && vdst == vsrc0 { + lo = Some((j, vdst, s0, s1)); + } else if need_hi && hi.is_none() && vdst == vsrc1 { ~~~~ --- When the export reads the same register twice (vsrc0 == vsrc1), `lo` takes the nearest conversion and `hi` takes an older conversion to that register, whose result was already overwritten. `v_cvt_pkrtz v4,v10,v11; v_cvt_pkrtz v4,v12,v13; exp mrt1 v4,v4 en=0xF compr` becomes `exp v12,v13,v10,v11`, where the console exports (v12,v13) in both halves, and the older conversion is NOPed.
bmjask commented 2026-12-01 20:14:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T20:14:46Z.

Code review location: crates/xcl/src/gpu/dxil/kernel.rs; line 768

@@ -607,6 +610,169 @@ fn translate_on_this_thread(
     lw.finish_dispatch(numthreads, hw, limit, split_tid)
 }
 
+/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`)
+/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder
+/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console
+/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the
+/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into
+/// f16 saturation values while `compr=1` splits the 32-bit value across two channels.
+fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) {
+    use crate::gpu::rdna2;
+    let mut int_targets = 0u8;
+    for e in &ps.outputs {
+        if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) {
+            int_targets |= 1u8 << e.index;
+        }
+    }
+    if int_targets == 0 {
+        return;
+    }
+
+    let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> {
+        if d.inst.name != "V_CVT_PKRTZ_F16_F32" {
+            return None;
+        }
+        let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n);
+        let vdst = fv("VDST")?;
+        let s0 = fv("SRC0")?;
+        if !(256..512).contains(&s0) {
+            return None;
+        }
+        let s1 = match d.enc.name {
+            "ENC_VOP2" => fv("VSRC1")?,
+            "ENC_VOP3" => {
+                if fv("ABS").unwrap_or(0) != 0
+                    || fv("NEG").unwrap_or(0) != 0
+                    || fv("CLAMP").unwrap_or(0) != 0
+                    || fv("OMOD").unwrap_or(0) != 0
+                {
+                    return None;
+                }
+                let raw = fv("SRC1")?;
+                if !(256..512).contains(&raw) {
+                    return None;
+                }
+                raw - 256
+            }
+            _ => return None,
+        };
+        if vdst >= 256 || s1 >= 256 {
+            return None;
+        }
+        Some((vdst, s0 - 256, s1))
+    };
+
+    let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool {
+        let mut k = after + 1;
+        while k < insts.len() {
+            if leaders.contains(&insts[k].pc) {
+                return false;
+            }
+            let (reads, writes) = rdna2::vgpr_uses(&insts[k]);
+            if reads.contains(&reg) {
+                return false;
+            }
+            if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" {
+                return true;
+            }
+            if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 {
+                return false;
+            }
+            k += 1;
+        }
+        true
+    };
+
+    for i in 0..insts.len() {
+        if insts[i].enc.name != "ENC_EXP" {
+            continue;
+        }
+        let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0);
+        let tgt = fv("TGT");
+        let en = fv("EN");
+        let compr = fv("COMPR") != 0;
+        if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 {
+            continue;
+        }
+        let need_lo = (en & 0x3) != 0;
+        let need_hi = (en & 0xC) != 0;
+        let vsrc0 = fv("VSRC0");
+        let vsrc1 = fv("VSRC1");
+        let done = fv("DONE");
+        let vm = fv("VM");
+
+        let mut lo: Option<(usize, u64, u64, u64)> = None;
+        let mut hi: Option<(usize, u64, u64, u64)> = None;
+        let mut j = i;
+        while j > 0 && !leaders.contains(&insts[j].pc) {
+            j -= 1;
+            if insts[j].inst.name == "S_NOP" {
+                continue;
+            }
+            let Some((vdst, s0, s1)) = parse_pkrtz(&insts[j]) else {
+                break;
+            };
+            if need_lo && lo.is_none() && vdst == vsrc0 {
+                lo = Some((j, vdst, s0, s1));
+            } else if need_hi && hi.is_none() && vdst == vsrc1 {
+                hi = Some((j, vdst, s0, s1));
+            } else {
+                break;
+            }
+            if (!need_lo || lo.is_some()) && (!need_hi || hi.is_some()) {
+                break;
+            }
+        }
+        if (need_lo && lo.is_none()) || (need_hi && hi.is_none()) {
+            continue;
+        }
+
+        let (c0, c1) = lo.map_or((0, 0), |(_, _, s0, s1)| (s0, s1));
+        let (c2, c3) = hi.map_or((0, 0), |(_, _, s0, s1)| (s0, s1));
+        let srcs = [c0, c1, c2, c3];
+        let lo_dead = lo.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd));
+        let hi_dead = hi.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd));
+        // If a live vDst aliases one of the original source VGPRs, we cannot both keep it and
+        // read the unmodified source at `exp`.
+        if (!lo_dead && lo.is_some_and(|(_, vd, _, _)| srcs.contains(&vd)))
+            || (!hi_dead && hi.is_some_and(|(_, vd, _, _)| srcs.contains(&vd)))
+        {
+            continue;
+        }
+
+        let Ok(exp_bytes) = rdna2::encode(
+            "EXP",
+            "ENC_EXP",
+            &[
+                ("TGT", tgt),
+                ("EN", en),
+                ("VSRC0", c0),
+                ("VSRC1", c1),
+                ("VSRC2", c2),
+                ("VSRC3", c3),
+                ("COMPR", 0),
+                ("DONE", done),
+                ("VM", vm),
+            ],
+            None,
+        ) else {
+            continue;
+        };
+        let Ok(new_exp) = rdna2::decode(&exp_bytes, insts[i].pc) else {
+            continue;
+        };
+        insts[i] = new_exp;
+
+        for (entry, dead) in [(lo, lo_dead), (hi, hi_dead)] {
+            if let (Some((idx, _, _, _)), true) = (entry, dead) {
+                if let Ok(nop) = rdna2::decode(&0xBF80_0000u32.to_le_bytes(), insts[idx].pc) {

The dead conversion is replaced with a freshly decoded 4-byte S_NOP, so a VOP3-encoded V_CVT_PKRTZ (8 bytes) leaves len=4 at that pc, and the instruction list no longer covers the code. Anything that computes pc + len (s_getpc at lower.rs:1395, loop ranges at recomp/mod.rs:921/951) sees a 4-byte gap. The existing helper sets nop.len = len.

<!-- greyfinch-sync:ivall/xcl:review-comment:4760337894 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#discussion_r4760337894). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T20:14:46Z. **Code review location:** `crates/xcl/src/gpu/dxil/kernel.rs`; line 768 ~~~~diff @@ -607,6 +610,169 @@ fn translate_on_this_thread( lw.finish_dispatch(numthreads, hw, limit, split_tid) } +/// Unpatch Xbox console pixel-shader placeholder export epilogues for integer (`uint`/`sint`) +/// `SV_Target`s. Precompiled console PS binaries emit a patchable FP16 placeholder +/// (`v_cvt_pkrtz_f16_f32 vTmp, vSrc0, vSrc1` + `exp mrtN, vTmp, ... compr=1`) that the console +/// driver patches at PSO creation time for `R32_UINT` / `R32G32B32A32_UINT` etc. Executing the +/// unpatched `v_cvt_pkrtz_f16_f32` bitcasts 32-bit integers as floats and destroys them into +/// f16 saturation values while `compr=1` splits the 32-bit value across two channels. +fn unpatch_ps_int_exports(insts: &mut [Decoded], leaders: &BTreeSet<u64>, ps: &PsInterface) { + use crate::gpu::rdna2; + let mut int_targets = 0u8; + for e in &ps.outputs { + if e.system_value == 64 && e.index < 8 && matches!(e.component_type, 1 | 2) { + int_targets |= 1u8 << e.index; + } + } + if int_targets == 0 { + return; + } + + let parse_pkrtz = |d: &Decoded| -> Option<(u64, u64, u64)> { + if d.inst.name != "V_CVT_PKRTZ_F16_F32" { + return None; + } + let fv = |n: &str| rdna2::field_value(d.enc, &d.words, n); + let vdst = fv("VDST")?; + let s0 = fv("SRC0")?; + if !(256..512).contains(&s0) { + return None; + } + let s1 = match d.enc.name { + "ENC_VOP2" => fv("VSRC1")?, + "ENC_VOP3" => { + if fv("ABS").unwrap_or(0) != 0 + || fv("NEG").unwrap_or(0) != 0 + || fv("CLAMP").unwrap_or(0) != 0 + || fv("OMOD").unwrap_or(0) != 0 + { + return None; + } + let raw = fv("SRC1")?; + if !(256..512).contains(&raw) { + return None; + } + raw - 256 + } + _ => return None, + }; + if vdst >= 256 || s1 >= 256 { + return None; + } + Some((vdst, s0 - 256, s1)) + }; + + let reg_dead_in_block = |insts: &[Decoded], after: usize, reg: u64| -> bool { + let mut k = after + 1; + while k < insts.len() { + if leaders.contains(&insts[k].pc) { + return false; + } + let (reads, writes) = rdna2::vgpr_uses(&insts[k]); + if reads.contains(&reg) { + return false; + } + if writes.contains(&reg) || insts[k].inst.name == "S_ENDPGM" { + return true; + } + if insts[k].inst.flags & rdna2::F_TERMINATOR != 0 { + return false; + } + k += 1; + } + true + }; + + for i in 0..insts.len() { + if insts[i].enc.name != "ENC_EXP" { + continue; + } + let fv = |n: &str| rdna2::field_value(insts[i].enc, &insts[i].words, n).unwrap_or(0); + let tgt = fv("TGT"); + let en = fv("EN"); + let compr = fv("COMPR") != 0; + if tgt >= 8 || !compr || en == 0 || (int_targets & (1u8 << tgt)) == 0 { + continue; + } + let need_lo = (en & 0x3) != 0; + let need_hi = (en & 0xC) != 0; + let vsrc0 = fv("VSRC0"); + let vsrc1 = fv("VSRC1"); + let done = fv("DONE"); + let vm = fv("VM"); + + let mut lo: Option<(usize, u64, u64, u64)> = None; + let mut hi: Option<(usize, u64, u64, u64)> = None; + let mut j = i; + while j > 0 && !leaders.contains(&insts[j].pc) { + j -= 1; + if insts[j].inst.name == "S_NOP" { + continue; + } + let Some((vdst, s0, s1)) = parse_pkrtz(&insts[j]) else { + break; + }; + if need_lo && lo.is_none() && vdst == vsrc0 { + lo = Some((j, vdst, s0, s1)); + } else if need_hi && hi.is_none() && vdst == vsrc1 { + hi = Some((j, vdst, s0, s1)); + } else { + break; + } + if (!need_lo || lo.is_some()) && (!need_hi || hi.is_some()) { + break; + } + } + if (need_lo && lo.is_none()) || (need_hi && hi.is_none()) { + continue; + } + + let (c0, c1) = lo.map_or((0, 0), |(_, _, s0, s1)| (s0, s1)); + let (c2, c3) = hi.map_or((0, 0), |(_, _, s0, s1)| (s0, s1)); + let srcs = [c0, c1, c2, c3]; + let lo_dead = lo.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd)); + let hi_dead = hi.is_none_or(|(_, vd, _, _)| reg_dead_in_block(insts, i, vd)); + // If a live vDst aliases one of the original source VGPRs, we cannot both keep it and + // read the unmodified source at `exp`. + if (!lo_dead && lo.is_some_and(|(_, vd, _, _)| srcs.contains(&vd))) + || (!hi_dead && hi.is_some_and(|(_, vd, _, _)| srcs.contains(&vd))) + { + continue; + } + + let Ok(exp_bytes) = rdna2::encode( + "EXP", + "ENC_EXP", + &[ + ("TGT", tgt), + ("EN", en), + ("VSRC0", c0), + ("VSRC1", c1), + ("VSRC2", c2), + ("VSRC3", c3), + ("COMPR", 0), + ("DONE", done), + ("VM", vm), + ], + None, + ) else { + continue; + }; + let Ok(new_exp) = rdna2::decode(&exp_bytes, insts[i].pc) else { + continue; + }; + insts[i] = new_exp; + + for (entry, dead) in [(lo, lo_dead), (hi, hi_dead)] { + if let (Some((idx, _, _, _)), true) = (entry, dead) { + if let Ok(nop) = rdna2::decode(&0xBF80_0000u32.to_le_bytes(), insts[idx].pc) { ~~~~ --- The dead conversion is replaced with a freshly decoded 4-byte S_NOP, so a VOP3-encoded V_CVT_PKRTZ (8 bytes) leaves len=4 at that pc, and the instruction list no longer covers the code. Anything that computes `pc + len` (s_getpc at lower.rs:1395, loop ranges at recomp/mod.rs:921/951) sees a 4-byte gap. The existing helper sets `nop.len = len`.
bmjask commented 2026-12-01 20:14:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T20:14:46Z.

Review: COMMENTED


Review of a5da9e83: 5 finding(s). 5 are inline.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6139283870 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#pullrequestreview-6139283870). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T20:14:46Z. **Review:** COMMENTED --- Review of `a5da9e83`: 5 finding(s). 5 are inline. --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
Kalmarv commented 2026-12-02 19:38:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T19:38:46Z.


Closing: superseded by merged #3200 (0b6217bda, 32-bit exports for FP16 placeholder exports in both backends).

<!-- greyfinch-sync:ivall/xcl:comment:7356500714 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3273#issuecomment-7356500714). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T19:38:46Z. --- Closing: superseded by merged #3200 (`0b6217bda`, 32-bit exports for FP16 placeholder exports in both backends).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3273
No description provided.