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

[NativeAOT] Fix Activator.CreateInstance for shared generic structs #101021

Merged
merged 3 commits into from
Apr 15, 2024

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Apr 14, 2024

The default constructor has to be invoked using fat pointer in shared generic structs.

@jkotas jkotas changed the title [NativeAOT] Fix Activator.CreateInstance for shared generic structs w… [NativeAOT] Fix Activator.CreateInstance for shared generic structs Apr 14, 2024
The default constructor has to be invoked using fat pointer in shared generic structs.
@jkotas jkotas force-pushed the CreateInstanceT_GenericTypes branch from 6767b01 to e49c6c3 Compare April 14, 2024 13:16
@jkotas
Copy link
Member Author

jkotas commented Apr 14, 2024

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkotas
Copy link
Member Author

jkotas commented Apr 14, 2024

cc @AaronRobinsonMSFT Bug that I have noticed as I was replying to your email

@jkotas jkotas marked this pull request as ready for review April 14, 2024 19:34
Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

Thanks! I wanted to ask by how much this makes things slower, but optimizing the "no default ctor" case can be a separate thing. I think I can add a RuntimeHelpers.MightHaveDefaultConstructor<T> that can be used to avoid this extra work (and more) in the 99% common case.

@jkotas
Copy link
Member Author

jkotas commented Apr 15, 2024

optimizing the "no default ctor"

This should be taken care of by

if (methodName == "CreateInstance" && method.Signature.Length == 0 && method.HasInstantiation
&& method.Instantiation[0] is TypeDesc activatedType
&& activatedType != context.UniversalCanonType
&& activatedType.IsValueType
&& activatedType.GetParameterlessConstructor() == null)
{
ILEmitter emit = new ILEmitter();
ILCodeStream codeStream = emit.NewCodeStream();
var t = emit.NewLocal(context.GetSignatureVariable(0, method: true));
codeStream.EmitLdLoca(t);
codeStream.Emit(ILOpcode.initobj, emit.NewToken(context.GetSignatureVariable(0, method: true)));
codeStream.EmitLdLoc(t);
codeStream.Emit(ILOpcode.ret);
return new InstantiatedMethodIL(method, emit.Link(method.GetMethodDefinition()));
}
. I am not sure what else is there to optimize.

@MichalStrehovsky
Copy link
Member

This should be taken care of by

Keep forgetting how this works. I guess the throw on line 46 might be unreachable for value types then?

@jkotas
Copy link
Member Author

jkotas commented Apr 15, 2024

I guess the throw on line 46 might be unreachable for value types then?

It may be reachable for value types with private default constructor.

@jkotas jkotas merged commit 8a1e0b7 into dotnet:main Apr 15, 2024
130 of 133 checks passed
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
…otnet#101021)

The default constructor has to be invoked using fat pointer in shared generic structs.
@jkotas jkotas deleted the CreateInstanceT_GenericTypes branch May 6, 2024 00:57
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
…otnet#101021)

The default constructor has to be invoked using fat pointer in shared generic structs.
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2024
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.

3 participants