Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

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

Open
RalfKornmannEnvision opened this issue Aug 27, 2020 · 2 comments

Comments

@RalfKornmannEnvision
Copy link
Contributor

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
Copy link
Member

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.

@RalfKornmannEnvision
Copy link
Contributor Author

RalfKornmannEnvision commented Aug 27, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants