Search Unity

Detect backend (IL2CPP) or JIT support at runtime

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

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    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. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    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.
     
    LazloBonin likes this.
  4. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    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. }
     
    Max-Bot likes this.
  5. Moe_Baker

    Moe_Baker

    Joined:
    Oct 22, 2017
    Posts:
    36
    Such a small feature yet it still doesn't exist
     
  6. imaxs

    imaxs

    Joined:
    Oct 31, 2020
    Posts:
    14
    Code (CSharp):
    1.     public static class IL2CPP
    2.     {
    3.         public static bool IsEnabled
    4.         {
    5.             get
    6.             {
    7. #if ENABLE_IL2CPP
    8.                 return true;
    9. #else
    10.                 return false;
    11. #endif
    12.             }
    13.         }
     
    JakubNei, LaireonGames and Moe_Baker like this.
  7. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    They were specifically talking about runtime checking, though