Debian Codegraph @

third_party/spirv-tools/test/opt/debug_info_manager_test.cpp

raw
1 // Copyright (c) 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/opt/debug_info_manager.h"
16
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 #include "effcee/effcee.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "source/opt/build_module.h"
25 #include "source/opt/instruction.h"
26 #include "spirv-tools/libspirv.hpp"
27
28 // Constants for OpenCL.DebugInfo.100 extension instructions.
29
30 static const uint32_t kDebugFunctionOperandFunctionIndex = 13;
31 static const uint32_t kDebugInlinedAtOperandLineIndex = 4;
32 static const uint32_t kDebugInlinedAtOperandScopeIndex = 5;
33 static const uint32_t kDebugInlinedAtOperandInlinedIndex = 6;
34
35 namespace spvtools {
36 namespace opt {
37 namespace analysis {
38 namespace {
39
40 TEST(DebugInfoManager, GetDebugInlinedAt) {
41 const std::string text = R"(
42 OpCapability Shader
43 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
44 OpMemoryModel Logical GLSL450
45 OpEntryPoint Fragment %main "main" %in_var_COLOR
46 OpExecutionMode %main OriginUpperLeft
47 %5 = OpString "ps.hlsl"
48 %14 = OpString "#line 1 \"ps.hlsl\"
49 void main(float in_var_color : COLOR) {
50 float color = in_var_color;
51 }
52 "
53 %17 = OpString "float"
54 %21 = OpString "main"
55 %24 = OpString "color"
56 OpName %in_var_COLOR "in.var.COLOR"
57 OpName %main "main"
58 OpDecorate %in_var_COLOR Location 0
59 %uint = OpTypeInt 32 0
60 %uint_32 = OpConstant %uint 32
61 %float = OpTypeFloat 32
62 %_ptr_Input_float = OpTypePointer Input %float
63 %void = OpTypeVoid
64 %27 = OpTypeFunction %void
65 %_ptr_Function_float = OpTypePointer Function %float
66 %in_var_COLOR = OpVariable %_ptr_Input_float Input
67 %13 = OpExtInst %void %1 DebugExpression
68 %15 = OpExtInst %void %1 DebugSource %5 %14
69 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
70 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
71 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
72 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
73 %100 = OpExtInst %void %1 DebugInlinedAt 7 %22
74 %main = OpFunction %void None %27
75 %28 = OpLabel
76 %31 = OpLoad %float %in_var_COLOR
77 OpStore %100 %31
78 OpReturn
79 OpFunctionEnd
80 )";
81
82 std::unique_ptr<IRContext> context =
83 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
84 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
85 DebugInfoManager manager(context.get());
86
87 EXPECT_EQ(manager.GetDebugInlinedAt(150), nullptr);
88 EXPECT_EQ(manager.GetDebugInlinedAt(31), nullptr);
89 EXPECT_EQ(manager.GetDebugInlinedAt(22), nullptr);
90
91 auto* inst = manager.GetDebugInlinedAt(100);
92 EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex), 7);
93 EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex), 22);
94 }
95
96 TEST(DebugInfoManager, CreateDebugInlinedAt) {
97 const std::string text = R"(
98 OpCapability Shader
99 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
100 OpMemoryModel Logical GLSL450
101 OpEntryPoint Fragment %main "main" %in_var_COLOR
102 OpExecutionMode %main OriginUpperLeft
103 %5 = OpString "ps.hlsl"
104 %14 = OpString "#line 1 \"ps.hlsl\"
105 void main(float in_var_color : COLOR) {
106 float color = in_var_color;
107 }
108 "
109 %17 = OpString "float"
110 %21 = OpString "main"
111 %24 = OpString "color"
112 OpName %in_var_COLOR "in.var.COLOR"
113 OpName %main "main"
114 OpDecorate %in_var_COLOR Location 0
115 %uint = OpTypeInt 32 0
116 %uint_32 = OpConstant %uint 32
117 %float = OpTypeFloat 32
118 %_ptr_Input_float = OpTypePointer Input %float
119 %void = OpTypeVoid
120 %27 = OpTypeFunction %void
121 %_ptr_Function_float = OpTypePointer Function %float
122 %in_var_COLOR = OpVariable %_ptr_Input_float Input
123 %13 = OpExtInst %void %1 DebugExpression
124 %15 = OpExtInst %void %1 DebugSource %5 %14
125 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
126 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
127 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
128 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
129 %100 = OpExtInst %void %1 DebugInlinedAt 7 %22
130 %main = OpFunction %void None %27
131 %28 = OpLabel
132 %31 = OpLoad %float %in_var_COLOR
133 OpStore %100 %31
134 OpReturn
135 OpFunctionEnd
136 )";
137
138 DebugScope scope(22U, 0U);
139
140 std::unique_ptr<IRContext> context =
141 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
142 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
143 DebugInfoManager manager(context.get());
144
145 uint32_t inlined_at_id = manager.CreateDebugInlinedAt(nullptr, scope);
146 auto* inlined_at = manager.GetDebugInlinedAt(inlined_at_id);
147 EXPECT_NE(inlined_at, nullptr);
148 EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex),
149 1);
150 EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex),
151 22);
152 EXPECT_EQ(inlined_at->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
153
154 const uint32_t line_number = 77U;
155 Instruction line(context.get(), SpvOpLine);
156 line.SetInOperands({
157 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {5U}},
158 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {line_number}},
159 {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {0U}},
160 });
161
162 inlined_at_id = manager.CreateDebugInlinedAt(&line, scope);
163 inlined_at = manager.GetDebugInlinedAt(inlined_at_id);
164 EXPECT_NE(inlined_at, nullptr);
165 EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex),
166 line_number);
167 EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex),
168 22);
169 EXPECT_EQ(inlined_at->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
170
171 scope.SetInlinedAt(100U);
172 inlined_at_id = manager.CreateDebugInlinedAt(&line, scope);
173 inlined_at = manager.GetDebugInlinedAt(inlined_at_id);
174 EXPECT_NE(inlined_at, nullptr);
175 EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex),
176 line_number);
177 EXPECT_EQ(inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex),
178 22);
179 EXPECT_EQ(inlined_at->NumOperands(), kDebugInlinedAtOperandInlinedIndex + 1);
180 EXPECT_EQ(
181 inlined_at->GetSingleWordOperand(kDebugInlinedAtOperandInlinedIndex),
182 100U);
183 }
184
185 TEST(DebugInfoManager, GetDebugInfoNone) {
186 const std::string text = R"(
187 OpCapability Shader
188 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
189 OpMemoryModel Logical GLSL450
190 OpEntryPoint Fragment %main "main" %in_var_COLOR
191 OpExecutionMode %main OriginUpperLeft
192 %5 = OpString "ps.hlsl"
193 %14 = OpString "#line 1 \"ps.hlsl\"
194 void main(float in_var_color : COLOR) {
195 float color = in_var_color;
196 }
197 "
198 %17 = OpString "float"
199 %21 = OpString "main"
200 %24 = OpString "color"
201 OpName %in_var_COLOR "in.var.COLOR"
202 OpName %main "main"
203 OpDecorate %in_var_COLOR Location 0
204 %uint = OpTypeInt 32 0
205 %uint_32 = OpConstant %uint 32
206 %float = OpTypeFloat 32
207 %_ptr_Input_float = OpTypePointer Input %float
208 %void = OpTypeVoid
209 %27 = OpTypeFunction %void
210 %_ptr_Function_float = OpTypePointer Function %float
211 %in_var_COLOR = OpVariable %_ptr_Input_float Input
212 %13 = OpExtInst %void %1 DebugExpression
213 %15 = OpExtInst %void %1 DebugSource %5 %14
214 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
215 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
216 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
217 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
218 %12 = OpExtInst %void %1 DebugInfoNone
219 %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
220 %main = OpFunction %void None %27
221 %28 = OpLabel
222 %100 = OpVariable %_ptr_Function_float Function
223 %31 = OpLoad %float %in_var_COLOR
224 OpStore %100 %31
225 %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
226 OpReturn
227 OpFunctionEnd
228 )";
229
230 std::unique_ptr<IRContext> context =
231 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
232 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
233 DebugInfoManager manager(context.get());
234
235 Instruction* debug_info_none_inst = manager.GetDebugInfoNone();
236 EXPECT_NE(debug_info_none_inst, nullptr);
237 EXPECT_EQ(debug_info_none_inst->GetOpenCL100DebugOpcode(),
238 OpenCLDebugInfo100DebugInfoNone);
239 EXPECT_EQ(debug_info_none_inst->PreviousNode(), nullptr);
240 }
241
242 TEST(DebugInfoManager, CreateDebugInfoNone) {
243 const std::string text = R"(
244 OpCapability Shader
245 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
246 OpMemoryModel Logical GLSL450
247 OpEntryPoint Fragment %main "main" %in_var_COLOR
248 OpExecutionMode %main OriginUpperLeft
249 %5 = OpString "ps.hlsl"
250 %14 = OpString "#line 1 \"ps.hlsl\"
251 void main(float in_var_color : COLOR) {
252 float color = in_var_color;
253 }
254 "
255 %17 = OpString "float"
256 %21 = OpString "main"
257 %24 = OpString "color"
258 OpName %in_var_COLOR "in.var.COLOR"
259 OpName %main "main"
260 OpDecorate %in_var_COLOR Location 0
261 %uint = OpTypeInt 32 0
262 %uint_32 = OpConstant %uint 32
263 %float = OpTypeFloat 32
264 %_ptr_Input_float = OpTypePointer Input %float
265 %void = OpTypeVoid
266 %27 = OpTypeFunction %void
267 %_ptr_Function_float = OpTypePointer Function %float
268 %in_var_COLOR = OpVariable %_ptr_Input_float Input
269 %13 = OpExtInst %void %1 DebugExpression
270 %15 = OpExtInst %void %1 DebugSource %5 %14
271 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
272 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
273 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
274 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
275 %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
276 %main = OpFunction %void None %27
277 %28 = OpLabel
278 %100 = OpVariable %_ptr_Function_float Function
279 %31 = OpLoad %float %in_var_COLOR
280 OpStore %100 %31
281 %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
282 OpReturn
283 OpFunctionEnd
284 )";
285
286 std::unique_ptr<IRContext> context =
287 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
288 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
289 DebugInfoManager manager(context.get());
290
291 Instruction* debug_info_none_inst = manager.GetDebugInfoNone();
292 EXPECT_NE(debug_info_none_inst, nullptr);
293 EXPECT_EQ(debug_info_none_inst->GetOpenCL100DebugOpcode(),
294 OpenCLDebugInfo100DebugInfoNone);
295 EXPECT_EQ(debug_info_none_inst->PreviousNode(), nullptr);
296 }
297
298 TEST(DebugInfoManager, GetDebugFunction) {
299 const std::string text = R"(
300 OpCapability Shader
301 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
302 OpMemoryModel Logical GLSL450
303 OpEntryPoint Fragment %200 "200" %in_var_COLOR
304 OpExecutionMode %200 OriginUpperLeft
305 %5 = OpString "ps.hlsl"
306 %14 = OpString "#line 1 \"ps.hlsl\"
307 void 200(float in_var_color : COLOR) {
308 float color = in_var_color;
309 }
310 "
311 %17 = OpString "float"
312 %21 = OpString "200"
313 %24 = OpString "color"
314 OpName %in_var_COLOR "in.var.COLOR"
315 OpName %200 "200"
316 OpDecorate %in_var_COLOR Location 0
317 %uint = OpTypeInt 32 0
318 %uint_32 = OpConstant %uint 32
319 %float = OpTypeFloat 32
320 %_ptr_Input_float = OpTypePointer Input %float
321 %void = OpTypeVoid
322 %27 = OpTypeFunction %void
323 %_ptr_Function_float = OpTypePointer Function %float
324 %in_var_COLOR = OpVariable %_ptr_Input_float Input
325 %13 = OpExtInst %void %1 DebugExpression
326 %15 = OpExtInst %void %1 DebugSource %5 %14
327 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
328 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
329 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
330 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %200
331 %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
332 %200 = OpFunction %void None %27
333 %28 = OpLabel
334 %100 = OpVariable %_ptr_Function_float Function
335 %31 = OpLoad %float %in_var_COLOR
336 OpStore %100 %31
337 %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
338 OpReturn
339 OpFunctionEnd
340 )";
341
342 std::unique_ptr<IRContext> context =
343 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
344 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
345 DebugInfoManager manager(context.get());
346
347 EXPECT_EQ(manager.GetDebugFunction(100), nullptr);
348 EXPECT_EQ(manager.GetDebugFunction(150), nullptr);
349
350 Instruction* dbg_fn = manager.GetDebugFunction(200);
351
352 EXPECT_EQ(dbg_fn->GetOpenCL100DebugOpcode(), OpenCLDebugInfo100DebugFunction);
353 EXPECT_EQ(dbg_fn->GetSingleWordOperand(kDebugFunctionOperandFunctionIndex),
354 200);
355 }
356
357 TEST(DebugInfoManager, GetDebugFunction_InlinedAway) {
358 // struct PS_INPUT
359 // {
360 // float4 iColor : COLOR;
361 // };
362 //
363 // struct PS_OUTPUT
364 // {
365 // float4 oColor : SV_Target0;
366 // };
367 //
368 // float4 foo(float4 ic)
369 // {
370 // float4 c = ic / 2.0;
371 // return c;
372 // }
373 //
374 // PS_OUTPUT MainPs(PS_INPUT i)
375 // {
376 // PS_OUTPUT ps_output;
377 // float4 ic = i.iColor;
378 // ps_output.oColor = foo(ic);
379 // return ps_output;
380 // }
381 const std::string text = R"(
382 OpCapability Shader
383 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
384 OpMemoryModel Logical GLSL450
385 OpEntryPoint Fragment %MainPs "MainPs" %in_var_COLOR %out_var_SV_Target0
386 OpExecutionMode %MainPs OriginUpperLeft
387 %15 = OpString "foo2.frag"
388 %19 = OpString "PS_OUTPUT"
389 %23 = OpString "float"
390 %26 = OpString "oColor"
391 %28 = OpString "PS_INPUT"
392 %31 = OpString "iColor"
393 %33 = OpString "foo"
394 %37 = OpString "c"
395 %39 = OpString "ic"
396 %42 = OpString "src.MainPs"
397 %47 = OpString "ps_output"
398 %50 = OpString "i"
399 OpName %in_var_COLOR "in.var.COLOR"
400 OpName %out_var_SV_Target0 "out.var.SV_Target0"
401 OpName %MainPs "MainPs"
402 OpDecorate %in_var_COLOR Location 0
403 OpDecorate %out_var_SV_Target0 Location 0
404 %int = OpTypeInt 32 1
405 %int_0 = OpConstant %int 0
406 %float = OpTypeFloat 32
407 %v4float = OpTypeVector %float 4
408 %uint = OpTypeInt 32 0
409 %uint_32 = OpConstant %uint 32
410 %_ptr_Input_v4float = OpTypePointer Input %v4float
411 %_ptr_Output_v4float = OpTypePointer Output %v4float
412 %void = OpTypeVoid
413 %uint_128 = OpConstant %uint 128
414 %uint_0 = OpConstant %uint 0
415 %52 = OpTypeFunction %void
416 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
417 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
418 %float_0_5 = OpConstant %float 0.5
419 %130 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
420 %115 = OpExtInst %void %1 DebugInfoNone
421 %49 = OpExtInst %void %1 DebugExpression
422 %17 = OpExtInst %void %1 DebugSource %15
423 %18 = OpExtInst %void %1 DebugCompilationUnit 1 4 %17 HLSL
424 %21 = OpExtInst %void %1 DebugTypeComposite %19 Structure %17 6 1 %18 %19 %uint_128 FlagIsProtected|FlagIsPrivate %22
425 %24 = OpExtInst %void %1 DebugTypeBasic %23 %uint_32 Float
426 %25 = OpExtInst %void %1 DebugTypeVector %24 4
427 %22 = OpExtInst %void %1 DebugTypeMember %26 %25 %17 8 5 %21 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
428 %29 = OpExtInst %void %1 DebugTypeComposite %28 Structure %17 1 1 %18 %28 %uint_128 FlagIsProtected|FlagIsPrivate %30
429 %30 = OpExtInst %void %1 DebugTypeMember %31 %25 %17 3 5 %29 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
430 %32 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %25 %25
431 %34 = OpExtInst %void %1 DebugFunction %33 %32 %17 11 1 %18 %33 FlagIsProtected|FlagIsPrivate 12 %115
432 %36 = OpExtInst %void %1 DebugLexicalBlock %17 12 1 %34
433 %38 = OpExtInst %void %1 DebugLocalVariable %37 %25 %17 13 12 %36 FlagIsLocal
434 %41 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %21 %29
435 %43 = OpExtInst %void %1 DebugFunction %42 %41 %17 17 1 %18 %42 FlagIsProtected|FlagIsPrivate 18 %115
436 %45 = OpExtInst %void %1 DebugLexicalBlock %17 18 1 %43
437 %46 = OpExtInst %void %1 DebugLocalVariable %39 %25 %17 20 12 %45 FlagIsLocal
438 %48 = OpExtInst %void %1 DebugLocalVariable %47 %21 %17 19 15 %45 FlagIsLocal
439 %107 = OpExtInst %void %1 DebugInlinedAt 21 %45
440 %MainPs = OpFunction %void None %52
441 %53 = OpLabel
442 %57 = OpLoad %v4float %in_var_COLOR
443 %131 = OpExtInst %void %1 DebugScope %45
444 OpLine %15 20 12
445 %117 = OpExtInst %void %1 DebugValue %46 %57 %49
446 %132 = OpExtInst %void %1 DebugScope %36 %107
447 OpLine %15 13 19
448 %112 = OpFMul %v4float %57 %130
449 OpLine %15 13 12
450 %116 = OpExtInst %void %1 DebugValue %38 %112 %49
451 %133 = OpExtInst %void %1 DebugScope %45
452 %128 = OpExtInst %void %1 DebugValue %48 %112 %49 %int_0
453 %134 = OpExtInst %void %1 DebugNoScope
454 OpStore %out_var_SV_Target0 %112
455 OpReturn
456 OpFunctionEnd
457 )";
458
459 std::unique_ptr<IRContext> context =
460 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
461 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
462 DebugInfoManager manager(context.get());
463
464 EXPECT_EQ(manager.GetDebugFunction(115), nullptr);
465 }
466
467 TEST(DebugInfoManager, CloneDebugInlinedAt) {
468 const std::string text = R"(
469 OpCapability Shader
470 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
471 OpMemoryModel Logical GLSL450
472 OpEntryPoint Fragment %main "main" %in_var_COLOR
473 OpExecutionMode %main OriginUpperLeft
474 %5 = OpString "ps.hlsl"
475 %14 = OpString "#line 1 \"ps.hlsl\"
476 void main(float in_var_color : COLOR) {
477 float color = in_var_color;
478 }
479 "
480 %17 = OpString "float"
481 %21 = OpString "main"
482 %24 = OpString "color"
483 OpName %in_var_COLOR "in.var.COLOR"
484 OpName %main "main"
485 OpDecorate %in_var_COLOR Location 0
486 %uint = OpTypeInt 32 0
487 %uint_32 = OpConstant %uint 32
488 %float = OpTypeFloat 32
489 %_ptr_Input_float = OpTypePointer Input %float
490 %void = OpTypeVoid
491 %27 = OpTypeFunction %void
492 %_ptr_Function_float = OpTypePointer Function %float
493 %in_var_COLOR = OpVariable %_ptr_Input_float Input
494 %13 = OpExtInst %void %1 DebugExpression
495 %15 = OpExtInst %void %1 DebugSource %5 %14
496 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
497 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
498 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
499 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
500 %100 = OpExtInst %void %1 DebugInlinedAt 7 %22
501 %main = OpFunction %void None %27
502 %28 = OpLabel
503 %31 = OpLoad %float %in_var_COLOR
504 OpStore %100 %31
505 OpReturn
506 OpFunctionEnd
507 )";
508
509 std::unique_ptr<IRContext> context =
510 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
511 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
512 DebugInfoManager manager(context.get());
513
514 EXPECT_EQ(manager.CloneDebugInlinedAt(150), nullptr);
515 EXPECT_EQ(manager.CloneDebugInlinedAt(22), nullptr);
516
517 auto* inst = manager.CloneDebugInlinedAt(100);
518 EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex), 7);
519 EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex), 22);
520 EXPECT_EQ(inst->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
521
522 Instruction* before_100 = nullptr;
523 for (auto it = context->module()->ext_inst_debuginfo_begin();
524 it != context->module()->ext_inst_debuginfo_end(); ++it) {
525 if (it->result_id() == 100) break;
526 before_100 = &*it;
527 }
528 EXPECT_NE(inst, before_100);
529
530 inst = manager.CloneDebugInlinedAt(100, manager.GetDebugInlinedAt(100));
531 EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandLineIndex), 7);
532 EXPECT_EQ(inst->GetSingleWordOperand(kDebugInlinedAtOperandScopeIndex), 22);
533 EXPECT_EQ(inst->NumOperands(), kDebugInlinedAtOperandScopeIndex + 1);
534
535 before_100 = nullptr;
536 for (auto it = context->module()->ext_inst_debuginfo_begin();
537 it != context->module()->ext_inst_debuginfo_end(); ++it) {
538 if (it->result_id() == 100) break;
539 before_100 = &*it;
540 }
541 EXPECT_EQ(inst, before_100);
542 }
543
544 TEST(DebugInfoManager, KillDebugDeclares) {
545 const std::string text = R"(
546 OpCapability Shader
547 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
548 OpMemoryModel Logical GLSL450
549 OpEntryPoint Fragment %main "main" %in_var_COLOR
550 OpExecutionMode %main OriginUpperLeft
551 %5 = OpString "ps.hlsl"
552 %14 = OpString "#line 1 \"ps.hlsl\"
553 void main(float in_var_color : COLOR) {
554 float color = in_var_color;
555 }
556 "
557 %17 = OpString "float"
558 %21 = OpString "main"
559 %24 = OpString "color"
560 OpName %in_var_COLOR "in.var.COLOR"
561 OpName %main "main"
562 OpDecorate %in_var_COLOR Location 0
563 %uint = OpTypeInt 32 0
564 %uint_32 = OpConstant %uint 32
565 %float = OpTypeFloat 32
566 %_ptr_Input_float = OpTypePointer Input %float
567 %void = OpTypeVoid
568 %27 = OpTypeFunction %void
569 %_ptr_Function_float = OpTypePointer Function %float
570 %in_var_COLOR = OpVariable %_ptr_Input_float Input
571 %13 = OpExtInst %void %1 DebugExpression
572 %15 = OpExtInst %void %1 DebugSource %5 %14
573 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
574 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
575 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %18 %18
576 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %main
577 %12 = OpExtInst %void %1 DebugInfoNone
578 %25 = OpExtInst %void %1 DebugLocalVariable %24 %18 %15 1 20 %22 FlagIsLocal 0
579 %main = OpFunction %void None %27
580 %28 = OpLabel
581 %100 = OpVariable %_ptr_Function_float Function
582 %31 = OpLoad %float %in_var_COLOR
583 OpStore %100 %31
584 %36 = OpExtInst %void %1 DebugDeclare %25 %100 %13
585 %37 = OpExtInst %void %1 DebugDeclare %25 %100 %13
586 %38 = OpExtInst %void %1 DebugDeclare %25 %100 %13
587 OpReturn
588 OpFunctionEnd
589 )";
590
591 std::unique_ptr<IRContext> context =
592 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
593 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
594 auto* dbg_info_mgr = context->get_debug_info_mgr();
595 auto* def_use_mgr = context->get_def_use_mgr();
596
597 EXPECT_TRUE(dbg_info_mgr->IsVariableDebugDeclared(100));
598 EXPECT_EQ(def_use_mgr->GetDef(36)->GetOpenCL100DebugOpcode(),
599 OpenCLDebugInfo100DebugDeclare);
600 EXPECT_EQ(def_use_mgr->GetDef(37)->GetOpenCL100DebugOpcode(),
601 OpenCLDebugInfo100DebugDeclare);
602 EXPECT_EQ(def_use_mgr->GetDef(38)->GetOpenCL100DebugOpcode(),
603 OpenCLDebugInfo100DebugDeclare);
604
605 dbg_info_mgr->KillDebugDeclares(100);
606 EXPECT_EQ(def_use_mgr->GetDef(36), nullptr);
607 EXPECT_EQ(def_use_mgr->GetDef(37), nullptr);
608 EXPECT_EQ(def_use_mgr->GetDef(38), nullptr);
609 EXPECT_FALSE(dbg_info_mgr->IsVariableDebugDeclared(100));
610 }
611
612 } // namespace
613 } // namespace analysis
614 } // namespace opt
615 } // namespace spvtools