Skip to content

Cleanup some code under FEATURE_STUBS_AS_IL#104731

Merged
jkotas merged 33 commits into
dotnet:mainfrom
huoyaoyuan:stubs-as-il
Sep 19, 2024
Merged

Cleanup some code under FEATURE_STUBS_AS_IL#104731
jkotas merged 33 commits into
dotnet:mainfrom
huoyaoyuan:stubs-as-il

Conversation

@huoyaoyuan

@huoyaoyuan huoyaoyuan commented Jul 11, 2024

Copy link
Copy Markdown
Member

There's no actually no IL stubs under this feature switch now, only asm stubs.

This PR addresses the simple cases. On all platforms except Windows x86, single cast delegate is written in invariant assembly code.

Remaining code under the feature switch is COM call stub, which is more complicated and is better separated out.

@ghost ghost added the area-VM-coreclr label Jul 11, 2024
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 11, 2024
@jkotas jkotas changed the title Cleanup some code under FEATURE_STUBS_AS_AL Cleanup some code under FEATURE_STUBS_AS_IL Jul 12, 2024
Comment thread src/coreclr/vm/i386/asmhelpers.asm Outdated
Comment thread src/coreclr/vm/jitinterface.h Outdated
Comment thread src/coreclr/vm/stubmgr.cpp Outdated
Comment thread src/coreclr/vm/stubmgr.cpp Outdated
@jkotas

jkotas commented Jul 17, 2024

Copy link
Copy Markdown
Member

Code quality

If this helps with code quality, could you please make the same change in

codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emit.NewToken(functionPointerField.InstantiateAsOpen()));
as well? It is naot equivalent of this stub.

Comment thread src/coreclr/vm/comdelegate.cpp Outdated
@huoyaoyuan

Copy link
Copy Markdown
Member Author

The same tests in DI are still failing. Can it be related to the signature instantiation in ILC?

if (method.OwningType.HasInstantiation)
{
// If the owning type is generic, the signature will contain T's and U's.
// We need !0's and !1's.
TypeDesc[] typesToReplace = new TypeDesc[method.OwningType.Instantiation.Length];
TypeDesc[] replacementTypes = new TypeDesc[typesToReplace.Length];
for (int i = 0; i < typesToReplace.Length; i++)
{
typesToReplace[i] = method.OwningType.Instantiation[i];
replacementTypes[i] = context.GetSignatureVariable(i, method: false);
}
TypeDesc[] parameters = new TypeDesc[method.Signature.Length];
for (int i = 0; i < parameters.Length; i++)
{
parameters[i] = method.Signature[i].ReplaceTypesInConstructionOfType(typesToReplace, replacementTypes);
}
TypeDesc returnType = method.Signature.ReturnType.ReplaceTypesInConstructionOfType(typesToReplace, replacementTypes);
signature = new MethodSignature(signature.Flags, signature.GenericParameterCount, returnType, parameters);
}
Is it also required for coreclr?

@jkotas

jkotas commented Jul 18, 2024

Copy link
Copy Markdown
Member

The same tests in DI are still failing. Can it be related to the signature instantiation in ILC?

We should not need a special signature instantiation like that in CoreCLR. I suspect that it is related to a special delegate calling convention used for delegate Invocation in some cases. The delegate target cannot be called via pointer - it must be called as Delegate.Invoke method so that the special calling convention kicks in.

Do you know if the Invoke method is used in the DI on a performance critical path?

This is proving to be much harder than what I thought it is going to be.

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Comment thread src/coreclr/vm/dllimport.h Outdated
Comment thread src/coreclr/vm/dllimport.h Outdated
Comment thread src/coreclr/vm/stublink.h Outdated
Comment thread src/coreclr/vm/stublink.h Outdated
Comment thread src/coreclr/vm/method.hpp Outdated
@jkotas

jkotas commented Sep 19, 2024

Copy link
Copy Markdown
Member

I have pushed a commit with a small change to handle the new IL stub in stub manager. The manual debugger test from #104731 (comment) works fine now.

@jkotas jkotas left a comment

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.

LGTM. Thank you!

@jkotas

jkotas commented Sep 19, 2024

Copy link
Copy Markdown
Member

Remaining code under the feature switch is COM call stub, which is more complicated and is better separated out.

Do you plan to work on cleaning up the remaining code under the feature switch?

@huoyaoyuan
huoyaoyuan deleted the stubs-as-il branch September 19, 2024 16:18
@huoyaoyuan

Copy link
Copy Markdown
Member Author

Do you plan to work on cleaning up the remaining code under the feature switch?

Aaron suggested me to avoid converting COM interop related code because the effort needed for reviewing. I'm already busy now until October.

If you can confirm the review effort on behalf of interop team, I can start to investigate this when I finish urgent tasks.

@jkotas

jkotas commented Sep 20, 2024

Copy link
Copy Markdown
Member

I gave it a shot in #108048.

sirntar pushed a commit to sirntar/runtime that referenced this pull request Sep 30, 2024
* Use direct SinglecastDelegateInvokeStub on windows x86

* Cleanup ifdefs

* EmitDebugBreak is unused

* Use IL stub for Delegate.Invoke

* Remove SinglecastDelegateInvokeStub in asm

* Fix method name and argument count

* Code quality

* Delete JIT_InternalThrow and CorInfoException

* Apply the same codegen to ilc

* Add KeepAlive

* Emit recursive call instead

* Add tests for indirect delegate invocation

* Use public reflection instead.

* Use ldvirtftn to bypass this check

* Use winner from CompareExchange

* Return Stub from GetInvokeMethodStub

* Delete NEWSTUB_FL_MULTICAST and dependents

* Handle new stub in ILStubManager

---------

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-VM-coreclr 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