Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Nov 29, 2024

This change is part of "Allow inlining for helper methods" effort, I already implemented it locally, but it turns out to be a lot of changes so I split it into 3 PRs for simpler code review

  • Add JIT-EE API to query real CORINFO_METHOD_HANDLE out of a helper id (this PR)
  • Refactor GenTreeCall::gtCallMethHnd (currently, we store helper id in it, I want it to be always a real method handle and the actual id is stored separately, fortunately, we have a room for that in GenTreeCall) -- quite a lot of changes.
  • Minor changes in the inliner to allow helper calls

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 29, 2024
@dotnet-policy-service
Copy link
Contributor

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

@EgorBo
Copy link
Member Author

EgorBo commented Nov 29, 2024

PTAL @jkotas, no functional changes (yet).

@EgorBo EgorBo requested a review from jkotas November 29, 2024 14:27
@am11
Copy link
Member

am11 commented Nov 29, 2024

@EgorBo, thank you for this. Sound like it will help existing helpers as well. 👍

Minor changes in the inliner to allow helper calls

Would it inline FCall in managed -> fcall transition? Current (main branch) codegen for DIV on win-x86 is approximately https://godbolt.org/z/P8Pnx4Tdn.

@EgorBo
Copy link
Member Author

EgorBo commented Nov 29, 2024

Would it inline FCall in managed -> fcall transition?

I am not sure I understand the question. An FCall that redirects to managed? Why is it an FCall in the first place?

@am11
Copy link
Member

am11 commented Nov 29, 2024

Would it inline FCall in managed -> fcall transition?

I am not sure I understand the question. An FCall that redirects to managed? Why is it an FCall in the first place?

In main, it's using native helpers. With #109087, it is using managed helpers which make fcall to get CRT div/mod impl. Managed call is taking care of overflow/divbyzero checks.

@EgorBo
Copy link
Member Author

EgorBo commented Nov 29, 2024

Would it inline FCall in managed -> fcall transition?

I am not sure I understand the question. An FCall that redirects to managed? Why is it an FCall in the first place?

In main, it's using native helpers. With #109087, it is using managed helpers which make fcall to get CRT div/mod impl. Managed call is taking care of overflow/divbyzero checks.

Then yes, it should be handled, because JIT knows these methods as helpers.

) = 0;

// return the native entry point to an EE helper (see CorInfoHelpFunc)
virtual void* getHelperFtn (
Copy link
Member

Choose a reason for hiding this comment

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

Is the JIT going to use both the code pointer and the method handle when callling this method with non-null pMethod?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is the JIT going to use both the code pointer and the method handle when callling this method with non-null pMethod?

No, are you implying that it's better to have a separate API?

Copy link
Member

@jkotas jkotas Dec 2, 2024

Choose a reason for hiding this comment

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

It does not have to be a separate API. It can be one API with two optional out args. Using ppIndirection to indicate whether you are interested in the actual entrypoint would look odd. I am thinking something like this may be the best shape:

void getHelperFtn(CorInfoHelpFunc ftnNum, 
    CORINFO_CONST_LOOKUP* addr, // optional [OUT]
    CORINFO_METHOD_HANDLE* method) // optional [OUT]

MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this pull request Dec 5, 2024
We had indirections set up where we'd `RuntimeExport` a method under a symbolic name and then `RuntimeImport` it elsewhere (or call the export from JIT-generated code).

Most of this was motivated by the old Redhawk layering where things like casting and interface dispatch were part of Runtime.Base and not part of CoreLib. Since we decided if we ever reintroduce a Runtime.Base, we wouldn't have casting in it, this indirection will no longer be needed.

Motivated by dotnet#110267 (and also the reason why I'm only doing it for `RuntimeExport`s used from the JIT right now). Once that PR gets in, calling methods through `RuntimeExport`s would actually become a bigger deoptimization than it is now.
MichalStrehovsky added a commit that referenced this pull request Dec 9, 2024
We had indirections set up where we'd `RuntimeExport` a method under a symbolic name and then `RuntimeImport` it elsewhere (or call the export from JIT-generated code).

Most of this was motivated by the old Redhawk layering where things like casting and interface dispatch were part of Runtime.Base and not part of CoreLib. Since we decided if we ever reintroduce a Runtime.Base, we wouldn't have casting in it, this indirection will no longer be needed.

Motivated by #110267 (and also the reason why I'm only doing it for `RuntimeExport`s used from the JIT right now). Once that PR gets in, calling methods through `RuntimeExport`s would actually become a bigger deoptimization than it is now.
hez2010 pushed a commit to hez2010/runtime that referenced this pull request Dec 14, 2024
We had indirections set up where we'd `RuntimeExport` a method under a symbolic name and then `RuntimeImport` it elsewhere (or call the export from JIT-generated code).

Most of this was motivated by the old Redhawk layering where things like casting and interface dispatch were part of Runtime.Base and not part of CoreLib. Since we decided if we ever reintroduce a Runtime.Base, we wouldn't have casting in it, this indirection will no longer be needed.

Motivated by dotnet#110267 (and also the reason why I'm only doing it for `RuntimeExport`s used from the JIT right now). Once that PR gets in, calling methods through `RuntimeExport`s would actually become a bigger deoptimization than it is now.
@am11
Copy link
Member

am11 commented Jan 3, 2025

@EgorBo any chance you could address Jan's comment and we move to part 2 soon'ish. Just checking. Happy new year! :)

@EgorBo
Copy link
Member Author

EgorBo commented Jan 20, 2025

@EgorBo any chance you could address Jan's comment and we move to part 2 soon'ish. Just checking. Happy new year! :)

@am11 oops, didn't see this ping, I'll finish it today/tomorrow 🙂

davidwrighton added a commit to davidwrighton/runtime that referenced this pull request Jun 12, 2025
- If there is one associated, we will get the method handle. This is an optional parameter, as is the logic to get the existing result.
- This will be used in the interpreter to generate different bytecode that knows about calling managed methods when needed
- There is also some prior interest from the JIT team about using such an api, but it didn't quite get in. See PR dotnet#110267. This PR takes the comment from @jkotas into account to adjust the signature of the method to be something which is simpler and easier to understand
@jkotas
Copy link
Member

jkotas commented Jun 12, 2025

Superseded by #116603

@jkotas jkotas closed this Jun 12, 2025
davidwrighton added a commit that referenced this pull request Jun 13, 2025
* getHelperFtn support for getting CORINFO_METHOD_HANDLE
- If there is one associated, we will get the method handle. This is an optional parameter, as is the logic to get the existing result.
- This will be used in the interpreter to generate different bytecode that knows about calling managed methods when needed
- There is also some prior interest from the JIT team about using such an api, but it didn't quite get in. See PR #110267. This PR takes the comment from @jkotas into account to adjust the signature of the method to be something which is simpler and easier to understand
@github-actions github-actions bot locked and limited conversation to collaborators Jul 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants