shader: output modifiers turn -0 into +0 #1620
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1620
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1620/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Nawid3333 on GitHub · 2026-10-17 20:56 UTC · original
Nawid3333:shader/omod-signed-zero→main· +37 −0 · 2 files · 7 commits · closed without mergingWhat is wrong
Many RDNA float instructions can scale their result by 2, 4 or 0.5 in the same instruction. This is the output modifier (OMOD), shown as
mul:2,mul:4ordiv:2in disassembly. The RDNA2 ISA (section 6.2.2, "Instruction Outputs") says: "Output modifiers are not IEEE compatible: -0 is flushed to +0."Kyty applies OMOD as a plain multiply, and a plain multiply keeps the sign of zero (-0 × 2 = -0). So where the hardware writes +0, Kyty writes -0. It only shows when later code depends on the sign of zero, for example
1/x(-inf instead of +inf),atan2, a sign test or a bit comparison. LLVM's AMDGPU backend relies on the same hardware rule: it only folds a multiply into OMOD when the sign of zero doesn't matter (SIFoldOperands.cpp: "omod is ignored by hardware if IEEE bit is enabled. omod also does not correctly handle signed zeros.").What changes
Before the OMOD multiply, Kyty adds +0.0 to the result. -0 + (+0) = +0, and x + 0 = x for every other number (a NaN stays a NaN), so only a -0 going into the modifier changes. The module already declares
SignedZeroInfNanPreserve 32, which doesn't allow the driver to remove the add. Clamp still comes after it. Instructions without OMOD are not affected.The add comes before the multiply, not after it, because the hardware keeps the sign of a zero that the modifier itself produces. I measured this on an RDNA2 GPU (Ryzen 7600X iGPU, gfx1036, AMD driver 26.10.1) with AnyPS5's
tools/hw-oracle, IEEE mode off, f32 denormals flushed, round to nearest:mul:2div:2mul:4A -0 (or a flushed denormal) going into the modifier becomes +0, but -2^-126 with
div:2gives -0. With the add after the multiply, Kyty turned that -0 into +0 too; with the add before it, the -0 the multiply produces stays. AnyPS5's RDNA2 (gfx1035) test pins the same row (core/libs/prx/libSceAgcDriver/tests/execution/F32OutputModifier.cpp).What doesn't change
The same ISA section says OMOD does nothing at all when IEEE mode is set or when output denormals are enabled. Kyty doesn't look at those two bits today (
IEEE_MODEisn't passed to the recompiler,FLOAT_MODEonly for compute shaders), so in those modes it already applies OMOD to every value, before and after this change. Compilers only emit OMOD when both bits are off, as LLVM does.Tests
Vop3OutputModifierFlushesNegativeZero: -0.0 × 1.0 withmul:2and -0.0 + -0.0 withdiv:2give +0, -0.5 × 1.0 withmul:4gives -2.0, -0.0 × 1.0 without OMOD stays -0, -0.0 × 1.0 withmul:2in the SDWA encoding gives +0, and -2^-126 × 1.0 withdiv:2has the same sign as the same underflow without OMOD (-2^-126 × 0.5 as a plain multiply). Kyty selects no f32 denormal mode, so a device may keep the denormal or flush it, and the spec doesn't fix the sign of a flushed denormal (KhronosGroup/SPIRV-Registry#430); comparing with the plain multiply holds either way. A last row checks that clamp still comes after the modifier: 2.0 × 1.0 withdiv:2and clamp gives 1.0 (measured the same way on the iGPU; clamping first would give 0.5).eecd98219) with #2038 (needed to start the compute tests on this GPU, see #2036), RX 9070 XT, AMD driver 26.10.1: this test,CvtPkrtzF16F32SdwaAndOutputModifiersandVectorFloatArithmeticOpspass. With the add after the multiply (this PR's first commit) thediv:2row fails: the modifier gives +0 while the plain multiply gives -0. With main's code the -0 rows fail.IndirectImageQueryDimensions), so I ran the three output-modifier cases by name.shader_cfg_tests(not run by CI) passes.477386b88: the Windows, Linux and macOS builds pass.If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it wrote the change and the test, ran the tests and the hardware measurement on my PC (Windows 11, RX 9070 XT and Ryzen 7600X iGPU).
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-17 21:10 UTC · original · edited 2026-11-03 23:44 UTC
Walkthrough
ApplyF32ResultModifiersnow adds0.0fbefore multiplying an F32 result by the selected output modifier. A new compute test assembles instructions with multiple output modifier settings and related VOP2 and SDWA operations. It stores results and checks expected output words and opcodes. The test is registered with the compute test suite.Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Moderate · previous review
58b3dThe new shader test can reject permitted results on some Vulkan devices. Make the assertion portable before merging.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@ -369,6 +369,7 @@ IR::F32 Translator::ApplyF32ResultModifiers(const Decoder::Operand& operand, IR:case 2u: multiplier = 4.0f; break;default: break;}value = IR::F32(ir.Emit(IR::ValueOpcode::FPAdd32, {value, IR::Value::F32(0.0f)}));@coderabbitai[bot] on GitHub · 2026-10-17 21:10 UTC · original · edited 2026-10-17 21:14 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4652
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 20842
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 23559
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8222
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7664
🌐 Web query:
official AMD RDNA2 ISA output modifier OMOD IEEE mode output denormals ignored💡 Result:
Gate OMOD on the active floating-point modes.
PrepareCallTargetpasses only an input-denormal flag toTranslator; it does not passieee_modeor the completefloat_mode.ApplyF32ResultModifiersthen appliesFPMul32andFPAdd32for every nonzerooperand.omod. When IEEE mode or output denormals are enabled, this can change results such as-0.0fto+0.0f, although RDNA2 ignores OMOD in those modes.Pass the relevant mode settings into
Translatorand skip both OMOD operations when OMOD is inactive. Keep clamp handling unchanged.@coderabbitai[bot] on GitHub · 2026-10-17 21:10 UTC · original · reviewed
ℹ️ Review info
⚙️ Run configuration
221b2489-fd4b-4f8a-a49c-8facdcfb93be📥 Commits
Reviewing files that changed from the base of the PR and between
763d3be3a9and04cc9924fd.📒 Files selected for processing: 2
src/graphics/shader/recompiler/frontend/translate/Translate.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -369,6 +369,7 @@ IR::F32 Translator::ApplyF32ResultModifiers(const Decoder::Operand& operand, IR:case 2u: multiplier = 4.0f; break;default: break;}value = IR::F32(ir.Emit(IR::ValueOpcode::FPAdd32, {value, IR::Value::F32(0.0f)}));@Nawid3333 on GitHub · 2026-10-17 21:14 UTC · original
Agreed that OMOD should be skipped when IEEE mode or output denormals are on, but that gap is older than this PR: in those modes Kyty already applies the OMOD multiply to every value, and the description says so under "What doesn't change". Doing it properly needs
IEEE_MODEandFLOAT_MODEfor every shader stage in the recompiler; today only compute shaders getFLOAT_MODEand no stage getsIEEE_MODE. That's a separate change, so I'd keep this PR to the -0 rule. Compilers only emit OMOD when both bits are off, so the -0 case in those modes doesn't come up in compiled shaders.@ -369,6 +369,7 @@ IR::F32 Translator::ApplyF32ResultModifiers(const Decoder::Operand& operand, IR:case 2u: multiplier = 4.0f; break;default: break;}value = IR::F32(ir.Emit(IR::ValueOpcode::FPAdd32, {value, IR::Value::F32(0.0f)}));@coderabbitai[bot] on GitHub · 2026-10-17 21:15 UTC · original
@Nawid3333 Thanks for the clarification. The OMOD mode-gating gap predates this PR and is documented in its scope. I withdraw the request to add mode gating here. This PR can remain focused on the
-0rule.Would you like me to open a follow-up issue for
IEEE_MODEandFLOAT_MODEpropagation across all shader stages and the corresponding OMOD gating?✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-11-03 18:13 UTC · original · edited 2026-11-03 18:24 UTC
On
tests/ShaderRecompilerComputeTests.cppline 28974 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 12576
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41961
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41590
Make the subnormal assertion independent of Vulkan’s F32 denormal mode.
The final case computes
-2^-126 * 0.5. A device that preserves F32 denormals produces0x80400000, while a device that flushes them produces0x80000000. The emitted SPIR-V does not select either F32 denormal mode.Accept both permitted results, but do not accept
+0.0. This retains the check that zero normalization occurs before OMOD rather than after it.✅ Addressed in commit
2891c72@coderabbitai[bot] on GitHub · 2026-11-03 18:13 UTC · original · reviewed
@coderabbitai[bot] on GitHub · 2026-11-03 18:37 UTC · original · reviewed
40e71613-5d40-4a9c-9924-517d52d57d49Reviewing files that changed from the base of the PR and between
15d500be44and2891c72766.tests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-03 23:24 UTC · original · edited 2026-11-03 23:45 UTC
On
tests/ShaderRecompilerComputeTests.cppline 29061 (outdated):Verify the sign of a flushed denormal.
The
v15assertion requires a negative sign even if the OMOD result flushes to zero. The SPIR-V specification does not clearly require that flush to preserve the sign. A device that flushes to+0would fail this test despite producing a permitted result. (github.com)Verify the applicable Vulkan contract. If the sign is not guaranteed, avoid requiring it for this underflow case.
✅ Addressed in commit
477386b@coderabbitai[bot] on GitHub · 2026-11-03 23:24 UTC · original · reviewed
Actionable comments posted: 1
c74ed8b7-f8c5-4058-89c1-2c7c110e6d8cReviewing files that changed from the base of the PR and between
870c40063aand58b3d0747f.src/graphics/shader/recompiler/frontend/translate/Translate.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@Nawid3333 on GitHub · 2026-11-03 23:44 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 29061 (outdated):Good point: the spec doesn't pin down the sign of a flushed denormal (KhronosGroup/SPIRV-Registry#430 is still open). Changed in
477386b88: the test now computes the same underflow a second time without the output modifier (-2^-126 × 0.5 as a plainv_mul_f32) and only requires both results to have the same sign. That holds whether a device keeps f32 denormals or flushes them to a zero of either sign.The row still fails for the order this PR fixes. On an RX 9070 XT, flushing -0 after the modifier (this PR's first commit) gives +0 for the
div:2row while the plain multiply gives -0. With the current order both are -0. Main fails earlier, at the -0 rows.@coderabbitai[bot] on GitHub · 2026-11-03 23:46 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 29061 (outdated):Analysis chain
Script executed:
Length of output: 4495
@Nawid3333Verified the revised assertion intests/ShaderRecompilerComputeTests.cpp. The test now XORs the OMOD result with the plain multiply result, masks the sign bit, and expects zero. It no longer requires a fixed sign for the flushed denormal.This addresses the finding. Your reported RX 9070 XT comparison also confirms that the test still detects the incorrect modifier order. I inspected the code but did not run GPU tests.
Learnings used
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
Review thread resolved.
You are interacting with an AI system.
Pull request closed