microsoft / DirectXShaderCompiler

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

[SPIR-V] SV_ShadingRate disables optimizations #6915

Closed Error-mdl closed 2 weeks ago

Error-mdl commented 2 months ago

Description If the SV_ShadingRate semantic is used in any shader stage that supports it, the compiler seems to skip optimizing out unused resources as though the -fspv-preserve-bindings flag were set. Additionally it does not remove unused variables in the input. Directly appending just the SPV_KHR_fragment_shading_rate extension via inline SPIR-V produces the same result.

Steps to Reproduce Godbolt Link,l:'5',n:'1',o:'HLSL+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:dxc_1_8_2403_2,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:hlsl,libs:!(),options:'-E+vertex+-T+vs_6_5+-spirv+-fspv-target-env%3Dvulkan1.2',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+DXC+1.8.2403.2+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Using this shader: test.hlsl:

Compile 3 times with the following command line arguments: normal: -E vertex -T vs_6_5 -spirv -fspv-target-env=vulkan1.2 with SV_ShadingRate: -E vertex -T vs_6_5 -spirv -fspv-target-env=vulkan1.2 -D SHADING_RATE with just the extension: -E vertex -T vs_6_5 -spirv -fspv-target-env=vulkan1.2 -D EXT_ONLY

Results: Normal Shading Rate Extension Only

Behavior This vertex shader outputs a constant 0, so we should expect that all the textures, samplers, and cbuffer should be removed and the input vertex and texcoord semantics should be optimized away as well. This happens when the base shader is compiled. When adding SV_ShadingRate to the output or simply declaring the extension via inline SPIR-V, no resources are removed and the unused position and texcoord semantics are loaded.

Environment

Error-mdl commented 2 months ago

Managed to track down the issue. Its the SPIRV-Tools aggressive dead code elimination pass. There's a whitelist of allowed extensions, and if the code contains an extension not on it the pass exits: https://github.com/KhronosGroup/SPIRV-Tools/blob/4451f6ab13dda98bf255a7cd7b4d120132dc0dfd/source/opt/aggressive_dead_code_elim_pass.cpp#L952 . I see no reason why SPV_KHR_fragment_shading_rate isn't in the list, and adding myself seems to work fine.

s-perron commented 2 months ago

Reopening to make sure we update the allow lists in spirv-opt. There are three passes that need to be updated.

s-perron commented 2 months ago

Reopening to make sure we update the allow lists in spirv-opt. There are three passes that need to be updated.

hosea1008 commented 3 weeks ago

This also fixes #6982