Skip to content

Fix xarch instruction size estimates#129877

Draft
AndyAyersMS wants to merge 5 commits into
dotnet:mainfrom
AndyAyersMS:andy/fix-x64-instruction-size-estimates
Draft

Fix xarch instruction size estimates#129877
AndyAyersMS wants to merge 5 commits into
dotnet:mainfrom
AndyAyersMS:andy/fix-x64-instruction-size-estimates

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Summary

  • Correct xarch instruction size estimates for VEX/EVEX prefix accounting, K-register moves, CRC32, 16-bit cmpxchg, direct-memory unary ops, IMUL forms, and AVX512 blend-mask operands.
  • Add a DEBUG-only emit-size check that is exact by default, with known x86 stack-memory formats excluded because their size depends on final stack depth.

Validation

  • build.cmd -subset clr.jit -c Checked
  • build.cmd -subset clr.jit -arch x86 -c Checked
  • Windows x64 Checked SuperPMI replay: clean
  • Windows x64 Checked SuperPMI asmdiffs/metricdiff
  • x86 crossgen2 System.Private.CoreLib repro command

Note

This PR description was generated by GitHub Copilot.

AndyAyersMS and others added 2 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>
Copilot AI review requested due to automatic review settings June 26, 2026 01:45
@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 Jun 26, 2026
@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.

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.

Pull request overview

This PR updates the xarch emitter’s instruction-size estimation logic to more accurately account for encoding details (prefixes/opcode forms) and adds a DEBUG-only sanity check to assert when the emitted size differs from the estimated size.

Changes:

  • Add a new DEBUG-only JIT config switch to enable/disable an emit-size mismatch assertion.
  • Refine xarch size estimation in multiple paths (VEX/EVEX prefix accounting, certain opcode forms, K-reg moves, etc.).
  • Add a DEBUG-only emit-time verification in emitOutputInstr to compare estimated vs. actual encoded byte count (with skips for known intentionally-variable cases).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/jitconfigvalues.h Adds JitAssertOnEmitSizeMismatch debug config switch.
src/coreclr/jit/emitxarch.cpp Adjusts instruction size estimation for various xarch encodings and adds DEBUG-only emit-size mismatch checking.

Comment thread src/coreclr/jit/emitxarch.cpp Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr jitstress-isas-avx512, runtime-coreclr jitstress-isas-x86

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 26, 2026 14:38
@AndyAyersMS

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr jitstress-isas-avx512, runtime-coreclr jitstress-isas-x86

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

// RyuJIT where possible
CONFIG_INTEGER(JitAssertOnMaxRAPasses, "JitAssertOnMaxRAPasses", 0)
CONFIG_INTEGER(JitBreakEmitOutputInstr, "JitBreakEmitOutputInstr", -1)
// Assert if the xarch encoder emits a different number of bytes than its estimator predicted; set to 0 to disable.
Comment on lines +20223 to +20233
// Labels and align pseudo-instructions can intentionally shrink after initial size estimation.
const IS_INFO insFmtInfo = emitGetSchedInfo(insFmt);
#if !FEATURE_FIXED_OUT_ARGS
const bool skipStackSizeCheck = ((insFmtInfo & (IS_SF_RD | IS_SF_RW | IS_SF_WR)) != 0);
#else
const bool skipStackSizeCheck = false;
#endif
const bool skipSizeCheck = (id->idIns() == INS_align) || (insFmt == IF_LABEL) || (insFmt == IF_RWR_LABEL) ||
(insFmt == IF_SWR_LABEL) || skipStackSizeCheck;
const UNATIVE_OFFSET actualCodeSize = static_cast<UNATIVE_OFFSET>(dst - *dp);
if (!skipSizeCheck && (id->idCodeSize() != actualCodeSize))
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>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr jitstress-isas-avx512, runtime-coreclr jitstress-isas-x86

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

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.

2 participants