Simplify branching in ILImporter.Scanner.cs with Debug.Assert#123235
Merged
MichalStrehovsky merged 2 commits intomainfrom Jan 19, 2026
Merged
Simplify branching in ILImporter.Scanner.cs with Debug.Assert#123235MichalStrehovsky merged 2 commits intomainfrom
MichalStrehovsky merged 2 commits intomainfrom
Conversation
…romThis() Co-authored-by: MichalStrehovsky <[email protected]>
Copilot
AI
changed the title
[WIP] Refactor ILImporter.Scanner.cs branching logic
Simplify branching in ILImporter.Scanner.cs with Debug.Assert
Jan 15, 2026
MichalStrehovsky
approved these changes
Jan 16, 2026
Member
|
@sbomer could you have a look please? I wasn't sure about this one in your PR so I didn't bring it up, but I just made copilot try it and it works. |
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request simplifies conditional branching logic in the ILCompiler scanner by replacing an explicit else if (targetMethod.AcquiresInstMethodTableFromThis()) check with a plain else block containing a Debug.Assert. This tightens the invariant that shared generic methods acquire their generic context through exactly one of three mutually exclusive paths.
Changes:
- Replaced explicit conditional check with assertion to enforce mutually exclusive invariant for generic context acquisition methods
- Aligns code pattern with existing implementations in
Compilation.csandCorInfoImpl.RyuJit.cs
rosebyte
pushed a commit
that referenced
this pull request
Jan 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replace explicit
else if (targetMethod.AcquiresInstMethodTableFromThis())withelse { Debug.Assert(...); }to tighten the invariant that shared generic methods acquire context via exactly one of three mutually exclusive paths: method dictionary, type dictionary, orthispointer.Changes
File:
src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.csIn the
directCallpath withinexactContextNeedsRuntimeLookup:Aligns with existing pattern in
CorInfoImpl.GetGenericRuntimeLookupKind. No functional behavior change.Testing
Original prompt
Goal: Simplify branching in ILImporter.Scanner.cs by replacing the explicit
else if (targetMethod.AcquiresInstMethodTableFromThis())with a plainelsethat begins withDebug.Assert(targetMethod.AcquiresInstMethodTableFromThis()). This matches the invariant that, in shared generic code, if a method does not require a method dictionary or a type (method table) dictionary argument, it must acquire the generic context fromthis.Background:
RequiresInstMethodDescArg()for method dictionaryRequiresInstMethodTableArg()for type/method table dictionaryAcquiresInstMethodTableFromThis()forthispointerMethodDesccomments) and mirrored in managedTypeExtensions. Other places in the codebase (e.g.,CorInfoImpl.GetGenericRuntimeLookupKind) already use theelse + Debug.Assert(...)pattern for this invariant.Change scope:
directCallpath, inside theexactContextNeedsRuntimeLookupbranch, under the conditionif (targetMethod.IsSharedByGenericInstantiations && !resolvedConstraint && !referencingArrayAddressMethod).else if (targetMethod.AcquiresInstMethodTableFromThis()) { _dependencies.Add(_factory.ShadowNonConcreteMethod(concreteMethod), reason); }with:
else { Debug.Assert(targetMethod.AcquiresInstMethodTableFromThis()); _dependencies.Add(_factory.ShadowNonConcreteMethod(concreteMethod), reason); }Rationale:
CorInfoImpl.GetGenericRuntimeLookupKind).Exact edit (before/after):
Before:
After: