KhronosGroup / KhronosGroup/SPIRV-Cross

[MSL] Is it necessary to use `spvUnsafeArray` type for array on the stack?

Open
#2,010 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
GLSL
Stars
2.5k
Forks
713
Avg merge
2d 18h
Merged PRs (30d)
16

Description

For example, use this glsl code to generate SPIR-V for Vulkan with `glslangValidator -V test.frag`:

```glsl
#version 310 es
precision highp float;

layout(location=0) in vec3 color;
layout(location=0) out vec4 fragColor;

void main()
{
vec2 arr[3] = vec2[3](color.xy, color.yz, color.zx);
fragColor = vec4(arr[0], arr[1]);
}
```

Then pass it to SPIRV-Cross with `spirv-cross --msl frag.spv`, this would generate:

```msl
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"

#include
#include

using namespace metal;

template
struct spvUnsafeArray
{
T elements[Num ? Num : 1];

thread T& operator [] (size_t pos) thread
{
return elements[pos];
}
constexpr const thread T& operator [] (size_t pos) const thread
{
return elements[pos];
}

device T& operator [] (size_t pos) device
{
return elements[pos];
}
constexpr const device T& operator [] (size_t pos) const device
{
return elements[pos];
}

constexpr const constant T& operator [] (size_t pos) const constant
{
return elements[pos];
}

threadgroup T& operator [] (size_t pos) threadgroup
{
return elements[pos];
}
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup
{
return elements[pos];
}
};

struct main0_out
{
float4 fragColor [[color(0)]];
};

struct main0_in
{
float3 color [[user(locn0)]];
};

fragment main0_out main0(main0_in in [[stage_in]])
{
main0_out out = {};
spvUnsafeArray _22 = spvUnsafeArray({ in.color.xy, in.color.yz, in.color.zx });
spvUnsafeArray arr;
arr = _22;
out.fragColor = float4(arr[0], arr[1]);
return out;
}
```

The `spvUnsafeArray` type looks unnecessary, the better code would be like:

```msl
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wmissing-braces"

#include
#include

using namespace metal;

struct main0_out
{
float4 fragColor [[color(0)]];
};

struct main0_in
{
float3 color [[user(locn0)]];
};

fragment main0_out main0(main0_in in [[stage_in]])
{
main0_out out = {};
float2 arr[3] = { in.color.xy, in.color.yz, in.color.zx };
out.fragColor = float4(arr[0], arr[1]);
return out;
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the example with glslangValidator and SPIRV-Cross, then trace the MSL code-generation path responsible for local array declarations. Compare the generated output with the requested native MSL array form and verify that the result remains valid for this shader and similar stack arrays.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, devtools
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.