dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.09k stars 4.7k forks source link

Does mono support partially aot partially interpreted runs #99807

Closed newNcy closed 4 months ago

newNcy commented 6 months ago

In the mono ios example, I noticed that with the tag can configure how mono runtime runs and how assembly compiles, is it possible to specify some dll for aot compile and the rest for interpreted? And i found that in MAUI we can use MtouchInterpreter to do that

vitek-karas commented 6 months ago

@simonrozsival as you've been looking into a similar area recently.

newNcy commented 6 months ago

@simonrozsival Hi, any information you can share?

simonrozsival commented 6 months ago

@newNcy Yes, the Mono AOT compiler can do exactly that. The MtouchInterpreter property is not exclusive to MAUI, it can be used with any iOS .NET app.

newNcy commented 6 months ago

@newNcy Yes, the Mono AOT compiler can do exactly that. The MtouchInterpreter property is not exclusive to MAUI, it can be used with any iOS .NET app.

Thanks ! Here are the things i have tried I made some changes to the project under runtime/src/mono/sample/ios

  1. added <MonoForceInterpreter> true</MonoForceInterpreter> property
  2. changed runtime/src/mono/msbuild/apple/build/AppleBuild.targets <_InternalForceInterpret Condition="'$(MonoForceInterpreter)' == 'true' and '%(FileName)%(Extension)' ! = 'System.Private.CoreLib.dll'">true</_InternalForceInterpret> Added Program.dll here. When I run make run, the speed of the program is exactly the same as without Program.dll.

Am I missing something or how do I use Mtouchinterpereter in this sample?

simonrozsival commented 6 months ago

Ah, ok, you are using mono from the runtime directly. I thought you were using the iOS SDK, just without MAUI. The MtouchInterpreter property is defined in the xamarin/xamarin-macios repo and so you can't use it just with the runtime.

@kotlarmilos do you know how to configure the AOT compiler in the iOS sample to AOT some assemblies and interpret the rest?

newNcy commented 6 months ago

Actully im not sure what sdk i should use, my scenario is to run c#(mono) runtime in cpp, and exec the assemblies which contains som downloaded from network and cant be updated without update the ios app

newNcy commented 6 months ago

@kotlarmilos need some help here

kotlarmilos commented 6 months ago

The runtime pipeline supports two modes: Full and JustInterp. In Full mode, it AOT compiles all assemblies. In JustInterp mode, only the wrappers and trampolines in System.Private.CoreLib.dll are AOT compiled. This enables interpreter to execute managed methods and P/Invokes for unmanaged callbacks.

Your code in Program.dll likely doesn't contain wrappers and trampolines, which could explain the performance issue. Do you have a specific use-case that require partial AOT compilation?

newNcy commented 6 months ago

The runtime pipeline supports two modes: Full and JustInterp. In Full mode, it AOT compiles all assemblies. In JustInterp mode, only the wrappers and trampolines in System.Private.CoreLib.dll are AOT compiled. This enables interpreter to execute managed methods and P/Invokes for unmanaged callbacks.

Your code in Program.dll likely doesn't contain wrappers and trampolines, which could explain the performance issue. Do you have a specific use-case that require partial AOT compilation?

yup, we plan to use c# as gameplay dev language, and support hotfix on ios, some dll like core library we can accept not to hotfix, and the rest part we need to hotfix, but seems like full aot counld not hotfix cause it requires static-linking

kotlarmilos commented 6 months ago

Partial AOT compilation is available in Maui framework. The runtime infrastructure is designed primarily for testing purposes and is not intended for the deployment of customer-facing apps. Do you have some limitations with Maui framework?

simonrozsival commented 6 months ago

@kotlarmilos if I understand it correctly they are embedding mono in their game so they can't even use MAUI. Should this be achievable by some combination of flags passed to the AOT compiler?

kotlarmilos commented 6 months ago

In this case, we can update our infrastructure to support partial AOT compilation. The MonoAOTCompiler task is already parametrized with Mode property, and the FullInterp mode could be added.

newNcy commented 6 months ago

In this case, we can update our infrastructure to support partial AOT compilation. The MonoAOTCompiler task is already parametrized with Mode property, and the FullInterp mode could be added.

yes we are embedding mono so we cant use MAUI and I have tried the FulInterp mode and it works . thx! So will this be done in AppleBuild.targets or i need to do it my self?

kotlarmilos commented 6 months ago

Do you use runtime infrastructure (MonoAOTCompiler and AppleBuild.targets) for publishing?

newNcy commented 6 months ago

Do you use runtime infrastructure (MonoAOTCompiler and AppleBuild.targets) for publishing?

Maybe not, if we can do it with daily build. use it for prototype testing now

newNcy commented 6 months ago

In this case, we can update our infrastructure to support partial AOT compilation. The MonoAOTCompiler task is already parametrized with Mode property, and the FullInterp mode could be added.

can i use this in mono wasi-wasm? how to pass params to mono-aot-compiler

vitek-karas commented 6 months ago

@lewing not sure I have the necessary knowledge to answer the wasi-wasm part of this question, maybe you'll be able to answer.

newNcy commented 6 months ago

I checked the runtime.c to compile wasi-wasm, found out that only have LLVMONLY/LLVMONLY_INTERP aot mode , but llvm aot can't excluse assembly, is it possible to change to FullInterp + MONO_AOT_MODE_INTERP ? @lewing

kotlarmilos commented 6 months ago

how to pass params to mono-aot-compiler

To run FullInterp mode, the AOT compiler expects --aot=full,interp flags At runtime, the --full-aot-interp mono env variable is used.

The AppleAppBuilder is already parametrized and the AppleBuild.targets can be changed to enable this mode. The new mode can be added in: https://github.com/dotnet/runtime/blob/b42c51612c3c0cadf0b0a52fa0ac8f5f1532e6be/src/mono/msbuild/apple/build/AppleBuild.targets#L147-L150

Then, _AssembliesToBundleInternal can be updated to include specific assemblies in: https://github.com/dotnet/runtime/blob/b42c51612c3c0cadf0b0a52fa0ac8f5f1532e6be/src/mono/msbuild/apple/build/AppleBuild.targets#L187-L202

newNcy commented 6 months ago

how to pass params to mono-aot-compiler

To run FullInterp mode, the AOT compiler expects --aot=full,interp flags At runtime, the --full-aot-interp mono env variable is used.

The AppleAppBuilder is already parametrized and the AppleBuild.targets can be changed to enable this mode. The new mode can be added in:

https://github.com/dotnet/runtime/blob/b42c51612c3c0cadf0b0a52fa0ac8f5f1532e6be/src/mono/msbuild/apple/build/AppleBuild.targets#L147-L150

Then, _AssembliesToBundleInternal can be updated to include specific assemblies in:

https://github.com/dotnet/runtime/blob/b42c51612c3c0cadf0b0a52fa0ac8f5f1532e6be/src/mono/msbuild/apple/build/AppleBuild.targets#L187-L202

thx! Does this work with native aot? A.dll native AOT static linking with executable and the rest DLL will be interpreted

kotlarmilos commented 6 months ago

Does this work with native aot? A.dll native AOT static linking with executable and the rest DLL will be interpreted

The Native AOT does full program analysis and doesn't have partial AOT mode. Please note that it doesn't expose the same API as Mono runtime which may be a limitation in embedded scenarios.

kotlarmilos commented 4 months ago

This issue hasn't been active for a while. I'm closing it now. Please reopen it if you feel more input is needed.