@@ -9025,6 +9025,199 @@ TEST_F(AggressiveDCETest, DebugValueWithDeadOperandKeepsDebugScope) {
90259025 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true );
90269026}
90279027
9028+ TEST_F (AggressiveDCETest, EliminateUntypedAccessChain) {
9029+ const std::string spirv = R"(
9030+ OpCapability Shader
9031+ OpCapability Sampled1D
9032+ OpCapability DescriptorHeapEXT
9033+ OpCapability UntypedPointersKHR
9034+ OpExtension "SPV_EXT_descriptor_heap"
9035+ OpExtension "SPV_KHR_untyped_pointers"
9036+ OpMemoryModel Logical GLSL450
9037+ OpEntryPoint Fragment %main "main"
9038+ OpExecutionMode %main OriginUpperLeft
9039+ OpName %main "main"
9040+ %uint = OpTypeInt 32 0
9041+ %uint_0 = OpConstant %uint 0
9042+ %type_untyped_pointer = OpTypeUntypedPointerKHR Uniform
9043+ %void = OpTypeVoid
9044+ %float = OpTypeFloat 32
9045+ %10 = OpTypeFunction %void
9046+ %type_1d_image = OpTypeImage %float 1D 2 0 0 1 Unknown
9047+ %_ptr_Function_type_1d_image = OpTypePointer Function %type_1d_image
9048+ %type_buffer_ext = OpTypeBufferEXT StorageBuffer
9049+ %_runtimearr_type_buffer_ext = OpTypeRuntimeArray %type_buffer_ext
9050+ %resource_heap = OpUntypedVariableKHR %type_untyped_pointer Uniform
9051+ %main = OpFunction %void None %10
9052+ %20 = OpLabel
9053+ %t1d = OpVariable %_ptr_Function_type_1d_image Function
9054+ ; CHECK-NOT: OpUntypedAccessChainKHR
9055+ %21 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %resource_heap %uint_0
9056+ ; CHECK-NOT: OpLoad %type_1d_image
9057+ %22 = OpLoad %type_1d_image %21
9058+ ; CHECK-NOT: OpStore %t1d
9059+ OpStore %t1d %22
9060+ OpReturn
9061+ OpFunctionEnd
9062+ )" ;
9063+ SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true );
9064+ }
9065+
9066+ TEST_F (AggressiveDCETest, NoEliminateLiveUntypedAccessChain) {
9067+ const std::string spirv = R"(
9068+ OpCapability Shader
9069+ OpCapability DescriptorHeapEXT
9070+ OpCapability UntypedPointersKHR
9071+ OpExtension "SPV_EXT_descriptor_heap"
9072+ OpExtension "SPV_KHR_untyped_pointers"
9073+ OpMemoryModel Logical GLSL450
9074+ OpEntryPoint Fragment %main "main" %outColor
9075+ OpExecutionMode %main OriginUpperLeft
9076+ OpName %main "main"
9077+ %uint = OpTypeInt 32 0
9078+ %uint_0 = OpConstant %uint 0
9079+ %type_untyped_pointer = OpTypeUntypedPointerKHR Uniform
9080+ %void = OpTypeVoid
9081+ %float = OpTypeFloat 32
9082+ %v4float = OpTypeVector %float 4
9083+ %_ptr_Output_v4float = OpTypePointer Output %v4float
9084+ %outColor = OpVariable %_ptr_Output_v4float Output
9085+ %10 = OpTypeFunction %void
9086+ %type_buffer_ext = OpTypeBufferEXT StorageBuffer
9087+ %_runtimearr_type_buffer_ext = OpTypeRuntimeArray %type_buffer_ext
9088+ %resource_heap = OpUntypedVariableKHR %type_untyped_pointer Uniform
9089+ %main = OpFunction %void None %10
9090+ %20 = OpLabel
9091+ ; CHECK: OpUntypedAccessChainKHR
9092+ %21 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %resource_heap %uint_0
9093+ ; CHECK: OpLoad
9094+ %22 = OpLoad %v4float %21
9095+ OpStore %outColor %22
9096+ OpReturn
9097+ OpFunctionEnd
9098+ )" ;
9099+ SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true );
9100+ }
9101+
9102+ TEST_F (AggressiveDCETest, EliminateUntypedAccessChainWithCopyObject) {
9103+ const std::string spirv = R"(
9104+ OpCapability Shader
9105+ OpCapability Sampled1D
9106+ OpCapability DescriptorHeapEXT
9107+ OpCapability UntypedPointersKHR
9108+ OpExtension "SPV_EXT_descriptor_heap"
9109+ OpExtension "SPV_KHR_untyped_pointers"
9110+ OpMemoryModel Logical GLSL450
9111+ OpEntryPoint Fragment %main "main"
9112+ OpExecutionMode %main OriginUpperLeft
9113+ OpName %main "main"
9114+ OpName %type_1d_image "type_1d_image"
9115+ %uint = OpTypeInt 32 0
9116+ %uint_0 = OpConstant %uint 0
9117+ %type_untyped_pointer = OpTypeUntypedPointerKHR Uniform
9118+ %void = OpTypeVoid
9119+ %float = OpTypeFloat 32
9120+ %10 = OpTypeFunction %void
9121+ %type_1d_image = OpTypeImage %float 1D 2 0 0 1 Unknown
9122+ %_ptr_Function_type_1d_image = OpTypePointer Function %type_1d_image
9123+ %type_buffer_ext = OpTypeBufferEXT StorageBuffer
9124+ %_runtimearr_type_buffer_ext = OpTypeRuntimeArray %type_buffer_ext
9125+ %resource_heap = OpUntypedVariableKHR %type_untyped_pointer Uniform
9126+ %main = OpFunction %void None %10
9127+ %20 = OpLabel
9128+ %t1d = OpVariable %_ptr_Function_type_1d_image Function
9129+ ; CHECK-NOT: OpUntypedAccessChainKHR
9130+ %21 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %resource_heap %uint_0
9131+ ; CHECK-NOT: OpCopyObject
9132+ %22 = OpCopyObject %type_untyped_pointer %21
9133+ ; CHECK-NOT: OpLoad %type_1d_image
9134+ %23 = OpLoad %type_1d_image %22
9135+ ; CHECK-NOT: OpStore %t1d
9136+ OpStore %t1d %23
9137+ OpReturn
9138+ OpFunctionEnd
9139+ )" ;
9140+ SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true );
9141+ }
9142+
9143+ // For now, aggressive DCE does not optimizes this pattern. If you implement
9144+ // it, remove this test.
9145+ TEST_F (AggressiveDCETest, EliminateUntypedAtomic) {
9146+ const std::string spirv = R"(
9147+ OpCapability Shader
9148+ OpCapability Int64
9149+ OpCapability DescriptorHeapEXT
9150+ OpCapability UntypedPointersKHR
9151+ OpExtension "SPV_EXT_descriptor_heap"
9152+ OpExtension "SPV_KHR_untyped_pointers"
9153+ OpMemoryModel Logical GLSL450
9154+ OpEntryPoint Fragment %main "main"
9155+ OpExecutionMode %main OriginUpperLeft
9156+ %uint = OpTypeInt 32 0
9157+ %uint_0 = OpConstant %uint 0
9158+ %type_untyped_pointer = OpTypeUntypedPointerKHR Uniform
9159+ %void = OpTypeVoid
9160+ %10 = OpTypeFunction %void
9161+ %type_buffer_ext = OpTypeBufferEXT StorageBuffer
9162+ %_runtimearr_type_buffer_ext = OpTypeRuntimeArray %type_buffer_ext
9163+ %resource_heap = OpUntypedVariableKHR %type_untyped_pointer Uniform
9164+ %main = OpFunction %void None %10
9165+ %20 = OpLabel
9166+ %21 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %resource_heap %uint_0
9167+ ; CHECK: [[ptr:%\w+]] = OpUntypedAccessChainKHR
9168+ %22 = OpAtomicLoad %uint %21 %uint_0 %uint_0
9169+ ; CHECK: OpAtomicLoad %uint [[ptr]]
9170+ OpReturn
9171+ OpFunctionEnd
9172+ )" ;
9173+ SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true );
9174+ }
9175+
9176+ TEST_F (AggressiveDCETest, EliminateUntypedAccessChainLoop) {
9177+ const std::string spirv = R"(
9178+ OpCapability Shader
9179+ OpCapability Sampled1D
9180+ OpCapability DescriptorHeapEXT
9181+ OpCapability UntypedPointersKHR
9182+ OpExtension "SPV_EXT_descriptor_heap"
9183+ OpExtension "SPV_KHR_untyped_pointers"
9184+ OpMemoryModel Logical GLSL450
9185+ OpEntryPoint Fragment %main "main"
9186+ OpExecutionMode %main OriginUpperLeft
9187+ %uint = OpTypeInt 32 0
9188+ %uint_0 = OpConstant %uint 0
9189+ %uint_1 = OpConstant %uint 1
9190+ %uint_10 = OpConstant %uint 10
9191+ %type_untyped_pointer = OpTypeUntypedPointerKHR Uniform
9192+ %void = OpTypeVoid
9193+ %bool = OpTypeBool
9194+ %10 = OpTypeFunction %void
9195+ %type_1d_image = OpTypeImage %uint 1D 2 0 0 1 Unknown
9196+ %type_buffer_ext = OpTypeBufferEXT StorageBuffer
9197+ %_runtimearr_type_buffer_ext = OpTypeRuntimeArray %type_buffer_ext
9198+ %resource_heap = OpUntypedVariableKHR %type_untyped_pointer Uniform
9199+ %main = OpFunction %void None %10
9200+ %20 = OpLabel
9201+ OpBranch %header
9202+ %header = OpLabel
9203+ %count = OpPhi %uint %uint_0 %20 %next %loop
9204+ %cond = OpULessThan %bool %count %uint_10
9205+ OpLoopMerge %exit %loop None
9206+ OpBranchConditional %cond %loop %exit
9207+ %loop = OpLabel
9208+ ; CHECK-NOT: OpUntypedAccessChainKHR
9209+ %21 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %resource_heap %count
9210+ ; CHECK-NOT: OpLoad
9211+ %22 = OpLoad %type_1d_image %21
9212+ %next = OpIAdd %uint %count %uint_1
9213+ OpBranch %header
9214+ %exit = OpLabel
9215+ OpReturn
9216+ OpFunctionEnd
9217+ )" ;
9218+ SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true );
9219+ }
9220+
90289221} // namespace
90299222} // namespace opt
90309223} // namespace spvtools
0 commit comments