Skip to content

JIT: Pass constrained type context to resolved shared generic calls - #137990

Open
NinoFloris wants to merge 4 commits into
dotnet:mainfrom
NinoFloris:abstraction-tax/constrained-call-context
Open

NinoFloris wants to merge 4 commits into
dotnet:mainfrom
NinoFloris:abstraction-tax/constrained-call-context

Conversation

@NinoFloris

Copy link
Copy Markdown
Contributor

Allow direct calls to resolved non-generic methods on the constrained value type by passing its type context explicitly. Teach the JIT and interpreter to obtain that context from the constrained token, preserving runtime lookup for unsupported or ambiguous targets.

Add functional and codegen regressions covering inlining, context-dependent implementations, mutation, initialization, delegates, generic methods, and return shapes.

@NinoFloris NinoFloris changed the title Pass constrained type context to resolved shared generic calls JIT: Pass constrained type context to resolved shared generic calls Nov 20, 2026
@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 Nov 20, 2026
@NinoFloris

Copy link
Copy Markdown
Contributor Author

@MihuBot

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 20, 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.

@NinoFloris

Copy link
Copy Markdown
Contributor Author

@MihuBot -nuget

@MihaZupan

Copy link
Copy Markdown
Member

@MihuBot -nuget

(nuget support was off for a little bit)

@NinoFloris

Copy link
Copy Markdown
Contributor Author

@jakobbotsch this has hits all over the place. Regressions and improvements are all inlining related.

Regressions:
 18426 : System.Collections.Immutable.dasm (0.78 % of base)
  6397 : protobuf-net.Core.dasm (0.62 % of base)
   182 : SharpYaml.dasm (0.02 % of base)
   133 : Microsoft.Extensions.Logging.Abstractions.dasm (0.16 % of base)
    32 : OpenTelemetry.dasm (0.01 % of base)

