microsoft / DirectXShaderCompiler

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

Returning a `globallycoherent` resource from a function results in a "global coherent mismatch" warning #4537

Closed jeremyong closed 1 year ago

jeremyong commented 2 years ago

Premise

While other RHIs still do not have dynamic resource indexing, it's convenient to implement a common platform-neutral interface with getters for various resource types. However, an issue seems to arise specifically when trying to return a globallycoherent annotated UAV from a function. While the DXIL generated correctly annotates the resource as globallycoherent, the shader compilation result produces warnings. Please let me know if I missed something in the correct way to do this, thanks!

Issue Description

Consider the following snippet:

// test.hlsl
RWTexture2D<float> get_texture() {
    globallycoherent RWTexture2D<float> output = ResourceDescriptorHeap[0];
    return output;
}
[numthreads(1, 1, 1)]
void CSMain() {
    get_texture()[uint2(0, 0)] = 1.0;
}

Compiling this as dxc.exe -E CSMain -T cs_6_6 -HV 2021 test.hlsl > test.dxil produces the following warning:

.\test.hlsl::4:5: warning: global coherent mismatch
    return output;
    ^

which is the result of this diagnostic.

However, there doesn't appear to be a clean way to propagate the annotation to the function's return type. For example:

globallycoherent RWTexture2D<float> get_texture() {
    globallycoherent RWTexture2D<float> output = ResourceDescriptorHeap[0];
    return output;
}
[numthreads(1, 1, 1)]
void CSMain() {
    get_texture()[uint2(0, 0)] = 1.0;
}

resolves the diagnostic above, but leaves you with a new warning:

.\test.hlsl:1:1: warning: 'globallycoherent' attribute only applies to variables [-Wignored-attributes]
globallycoherent RWTexture2D<float> get_texture()
^

This warning may be suppressed using -Wno-ignored-attributes but would globally turn off the warning.

Jasper-Bekkers commented 2 years ago

We've also ran into this with our abstractions, and haven't found a nice way around it other then to break them.

Having globallycoherent as a type modifier makes sense to me but it would also mean that it needs to be valid on return types.

EpicChrisWaters commented 1 year ago

We have also started running into this when converting our shaders to bindless. Since we do a direct variable -> function, the function type has to return a globallycoherent type.