Claim and design question: how should the shared lowering handle LAHF? #1015

Closed
opened 2026-11-23 21:46:04 +00:00 by zohaiblazuli · 4 comments
zohaiblazuli commented 2026-11-23 21:46:04 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by zohaiblazuli on 2026-11-23T21:46:04Z.


Question

How should the shared lowering (tools/ffx_version_query.py) handle LAHF, and what is the smallest first slice you would accept?

lahf (Integer group of docs/coverage/x64_zen2.tsv) has no passing fixture, and no translator accepts it. tests/cpu/x87_compare.py pins it as an "unsupported query operation" after fucomi. I found no open issue, PR or claim about it and would like to take it. The flag model is a design decision, so I'm asking before I start.

LAHF loads AH = SF:ZF:0:AF:0:PF:1:CF. The shared lowering keeps zero, carry, overflow and sign as bools for every function. It adds parity only inside a checked x87 profile that contains JP, JNP, FCMOVU or FCMOVNU, and it doesn't model AF at all. So two of the five bits LAHF reads have no state today.

Options

  1. Gated parity and aux state (my proposal). When a body contains LAHF, declare parity and aux the way parity is gated today. The flag writers whose AF and PF are architecturally defined assign them:

    • ADD, ADC, SUB, SBB, CMP, INC, DEC and NEG set aux = ((a ^ b ^ v) & 0x10) != 0.
    • parity is the even parity of v & 0xff.
    • SAHF and STC/CLC/CMC keep their documented effects. The carry instructions leave AF and PF unchanged.

    Refuse LAHF when the most recent flag writer on any path leaves AF undefined (AND, OR, XOR, TEST, shifts and rotates, MUL/IMUL, BSF/BSR and so on) or is unknown (block entry, a call, a merge of paths with different writers).

  2. SAHF round trip only. Accept LAHF only when the last writer is SAHF, so all five bits come straight from a known AH. This is very small, but it only covers the save/restore idiom.

  3. Full lazy flags. Keep the last result and operands, and compute PF and AF on demand everywhere. This is more general, but it touches every flag writer and the existing parity gating, so I'd rather not start there.

Proposed first slice: option 1, limited to ADD, SUB, CMP, INC, DEC and SAHF as writers, register operands at 8, 16, 32 and 64 bits, and LAHF in the same basic block as its writer. ADC, SBB, NEG and cross-block flows can follow once this is reviewed.

Evidence so far

  • parity gating and the x87-only parity assignments are in lower() (tools/ffx_version_query.py), with the gate comment above them. AF has no state.
  • SAHF is lowered only in the x87 profile, where it sets carry, zero, sign and (when gated) parity from AH. Its comment notes that AF is not modelled.
  • Intel SDM Vol. 2A LAHF and Vol. 1 §3.4.3.1 define the bit layout. AF is defined for the add/subtract family and undefined after logical operations, shifts and multiplies. That's why option 1 refuses those writers instead of guessing a value.
  • LAHF is valid in 64-bit mode when CPUID.80000001H:ECX.LAHF-SAHF[bit 0] = 1. The fixture would check that bit and exit 77 without it, as the AVX fixtures do.

Completion condition

  • There's a source-built MASM fixture with a native and translated comparison of the full AH byte (and the untouched AL, and the rest of RAX) after each accepted writer at each width. Inputs cover:
    • a carry out of bit 3 (AF) with and without a carry out of the top bit;
    • odd and even parity of the low byte, where the higher bytes would flip parity if they were counted;
    • zero and sign results;
    • INC/DEC leaving CF unchanged;
    • SAHF→LAHF with reserved bits 1, 3 and 5 set in the input AH.
  • Refusal controls: LAHF after AND, XOR, TEST, SHL and IMUL; at block entry; after a call; and after a join with different writers.
  • Mutated controls: AF from bit 4 instead of bit 3, parity over the full width, and bit 1 cleared.
  • The existing x87_compare rejection for fucomi then lahf either stays (if the FCOMI family is outside the slice) or moves to a still-unsupported reader.
  • dev and release presets pass, and tools/form_coverage.py no longer lists lahf.

Required environment

x64 Windows with MSVC, the Windows SDK, CMake/Ninja and Python 3.11+ (the standard first build). Any x64 CPU with LAHF-SAHF in 64-bit mode. I'll run it on an Intel Core i5-14600K, Windows 11 build 26300, MSVC 19.50.

