dotnet / runtime

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

some quesion about aot-interp mix execute mode on ios #97610

Open srxqds opened 5 months ago

srxqds commented 5 months ago

I found in interp_jit_call_can_be_supported method https://github.com/dotnet/runtime/blob/c987e4f902859d4f80a24b45c5b2a7e4222b6c0e/src/mono/mono/mini/interp/interp.c#L8641

the generic method only support aot in llvmonly?

we want to use aot-interp mode, full aot System.Private.CoreLib.dll, and use interp for our code.

  1. what params should i use to build aot file
  2. what excecute mode should I set? MONO_AOT_MODE_INTERP will not enable llvmonly ?

how can I did it?

@vargaz @BrzVlad

srxqds commented 5 months ago

and this lines set MONO_OPT_GSHAREDVT option are conflict: https://github.com/dotnet/runtime/blob/91beecdc32a893a7e2a16e4026dea7985c3aac1d/src/mono/mono/mini/aot-compiler.c#L14984-L15003

srxqds commented 5 months ago

I have try to use llvm to not

/monoinstaller/bin/mono-aot-cross -O=gsharedvt,float32 --debug --llvm --aot=mtriple=arm64-ios,static,dwarfdebug,mattr=+crc,direct-icalls,deterministic,nimt-trampolines=2000,ntrampolines=10000,nrgctx-fetch-trampolines=256,ngsharedvt-trampolines=4400,nftnptr-arg-trampolines=4000,nrgctx-trampolines=21000,nodebug,full,interp,asmonly,internal-logfile=/monoinstaller/log.txt,dump,data-outfile=/Users/game-netease/monoinstaller/native/System.Private.CoreLib.aotdata,llvm-path=/monoinstaller/bin,outfile=/monoinstaller/native/System.Private.CoreLib.dll.s,llvm-outfile=/monoinstaller/native/System.Private.CoreLib.dll-llvm.o /monoinstaller/native/System.Private.CoreLib.dll

and use lib tool to generate static library:

libtool -static -arch_only arm64 -o /monoinstaller/native/System.Private.CoreLib.dll.a /monoinstaller/native/System.Private.CoreLib.dll-llvm.o

it will generate System.Private.CoreLib.dll.a success, but it error on link:

ld: warning: no platform load command found in '/Users/game-netease/dotnet/benchmarks/runmonoonios/mono-8.0.0/native/System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)', assuming: iOS
ld: Undefined symbols:
  _mono_aot_corlibglobals, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibjit_code_end, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibjit_code_start, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibjit_got, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibmethod_addresses, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibplt, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibplt_end, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibunbox_trampoline_addresses, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibunbox_trampolines, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
  _mono_aot_corlibunbox_trampolines_end, referenced from:
      _mono_aot_file_info in System.Private.CoreLib.dll.a[2](System.Private.CoreLib.dll-llvm.o)
srxqds commented 5 months ago

System.Private.CoreLib.zip

this is the System.Private.CoreLib.dll.a and System.Private.CoreLib.dll.aotdata files

srxqds commented 5 months ago

and if replace aot mode with llvmonly

/monoinstaller/bin/mono-aot-cross -O=gsharedvt,float32 --debug --llvm --aot=mtriple=arm64-ios,static,dwarfdebug,mattr=+crc,direct-icalls,deterministic,nimt-trampolines=2000,ntrampolines=10000,nrgctx-fetch-trampolines=256,ngsharedvt-trampolines=4400,nftnptr-arg-trampolines=4000,nrgctx-trampolines=21000,nodebug,llvmonly,asmonly,internal-logfile=/monoinstaller/log.txt,dump,data-outfile=/Users/game-netease/monoinstaller/native/System.Private.CoreLib.aotdata,llvm-path=/monoinstaller/bin,outfile=/monoinstaller/native/System.Private.CoreLib.dll.s,llvm-outfile=/monoinstaller/native/System.Private.CoreLib.dll-llvm.o /monoinstaller/native/System.Private.CoreLib.dll

