Search Unity

Burst Compiler Static Machine Code Analysis

Discussion in 'Burst' started by TapVoxel, Feb 24, 2019.

  1. TapVoxel

    TapVoxel

    Joined:
    Dec 28, 2018
    Posts:
    4
    Hi everyone,

    I recently read about the burst compiler in Unity and was intrigued enough to download Unity and start playing with the burst compiler. I happened to read about burst on the same day I was starting to play with llvm-mca (a static machine code analysis tool) to analyze the output of the compiler I work on at work, so I wondered if it would be possible to glue the two together somehow.

    I've spent yesterday evening and today hacking together a simple editor plugin which allows pasting assembly from the burst inspector tool and uses the enhanced disassembly output to map assembly instructions to original C# source lines.

    Static analysis tools are best used to analyze a kernel with no branches, calls or other conditionals. To support this, I've added a begin and end marker function call which will show up in the assembly output by the burst inspector and can be used to feed the correct instruction block to llvm-mca for analysis.

    Since this is a quickly hacked together static analysis tool, there are a number of limitations:

    • Loops will only be analyzed as if they ran a single time (static analysis doesn't "see" jump instructions)
    • Conditional branches are currently ignored and the contents are not analyzed
    • Calls are not analyzed and are currently ignored, counted as executing in zero cycles
    • Static analysis of code with optimizations turned on is not currently supported due to the difficulty of mapping optimized assembly back to original C# source lines
    As a way of testing the integration, I’ve written a quick hydraulic+thermal terrain erosion algorithm as a burst job. This algorithm has a lot of branches and conditionals, so it’s not ideal for static analysis, but did help with seeing how the burst compiler output code and reconstruct just enough of a control flow graph to allow analyzing an algorithm like this with llvm-mca.

    Analyzing the burst compiler's output with llvm-mca turned out to be an interesting challenge because llvm-mca doesn't understand everything in the assembly output, so I ended up having to "sanitize" the assembly in ways that didn't affect analysis before feeding it to llvm-mca. Writing an editor plugin also turned out to be interesting in getting enough performance in the timeline view to enable smooth scrolling and display of possibly a few thousand instructions.

    I’m very new to Unity and the burst compiler, so I don’t know if anyone else would find a tool like this useful despite its limitations. I’ll probably continue to work on this project in my very limited spare time, but if there is any interest, my intention is to put it up on GitHub as an open source project once it’s cleaned up enough to work anywhere besides my own machine. Let me know if a tool like this would be useful to you.

    Oh, and I should also note that currently the tool only works on Windows.


    erosion_screenshot.jpg erode_timeline.png erode_profile.png erode_detailed_report.png
     
    Recart, Rewaken, GilCat and 6 others like this.
  2. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
  3. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    I guess long term unity is planning on exposing a lot more resources on improving burst compiled code, but in the mean time this would be really useful. Looking forward to checking it out!
     
  4. TapVoxel

    TapVoxel

    Joined:
    Dec 28, 2018
    Posts:
    4
    @sngdan @Carpet_Head Great! I'll get this posted on GitHub soon (probably next weekend, I'm traveling today). I do have a 2013 MacBook Pro that I can use for testing and I'd definitely like to get it working there also.

    Hopefully it will be useful to both of you even in its current state. I'm getting up to speed on C# and had no particular plan during my 12 hour personal hackathon, so the code is a mess, but still kinda sorta works. :) I'll be cleaning it up over the next few weekends and getting it running on a Mac as well. There's nothing special about the code that would keep it from running on other platforms, I just need to get a Unity environment set up on Mac (and I'll try out Linux as well) and get llvm-mca compiled for those platforms.

    I'll post further updates here.
     
  5. TapVoxel

    TapVoxel

    Joined:
    Dec 28, 2018
    Posts:
    4
    Hi everyone,

    I apologize for the lack of updates. I just worked 7 days straight and haven't had a chance to do the code cleanup or any portability work for Mac or Linux.

    However, as promised, I'm making the tool available for Windows users on Github: https://github.com/kcers/coda

    Please try it out and let me know if it works and is useful to you. Bug reports and PRs are welcome!
     
    Deleted User and SugoiDev like this.
  6. TapVoxel

    TapVoxel

    Joined:
    Dec 28, 2018
    Posts:
    4
    @sngdan Thanks for the link! I'd assumed the Unity team was working on something, but I wanted something to give me a little more visibility into what the burst compiler was doing for now. Since I'd recently started playing with llvm-mca, this personal hackathon project was born.

    Hopefully it will be of some use to others until the official analysis toolset is released.
     
  7. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    I think your efforts are fantastic - who knows when/if Unity will provide something in this area.

    I hope you get a chance to port this to Mac - what part is actually Windows specific?
     
  8. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    418
    @kcers nice work! We have actually a plan to integrate llvm-mca as part of burst inspector analysis, so it's great to see that you were able to prototype something with burst.
     
  9. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    I tried to get this working, but can't seem to track down a compiled binary of llvm-mca, could you include it? or provide instructions?