Debian Codegraph @

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

raw
1 // Copyright (c) 2016 Google Inc.
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 <algorithm>
16 #include <memory>
17 #include <string>
18 #include <unordered_set>
19 #include <utility>
20 #include <vector>
21
22 #include "gtest/gtest.h"
23 #include "source/opt/build_module.h"
24 #include "source/opt/def_use_manager.h"
25 #include "source/opt/ir_context.h"
26 #include "spirv-tools/libspirv.hpp"
27
28 namespace spvtools {
29 namespace opt {
30 namespace {
31
32 constexpr uint32_t kOpLineOperandLineIndex = 1;
33
34 void DoRoundTripCheck(const std::string& text) {
35 SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
36 std::unique_ptr<IRContext> context =
37 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
38 ASSERT_NE(nullptr, context) << "Failed to assemble\n" << text;
39
40 std::vector<uint32_t> binary;
41 context->module()->ToBinary(&binary, /* skip_nop = */ false);
42
43 std::string disassembled_text;
44 EXPECT_TRUE(t.Disassemble(binary, &disassembled_text));
45 EXPECT_EQ(text, disassembled_text);
46 }
47
48 TEST(IrBuilder, RoundTrip) {
49 // #version 310 es
50 // int add(int a, int b) { return a + b; }
51 // void main() { add(1, 2); }
52 DoRoundTripCheck(
53 // clang-format off
54 "OpCapability Shader\n"
55 "%1 = OpExtInstImport \"GLSL.std.450\"\n"
56 "OpMemoryModel Logical GLSL450\n"
57 "OpEntryPoint Vertex %main \"main\"\n"
58 "OpSource ESSL 310\n"
59 "OpSourceExtension \"GL_GOOGLE_cpp_style_line_directive\"\n"
60 "OpSourceExtension \"GL_GOOGLE_include_directive\"\n"
61 "OpName %main \"main\"\n"
62 "OpName %add_i1_i1_ \"add(i1;i1;\"\n"
63 "OpName %a \"a\"\n"
64 "OpName %b \"b\"\n"
65 "OpName %param \"param\"\n"
66 "OpName %param_0 \"param\"\n"
67 "%void = OpTypeVoid\n"
68 "%9 = OpTypeFunction %void\n"
69 "%int = OpTypeInt 32 1\n"
70 "%_ptr_Function_int = OpTypePointer Function %int\n"
71 "%12 = OpTypeFunction %int %_ptr_Function_int %_ptr_Function_int\n"
72 "%int_1 = OpConstant %int 1\n"
73 "%int_2 = OpConstant %int 2\n"
74 "%main = OpFunction %void None %9\n"
75 "%15 = OpLabel\n"
76 "%param = OpVariable %_ptr_Function_int Function\n"
77 "%param_0 = OpVariable %_ptr_Function_int Function\n"
78 "OpStore %param %int_1\n"
79 "OpStore %param_0 %int_2\n"
80 "%16 = OpFunctionCall %int %add_i1_i1_ %param %param_0\n"
81 "OpReturn\n"
82 "OpFunctionEnd\n"
83 "%add_i1_i1_ = OpFunction %int None %12\n"
84 "%a = OpFunctionParameter %_ptr_Function_int\n"
85 "%b = OpFunctionParameter %_ptr_Function_int\n"
86 "%17 = OpLabel\n"
87 "%18 = OpLoad %int %a\n"
88 "%19 = OpLoad %int %b\n"
89 "%20 = OpIAdd %int %18 %19\n"
90 "OpReturnValue %20\n"
91 "OpFunctionEnd\n");
92 // clang-format on
93 }
94
95 TEST(IrBuilder, RoundTripIncompleteBasicBlock) {
96 DoRoundTripCheck(
97 "%2 = OpFunction %1 None %3\n"
98 "%4 = OpLabel\n"
99 "OpNop\n");
100 }
101
102 TEST(IrBuilder, RoundTripIncompleteFunction) {
103 DoRoundTripCheck("%2 = OpFunction %1 None %3\n");
104 }
105
106 TEST(IrBuilder, KeepLineDebugInfo) {
107 // #version 310 es
108 // void main() {}
109 DoRoundTripCheck(
110 // clang-format off
111 "OpCapability Shader\n"
112 "%1 = OpExtInstImport \"GLSL.std.450\"\n"
113 "OpMemoryModel Logical GLSL450\n"
114 "OpEntryPoint Vertex %main \"main\"\n"
115 "%3 = OpString \"minimal.vert\"\n"
116 "OpSource ESSL 310\n"
117 "OpName %main \"main\"\n"
118 "OpLine %3 10 10\n"
119 "%void = OpTypeVoid\n"
120 "OpLine %3 100 100\n"
121 "%5 = OpTypeFunction %void\n"
122 "%main = OpFunction %void None %5\n"
123 "OpLine %3 1 1\n"
124 "OpNoLine\n"
125 "OpLine %3 2 2\n"
126 "OpLine %3 3 3\n"
127 "%6 = OpLabel\n"
128 "OpLine %3 4 4\n"
129 "OpNoLine\n"
130 "OpReturn\n"
131 "OpFunctionEnd\n");
132 // clang-format on
133 }
134
135 TEST(IrBuilder, DistributeLineDebugInfo) {
136 const std::string text =
137 // clang-format off
138 "OpCapability Shader\n"
139 "%1 = OpExtInstImport \"GLSL.std.450\"\n"
140 "OpMemoryModel Logical GLSL450\n"
141 "OpEntryPoint Vertex %main \"main\"\n"
142 "OpSource ESSL 310\n"
143 "%file = OpString \"test\"\n"
144 "OpName %main \"main\"\n"
145 "OpName %f_ \"f(\"\n"
146 "OpName %gv1 \"gv1\"\n"
147 "OpName %gv2 \"gv2\"\n"
148 "OpName %lv1 \"lv1\"\n"
149 "OpName %lv2 \"lv2\"\n"
150 "OpName %lv1_0 \"lv1\"\n"
151 "%void = OpTypeVoid\n"
152 "%10 = OpTypeFunction %void\n"
153 "OpLine %file 10 0\n"
154 "%float = OpTypeFloat 32\n"
155 "%12 = OpTypeFunction %float\n"
156 "%_ptr_Private_float = OpTypePointer Private %float\n"
157 "%gv1 = OpVariable %_ptr_Private_float Private\n"
158 "%float_10 = OpConstant %float 10\n"
159 "%gv2 = OpVariable %_ptr_Private_float Private\n"
160 "%float_100 = OpConstant %float 100\n"
161 "%_ptr_Function_float = OpTypePointer Function %float\n"
162 "%main = OpFunction %void None %10\n"
163 "%17 = OpLabel\n"
164 "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
165 "OpStore %gv1 %float_10\n"
166 "OpStore %gv2 %float_100\n"
167 "OpLine %file 1 0\n"
168 "OpNoLine\n"
169 "OpLine %file 2 0\n"
170 "%18 = OpLoad %float %gv1\n"
171 "%19 = OpLoad %float %gv2\n"
172 "%20 = OpFSub %float %18 %19\n"
173 "OpStore %lv1_0 %20\n"
174 "OpReturn\n"
175 "OpFunctionEnd\n"
176 "%f_ = OpFunction %float None %12\n"
177 "%21 = OpLabel\n"
178 "%lv1 = OpVariable %_ptr_Function_float Function\n"
179 "%lv2 = OpVariable %_ptr_Function_float Function\n"
180 "OpLine %file 3 0\n"
181 "OpLine %file 4 0\n"
182 "%22 = OpLoad %float %gv1\n"
183 "%23 = OpLoad %float %gv2\n"
184 "%24 = OpFAdd %float %22 %23\n"
185 "OpStore %lv1 %24\n"
186 "OpLine %file 5 0\n"
187 "OpLine %file 6 0\n"
188 "OpNoLine\n"
189 "%25 = OpLoad %float %gv1\n"
190 "%26 = OpLoad %float %gv2\n"
191 "%27 = OpFMul %float %25 %26\n"
192 "OpBranch %28\n"
193 "%28 = OpLabel\n"
194 "OpStore %lv2 %27\n"
195 "%29 = OpLoad %float %lv1\n"
196 "OpLine %file 7 0\n"
197 "%30 = OpLoad %float %lv2\n"
198 "%31 = OpFDiv %float %28 %29\n"
199 "OpReturnValue %30\n"
200 "OpFunctionEnd\n";
201 // clang-format on
202
203 std::unique_ptr<IRContext> context =
204 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
205 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
206 ASSERT_NE(nullptr, context);
207
208 struct LineInstrCheck {
209 uint32_t id;
210 std::vector<uint32_t> line_numbers;
211 };
212 const uint32_t kNoLine = 0;
213 const LineInstrCheck line_checks[] = {
214 {12, {10}}, {18, {1, kNoLine, 2}},
215 {19, {2}}, {20, {2}},
216 {22, {3, 4}}, {23, {4}},
217 {24, {4}}, {25, {5, 6, kNoLine}},
218 {26, {}}, {27, {}},
219 {28, {}}, {29, {}},
220 {30, {7}}, {31, {7}},
221 };
222
223 spvtools::opt::analysis::DefUseManager* def_use_mgr =
224 context->get_def_use_mgr();
225 for (const LineInstrCheck& check : line_checks) {
226 auto& lines = def_use_mgr->GetDef(check.id)->dbg_line_insts();
227 for (uint32_t i = 0; i < check.line_numbers.size(); ++i) {
228 if (check.line_numbers[i] == kNoLine) {
229 EXPECT_EQ(lines[i].opcode(), SpvOpNoLine);
230 continue;
231 }
232 EXPECT_EQ(lines[i].opcode(), SpvOpLine);
233 EXPECT_EQ(lines[i].GetSingleWordOperand(kOpLineOperandLineIndex),
234 check.line_numbers[i]);
235 }
236 }
237 }
238
239 TEST(IrBuilder, ConsumeDebugInfoInst) {
240 // /* HLSL */
241 //
242 // struct VS_OUTPUT {
243 // float4 pos : SV_POSITION;
244 // float4 color : COLOR;
245 // };
246 //
247 // VS_OUTPUT main(float4 pos : POSITION,
248 // float4 color : COLOR) {
249 // VS_OUTPUT vout;
250 // vout.pos = pos;
251 // vout.color = color;
252 // return vout;
253 // }
254 DoRoundTripCheck(R"(OpCapability Shader
255 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
256 OpMemoryModel Logical GLSL450
257 OpEntryPoint Vertex %main "main" %pos %color %gl_Position %out_var_COLOR
258 %7 = OpString "simple_vs.hlsl"
259 %8 = OpString "#line 1 \"simple_vs.hlsl\"
260 struct VS_OUTPUT {
261 float4 pos : SV_POSITION;
262 float4 color : COLOR;
263 };
264
265 VS_OUTPUT main(float4 pos : POSITION,
266 float4 color : COLOR) {
267 VS_OUTPUT vout;
268 vout.pos = pos;
269 vout.color = color;
270 return vout;
271 }
272 "
273 OpSource HLSL 600 %7 "#line 1 \"simple_vs.hlsl\"
274 struct VS_OUTPUT {
275 float4 pos : SV_POSITION;
276 float4 color : COLOR;
277 };
278
279 VS_OUTPUT main(float4 pos : POSITION,
280 float4 color : COLOR) {
281 VS_OUTPUT vout;
282 vout.pos = pos;
283 vout.color = color;
284 return vout;
285 }
286 "
287 %9 = OpString "struct VS_OUTPUT"
288 %10 = OpString "float"
289 %11 = OpString "pos : SV_POSITION"
290 %12 = OpString "color : COLOR"
291 %13 = OpString "VS_OUTPUT"
292 %14 = OpString "main"
293 %15 = OpString "VS_OUTPUT_main_v4f_v4f"
294 %16 = OpString "pos : POSITION"
295 %17 = OpString "color : COLOR"
296 %18 = OpString "vout"
297 OpName %out_var_COLOR "out.var.COLOR"
298 OpName %main "main"
299 OpName %VS_OUTPUT "VS_OUTPUT"
300 OpMemberName %VS_OUTPUT 0 "pos"
301 OpMemberName %VS_OUTPUT 1 "color"
302 OpName %pos "pos"
303 OpName %color "color"
304 OpName %vout "vout"
305 OpDecorate %gl_Position BuiltIn Position
306 OpDecorate %pos Location 0
307 OpDecorate %color Location 1
308 OpDecorate %out_var_COLOR Location 0
309 %int = OpTypeInt 32 1
310 %int_0 = OpConstant %int 0
311 %int_1 = OpConstant %int 1
312 %int_32 = OpConstant %int 32
313 %int_128 = OpConstant %int 128
314 %float = OpTypeFloat 32
315 %v4float = OpTypeVector %float 4
316 %_ptr_Input_v4float = OpTypePointer Input %v4float
317 %_ptr_Output_v4float = OpTypePointer Output %v4float
318 %void = OpTypeVoid
319 %31 = OpTypeFunction %void
320 %_ptr_Function_v4float = OpTypePointer Function %v4float
321 %VS_OUTPUT = OpTypeStruct %v4float %v4float
322 %_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT
323 OpLine %7 6 23
324 %pos = OpVariable %_ptr_Input_v4float Input
325 OpLine %7 7 23
326 %color = OpVariable %_ptr_Input_v4float Input
327 OpLine %7 2 16
328 %gl_Position = OpVariable %_ptr_Output_v4float Output
329 OpLine %7 3 18
330 %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
331 %34 = OpExtInst %void %1 DebugSource %7 %8
332 %35 = OpExtInst %void %1 DebugCompilationUnit 2 4 %34 HLSL
333 %36 = OpExtInst %void %1 DebugTypeComposite %9 Structure %34 1 1 %35 %13 %int_128 FlagIsProtected|FlagIsPrivate %37 %38
334 %39 = OpExtInst %void %1 DebugTypeBasic %10 %int_32 Float
335 %40 = OpExtInst %void %1 DebugTypeVector %39 4
336 %37 = OpExtInst %void %1 DebugTypeMember %11 %40 %34 2 3 %36 %int_0 %int_128 FlagIsProtected|FlagIsPrivate
337 %38 = OpExtInst %void %1 DebugTypeMember %12 %40 %34 3 3 %36 %int_128 %int_128 FlagIsProtected|FlagIsPrivate
338 %41 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %36 %40 %40
339 %42 = OpExtInst %void %1 DebugExpression
340 %43 = OpExtInst %void %1 DebugFunction %14 %41 %34 6 1 %35 %15 FlagIsProtected|FlagIsPrivate 7 %main
341 %44 = OpExtInst %void %1 DebugLocalVariable %16 %40 %34 6 16 %43 FlagIsLocal 0
342 %45 = OpExtInst %void %1 DebugLocalVariable %17 %40 %34 7 16 %43 FlagIsLocal 1
343 %46 = OpExtInst %void %1 DebugLocalVariable %18 %36 %34 8 3 %43 FlagIsLocal
344 OpLine %7 6 1
345 %main = OpFunction %void None %31
346 %47 = OpLabel
347 %60 = OpExtInst %void %1 DebugScope %43
348 OpLine %7 8 13
349 %vout = OpVariable %_ptr_Function_VS_OUTPUT Function
350 %49 = OpExtInst %void %1 DebugDeclare %46 %vout %42
351 OpLine %7 9 14
352 %50 = OpLoad %v4float %pos
353 OpLine %7 9 3
354 %51 = OpAccessChain %_ptr_Function_v4float %vout %int_0
355 %52 = OpExtInst %void %1 DebugValue %46 %51 %42 %int_0
356 OpStore %51 %50
357 OpLine %7 10 16
358 %53 = OpLoad %v4float %color
359 OpLine %7 10 3
360 %54 = OpAccessChain %_ptr_Function_v4float %vout %int_1
361 %55 = OpExtInst %void %1 DebugValue %46 %54 %42 %int_1
362 OpStore %54 %53
363 OpLine %7 11 10
364 %56 = OpLoad %VS_OUTPUT %vout
365 OpLine %7 11 3
366 %57 = OpCompositeExtract %v4float %56 0
367 OpStore %gl_Position %57
368 %58 = OpCompositeExtract %v4float %56 1
369 OpStore %out_var_COLOR %58
370 %61 = OpExtInst %void %1 DebugNoScope
371 OpReturn
372 OpFunctionEnd
373 )");
374 }
375
376 TEST(IrBuilder, ConsumeDebugInfoLexicalScopeInst) {
377 // /* HLSL */
378 //
379 // float4 func2(float arg2) { // func2_block
380 // return float4(arg2, 0, 0, 0);
381 // }
382 //
383 // float4 func1(float arg1) { // func1_block
384 // if (arg1 > 1) { // if_true_block
385 // return float4(0, 0, 0, 0);
386 // }
387 // return func2(arg1); // if_merge_block
388 // }
389 //
390 // float4 main(float pos : POSITION) : SV_POSITION { // main
391 // return func1(pos);
392 // }
393 DoRoundTripCheck(R"(OpCapability Shader
394 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
395 OpMemoryModel Logical GLSL450
396 OpEntryPoint Vertex %main "main" %pos %gl_Position
397 %5 = OpString "block/block.hlsl"
398 %6 = OpString "#line 1 \"block/block.hlsl\"
399 float4 func2(float arg2) {
400 return float4(arg2, 0, 0, 0);
401 }
402
403 float4 func1(float arg1) {
404 if (arg1 > 1) {
405 return float4(0, 0, 0, 0);
406 }
407 return func2(arg1);
408 }
409
410 float4 main(float pos : POSITION) : SV_POSITION {
411 return func1(pos);
412 }
413 "
414 OpSource HLSL 600 %5 "#line 1 \"block/block.hlsl\"
415 float4 func2(float arg2) {
416 return float4(arg2, 0, 0, 0);
417 }
418
419 float4 func1(float arg1) {
420 if (arg1 > 1) {
421 return float4(0, 0, 0, 0);
422 }
423 return func2(arg1);
424 }
425
426 float4 main(float pos : POSITION) : SV_POSITION {
427 return func1(pos);
428 }
429 "
430 %7 = OpString "float"
431 %8 = OpString "main"
432 %9 = OpString "v4f_main_f"
433 %10 = OpString "v4f_func1_f"
434 %11 = OpString "v4f_func2_f"
435 %12 = OpString "pos : POSITION"
436 %13 = OpString "func1"
437 %14 = OpString "func2"
438 OpName %main "main"
439 OpName %pos "pos"
440 OpName %bb_entry "bb.entry"
441 OpName %param_var_arg1 "param.var.arg1"
442 OpName %func1 "func1"
443 OpName %arg1 "arg1"
444 OpName %bb_entry_0 "bb.entry"
445 OpName %param_var_arg2 "param.var.arg2"
446 OpName %if_true "if.true"
447 OpName %if_merge "if.merge"
448 OpName %func2 "func2"
449 OpName %arg2 "arg2"
450 OpName %bb_entry_1 "bb.entry"
451 OpDecorate %gl_Position BuiltIn Position
452 OpDecorate %pos Location 0
453 %float = OpTypeFloat 32
454 %int = OpTypeInt 32 1
455 %float_1 = OpConstant %float 1
456 %float_0 = OpConstant %float 0
457 %int_32 = OpConstant %int 32
458 %v4float = OpTypeVector %float 4
459 %32 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
460 %_ptr_Input_float = OpTypePointer Input %float
461 %_ptr_Output_v4float = OpTypePointer Output %v4float
462 %void = OpTypeVoid
463 %36 = OpTypeFunction %void
464 %_ptr_Function_float = OpTypePointer Function %float
465 %38 = OpTypeFunction %v4float %_ptr_Function_float
466 %bool = OpTypeBool
467 OpLine %5 12 25
468 %pos = OpVariable %_ptr_Input_float Input
469 OpLine %5 12 37
470 %gl_Position = OpVariable %_ptr_Output_v4float Output
471 %40 = OpExtInst %void %1 DebugSource %5 %6
472 %41 = OpExtInst %void %1 DebugCompilationUnit 2 4 %40 HLSL
473 %42 = OpExtInst %void %1 DebugTypeBasic %7 %int_32 Float
474 %43 = OpExtInst %void %1 DebugTypeVector %42 4
475 %44 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %42
476 %45 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %42
477 %46 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %42
478 %47 = OpExtInst %void %1 DebugFunction %8 %44 %40 12 1 %41 %9 FlagIsProtected|FlagIsPrivate 13 %main
479 %48 = OpExtInst %void %1 DebugFunction %13 %45 %40 5 1 %41 %10 FlagIsProtected|FlagIsPrivate 13 %func1
480 %49 = OpExtInst %void %1 DebugFunction %14 %46 %40 1 1 %41 %11 FlagIsProtected|FlagIsPrivate 13 %func2
481 %50 = OpExtInst %void %1 DebugLexicalBlock %40 6 17 %48
482 %51 = OpExtInst %void %1 DebugLexicalBlock %40 9 3 %48
483 OpLine %5 12 1
484 %main = OpFunction %void None %36
485 %bb_entry = OpLabel
486 %70 = OpExtInst %void %1 DebugScope %47
487 OpLine %5 13 16
488 %param_var_arg1 = OpVariable %_ptr_Function_float Function
489 %53 = OpLoad %float %pos
490 OpStore %param_var_arg1 %53
491 OpLine %5 13 10
492 %54 = OpFunctionCall %v4float %func1 %param_var_arg1
493 OpLine %5 13 3
494 OpStore %gl_Position %54
495 %71 = OpExtInst %void %1 DebugNoScope
496 OpReturn
497 OpFunctionEnd
498 OpLine %5 5 1
499 %func1 = OpFunction %v4float None %38
500 OpLine %5 5 20
501 %arg1 = OpFunctionParameter %_ptr_Function_float
502 %bb_entry_0 = OpLabel
503 %72 = OpExtInst %void %1 DebugScope %48
504 OpLine %5 9 16
505 %param_var_arg2 = OpVariable %_ptr_Function_float Function
506 OpLine %5 6 7
507 %57 = OpLoad %float %arg1
508 OpLine %5 6 12
509 %58 = OpFOrdGreaterThan %bool %57 %float_1
510 OpLine %5 6 17
511 %73 = OpExtInst %void %1 DebugNoScope
512 OpSelectionMerge %if_merge None
513 OpBranchConditional %58 %if_true %if_merge
514 %if_true = OpLabel
515 %74 = OpExtInst %void %1 DebugScope %50
516 OpLine %5 7 5
517 %75 = OpExtInst %void %1 DebugNoScope
518 OpReturnValue %32
519 %if_merge = OpLabel
520 %76 = OpExtInst %void %1 DebugScope %51
521 OpLine %5 9 16
522 %63 = OpLoad %float %arg1
523 OpStore %param_var_arg2 %63
524 OpLine %5 9 10
525 %64 = OpFunctionCall %v4float %func2 %param_var_arg2
526 OpLine %5 9 3
527 %77 = OpExtInst %void %1 DebugNoScope
528 OpReturnValue %64
529 OpFunctionEnd
530 OpLine %5 1 1
531 %func2 = OpFunction %v4float None %38
532 OpLine %5 1 20
533 %arg2 = OpFunctionParameter %_ptr_Function_float
534 %bb_entry_1 = OpLabel
535 %78 = OpExtInst %void %1 DebugScope %49
536 OpLine %5 2 17
537 %67 = OpLoad %float %arg2
538 %68 = OpCompositeConstruct %v4float %67 %float_0 %float_0 %float_0
539 OpLine %5 2 3
540 %79 = OpExtInst %void %1 DebugNoScope
541 OpReturnValue %68
542 OpFunctionEnd
543 )");
544 }
545
546 TEST(IrBuilder, ConsumeDebugInlinedAt) {
547 // /* HLSL */
548 //
549 // float4 func2(float arg2) { // func2_block
550 // return float4(arg2, 0, 0, 0);
551 // }
552 //
553 // float4 func1(float arg1) { // func1_block
554 // if (arg1 > 1) { // if_true_block
555 // return float4(0, 0, 0, 0);
556 // }
557 // return func2(arg1); // if_merge_block
558 // }
559 //
560 // float4 main(float pos : POSITION) : SV_POSITION { // main
561 // return func1(pos);
562 // }
563 //
564 // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/533): In the following
565 // SPIRV code, we use DebugInfoNone to reference opted-out function from
566 // DebugFunction similar to opted-out global variable for DebugGlobalVariable,
567 // but this is not a part of the spec yet. We are still in discussion and we
568 // must correct it if our decision is different.
569 DoRoundTripCheck(R"(OpCapability Shader
570 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
571 OpMemoryModel Logical GLSL450
572 OpEntryPoint Vertex %main "main" %pos %gl_Position
573 %5 = OpString "block/block.hlsl"
574 %6 = OpString "#line 1 \"block/block.hlsl\"
575 float4 func2(float arg2) {
576 return float4(arg2, 0, 0, 0);
577 }
578
579 float4 func1(float arg1) {
580 if (arg1 > 1) {
581 return float4(0, 0, 0, 0);
582 }
583 return func2(arg1);
584 }
585
586 float4 main(float pos : POSITION) : SV_POSITION {
587 return func1(pos);
588 }
589 "
590 OpSource HLSL 600 %5 "#line 1 \"block/block.hlsl\"
591 float4 func2(float arg2) {
592 return float4(arg2, 0, 0, 0);
593 }
594
595 float4 func1(float arg1) {
596 if (arg1 > 1) {
597 return float4(0, 0, 0, 0);
598 }
599 return func2(arg1);
600 }
601
602 float4 main(float pos : POSITION) : SV_POSITION {
603 return func1(pos);
604 }
605 "
606 %7 = OpString "float"
607 %8 = OpString "main"
608 %9 = OpString "v4f_main_f"
609 %10 = OpString "v4f_func1_f"
610 %11 = OpString "v4f_func2_f"
611 %12 = OpString "pos : POSITION"
612 %13 = OpString "func1"
613 %14 = OpString "func2"
614 OpName %main "main"
615 OpName %pos "pos"
616 OpName %bb_entry "bb.entry"
617 OpName %if_true "if.true"
618 OpName %if_merge "if.merge"
619 OpDecorate %gl_Position BuiltIn Position
620 OpDecorate %pos Location 0
621 %float = OpTypeFloat 32
622 %int = OpTypeInt 32 1
623 %float_1 = OpConstant %float 1
624 %float_0 = OpConstant %float 0
625 %int_32 = OpConstant %int 32
626 %v4float = OpTypeVector %float 4
627 %24 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
628 %_ptr_Input_float = OpTypePointer Input %float
629 %_ptr_Output_v4float = OpTypePointer Output %v4float
630 %void = OpTypeVoid
631 %28 = OpTypeFunction %void
632 %_ptr_Function_float = OpTypePointer Function %float
633 %30 = OpTypeFunction %v4float %_ptr_Function_float
634 %bool = OpTypeBool
635 OpLine %5 12 25
636 %pos = OpVariable %_ptr_Input_float Input
637 OpLine %5 12 37
638 %gl_Position = OpVariable %_ptr_Output_v4float Output
639 %32 = OpExtInst %void %1 DebugInfoNone
640 %33 = OpExtInst %void %1 DebugSource %5 %6
641 %34 = OpExtInst %void %1 DebugCompilationUnit 2 4 %33 HLSL
642 %35 = OpExtInst %void %1 DebugTypeBasic %7 %int_32 Float
643 %36 = OpExtInst %void %1 DebugTypeVector %35 4
644 %37 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %36 %35
645 %38 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %36 %35
646 %39 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %36 %35
647 %40 = OpExtInst %void %1 DebugFunction %8 %37 %33 12 1 %34 %9 FlagIsProtected|FlagIsPrivate 13 %main
648 %41 = OpExtInst %void %1 DebugFunction %13 %38 %33 5 1 %34 %10 FlagIsProtected|FlagIsPrivate 13 %32
649 %42 = OpExtInst %void %1 DebugFunction %14 %39 %33 1 1 %34 %11 FlagIsProtected|FlagIsPrivate 13 %32
650 %43 = OpExtInst %void %1 DebugLexicalBlock %33 12 49 %40
651 %44 = OpExtInst %void %1 DebugLexicalBlock %33 5 26 %41
652 %45 = OpExtInst %void %1 DebugLexicalBlock %33 1 26 %42
653 %46 = OpExtInst %void %1 DebugLexicalBlock %33 6 17 %44
654 %47 = OpExtInst %void %1 DebugLexicalBlock %33 9 3 %44
655 %48 = OpExtInst %void %1 DebugInlinedAt 9 %47
656 %49 = OpExtInst %void %1 DebugInlinedAt 13 %43
657 %50 = OpExtInst %void %1 DebugInlinedAt 13 %43 %48
658 OpLine %5 12 1
659 %main = OpFunction %void None %28
660 %bb_entry = OpLabel
661 %62 = OpExtInst %void %1 DebugScope %44 %49
662 OpLine %5 6 7
663 %52 = OpLoad %float %pos
664 OpLine %5 6 12
665 %53 = OpFOrdGreaterThan %bool %52 %float_1
666 OpLine %5 6 17
667 %63 = OpExtInst %void %1 DebugNoScope
668 OpSelectionMerge %if_merge None
669 OpBranchConditional %53 %if_true %if_merge
670 %if_true = OpLabel
671 %64 = OpExtInst %void %1 DebugScope %46 %49
672 OpLine %5 7 5
673 OpStore %gl_Position %24
674 %65 = OpExtInst %void %1 DebugNoScope
675 OpReturn
676 %if_merge = OpLabel
677 %66 = OpExtInst %void %1 DebugScope %45 %50
678 OpLine %5 2 17
679 %58 = OpLoad %float %pos
680 OpLine %5 2 10
681 %59 = OpCompositeConstruct %v4float %58 %float_0 %float_0 %float_0
682 %67 = OpExtInst %void %1 DebugScope %43
683 OpLine %5 13 3
684 OpStore %gl_Position %59
685 %68 = OpExtInst %void %1 DebugNoScope
686 OpReturn
687 OpFunctionEnd
688 )");
689 }
690
691 TEST(IrBuilder, DebugInfoInstInFunctionOutOfBlock) {
692 // /* HLSL */
693 //
694 // float4 func2(float arg2) { // func2_block
695 // return float4(arg2, 0, 0, 0);
696 // }
697 //
698 // float4 func1(float arg1) { // func1_block
699 // if (arg1 > 1) { // if_true_block
700 // return float4(0, 0, 0, 0);
701 // }
702 // return func2(arg1); // if_merge_block
703 // }
704 //
705 // float4 main(float pos : POSITION) : SV_POSITION { // main
706 // return func1(pos);
707 // }
708 const std::string text = R"(OpCapability Shader
709 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
710 OpMemoryModel Logical GLSL450
711 OpEntryPoint Vertex %main "main" %pos %gl_Position
712 %5 = OpString "block/block.hlsl"
713 %6 = OpString "#line 1 \"block/block.hlsl\"
714 float4 func2(float arg2) {
715 return float4(arg2, 0, 0, 0);
716 }
717
718 float4 func1(float arg1) {
719 if (arg1 > 1) {
720 return float4(0, 0, 0, 0);
721 }
722 return func2(arg1);
723 }
724
725 float4 main(float pos : POSITION) : SV_POSITION {
726 return func1(pos);
727 }
728 "
729 OpSource HLSL 600 %5 "#line 1 \"block/block.hlsl\"
730 float4 func2(float arg2) {
731 return float4(arg2, 0, 0, 0);
732 }
733
734 float4 func1(float arg1) {
735 if (arg1 > 1) {
736 return float4(0, 0, 0, 0);
737 }
738 return func2(arg1);
739 }
740
741 float4 main(float pos : POSITION) : SV_POSITION {
742 return func1(pos);
743 }
744 "
745 %7 = OpString "float"
746 %8 = OpString "main"
747 %9 = OpString "v4f_main_f"
748 %10 = OpString "v4f_func1_f"
749 %11 = OpString "v4f_func2_f"
750 %12 = OpString "pos : POSITION"
751 %13 = OpString "func1"
752 %14 = OpString "func2"
753 OpName %main "main"
754 OpName %pos "pos"
755 OpName %bb_entry "bb.entry"
756 OpName %param_var_arg1 "param.var.arg1"
757 OpName %func1 "func1"
758 OpName %arg1 "arg1"
759 OpName %bb_entry_0 "bb.entry"
760 OpName %param_var_arg2 "param.var.arg2"
761 OpName %if_true "if.true"
762 OpName %if_merge "if.merge"
763 OpName %func2 "func2"
764 OpName %arg2 "arg2"
765 OpName %bb_entry_1 "bb.entry"
766 OpDecorate %gl_Position BuiltIn Position
767 OpDecorate %pos Location 0
768 %float = OpTypeFloat 32
769 %int = OpTypeInt 32 1
770 %float_1 = OpConstant %float 1
771 %float_0 = OpConstant %float 0
772 %int_32 = OpConstant %int 32
773 %v4float = OpTypeVector %float 4
774 %32 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
775 %_ptr_Input_float = OpTypePointer Input %float
776 %_ptr_Output_v4float = OpTypePointer Output %v4float
777 %void = OpTypeVoid
778 %36 = OpTypeFunction %void
779 %_ptr_Function_float = OpTypePointer Function %float
780 %38 = OpTypeFunction %v4float %_ptr_Function_float
781 %bool = OpTypeBool
782 OpLine %5 12 25
783 %pos = OpVariable %_ptr_Input_float Input
784 OpLine %5 12 37
785 %gl_Position = OpVariable %_ptr_Output_v4float Output
786 %40 = OpExtInst %void %1 DebugSource %5 %6
787 %41 = OpExtInst %void %1 DebugCompilationUnit 2 4 %40 HLSL
788 %42 = OpExtInst %void %1 DebugTypeBasic %7 %int_32 Float
789 %43 = OpExtInst %void %1 DebugTypeVector %42 4
790 %44 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %42
791 %45 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %42
792 %46 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %42
793 %47 = OpExtInst %void %1 DebugFunction %8 %44 %40 12 1 %41 %9 FlagIsProtected|FlagIsPrivate 13 %main
794 %48 = OpExtInst %void %1 DebugFunction %13 %45 %40 5 1 %41 %10 FlagIsProtected|FlagIsPrivate 13 %func1
795 %49 = OpExtInst %void %1 DebugFunction %14 %46 %40 1 1 %41 %11 FlagIsProtected|FlagIsPrivate 13 %func2
796 %50 = OpExtInst %void %1 DebugLexicalBlock %40 6 17 %48
797 %51 = OpExtInst %void %1 DebugLexicalBlock %40 9 3 %48
798 OpLine %5 12 1
799 %main = OpFunction %void None %36
800 %bb_entry = OpLabel
801 %70 = OpExtInst %void %1 DebugScope %47
802 OpLine %5 13 16
803 %param_var_arg1 = OpVariable %_ptr_Function_float Function
804 %53 = OpLoad %float %pos
805 OpStore %param_var_arg1 %53
806 OpLine %5 13 10
807 %54 = OpFunctionCall %v4float %func1 %param_var_arg1
808 OpLine %5 13 3
809 OpStore %gl_Position %54
810 %71 = OpExtInst %void %1 DebugNoScope
811 OpReturn
812 OpFunctionEnd
813 OpLine %5 5 1
814 %func1 = OpFunction %v4float None %38
815 OpLine %5 5 20
816 %arg1 = OpFunctionParameter %_ptr_Function_float
817 %bb_entry_0 = OpLabel
818 %72 = OpExtInst %void %1 DebugScope %48
819 OpLine %5 9 16
820 %param_var_arg2 = OpVariable %_ptr_Function_float Function
821 OpLine %5 6 7
822 %57 = OpLoad %float %arg1
823 OpLine %5 6 12
824 %58 = OpFOrdGreaterThan %bool %57 %float_1
825 OpLine %5 6 17
826 %73 = OpExtInst %void %1 DebugNoScope
827 OpSelectionMerge %if_merge None
828 OpBranchConditional %58 %if_true %if_merge
829 %if_true = OpLabel
830 %74 = OpExtInst %void %1 DebugScope %50
831 OpLine %5 7 5
832 %75 = OpExtInst %void %1 DebugNoScope
833 OpReturnValue %32
834 %if_merge = OpLabel
835 %76 = OpExtInst %void %1 DebugScope %51
836 OpLine %5 9 16
837 %63 = OpLoad %float %arg1
838 OpStore %param_var_arg2 %63
839 OpLine %5 9 10
840 %64 = OpFunctionCall %v4float %func2 %param_var_arg2
841 OpLine %5 9 3
842 %77 = OpExtInst %void %1 DebugNoScope
843 OpReturnValue %64
844 OpFunctionEnd
845 OpLine %5 1 1
846 %func2 = OpFunction %v4float None %38
847 OpLine %5 1 20
848 %arg2 = OpFunctionParameter %_ptr_Function_float
849 %bb_entry_1 = OpLabel
850 %78 = OpExtInst %void %1 DebugScope %49
851 OpLine %5 2 17
852 %67 = OpLoad %float %arg2
853 %68 = OpCompositeConstruct %v4float %67 %float_0 %float_0 %float_0
854 OpLine %5 2 3
855 %79 = OpExtInst %void %1 DebugNoScope
856 OpReturnValue %68
857 OpFunctionEnd
858 )";
859
860 SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
861 std::unique_ptr<IRContext> context =
862 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
863 ASSERT_NE(nullptr, context);
864
865 std::vector<uint32_t> binary;
866 context->module()->ToBinary(&binary, /* skip_nop = */ false);
867
868 std::string disassembled_text;
869 EXPECT_TRUE(t.Disassemble(binary, &disassembled_text));
870 EXPECT_EQ(text, disassembled_text);
871 }
872
873 TEST(IrBuilder, DebugInfoInstInFunctionOutOfBlock2) {
874 // /* HLSL */
875 //
876 // struct VS_OUTPUT {
877 // float4 pos : SV_POSITION;
878 // float4 color : COLOR;
879 // };
880 //
881 // VS_OUTPUT main(float4 pos : POSITION,
882 // float4 color : COLOR) {
883 // VS_OUTPUT vout;
884 // vout.pos = pos;
885 // vout.color = color;
886 // return vout;
887 // }
888 const std::string text = R"(OpCapability Shader
889 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
890 OpMemoryModel Logical GLSL450
891 OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR
892 %7 = OpString "vs.hlsl"
893 OpSource HLSL 600 %7 "#line 1 \"vs.hlsl\"
894 struct VS_OUTPUT {
895 float4 pos : SV_POSITION;
896 float4 color : COLOR;
897 };
898
899 VS_OUTPUT main(float4 pos : POSITION,
900 float4 color : COLOR) {
901 VS_OUTPUT vout;
902 vout.pos = pos;
903 vout.color = color;
904 return vout;
905 }
906 "
907 %8 = OpString "#line 1 \"vs.hlsl\"
908 struct VS_OUTPUT {
909 float4 pos : SV_POSITION;
910 float4 color : COLOR;
911 };
912
913 VS_OUTPUT main(float4 pos : POSITION,
914 float4 color : COLOR) {
915 VS_OUTPUT vout;
916 vout.pos = pos;
917 vout.color = color;
918 return vout;
919 }
920 "
921 %9 = OpString "VS_OUTPUT"
922 %10 = OpString "float"
923 %11 = OpString "src.main"
924 %12 = OpString "pos"
925 %13 = OpString "color"
926 %14 = OpString "vout"
927 OpName %in_var_POSITION "in.var.POSITION"
928 OpName %in_var_COLOR "in.var.COLOR"
929 OpName %out_var_COLOR "out.var.COLOR"
930 OpName %main "main"
931 OpName %param_var_pos "param.var.pos"
932 OpName %param_var_color "param.var.color"
933 OpName %VS_OUTPUT "VS_OUTPUT"
934 OpMemberName %VS_OUTPUT 0 "pos"
935 OpMemberName %VS_OUTPUT 1 "color"
936 OpName %src_main "src.main"
937 OpName %pos "pos"
938 OpName %color "color"
939 OpName %bb_entry "bb.entry"
940 OpName %vout "vout"
941 OpDecorate %gl_Position BuiltIn Position
942 OpDecorate %in_var_POSITION Location 0
943 OpDecorate %in_var_COLOR Location 1
944 OpDecorate %out_var_COLOR Location 0
945 %int = OpTypeInt 32 1
946 %int_0 = OpConstant %int 0
947 %int_1 = OpConstant %int 1
948 %uint = OpTypeInt 32 0
949 %uint_32 = OpConstant %uint 32
950 %float = OpTypeFloat 32
951 %v4float = OpTypeVector %float 4
952 %_ptr_Input_v4float = OpTypePointer Input %v4float
953 %_ptr_Output_v4float = OpTypePointer Output %v4float
954 %void = OpTypeVoid
955 %uint_256 = OpConstant %uint 256
956 %uint_0 = OpConstant %uint 0
957 %uint_128 = OpConstant %uint 128
958 %36 = OpTypeFunction %void
959 %_ptr_Function_v4float = OpTypePointer Function %v4float
960 %VS_OUTPUT = OpTypeStruct %v4float %v4float
961 %38 = OpTypeFunction %VS_OUTPUT %_ptr_Function_v4float %_ptr_Function_v4float
962 %_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT
963 OpLine %7 6 29
964 %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
965 OpLine %7 7 31
966 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
967 OpLine %7 2 16
968 %gl_Position = OpVariable %_ptr_Output_v4float Output
969 OpLine %7 3 18
970 %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
971 %40 = OpExtInst %void %1 DebugExpression
972 %41 = OpExtInst %void %1 DebugSource %7 %8
973 %42 = OpExtInst %void %1 DebugCompilationUnit 1 4 %41 HLSL
974 %43 = OpExtInst %void %1 DebugTypeComposite %9 Structure %41 1 1 %42 %9 %uint_256 FlagIsProtected|FlagIsPrivate %44 %45
975 %46 = OpExtInst %void %1 DebugTypeBasic %10 %uint_32 Float
976 %47 = OpExtInst %void %1 DebugTypeVector %46 4
977 %48 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %43 %47 %47
978 %49 = OpExtInst %void %1 DebugFunction %11 %48 %41 6 1 %42 %11 FlagIsProtected|FlagIsPrivate 7 %src_main
979 %50 = OpExtInst %void %1 DebugLocalVariable %12 %47 %41 6 23 %49 FlagIsLocal 0
980 %51 = OpExtInst %void %1 DebugLocalVariable %13 %47 %41 7 23 %49 FlagIsLocal 1
981 %52 = OpExtInst %void %1 DebugLexicalBlock %41 7 38 %49
982 %53 = OpExtInst %void %1 DebugLocalVariable %14 %43 %41 8 13 %52 FlagIsLocal
983 %44 = OpExtInst %void %1 DebugTypeMember %12 %47 %41 2 3 %43 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
984 %45 = OpExtInst %void %1 DebugTypeMember %13 %47 %41 3 3 %43 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate
985 OpLine %7 6 1
986 %main = OpFunction %void None %36
987 %54 = OpLabel
988 %74 = OpExtInst %void %1 DebugScope %42
989 OpLine %7 6 23
990 %param_var_pos = OpVariable %_ptr_Function_v4float Function
991 OpLine %7 7 23
992 %param_var_color = OpVariable %_ptr_Function_v4float Function
993 OpLine %7 6 23
994 %56 = OpLoad %v4float %in_var_POSITION
995 OpStore %param_var_pos %56
996 OpLine %7 7 23
997 %57 = OpLoad %v4float %in_var_COLOR
998 OpStore %param_var_color %57
999 OpLine %7 6 1
1000 %58 = OpFunctionCall %VS_OUTPUT %src_main %param_var_pos %param_var_color
1001 OpLine %7 6 11
1002 %59 = OpCompositeExtract %v4float %58 0
1003 OpLine %7 2 16
1004 OpStore %gl_Position %59
1005 OpLine %7 6 11
1006 %60 = OpCompositeExtract %v4float %58 1
1007 OpLine %7 3 18
1008 OpStore %out_var_COLOR %60
1009 %75 = OpExtInst %void %1 DebugNoScope
1010 OpReturn
1011 OpFunctionEnd
1012 OpLine %7 6 1
1013 %src_main = OpFunction %VS_OUTPUT None %38
1014 %76 = OpExtInst %void %1 DebugScope %49
1015 OpLine %7 6 23
1016 %pos = OpFunctionParameter %_ptr_Function_v4float
1017 OpLine %7 7 23
1018 %color = OpFunctionParameter %_ptr_Function_v4float
1019 %63 = OpExtInst %void %1 DebugDeclare %50 %pos %40
1020 %64 = OpExtInst %void %1 DebugDeclare %51 %color %40
1021 %77 = OpExtInst %void %1 DebugNoScope
1022 %bb_entry = OpLabel
1023 %78 = OpExtInst %void %1 DebugScope %52
1024 OpLine %7 8 13
1025 %vout = OpVariable %_ptr_Function_VS_OUTPUT Function
1026 %67 = OpExtInst %void %1 DebugDeclare %53 %vout %40
1027 OpLine %7 9 14
1028 %68 = OpLoad %v4float %pos
1029 OpLine %7 9 3
1030 %69 = OpAccessChain %_ptr_Function_v4float %vout %int_0
1031 OpStore %69 %68
1032 OpLine %7 10 16
1033 %70 = OpLoad %v4float %color
1034 OpLine %7 10 3
1035 %71 = OpAccessChain %_ptr_Function_v4float %vout %int_1
1036 OpStore %71 %70
1037 OpLine %7 11 10
1038 %72 = OpLoad %VS_OUTPUT %vout
1039 OpLine %7 11 3
1040 %79 = OpExtInst %void %1 DebugNoScope
1041 OpReturnValue %72
1042 OpFunctionEnd
1043 )";
1044
1045 SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
1046 std::unique_ptr<IRContext> context =
1047 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
1048 ASSERT_NE(nullptr, context);
1049
1050 std::vector<uint32_t> binary;
1051 context->module()->ToBinary(&binary, /* skip_nop = */ false);
1052
1053 std::string disassembled_text;
1054 EXPECT_TRUE(t.Disassemble(binary, &disassembled_text));
1055 EXPECT_EQ(text, disassembled_text);
1056 }
1057
1058 TEST(IrBuilder, DebugInfoForTerminationInsts) {
1059 // Check that DebugScope instructions for termination instructions are
1060 // preserved.
1061 DoRoundTripCheck(R"(OpCapability Shader
1062 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
1063 OpMemoryModel Logical GLSL450
1064 OpEntryPoint Fragment %main "main"
1065 OpExecutionMode %main OriginUpperLeft
1066 %3 = OpString "simple_vs.hlsl"
1067 OpSource HLSL 600 %3
1068 OpName %main "main"
1069 %void = OpTypeVoid
1070 %5 = OpTypeFunction %void
1071 %6 = OpExtInst %void %1 DebugSource %3
1072 %7 = OpExtInst %void %1 DebugCompilationUnit 2 4 %6 HLSL
1073 %main = OpFunction %void None %5
1074 %8 = OpLabel
1075 %20 = OpExtInst %void %1 DebugScope %7
1076 OpBranch %10
1077 %21 = OpExtInst %void %1 DebugNoScope
1078 %10 = OpLabel
1079 %22 = OpExtInst %void %1 DebugScope %7
1080 OpKill
1081 %23 = OpExtInst %void %1 DebugNoScope
1082 %14 = OpLabel
1083 %24 = OpExtInst %void %1 DebugScope %7
1084 OpUnreachable
1085 %25 = OpExtInst %void %1 DebugNoScope
1086 %17 = OpLabel
1087 %26 = OpExtInst %void %1 DebugScope %7
1088 OpReturn
1089 %27 = OpExtInst %void %1 DebugNoScope
1090 OpFunctionEnd
1091 )");
1092 }
1093
1094 TEST(IrBuilder, LocalGlobalVariables) {
1095 // #version 310 es
1096 //
1097 // float gv1 = 10.;
1098 // float gv2 = 100.;
1099 //
1100 // float f() {
1101 // float lv1 = gv1 + gv2;
1102 // float lv2 = gv1 * gv2;
1103 // return lv1 / lv2;
1104 // }
1105 //
1106 // void main() {
1107 // float lv1 = gv1 - gv2;
1108 // }
1109 DoRoundTripCheck(
1110 // clang-format off
1111 "OpCapability Shader\n"
1112 "%1 = OpExtInstImport \"GLSL.std.450\"\n"
1113 "OpMemoryModel Logical GLSL450\n"
1114 "OpEntryPoint Vertex %main \"main\"\n"
1115 "OpSource ESSL 310\n"
1116 "OpName %main \"main\"\n"
1117 "OpName %f_ \"f(\"\n"
1118 "OpName %gv1 \"gv1\"\n"
1119 "OpName %gv2 \"gv2\"\n"
1120 "OpName %lv1 \"lv1\"\n"
1121 "OpName %lv2 \"lv2\"\n"
1122 "OpName %lv1_0 \"lv1\"\n"
1123 "%void = OpTypeVoid\n"
1124 "%10 = OpTypeFunction %void\n"
1125 "%float = OpTypeFloat 32\n"
1126 "%12 = OpTypeFunction %float\n"
1127 "%_ptr_Private_float = OpTypePointer Private %float\n"
1128 "%gv1 = OpVariable %_ptr_Private_float Private\n"
1129 "%float_10 = OpConstant %float 10\n"
1130 "%gv2 = OpVariable %_ptr_Private_float Private\n"
1131 "%float_100 = OpConstant %float 100\n"
1132 "%_ptr_Function_float = OpTypePointer Function %float\n"
1133 "%main = OpFunction %void None %10\n"
1134 "%17 = OpLabel\n"
1135 "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
1136 "OpStore %gv1 %float_10\n"
1137 "OpStore %gv2 %float_100\n"
1138 "%18 = OpLoad %float %gv1\n"
1139 "%19 = OpLoad %float %gv2\n"
1140 "%20 = OpFSub %float %18 %19\n"
1141 "OpStore %lv1_0 %20\n"
1142 "OpReturn\n"
1143 "OpFunctionEnd\n"
1144 "%f_ = OpFunction %float None %12\n"
1145 "%21 = OpLabel\n"
1146 "%lv1 = OpVariable %_ptr_Function_float Function\n"
1147 "%lv2 = OpVariable %_ptr_Function_float Function\n"
1148 "%22 = OpLoad %float %gv1\n"
1149 "%23 = OpLoad %float %gv2\n"
1150 "%24 = OpFAdd %float %22 %23\n"
1151 "OpStore %lv1 %24\n"
1152 "%25 = OpLoad %float %gv1\n"
1153 "%26 = OpLoad %float %gv2\n"
1154 "%27 = OpFMul %float %25 %26\n"
1155 "OpStore %lv2 %27\n"
1156 "%28 = OpLoad %float %lv1\n"
1157 "%29 = OpLoad %float %lv2\n"
1158 "%30 = OpFDiv %float %28 %29\n"
1159 "OpReturnValue %30\n"
1160 "OpFunctionEnd\n");
1161 // clang-format on
1162 }
1163
1164 TEST(IrBuilder, OpUndefOutsideFunction) {
1165 // #version 310 es
1166 // void main() {}
1167 const std::string text =
1168 // clang-format off
1169 "OpMemoryModel Logical GLSL450\n"
1170 "%int = OpTypeInt 32 1\n"
1171 "%uint = OpTypeInt 32 0\n"
1172 "%float = OpTypeFloat 32\n"
1173 "%4 = OpUndef %int\n"
1174 "%int_10 = OpConstant %int 10\n"
1175 "%6 = OpUndef %uint\n"
1176 "%bool = OpTypeBool\n"
1177 "%8 = OpUndef %float\n"
1178 "%double = OpTypeFloat 64\n";
1179 // clang-format on
1180
1181 SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
1182 std::unique_ptr<IRContext> context =
1183 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
1184 ASSERT_NE(nullptr, context);
1185
1186 const auto opundef_count = std::count_if(
1187 context->module()->types_values_begin(),
1188 context->module()->types_values_end(),
1189 [](const Instruction& inst) { return inst.opcode() == SpvOpUndef; });
1190 EXPECT_EQ(3, opundef_count);
1191
1192 std::vector<uint32_t> binary;
1193 context->module()->ToBinary(&binary, /* skip_nop = */ false);
1194
1195 std::string disassembled_text;
1196 EXPECT_TRUE(t.Disassemble(binary, &disassembled_text));
1197 EXPECT_EQ(text, disassembled_text);
1198 }
1199
1200 TEST(IrBuilder, OpUndefInBasicBlock) {
1201 DoRoundTripCheck(
1202 // clang-format off
1203 "OpMemoryModel Logical GLSL450\n"
1204 "OpName %main \"main\"\n"
1205 "%void = OpTypeVoid\n"
1206 "%uint = OpTypeInt 32 0\n"
1207 "%double = OpTypeFloat 64\n"
1208 "%5 = OpTypeFunction %void\n"
1209 "%main = OpFunction %void None %5\n"
1210 "%6 = OpLabel\n"
1211 "%7 = OpUndef %uint\n"
1212 "%8 = OpUndef %double\n"
1213 "OpReturn\n"
1214 "OpFunctionEnd\n");
1215 // clang-format on
1216 }
1217
1218 TEST(IrBuilder, KeepLineDebugInfoBeforeType) {
1219 DoRoundTripCheck(
1220 // clang-format off
1221 "OpCapability Shader\n"
1222 "OpMemoryModel Logical GLSL450\n"
1223 "%1 = OpString \"minimal.vert\"\n"
1224 "OpLine %1 1 1\n"
1225 "OpNoLine\n"
1226 "%void = OpTypeVoid\n"
1227 "OpLine %1 2 2\n"
1228 "%3 = OpTypeFunction %void\n");
1229 // clang-format on
1230 }
1231
1232 TEST(IrBuilder, KeepLineDebugInfoBeforeLabel) {
1233 DoRoundTripCheck(
1234 // clang-format off
1235 "OpCapability Shader\n"
1236 "OpMemoryModel Logical GLSL450\n"
1237 "%1 = OpString \"minimal.vert\"\n"
1238 "%void = OpTypeVoid\n"
1239 "%3 = OpTypeFunction %void\n"
1240 "%4 = OpFunction %void None %3\n"
1241 "%5 = OpLabel\n"
1242 "OpBranch %6\n"
1243 "OpLine %1 1 1\n"
1244 "OpLine %1 2 2\n"
1245 "%6 = OpLabel\n"
1246 "OpBranch %7\n"
1247 "OpLine %1 100 100\n"
1248 "%7 = OpLabel\n"
1249 "OpReturn\n"
1250 "OpFunctionEnd\n");
1251 // clang-format on
1252 }
1253
1254 TEST(IrBuilder, KeepLineDebugInfoBeforeFunctionEnd) {
1255 DoRoundTripCheck(
1256 // clang-format off
1257 "OpCapability Shader\n"
1258 "OpMemoryModel Logical GLSL450\n"
1259 "%1 = OpString \"minimal.vert\"\n"
1260 "%void = OpTypeVoid\n"
1261 "%3 = OpTypeFunction %void\n"
1262 "%4 = OpFunction %void None %3\n"
1263 "OpLine %1 1 1\n"
1264 "OpLine %1 2 2\n"
1265 "OpFunctionEnd\n");
1266 // clang-format on
1267 }
1268
1269 TEST(IrBuilder, KeepModuleProcessedInRightPlace) {
1270 DoRoundTripCheck(
1271 // clang-format off
1272 "OpCapability Shader\n"
1273 "OpMemoryModel Logical GLSL450\n"
1274 "%1 = OpString \"minimal.vert\"\n"
1275 "OpName %void \"void\"\n"
1276 "OpModuleProcessed \"Made it faster\"\n"
1277 "OpModuleProcessed \".. and smaller\"\n"
1278 "%void = OpTypeVoid\n");
1279 // clang-format on
1280 }
1281
1282 // Checks the given |error_message| is reported when trying to build a module
1283 // from the given |assembly|.
1284 void DoErrorMessageCheck(const std::string& assembly,
1285 const std::string& error_message, uint32_t line_num) {
1286 auto consumer = [error_message, line_num](spv_message_level_t, const char*,
1287 const spv_position_t& position,
1288 const char* m) {
1289 EXPECT_EQ(error_message, m);
1290 EXPECT_EQ(line_num, position.line);
1291 };
1292
1293 SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
1294 std::unique_ptr<IRContext> context =
1295 BuildModule(SPV_ENV_UNIVERSAL_1_1, std::move(consumer), assembly);
1296 EXPECT_EQ(nullptr, context);
1297 }
1298
1299 TEST(IrBuilder, FunctionInsideFunction) {
1300 DoErrorMessageCheck("%2 = OpFunction %1 None %3\n%5 = OpFunction %4 None %6",
1301 "function inside function", 2);
1302 }
1303
1304 TEST(IrBuilder, MismatchOpFunctionEnd) {
1305 DoErrorMessageCheck("OpFunctionEnd",
1306 "OpFunctionEnd without corresponding OpFunction", 1);
1307 }
1308
1309 TEST(IrBuilder, OpFunctionEndInsideBasicBlock) {
1310 DoErrorMessageCheck(
1311 "%2 = OpFunction %1 None %3\n"
1312 "%4 = OpLabel\n"
1313 "OpFunctionEnd",
1314 "OpFunctionEnd inside basic block", 3);
1315 }
1316
1317 TEST(IrBuilder, BasicBlockOutsideFunction) {
1318 DoErrorMessageCheck("OpCapability Shader\n%1 = OpLabel",
1319 "OpLabel outside function", 2);
1320 }
1321
1322 TEST(IrBuilder, OpLabelInsideBasicBlock) {
1323 DoErrorMessageCheck(
1324 "%2 = OpFunction %1 None %3\n"
1325 "%4 = OpLabel\n"
1326 "%5 = OpLabel",
1327 "OpLabel inside basic block", 3);
1328 }
1329
1330 TEST(IrBuilder, TerminatorOutsideFunction) {
1331 DoErrorMessageCheck("OpReturn", "terminator instruction outside function", 1);
1332 }
1333
1334 TEST(IrBuilder, TerminatorOutsideBasicBlock) {
1335 DoErrorMessageCheck("%2 = OpFunction %1 None %3\nOpReturn",
1336 "terminator instruction outside basic block", 2);
1337 }
1338
1339 TEST(IrBuilder, NotAllowedInstAppearingInFunction) {
1340 DoErrorMessageCheck("%2 = OpFunction %1 None %3\n%5 = OpVariable %4 Function",
1341 "Non-OpFunctionParameter (opcode: 59) found inside "
1342 "function but outside basic block",
1343 2);
1344 }
1345
1346 TEST(IrBuilder, UniqueIds) {
1347 const std::string text =
1348 // clang-format off
1349 "OpCapability Shader\n"
1350 "%1 = OpExtInstImport \"GLSL.std.450\"\n"
1351 "OpMemoryModel Logical GLSL450\n"
1352 "OpEntryPoint Vertex %main \"main\"\n"
1353 "OpSource ESSL 310\n"
1354 "OpName %main \"main\"\n"
1355 "OpName %f_ \"f(\"\n"
1356 "OpName %gv1 \"gv1\"\n"
1357 "OpName %gv2 \"gv2\"\n"
1358 "OpName %lv1 \"lv1\"\n"
1359 "OpName %lv2 \"lv2\"\n"
1360 "OpName %lv1_0 \"lv1\"\n"
1361 "%void = OpTypeVoid\n"
1362 "%10 = OpTypeFunction %void\n"
1363 "%float = OpTypeFloat 32\n"
1364 "%12 = OpTypeFunction %float\n"
1365 "%_ptr_Private_float = OpTypePointer Private %float\n"
1366 "%gv1 = OpVariable %_ptr_Private_float Private\n"
1367 "%float_10 = OpConstant %float 10\n"
1368 "%gv2 = OpVariable %_ptr_Private_float Private\n"
1369 "%float_100 = OpConstant %float 100\n"
1370 "%_ptr_Function_float = OpTypePointer Function %float\n"
1371 "%main = OpFunction %void None %10\n"
1372 "%17 = OpLabel\n"
1373 "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
1374 "OpStore %gv1 %float_10\n"
1375 "OpStore %gv2 %float_100\n"
1376 "%18 = OpLoad %float %gv1\n"
1377 "%19 = OpLoad %float %gv2\n"
1378 "%20 = OpFSub %float %18 %19\n"
1379 "OpStore %lv1_0 %20\n"
1380 "OpReturn\n"
1381 "OpFunctionEnd\n"
1382 "%f_ = OpFunction %float None %12\n"
1383 "%21 = OpLabel\n"
1384 "%lv1 = OpVariable %_ptr_Function_float Function\n"
1385 "%lv2 = OpVariable %_ptr_Function_float Function\n"
1386 "%22 = OpLoad %float %gv1\n"
1387 "%23 = OpLoad %float %gv2\n"
1388 "%24 = OpFAdd %float %22 %23\n"
1389 "OpStore %lv1 %24\n"
1390 "%25 = OpLoad %float %gv1\n"
1391 "%26 = OpLoad %float %gv2\n"
1392 "%27 = OpFMul %float %25 %26\n"
1393 "OpStore %lv2 %27\n"
1394 "%28 = OpLoad %float %lv1\n"
1395 "%29 = OpLoad %float %lv2\n"
1396 "%30 = OpFDiv %float %28 %29\n"
1397 "OpReturnValue %30\n"
1398 "OpFunctionEnd\n";
1399 // clang-format on
1400
1401 std::unique_ptr<IRContext> context =
1402 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
1403 ASSERT_NE(nullptr, context);
1404
1405 std::unordered_set<uint32_t> ids;
1406 context->module()->ForEachInst([&ids](const Instruction* inst) {
1407 EXPECT_TRUE(ids.insert(inst->unique_id()).second);
1408 });
1409 }
1410
1411 } // namespace
1412 } // namespace opt
1413 } // namespace spvtools