shader-slang / slang

Making it easier to work with shaders
MIT License
1.82k stars 165 forks source link

Invalid spirv generated when conditionally assigning buffer pointers #4111

Closed Ipotrick closed 2 months ago

Ipotrick commented 2 months ago

For the following repro:

struct Buf
{
    uint test;
};

struct Push
{
    bool a_or_b;
    Buf * a;
    Buf * b;
};

[[vk::push_constant]] Push push;

func tester(Buf * buf)
{
    buf->test = 1;
}

[shader("compute")]
[numthreads(1, 1, 1)]
void main()
{
    Buf * b = push.a_or_b ? push.a : push.b;
    tester(b);
}

I get the following validation error:

VUID-VkShaderModuleCreateInfo-pCode-08737(ERROR / SPEC): msgNum: -1520283006 - Validation Error: [ VUID-VkShaderModuleCreateInfo-pCode-08737 ] | MessageID = 0xa5625282 | vkCreateShaderModule(): pCreateInfo->pCode is not valid SPIR-V: OpVariable 8: expected AliasedPointer or RestrictPointer for PhysicalStorageBuffer pointer.
  %b = OpVariable %_ptr_Function_6 Function
. The Vulkan spec states: If pCode is a pointer to SPIR-V code, pCode must adhere to the validation rules described by the Validation Rules within a Module section of the SPIR-V Environment appendix (https://vulkan.lunarg.com/doc/view/1.3.268.0/windows/1.3-extensions/vkspec.html#VUID-VkShaderModuleCreateInfo-pCode-08737)
    Objects: 0

The error vanishes as soon as the conditional assignment for b is removed.

Config:

Slang Version: v2024.1.12
pc platform: x86-64msvc windows11
slang::TargetDesc::format: SlangCompileTarget::SLANG_SPIRV
slang::TargetDesc::profile: glsl_450+spirv_1_4
slang::TargetDesc::flags: SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY
slang::SessionDesc::defaultMatrixLayoutMode: SLANG_MATRIX_LAYOUT_COLUMN_MAJOR
csyonghe commented 2 months ago

Is this with -O0?

Ipotrick commented 2 months ago

yes