shader-slang / slang

Making it easier to work with shaders
MIT License
2.06k stars 177 forks source link

spirv_asm blocks can introduce duplicate types #5007

Open expipiplus1 opened 4 weeks ago

expipiplus1 commented 4 weeks ago

If our program contains somewhere the type, for example, T[], and a spirv block contains $$T[] then we don't deduplicate these.

expipiplus1 commented 3 weeks ago

This happens because we emit an array type with an explicit stride for StructuredBuffers, and without for the $$T[] snippet.

Reproducer:

//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-vk

// CHECK: type: int32_t
// CHECK-NEXT: 0
// CHECK-NEXT: 1
// CHECK-NEXT: 2
// CHECK-NEXT: 3

//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer;

T foo<T>(RWStructuredBuffer<T> b)
{
    let zero : int = 0;
    return spirv_asm
    {
        %ptrType = OpTypePointer StorageBuffer $$T[];
        %ptr:%ptrType = OpAccessChain $b $zero;
        %ptr1Type = OpTypePointer StorageBuffer $$T;
        %ptr1:%ptr1Type = OpAccessChain %ptr $zero;
        result:$$T = OpLoad %ptr1;
    };
}

[numthreads(4, 1, 1)]
void computeMain(uint i : SV_GroupIndex)
{
    let xs : int[4] = {1,2,3,4};
    outputBuffer[i] = foo(outputBuffer);
}