Improvements:
 -3786 : EntityFramework.dasm (-0.05 % of base)
 -3764 : Microsoft.CodeAnalysis.AnalyzerUtilities.dasm (-0.07 % of base)
 -2972 : FluentValidation.dasm (-0.27 % of base)
 -2615 : MoreLinq.dasm (-0.20 % of base)
 -2091 : CommunityToolkit.Mvvm.dasm (-0.65 % of base)
 -1574 : MongoDB.Driver.dasm (-0.01 % of base)
 -1569 : System.Collections.dasm (-0.22 % of base)
 -1208 : System.Data.Common.dasm (-0.07 % of base)
 -1156 : NLog.dasm (-0.10 % of base)
  -938 : System.Reactive.dasm (-0.02 % of base)
  -864 : Microsoft.EntityFrameworkCore.dasm (-0.02 % of base)
  -787 : MessagePack.dasm (-0.07 % of base)
  -769 : NuGet.DependencyResolver.Core.dasm (-0.27 % of base)
  -764 : Azure.Data.AppConfiguration.dasm (-0.06 % of base)
  -752 : Castle.Core.dasm (-0.14 % of base)
  -712 : nunit.framework.dasm (-0.05 % of base)
  -637 : Bogus.dasm (-0.20 % of base)
  -595 : NuGet.Packaging.dasm (-0.07 % of base)
  -576 : System.Threading.Tasks.Dataflow.dasm (-0.05 % of base)
  -576 : Microsoft.EntityFrameworkCore.Proxies.dasm (-0.16 % of base)
  -569 : NSwag.Core.dasm (-0.55 % of base)
  -560 : System.Threading.RateLimiting.dasm (-0.37 % of base)
  -534 : NuGet.Configuration.dasm (-0.24 % of base)
  -534 : NuGet.ProjectModel.dasm (-0.15 % of base)
  -534 : NuGet.LibraryModel.dasm (-0.54 % of base)
  -534 : NuGet.Protocol.dasm (-0.04 % of base)
  -534 : NuGet.Versioning.dasm (-0.42 % of base)
  -499 : Handlebars.dasm (-0.07 % of base)
  -487 : Microsoft.CodeAnalysis.Features.dasm (-0.01 % of base)
  -460 : Polly.Core.dasm (-0.04 % of base)
  -453 : GraphQL.dasm (-0.02 % of base)
  -450 : System.Net.Http.dasm (-0.05 % of base)
  -450 : Microsoft.VisualStudio.SolutionPersistence.dasm (-0.16 % of base)
  -440 : DocumentFormat.OpenXml.Framework.dasm (-0.10 % of base)
  -436 : Google.Protobuf.dasm (-0.06 % of base)
  -431 : BouncyCastle.Cryptography.dasm (-0.01 % of base)
  -428 : System.Text.Json.dasm (-0.03 % of base)
  -413 : Quartz.dasm (-0.02 % of base)
  -410 : Azure.Messaging.EventGrid.dasm (-0.06 % of base)
  -409 : Azure.Security.KeyVault.Secrets.dasm (-0.06 % of base)
  -409 : Azure.Data.Tables.dasm (-0.04 % of base)
  -409 : Azure.Security.KeyVault.Keys.dasm (-0.04 % of base)
  -409 : Azure.Storage.Blobs.dasm (-0.02 % of base)
  -409 : Azure.Storage.Queues.dasm (-0.04 % of base)
  -409 : Azure.Storage.Files.Shares.dasm (-0.02 % of base)
  -409 : Azure.Monitor.OpenTelemetry.Exporter.dasm (-0.03 % of base)
  -409 : Azure.ResourceManager.Storage.dasm (-0.01 % of base)
  -409 : Azure.Search.Documents.dasm (-0.01 % of base)
  -409 : Azure.Storage.Common.dasm (-0.06 % of base)
  -409 : Azure.ResourceManager.dasm (-0.01 % of base)
  -409 : Azure.Security.KeyVault.Certificates.dasm (-0.05 % of base)
  -385 : YamlDotNet.dasm (-0.08 % of base)
  -384 : HotChocolate.Types.dasm (-0.01 % of base)
  -374 : Microsoft.Azure.Amqp.dasm (-0.06 % of base)
  -347 : ClosedXML.dasm (-0.02 % of base)
  -343 : System.ServiceModel.NetFramingBase.dasm (-0.15 % of base)
  -341 : NetTopologySuite.dasm (-0.03 % of base)
  -338 : Microsoft.Diagnostics.NETCore.Client.dasm (-0.16 % of base)
  -321 : System.Private.CoreLib.dasm (-0.00 % of base)
  -296 : xunit.v3.core.dasm (-0.06 % of base)
  -271 : AWSSDK.Core.dasm (-0.02 % of base)
  -270 : Microsoft.Extensions.Azure.dasm (-0.19 % of base)
  -218 : Ninject.dasm (-0.06 % of base)
  -216 : JsonSubTypes.dasm (-1.01 % of base)
  -207 : NJsonSchema.dasm (-0.05 % of base)
  -204 : Microsoft.AspNetCore.Mvc.Testing.dasm (-0.38 % of base)
  -199 : System.Threading.Channels.dasm (-0.06 % of base)
  -198 : Polly.Extensions.dasm (-0.19 % of base)
  -193 : System.Linq.Parallel.dasm (-0.01 % of base)
  -193 : SixLabors.Fonts.dasm (-0.02 % of base)
  -189 : Asp.Versioning.Mvc.dasm (-0.30 % of base)
  -187 : Flurl.dasm (-0.26 % of base)
  -182 : Nest.dasm (-0.00 % of base)
  -180 : HotChocolate.Subscriptions.dasm (-0.16 % of base)
  -178 : Elastic.Apm.dasm (-0.01 % of base)
  -173 : System.Linq.dasm (-0.01 % of base)
  -172 : HotChocolate.Abstractions.dasm (-0.05 % of base)
  -163 : Markdig.dasm (-0.03 % of base)
  -146 : HotChocolate.Utilities.dasm (-0.08 % of base)
  -145 : MongoDB.Bson.dasm (-0.01 % of base)
  -126 : Stripe.net.dasm (-0.01 % of base)
  -124 : StatsdClient.dasm (-0.12 % of base)
  -108 : Microsoft.AspNetCore.OData.dasm (-0.01 % of base)
  -105 : GreenDonut.dasm (-0.02 % of base)
  -104 : HotChocolate.Validation.dasm (-0.10 % of base)
  -104 : AWSSDK.DynamoDBv2.dasm (-0.01 % of base)
  -100 : System.Interactive.Async.dasm (-0.00 % of base)
   -80 : Azure.Core.dasm (-0.01 % of base)
   -45 : Serilog.Expressions.dasm (-0.01 % of base)
    -2 : Microsoft.Extensions.AI.Abstractions.dasm (-0.00 % of base)

