Skip to content

Reject folding local addresses the emitter cannot encode - #130990

Merged
tannergooding merged 4 commits into
dotnet:mainfrom
tannergooding:tannergooding-fix-minopts-avx512-invalidprogram
Jul 21, 2026
Merged

tannergooding merged 4 commits into
dotnet:mainfrom
tannergooding:tannergooding-fix-minopts-avx512-invalidprogram

Conversation

@tannergooding

@tannergooding tannergooding commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Fixes #126584.

The emitter only supports byte-sized offsets (< 256) for local variable numbers >= 32768 -- see emitLclVarAddr::initLclVarAddr, which otherwise hits IMPL_LIMITATION("JIT doesn't support offsets larger than 255 into valuetypes for local vars > 32767"). Compiler::IsValidLclAddr already encodes the other emitter limits (16-bit offset, in-bounds) but didn't account for this one.

As a result, a struct block-init of a local numbered >= 32768 whose access reaches offset >= 256 could be contained/folded into the stack (S_R) addressing form by ContainBlockStoreAddress, and then fail at emit time. Since the fold only happens under MinOpts here, it surfaced as an InvalidProgramException in Debug but not Release. The IL is valid (ILVerify agrees); the JIT was simply generating an address form the emitter can't encode.

The fix rejects that case in IsValidLclAddr. Returning false there declines only the fold -- lclmorph then keeps the access as an explicit address computation and codegen materializes it into a register (the ARX form, which uses a real base register + 32-bit displacement and has no varNum-encoding limit). This also covers direct LCL_FLD accesses at offset >= 256 for such locals, not just the block-init path.

The constants 32768 and 256 mirror emitLclVarAddr::initLclVarAddr exactly.

fgOptimizeAddition folds ADD(LCL_ADDR, const) into a single LCL_ADDR. That is a FullOpts-only sibling of the same problem: it previously validated only the addend, so repeated folds could accumulate an offset the emitter can't encode. It now validates the resulting offset through IsValidLclAddr, with the addend bounded to [0, UINT16_MAX] first so the sum is computed without signed overflow.


Added a regression test (Runtime_126584). It uses Reflection.Emit to build a MinOpts method with >32768 IL locals plus a >= 256-byte struct block-init, which is the minimal shape that reproduces the emitter limit without carrying a ~1.5MB source file. It is gated on IsReflectionEmitSupported, so it skips on NativeAOT and other no-dynamic-code scenarios. Harness-validated both directions: it fails against the baseline JIT (InvalidProgramException in M0) and passes with the fix.

The repro is CPU-independent -- despite the issue title, the bug isn't AVX-512-specific; those ISAs just perturbed the local numbering/codegen enough to expose it.

Note

This PR was authored with the assistance of GitHub Copilot.

The emitter only supports byte-sized offsets for locals numbered 32768 or
greater (see emitLclVarAddr::initLclVarAddr). IsValidLclAddr did not account
for this, so a struct block-init at such a local with an offset >= 256 could
be folded into the stack addressing form and hit an IMPL_LIMITATION, surfacing
as an InvalidProgramException under MinOpts.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 18:48
@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 Jul 17, 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.

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 CoreCLR JIT’s Compiler::IsValidLclAddr to account for an additional emitter encoding constraint: when a local number is >= 32768, the emitter can only encode byte-sized offsets (< 256). The intent is to prevent IR forms like LCL_ADDR / LCL_FLD from being produced in cases the emitter can’t encode, avoiding emit-time failures.

Changes:

  • Extend IsValidLclAddr to reject (lclNum >= 32768 && offset >= 256).
  • Update the function’s header comment to document the emitter’s large-varnum offset encoding restriction.
Show a summary per file
File Description
src/coreclr/jit/compiler.hpp Adds an IsValidLclAddr guard for the emitter’s “large varnum => byte offset only” encoding limitation and documents it.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Comment thread src/coreclr/jit/compiler.hpp Resolved
Comment thread src/coreclr/jit/compiler.hpp Resolved
tannergooding and others added 2 commits July 17, 2026 12:04
fgOptimizeAddition folds ADD(LCL_ADDR(off), const) into LCL_ADDR(off+const)
but validated only the addend against IsValidLclAddr, not the resulting
offset. Repeated folds (or a fold onto an already-offset LCL_ADDR) could
accumulate an offset the emitter cannot encode. Validate the computed offset
instead.

Co-authored-by: Copilot App <[email protected]>
Builds a method with more than 32768 locals via Reflection.Emit so the struct
local's number crosses the emitter's large-varnum boundary, then block-inits it
in-body reaching an offset >= 256 under MinOpts. Skips where dynamic code isn't
supported.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 19:35

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.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment thread src/coreclr/jit/morph.cpp Resolved
Bound the addend to [0, UINT16_MAX] before adding it to the existing
offset so the sum is computed in an unsigned domain that cannot overflow.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 20:28

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.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new

@tannergooding

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib. This one is a also bug fix, but its also a low priority one given the rarity of compiling a method with over 32k locals.

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "b84d42c3439e7ad56d73f17346e8fa2145aae005",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "f94898a9b55df07348434e86915c7405962427b6",
  "last_reviewed_commit": "b84d42c3439e7ad56d73f17346e8fa2145aae005",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "f94898a9b55df07348434e86915c7405962427b6",
  "last_recorded_worker_run_id": "29688137429",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "b84d42c3439e7ad56d73f17346e8fa2145aae005",
      "review_id": 4730810857
    }
  ]
}

@github-actions github-actions Bot 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.

Holistic Review

Motivation: Justified. This fixes a real bug (#126584): a struct block-init of a local numbered >= 32768 reaching offset >= 256 could be folded into the stack (S_R) addressing form, then hit IMPL_LIMITATION at emit time (surfacing as InvalidProgramException in Debug/MinOpts). ILVerify agrees the IL is valid.

Approach: Sound and minimal. Compiler::IsValidLclAddr already encoded the emitter's other encoding limits (16-bit offset, in-bounds); this adds the missing emitLclVarAddr::initLclVarAddr constraint (lclNum >= 32768 && offset >= 256). The constants exactly mirror the LVA_LARGE_VARNUM encoding path in emit.cpp (offset stored in 8 bits at _lvaExtra bits 14..7). Returning false declines only the fold, so lclmorph keeps the access as an explicit address computation (ARX form, 32-bit displacement, no varNum-encoding limit). The fgOptimizeAddition change is a correct companion: validating the resulting offset instead of just the addend prevents repeated folds from accumulating an unencodable offset. The overflow reasoning holds -- consVal is guarded by FitsIn<uint16_t> and GetLclOffs() returns a 16-bit value, so the unsigned sum (max 131070) cannot overflow, and FitsIn<uint16_t>(newOffset) plus IsValidLclAddr (which requires offset < UINT16_MAX) keep the two paths consistent.

Summary: LGTM. The fix is precise, mirrors the emitter's encoding limits exactly, and is conservative (it only declines an optimization, falling back to a correct address form). The Runtime_126584 regression test reproduces the minimal shape via Reflection.Emit (>32768 locals + >= 256-byte block-init under DOTNET_JITMinOpts), is gated on IsReflectionEmitSupported, and the author reports it fails against baseline and passes with the fix. No actionable findings.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 76.5 AIC · ⌖ 14.6 AIC · ⊞ 10K

@JulieLeeMSFT JulieLeeMSFT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with nit.

Comment thread src/coreclr/jit/compiler.hpp Resolved
@tannergooding

Copy link
Copy Markdown
Member Author

/ba-g unrelated libraryimport generator test failure

@tannergooding
tannergooding merged commit 842d3a1 into dotnet:main Jul 21, 2026
144 of 146 checks passed
@tannergooding
tannergooding deleted the tannergooding-fix-minopts-avx512-invalidprogram branch July 21, 2026 00:46
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

InvalidProgramException in Debug build only (SIMD/AVX-512 intrinsics, CPU-dependent)

3 participants