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

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

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T00:05:16Z.


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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6549553085 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/739). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T00:05:16Z. --- ### 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).
R0Y00 commented 2026-11-24 06:48:53 +00:00 (Migrated from github.com)
Author
Contributor

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


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7088355590 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/739#issuecomment-7088355590). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T00:26:49Z. --- 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 commented 2026-11-24 06:49:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T06:25:10Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7093316145 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/739#issuecomment-7093316145). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T06:25:10Z. --- 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.
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#733
No description provided.