shader-slang / slang

Making it easier to work with shaders
MIT License
2k stars 169 forks source link

spirv-opt crashes on shader that passes spirv-val #5045

Open cheneym2 opened 1 week ago

cheneym2 commented 1 week ago

A spirv shader produced by slang is crashing spirv-opt. The same shader passes spirv-val.

NVIDIA.COM+acheney@acheney-lt3 /cygdrive/c/Documents and Settings/acheney/Documents/git/slang3
$ /cygdrive/c/VulkanSDK/1.3.290.0/Bin/spirv-val.exe --target-env spv1.5 ./pre-val.spv
NVIDIA.COM+acheney@acheney-lt3 /cygdrive/c/Documents and Settings/acheney/Documents/git/slang3
$ /cygdrive/c/VulkanSDK/1.3.290.0/Bin/spirv-opt.exe --eliminate-dead-code-aggressive ./pre-val.spv -o out.spv
Segmentation fault
$ /cygdrive/c/VulkanSDK/1.3.290.0/Bin/spirv-dis.exe ./pre-val.spv
; SPIR-V
; Version: 1.5
; Generator: Khronos; 40
; Bound: 36
; Schema: 0
               OpCapability Linkage
               OpCapability RayTracingKHR
               OpCapability Shader
               OpExtension "SPV_KHR_ray_tracing"
               OpMemoryModel Logical GLSL450
               OpEntryPoint AnyHitKHR %anyhit "main" %Constants %8
               OpSource Slang 1
               OpName %Payload "Payload"
               OpMemberName %Payload 0 "val"
               OpName %SLANG_ParameterGroup_Constants_std140 "SLANG_ParameterGroup_Constants_std140"
               OpMemberName %SLANG_ParameterGroup_Constants_std140 0 "x"
               OpMemberName %SLANG_ParameterGroup_Constants_std140 1 "y"
               OpName %Constants "Constants"
               OpName %a "a"
               OpName %normalFunc "normalFunc"
               OpName %a_0 "a"
               OpName %normalFuncUsesGeneric "normalFuncUsesGeneric"
               OpName %anyhit "anyhit"
               OpMemberDecorate %Payload 0 Offset 0
               OpDecorate %SLANG_ParameterGroup_Constants_std140 Block
               OpMemberDecorate %SLANG_ParameterGroup_Constants_std140 0 Offset 0
               OpMemberDecorate %SLANG_ParameterGroup_Constants_std140 1 Offset 4
               OpDecorate %Constants Binding 0
               OpDecorate %Constants DescriptorSet 0
               OpDecorate %normalFunc LinkageAttributes "_SR29export_2Dxlibrary_2Dxgenerics10normalFuncp1pi_ii" Import
               OpDecorate %normalFuncUsesGeneric LinkageAttributes "_SR29export_2Dxlibrary_2Dxgenerics21normalFuncUsesGenericp1pi_ii" Import
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
        %int = OpTypeInt 32 1
    %Payload = OpTypeStruct %int
%_ptr_IncomingRayPayloadKHR_Payload = OpTypePointer IncomingRayPayloadKHR %Payload
      %int_0 = OpConstant %int 0
%_ptr_IncomingRayPayloadKHR_int = OpTypePointer IncomingRayPayloadKHR %int
      %float = OpTypeFloat 32
%SLANG_ParameterGroup_Constants_std140 = OpTypeStruct %float %float
%_ptr_Uniform_SLANG_ParameterGroup_Constants_std140 = OpTypePointer Uniform %SLANG_ParameterGroup_Constants_std140
%_ptr_Uniform_float = OpTypePointer Uniform %float
      %int_1 = OpConstant %int 1
         %26 = OpTypeFunction %int %int
          %8 = OpVariable %_ptr_IncomingRayPayloadKHR_Payload IncomingRayPayloadKHR
  %Constants = OpVariable %_ptr_Uniform_SLANG_ParameterGroup_Constants_std140 Uniform
 %normalFunc = OpFunction %int None %26
          %a = OpFunctionParameter %int
               OpFunctionEnd
%normalFuncUsesGeneric = OpFunction %int None %26
        %a_0 = OpFunctionParameter %int
               OpFunctionEnd
     %anyhit = OpFunction %void None %3
          %4 = OpLabel
         %11 = OpAccessChain %_ptr_IncomingRayPayloadKHR_int %8 %int_0
         %17 = OpAccessChain %_ptr_Uniform_float %Constants %int_0
         %18 = OpLoad %float %17
         %20 = OpAccessChain %_ptr_Uniform_float %Constants %int_1
         %21 = OpLoad %float %20
         %22 = OpFMul %float %18 %21
         %23 = OpConvertFToS %int %22
         %24 = OpFunctionCall %int %normalFunc %23
         %28 = OpLoad %float %20
         %29 = OpConvertFToS %int %28
         %30 = OpFunctionCall %int %normalFuncUsesGeneric %29
         %33 = OpIAdd %int %24 %30
               OpStore %11 %33
               OpReturn
               OpFunctionEnd
cheneym2 commented 1 week ago

It's probably related somehow to these function declarations, since that's being generated from the slang IR that I hacked.

%normalFunc = OpFunction %int None %26 %a = OpFunctionParameter %int OpFunctionEnd %normalFuncUsesGeneric = OpFunction %int None %26 %a_0 = OpFunctionParameter %int OpFunctionEnd

cheneym2 commented 1 week ago

This patch fixes the issue, which appears to be spirv-opt's fault, nothing wrong that spirv-val should have caught. https://github.com/shader-slang/SPIRV-Tools/pull/8