Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Detect backend (IL2CPP) or JIT support at runtime

Discussion in 'Scripting' started by Ludiq, Aug 6, 2018.

  1. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    618
    Hi! I have code that works differently whether JIT is available. Basically, it will get optimized if JIT is available.

    Up until Unity 2018.1, I used this code to check at runtime whether JIT was supported:

    Code (CSharp):
    1. public static bool SupportsJit(this RuntimePlatform platform)
    2. {
    3.     return
    4.         platform == RuntimePlatform.WindowsEditor ||
    5.         platform == RuntimePlatform.WindowsPlayer ||
    6.         platform == RuntimePlatform.OSXEditor ||
    7.         platform == RuntimePlatform.OSXPlayer ||
    8.         platform == RuntimePlatform.LinuxEditor ||
    9.         platform == RuntimePlatform.LinuxPlayer;
    10. }
    11.  
    However now that Standalone builds support an IL2CPP backend, it means JIT might not be available on those. But I can't seem to find any way of checking which backend is currently being used (or if JIT is supported) at runtime.

    Note that:
    • I can't check this in the editor, it has to be at runtime
    • I can't use script defines (#if ...) because I'm shipping my code as a DLL
    Am I missing an API method? How can I go about this?
     
  2. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    618
    Bump?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,763
    Hrmm... I don't know if anything exists directly.

    But this does intrigue me. If anyone else knows, please share.

    With that said, tomorrow when I get up I may look into this if I have a spare moment during the day. If one doesn't exist, I'm willing to bet you could cheat a method of determining it. Like maybe attempting to exploit a JIT only feature (maybe emit, implicit generics, or something else?), and if it fails consider yourself in a non-JIT situation and flag some static bool so you only test it once.
     
    Ludiq likes this.
  4. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    618
    Here's my attempt with the try-catch method. It seems to work, but it's a bit hacky:

    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using System.Reflection.Emit;
    4. using UnityEngine;
    5.  
    6. namespace Ludiq
    7. {
    8.     public static class PlatformUtility
    9.     {
    10.         public static readonly bool supportsJit;
    11.  
    12.         static PlatformUtility()
    13.         {
    14.             var isStandalone = Application.platform.IsStandalone();
    15.  
    16.             if (isStandalone)
    17.             {
    18.                 try
    19.                 {
    20.                     AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("EmitCompatibilityCheck"), AssemblyBuilderAccess.Save);
    21.                     supportsJit = true;
    22.                 }
    23.                 catch (Exception)
    24.                 {
    25.                     Debug.Log("JIT has been disabled on this standalone build.");
    26.                 }
    27.             }
    28.         }
    29.        
    30.         public static bool IsStandalone(this RuntimePlatform platform)
    31.         {
    32.             return
    33.                 platform == RuntimePlatform.WindowsEditor ||
    34.                 platform == RuntimePlatform.WindowsPlayer ||
    35.                 platform == RuntimePlatform.OSXEditor ||
    36.                 platform == RuntimePlatform.OSXPlayer ||
    37.                 platform == RuntimePlatform.LinuxEditor ||
    38.                 platform == RuntimePlatform.LinuxPlayer;
    39.         }
    40.     }
    41. }