Skip to content

JIT: Centralize instantiation argument preparation in the EE - #138013

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

NinoFloris wants to merge 4 commits into
dotnet:mainfrom
NinoFloris:abstraction-tax/deferred-call-lookups

Conversation

@NinoFloris

@NinoFloris NinoFloris commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

The JIT importer and interpreter both reconstruct the hidden instantiation argument lookup from call tokens. This PR moves that logic into the EE, following #126947 and #128702. Constrained calls show how easy it is to pick the wrong context. See #137845 and #137846.

getCallInfo resolves the target and signature and supplies a constant instantiation argument when it can. When the argument requires a runtime lookup, the importer or interpreter requests it through prepareCallLookups at the point of use. Instantiation lookups were already on demand. SuperPMI records and replays the new EE query.

This split also lays the groundwork for more inlining in shared generic code. Today getCallInfo can resolve a constrained static interface call to a generic method on S<__Canon> and still return an indirect call because the method’s generic context needs a runtime lookup. #137990 keeps the call direct when the constrained type supplies that context. Moving lookup preparation into the EE gives us a way to extend that to resolved generic methods and DIMs that need a different context.

@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
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging @dotnet/jit-contrib for JIT-EE GUID update

@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 6 pipeline(s).
9 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

}
}

void CEEInfo::prepareCallLookups(CORINFO_RESOLVED_TOKEN* pResolvedToken,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas PTAL. What do you think about separating call resolution from lookup preparation?

The full change centralizes lookup construction in the EE. The importer and interpreter no longer need to reconstruct it from the call tokens, getCallInfo also stops eagerly building lookups (and reserving dictionary slots) that import may never need.

It complements #135169 and #137990. For a shared generic struct like S<__Canon>, the JIT can already know which generic method it’s calling and still need to look up the generic context to pass to it. Today that turns into an indirect call and blocks inlining. This split gives us a way to pass the dictionary separately.

Note, the same also applies to resolved DIMs that take a generic context (just less relevant).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about separating call resolution from lookup preparation?

The concept sounds fine to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I’ve narrowed this PR to the API split. Would @jakobbotsch and @MichalStrehovsky be the right reviewers for the JIT and NAOT/R2R sides respectively?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change JitInterface here? When I'm looking at the change from native AOT perspective only (changes in CorInfoImpl.RyuJit.cs and CorInfoImpl.cs), it seems like RyuJIT can check pResult->sig.callConv and pResult->exactContextNeedsRuntimeLookup to do what it needs and then call embedGenericHandle as needed. I.e. CORINFO_LOOKUP_DEFERRED is redundant with hasTypeArg() && exactContextNeedsRuntimeLookup and the new API is just a wrapper over emebedGenericHandle.

If we want to tackle more generic inlining, we should teach RyuJIT about runtime determined types, but that's a complicated thing.

@NinoFloris NinoFloris Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getCallInfo contract does change, today it blocks certain target refinements when they have a different owner, with CORINFO_LOOKUP_DEFERRED we get to split them up. And it also solves your own issue from #129560 (comment) as we stop asking for dictionary slots too early.

Afaik @jakobbotsch didn't want us to build more tokens and contexts manually in the jit. At least I got that idea from the GVM devirt work @hez2010 is pushing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to Jakob for review, there is nothing novel to review on the JitInterface side, the new flags and APIs are redundant.

@NinoFloris NinoFloris Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should teach RyuJIT about runtime determined types

could you elaborate?

I pushed the provisional follow-up here for now: main...NinoFloris:dotnet-runtime:abstraction-tax/deferred-interface-dispatch

See NinoFloris@77bfbd7#diff-98b4e379205d747e922689cb993f985be190fedb509fd486e99280c842703472R5240-R5244
and NinoFloris@3c74b0b#diff-98b4e379205d747e922689cb993f985be190fedb509fd486e99280c842703472R5294-R5298

for the additional uses of CORINFO_LOOKUP_DEFERRED, handling code pointers and interface dispatch in shared generics

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichalStrehovsky take a look, I elaborated a bit on what the plan is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you elaborate?

It's the representation that .NET Native's UTC codegen backend used. People on the runtime side are more familiar and this is probably not something we want as a community PR so I'll just leave it at that, the comment was meant for the other reviewers.

@MichalStrehovsky take a look, I elaborated a bit on what the plan is.

If I'm seeing correctly, this adds some extra handling for ReadyToRun, so still not something I can provide input on and will defer to other reviewers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to tackle more generic inlining, we should teach RyuJIT about runtime determined types, but that's a complicated thing.

It's the representation that .NET Native's UTC codegen backend used.

I think RyuJIT can gradually evolve towards that by carrying type argument bindings through inlines. It could keep CORINFO_LOOKUP_DEFERRED lookups pending until inlining is done, then ask the EE to prepare only the ones it still needs. In principle RyuJIT could already do a limited version of it without this PR, but it still has to work out which lookup to use for a target the EE has already resolved, including what the different backends like R2R can't represent. And as long as getCallInfo builds the lookup eagerly, it may already reserve a dictionary slot that inlining makes unnecessary.

This is why I consider that getCallInfo ties two related decisions together too early:

  • For a constrained call to a generic method on S<__Canon>, it can know the target and still return an indirect call because the implementation’s method dictionary needs a runtime lookup.
  • Where getCallInfo allows a hidden argument, the importer and interpreter use contextHandle to choose which lookup to request from the call token. That creates avoidable coupling because getCallInfo can only expose a direct call if both importers know how to reconstruct its lookup. And reconstructing it from the original token can’t cover every resolved target. The token can name the interface method while the dictionary belongs to the implementation the EE selected, so there’s a limit to what RyuJIT can reconstruct from the call token alone. It's also why prepareCallLookups does more than embedGenericHandle. The latter starts from a token, while prepareCallLookups has the EE supply the lookup for the target it actually resolved.
  • GVMs have a similar gap. Even with an exact receiver and a known override, resolveVirtualMethod bails if the shared method instantiation needs a runtime dictionary lookup. This PR leaves that path alone, but it could use the same split (I can address it in this PR if that would be better). CoreCLR GVM resolver

Just have the EE supply instParamLookup for the target it actually resolved, leaving contextHandle for resolving inlinee tokens. That’s the distinction Jakob raised in #126947 and it removes all these fidelity gaps.

If I'm seeing correctly, this adds some extra handling for ReadyToRun, so still not something I can provide input on and will defer to other reviewers.

R2R has to be able to validate the direct target and encode the lookup it needs. I ran into the validation side of that with IResult<__Canon> in #135169.

Each EE can choose a call shape and lookup its backend can represent. If a format limitation forces R2R to return an indirect call, CoreCLR can still expose a direct target for the same call and mark its dictionary lookup CORINFO_LOOKUP_DEFERRED. That lets RyuJIT do the best it can everywhere, without making certain backend limitations a restriction on every other backend. Even if the backends eventually support all the same call shapes and lookups, the split still lets RyuJIT defer figuring out which lookups it actually needs until after inlining, so it stays relevant.

Add prepareCallLookups to supply the hidden instantiation argument as a full CORINFO_LOOKUP. The JIT and interpreter request runtime-dependent lookups only when needed, preserving on-demand preparation while moving token and context selection into the EE. Update NativeAOT, ReadyToRun and SuperPMI for the shared interface.
@NinoFloris
NinoFloris force-pushed the abstraction-tax/deferred-call-lookups branch from 666a7db to 192e804 Compare November 24, 2026 17:13
@NinoFloris NinoFloris changed the title JIT: Deferred call lookups JIT: Centralize instantiation argument preparation in the EE Nov 24, 2026
@NinoFloris
NinoFloris marked this pull request as ready for review November 24, 2026 17:16
@jkotas
jkotas requested review from jakobbotsch and a balanced review from Copilot November 24, 2026 18:50

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

The generic-context and ABI refactor spans multiple execution paths, requiring cross-runtime codegen, regression, and SuperPMI record/replay validation that was not performed in this review.

No open issues 🎉

What changed in this PR

This PR moves hidden generic instantiation argument preparation from the JIT importer and interpreter into the execution engine (EE). It preserves on-demand runtime lookups and provides a foundation for extending direct calls and inlining in shared generic code.

Changes:

  • Adds prepareCallLookups and a deferred lookup state to the JIT–EE interface.
  • Updates the JIT, interpreter, and ahead-of-time compiler implementations to use EE-supplied instantiation lookups.
  • Updates interface versioning, generated wrappers, and SuperPMI recording and replay.
File Description
src/​coreclr/​vm/​jitinterface.cpp Supplies constant lookups and prepares deferred runtime lookups.
src/​coreclr/​tools/​superpmi/​superpmi/​icorjitinfo.cpp Replays the new EE query.
src/​coreclr/​tools/​superpmi/​superpmi-shim-simple/​icorjitinfo_generated.cpp Forwards the new query.
src/​coreclr/​tools/​superpmi/​superpmi-shim-counter/​icorjitinfo_generated.cpp Counts and forwards the new query.
src/​coreclr/​tools/​superpmi/​superpmi-shim-collector/​icorjitinfo.cpp Records lookup preparation results.
src/​coreclr/​tools/​superpmi/​superpmi-shared/​methodcontext.h Declares recording, replay, and packet support.
src/​coreclr/​tools/​superpmi/​superpmi-shared/​methodcontext.cpp Serializes and restores full instantiation lookups.
src/​coreclr/​tools/​superpmi/​superpmi-shared/​lwmlist.h Registers the lookup preparation map.
src/​coreclr/​tools/​superpmi/​superpmi-shared/​agnostic.h Updates portable recording structures.
src/​coreclr/​tools/​Common/​JitInterface/​ThunkGenerator/​ThunkInput.txt Adds the query to thunk generation input.
src/​coreclr/​tools/​Common/​JitInterface/​CorInfoTypes.cs Mirrors the deferred state and full lookup structure.
src/​coreclr/​tools/​Common/​JitInterface/​CorInfoImpl.cs Implements shared managed lookup preparation.
src/​coreclr/​tools/​Common/​JitInterface/​CorInfoImpl_generated.cs Adds the managed callback binding.
src/​coreclr/​tools/​aot/​jitinterface/​jitinterface_generated.h Adds the native callback and forwarding wrapper.
src/​coreclr/​tools/​aot/​ILCompiler.RyuJit/​JitInterface/​CorInfoImpl.RyuJit.cs Adapts NativeAOT call information to the lookup model.
src/​coreclr/​tools/​aot/​ILCompiler.ReadyToRun/​JitInterface/​CorInfoImpl.ReadyToRun.cs Adapts ReadyToRun constant and deferred lookups.
src/​coreclr/​jit/​importercalls.cpp Replaces token-based reconstruction with EE-supplied lookups.
src/​coreclr/​jit/​importer.cpp Asserts deferred lookups are prepared before use.
src/​coreclr/​jit/​ICorJitInfo_wrapper_generated.hpp Wraps the new query with API instrumentation.
src/​coreclr/​jit/​ICorJitInfo_names_generated.h Registers the new API name.
src/​coreclr/​jit/​compiler.h Updates the instantiation argument helper signature.
src/​coreclr/​interpreter/​compiler.cpp Uses EE-supplied lookups while preserving readonly array handling.
src/​coreclr/​inc/​jiteeversionguid.h Updates the JIT–EE compatibility identifier.
src/​coreclr/​inc/​icorjitinfoimpl_generated.h Declares the new interface override.
src/​coreclr/​inc/​corinfo.h Defines and documents the deferred lookup contract.

Review effort: Balanced

Comment thread src/coreclr/vm/jitinterface.cpp Resolved
Comment thread src/coreclr/vm/jitinterface.cpp Resolved
Give prepareCallLookups its own transition and share the lookup implementation with embedGenericHandle so both JIT entry points cross the boundary once.

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