Comment thread src/coreclr/vm/jitinterface.cpp Resolved

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.

🔵 Needs a closer look

Shared-generic hidden-context ABI changes span the VM, JIT, and interpreter, requiring checked-runtime regressions, interpreter coverage, and x64/ARM64 disassembly checks that static review alone could not provide.

No open issues 🎉

What changed in this PR

This PR enables direct calls to resolved non-generic methods on constrained value types by explicitly passing their type context through CoreCLR’s JIT and interpreter.

Changes:

  • Allows eligible shared generic calls to bypass runtime dispatch while preserving fallback lookup for unsupported or ambiguous targets.
  • Updates the JIT and interpreter to obtain the implementation’s context from the constrained type token.
  • Adds functional and codegen regressions covering inlining, context-dependent behavior, mutation, initialization, delegates, generic methods, and return shapes.
File Description
src/​tests/​Loader/​classloader/​generics/​Misc/​ConstrainedTypeContext.csproj Configures isolated, optimized regression tests with disassembly checks.
src/​tests/​Loader/​classloader/​generics/​Misc/​ConstrainedTypeContext.cs Adds functional assertions and inlining codegen checks.
src/​coreclr/​vm/​jitinterface.cpp Permits direct calls when the constrained type supplies the required context.
src/​coreclr/​jit/​importercalls.cpp Uses the constrained token to construct the hidden context argument.
src/​coreclr/​jit/​compiler.h Adds the constrained token parameter to the importer helper declaration.
src/​coreclr/​interpreter/​compiler.cpp Mirrors constrained-token context lookup in the interpreter.

Review effort: Balanced

Allow direct calls to resolved non-generic methods on the constrained value type by passing its type context explicitly. Teach the JIT and interpreter to obtain that context from the constrained token, preserving runtime lookup for unsupported or ambiguous targets.

Add functional and codegen regressions covering inlining, context-dependent implementations, mutation, initialization, delegates, generic methods, and return shapes.
@NinoFloris
NinoFloris force-pushed the abstraction-tax/constrained-call-context branch from ff9e525 to 7e996e0 Compare November 21, 2026 00:44
…iveAOT

Mirror the CoreCLR getCallInfo decision in the NativeAOT compiler. When a constrained call resolves to a non-generic method on the constrained value type itself, keep the call direct and inlinable and supply the instantiation argument from the constrained token instead of falling back to a fat function pointer through a dictionary slot.

NativeAOT precomputes generic dictionary layouts in the scanner, so the scanner must reserve the same lookup the compile-time JIT requests. Teach the scanner to keep the call direct for this case and reserve the constrained type's handle as the instantiation argument, matching getCallInfo and the shared importer that reads the context from the constrained token.
…dyToRun

Mirror the CoreCLR getCallInfo decision in crossgen2. When a constrained call resolves to a non-generic method on the constrained value type itself, keep the call direct and inlinable and supply the instantiation argument from the constrained token instead of a fat function pointer through a dictionary slot.

The constrained type is captured before it is cleared for static virtual calls. Resolution already refuses targets outside the version bubble, so the direct call stays version resilient. Crossgen2 has no separate scanner, so no dictionary layout reservation is needed beyond keeping the call off the code pointer path.
LowerVirtualStubCall rewrites an interface dispatch into a direct call to the dispatch helper and clears the method handle, leaving a CT_USER_FUNC call whose gtCallMethHnd is null. The debug dump and disassembly paths passed that handle to eeGetMethodFullName and eeGetMethodName, which call getMethodClass without a null check and assert on hosts that reject a null handle, such as a Checked ILC, so JitDump and JitDisasm tore down the compiler on any surviving interface dispatch.

Guard both name helpers so a null handle yields a placeholder rather than a crash. In the tree dump, recognize the lowered dispatch-helper shape, a direct-address call with no method handle, and describe it as the stub dispatch helper with its target address. Normal compilation never takes these dump paths and is unchanged.
@NinoFloris
NinoFloris force-pushed the abstraction-tax/constrained-call-context branch from 7e996e0 to 49c0461 Compare November 21, 2026 00:51

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

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants