Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Burst compiler can't do "pointer + -1" or "pointer - -1"

Discussion in 'Burst' started by Zuntatos, May 28, 2020.

  1. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    These two operations are not equivalent when compiled with [BurstCompile]; they are without the attribute.
    pointer = pointer + -1;
    pointer = pointer - 1;
    the same goes for these two:
    pointer = pointer - -1;
    pointer = pointer + 1;

    test code:
    Code (CSharp):
    1.  
    2.         unsafe {
    3.             DebugJob debugJob = new DebugJob();
    4.             System.IntPtr[] ptrs = new System.IntPtr[1000];
    5.             fixed (void* ptr = ptrs) {
    6.                 debugJob.pointers = (int**)ptr;
    7.                 debugJob.Schedule().Complete();
    8.                 Debug.LogWarning($"Ptr 10 -> {ptrs[10].ToInt64()}");
    9.                 Debug.LogWarning($"Ptr 20 -> {ptrs[20].ToInt64()}");
    10.                 Debug.LogWarning($"Ptr 30 -> {ptrs[30].ToInt64()}");
    11.                 Debug.LogWarning($"Ptr 40 -> {ptrs[40].ToInt64()}");
    12.             }
    13.         }
    job code:
    Code (CSharp):
    1.  
    2.     [BurstCompile] public unsafe struct DebugJob : IJob
    3.    {
    4.        [NativeDisableUnsafePtrRestriction] public int** pointers;
    5.  
    6.        [BurstCompile] public unsafe void Execute ()
    7.        {
    8.            pointers[10] = pointers[10] + +1;
    9.            pointers[20] = pointers[20] - +1;
    10.            pointers[30] = pointers[30] - -1;
    11.            pointers[40] = pointers[40] + -1;
    12.        }
    13.    }
    14.  
    When ran, this gives the following result:
    Code (CSharp):
    1. Ptr 10 -> 4
    2. Ptr 20 -> -4
    3. Ptr 30 -> -17179869180
    4. Ptr 40 -> 17179869180
    Without Burst, you get "4, -4, 4, -4" which is the expected result.

    The generated assembly for the above test job is like this:
    Code (CSharp):
    1.         mov        rax, qword ptr [rcx]
    2.         add        qword ptr [rax + 80], 4
    3.         add        qword ptr [rax + 160], -4
    4.         movabs        rcx, -17179869180
    5.         add        qword ptr [rax + 240], rcx
    6.         movabs        rcx, 17179869180
    7.         add        qword ptr [rax + 320], rcx
    I ran into this bug in a project of mine (at https://github.com/pipliz/cpuvox ); there I have a method that is called with a -1 or a 1 via a parameter, which then gets inlined all the way into a "pointer + -1"; which then crashes due to the invalid pointer generated.
     
    richardkettlewell likes this.
  2. Lee_Hammerton

    Lee_Hammerton

    Unity Technologies

    Joined:
    Jul 26, 2018
    Posts:
    118
    Thanks for this, we have logged an issue and it is being investigated.
     
    MNNoxMortem likes this.