22

I am using compiled expressions to create instance. It is very fast in JIT but not in AOT (even slower) because of the fallback process. So I want to check whether the code is running in AOT. If yes, I will use ConstructorInfo.Invoke instead.

ATM, I only have an idea to check this by calling one of the methods are not allowed in AOT and then catch the error. Does any other better ways to check?

shtse8
  • 1,092
  • 12
  • 20
  • does your question refer to ReadyToRun compilation (https://learn.microsoft.com/en-us/dotnet/core/deploying/ready-to-run). So you want to check the current frame if it is native or il code to determine if your compiled expressions or constructorinfo.invoke is used? – Jehof Mar 17 '21 at 12:53
  • @Jehof I am not sure if it is the same. According to the description, R2R is a form of AOT. But does it mean all AOT is R2R? e.g il2cpp, Mono-aot. – shtse8 Mar 17 '21 at 13:14
  • No, not all AOT is R2R. – Jehof Mar 17 '21 at 13:20
  • I am making a library for others. So I think checking R2R can't help because there are different kinds of AOTs. currently I am checking by `Reflection.Emit` to see if there is any exception thrown as AOT cannot allow compile code in runtime. but it's just a trial and error checking. I would like to have a proper way to check if my code is running under AOT. – shtse8 Mar 17 '21 at 13:29
  • Considering the chaos of .NET AOT like prajaybasu mentioned [link](https://github.com/dotnet/runtime/issues/40430#issuecomment-669674196), I think that checking `Reflection.Emit` should be the best way before there is kind of flag to check like `Assembly.CompilationMode == CompilationMode.AOT`. – victor6510 Mar 24 '21 at 05:37
  • I can see there is a flag `GetAotId` in [RuntimeAssembly](https://github.com/mono/mono/blob/466a995cfca6f4494d512340aa65ad755e7046f3/mcs/class/corlib/System.Reflection/RuntimeAssembly.cs#L335). But unluckily, it's internal. I am thinking if there is a way to get this value. – shtse8 Mar 24 '21 at 08:45

2 Answers2

2

Does the System.Runtime.CompilerSerivces.RuntimeFeatures class, with its IsDynamicCodeCompiled and IsDynamicCodeSupported properties, meet the need? It is available in more recent versions of .NET.

  • .NET Core 2.0+
  • .NET Framework 4.7.1+
  • .NET Standard 2.1

 

From the .NET Standard 2.1 Announcement:

We also exposed two new capability APIs that allow you to check for the ability to generate code at all (RuntimeFeature.IsDynamicCodeSupported) as well as whether the generated code is interpreted or compiled (RuntimeFeature.IsDynamicCodeCompiled). This will make it much easier to write libraries that can exploit these capabilities in a portable fashion.

In looking at the .NET source code for Regex, this perhaps might be the check that is triggering the fallback process that was noted, in which case you could then potentially check IsDynamicCodeCompiled yourself and provide your alternate fallback.

mes
  • 86
  • 2
0

From what i can tell, there isn't really a native way to check if you are running in AOT. The method you described of running JIT-only methods and catching the errors would probably be the best way to go. that would probably be how the native method would do it anyway, if one existed.

Base64__
  • 97
  • 8