Xcode 13 metal compiler crash with `inline` functions in Core Image kernels

Hello,

The metal compiler is crashing for me when attempting to compile a metal source file that contains Core Image kernel implementations. This is a minimal version of a file that produces the crash:

extern "C" { namespace coreimage {
    inline void swap(thread float4 &a, thread float4 &b) {
        float4 tmp = a;
        a = min(a, b);
        b = max(tmp, b);
    }

    typedef sample_t s;
    
    float4 median_reduction_3(s v0, s v1, s v2) {
        swap(v1, v2); swap(v0, v2); swap(v0, v1);
        return v1;
    }     
}}

Some observations:

  • If inline is removed, the code compiles fine. I'm not sure if there's a performance impact, as the backend llvm compiler might as well decide to inline it on its own.
  • If the calls to swap are commented in the median reduction function, the code compiles.
  • If the -fcikernel compilation flag is not used, it also compiles fine (doesn't crash). Of course, that configuration doesn't allow the use of functions inside the file as Core Image kernels.

I'm using the build settings recommended in this WWDC20 session (without indicating the location of the header files, since it's empty in my project and the new compiler interprets the argument following -I as a directory).

Answered by Graphics and Games Engineer in 695650022

Thank you for filing the Feedback Assistant case. Please try again with Xcode 13.2 beta 2 (at https://developer.apple.com/download/ ), where the compiler crash has been fixed. Note that extern "C" inline functions will not be exported as cikernels.

We checked the AIR output of the code posted above and it looks like the swap function gets inlined by the Metal Frontend compiler regardless of whether the inline keyword is present. I realize you've posted a simplified case however, so the compiler may behave differently depending on the code you've written.

Please create a request via Feedback Assistant and repost the number you get back. If you can include the shading language code you're working with, we can give it a try to determine if the inline keyword has any impact. At the very least we can use that Feedback Assistant request to track a fix in the Metal Compiler.

Thanks for taking a look! I submitted FB9588695 yesterday, and I just attached the full source of the Metal file.

Accepted Answer

Thank you for filing the Feedback Assistant case. Please try again with Xcode 13.2 beta 2 (at https://developer.apple.com/download/ ), where the compiler crash has been fixed. Note that extern "C" inline functions will not be exported as cikernels.

Thank you, I can confirm it's fixed in Xcode 13.2 beta 2.

Xcode 13 metal compiler crash with `inline` functions in Core Image kernels
 
 
Q