shader: saturate f32-to-int conversions once #1568

Closed
github-bot wants to merge 1 commit from gh-pr/1568/head into gh-pr/1568/base
Owner

@⁠Nawid3333 on GitHub · 2026-10-16 19:06 UTC · original
Nawid3333:shader/f32-to-int-single-saturation → main · +22 −41 · 3 files · 1 commits · closed without merging

Another one for nmzik's idea in #1534. V_CVT_U32_F32, V_CVT_I32_F32, V_CVT_FLR_I32_F32 and V_CVT_RPI_I32_F32 saturated twice: ConvertF32ToU32Saturated/ConvertF32ToI32Saturated clamped the value in IR (NaN test, two compares, trunc, float and integer selects), and EmitF32ToU32 clamped it again.

Change

  • The four ops emit ConvertU32F32/ConvertS32F32 directly. The 16-bit and byte conversions keep the IR clamp to their own bounds.
  • EmitF32ToU32 applies the guest rules (truncate, saturate, NaN -> 0): NClamp to the largest in-range float (for i32 after a NaN -> 0 select), so OpConvertFToU/S never sees an out-of-range value, and one select for inputs >= 2^32 or 2^31.
  • The unused EmitTruncF32Value alias is removed.

Size

before after
SPIR-V per V_CVT_U32_F32 / V_CVT_I32_F32 19 / 22 4 / 7
AMD ISA, 64 conversions of per-lane buffer values (16 per op), VALU instructions, RX 9070 XT 1,504 768
same, Ryzen 7600X iGPU 1,696 976

Same results

  • GPU check of the guest rules against the new form for all 2^32 inputs, unsigned and signed, with and without Kyty's float controls: 0 differences on the RX 9070 XT and on the Ryzen 7600X iGPU. A signed variant without the NaN select reports 16,777,214 differences, one per NaN, so the check does catch errors.
  • A test that converts buffer-loaded values per lane with all four ops (16 values including NaNs, ±inf, -0, a denormal, -0.75, -2.5, ±2^31, just below -2^31, 2^32 and the largest in-range values) passes on both GPUs before and after.
  • At 3ff6606f on both GPUs: all compute-suite entries, also with the harness forced to 32-wide subgroups (a local switch), and the 26 graphics cases plus 16 production raster checks give the same status and first mismatch as before; CvtF32ToIntSaturatesNaNAndOutOfRange and VectorFloatConversionOps pass.
  • NClamp is new to Kyty. GLSL.std.450 gives it NMin/NMax semantics, so a NaN input yields the lower bound: that is the guest's NaN -> 0 for the unsigned conversion, while the signed one needs the extra NaN select because its lower bound is -2^31. SPIRV-Cross (MoltenVK) maps it to Metal's precise::clamp.
  • Fork CI (build and tests on Windows, Linux and macOS) passes.

#1495 (open) removes the same duplicate from the other side: it skips the backend clamp when the IR already clamped.

If you'd like this done differently, tell me and I'll change it.

Written with Claude Code (AI): it wrote the change and ran the tests and measurements on my PC (driver 26.8.1, Windows 11). I reviewed the diff and this description before opening the PR.

🤖 Generated with Claude Code

**@⁠Nawid3333** on GitHub · 2026-10-16 19:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1568) `Nawid3333:shader/f32-to-int-single-saturation` → `main` · +22 −41 · 3 files · 1 commits · closed without merging Another one for nmzik's idea in #1534. V_CVT_U32_F32, V_CVT_I32_F32, V_CVT_FLR_I32_F32 and V_CVT_RPI_I32_F32 saturated twice: `ConvertF32ToU32Saturated`/`ConvertF32ToI32Saturated` clamped the value in IR (NaN test, two compares, trunc, float and integer selects), and `EmitF32ToU32` clamped it again. **Change** - The four ops emit `ConvertU32F32`/`ConvertS32F32` directly. The 16-bit and byte conversions keep the IR clamp to their own bounds. - `EmitF32ToU32` applies the guest rules (truncate, saturate, NaN -> 0): `NClamp` to the largest in-range float (for i32 after a NaN -> 0 select), so `OpConvertFToU/S` never sees an out-of-range value, and one select for inputs >= 2^32 or 2^31. - The unused `EmitTruncF32Value` alias is removed. **Size** | | before | after | |---|---|---| | SPIR-V per V_CVT_U32_F32 / V_CVT_I32_F32 | 19 / 22 | 4 / 7 | | AMD ISA, 64 conversions of per-lane buffer values (16 per op), VALU instructions, RX 9070 XT | 1,504 | 768 | | same, Ryzen 7600X iGPU | 1,696 | 976 | **Same results** - GPU check of the guest rules against the new form for all 2^32 inputs, unsigned and signed, with and without Kyty's float controls: 0 differences on the RX 9070 XT and on the Ryzen 7600X iGPU. A signed variant without the NaN select reports 16,777,214 differences, one per NaN, so the check does catch errors. - A test that converts buffer-loaded values per lane with all four ops (16 values including NaNs, ±inf, -0, a denormal, -0.75, -2.5, ±2^31, just below -2^31, 2^32 and the largest in-range values) passes on both GPUs before and after. - At 3ff6606f on both GPUs: all compute-suite entries, also with the harness forced to 32-wide subgroups (a local switch), and the 26 graphics cases plus 16 production raster checks give the same status and first mismatch as before; `CvtF32ToIntSaturatesNaNAndOutOfRange` and `VectorFloatConversionOps` pass. - `NClamp` is new to Kyty. GLSL.std.450 gives it NMin/NMax semantics, so a NaN input yields the lower bound: that is the guest's NaN -> 0 for the unsigned conversion, while the signed one needs the extra NaN select because its lower bound is -2^31. SPIRV-Cross (MoltenVK) maps it to Metal's `precise::clamp`. - Fork CI (build and tests on Windows, Linux and macOS) passes. #1495 (open) removes the same duplicate from the other side: it skips the backend clamp when the IR already clamped. If you'd like this done differently, tell me and I'll change it. Written with Claude Code (AI): it wrote the change and ran the tests and measurements on my PC (driver 26.8.1, Windows 11). I reviewed the diff and this description before opening the PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
V_CVT_U32_F32, V_CVT_I32_F32, V_CVT_FLR_I32_F32 and V_CVT_RPI_I32_F32
saturated twice: the frontend clamped the value in IR (NaN test, two
compares, trunc, float and integer selects), and EmitF32ToU32 clamped it
again. The 32-bit conversions now emit ConvertU32F32/ConvertS32F32
directly. EmitF32ToU32 applies the guest rules (truncate, saturate,
NaN -> 0) with NClamp to the largest in-range float, so the conversion
never sees an out-of-range value, and one select for the upper bound.
SPIR-V per guest conversion: 19 -> 4 (u32), 22 -> 7 (i32). The 16-bit and
byte conversions keep the IR clamp to their own bounds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:59:55 +00:00
github-bot closed this pull request 2026-11-26 09:59:55 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-16 19:10 UTC · original · edited 2026-10-17 00:46 UTC

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: acd39cf3-e96d-4ec1-a055-c472dddf1fcb
📥 Commits

Reviewing files that changed from the base of the PR and between 5dec523143 and ab339f74e4.

📒 Files selected for processing: 3
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cpp
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h
  • src/graphics/shader/recompiler/frontend/translate/Convert.cpp
💤 Files with no reviewable changes: 1
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

📝 Walkthrough

The frontend now emits direct float-to-integer conversions for four conversion handlers. The SPIR-V backend clamps float inputs before conversion and applies specified upper-bound results. The signed conversion path maps NaN to zero. The EmitTruncF32Value alias was removed.

Suggested reviewers: nmzik

Priority: ⬇️ Low

Change: Refactor

Merge Risk: ⚪ Minimal · up to ab339

No actionable issue is established from the supplied changes. The conversion update appears mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly summarizes the main change: avoiding duplicate saturation in f32-to-int conversions.
Description check Passed The description directly explains the conversion changes, backend saturation behavior, performance results, and test coverage.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-16 19:10 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1568#issuecomment-6222805615) · edited 2026-10-17 00:46 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1568?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary><strong>ℹ️ Recent review info</strong></summary> <dl> <dd> <details> <summary><strong>⚙️ Run configuration</strong></summary> <dl> <dd> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `acd39cf3-e96d-4ec1-a055-c472dddf1fcb` </dd> </dl> </details> <details> <summary><strong>📥 Commits</strong></summary> <dl> <dd> Reviewing files that changed from the base of the PR and between 5dec523143930e35a6a5a9839f972774420b7958 and ab339f74e4ccd1752d1cd33301ff5d8409c751d3. </dd> </dl> </details> <details> <summary><strong>📒 Files selected for processing: 3</strong></summary> <dl> <dd> * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cpp` * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h` * `src/graphics/shader/recompiler/frontend/translate/Convert.cpp` </dd> </dl> </details> <details> <summary><strong>💤 Files with no reviewable changes: 1</strong></summary> <dl> <dd> * src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review. </dd> </dl> </details> <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> The frontend now emits direct float-to-integer conversions for four conversion handlers. The SPIR-V backend clamps float inputs before conversion and applies specified upper-bound results. The signed conversion path maps NaN to zero. The `EmitTruncF32Value` alias was removed. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ⬇️ Low <!-- change_assessment_commit:"ab339f74e4ccd1752d1cd33301ff5d8409c751d3" --> **Change:** Refactor <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>⚪ Minimal</strong> · up to <code>ab339</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `ab339` --> <!-- final_review_risk_coverage:{"sourceCommitId":"ab339f74e4ccd1752d1cd33301ff5d8409c751d3","coveredCommitId":"ab339f74e4ccd1752d1cd33301ff5d8409c751d3","kind":"reviewed"} --> No actionable issue is established from the supplied changes. The conversion update appears mergeable after normal checks. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------- | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly summarizes the main change: avoiding duplicate saturation in f32-to-int conversions. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description directly explains the conversion changes, backend saturation behavior, performance results, and test coverage. | </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1568)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-24 18:36 UTC · original

Closing: main covers this since 2d00e13b ("rely on the saturating float-to-int conversion instead of re-saturating in the translator") and 3bb09ed2 ("clamp before float-to-int conversion and drop the redundant truncate").

**@⁠Nawid3333** on GitHub · 2026-10-24 18:36 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1568#issuecomment-6388016646) Closing: `main` covers this since 2d00e13b ("rely on the saturating float-to-int conversion instead of re-saturating in the translator") and 3bb09ed2 ("clamp before float-to-int conversion and drop the redundant truncate").

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.