it don't generate the System.Private.CoreLib.dll.s file, and can't found any error in log.txt file

srxqds commented 5 months ago

@vargaz @BrzVlad could you have time to help?

we want to use aot generic method such as Dictionary<int, int>, Dictionary<int, string> in the interp assembly.

for example

// game.dll

class GameLogic
{
public void Test()
{
Dictionary<int, int> a = new Dictionary<int, int>();
a.Add(1, 2);  
}
}

I want to use interp mode to excecute game.dll and aot the System.Private.CoreLib.dll, so it can find in aot method. because the dictionary operator (add element, or iterator) are slow, we want use aot to speed up.

how can I make it work?

BrzVlad commented 5 months ago

I would first try to get things going without llvm, it might be a bit easier. The important flags to use are --aot=full,interp for aot compilation of SPC.dll, and --full-aot-interp when running (MONO_AOT_MODE_INTERP). In this scenario, only methods that are not found in aot images are interpreted. I'm not familiar with the toolchain setup when using mono-aot-cross, might want to replicate what our build tasks for iOS are doing.

srxqds commented 5 months ago

I would first try to get things going without llvm, it might be a bit easier. The important flags to use are --aot=full,interp for aot compilation of SPC.dll, and --full-aot-interp when running (MONO_AOT_MODE_INTERP). In this scenario, only methods that are not found in aot images are interpreted. I'm not familiar with the toolchain setup when using mono-aot-cross, might want to replicate what our build tasks for iOS are doing.

yes, if not use llvm, it can work, but the generic method can't use aot method because it will skip by interp_jit_call_can_be_supported https://github.com/dotnet/runtime/blob/c987e4f902859d4f80a24b45c5b2a7e4222b6c0e/src/mono/mono/mini/interp/interp.c#L8641

this line said the generic method only work in llvmonly

BrzVlad commented 5 months ago

I see, it seems that we haven't yet implemented passing of the generic context in the non-llvmonly case, when transitioning from interpreter. I'm not really familiar with running in llvmonly mode, but I would have assumed that passing similarly the llvmonly flag should have done the trick.

srxqds commented 5 months ago

yes, I have try it, there are many mono_llvm_only conditional statements, I just comment out this line https://github.com/dotnet/runtime/blob/c987e4f902859d4f80a24b45c5b2a7e4222b6c0e/src/mono/mono/mini/interp/interp.c#L8641

but I will crash

