cpu: how should the shared lowering handle LDMXCSR and STMXCSR? #988

Closed
opened 2026-11-23 20:24:18 +00:00 by legacy-code-archaeologist · 2 comments
legacy-code-archaeologist commented 2026-11-23 20:24:18 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T20:24:18Z.


Claim and design question: how should the shared lowering handle LDMXCSR and STMXCSR?

ldmxcsr and stmxcsr (SSE group) and vldmxcsr and vstmxcsr (AVX group) of docs/coverage/x64_zen2.tsv have no passing fixture, and no translator accepts them. The shared lowering assumes the default MXCSR today. tools/ffx_version_query.py refuses an explicit non-default rounding immediate with unsupported MXCSR rounding mode, and tools/cpu_sse2_aot.py documents round-to-nearest-even. CRT startup and game code commonly read and write MXCSR, for example _controlfp, fesetround, or setting FTZ/DAZ for denormal performance. So these two instructions sit on the path to #870. I found no open issue or PR about them and would like to take a first increment. The model is a design decision, so I'm asking before I start.

Options

  1. Host passthrough (opt-in profile, like #937).
    • STMXCSR m32 stores _mm_getcsr(), and LDMXCSR m32 calls _mm_setcsr().
    • Translated float operations are host SSE instructions, so they follow the new rounding mode and FTZ/DAZ at run time.
    • Risk: the C++ compiler may fold or reorder float operations across the MXCSR write unless the generated file uses #pragma fenv_access(on) (or /fp:strict). A guest LDMXCSR with reserved bits set raises #GP natively; _mm_setcsr has to reproduce that, or the lowering has to check the value.
  2. Fixed default profile.
    • STMXCSR stores the current guest value, initially 0x1F80.
    • LDMXCSR accepts only values that keep round-to-nearest-even with FTZ and DAZ clear, so only the exception masks and sticky flags may change. Any other value is refused at run time through a named host fault.
    • This keeps every existing float lowering valid without changes, but programs that set FTZ/DAZ would stop there.
  3. A guest MXCSR model. MXCSR becomes guest state, read by every float lowering. That is the most faithful option and the most invasive; I would not start there.

Proposed first increment, if you agree with option 1:

  • An opt-in keyword, separate from host_cpu and host_entropy.
  • #pragma fenv_access(on) in files that use it.
  • A source-built differential that:
    • sets each rounding mode and FTZ/DAZ with LDMXCSR and reads it back with STMXCSR;
    • runs ADDSS/MULSS/CVTSS2SI on inputs whose results depend on the mode (ties, denormals);
    • compares native and translated bits;
    • restores the caller's MXCSR afterwards.
  • Controls: a translation that ignores the write, one that stores a fixed 0x1F80, and a reserved-bit LDMXCSR in a child process (same fault both ways).

Questions

  1. Which model do you want first: host passthrough, the fixed default with runtime refusal, or something else?
  2. Should FTZ/DAZ be in scope for the first increment, or only rounding control and the exception masks?

Test host: Intel Skylake (i7-6820HQ) on Windows 11 26100.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6568105746 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/995). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T20:24:18Z. --- ### Claim and design question: how should the shared lowering handle `LDMXCSR` and `STMXCSR`? `ldmxcsr` and `stmxcsr` (SSE group) and `vldmxcsr` and `vstmxcsr` (AVX group) of `docs/coverage/x64_zen2.tsv` have no passing fixture, and no translator accepts them. The shared lowering assumes the default MXCSR today. `tools/ffx_version_query.py` refuses an explicit non-default rounding immediate with `unsupported MXCSR rounding mode`, and `tools/cpu_sse2_aot.py` documents round-to-nearest-even. CRT startup and game code commonly read and write MXCSR, for example `_controlfp`, `fesetround`, or setting FTZ/DAZ for denormal performance. So these two instructions sit on the path to #870. I found no open issue or PR about them and would like to take a first increment. The model is a design decision, so I'm asking before I start. **Options** 1. **Host passthrough (opt-in profile, like #937).** - `STMXCSR m32` stores `_mm_getcsr()`, and `LDMXCSR m32` calls `_mm_setcsr()`. - Translated float operations are host SSE instructions, so they follow the new rounding mode and FTZ/DAZ at run time. - Risk: the C++ compiler may fold or reorder float operations across the MXCSR write unless the generated file uses `#pragma fenv_access(on)` (or `/fp:strict`). A guest `LDMXCSR` with reserved bits set raises #GP natively; `_mm_setcsr` has to reproduce that, or the lowering has to check the value. 2. **Fixed default profile.** - `STMXCSR` stores the current guest value, initially `0x1F80`. - `LDMXCSR` accepts only values that keep round-to-nearest-even with FTZ and DAZ clear, so only the exception masks and sticky flags may change. Any other value is refused at run time through a named host fault. - This keeps every existing float lowering valid without changes, but programs that set FTZ/DAZ would stop there. 3. **A guest MXCSR model.** MXCSR becomes guest state, read by every float lowering. That is the most faithful option and the most invasive; I would not start there. **Proposed first increment, if you agree with option 1:** - An opt-in keyword, separate from `host_cpu` and `host_entropy`. - `#pragma fenv_access(on)` in files that use it. - A source-built differential that: - sets each rounding mode and FTZ/DAZ with `LDMXCSR` and reads it back with `STMXCSR`; - runs `ADDSS`/`MULSS`/`CVTSS2SI` on inputs whose results depend on the mode (ties, denormals); - compares native and translated bits; - restores the caller's MXCSR afterwards. - Controls: a translation that ignores the write, one that stores a fixed `0x1F80`, and a reserved-bit `LDMXCSR` in a child process (same fault both ways). **Questions** 1. Which model do you want first: host passthrough, the fixed default with runtime refusal, or something else? 2. Should FTZ/DAZ be in scope for the first increment, or only rounding control and the exception masks? Test host: Intel Skylake (i7-6820HQ) on Windows 11 26100. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:38 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T20:30:24Z.


Please take a narrow opt-in host-MXCSR profile for the shared lowerer. Start with the legacy m32 load/store and a straight-line source-built leaf; VEX spellings can follow with their own encoding and host-capability checks. Use a checked, isolated call boundary that saves and restores the caller's entire MXCSR on normal return and on error, including a guarded memory fault. Keep exceptions masked in this first slice. Refuse unmasked exceptions, unsupported bits and reserved bits before loading the host register, with a distinguishable unsupported outcome. The native reserved-bit child result is a reference observation, not the promised status of the checked entry.

Test all four rounding modes by round-tripping the word and comparing an actual rounding-sensitive operation that is already supported by the shared lowerer, such as a source-built ADDSS halfway case. Keep FTZ/DAZ changes outside the first slice; they can follow with independent signed subnormal input/output cases and a hardware mask check for DAZ. Compare an input selected at runtime, use MSVC floating-environment access in code that observes the mode, and run normal and /O2. The no-op LDMXCSR and fixed STMXCSR controls should both fail. Also check a non-default caller word (including sticky flags) is restored, that adjacent memory is unchanged, and that an invalid requested value cannot run subsequent guest work. Existing fixed-profile generation stays as-is.

Intel instruction reference · MSVC floating-environment pragma

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7113162090 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/995#issuecomment-7113162090). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T20:30:24Z. --- Please take a narrow opt-in host-MXCSR profile for the shared lowerer. Start with the legacy m32 load/store and a straight-line source-built leaf; VEX spellings can follow with their own encoding and host-capability checks. Use a checked, isolated call boundary that saves and restores the caller's entire MXCSR on normal return and on error, including a guarded memory fault. Keep exceptions masked in this first slice. Refuse unmasked exceptions, unsupported bits and reserved bits before loading the host register, with a distinguishable unsupported outcome. The native reserved-bit child result is a reference observation, not the promised status of the checked entry. Test all four rounding modes by round-tripping the word and comparing an actual rounding-sensitive operation that is already supported by the shared lowerer, such as a source-built ADDSS halfway case. Keep FTZ/DAZ changes outside the first slice; they can follow with independent signed subnormal input/output cases and a hardware mask check for DAZ. Compare an input selected at runtime, use MSVC floating-environment access in code that observes the mode, and run normal and /O2. The no-op LDMXCSR and fixed STMXCSR controls should both fail. Also check a non-default caller word (including sticky flags) is restored, that adjacent memory is unchanged, and that an invalid requested value cannot run subsequent guest work. Existing fixed-profile generation stays as-is. [Intel instruction reference](https://cdrdv2-public.intel.com/835757/325383-sdm-vol-2abcd.pdf) · [MSVC floating-environment pragma](https://learn.microsoft.com/en-us/cpp/preprocessor/fenv-access?view=msvc-170)
R0Y00 commented 2026-11-24 06:49:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T21:41:07Z.


The agreed first increment landed in #1009 (44c7a7b4). The opt-in legacy m32 leaf profile checks requested MXCSR, observes all four rounding modes, and restores the host word on return, refusal and fault. The source-built comparison covers the fixed-word, skipped-load and same-export fault controls, including /O2. VEX spellings and changing FTZ or DAZ remain separate follow-ups with their own capability and denormal evidence.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114784495 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/995#issuecomment-7114784495). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:41:07Z. --- The agreed first increment landed in #1009 (44c7a7b4). The opt-in legacy m32 leaf profile checks requested MXCSR, observes all four rounding modes, and restores the host word on return, refusal and fault. The source-built comparison covers the fixed-word, skipped-load and same-export fault controls, including /O2. VEX spellings and changing FTZ or DAZ remain separate follow-ups with their own capability and denormal evidence.
Sign in to join this conversation.
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
R0Y00/Xbox2Windows#988
No description provided.