microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
3.09k stars 690 forks source link

Sema should not allow assignment to resource #3726

Open jaebaek opened 3 years ago

jaebaek commented 3 years ago

DXIL backend reports error if the code has assignments to resources e.g.,

Texture2D<float4>               r0;
SamplerState                    r1;
RWByteAddressBuffer             r2;

Texture2D<float4>               x0;
SamplerState                    x1;
RWByteAddressBuffer             x2;

void getResource(out    Texture2D<float4>               a0,
                 out    SamplerState                    a1,
                 out    RWByteAddressBuffer             a2)
{
    a0 = r0;
    a1 = r1;
    a2 = r2;
}

float4 main(): SV_Target
{
    getResource(x0, x1, x2);
    return x0.Sample(x1, float2(x2.Load(0), x2.Load(1)));
}

To enable the error report for the SPIR-V backend, we need a SPIR-V analysis to check if the code conducts OpStore to a resource because it is not trivial to check the assignment by checking only a0 = r0. We have no idea a0 is a reference to a global resource or not.

Unfortunately, the spirv-opt legalization handles the code and somehow generates the legal code: x.legal.txt from the initial code generated by DXC: x.txt

To fix this issue, I guess we need a sophisticated handling in spirv-val ...

jaebaek commented 3 years ago

This is the first issue mentioned by https://github.com/microsoft/DirectXShaderCompiler/pull/3721#issuecomment-829537469

damyanp commented 3 months ago

@jaebaek - we think that this is problem that impacts both the DXIL and SPIR-V backends, since relaly this error should be detected in sema.

(For reference, x0, x1 and x2 in the repro should be static)