* thread #1, name = 'tid_103', queue = 'com.apple.main-thread', stop reason = signal SIGSEGV
    frame #0: 0x0000000102d5cad8 RunMonoOniOS`System_Collections_Generic_Dictionary_2_TKey_GSHAREDVT_TValue_GSHAREDVT__ctor_int_System_Collections_Generic_IEqualityComparer_1_TKey_GSHAREDVT + 72
    frame #1: 0x000000010329583c RunMonoOniOS`wrapper_other_object_gsharedvt_out_sig_int__object__intptr + 60
  * frame #2: 0x000000010249efe0 RunMonoOniOS`do_jit_call(context=0x0000000282e308c0, ret_sp=0x0000000108b58090, sp=0x0000000108b58090, frame=0x000000016dc762e0, rmethod=0x00000001058cf430, error=0x000000016dc76368) at interp.c:2745:3 [opt]
    frame #3: 0x0000000102494bd0 RunMonoOniOS`mono_interp_exec_method(frame=<unavailable>, context=0x0000000282e308c0, clause_args=<unavailable>) at interp.c:4313:4 [opt]
    frame #4: 0x0000000102492444 RunMonoOniOS`interp_runtime_invoke(method=<unavailable>, obj=0x0000000000000000, params=0x0000000000000000, exc=<unavailable>, error=0x000000016dc76fb0) at interp.c:2109:2 [opt]
    frame #5: 0x000000010245cae0 RunMonoOniOS`mono_jit_runtime_invoke(method=0x00000001058a6e50, obj=<unavailable>, params=<unavailable>, exc=<unavailable>, error=0x000000016dc76fb0) at mini-runtime.c:3672:12 [opt]
    frame #6: 0x00000001023fdb74 RunMonoOniOS`mono_runtime_try_invoke [inlined] do_runtime_invoke(method=0x00000001058a6e50, obj=0x0000000000000000, params=0x0000000000000000, exc=0x000000016dc76a38, error=0x000000016dc76fb0) at object.c:2578:11 [opt]
    frame #7: 0x00000001023fdb38 RunMonoOniOS`mono_runtime_try_invoke(method=0x00000001058a6e50, obj=0x0000000000000000, params=0x0000000000000000, exc=0x000000016dc76a38, error=0x000000016dc76fb0) at object.c:2735:9 [opt]
    frame #8: 0x00000001023fc72c RunMonoOniOS`mono_runtime_class_init_full(vtable=0x00000001058c4568, error=0x000000016dc76fb0) at object.c:553:3 [opt]
    frame #9: 0x000000010245ce14 RunMonoOniOS`mono_jit_runtime_invoke(method=0x00000001058a6df0, obj=<unavailable>, params=0x000000016dc76f50, exc=0x0000000000000000, error=0x000000016dc76fb0) at mini-runtime.c:3554:7 [opt]
    frame #10: 0x00000001023fc068 RunMonoOniOS`mono_runtime_invoke_checked [inlined] do_runtime_invoke(method=0x00000001058a6df0, obj=0x0000000000000000, params=0x000000016dc76f50, exc=0x0000000000000000, error=0x000000016dc76fb0) at object.c:2578:11 [opt]
    frame #11: 0x00000001023fc02c RunMonoOniOS`mono_runtime_invoke_checked(method=0x00000001058a6df0, obj=0x0000000000000000, params=0x000000016dc76f50, error=0x000000016dc76fb0) at object.c:2794:9 [opt]
    frame #12: 0x0000000102402db4 RunMonoOniOS`mono_runtime_exec_main_checked [inlined] do_exec_main_checked(method=0x00000001058a6df0, args=<unavailable>, error=0x000000016dc76fb0) at object.c:0 [opt]
    frame #13: 0x0000000102402d78 RunMonoOniOS`mono_runtime_exec_main_checked(method=0x00000001058a6df0, args=<unavailable>, error=0x000000016dc76fb0) at object.c:4781:9 [opt]
    frame #14: 0x0000000102402e60 RunMonoOniOS`mono_runtime_run_main_checked(method=<unavailable>, argc=<unavailable>, argv=<unavailable>, error=<unavailable>) at object.c:4345:9 [opt] [artificial]
    frame #15: 0x0000000102463ab8 RunMonoOniOS`mono_jit_exec at driver.c:1369:13 [opt]
    frame #16: 0x0000000102463aa8 RunMonoOniOS`mono_jit_exec(domain=<unavailable>, assembly=<unavailable>, argc=1, argv=0x000000016dc77210) at driver.c:1314:7 [opt]
    frame #17: 0x000000010218cc58 RunMonoOniOS`TestEntry(argc=2, argv=0x0000000280b38350) at TestInterop.Mono.cpp:280:5
    frame #18: 0x0000000102193228 RunMonoOniOS`static RunMonoOniOSApp.main(self=RunMonoOniOS.RunMonoOniOSApp) at RunMonoOniOSApp.swift:22:9
    frame #19: 0x00000001021937dc RunMonoOniOS`static RunMonoOniOSApp.$main(self=RunMonoOniOS.RunMonoOniOSApp) at <compiler-generated>:0
    frame #20: 0x000000010219381c RunMonoOniOS`main at RunMonoOniOSApp.swift:11:8
    frame #21: 0x00000001e44a9df0 dyld`start + 2096

I don't know what wrong with it. There should be other places that need to be modified as well.

srxqds commented 5 months ago

@vargaz may you give me some help?