Repository navigation
[Crossgen2] Omit constrained type from imports of resolved constrained calls - #138262
Open
jtschuster wants to merge 6 commits into
Open
jtschuster wants to merge 6 commits into
jtschuster wants to merge 6 commits into
Conversation
When crossgen2 resolves a constrained call at compile time to a method body in the version bubble, encode the call target import with that method's own token and no constrained type. The runtime cannot resolve the call differently, and decoding the constraint eagerly loaded the exact constrained type and its instantiation arguments (for example Enumerator<Marker> for a foreach Dispose call), which could load application assemblies during early startup on Wasm. Fixes dotnet#138199 Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/crossgen-contrib |
Open
3 tasks
…onstrainedType Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
…calls MethodDictionary and DeclaringTypeHandle instantiation-argument lookups for a constrained call resolved at compile time now name the resolved method by its own token, like the call's entrypoint import. Assert in MethodFixupSignature that re-tokenized methods never carry a constraint. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
jtschuster
marked this pull request as ready for review
November 25, 2026 23:43
jtschuster
requested review from
AndyAyersMS,
davidwrighton and
jkotas
and removed request for
davidwrighton
November 25, 2026 23:43
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The test supplies CoreLib only as a reference, so it never exercises deferred Wasm calls and would miss regressions specific to MethodEntry_ReadyToRun imports.
1 open issue
What changed in this PR
This PR updates Crossgen2 to omit unnecessary constrained-type information when a constrained call resolves to a method body within the version bubble, avoiding assembly loads caused by decoding ReadyToRun imports.
Changes:
- Centralizes call-target token construction for entrypoint and generic-context imports.
- Adds assertions against retaining constrained types when re-tokenizing resolved methods.
- Adds regression fixtures and checks for entrypoint and method-dictionary signatures.
| File | Description |
|---|---|
src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs |
Builds constraint-free tokens for eligible resolved call targets. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/MethodFixupSignature.cs |
Asserts constrained types are absent during re-tokenization. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.cs |
Checks emitted imports for unnecessary constrained-type information. |
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/ConstrainedCalls/ResolvedConstrainedCall.cs |
Adds enumerator and generic-method constrained-call fixtures. |
Review effort: Balanced
Comment on lines
+627
to
+628
| Options = [Crossgen2Option.Optimize], | ||
| AdditionalArgs = ["--codegenopt", "JitNoInline=1"], |
Cover a shared target instantiated over an external reference type (the constraint is omitted), and an unshared value-type instantiation and generic method dictionary over external types (the exact instantiation is kept). Also check that the shared body's exact instantiation is still supplied by its TypeDictionary import. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
The existing test supplies CoreLib only as a reference, so deferred Wasm direct calls are disabled and its MethodEntry_ReadyToRun readiness fixups are never emitted. Add a Wasm composite variant with CoreLib as an unrooted input that requires a constraint-free MethodEntry_ReadyToRun import for Enumerator`1.Dispose and an ordinary MethodEntry import for the NoInlining control. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This branch has not been deployed
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.

When resolving a constrained method call in CorInfoImpl, we may resolve a constrained call to an exact method body. If that method is within the version bubble, we can use the resolved method's token without the constrained type information as the MethodWithToken which is used to create ReadyToRun MethodSignatures. We already modify the token to be the resolved method's token in
MethodFixupSignature.GetData(), but wouldn't remove the constrained type from the signature. This led to the linked issue where eager resolution of READYTORUN_METHOD_ENTRY fixups for direct-calling resolved constrained calls would trigger assembly loading of the constrained type.This PR clears the constrained type property on a MethodWithToken when a constrained call is resolved to an exact method body.
Fixes #138199
AI-generated description with additional details below.
As suggested in #138199 (comment): when Crossgen2 resolves a constrained call to a specific method body, the method-entry imports for that call no longer need the constrained type.
Before this change,
MethodFixupSignatureswapped the interface MemberRef for the resolved MethodDef but still encoded the exact constrained owner (e.g.Enumerator`1<Marker>). Decoding the import at runtime then eagerly loadedMarker's assembly.Changes
CorInfoImpl.ReadyToRun.cs:ceeInfoGetCallInfonow reportsresolvedConstraint. A newComputeCallTargetMethodWithTokenhelper builds theMethodWithTokenfrom the resolved method's own token, with no constrained type, when all of the following hold:Otherwise it falls back to
ComputeMethodWithToken. BothCORINFO_CALLentrypoint paths ingetCallInfouse it: the deferred Wasm path and the regularMethodEntrypointpath.The
MethodDictionaryandDeclaringTypeHandleinstantiation-argument lookups for the call use the same helper. Previously, e.g.constrained. GenericMethodStruct callvirt IGenericMethod::Invoke<string>produced aMethodDictionaryimport ofGenericMethodStruct.Invoke<string> @ GenericMethodStruct.MethodFixupSignature.GetDatanow asserts that a method whose token it re-tokenizes never carries a constrained type.New regression test
ResolvedConstrainedCallImportsOmitConstrainedTypein ILCompiler.ReadyToRun.Tests covers both the entrypoint andMethodDictionaryimports. It fails without the fix. It also covers instantiations over types from a reference assembly outside the version bubble:Enumerator`1<ExternalMarker>) omits the constraintEnumerator`1<ExternalValue>) and theGenericMethodStruct.Invoke<ExternalMarker>dictionary keep their exact instantiationTypeDictionaryimportTesting
ILCompiler.ReadyToRun.Tests, no failures:
Debug Crossgen2 (with the new assert) ran both ILCompiler.ReadyToRun.Tests suites and compiled CoreLib (x64 and wasm) and a large-bubble composite of CoreLib, Linq, Collections, Immutable, Json and Xml, with no asserts.
src/tests with
--runcrossgen2tests(linux-x64) passed:readytorunLoader(includingclassloader/StaticVirtualMethodsandclassloader/DefaultInterfaceMethods)JIT.Generics_roThe only two failures came from the local environment:
R2RTestandhostpolicyweren't built.Size impact (14 framework libraries, non-composite): x64 unchanged; wasm -224 bytes (-0.001%), with 19 of 191 constrained method-entry imports removed. CoreLib has no exact-type constrained imports, so it is unchanged.