Checklist

  • The task can be reproduced with public or source-built inputs.
  • I did not include game files, dumps, distribution links or credentials.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6569471740 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1022). Originally posted by [zohaiblazuli](https://github.com/zohaiblazuli) on 2026-11-23T21:46:04Z. --- ### Question How should the shared lowering (`tools/ffx_version_query.py`) handle `LAHF`, and what is the smallest first slice you would accept? `lahf` (Integer group of `docs/coverage/x64_zen2.tsv`) has no passing fixture, and no translator accepts it. `tests/cpu/x87_compare.py` pins it as an "unsupported query operation" after `fucomi`. I found no open issue, PR or claim about it and would like to take it. The flag model is a design decision, so I'm asking before I start. `LAHF` loads `AH = SF:ZF:0:AF:0:PF:1:CF`. The shared lowering keeps `zero`, `carry`, `overflow` and `sign` as `bool`s for every function. It adds `parity` only inside a checked x87 profile that contains `JP`, `JNP`, `FCMOVU` or `FCMOVNU`, and it doesn't model AF at all. So two of the five bits `LAHF` reads have no state today. **Options** 1. **Gated `parity` and `aux` state (my proposal).** When a body contains `LAHF`, declare `parity` and `aux` the way `parity` is gated today. The flag writers whose AF and PF are architecturally defined assign them: - `ADD`, `ADC`, `SUB`, `SBB`, `CMP`, `INC`, `DEC` and `NEG` set `aux = ((a ^ b ^ v) & 0x10) != 0`. - `parity` is the even parity of `v & 0xff`. - `SAHF` and `STC`/`CLC`/`CMC` keep their documented effects. The carry instructions leave AF and PF unchanged. Refuse `LAHF` when the most recent flag writer on any path leaves AF undefined (`AND`, `OR`, `XOR`, `TEST`, shifts and rotates, `MUL`/`IMUL`, `BSF`/`BSR` and so on) or is unknown (block entry, a call, a merge of paths with different writers). 2. **`SAHF` round trip only.** Accept `LAHF` only when the last writer is `SAHF`, so all five bits come straight from a known `AH`. This is very small, but it only covers the save/restore idiom. 3. **Full lazy flags.** Keep the last result and operands, and compute PF and AF on demand everywhere. This is more general, but it touches every flag writer and the existing `parity` gating, so I'd rather not start there. **Proposed first slice:** option 1, limited to `ADD`, `SUB`, `CMP`, `INC`, `DEC` and `SAHF` as writers, register operands at 8, 16, 32 and 64 bits, and `LAHF` in the same basic block as its writer. `ADC`, `SBB`, `NEG` and cross-block flows can follow once this is reviewed. ### Evidence so far - `parity` gating and the x87-only `parity` assignments are in `lower()` (`tools/ffx_version_query.py`), with the gate comment above them. AF has no state. - `SAHF` is lowered only in the x87 profile, where it sets `carry`, `zero`, `sign` and (when gated) `parity` from AH. Its comment notes that AF is not modelled. - Intel SDM Vol. 2A `LAHF` and Vol. 1 §3.4.3.1 define the bit layout. AF is defined for the add/subtract family and undefined after logical operations, shifts and multiplies. That's why option 1 refuses those writers instead of guessing a value. - `LAHF` is valid in 64-bit mode when CPUID.80000001H:ECX.LAHF-SAHF[bit 0] = 1. The fixture would check that bit and exit 77 without it, as the AVX fixtures do. ### Completion condition - There's a source-built MASM fixture with a native and translated comparison of the full AH byte (and the untouched AL, and the rest of RAX) after each accepted writer at each width. Inputs cover: - a carry out of bit 3 (AF) with and without a carry out of the top bit; - odd and even parity of the low byte, where the higher bytes would flip parity if they were counted; - zero and sign results; - `INC`/`DEC` leaving CF unchanged; - `SAHF`→`LAHF` with reserved bits 1, 3 and 5 set in the input `AH`. - Refusal controls: `LAHF` after `AND`, `XOR`, `TEST`, `SHL` and `IMUL`; at block entry; after a call; and after a join with different writers. - Mutated controls: AF from bit 4 instead of bit 3, parity over the full width, and bit 1 cleared. - The existing `x87_compare` rejection for `fucomi` then `lahf` either stays (if the FCOMI family is outside the slice) or moves to a still-unsupported reader. - `dev` and `release` presets pass, and `tools/form_coverage.py` no longer lists `lahf`. ### Required environment x64 Windows with MSVC, the Windows SDK, CMake/Ninja and Python 3.11+ (the standard first build). Any x64 CPU with LAHF-SAHF in 64-bit mode. I'll run it on an Intel Core i5-14600K, Windows 11 build 26300, MSVC 19.50. ### Checklist - [x] The task can be reproduced with public or source-built inputs. - [x] I did not include game files, dumps, distribution links or credentials.
R0Y00 commented 2026-11-24 06:49:45 +00:00 (Migrated from github.com)
Author
Contributor

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


Please start with same-block LAHF after register ADD, SUB or CMP with defined flags. Track AF and PF only for the initial supported writers, and require every consumed flag to be defined at the use. Keep INC/DEC separate until their preserved CF has an established incoming value. SAHF can follow with a source byte whose five flags are all known; the existing x87 status-word cases must still refuse if AH would expose unknown AF or PF.

Check the whole resulting RAX so the fixture observes that LAHF replaces AH only. Use source-built native/lifted comparisons for both AF and PF values, signed/zero/carry cases and reserved bits 5/3/1. Also check a flag consumer after LAHF to catch unintended flag writes. Add refusals after TEST, at entry and across a call or unmodeled join, and gate the native fixture on LAHF/SAHF capability. Keep the x87 and ordinary shared-lowerer suites in the Windows checks. Intel instruction manuals

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7115105042 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1022#issuecomment-7115105042). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:55:48Z. --- Please start with same-block LAHF after register ADD, SUB or CMP with defined flags. Track AF and PF only for the initial supported writers, and require every consumed flag to be defined at the use. Keep INC/DEC separate until their preserved CF has an established incoming value. SAHF can follow with a source byte whose five flags are all known; the existing x87 status-word cases must still refuse if AH would expose unknown AF or PF. Check the whole resulting RAX so the fixture observes that LAHF replaces AH only. Use source-built native/lifted comparisons for both AF and PF values, signed/zero/carry cases and reserved bits 5/3/1. Also check a flag consumer after LAHF to catch unintended flag writes. Add refusals after TEST, at entry and across a call or unmodeled join, and gate the native fixture on LAHF/SAHF capability. Keep the x87 and ordinary shared-lowerer suites in the Windows checks. [Intel instruction manuals](https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html)
zohaiblazuli commented 2026-11-24 06:49:48 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by zohaiblazuli on 2026-11-23T22:32:43Z.


Thanks. I'm starting the first slice now, as scoped above: LAHF in the same block after a register ADD, SUB or CMP, with AF and PF tracked only for those writers, and every consumed flag required to be defined at the use. INC/DEC and SAHF stay out of this slice.

The fixture will compare the whole RAX value against native code, cover both AF and PF values, the signed, zero and carry cases and reserved bits 5, 3 and 1, and include a flag consumer after LAHF. It will also check refusals after TEST, at entry, and across a call or an unmodeled join. The native run will be gated on LAHF/SAHF CPUID support, and I'll run the x87 and ordinary shared-lowerer suites in both Windows presets.

@Ammmae0x, #1031's LAHF part overlaps this. Unless you've already started this exact slice, I'll keep it here, and the PUSHF/POPF work can continue separately as suggested.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7115887229 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1022#issuecomment-7115887229). Originally posted by [zohaiblazuli](https://github.com/zohaiblazuli) on 2026-11-23T22:32:43Z. --- Thanks. I'm starting the first slice now, as scoped above: `LAHF` in the same block after a register `ADD`, `SUB` or `CMP`, with AF and PF tracked only for those writers, and every consumed flag required to be defined at the use. `INC`/`DEC` and `SAHF` stay out of this slice. The fixture will compare the whole RAX value against native code, cover both AF and PF values, the signed, zero and carry cases and reserved bits 5, 3 and 1, and include a flag consumer after `LAHF`. It will also check refusals after `TEST`, at entry, and across a call or an unmodeled join. The native run will be gated on LAHF/SAHF CPUID support, and I'll run the x87 and ordinary shared-lowerer suites in both Windows presets. @Ammmae0x, #1031's LAHF part overlaps this. Unless you've already started this exact slice, I'll keep it here, and the PUSHF/POPF work can continue separately as suggested.
R0Y00 commented 2026-11-24 06:49:49 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T22:49:46Z.


#1031 now has the bounded candidate and its full-RAX, live-flag, CPUID and refusal controls. @Ammmae0x and @zohaiblazuli, please use that PR as the one point of integration for this slice. Keep the existing native fixture and its controls, and coordinate the small shared-lowerer change there so callers use the same lowering for this instruction. An independent review of AH preservation and AF/PF definedness would help more than a second PR implementing the same forms. Keep the flag-stack forms separate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7116231546 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1022#issuecomment-7116231546). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T22:49:46Z. --- #1031 now has the bounded candidate and its full-RAX, live-flag, CPUID and refusal controls. @Ammmae0x and @zohaiblazuli, please use that PR as the one point of integration for this slice. Keep the existing native fixture and its controls, and coordinate the small shared-lowerer change there so callers use the same lowering for this instruction. An independent review of AH preservation and AF/PF definedness would help more than a second PR implementing the same forms. Keep the flag-stack forms separate.
R0Y00 commented 2026-11-24 06:49:55 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:17:42Z.


The agreed shared LAHF slice landed in #1031 (995f4e17). Windows and /O2 passed, including the native full-RAX differential and the refusal when a host operation replaces the instruction whose flags LAHF would read. Closing the agreed first slice. Flag-stack forms remain separate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7117916169 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1022#issuecomment-7117916169). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:17:42Z. --- The agreed shared LAHF slice landed in #1031 (995f4e17). Windows and /O2 passed, including the native full-RAX differential and the refusal when a host operation replaces the instruction whose flags LAHF would read. Closing the agreed first slice. Flag-stack forms remain separate.
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#1015
No description provided.