Skip to content

[Crossgen2] Omit constrained type from imports of resolved constrained calls - #138262

Open
jtschuster wants to merge 6 commits into
dotnet:mainfrom
jtschuster:wasm-r2r-resolved-constraint-imports
Open

jtschuster wants to merge 6 commits into
dotnet:mainfrom
jtschuster:wasm-r2r-resolved-constraint-imports

Conversation

@jtschuster

@jtschuster jtschuster commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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, MethodFixupSignature swapped 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 loaded Marker's assembly.

Changes

  • CorInfoImpl.ReadyToRun.cs: ceeInfoGetCallInfo now reports resolvedConstraint. A new ComputeCallTargetMethodWithToken helper builds the MethodWithToken from the resolved method's own token, with no constrained type, when all of the following hold:

    • the constraint was resolved
    • the call is not unboxing and doesn't use an instantiating stub
    • the method is a primary, non-abstract method desc that versions with the method body

    Otherwise it falls back to ComputeMethodWithToken. Both CORINFO_CALL entrypoint paths in getCallInfo use it: the deferred Wasm path and the regular MethodEntrypoint path.

  • The MethodDictionary and DeclaringTypeHandle instantiation-argument lookups for the call use the same helper. Previously, e.g. constrained. GenericMethodStruct callvirt IGenericMethod::Invoke<string> produced a MethodDictionary import of GenericMethodStruct.Invoke<string> @ GenericMethodStruct.

  • MethodFixupSignature.GetData now asserts that a method whose token it re-tokenizes never carries a constrained type.

  • New regression test ResolvedConstrainedCallImportsOmitConstrainedType in ILCompiler.ReadyToRun.Tests covers both the entrypoint and MethodDictionary imports. It fails without the fix. It also covers instantiations over types from a reference assembly outside the version bubble:

    • a shared target over an external reference type (Enumerator`1<ExternalMarker>) omits the constraint
    • an unshared value-type instantiation (Enumerator`1<ExternalValue>) and the GenericMethodStruct.Invoke<ExternalMarker> dictionary keep their exact instantiation
    • the shared body's exact instantiation is still supplied by its TypeDictionary import

Testing

  • ILCompiler.ReadyToRun.Tests, no failures:

    • browser-wasm: 398 passed, 43 skipped
    • linux-x64: 289 passed, 21 skipped
  • 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:

    • readytorun
    • Loader (including classloader/StaticVirtualMethods and classloader/DefaultInterfaceMethods)
    • JIT.Generics_ro

    The only two failures came from the local environment: R2RTest and hostpolicy weren'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.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/crossgen-contrib
See info in area-owners.md if you want to be subscribed.

jtschuster and others added 2 commits November 25, 2026 18:19
…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
jtschuster marked this pull request as ready for review November 25, 2026 23:43
@jtschuster
jtschuster requested review from AndyAyersMS, davidwrighton and jkotas and removed request for davidwrighton November 25, 2026 23:43
@jkotas
jkotas requested a balanced review from Copilot November 26, 2026 00:26

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.

🟡 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"],
jtschuster and others added 2 commits November 26, 2026 01:05
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[browser][CoreCLR] Key Wasm R2R method-entry imports by shared body

2 participants