Open dougallj opened 1 year ago
I've gotten some good results using metal-lipo
and metal-nm
to extract file offsets that can be plugged into disassemble.py
.
#!/bin/sh
HARVESTED_METALLIB_FILE=$1
# The harvested metallib contains both AIR/LLVM and native architecture (ex. "applegpu_g13s") representations.
# List and grab the first architecture that looks native (starts with "applegpu_")
ARCH=$(xcrun metal-lipo -archs "$HARVESTED_METALLIB_FILE" | tr ' ' '\n' | grep "applegpu_" | head -n1)
TMP_ARCH_METALLIB_FILE="/tmp/$ARCH.metallib"
# By extracting the native architecture metallib, metal-nm seems to give accurate file offsets to
# each shader function.
xcrun metal-lipo -thin "$ARCH" "$HARVESTED_METALLIB_FILE" -o "$TMP_ARCH_METALLIB_FILE"
IFS=$'\n'
for i in $(xcrun metal-nm "$TMP_ARCH_METALLIB_FILE" | grep "_agc.main"); do
addr=$(echo "$i" | cut -d' ' -f1)
fn_name=$(echo "$i" | cut -d' ' -f3)
echo "\nFunction: $fn_name"
echo "================================================================="
python3 disassemble.py "$TMP_ARCH_METALLIB_FILE" "0x$addr"
done
I have a terrible mockup of this that I've been using on compute shaders. You shouldn't trust this, but being able to find the code and print names is worthwhile.
(Pre-compilation is now supported, so that's nice: https://developer.apple.com/videos/play/wwdc2022/10102/)
Terrible mockup: