Can't profile Metal shaders within a package

AFAIK, I can't pass compiler options to the Metal compiler from Package.swift, so I can't enable debug symbols for metal and use the shader debugger/profiler on shaders within a Swift Package. So currently, whenever I want to debug/profile my shaders, I move all the relevant code out of the package.

How could this be improved? Xcode seems to pass debug flags to SPM for Swift or ObjC files. I suppose the same could be done for Metal?


1 Like

I have the same problem in Xcode 12. I gave up and use .frameworks instead. In Xcode 13 there are new features for shader compiling and debugging. Now the compiler can generate a .metallibsym file (see here at about 16:45). I have not tried to get this work with Swift Package Manager yet. Has anyone else tried?

We encountered this recently as well and I asked a Swift Package Manager engineer about it during WWDC last week. They confirmed it is not supported.

Our work around is to create a runscript that runs just after the file compilation phase of our project. We do this only when we detect a package that contains metal files has been dragged into our workspace as a local package, thus replacing our production, remote package.

The script finds the local reference path in our XCWorkspace file, finds the metal files in the package at the referenced path and then compiles them like this (I left out the specifics of finding the metal files... this is just the compile and link step for metal):

compile_air() {
        $(xcrun --sdk iphoneos -f metal) -c -target air64-apple-ios13.0 -gline-tables-only -MO \
        -I${OUTPUT_DIR}/include \
        -F${OUTPUT_DIR} \
        -isysroot $(xcrun --show-sdk-path --sdk iphoneos) \
        -ffast-math \
        -o $2 \

//do the compile_air for each metal file

echo "#### LINKING AIR"
$(xcrun --sdk iphoneos -f metal) \
-target air64-apple-ios13.0 \
-MO \
-o ${OUTPUT_DIR}/MyPackage.bundle/default.metallib \
$RENDERER_AIR //can add more air files here if you have them

This overwrites the .metallib generated by SwiftPM with one that can be debugged.

Edit: Also note, the OUTPUT_DIR is passed to the script and is just the Xcode BUILT_PRODUCTS_DIR


That looks like a great work around. I'm able to compile the .air files, but getting this when linking:

air-lld: error: library not found for -lmetal_rt_ios_air2.3

EDIT: got it, had to set SDKROOT

Ah! Yes that makes sense. I did that too, but neglected to paste it in.

Just need export SDKROOT=$(xcrun --show-sdk-path --sdk iphoneos) before the compiler_air step

1 Like

cc @abertelrud

Terms of Service

Privacy Policy

Cookie Policy