Skip to content

Support HasThis and ExplicitThis calling conventions in AssemblyBuilder and DynamicMethod #113666

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

Merged
merged 7 commits into from
Mar 24, 2025

Conversation

steveharter
Copy link
Contributor

@steveharter steveharter commented Mar 18, 2025

Fixes issues around using calli with CallingConventions.HasThis and CallingConventions.ExplicitThis both when generating the IL and when executing the IL.

Fixes #113626

@steveharter steveharter added this to the 10.0.0 milestone Mar 18, 2025
@steveharter steveharter self-assigned this Mar 18, 2025
@Copilot Copilot AI review requested due to automatic review settings March 18, 2025 19:32
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the HasThis calling convention in PersistedAssemblyBuilder by extending the signature calling convention and updating associated tests and module builder logic.

  • Introduces an extended enum (SignatureCallingConventionEx) with a HasThis flag.
  • Adds a new test (AssemblyWithInstanceBasedFunctionPointer) to verify the instance-based function pointer behavior.
  • Updates ModuleBuilderImpl to check and set the HasThis flag based on the calling convention.

Reviewed Changes

Copilot reviewed 3 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/SignatureCallingConventionEx.cs Adds an extended enum for calling convention flags.
src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveAssemblyBuilderTests.cs Adds tests for instance-based function pointer calls.
src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ModuleBuilderImpl.cs Updates GetSignatureConvention to incorporate the HasThis flag.
Files not reviewed (2)
  • src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj: Language not supported
  • src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj: Language not supported
Comments suppressed due to low confidence (1)

src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveAssemblyBuilderTests.cs:351

  • [nitpick] Consider adding a comment to clarify why a static method is being used, how it simulates an instance call with HasThis, and what the expected behavior is, to aid future maintainers in understanding the test.
il.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(Guid), null, null);

}

return convention;
if ((callingConvention & CallingConventions.HasThis) != 0)
Copy link
Preview

Copilot AI Mar 18, 2025

Choose a reason for hiding this comment

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

Ensure that the conversion from SignatureCallingConvention to SignatureCallingConventionEx preserves all intended flags without conflicts. Adding a short comment to document why the cast to the extended enum is safe would improve maintainability.

Copilot uses AI. Check for mistakes.

@steveharter
Copy link
Contributor Author

Wasm build failures due to #113685

@steveharter steveharter changed the title Support HasThis calling convention in PersistedAssemblyBuilder Support HasThis and ExplicitThis calling conventions in AssemblyBuilder and DynamicMethod Mar 21, 2025
@AaronRobinsonMSFT
Copy link
Member

/cc @dotnet/jit-contrib

@jkotas
Copy link
Member

jkotas commented Mar 22, 2025

Use 'object' for type in tests

What was wrong with MyClassWithGuidProperty?

@steveharter
Copy link
Contributor Author

steveharter commented Mar 22, 2025

What was wrong with MyClassWithGuidProperty?

Nothing, just wanted to make sure I can use object for reference types and that the concrete type wasn't necessary. It was verifying there are no issues for shared generated IL across different reference types. I'll add a comment about this.

@steveharter steveharter merged commit 2fe5b1b into dotnet:main Mar 24, 2025
133 of 139 checks passed
@steveharter steveharter deleted the FpToCalli branch March 24, 2025 17:17
if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis)

// Pop the this parameter if the method has an implicit this parameter.
if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Could simplify the condition to (callingConvention & (CallingConventions.HasThis | CallingConventions.ExplicitThis)) == CallingConventions.HasThis

jakobbotsch added a commit to dotnet/runtimelab that referenced this pull request Apr 10, 2025
Fixes:
- `BuildResumptionStubCalliSignature` was building a signature with `HASTHIS | EXPLICITTHIS` calling convention always, even for signatures without any 'this'. With dotnet/runtime#113666 this broke as we started reordering the first argument with ret buffers.
- Update JIT interface `getEHinfo` to handle getting EH clause information when trying to inline methods with transient IL, like async thunks
- Update for new refactored jitinterface classes
- Update for removed `g_isNewExceptionHandlingEnabled`
@github-actions github-actions bot locked and limited conversation to collaborators Apr 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PersistedAssemblyBuilder not emitting calling convention for Calli + CallingConventions.HasThis
5 participants