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

Does AOT plan to support EMIT in the future #95841

Closed
DotNetNext opened this issue Dec 10, 2023 · 12 comments
Closed

Does AOT plan to support EMIT in the future #95841

DotNetNext opened this issue Dec 10, 2023 · 12 comments
Labels
area-System.Reflection.Emit question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@DotNetNext
Copy link

DotNetNext commented Dec 10, 2023

Background and motivation

EMIT performs very well when running create classes and code.
It is also important in low code development

Hope to support it in AOT

       AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
       AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, 
      AssemblyBuilderAccess.Run);
       

       ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");


       TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicClass", TypeAttributes.Public);


       FieldBuilder fieldBuilder = typeBuilder.DefineField("dynamicField", typeof(string), FieldAttributes.Private);


       ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) });
       ILGenerator ctorIL = constructorBuilder.GetILGenerator();
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
       ctorIL.Emit(OpCodes.Ldarg_0);
       ctorIL.Emit(OpCodes.Ldarg_1);
       ctorIL.Emit(OpCodes.Stfld, fieldBuilder);
       ctorIL.Emit(OpCodes.Ret);


       MethodBuilder methodBuilder = typeBuilder.DefineMethod("GetDynamicField", MethodAttributes.Public, typeof(string), Type.EmptyTypes);
       ILGenerator methodIL = methodBuilder.GetILGenerator();
       methodIL.Emit(OpCodes.Ldarg_0);
       methodIL.Emit(OpCodes.Ldfld, fieldBuilder);
       methodIL.Emit(OpCodes.Ret);

  
       Type dynamicType = typeBuilder.CreateType();


       object dynamicInstance = Activator.CreateInstance(dynamicType, "Hello, Dynamic World!");


       MethodInfo methodInfo = dynamicType.GetMethod("GetDynamicField");
       string result = (string)methodInfo.Invoke(dynamicInstance, null);

       Console.WriteLine(result);  
@DotNetNext DotNetNext added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 10, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 10, 2023
@ghost
Copy link

ghost commented Dec 10, 2023

Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

EMIT performs very well when running create classes and code.
It is also important in low code development

Hope to support it in AOT

API Proposal

// 创建一个程序集
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");


    TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicClass", TypeAttributes.Public);


    FieldBuilder fieldBuilder = typeBuilder.DefineField("dynamicField", typeof(string), FieldAttributes.Private);


    ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) });
    ILGenerator ctorIL = constructorBuilder.GetILGenerator();
    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Ldarg_1);
    ctorIL.Emit(OpCodes.Stfld, fieldBuilder);
    ctorIL.Emit(OpCodes.Ret);


    MethodBuilder methodBuilder = typeBuilder.DefineMethod("GetDynamicField", MethodAttributes.Public, typeof(string), Type.EmptyTypes);
    ILGenerator methodIL = methodBuilder.GetILGenerator();
    methodIL.Emit(OpCodes.Ldarg_0);
    methodIL.Emit(OpCodes.Ldfld, fieldBuilder);
    methodIL.Emit(OpCodes.Ret);


    Type dynamicType = typeBuilder.CreateType();


    object dynamicInstance = Activator.CreateInstance(dynamicType, "Hello, Dynamic World!");


    MethodInfo methodInfo = dynamicType.GetMethod("GetDynamicField");
    string result = (string)methodInfo.Invoke(dynamicInstance, null);

    Console.WriteLine(result);  

API Usage

// Fancy the value
var c = new MyFancyCollection<int>();
c.Fancy(42);

// Getting the values out
foreach (var v in c)
    Console.WriteLine(v);

Alternative Designs

No response

Risks

No response

Author: DotNetNext
Assignees: -
Labels:

api-suggestion, area-System.Reflection.Emit

Milestone: -

@huoyaoyuan
Copy link
Member

See the current experiment:

  • NativeAOT-Mint - Mono interpreter ported to Native AOT for dynamic execution support

If you want the benefit of AOT compilation together with dynamic execution, you can use ReadyToRun. Native AOT is designed for environments that doesn't allow dynamic code at all.

@DotNetNext
Copy link
Author

It is theoretically possible to turn dynamic IL into dynamic machine language at the time of release

@huoyaoyuan
Copy link
Member

turn dynamic IL into dynamic machine language at the time of release

How dynamic it would be? Source generator is mainly created for generating code at build time, with the information from existing code.

@DotNetNext
Copy link
Author

turn dynamic IL into dynamic machine language at the time of release

How dynamic it would be? Source generator is mainly created for generating code at build time, with the information from existing code.

EMIT is used to create classes at runtime. For example, if a user wants to create a table, the table does not exist, so I need to create classes online

@DotNetNext
Copy link
Author

Future low-code AI methods will have to be created automatically at runtime

@jkotas
Copy link
Member

jkotas commented Dec 11, 2023

What do you expect to get out of this? The performance characteristics of a configuration like this would be very similar to CoreCLR with trimming and ReadyToRun. What prevents you from using CoreCLR with trimming and ReadyToRun?

@DotNetNext
Copy link
Author

What do you expect to get out of this? The performance characteristics of a configuration like this would be very similar to CoreCLR with trimming and ReadyToRun. What prevents you from using CoreCLR with trimming and ReadyToRun?

ReadyToRun cannot create objects after the application starts, unlike the runtime creation of EMIT

@huoyaoyuan
Copy link
Member

ReadyToRun cannot create objects after the application starts, unlike the runtime creation of EMIT

ReadyToRun works with JIT. You can just use Emit together with ReadyToRun.

@DotNetNext
Copy link
Author

ReadyToRun cannot create objects after the application starts, unlike the runtime creation of EMIT

ReadyToRun works with JIT. You can just use Emit together with ReadyToRun.

【AOT+EMIT+ReadyToRun 】 Can you use it together?

@huoyaoyuan
Copy link
Member

【AOT+EMIT+ReadyToRun 】

NativeAOT and ReadyToRun provides similar AOT compilation for different environment. NativeAOT is "full AOT" used for environment where dynamic code is disallowed. ReadyToRun is "partial AOT" and brings the benefit of AOT to environment where JIT is still allowed. Which environment are you interested in? What benefit of AOT are you caring about?

@DotNetNext
Copy link
Author

NativeAOT and ReadyToRun provides similar AOT compilation for different environment. NativeAOT is "full AOT" used for environment where dynamic code is disallowed. ReadyToRun is "partial AOT" and brings the benefit of AOT to environment where JIT is still allowed. Which environment are you interested in? What benefit of AOT are you caring about?

Thank you very much. Got it

@jkotas jkotas added question Answer questions and provide assistance, not an issue with source code or documentation. and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Dec 11, 2023
@jkotas jkotas closed this as completed Dec 11, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Dec 11, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection.Emit question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants