Skip to content

Commit 066b67f

Browse files
committed
[OPT] In copy propagate arrays, debug instructions are not stores.
When checking if a variable is possibly stored to, debug instructions fall into the default case, which assumes it could be a store. We add a specific check for debug instruction, which are not stores. See microsoft/DirectXShaderCompiler#7181
1 parent 6add4e4 commit 066b67f

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

source/opt/copy_prop_arrays.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ Pass::Status CopyPropagateArrays::Process() {
9595
std::unique_ptr<MemoryObject> source_object =
9696
FindSourceObjectIfPossible(&*var_inst, store_inst);
9797

98-
if (source_object != nullptr) {
99-
if (!IsPointerToArrayType(var_inst->type_id()) &&
100-
source_object->GetStorageClass() != spv::StorageClass::Input) {
101-
continue;
102-
}
98+
if (source_object == nullptr) {
99+
continue;
100+
}
101+
102+
if (!IsPointerToArrayType(var_inst->type_id()) &&
103+
source_object->GetStorageClass() != spv::StorageClass::Input) {
104+
continue;
105+
}
103106

104-
if (CanUpdateUses(&*var_inst, source_object->GetPointerTypeId(this))) {
105-
modified = true;
107+
if (CanUpdateUses(&*var_inst, source_object->GetPointerTypeId(this))) {
108+
modified = true;
106109

107-
PropagateObject(&*var_inst, source_object.get(), store_inst);
108-
}
110+
PropagateObject(&*var_inst, source_object.get(), store_inst);
109111
}
110112
}
111113

@@ -219,6 +221,8 @@ bool CopyPropagateArrays::HasNoStores(Instruction* ptr_inst) {
219221
return true;
220222
} else if (IsInterpolationInstruction(use)) {
221223
return true;
224+
} else if (use->IsCommonDebugInstr()) {
225+
return true;
222226
}
223227
// Some other instruction. Be conservative.
224228
return false;

test/opt/copy_prop_array_test.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,93 @@ TEST_F(CopyPropArrayPassTest, PropCopyLogical) {
22262226
SinglePassRunAndMatch<CopyPropagateArrays>(before, true);
22272227
}
22282228

2229+
// Ensure that the use of the global variable in a debug instruction does not
2230+
// stop copy propagation. We expect the image operand to OpImageTexelPointer to
2231+
// be replaced.
2232+
TEST_F(CopyPropArrayPassTest, DebugInstNotStore) {
2233+
const std::string before = R"(
2234+
OpCapability Shader
2235+
OpCapability SampledBuffer
2236+
OpExtension "SPV_KHR_non_semantic_info"
2237+
OpExtension "SPV_EXT_descriptor_indexing"
2238+
%1 = OpExtInstImport "GLSL.std.450"
2239+
%2 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
2240+
OpMemoryModel Logical GLSL450
2241+
OpEntryPoint GLCompute %3 "maincomp"
2242+
OpExecutionMode %3 LocalSize 16 16 1
2243+
%4 = OpString ""
2244+
%uint = OpTypeInt 32 0
2245+
%uint_0 = OpConstant %uint 0
2246+
%uint_1 = OpConstant %uint 1
2247+
%uint_6 = OpConstant %uint 6
2248+
%uint_8 = OpConstant %uint 8
2249+
%uint_2 = OpConstant %uint 2
2250+
%uint_7 = OpConstant %uint 7
2251+
%uint_3 = OpConstant %uint 3
2252+
%uint_32 = OpConstant %uint 32
2253+
%uint_16 = OpConstant %uint 16
2254+
%uint_4 = OpConstant %uint 4
2255+
%16 = OpTypeImage %uint Buffer 2 0 0 2 R32ui
2256+
%_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
2257+
%void = OpTypeVoid
2258+
%uint_5 = OpConstant %uint 5
2259+
%uint_70 = OpConstant %uint 70
2260+
%uint_71 = OpConstant %uint 71
2261+
%uint_72 = OpConstant %uint 72
2262+
%uint_17 = OpConstant %uint 17
2263+
%uint_9 = OpConstant %uint 9
2264+
%uint_25 = OpConstant %uint 25
2265+
%uint_14 = OpConstant %uint 14
2266+
%uint_24 = OpConstant %uint 24
2267+
%uint_13 = OpConstant %uint 13
2268+
%29 = OpTypeFunction %void
2269+
%_ptr_Function_16 = OpTypePointer Function %16
2270+
%_ptr_Image_uint = OpTypePointer Image %uint
2271+
; CHECK: [[GV:%\w+]] = OpVariable {{%\w+}} UniformConstant
2272+
%32 = OpVariable %_ptr_UniformConstant_16 UniformConstant
2273+
%33 = OpExtInst %void %2 DebugInfoNone
2274+
%34 = OpExtInst %void %2 DebugExpression
2275+
%35 = OpExtInst %void %2 DebugTypeBasic %4 %uint_32 %uint_3 %uint_0
2276+
%36 = OpExtInst %void %2 DebugTypeVector %35 %uint_3
2277+
%37 = OpExtInst %void %2 DebugSource %4 %4
2278+
%38 = OpExtInst %void %2 DebugCompilationUnit %uint_1 %uint_4 %37 %uint_5
2279+
%39 = OpExtInst %void %2 DebugTypeTemplateParameter %4 %36 %33 %37 %uint_0 %uint_0
2280+
%40 = OpExtInst %void %2 DebugTypeBasic %4 %uint_32 %uint_6 %uint_0
2281+
%41 = OpExtInst %void %2 DebugSource %4 %4
2282+
%42 = OpExtInst %void %2 DebugCompilationUnit %uint_1 %uint_4 %41 %uint_5
2283+
%43 = OpExtInst %void %2 DebugTypeComposite %4 %uint_0 %37 %uint_0 %uint_0 %38 %4 %33 %uint_3
2284+
%44 = OpExtInst %void %2 DebugTypeTemplateParameter %4 %40 %33 %37 %uint_0 %uint_0
2285+
%45 = OpExtInst %void %2 DebugTypeTemplate %43 %44
2286+
%46 = OpExtInst %void %2 DebugTypeFunction %uint_3 %void %40
2287+
%47 = OpExtInst %void %2 DebugFunction %4 %46 %37 %uint_70 %uint_1 %38 %4 %uint_3 %uint_71
2288+
%48 = OpExtInst %void %2 DebugLexicalBlock %37 %uint_71 %uint_1 %47
2289+
%49 = OpExtInst %void %2 DebugLocalVariable %4 %45 %37 %uint_72 %uint_17 %48 %uint_4
2290+
%50 = OpExtInst %void %2 DebugTypeFunction %uint_3 %void
2291+
%51 = OpExtInst %void %2 DebugSource %4 %4
2292+
%52 = OpExtInst %void %2 DebugCompilationUnit %uint_1 %uint_4 %51 %uint_5
2293+
%53 = OpExtInst %void %2 DebugFunction %4 %50 %51 %uint_24 %uint_1 %52 %4 %uint_3 %uint_25
2294+
%54 = OpExtInst %void %2 DebugGlobalVariable %4 %45 %37 %uint_17 %uint_16 %38 %4 %32 %uint_8
2295+
%55 = OpExtInst %void %2 DebugTypeMember %4 %40 %37 %uint_14 %uint_7 %uint_0 %uint_32 %uint_3
2296+
%56 = OpExtInst %void %2 DebugTypeComposite %4 %uint_1 %37 %uint_13 %uint_9 %38 %4 %uint_32 %uint_3 %55
2297+
%57 = OpExtInst %void %2 DebugEntryPoint %53 %42 %4 %4
2298+
%3 = OpFunction %void None %29
2299+
%58 = OpLabel
2300+
%59 = OpVariable %_ptr_Function_16 Function
2301+
%60 = OpLoad %16 %32
2302+
OpStore %59 %60
2303+
%61 = OpExtInst %void %2 DebugDeclare %49 %59 %34
2304+
%62 = OpExtInst %void %2 DebugLine %37 %uint_0 %uint_0 %uint_2 %uint_2
2305+
; CHECK: OpImageTexelPointer %_ptr_Image_uint [[GV]] %uint_0 %uint_0
2306+
%63 = OpImageTexelPointer %_ptr_Image_uint %59 %uint_0 %uint_0
2307+
%64 = OpAtomicIAdd %uint %63 %uint_1 %uint_0 %uint_1
2308+
OpReturn
2309+
OpFunctionEnd
2310+
)";
2311+
2312+
SetTargetEnv(SPV_ENV_VULKAN_1_1);
2313+
SinglePassRunAndMatch<CopyPropagateArrays>(before, false);
2314+
}
2315+
22292316
} // namespace
22302317
} // namespace opt
22312318
} // namespace spvtools

0 commit comments

Comments
 (0)