cpu: lower RDTSC and RDTSCP in the shared lowering #739

Closed
opened 2026-11-26 22:15:13 +00:00 by github-bot · 2 comments
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-23 00:05 UTC · original

Claim and design question: lower RDTSC and RDTSCP in the shared lowering

docs/coverage/x64_zen2.tsv lists rdtsc and rdtscp in the Integer group, and on main at fd2486f neither has a passing fixture (tools/form_coverage.py --gaps x64_zen2). lower() in tools/ffx_version_query.py rejects both today. I found no open issue or PR that covers them. I'd like to take them as one small PR.

Proposed behavior

  • RDTSC reads the host time-stamp counter (__rdtsc()) and writes the low 32 bits to EAX and the high 32 bits to EDX, zero-extending RAX and RDX. Flags are unchanged.
  • RDTSCP does the same through __rdtscp(&aux) and also writes aux (IA32_TSC_AUX) to ECX, zero-extending RCX.
  • Both reads are volatile, so /O2 cannot merge or drop them.

Proposed fixture (source-built MASM, differential)

Because the value is non-deterministic, the check compares properties rather than bytes:

  • The upper halves of RAX, RDX and RCX are zero.
  • On one thread, native_before <= translated <= native_after (bracketing reads in the harness).
  • Two back-to-back translated reads are non-decreasing and not merged at /O2.
  • RDTSCP's ECX equals the native RDTSCP ECX on the same thread.
  • Negative controls: a mutated translation that returns a constant, and one that swaps EAX/EDX, must fail. A CPUID gate (RDTSCP bit) returns 77 on hosts without it.

Question before I start

The translation would pass the host TSC through unscaled. Guest code that converts ticks to time with its own fixed frequency would then run at the host's rate. Is passing the host value through the intended contract, with timing calibration left to the runtime layer? Or should the lowering call a runtime hook, so the shim can scale or virtualize the counter later? I'd keep the first PR to the pass-through, and document the frequency question in docs/experiments.md.

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

AI-assisted (Claude Code).

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-23 00:05 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/739) ### Claim and design question: lower `RDTSC` and `RDTSCP` in the shared lowering `docs/coverage/x64_zen2.tsv` lists `rdtsc` and `rdtscp` in the Integer group, and on `main` at `fd2486f` neither has a passing fixture (`tools/form_coverage.py --gaps x64_zen2`). `lower()` in `tools/ffx_version_query.py` rejects both today. I found no open issue or PR that covers them. I'd like to take them as one small PR. **Proposed behavior** - `RDTSC` reads the host time-stamp counter (`__rdtsc()`) and writes the low 32 bits to EAX and the high 32 bits to EDX, zero-extending RAX and RDX. Flags are unchanged. - `RDTSCP` does the same through `__rdtscp(&aux)` and also writes `aux` (IA32_TSC_AUX) to ECX, zero-extending RCX. - Both reads are volatile, so `/O2` cannot merge or drop them. **Proposed fixture (source-built MASM, differential)** Because the value is non-deterministic, the check compares properties rather than bytes: - The upper halves of RAX, RDX and RCX are zero. - On one thread, `native_before <= translated <= native_after` (bracketing reads in the harness). - Two back-to-back translated reads are non-decreasing and not merged at `/O2`. - `RDTSCP`'s ECX equals the native `RDTSCP` ECX on the same thread. - **Negative controls:** a mutated translation that returns a constant, and one that swaps EAX/EDX, must fail. A CPUID gate (`RDTSCP` bit) returns 77 on hosts without it. **Question before I start** The translation would pass the **host** TSC through unscaled. Guest code that converts ticks to time with its own fixed frequency would then run at the host's rate. Is passing the host value through the intended contract, with timing calibration left to the runtime layer? Or should the lowering call a runtime hook, so the shim can scale or virtualize the counter later? I'd keep the first PR to the pass-through, and document the frequency question in `docs/experiments.md`. Test hosts: Intel Skylake (i7-6820HQ) on Windows 11 26100. AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:15:13 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 00:26 UTC · original

The first PR can use the unscaled host TSC as an explicit profile. Document that it supplies host ticks and host TSC_AUX, with no guest frequency or virtual-CPU guarantee. Keep each read observable even when its result is unused, and preserve the distinct RDTSC/RDTSCP instruction ordering rather than adding fences to the generated code. Use serialized measurement boundaries in the harness and pin the test thread or detect migration before comparing TSC_AUX. Keep the feature skip specific to RDTSCP, so RDTSC checks still run on those hosts.

**@⁠R0Y00** on GitHub · 2026-11-23 00:26 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/739#issuecomment-7088355590) The first PR can use the unscaled host TSC as an explicit profile. Document that it supplies host ticks and host TSC_AUX, with no guest frequency or virtual-CPU guarantee. Keep each read observable even when its result is unused, and preserve the distinct RDTSC/RDTSCP instruction ordering rather than adding fences to the generated code. Use serialized measurement boundaries in the harness and pin the test thread or detect migration before comparing TSC_AUX. Keep the feature skip specific to RDTSCP, so RDTSC checks still run on those hosts.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 06:25 UTC · original

Implemented in #789 (2056156). The explicit host TSC profile preserves both read forms, including a read whose result is discarded at /O2. Native bracketing runs on a pinned thread and RDTSCP has its own capability check. The profile reports host ticks and TSC_AUX.

**@⁠R0Y00** on GitHub · 2026-11-23 06:25 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/739#issuecomment-7093316145) Implemented in #789 (2056156). The explicit host TSC profile preserves both read forms, including a read whose result is discarded at /O2. Native bracketing runs on a pinned thread and RDTSCP has its own capability check. The profile reports host ticks and TSC_AUX.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/R0Y00--Xbox2Windows#739
No description provided.