You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用newLibraryWithStitchedDescriptor构建Metal拼接内核触发XPC错误

Metal函数拼接触发XPC_ERROR_CONNECTION_INTERRUPTED内部错误的排查与修复

错误原因分析

你的代码存在多处参数错误与逻辑疏漏,直接导致Metal编译器内部崩溃,触发XPC连接中断错误:

  • 函数名绑定错误:多个MTLFunctionStitchingFunctionNode初始化时传入了错误的函数名(如add节点误用"load"、storeC节点误用"load")
  • 输入参数绑定错误:loadB节点错误绑定了srcA参数,实际应绑定srcB
  • 图节点集合不完整:创建MTLFunctionStitchingGraph时,nodes数组遗漏了storeC节点
  • 库创建参数错误:调用newLibraryWithStitchedDescriptor时错误传入graphDescriptor.functions,需传入完整的graphDescriptor对象
  • 着色器声明错误:two_inputs的第三个参数device *c类型不完整,应为device float *c

修复后的代码

修正后的着色器代码

[[stitchable]] float add(float a, float b) {
    return a + b;
}

[[stitchable]] float load(constant float *a, uint32_t index) {
    return a[index];
}

[[stitchable]] void store(device float *a, float value, uint32_t index) {
    a[index] = value;
}

[[visible]] void two_inputs(constant float *a, constant float *b, device float *c, uint32_t tid);

修正后的Objective-C驱动代码

id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> queue = [device newCommandQueue];

id<MTLLibrary> library = [device newDefaultLibrary];

NSArray *functions = @[
    [library newFunctionWithName:@"add"],
    [library newFunctionWithName:@"load"],
    [library newFunctionWithName:@"store"]
];

MTLFunctionStitchingInputNode *srcA = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:0];
MTLFunctionStitchingInputNode *srcB = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:1];
MTLFunctionStitchingInputNode *srcC = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:2];
MTLFunctionStitchingInputNode *srcI = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:3];

// 修正loadB的绑定参数为srcB
MTLFunctionStitchingFunctionNode *loadA = [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"load" arguments:@[srcA, srcI] controlDependencies:@[]];
MTLFunctionStitchingFunctionNode *loadB = [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"load" arguments:@[srcB, srcI] controlDependencies:@[]];
// 修正add节点的函数名为"add"
MTLFunctionStitchingFunctionNode *add = [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"add" arguments:@[loadA, loadB] controlDependencies:@[]];
// 修正storeC节点的函数名为"store"
MTLFunctionStitchingFunctionNode *storeC = [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"store" arguments:@[srcC, add, srcI] controlDependencies:@[]];

// 修正nodes数组,加入storeC节点
MTLFunctionStitchingGraph *graph = [[MTLFunctionStitchingGraph alloc] initWithFunctionName:@"two_inputs" nodes:@[loadA, loadB, add, storeC] outputNode:storeC attributes:@[]];

MTLStitchedLibraryDescriptor *graphDescriptor = [MTLStitchedLibraryDescriptor new];
graphDescriptor.functions = functions;
graphDescriptor.functionGraphs = @[graph];

NSError *error = NULL;
// 修正传入参数为完整的graphDescriptor
id<MTLLibrary> graphLibrary = [device newLibraryWithStitchedDescriptor:graphDescriptor error:&error];
if (error) {
    NSLog(@"Error: %@", error);
} else {
    NSLog(@"Stitched library created successfully");
}

额外注意事项

  • 确保所有stitchable函数的参数类型、数量与拼接时的调用完全匹配
  • 拼接图的outputNode必须属于图的nodes集合
  • 编译前建议清理项目缓存并重启Xcode,避免缓存异常干扰

内容的提问来源于stack exchange,提问作者user1139069

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 14:25:24