Skip to content

Commit dfc5ab8

Browse files
spirv-opt: Also remap DebugScope instructions (#6501)
The canonicalize-ids pass does not remap DebugScope instructions. This can lead to outputting invalid SPIR-V when DebugScope instructions are present, as the test shader demonstrates. This also seems to fix #4018.
1 parent 28a45f8 commit dfc5ab8

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

source/opt/canonicalize_ids_pass.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,21 @@ bool CanonicalizeIdsPass::ApplyMap() {
428428
}
429429
}
430430
}
431+
const auto& debug_scope = inst->GetDebugScope();
432+
if (debug_scope.GetLexicalScope() != kNoDebugScope) {
433+
uint32_t old_scope = debug_scope.GetLexicalScope();
434+
uint32_t new_scope = GetNewId(old_scope);
435+
uint32_t old_inlined_at = debug_scope.GetInlinedAt();
436+
uint32_t new_inlined_at = old_inlined_at != kNoInlinedAt
437+
? GetNewId(old_inlined_at)
438+
: old_inlined_at;
439+
if ((new_scope != unused_ && new_scope != old_scope) ||
440+
(new_inlined_at != unused_ && new_inlined_at != old_inlined_at)) {
441+
DebugScope new_debug_scope(new_scope, new_inlined_at);
442+
inst->SetDebugScope(new_debug_scope);
443+
modified = true;
444+
}
445+
}
431446
},
432447
true);
433448

test/opt/canonicalize_ids_test.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,111 @@ OpFunctionEnd
20082008
SinglePassRunAndCheck<CanonicalizeIdsPass>(before, after, false, false);
20092009
}
20102010

