Skip to content

Commit 93cde4f

Browse files
authored
opt: fix OpUntypedVariableKHR removed from interfaces (KhronosGroup#6607)
The remove_unused_interface pass was unaware of the OpUntypedVariableKHR opcode, and thus was wrongly removing those from the listed interfaces.
1 parent 26fb01b commit 93cde4f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

source/opt/remove_unused_interface_variables_pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class RemoveUnusedInterfaceVariablesContext {
3434
instruction.ForEachInId([&](const uint32_t* id) {
3535
if (used_variables_.count(*id)) return;
3636
auto* var = parent_.get_def_use_mgr()->GetDef(*id);
37-
if (!var || var->opcode() != spv::Op::OpVariable) return;
37+
if (!var || (var->opcode() != spv::Op::OpVariable &&
38+
var->opcode() != spv::Op::OpUntypedVariableKHR))
39+
return;
3840
auto storage_class =
3941
spv::StorageClass(var->GetSingleWordInOperand(0));
4042
if (storage_class != spv::StorageClass::Function &&

test/opt/remove_unused_interface_variables_test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,50 @@ OpFunctionEnd
179179
true, true);
180180
}
181181

182+
TEST_F(RemoveUnusedInterfaceVariablesTest,
183+
DontEliminateUntypedVariableInterfaceVulkan12) {
184+
const std::string spirv = R"(
185+
OpCapability Shader
186+
OpCapability SampledBuffer
187+
OpCapability ImageBuffer
188+
OpCapability Int64
189+
OpCapability DescriptorHeapEXT
190+
OpCapability UntypedPointersKHR
191+
OpExtension "SPV_EXT_descriptor_heap"
192+
OpExtension "SPV_KHR_untyped_pointers"
193+
OpMemoryModel Logical GLSL450
194+
OpEntryPoint Fragment %1 "main" %2
195+
; CHECK: OpEntryPoint Fragment %1 "main" %2
196+
OpExecutionMode %1 OriginUpperLeft
197+
OpDecorate %2 BuiltIn ResourceHeapEXT
198+
%uint = OpTypeInt 32 0
199+
%float = OpTypeFloat 32
200+
%v4float = OpTypeVector %float 4
201+
%uint_0 = OpConstant %uint 0
202+
%uint_1 = OpConstant %uint 1
203+
%type_untyped_pointer = OpTypeUntypedPointerKHR UniformConstant
204+
%void = OpTypeVoid
205+
%10 = OpTypeFunction %void
206+
%type_buffer_image = OpTypeImage %float Buffer 2 0 0 1 Rgba32f
207+
%type_buffer_image_0 = OpTypeImage %float Buffer 2 0 0 2 Rgba32f
208+
%type_buffer_ext = OpTypeBufferEXT StorageBuffer
209+
%_runtimearr_type_buffer_ext = OpTypeRuntimeArray %type_buffer_ext
210+
%2 = OpUntypedVariableKHR %type_untyped_pointer UniformConstant
211+
%1 = OpFunction %void None %10
212+
%3 = OpLabel
213+
%20 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %2 %uint_0
214+
%24 = OpUntypedAccessChainKHR %type_untyped_pointer %_runtimearr_type_buffer_ext %2 %uint_1
215+
%26 = OpLoad %type_buffer_image %20
216+
%28 = OpImageFetch %v4float %26 %uint_0 None
217+
%29 = OpLoad %type_buffer_image_0 %24
218+
OpImageWrite %29 %uint_0 %28 None
219+
OpReturn
220+
OpFunctionEnd
221+
)";
222+
SetTargetEnv(SPV_ENV_VULKAN_1_2);
223+
SinglePassRunAndMatch<RemoveUnusedInterfaceVariablesPass>(spirv, true);
224+
}
225+
182226
} // namespace
183227
} // namespace opt
184228
} // namespace spvtools

0 commit comments

Comments
 (0)