dotnet / corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.
http://dot.net
MIT License
2.91k stars 508 forks source link

Question: Recommended way to add cross target support to the compiler. #8287

Open RalfKornmannEnvision opened 4 years ago

RalfKornmannEnvision commented 4 years ago

If you have one or more RyuJIT variants that are compiled to generated code for a different CPU/target OS the CoreRT compiler is able to do cross compile. I successfully tested this with AMD64/Windows => AMD64/Unix and AMD64/Windows => ARM64/Unix. 

But to make it actually work I ether need to replace clrjitilc with the needed variant or change the JitLibrary constant. Both solutions are not optimal.

I currently see 3 options 

  1. Add DLLImports for all possible variants and choose the right one when RyuJIT needs to be loaded. Easy solution but would require more work every time a new CPU target OS is added.
  2. Load the correct RyuJIT dynamic with LoadLibrary/GetProcAddress (on windows). Very flexible but would require a custom implementation for every host system as the way to load a dynamic lib is different everywhere
  3. Use reflection emit to generate the needed pinvokes. It's flexible and will work on all host systems the same. But will prevent to AOT the compiler if this is planned at some point.

In any case this codepath might only be used when the host and target are different. If they are the same the current solution can still be used. 

MichalStrehovsky commented 4 years ago

We have an implementation for this here:

https://github.com/dotnet/corert/blob/34af164b2faee337942b3980d72b7755f1c7f017/src/JitInterface/src/JitConfigProvider.cs#L45-L64

It's used by the crossgen2 AOT compiler over in the dotnet/runtime repo (crossgen2 targets CoreCLR and generates CoreCLR's ReadyToRun assemblies, which is CoreCLR's AOT technology). CoreRT AOT compiler shares a lot of code with crossgen2.

This code is currently ifdeffed out because the NativeLibrary API was added in .NET Core 3.0 and upgrading the build tooling in this repo to .NET Core 3.0 would be a lot of work.

We'll get this "for free" once the migration to the dotnet/runtimelab repo is completed because that build tooling already does that (hopefully, in the coming weeks). We can just mirror what crossgen2 is doing.

There's also dotnet/runtime#41126 in flight which will make it possible to build crosstargeting RyuJITs by passing flags to the root build script. The runtimelab repo will get that "for free" too.

RalfKornmannEnvision commented 4 years ago

Thanks for the information. In this case I will just use my hack a little bit longer.