2011+
TEST_F(CanonicalizeIdsTest, NonSemanticDebugInfo) {
2012+
std::string before = R"(
2013+
OpCapability Shader
2014+
OpExtension "SPV_KHR_non_semantic_info"
2015+
OpExtension "SPV_KHR_storage_buffer_storage_class"
2016+
%1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
2017+
OpMemoryModel Logical GLSL450
2018+
OpEntryPoint GLCompute %2 "main"
2019+
OpExecutionMode %2 LocalSize 1 1 1
2020+
%4 = OpString ""
2021+
OpDecorate %6 ArrayStride 4
2022+
OpDecorate %7 Block
2023+
OpMemberDecorate %7 0 Offset 0
2024+
OpDecorate %3 Binding 0
2025+
OpDecorate %3 DescriptorSet 0
2026+
%8 = OpTypeVoid
2027+
%9 = OpTypeInt 32 0
2028+
%10 = OpConstant %9 100
2029+
%11 = OpTypeFunction %8
2030+
%12 = OpTypeInt 32 1
2031+
%13 = OpConstant %12 0
2032+
%14 = OpTypeFloat 32
2033+
%5 = OpTypePointer StorageBuffer %14
2034+
%6 = OpTypeRuntimeArray %14
2035+
%7 = OpTypeStruct %6
2036+
%15 = OpTypePointer StorageBuffer %7
2037+
%16 = OpConstant %14 0
2038+
%3 = OpVariable %15 StorageBuffer
2039+
%17 = OpExtInst %8 %1 DebugInfoNone
2040+
%18 = OpExtInst %8 %1 DebugSource %4 %4
2041+
%19 = OpExtInst %8 %1 DebugCompilationUnit %10 %10 %18 %10
2042+
%20 = OpExtInst %8 %1 DebugTypeFunction %10 %8
2043+
%21 = OpExtInst %8 %1 DebugFunction %4 %20 %18 %10 %10 %19 %4 %10 %10
2044+
%22 = OpExtInst %8 %1 DebugEntryPoint %21 %19 %4 %4
2045+
%23 = OpExtInst %8 %1 DebugTypeBasic %4 %10 %10 %10
2046+
%24 = OpExtInst %8 %1 DebugTypeArray %23 %10
2047+
%25 = OpExtInst %8 %1 DebugTypeMember %4 %24 %18 %10 %10 %10 %10 %10
2048+
%26 = OpExtInst %8 %1 DebugTypeComposite %4 %10 %18 %10 %10 %19 %4 %10 %10 %25
2049+
%27 = OpExtInst %8 %1 DebugGlobalVariable %4 %26 %18 %10 %10 %19 %4 %3 %10
2050+
%2 = OpFunction %8 None %11
2051+
%28 = OpLabel
2052+
%29 = OpExtInst %8 %1 DebugFunctionDefinition %21 %2
2053+
%30 = OpExtInst %8 %1 DebugScope %21
2054+
%31 = OpExtInst %8 %1 DebugLine %18 %10 %10 %10 %10
2055+
%32 = OpAccessChain %5 %3 %13 %13
2056+
OpStore %32 %16
2057+
OpReturn
2058+
%33 = OpExtInst %8 %1 DebugNoScope
2059+
OpFunctionEnd
2060+
)";
2061+
2062+
std::string after = R"(OpCapability Shader
2063+
OpExtension "SPV_KHR_non_semantic_info"
2064+
OpExtension "SPV_KHR_storage_buffer_storage_class"
2065+
%1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
2066+
OpMemoryModel Logical GLSL450
2067+
OpEntryPoint GLCompute %10742 "main"
2068+
OpExecutionMode %10742 LocalSize 1 1 1
2069+
%3 = OpString ""
2070+
OpDecorate %2002 ArrayStride 4
2071+
OpDecorate %1952 Block
2072+
OpMemberDecorate %1952 0 Offset 0
2073+
OpDecorate %2 Binding 0
2074+
OpDecorate %2 DescriptorSet 0
2075+
%8 = OpTypeVoid
2076+
%11 = OpTypeInt 32 0
2077+
%2870 = OpConstant %11 100
2078+
%1282 = OpTypeFunction %8
2079+
%12 = OpTypeInt 32 1
2080+
%2571 = OpConstant %12 0
2081+
%13 = OpTypeFloat 32
2082+
%650 = OpTypePointer StorageBuffer %13
2083+
%2002 = OpTypeRuntimeArray %13
2084+
%1952 = OpTypeStruct %2002
2085+
%2589 = OpTypePointer StorageBuffer %1952
2086+
%2572 = OpConstant %13 0
2087+
%2 = OpVariable %2589 StorageBuffer
2088+
%4 = OpExtInst %8 %1 DebugInfoNone
2089+
%5 = OpExtInst %8 %1 DebugSource %3 %3
2090+
%6 = OpExtInst %8 %1 DebugCompilationUnit %2870 %2870 %5 %2870
2091+
%7 = OpExtInst %8 %1 DebugTypeFunction %2870 %8
2092+
%9 = OpExtInst %8 %1 DebugFunction %3 %7 %5 %2870 %2870 %6 %3 %2870 %2870
2093+
%10 = OpExtInst %8 %1 DebugEntryPoint %9 %6 %3 %3
2094+
%14 = OpExtInst %8 %1 DebugTypeBasic %3 %2870 %2870 %2870
2095+
%15 = OpExtInst %8 %1 DebugTypeArray %14 %2870
2096+
%16 = OpExtInst %8 %1 DebugTypeMember %3 %15 %5 %2870 %2870 %2870 %2870 %2870
2097+
%17 = OpExtInst %8 %1 DebugTypeComposite %3 %2870 %5 %2870 %2870 %6 %3 %2870 %2870 %16
2098+
%18 = OpExtInst %8 %1 DebugGlobalVariable %3 %17 %5 %2870 %2870 %6 %3 %2 %2870
2099+
%10742 = OpFunction %8 None %1282
2100+
%9442 = OpLabel
2101+
%15450 = OpExtInst %8 %1 DebugFunctionDefinition %9 %10742
2102+
%20275 = OpExtInst %8 %1 DebugScope %9
2103+
%19 = OpExtInst %8 %1 DebugLine %5 %2870 %2870 %2870 %2870
2104+
%20274 = OpAccessChain %650 %2 %2571 %2571
2105+
OpStore %20274 %2572
2106+
OpReturn
2107+
%20276 = OpExtInst %8 %1 DebugNoScope
2108+
OpFunctionEnd
2109+
)";
2110+
2111+
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2112+
SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
2113+
SinglePassRunAndCheck<CanonicalizeIdsPass>(before, after, false, true);
2114+
}
2115+
20112116
} // namespace
20122117
} // namespace opt
20132118
} // namespace spvtools

0 commit comments

Comments
 (0)