Search Unity

Discussion Inline extern calls?

Discussion in 'Burst' started by Deleted User, Oct 14, 2022.

  1. Deleted User

    Deleted User

    Guest

    Hi I know nothing about compilers. However, I am really liking the Zig programming language.

    Given an extern native function call in a C# function, what would it take to inline that extern call in the Bursted version of that function?

    It makes sense to me that IL2CPP can do this with cpp source files. Can Burst do this with LLVM IR?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,956
    This may give you some insight as to P/Invoke from Burst:
    https://www.jacksondunstan.com/articles/5341

    This article is 3 years old and it would need to be verified that P/Invoke is still possible with today's Burst. Regardless of that, the point of Burst is to compile the code for you and optimize it. Any P/Invoke method will not only cross language boundaries which carries a non-negligible overhead, it will also be running non-Burst code because that code has already been compiled with another compiler. There goes most of the benefit of using Burst out the window.

    Generally speaking, it's almost always a terrible idea to use framework A (here: Unity) but program it in unsupported language B (here: Zig) because it's just going to be a major clusterf*** of madness. Okay, I may be overstating, but only slightly. The fact that you're not using the tools native to the framework has usually a much higher cost associated with it than anyone naive enough trying to do so isn't seeing or feeling yet. If only for being alone out there. You'd literally be the person on Mars trying to call earth to send you instructions on how to operate your base where no one even knows what kind of base you're living in and how it operates. Do you want to be in this place? ;)

    Of course, this is still valid if you like to be a pioneer and risk everything just to make a learning experience only few could attempt. That's totally okay, just don't expect to build production code with Zig let alone publish a game.

    Btw, "inline extern" to my knowledge is a set of keywords that only work in the C/C++ languages, and the inline part is merely a suggestion, while extern simply states "compiled elsewhere" and the linker will take care of injecting that method call. It's more of a hint for the compiler to know that "yes, this method is missing in this source file but ignore that, it'll be okay". This only works within the C/C++ language compiler/linker toolchain and cannot be applied across language boundaries.
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,266
    I'm not 100% certain on this, but it might be possible to get such inlining to work if you compile your zig library or whatever into a static library. (.lib on Windows, .a everywhere else)

    I can't speak for other languages, but at least for C and C++, Burst does a great job eliminating the cross-language overhead calling functions into other libraries. I'm using an animation compression library as a native plugin, and Burst calls into that the same way it calls into a heavy memcpy or other Unity-provided function that doesn't get inlined. As long as the work inside of the function is sufficient, the overhead of the function call is negligible.
     
    Deleted User likes this.