Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return type from exact method context for call returns #100041

Closed
wants to merge 9 commits into from

Conversation

MichalPetryka
Copy link
Contributor

@MichalPetryka MichalPetryka commented Mar 20, 2024

This should let us devirtualize and inline more calls due to seeing the real type in variables instead of _Canon.

I've reused the existing LateDevirtualizationInfo for this and enabled storing it for non virtual calls too, it seemed safe as far as I've seen as all other places still checked IsVirtual before using it.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 20, 2024
@MichalPetryka
Copy link
Contributor Author

@MihuBot

Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS
Copy link
Member

@MichalPetryka what's the status of this PR?

@MichalPetryka
Copy link
Contributor Author

MichalPetryka commented Apr 12, 2024

@MichalPetryka what's the status of this PR?

NativeAOT/R2R VM doesn't like seeing non shared generic contexts here and iirc my last discussion about it with @MichalStrehovsky ended up with me asking whether that VM should be changed to behave like coreclr or if the JIT should check stuff here.

@jkotas
Copy link
Member

jkotas commented Apr 13, 2024

NativeAOT/R2R VM doesn't like seeing non shared generic contexts here

Where exactly?

@MichalPetryka
Copy link
Contributor Author

Where exactly?

In the getMethodSig path with the 2 asserts I've changed in this PR to have better messages.

@jkotas
Copy link
Member

jkotas commented Apr 15, 2024

The JIT/EE interface methods expect that the context and method should always match. If you are passing mismatched context and method around, it won't work well. I would expect that to hit problems in both AOT and JIT scenarios.

Maybe you need a new method on JIT/EE interface to compute context for a method from more derived type?

@MichalStrehovsky
Copy link
Member

NativeAOT/R2R VM doesn't like seeing non shared generic contexts here

Where exactly?

I think something along the lines of this will fix the problem:

diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs
index 2be89e7374f..07bdb48b21a 100644
--- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs
+++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs
@@ -1452,7 +1452,14 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
                     // the method with a method the intrinsic expands into. If it's not the special intrinsic,
                     // method stays unchanged.
                     var methodIL = (MethodIL)HandleToObject((void*)pResolvedToken.tokenScope);
-                    targetMethod = _compilation.ExpandIntrinsicForCallsite(targetMethod, methodIL.OwningMethod);
+                    MethodDesc callsiteMethod = _compilation.ExpandIntrinsicForCallsite(targetMethod, methodIL.OwningMethod);
+                    if (targetMethod != callsiteMethod)
+                    {
+                        Debug.Assert(!pResult->exactContextNeedsRuntimeLookup);
+                        Debug.Assert(!callsiteMethod.HasInstantiation && !targetMethod.HasInstantiation);
+                        pResult->contextHandle = contextFromType(callsiteMethod.OwningType);
+                        targetMethod = callsiteMethod;
+                    }

                     // For multidim array Address method, we pretend the method requires a hidden instantiation argument
                     // (even though it doesn't need one). We'll actually swap the method out for a differnt one with

I'd undo the assert changes.

Copy link
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@tannergooding tannergooding reopened this Jun 14, 2024
@tannergooding
Copy link
Member

@MichalPetryka asked for this to be reopened.

@MichalPetryka
Copy link
Contributor Author

@MihuBot

@MichalPetryka
Copy link
Contributor Author

NativeAOT/R2R VM doesn't like seeing non shared generic contexts here

Where exactly?

I think something along the lines of this will fix the problem:

diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs
index 2be89e7374f..07bdb48b21a 100644
--- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs
+++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs
@@ -1452,7 +1452,14 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
                     // the method with a method the intrinsic expands into. If it's not the special intrinsic,
                     // method stays unchanged.
                     var methodIL = (MethodIL)HandleToObject((void*)pResolvedToken.tokenScope);
-                    targetMethod = _compilation.ExpandIntrinsicForCallsite(targetMethod, methodIL.OwningMethod);
+                    MethodDesc callsiteMethod = _compilation.ExpandIntrinsicForCallsite(targetMethod, methodIL.OwningMethod);
+                    if (targetMethod != callsiteMethod)
+                    {
+                        Debug.Assert(!pResult->exactContextNeedsRuntimeLookup);
+                        Debug.Assert(!callsiteMethod.HasInstantiation && !targetMethod.HasInstantiation);
+                        pResult->contextHandle = contextFromType(callsiteMethod.OwningType);
+                        targetMethod = callsiteMethod;
+                    }

                     // For multidim array Address method, we pretend the method requires a hidden instantiation argument
                     // (even though it doesn't need one). We'll actually swap the method out for a differnt one with

I'd undo the assert changes.

Seems fine with this, can't say I fully understand how it fixes the issue though.

@MichalPetryka MichalPetryka marked this pull request as ready for review June 15, 2024 15:00
@MichalPetryka

This comment was marked as resolved.

1 similar comment
@MichalPetryka

This comment was marked as resolved.

@MihuBot

This comment was marked as resolved.

@MichalPetryka
Copy link
Contributor Author

@MihuBot merge

@build-analysis build-analysis bot mentioned this pull request Jun 15, 2024
@AndyAyersMS
Copy link
Member

The diffs from this are very minimal, which is a little surprising. As is I am not sure we should take this change. Either this particular pattern doesn't come up very often, or there is something else inhibiting the JIT from acting on this information.

You should consider trying to instrument to see how often this produces a "better" type for the return value, and then look into cases where we have a better return type but no visible impact, and see if there are any blockers.

@JulieLeeMSFT
Copy link
Member

Ping @MichalPetryka on where you are on this PR and if you will address the feedback from andyayersMS.

@JulieLeeMSFT JulieLeeMSFT added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jul 22, 2024
Copy link
Contributor

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@JulieLeeMSFT JulieLeeMSFT added this to the 10.0.0 milestone Aug 12, 2024
Copy link
Contributor

This pull request will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the pull request, but please note that it will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the 10.0.0 milestone Aug 26, 2024
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 needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants