Repository navigation
Reject folding local addresses the emitter cannot encode - #130990
tannergooding merged 4 commits into
Conversation
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]>
|
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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
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
IsValidLclAddrto 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
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]>
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]>
|
@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. |
|
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
}
]
} |
There was a problem hiding this comment.
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
|
/ba-g unrelated libraryimport generator test failure |
Fixes #126584.
The emitter only supports byte-sized offsets (
< 256) for local variable numbers>= 32768-- seeemitLclVarAddr::initLclVarAddr, which otherwise hitsIMPL_LIMITATION("JIT doesn't support offsets larger than 255 into valuetypes for local vars > 32767").Compiler::IsValidLclAddralready 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
>= 32768whose access reaches offset>= 256could be contained/folded into the stack (S_R) addressing form byContainBlockStoreAddress, and then fail at emit time. Since the fold only happens under MinOpts here, it surfaced as anInvalidProgramExceptionin 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. Returningfalsethere declines only the fold --lclmorphthen keeps the access as an explicit address computation and codegen materializes it into a register (theARXform, which uses a real base register + 32-bit displacement and has no varNum-encoding limit). This also covers directLCL_FLDaccesses at offset>= 256for such locals, not just the block-init path.The constants
32768and256mirroremitLclVarAddr::initLclVarAddrexactly.fgOptimizeAdditionfoldsADD(LCL_ADDR, const)into a singleLCL_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 throughIsValidLclAddr, with the addend bounded to[0, UINT16_MAX]first so the sum is computed without signed overflow.Added a regression test (
Runtime_126584). It usesReflection.Emitto 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 onIsReflectionEmitSupported, so it skips on NativeAOT and other no-dynamic-code scenarios. Harness-validated both directions: it fails against the baseline JIT (InvalidProgramExceptioninM0) 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.