dotnet / corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.

Home Page:http://dot.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

RalfKornmannEnvision opened this issue · comments

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. 

We have an implementation for this here:

#if READYTORUN
NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, (libName, assembly, searchPath) =>
{
IntPtr libHandle = IntPtr.Zero;
if (libName == CorInfoImpl.JitLibrary)
{
if (!string.IsNullOrEmpty(jitPath))
{
libHandle = NativeLibrary.Load(jitPath);
}
else
{
libHandle = NativeLibrary.Load("clrjit-" + GetTargetSpec(target), assembly, searchPath);
}
}
return libHandle;
});
#else
Debug.Assert(jitPath == null);
#endif

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.

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