This project demonstrates the metal-source: error: unsupported binary format error when trying to use metal-source on a harvested GPU archive.
Background
The WWDC 2022 session Target and optimize GPU binaries with Metal 3 suggests the metal-source tool can generate the JSON Pipeline Scripts.
Using the session's (5:55) command line directions:
> metal-source -flatbuffers=json harvested-binaryArchive.metallib -o /tmp/descriptors.mtlp-jsonReproduction Overview
Running this project...
- Displays XCode environment and version information
# Runs the following shell commands xcrun --show-sdk-path xcrun xcodebuild -version - Create a simple render pipeline
let device = MTLCreateSystemDefaultDevice()! let commandQueue = device.makeCommandQueue()! let lib = try device.makeLibrary( source: """ #include <metal_stdlib> using namespace metal; struct VertexOut { float4 position [[position]]; float point_size [[point_size]]; }; [[vertex]] VertexOut main_vertex() { return { .position = float4(0), .point_size = 128.0 }; } [[fragment]] half4 main_fragment() { return half4(1); } """, options: nil ) let pipelineDesc = MTLRenderPipelineDescriptor() pipelineDesc.vertexFunction = lib.makeFunction(name: "main_vertex") pipelineDesc.fragmentFunction = lib.makeFunction(name: "main_fragment") pipelineDesc.colorAttachments[0]?.pixelFormat = .bgra8Unorm
- Harvest GPU Archive
let archivePath = NSTemporaryDirectory().appending("harvested-archive.metallib") let archiveDesc = MTLBinaryArchiveDescriptor() let archive = try device.makeBinaryArchive(descriptor: archiveDesc) try archive.addRenderPipelineFunctions(descriptor: pipelineDesc) try archive.serialize(to: NSURL.fileURL(withPath: archivePath)) print("Created archive: \(archivePath)")
- Verify/Display Information about GPU Archive
# Runs the following shell command xcrun metal-readobj harvested-archive.metallib - Attempt to use
metal-sourceto get pipeline descriptor# Runs the following shell command xcrun metal-source -flatbuffers=json harvested-archive.metallib -o descriptors.json
Example output showing error
Environment
- MacBook Pro 2021 M1 Max
- macOS Version 13.0 Beta 22A5266r
- Xcode Version 14.0 beta 14A5228
-----------
Environment
-----------
Command: xcrun --show-sdk-path
Result: /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
Command: xcrun xcodebuild -version
Result: Xcode 14.0
Build version 14A5229c
------------------------
Creating Render Pipeline
------------------------
2022-06-23 11:43:39.920133-0500 x-metal-source-on-harvested-gpu-archive[8264:109027] Metal GPU Frame Capture Enabled
2022-06-23 11:43:39.920532-0500 x-metal-source-on-harvested-gpu-archive[8264:109027] Metal API Validation Enabled
----------------------
Harvesting GPU Archive
----------------------
Created archive: /var/folders/bd/9qd81pgj4xj01bg4sgp43dvr0000gn/T/harvested-archive.metallib
--------------------------------------------
Verify/Display Information about GPU Archive
--------------------------------------------
Command: xcrun metal-readobj /var/folders/bd/9qd81pgj4xj01bg4sgp43dvr0000gn/T/harvested-archive.metallib
Result: 2022-06-23 11:43:39.980679-0500 xcrun[8269:109521] Failed to open macho file at /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/metal-readobj for reading: Too many levels of symbolic links
File: /var/folders/bd/9qd81pgj4xj01bg4sgp43dvr0000gn/T/harvested-archive.metallib
Format: MetalLib
Arch: air64
AddressSize: 64bit
File: /var/folders/bd/9qd81pgj4xj01bg4sgp43dvr0000gn/T/harvested-archive.metallib
Format: Mach-O 64-bit Apple GPU
Arch: agx2
AddressSize: 64bit
---------------------------------------------
Using metal-source to get pipeline descriptor
---------------------------------------------
Command: xcrun metal-source -flatbuffers=json /var/folders/bd/9qd81pgj4xj01bg4sgp43dvr0000gn/T/harvested-archive.metallib -o /var/folders/bd/9qd81pgj4xj01bg4sgp43dvr0000gn/T/descriptors.json
Result: metal-source: error: unsupported binary format
Program ended with exit code: 0