Search Unity

Feature Request C# 9.0 Function Pointers

Discussion in 'Burst' started by jasonboukheir, Apr 19, 2021.

  1. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    84
    First off, I want to say I've been keeping an eye on the C# 9 function pointers, and I really appreciate the efforts the scripting team has made so far with its support. I was excited to use them in Burst with the hope that they can be used to overcome the generic delegate limitation when using
    FunctionPointer
    in non-bursted code.

    Currently running Unity 2021.2.0a13 with Burst 1.6.0-pre.2, I am running into
    Burst error BC1054: Unable to resolve type `method System.Int32 *(System.Int32). Reason: Unknown.`


    Here's a snip of the failing Job:
    Code (CSharp):
    1. [BurstCompile]
    2. public unsafe struct CallFuncJob : IJob
    3. {
    4.     [NativeDisableUnsafePtrRestriction]
    5.     public IntPtr FuncPtr;
    6.     public NativeArray<int> In;
    7.     public NativeArray<int> Out;
    8.  
    9.     public void Execute()
    10.     {
    11.         Out[0] = ((delegate* unmanaged[Cdecl]<int, int>)FuncPtr)(In[0]);
    12.     }
    13. }
     
  2. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    84
    Ideally, I hope it can support something like this:

    Code (CSharp):
    1. public unsafe struct BurstFunc<T, TResult>
    2.   where T : struct
    3.   where TResult : struct
    4. {
    5.   readonly delegate* unmanaged[Cdecl]<T, TResult> _ptr;
    6.  
    7.   public BurstFunc(IntPtr ptr)
    8.   {
    9.     _ptr = ptr;
    10.   }
    11.  
    12.   public TResult Invoke(T arg0) => _ptr(arg0);
    13.  
    14.   public static BurstFunc<T, TResult> Compile(Func<T, TResult> func)
    15.   {
    16.     var ptr = BurstCompiler.CompileFunctionPointer(func);
    17.     return new BurstFunc<T, TResult>(ptr.Value);
    18.   }
    19. }
     
  3. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    418
    Yes, function pointers should be fixed/supported for Burst 1.6

    Note that Burst compilation of open generics is not supported for delegate (they are not supported by .NET with interop either see this)
     
    jasonboukheir likes this.
  4. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406