KhronosGroup / SPIRV-Cross

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Apache License 2.0
2.02k stars 557 forks source link

[MSL] Incorrect identifier generated for local variable of user-defined type #2128

Closed varunk08 closed 1 year ago

varunk08 commented 1 year ago

The following is the definition of a struct type in the MSL is generated from SPV using spirv-cross

struct PerVertex
{
    float4 v_color;
    float2 v_texPos;
    float3 v_worldPosition;
};

However in the function body which has a local variable of this type there is an identifier name mismatch:

static inline __attribute__((always_inline))
void DrawWorldVS(thread PerVertex& v_14, thread float4& a_color, thread float2& a_texcoord, thread float3& a_position, constant u_objToWorlds& v_39, thread float4& gl_Position, constant SceneSettings& v_64)
{
    v_14.v_color = a_color;
    v_14.v_texPos = a_texcoord;
    v_14.v_worldPosition = a_position;
    float4 worldSpacePosition = v_39.u_objToWorld * float4(a_position, 1.0);
    gl_Position = v_64.u_cameras[0].u_projectFromWorld * worldSpacePosition;
}

vertex main0_out main0(main0_in in [[stage_in]], constant u_objToWorlds& v_39 [[buffer(0)]], constant SceneSettings& v_64 [[buffer(1)]])
{
    main0_out out = {};
    PerVertex v_14 = {};
    DrawWorldVS(v_14, in.a_color, in.a_texcoord, in.a_position, v_39, out.gl_Position, v_64);
    out.m_14_v_color = _14.v_color;
    out.m_14_v_texPos = _14.v_texPos;
    out.m_14_v_worldPosition = _14.v_worldPosition;
    return out;
}

Notice in out.m_14_v_color = _14.v_color;the "v" prefix is missing. This causes the MSL compiler to throw an undeclared identifier error.

error: use of undeclared identifier '_14'; did you mean 'v_14'?
    out.m_14_v_color = _14.v_color;
                       ^~~
                       v_14

If I remove the addition of the "v" prefix in spirv_msl.cpp then the compilation works fine.

set_name(arg_id, ensure_valid_name(to_name(arg_id), "v"));

to

set_name(arg_id, to_name(arg_id));

Any idea why incorrect identifier is being generated after the local variable declaration?

varunk08 commented 1 year ago

Attaching the spirv file spirv_source.zip