Skip to content

Fix x64 instruction size estimates - #137880

Merged
AndyAyersMS merged 10 commits into
dotnet:mainfrom
AndyAyersMS:andy/fix-x64-instruction-size-estimates
Nov 24, 2026
Merged

AndyAyersMS merged 10 commits into
dotnet:mainfrom
AndyAyersMS:andy/fix-x64-instruction-size-estimates

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Require precise estimation instead of tolerating some over-estimates.

AndyAyersMS and others added 7 commits June 25, 2026 14:29
Correct xarch size accounting for VEX/K encodings and add an opt-in debug check.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Make the emit-size check exact by default and cover the remaining xarch estimator gaps.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use the actual opcode prefix state when adjusting K-instruction size estimates so EVEX stress and VEX output stay consistent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Correct x86 K-instruction prefix accounting and memory-source IMUL estimates exposed by checked CI runs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 19, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@tannergooding have been sitting on this for a while but think we should take it. Code changes don't look too bad.

fyi @dotnet/jit-contrib

diffs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Incorrect APX instruction-size estimates for 64-bit IMUL and 16-bit cmpxchg cause valid instructions to fail size checks.

2 open issues
What changed in this PR

This PR tightens instruction-size accounting in the CoreCLR xarch JIT emitter, aiming for exact estimates rather than tolerating overestimates.

Changes:

  • Adjusts opcode, prefix, and masked-blend size calculations.
  • Strengthens validation of compressed stack displacements.
  • Adds a default-enabled debug assertion for estimated versus emitted instruction sizes, with exceptions for intentionally variable-size instructions.
File Description
src/​coreclr/​jit/​jitconfigvalues.h Adds the configuration switch for instruction-size mismatch assertions.
src/​coreclr/​jit/​emitxarch.cpp Updates size estimates and adds stricter emission checks.

Review effort: Balanced

Comment thread src/coreclr/jit/emitxarch.cpp Resolved
Comment thread src/coreclr/jit/emitxarch.cpp Resolved
Comment thread src/coreclr/jit/emitxarch.cpp Resolved
Comment thread src/coreclr/jit/emitxarch.cpp Resolved
@tannergooding

Copy link
Copy Markdown
Member

Changes otherwise LGTM. Looks like copilot found the same issues I did.

@AndyAyersMS

AndyAyersMS commented Nov 24, 2026 •

Copy link
Copy Markdown
Member Author

Local runs with various ISA stress modes found a few more things. I'll address them all with one upcoming commit.

From review:

  1. 16-bit cmpxchg: stack and static-field forms were sized one byte short.
  2. Extended-register imul: the built-in REX byte was double-counted under REX2/EVEX.

New, found by the APX stress runs:

  1. 16-bit shld/shrd: missing 0x66, which produces the wrong instruction.
  2. 16-bit memory xadd (and similar instructions): missing 0x66, also the wrong instruction. Every emitter path and the size estimator now share one rule for when to emit 0x66.
  3. vmovd xmm, r16d without EVEX: wrong code. The register allocator handed R16 to a VEX-only instruction, which silently encoded eax. The fix is in lsrabuild.cpp.
  4. or/and/sub/xor eax, imm8 under promoted-EVEX stress: size overestimated.
  5. Indirect helper call [..] under REX2 stress: size overestimated.

Address review feedback and issues found by APX SuperPMI stress:

- Count the 0x66 prefix for 16-bit cmpxchg consistently, and share one
  rule (NeedsLegacyOperandSizePrefix) between the estimator and the
  emitters. This also fixes 16-bit shld/shrd and memory xadd, which
  previously omitted the 0x66 prefix.
- Fix extended-register 3-op imul under REX2/EVEX, where the built-in
  REX byte was double-counted.
- Only add the EVEX MI-form byte when the accumulator form was assumed.
- Don't count a REX2 prefix for IF_METHPTR calls; it is never emitted.
- Subtract the empty prefix slot for no-prefix 0F38/0F3A SIMD opcodes
  (AVX10v1 FP16, AVX-VNNI-INT).
- LSRA: restrict contained int<->float BITCAST stores to low GPRs, as
  for standalone BITCAST; VEX vmovd cannot encode EGPRs.
- Add APX emitter unit test cases for the above.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AndyAyersMS
AndyAyersMS merged commit 57d8090 into dotnet:main Nov 24, 2026
132 of 133 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants