<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLFunctionStitchingGraph",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(cs)MTLFunctionStitchingGraph"
  },
  "title" : "MTLFunctionStitchingGraph"
}
-->

# MTLFunctionStitchingGraph

A description of a new stitched function.

```
class MTLFunctionStitchingGraph
```

## Overview

An [`MTLFunctionStitchingGraph`](/documentation/Metal/MTLFunctionStitchingGraph) instance describes the function graph for a stitched function. A *stitched function* is a visible function you create by composing other Metal shader functions together in a function graph. A function stitching graph contains nodes for the function’s arguments and any functions it calls in the implementation. Data flows from the arguments to the end of the graph until the stitched function evaluates all of the graph’s nodes.

The graph in the figure below constructs a new function that adds numbers from two source arrays, storing the result in a third array. The function’s parameters are pointers to the source and destination arrays, and an index for performing the array lookup. The graph uses three separate MSL functions to construct the stitched function: a function to look up a value from an array, a function that adds two numbers together, and a function that stores a value to an array.

![A function graph with four columns. The first column shows the function’s arguments, which consist of two source arrays, an index, and a destination array. The second column shows two function calls to look up numbers in the source arrays in the first column. The third column shows a function call to add the numbers, and the final column calls a function to store the sum to the destination array in the first column.](images/com.apple.metal/MTLFunctionStitchingGraph-1@2x.png)

Create an [`MTLFunctionStitchingGraph`](/documentation/Metal/MTLFunctionStitchingGraph) instance for each stitched function you want to create. Configure its properties to describe the new function and the nodes that define its behavior, as described below. To create a new library with these stitched functions, see [`MTLStitchedLibraryDescriptor`](/documentation/Metal/MTLStitchedLibraryDescriptor).

### Configuring a function stitching graph

To create a valid stitched function, the function stitching graph and shader code need to meet some requirements:

- Implement the MSL functions that you use to create the new function, adding the `stitchable` attribute to each. Stitchable functions are visible functions that you can also use in a function graph. Stitchable functions may require the compiler to do additional work or emit larger instance code, so mark functions as stitchable only when necessary.
- Declare the stitched function’s name and signature in a header file to include in any shader code that calls the new function. Alternatively, you can add the function to a function table with a matching type and pass the function table as an argument.
- Create an [`MTLFunctionStitchingInputNode`](/documentation/Metal/MTLFunctionStitchingInputNode) node for each of the function’s arguments, specifying which parameter each node references. The output type of each input node is the type of that parameter in your function signature.
- Create an [`MTLFunctionStitchingFunctionNode`](/documentation/Metal/MTLFunctionStitchingFunctionNode) for each function the implementation calls. A function node’s output type is the return type of the MSL function.
- Make sure the output types of each node match the types of the node they pass to. For example, if a function takes a `float` parameter, the node that provides that data need to output a `float` value. If you don’t match the types correctly, Metal doesn’t define the behavior of the resulting function.
- Create an array from the node instances and assign it to the [`nodes`](/documentation/Metal/MTLFunctionStitchingGraph/nodes) property.
- If the function produces an output, create another node and assign it to the [`outputNode`](/documentation/Metal/MTLFunctionStitchingGraph/outputNode) property. The output type of this node needs to match the function’s return type. Don’t include this node in the array of nodes you assign to the [`nodes`](/documentation/Metal/MTLFunctionStitchingGraph/nodes) property.

The MSL code below implements the functions in the example graph above, as well as the function’s signature:

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

[[stitchable]] float lookup(const constant float *a, uint index)
{
    return a[index];
}

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

// The output function declaration.
[[visible]] void add_arrays(constant float *a, constant float *b, device float*c, uint tid);
```

The following code creates the graph above:

```objective-c
// Load the functions from the library.
NSArray *functions = @[
    [library newFunctionWithName:@"add"],
    [library newFunctionWithName:@"lookup"],
    [library newFunctionWithName:@"store"],
];

// Create nodes for the input parameters.
MTLFunctionStitchingInputNode *srcA = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:0];
MTLFunctionStitchingInputNode *srcB = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:1];
MTLFunctionStitchingInputNode *dest = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:2];
MTLFunctionStitchingInputNode *index = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:3];

// Create nodes for the functions.
MTLFunctionStitchingFunctionNode *lookup_a =
    [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"read" arguments:@[srcA, index] controlDependencies:@[]];
MTLFunctionStitchingFunctionNode *lookup_b =
    [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"read" arguments:@[srcB, index] controlDependencies:@[]];
MTLFunctionStitchingFunctionNode *sum =
    [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"add" arguments:@[lookup_a, lookup_b] controlDependencies:@[]];
MTLFunctionStitchingFunctionNode *store =
    [[MTLFunctionStitchingFunctionNode alloc] initWithName:@"store" arguments:@[sum, dest, index] controlDependencies:@[]];

// Create the stitching graph.
MTLFunctionStitchingGraph *graph =
    [[MTLFunctionStitchingGraph alloc] initWithFunctionName:@"add_arrays" nodes:@[lookup_a, lookup_b, sum, store]
                                                 outputNode:nil attributes:@[]];
```

## Topics

### Initializing a function graph

[`init(functionName:nodes:outputNode:attributes:)`](/documentation/Metal/MTLFunctionStitchingGraph/init(functionName:nodes:outputNode:attributes:))

Creates a description of a new function call graph.

### Configuring a function graph

[`functionName`](/documentation/Metal/MTLFunctionStitchingGraph/functionName)

The name of the new stitched function.

[`nodes`](/documentation/Metal/MTLFunctionStitchingGraph/nodes)

The nodes in the function’s call graph.

[`outputNode`](/documentation/Metal/MTLFunctionStitchingGraph/outputNode)

The node with the output that’s the output of the new stitched function.

[`attributes`](/documentation/Metal/MTLFunctionStitchingGraph/attributes)

A list of attributes to configure how the Metal device object generates the new stitched function.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)