Skip to content

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

Done

Description

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 cpu: lower CPUID and XGETBV in an opt-in host-CPU profile #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).

Activity

  1. R0Y00 commented on Nov 23, 2026

    @R0Y00
    Owner

    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

  2. R0Y00 commented on Nov 23, 2026

    @R0Y00
    Owner

    The agreed first increment landed in #1009 (44c7a7b). 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions