Debian Codegraph @

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

raw
1 // Copyright (c) 2017 Valve Corporation
2 // Copyright (c) 2017 LunarG Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 #include "test/opt/pass_fixture.h"
21 #include "test/opt/pass_utils.h"
22
23 namespace spvtools {
24 namespace opt {
25 namespace {
26
27 using InlineTest = PassTest<::testing::Test>;
28
29 TEST_F(InlineTest, Simple) {
30 // #version 140
31 //
32 // in vec4 BaseColor;
33 //
34 // float foo(vec4 bar)
35 // {
36 // return bar.x + bar.y;
37 // }
38 //
39 // void main()
40 // {
41 // vec4 color = vec4(foo(BaseColor));
42 // gl_FragColor = color;
43 // }
44 const std::vector<const char*> predefs = {
45 // clang-format off
46 "OpCapability Shader",
47 "%1 = OpExtInstImport \"GLSL.std.450\"",
48 "OpMemoryModel Logical GLSL450",
49 "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
50 "OpExecutionMode %main OriginUpperLeft",
51 "OpSource GLSL 140",
52 "OpName %main \"main\"",
53 "OpName %foo_vf4_ \"foo(vf4;\"",
54 "OpName %bar \"bar\"",
55 "OpName %color \"color\"",
56 "OpName %BaseColor \"BaseColor\"",
57 "OpName %param \"param\"",
58 "OpName %gl_FragColor \"gl_FragColor\"",
59 "%void = OpTypeVoid",
60 "%10 = OpTypeFunction %void",
61 "%float = OpTypeFloat 32",
62 "%v4float = OpTypeVector %float 4",
63 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
64 "%14 = OpTypeFunction %float %_ptr_Function_v4float",
65 "%uint = OpTypeInt 32 0",
66 "%uint_0 = OpConstant %uint 0",
67 "%_ptr_Function_float = OpTypePointer Function %float",
68 "%uint_1 = OpConstant %uint 1",
69 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
70 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
71 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
72 "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
73 // clang-format on
74 };
75
76 const std::vector<const char*> nonEntryFuncs = {
77 // clang-format off
78 "%foo_vf4_ = OpFunction %float None %14",
79 "%bar = OpFunctionParameter %_ptr_Function_v4float",
80 "%26 = OpLabel",
81 "%27 = OpAccessChain %_ptr_Function_float %bar %uint_0",
82 "%28 = OpLoad %float %27",
83 "%29 = OpAccessChain %_ptr_Function_float %bar %uint_1",
84 "%30 = OpLoad %float %29",
85 "%31 = OpFAdd %float %28 %30",
86 "OpReturnValue %31",
87 "OpFunctionEnd",
88 // clang-format on
89 };
90
91 const std::vector<const char*> before = {
92 // clang-format off
93 "%main = OpFunction %void None %10",
94 "%21 = OpLabel",
95 "%color = OpVariable %_ptr_Function_v4float Function",
96 "%param = OpVariable %_ptr_Function_v4float Function",
97 "%22 = OpLoad %v4float %BaseColor",
98 "OpStore %param %22",
99 "%23 = OpFunctionCall %float %foo_vf4_ %param",
100 "%24 = OpCompositeConstruct %v4float %23 %23 %23 %23",
101 "OpStore %color %24",
102 "%25 = OpLoad %v4float %color",
103 "OpStore %gl_FragColor %25",
104 "OpReturn",
105 "OpFunctionEnd",
106 // clang-format on
107 };
108
109 const std::vector<const char*> after = {
110 // clang-format off
111 "%main = OpFunction %void None %10",
112 "%21 = OpLabel",
113 "%32 = OpVariable %_ptr_Function_float Function",
114 "%color = OpVariable %_ptr_Function_v4float Function",
115 "%param = OpVariable %_ptr_Function_v4float Function",
116 "%22 = OpLoad %v4float %BaseColor",
117 "OpStore %param %22",
118 "%34 = OpAccessChain %_ptr_Function_float %param %uint_0",
119 "%35 = OpLoad %float %34",
120 "%36 = OpAccessChain %_ptr_Function_float %param %uint_1",
121 "%37 = OpLoad %float %36",
122 "%38 = OpFAdd %float %35 %37",
123 "OpStore %32 %38",
124 "%23 = OpLoad %float %32",
125 "%24 = OpCompositeConstruct %v4float %23 %23 %23 %23",
126 "OpStore %color %24",
127 "%25 = OpLoad %v4float %color",
128 "OpStore %gl_FragColor %25",
129 "OpReturn",
130 "OpFunctionEnd",
131 // clang-format on
132 };
133 SinglePassRunAndCheck<InlineExhaustivePass>(
134 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
135 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
136 /* skip_nop = */ false, /* do_validate = */ true);
137 }
138
139 TEST_F(InlineTest, Nested) {
140 // #version 140
141 //
142 // in vec4 BaseColor;
143 //
144 // float foo2(float f, float f2)
145 // {
146 // return f * f2;
147 // }
148 //
149 // float foo(vec4 bar)
150 // {
151 // return foo2(bar.x + bar.y, bar.z);
152 // }
153 //
154 // void main()
155 // {
156 // vec4 color = vec4(foo(BaseColor));
157 // gl_FragColor = color;
158 // }
159 const std::vector<const char*> predefs = {
160 // clang-format off
161 "OpCapability Shader",
162 "%1 = OpExtInstImport \"GLSL.std.450\"",
163 "OpMemoryModel Logical GLSL450",
164 "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
165 "OpExecutionMode %main OriginUpperLeft",
166 "OpSource GLSL 140",
167 "OpName %main \"main\"",
168 "OpName %foo2_f1_f1_ \"foo2(f1;f1;\"",
169 "OpName %f \"f\"",
170 "OpName %f2 \"f2\"",
171 "OpName %foo_vf4_ \"foo(vf4;\"",
172 "OpName %bar \"bar\"",
173 "OpName %param \"param\"",
174 "OpName %param_0 \"param\"",
175 "OpName %color \"color\"",
176 "OpName %BaseColor \"BaseColor\"",
177 "OpName %param_1 \"param\"",
178 "OpName %gl_FragColor \"gl_FragColor\"",
179 "%void = OpTypeVoid",
180 "%15 = OpTypeFunction %void",
181 "%float = OpTypeFloat 32",
182 "%_ptr_Function_float = OpTypePointer Function %float",
183 "%18 = OpTypeFunction %float %_ptr_Function_float %_ptr_Function_float",
184 "%v4float = OpTypeVector %float 4",
185 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
186 "%21 = OpTypeFunction %float %_ptr_Function_v4float",
187 "%uint = OpTypeInt 32 0",
188 "%uint_0 = OpConstant %uint 0",
189 "%uint_1 = OpConstant %uint 1",
190 "%uint_2 = OpConstant %uint 2",
191 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
192 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
193 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
194 "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
195 // clang-format on
196 };
197
198 const std::vector<const char*> nonEntryFuncs = {
199 // clang-format off
200 "%foo2_f1_f1_ = OpFunction %float None %18",
201 "%f = OpFunctionParameter %_ptr_Function_float",
202 "%f2 = OpFunctionParameter %_ptr_Function_float",
203 "%33 = OpLabel",
204 "%34 = OpLoad %float %f",
205 "%35 = OpLoad %float %f2",
206 "%36 = OpFMul %float %34 %35",
207 "OpReturnValue %36",
208 "OpFunctionEnd",
209 "%foo_vf4_ = OpFunction %float None %21",
210 "%bar = OpFunctionParameter %_ptr_Function_v4float",
211 "%37 = OpLabel",
212 "%param = OpVariable %_ptr_Function_float Function",
213 "%param_0 = OpVariable %_ptr_Function_float Function",
214 "%38 = OpAccessChain %_ptr_Function_float %bar %uint_0",
215 "%39 = OpLoad %float %38",
216 "%40 = OpAccessChain %_ptr_Function_float %bar %uint_1",
217 "%41 = OpLoad %float %40",
218 "%42 = OpFAdd %float %39 %41",
219 "OpStore %param %42",
220 "%43 = OpAccessChain %_ptr_Function_float %bar %uint_2",
221 "%44 = OpLoad %float %43",
222 "OpStore %param_0 %44",
223 "%45 = OpFunctionCall %float %foo2_f1_f1_ %param %param_0",
224 "OpReturnValue %45",
225 "OpFunctionEnd",
226 // clang-format on
227 };
228
229 const std::vector<const char*> before = {
230 // clang-format off
231 "%main = OpFunction %void None %15",
232 "%28 = OpLabel",
233 "%color = OpVariable %_ptr_Function_v4float Function",
234 "%param_1 = OpVariable %_ptr_Function_v4float Function",
235 "%29 = OpLoad %v4float %BaseColor",
236 "OpStore %param_1 %29",
237 "%30 = OpFunctionCall %float %foo_vf4_ %param_1",
238 "%31 = OpCompositeConstruct %v4float %30 %30 %30 %30",
239 "OpStore %color %31",
240 "%32 = OpLoad %v4float %color",
241 "OpStore %gl_FragColor %32",
242 "OpReturn",
243 "OpFunctionEnd",
244 // clang-format on
245 };
246
247 const std::vector<const char*> after = {
248 // clang-format off
249 "%main = OpFunction %void None %15",
250 "%28 = OpLabel",
251 "%58 = OpVariable %_ptr_Function_float Function",
252 "%46 = OpVariable %_ptr_Function_float Function",
253 "%47 = OpVariable %_ptr_Function_float Function",
254 "%48 = OpVariable %_ptr_Function_float Function",
255 "%color = OpVariable %_ptr_Function_v4float Function",
256 "%param_1 = OpVariable %_ptr_Function_v4float Function",
257 "%29 = OpLoad %v4float %BaseColor",
258 "OpStore %param_1 %29",
259 "%50 = OpAccessChain %_ptr_Function_float %param_1 %uint_0",
260 "%51 = OpLoad %float %50",
261 "%52 = OpAccessChain %_ptr_Function_float %param_1 %uint_1",
262 "%53 = OpLoad %float %52",
263 "%54 = OpFAdd %float %51 %53",
264 "OpStore %46 %54",
265 "%55 = OpAccessChain %_ptr_Function_float %param_1 %uint_2",
266 "%56 = OpLoad %float %55",
267 "OpStore %47 %56",
268 "%60 = OpLoad %float %46",
269 "%61 = OpLoad %float %47",
270 "%62 = OpFMul %float %60 %61",
271 "OpStore %58 %62",
272 "%57 = OpLoad %float %58",
273 "OpStore %48 %57",
274 "%30 = OpLoad %float %48",
275 "%31 = OpCompositeConstruct %v4float %30 %30 %30 %30",
276 "OpStore %color %31",
277 "%32 = OpLoad %v4float %color",
278 "OpStore %gl_FragColor %32",
279 "OpReturn",
280 "OpFunctionEnd",
281 // clang-format on
282 };
283 SinglePassRunAndCheck<InlineExhaustivePass>(
284 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
285 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
286 /* skip_nop = */ false, /* do_validate = */ true);
287 }
288
289 TEST_F(InlineTest, InOutParameter) {
290 // #version 400
291 //
292 // in vec4 Basecolor;
293 //
294 // void foo(inout vec4 bar)
295 // {
296 // bar.z = bar.x + bar.y;
297 // }
298 //
299 // void main()
300 // {
301 // vec4 b = Basecolor;
302 // foo(b);
303 // vec4 color = vec4(b.z);
304 // gl_FragColor = color;
305 // }
306 const std::vector<const char*> predefs = {
307 // clang-format off
308 "OpCapability Shader",
309 "%1 = OpExtInstImport \"GLSL.std.450\"",
310 "OpMemoryModel Logical GLSL450",
311 "OpEntryPoint Fragment %main \"main\" %Basecolor %gl_FragColor",
312 "OpExecutionMode %main OriginUpperLeft",
313 "OpSource GLSL 400",
314 "OpName %main \"main\"",
315 "OpName %foo_vf4_ \"foo(vf4;\"",
316 "OpName %bar \"bar\"",
317 "OpName %b \"b\"",
318 "OpName %Basecolor \"Basecolor\"",
319 "OpName %param \"param\"",
320 "OpName %color \"color\"",
321 "OpName %gl_FragColor \"gl_FragColor\"",
322 "%void = OpTypeVoid",
323 "%11 = OpTypeFunction %void",
324 "%float = OpTypeFloat 32",
325 "%v4float = OpTypeVector %float 4",
326 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
327 "%15 = OpTypeFunction %void %_ptr_Function_v4float",
328 "%uint = OpTypeInt 32 0",
329 "%uint_0 = OpConstant %uint 0",
330 "%_ptr_Function_float = OpTypePointer Function %float",
331 "%uint_1 = OpConstant %uint 1",
332 "%uint_2 = OpConstant %uint 2",
333 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
334 "%Basecolor = OpVariable %_ptr_Input_v4float Input",
335 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
336 "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
337 // clang-format on
338 };
339
340 const std::vector<const char*> nonEntryFuncs = {
341 // clang-format off
342 "%foo_vf4_ = OpFunction %void None %15",
343 "%bar = OpFunctionParameter %_ptr_Function_v4float",
344 "%32 = OpLabel",
345 "%33 = OpAccessChain %_ptr_Function_float %bar %uint_0",
346 "%34 = OpLoad %float %33",
347 "%35 = OpAccessChain %_ptr_Function_float %bar %uint_1",
348 "%36 = OpLoad %float %35",
349 "%37 = OpFAdd %float %34 %36",
350 "%38 = OpAccessChain %_ptr_Function_float %bar %uint_2",
351 "OpStore %38 %37",
352 "OpReturn",
353 "OpFunctionEnd",
354 // clang-format on
355 };
356
357 const std::vector<const char*> before = {
358 // clang-format off
359 "%main = OpFunction %void None %11",
360 "%23 = OpLabel",
361 "%b = OpVariable %_ptr_Function_v4float Function",
362 "%param = OpVariable %_ptr_Function_v4float Function",
363 "%color = OpVariable %_ptr_Function_v4float Function",
364 "%24 = OpLoad %v4float %Basecolor",
365 "OpStore %b %24",
366 "%25 = OpLoad %v4float %b",
367 "OpStore %param %25",
368 "%26 = OpFunctionCall %void %foo_vf4_ %param",
369 "%27 = OpLoad %v4float %param",
370 "OpStore %b %27",
371 "%28 = OpAccessChain %_ptr_Function_float %b %uint_2",
372 "%29 = OpLoad %float %28",
373 "%30 = OpCompositeConstruct %v4float %29 %29 %29 %29",
374 "OpStore %color %30",
375 "%31 = OpLoad %v4float %color",
376 "OpStore %gl_FragColor %31",
377 "OpReturn",
378 "OpFunctionEnd",
379 // clang-format on
380 };
381
382 const std::vector<const char*> after = {
383 // clang-format off
384 "%26 = OpUndef %void",
385 "%main = OpFunction %void None %11",
386 "%23 = OpLabel",
387 "%b = OpVariable %_ptr_Function_v4float Function",
388 "%param = OpVariable %_ptr_Function_v4float Function",
389 "%color = OpVariable %_ptr_Function_v4float Function",
390 "%24 = OpLoad %v4float %Basecolor",
391 "OpStore %b %24",
392 "%25 = OpLoad %v4float %b",
393 "OpStore %param %25",
394 "%40 = OpAccessChain %_ptr_Function_float %param %uint_0",
395 "%41 = OpLoad %float %40",
396 "%42 = OpAccessChain %_ptr_Function_float %param %uint_1",
397 "%43 = OpLoad %float %42",
398 "%44 = OpFAdd %float %41 %43",
399 "%45 = OpAccessChain %_ptr_Function_float %param %uint_2",
400 "OpStore %45 %44",
401 "%27 = OpLoad %v4float %param",
402 "OpStore %b %27",
403 "%28 = OpAccessChain %_ptr_Function_float %b %uint_2",
404 "%29 = OpLoad %float %28",
405 "%30 = OpCompositeConstruct %v4float %29 %29 %29 %29",
406 "OpStore %color %30",
407 "%31 = OpLoad %v4float %color",
408 "OpStore %gl_FragColor %31",
409 "OpReturn",
410 "OpFunctionEnd",
411 // clang-format on
412 };
413 SinglePassRunAndCheck<InlineExhaustivePass>(
414 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
415 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
416 /* skip_nop = */ false, /* do_validate = */ true);
417 }
418
419 TEST_F(InlineTest, BranchInCallee) {
420 // #version 140
421 //
422 // in vec4 BaseColor;
423 //
424 // float foo(vec4 bar)
425 // {
426 // float r = bar.x;
427 // if (r < 0.0)
428 // r = -r;
429 // return r;
430 // }
431 //
432 // void main()
433 // {
434 // vec4 color = vec4(foo(BaseColor));
435 //
436 // gl_FragColor = color;
437 // }
438 const std::vector<const char*> predefs = {
439 // clang-format off
440 "OpCapability Shader",
441 "%1 = OpExtInstImport \"GLSL.std.450\"",
442 "OpMemoryModel Logical GLSL450",
443 "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
444 "OpExecutionMode %main OriginUpperLeft",
445 "OpSource GLSL 140",
446 "OpName %main \"main\"",
447 "OpName %foo_vf4_ \"foo(vf4;\"",
448 "OpName %bar \"bar\"",
449 "OpName %r \"r\"",
450 "OpName %color \"color\"",
451 "OpName %BaseColor \"BaseColor\"",
452 "OpName %param \"param\"",
453 "OpName %gl_FragColor \"gl_FragColor\"",
454 "%void = OpTypeVoid",
455 "%11 = OpTypeFunction %void",
456 "%float = OpTypeFloat 32",
457 "%v4float = OpTypeVector %float 4",
458 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
459 "%15 = OpTypeFunction %float %_ptr_Function_v4float",
460 "%_ptr_Function_float = OpTypePointer Function %float",
461 "%uint = OpTypeInt 32 0",
462 "%uint_0 = OpConstant %uint 0",
463 "%float_0 = OpConstant %float 0",
464 "%bool = OpTypeBool",
465 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
466 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
467 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
468 "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
469 // clang-format on
470 };
471
472 const std::vector<const char*> nonEntryFuncs = {
473 // clang-format off
474 "%foo_vf4_ = OpFunction %float None %15",
475 "%bar = OpFunctionParameter %_ptr_Function_v4float",
476 "%28 = OpLabel",
477 "%r = OpVariable %_ptr_Function_float Function",
478 "%29 = OpAccessChain %_ptr_Function_float %bar %uint_0",
479 "%30 = OpLoad %float %29",
480 "OpStore %r %30",
481 "%31 = OpLoad %float %r",
482 "%32 = OpFOrdLessThan %bool %31 %float_0",
483 "OpSelectionMerge %33 None",
484 "OpBranchConditional %32 %34 %33",
485 "%34 = OpLabel",
486 "%35 = OpLoad %float %r",
487 "%36 = OpFNegate %float %35",
488 "OpStore %r %36",
489 "OpBranch %33",
490 "%33 = OpLabel",
491 "%37 = OpLoad %float %r",
492 "OpReturnValue %37",
493 "OpFunctionEnd",
494 // clang-format on
495 };
496
497 const std::vector<const char*> before = {
498 // clang-format off
499 "%main = OpFunction %void None %11",
500 "%23 = OpLabel",
501 "%color = OpVariable %_ptr_Function_v4float Function",
502 "%param = OpVariable %_ptr_Function_v4float Function",
503 "%24 = OpLoad %v4float %BaseColor",
504 "OpStore %param %24",
505 "%25 = OpFunctionCall %float %foo_vf4_ %param",
506 "%26 = OpCompositeConstruct %v4float %25 %25 %25 %25",
507 "OpStore %color %26",
508 "%27 = OpLoad %v4float %color",
509 "OpStore %gl_FragColor %27",
510 "OpReturn",
511 "OpFunctionEnd",
512 // clang-format on
513 };
514
515 const std::vector<const char*> after = {
516 // clang-format off
517 "%main = OpFunction %void None %11",
518 "%23 = OpLabel",
519 "%38 = OpVariable %_ptr_Function_float Function",
520 "%39 = OpVariable %_ptr_Function_float Function",
521 "%color = OpVariable %_ptr_Function_v4float Function",
522 "%param = OpVariable %_ptr_Function_v4float Function",
523 "%24 = OpLoad %v4float %BaseColor",
524 "OpStore %param %24",
525 "%41 = OpAccessChain %_ptr_Function_float %param %uint_0",
526 "%42 = OpLoad %float %41",
527 "OpStore %38 %42",
528 "%43 = OpLoad %float %38",
529 "%44 = OpFOrdLessThan %bool %43 %float_0",
530 "OpSelectionMerge %48 None",
531 "OpBranchConditional %44 %45 %48",
532 "%45 = OpLabel",
533 "%46 = OpLoad %float %38",
534 "%47 = OpFNegate %float %46",
535 "OpStore %38 %47",
536 "OpBranch %48",
537 "%48 = OpLabel",
538 "%49 = OpLoad %float %38",
539 "OpStore %39 %49",
540 "%25 = OpLoad %float %39",
541 "%26 = OpCompositeConstruct %v4float %25 %25 %25 %25",
542 "OpStore %color %26",
543 "%27 = OpLoad %v4float %color",
544 "OpStore %gl_FragColor %27",
545 "OpReturn",
546 "OpFunctionEnd",
547 // clang-format on
548 };
549 SinglePassRunAndCheck<InlineExhaustivePass>(
550 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
551 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
552 /* skip_nop = */ false, /* do_validate = */ true);
553 }
554
555 TEST_F(InlineTest, PhiAfterCall) {
556 // #version 140
557 //
558 // in vec4 BaseColor;
559 //
560 // float foo(float bar)
561 // {
562 // float r = bar;
563 // if (r < 0.0)
564 // r = -r;
565 // return r;
566 // }
567 //
568 // void main()
569 // {
570 // vec4 color = BaseColor;
571 // if (foo(color.x) > 2.0 && foo(color.y) > 2.0)
572 // color = vec4(0.0);
573 // gl_FragColor = color;
574 // }
575 const std::vector<const char*> predefs = {
576 // clang-format off
577 "OpCapability Shader",
578 "%1 = OpExtInstImport \"GLSL.std.450\"",
579 "OpMemoryModel Logical GLSL450",
580 "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
581 "OpExecutionMode %main OriginUpperLeft",
582 "OpSource GLSL 140",
583 "OpName %main \"main\"",
584 "OpName %foo_f1_ \"foo(f1;\"",
585 "OpName %bar \"bar\"",
586 "OpName %r \"r\"",
587 "OpName %color \"color\"",
588 "OpName %BaseColor \"BaseColor\"",
589 "OpName %param \"param\"",
590 "OpName %param_0 \"param\"",
591 "OpName %gl_FragColor \"gl_FragColor\"",
592 "%void = OpTypeVoid",
593 "%12 = OpTypeFunction %void",
594 "%float = OpTypeFloat 32",
595 "%_ptr_Function_float = OpTypePointer Function %float",
596 "%15 = OpTypeFunction %float %_ptr_Function_float",
597 "%float_0 = OpConstant %float 0",
598 "%bool = OpTypeBool",
599 "%v4float = OpTypeVector %float 4",
600 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
601 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
602 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
603 "%uint = OpTypeInt 32 0",
604 "%uint_0 = OpConstant %uint 0",
605 "%float_2 = OpConstant %float 2",
606 "%uint_1 = OpConstant %uint 1",
607 "%25 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0",
608 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
609 "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
610 // clang-format on
611 };
612
613 const std::vector<const char*> nonEntryFuncs = {
614 // clang-format off
615 "%foo_f1_ = OpFunction %float None %15",
616 "%bar = OpFunctionParameter %_ptr_Function_float",
617 "%43 = OpLabel",
618 "%r = OpVariable %_ptr_Function_float Function",
619 "%44 = OpLoad %float %bar",
620 "OpStore %r %44",
621 "%45 = OpLoad %float %r",
622 "%46 = OpFOrdLessThan %bool %45 %float_0",
623 "OpSelectionMerge %47 None",
624 "OpBranchConditional %46 %48 %47",
625 "%48 = OpLabel",
626 "%49 = OpLoad %float %r",
627 "%50 = OpFNegate %float %49",
628 "OpStore %r %50",
629 "OpBranch %47",
630 "%47 = OpLabel",
631 "%51 = OpLoad %float %r",
632 "OpReturnValue %51",
633 "OpFunctionEnd",
634 // clang-format on
635 };
636
637 const std::vector<const char*> before = {
638 // clang-format off
639 "%main = OpFunction %void None %12",
640 "%27 = OpLabel",
641 "%color = OpVariable %_ptr_Function_v4float Function",
642 "%param = OpVariable %_ptr_Function_float Function",
643 "%param_0 = OpVariable %_ptr_Function_float Function",
644 "%28 = OpLoad %v4float %BaseColor",
645 "OpStore %color %28",
646 "%29 = OpAccessChain %_ptr_Function_float %color %uint_0",
647 "%30 = OpLoad %float %29",
648 "OpStore %param %30",
649 "%31 = OpFunctionCall %float %foo_f1_ %param",
650 "%32 = OpFOrdGreaterThan %bool %31 %float_2",
651 "OpSelectionMerge %33 None",
652 "OpBranchConditional %32 %34 %33",
653 "%34 = OpLabel",
654 "%35 = OpAccessChain %_ptr_Function_float %color %uint_1",
655 "%36 = OpLoad %float %35",
656 "OpStore %param_0 %36",
657 "%37 = OpFunctionCall %float %foo_f1_ %param_0",
658 "%38 = OpFOrdGreaterThan %bool %37 %float_2",
659 "OpBranch %33",
660 "%33 = OpLabel",
661 "%39 = OpPhi %bool %32 %27 %38 %34",
662 "OpSelectionMerge %40 None",
663 "OpBranchConditional %39 %41 %40",
664 "%41 = OpLabel",
665 "OpStore %color %25",
666 "OpBranch %40",
667 "%40 = OpLabel",
668 "%42 = OpLoad %v4float %color",
669 "OpStore %gl_FragColor %42",
670 "OpReturn",
671 "OpFunctionEnd",
672 // clang-format on
673 };
674
675 const std::vector<const char*> after = {
676 // clang-format off
677 "%main = OpFunction %void None %12",
678 "%27 = OpLabel",
679 "%63 = OpVariable %_ptr_Function_float Function",
680 "%64 = OpVariable %_ptr_Function_float Function",
681 "%52 = OpVariable %_ptr_Function_float Function",
682 "%53 = OpVariable %_ptr_Function_float Function",
683 "%color = OpVariable %_ptr_Function_v4float Function",
684 "%param = OpVariable %_ptr_Function_float Function",
685 "%param_0 = OpVariable %_ptr_Function_float Function",
686 "%28 = OpLoad %v4float %BaseColor",
687 "OpStore %color %28",
688 "%29 = OpAccessChain %_ptr_Function_float %color %uint_0",
689 "%30 = OpLoad %float %29",
690 "OpStore %param %30",
691 "%55 = OpLoad %float %param",
692 "OpStore %52 %55",
693 "%56 = OpLoad %float %52",
694 "%57 = OpFOrdLessThan %bool %56 %float_0",
695 "OpSelectionMerge %61 None",
696 "OpBranchConditional %57 %58 %61",
697 "%58 = OpLabel",
698 "%59 = OpLoad %float %52",
699 "%60 = OpFNegate %float %59",
700 "OpStore %52 %60",
701 "OpBranch %61",
702 "%61 = OpLabel",
703 "%62 = OpLoad %float %52",
704 "OpStore %53 %62",
705 "%31 = OpLoad %float %53",
706 "%32 = OpFOrdGreaterThan %bool %31 %float_2",
707 "OpSelectionMerge %33 None",
708 "OpBranchConditional %32 %34 %33",
709 "%34 = OpLabel",
710 "%35 = OpAccessChain %_ptr_Function_float %color %uint_1",
711 "%36 = OpLoad %float %35",
712 "OpStore %param_0 %36",
713 "%66 = OpLoad %float %param_0",
714 "OpStore %63 %66",
715 "%67 = OpLoad %float %63",
716 "%68 = OpFOrdLessThan %bool %67 %float_0",
717 "OpSelectionMerge %72 None",
718 "OpBranchConditional %68 %69 %72",
719 "%69 = OpLabel",
720 "%70 = OpLoad %float %63",
721 "%71 = OpFNegate %float %70",
722 "OpStore %63 %71",
723 "OpBranch %72",
724 "%72 = OpLabel",
725 "%73 = OpLoad %float %63",
726 "OpStore %64 %73",
727 "%37 = OpLoad %float %64",
728 "%38 = OpFOrdGreaterThan %bool %37 %float_2",
729 "OpBranch %33",
730 "%33 = OpLabel",
731 "%39 = OpPhi %bool %32 %61 %38 %72",
732 "OpSelectionMerge %40 None",
733 "OpBranchConditional %39 %41 %40",
734 "%41 = OpLabel",
735 "OpStore %color %25",
736 "OpBranch %40",
737 "%40 = OpLabel",
738 "%42 = OpLoad %v4float %color",
739 "OpStore %gl_FragColor %42",
740 "OpReturn",
741 "OpFunctionEnd",
742 // clang-format on
743 };
744 SinglePassRunAndCheck<InlineExhaustivePass>(
745 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
746 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
747 /* skip_nop = */ false, /* do_validate = */ true);
748 }
749
750 TEST_F(InlineTest, OpSampledImageOutOfBlock) {
751 // #version 450
752 //
753 // uniform texture2D t2D;
754 // uniform sampler samp;
755 // out vec4 FragColor;
756 // in vec4 BaseColor;
757 //
758 // float foo(vec4 bar)
759 // {
760 // float r = bar.x;
761 // if (r < 0.0)
762 // r = -r;
763 // return r;
764 // }
765 //
766 // void main()
767 // {
768 // vec4 color1 = texture(sampler2D(t2D, samp), vec2(1.0));
769 // vec4 color2 = vec4(foo(BaseColor));
770 // vec4 color3 = texture(sampler2D(t2D, samp), vec2(0.5));
771 // FragColor = (color1 + color2 + color3)/3;
772 // }
773 //
774 // Note: the before SPIR-V will need to be edited to create a use of
775 // the OpSampledImage across the function call.
776 const std::vector<const char*> predefs = {
777 // clang-format off
778 "OpCapability Shader",
779 "%1 = OpExtInstImport \"GLSL.std.450\"",
780 "OpMemoryModel Logical GLSL450",
781 "OpEntryPoint Fragment %main \"main\" %BaseColor %FragColor",
782 "OpExecutionMode %main OriginUpperLeft",
783 "OpSource GLSL 450",
784 "OpName %main \"main\"",
785 "OpName %foo_vf4_ \"foo(vf4;\"",
786 "OpName %bar \"bar\"",
787 "OpName %r \"r\"",
788 "OpName %color1 \"color1\"",
789 "OpName %t2D \"t2D\"",
790 "OpName %samp \"samp\"",
791 "OpName %color2 \"color2\"",
792 "OpName %BaseColor \"BaseColor\"",
793 "OpName %param \"param\"",
794 "OpName %color3 \"color3\"",
795 "OpName %FragColor \"FragColor\"",
796 "OpDecorate %t2D DescriptorSet 0",
797 "OpDecorate %samp DescriptorSet 0",
798 "%void = OpTypeVoid",
799 "%15 = OpTypeFunction %void",
800 "%float = OpTypeFloat 32",
801 "%v4float = OpTypeVector %float 4",
802 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
803 "%19 = OpTypeFunction %float %_ptr_Function_v4float",
804 "%_ptr_Function_float = OpTypePointer Function %float",
805 "%uint = OpTypeInt 32 0",
806 "%uint_0 = OpConstant %uint 0",
807 "%float_0 = OpConstant %float 0",
808 "%bool = OpTypeBool",
809 "%25 = OpTypeImage %float 2D 0 0 0 1 Unknown",
810 "%_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25",
811 "%t2D = OpVariable %_ptr_UniformConstant_25 UniformConstant",
812 "%27 = OpTypeSampler",
813 "%_ptr_UniformConstant_27 = OpTypePointer UniformConstant %27",
814 "%samp = OpVariable %_ptr_UniformConstant_27 UniformConstant",
815 "%29 = OpTypeSampledImage %25",
816 "%v2float = OpTypeVector %float 2",
817 "%float_1 = OpConstant %float 1",
818 "%32 = OpConstantComposite %v2float %float_1 %float_1",
819 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
820 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
821 "%float_0_5 = OpConstant %float 0.5",
822 "%35 = OpConstantComposite %v2float %float_0_5 %float_0_5",
823 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
824 "%FragColor = OpVariable %_ptr_Output_v4float Output",
825 "%float_3 = OpConstant %float 3",
826 // clang-format on
827 };
828
829 const std::vector<const char*> nonEntryFuncs = {
830 // clang-format off
831 "%foo_vf4_ = OpFunction %float None %19",
832 "%bar = OpFunctionParameter %_ptr_Function_v4float",
833 "%56 = OpLabel",
834 "%r = OpVariable %_ptr_Function_float Function",
835 "%57 = OpAccessChain %_ptr_Function_float %bar %uint_0",
836 "%58 = OpLoad %float %57",
837 "OpStore %r %58",
838 "%59 = OpLoad %float %r",
839 "%60 = OpFOrdLessThan %bool %59 %float_0",
840 "OpSelectionMerge %61 None",
841 "OpBranchConditional %60 %62 %61",
842 "%62 = OpLabel",
843 "%63 = OpLoad %float %r",
844 "%64 = OpFNegate %float %63",
845 "OpStore %r %64",
846 "OpBranch %61",
847 "%61 = OpLabel",
848 "%65 = OpLoad %float %r",
849 "OpReturnValue %65",
850 "OpFunctionEnd",
851 // clang-format on
852 };
853
854 const std::vector<const char*> before = {
855 // clang-format off
856 "%main = OpFunction %void None %15",
857 "%38 = OpLabel",
858 "%color1 = OpVariable %_ptr_Function_v4float Function",
859 "%color2 = OpVariable %_ptr_Function_v4float Function",
860 "%param = OpVariable %_ptr_Function_v4float Function",
861 "%color3 = OpVariable %_ptr_Function_v4float Function",
862 "%39 = OpLoad %25 %t2D",
863 "%40 = OpLoad %27 %samp",
864 "%41 = OpSampledImage %29 %39 %40",
865 "%42 = OpImageSampleImplicitLod %v4float %41 %32",
866 "OpStore %color1 %42",
867 "%43 = OpLoad %v4float %BaseColor",
868 "OpStore %param %43",
869 "%44 = OpFunctionCall %float %foo_vf4_ %param",
870 "%45 = OpCompositeConstruct %v4float %44 %44 %44 %44",
871 "OpStore %color2 %45",
872 "%46 = OpLoad %25 %t2D",
873 "%47 = OpLoad %27 %samp",
874 "%48 = OpImageSampleImplicitLod %v4float %41 %35",
875 "OpStore %color3 %48",
876 "%49 = OpLoad %v4float %color1",
877 "%50 = OpLoad %v4float %color2",
878 "%51 = OpFAdd %v4float %49 %50",
879 "%52 = OpLoad %v4float %color3",
880 "%53 = OpFAdd %v4float %51 %52",
881 "%54 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
882 "%55 = OpFDiv %v4float %53 %54",
883 "OpStore %FragColor %55",
884 "OpReturn",
885 "OpFunctionEnd",
886 // clang-format on
887 };
888
889 const std::vector<const char*> after = {
890 // clang-format off
891 "%main = OpFunction %void None %15",
892 "%38 = OpLabel",
893 "%66 = OpVariable %_ptr_Function_float Function",
894 "%67 = OpVariable %_ptr_Function_float Function",
895 "%color1 = OpVariable %_ptr_Function_v4float Function",
896 "%color2 = OpVariable %_ptr_Function_v4float Function",
897 "%param = OpVariable %_ptr_Function_v4float Function",
898 "%color3 = OpVariable %_ptr_Function_v4float Function",
899 "%39 = OpLoad %25 %t2D",
900 "%40 = OpLoad %27 %samp",
901 "%41 = OpSampledImage %29 %39 %40",
902 "%42 = OpImageSampleImplicitLod %v4float %41 %32",
903 "OpStore %color1 %42",
904 "%43 = OpLoad %v4float %BaseColor",
905 "OpStore %param %43",
906 "%69 = OpAccessChain %_ptr_Function_float %param %uint_0",
907 "%70 = OpLoad %float %69",
908 "OpStore %66 %70",
909 "%71 = OpLoad %float %66",
910 "%72 = OpFOrdLessThan %bool %71 %float_0",
911 "OpSelectionMerge %76 None",
912 "OpBranchConditional %72 %73 %76",
913 "%73 = OpLabel",
914 "%74 = OpLoad %float %66",
915 "%75 = OpFNegate %float %74",
916 "OpStore %66 %75",
917 "OpBranch %76",
918 "%76 = OpLabel",
919 "%77 = OpLoad %float %66",
920 "OpStore %67 %77",
921 "%44 = OpLoad %float %67",
922 "%45 = OpCompositeConstruct %v4float %44 %44 %44 %44",
923 "OpStore %color2 %45",
924 "%46 = OpLoad %25 %t2D",
925 "%47 = OpLoad %27 %samp",
926 "%78 = OpSampledImage %29 %39 %40",
927 "%48 = OpImageSampleImplicitLod %v4float %78 %35",
928 "OpStore %color3 %48",
929 "%49 = OpLoad %v4float %color1",
930 "%50 = OpLoad %v4float %color2",
931 "%51 = OpFAdd %v4float %49 %50",
932 "%52 = OpLoad %v4float %color3",
933 "%53 = OpFAdd %v4float %51 %52",
934 "%54 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
935 "%55 = OpFDiv %v4float %53 %54",
936 "OpStore %FragColor %55",
937 "OpReturn",
938 "OpFunctionEnd",
939 // clang-format on
940 };
941 SinglePassRunAndCheck<InlineExhaustivePass>(
942 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
943 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
944 /* skip_nop = */ false, /* do_validate = */ true);
945 }
946
947 TEST_F(InlineTest, OpImageOutOfBlock) {
948 // #version 450
949 //
950 // uniform texture2D t2D;
951 // uniform sampler samp;
952 // uniform sampler samp2;
953 //
954 // out vec4 FragColor;
955 //
956 // in vec4 BaseColor;
957 //
958 // float foo(vec4 bar)
959 // {
960 // float r = bar.x;
961 // if (r < 0.0)
962 // r = -r;
963 // return r;
964 // }
965 //
966 // void main()
967 // {
968 // vec4 color1 = texture(sampler2D(t2D, samp), vec2(1.0));
969 // vec4 color2 = vec4(foo(BaseColor));
970 // vec4 color3 = texture(sampler2D(t2D, samp2), vec2(0.5));
971 // FragColor = (color1 + color2 + color3)/3;
972 // }
973 // Note: the before SPIR-V will need to be edited to create an OpImage
974 // from the first OpSampledImage, place it before the call and use it
975 // in the second OpSampledImage following the call.
976 const std::vector<const char*> predefs = {
977 // clang-format off
978 "OpCapability Shader",
979 "%1 = OpExtInstImport \"GLSL.std.450\"",
980 "OpMemoryModel Logical GLSL450",
981 "OpEntryPoint Fragment %main \"main\" %BaseColor %FragColor",
982 "OpExecutionMode %main OriginUpperLeft",
983 "OpSource GLSL 450",
984 "OpName %main \"main\"",
985 "OpName %foo_vf4_ \"foo(vf4;\"",
986 "OpName %bar \"bar\"",
987 "OpName %r \"r\"",
988 "OpName %color1 \"color1\"",
989 "OpName %t2D \"t2D\"",
990 "OpName %samp \"samp\"",
991 "OpName %color2 \"color2\"",
992 "OpName %BaseColor \"BaseColor\"",
993 "OpName %param \"param\"",
994 "OpName %color3 \"color3\"",
995 "OpName %samp2 \"samp2\"",
996 "OpName %FragColor \"FragColor\"",
997 "OpDecorate %t2D DescriptorSet 0",
998 "OpDecorate %samp DescriptorSet 0",
999 "OpDecorate %samp2 DescriptorSet 0",
1000 "%void = OpTypeVoid",
1001 "%16 = OpTypeFunction %void",
1002 "%float = OpTypeFloat 32",
1003 "%v4float = OpTypeVector %float 4",
1004 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
1005 "%20 = OpTypeFunction %float %_ptr_Function_v4float",
1006 "%_ptr_Function_float = OpTypePointer Function %float",
1007 "%uint = OpTypeInt 32 0",
1008 "%uint_0 = OpConstant %uint 0",
1009 "%float_0 = OpConstant %float 0",
1010 "%bool = OpTypeBool",
1011 "%26 = OpTypeImage %float 2D 0 0 0 1 Unknown",
1012 "%_ptr_UniformConstant_26 = OpTypePointer UniformConstant %26",
1013 "%t2D = OpVariable %_ptr_UniformConstant_26 UniformConstant",
1014 "%28 = OpTypeSampler",
1015 "%_ptr_UniformConstant_28 = OpTypePointer UniformConstant %28",
1016 "%samp = OpVariable %_ptr_UniformConstant_28 UniformConstant",
1017 "%30 = OpTypeSampledImage %26",
1018 "%v2float = OpTypeVector %float 2",
1019 "%float_1 = OpConstant %float 1",
1020 "%33 = OpConstantComposite %v2float %float_1 %float_1",
1021 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
1022 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
1023 "%samp2 = OpVariable %_ptr_UniformConstant_28 UniformConstant",
1024 "%float_0_5 = OpConstant %float 0.5",
1025 "%36 = OpConstantComposite %v2float %float_0_5 %float_0_5",
1026 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
1027 "%FragColor = OpVariable %_ptr_Output_v4float Output",
1028 "%float_3 = OpConstant %float 3",
1029 // clang-format on
1030 };
1031
1032 const std::vector<const char*> nonEntryFuncs = {
1033 // clang-format off
1034 "%foo_vf4_ = OpFunction %float None %20",
1035 "%bar = OpFunctionParameter %_ptr_Function_v4float",
1036 "%58 = OpLabel",
1037 "%r = OpVariable %_ptr_Function_float Function",
1038 "%59 = OpAccessChain %_ptr_Function_float %bar %uint_0",
1039 "%60 = OpLoad %float %59",
1040 "OpStore %r %60",
1041 "%61 = OpLoad %float %r",
1042 "%62 = OpFOrdLessThan %bool %61 %float_0",
1043 "OpSelectionMerge %63 None",
1044 "OpBranchConditional %62 %64 %63",
1045 "%64 = OpLabel",
1046 "%65 = OpLoad %float %r",
1047 "%66 = OpFNegate %float %65",
1048 "OpStore %r %66",
1049 "OpBranch %63",
1050 "%63 = OpLabel",
1051 "%67 = OpLoad %float %r",
1052 "OpReturnValue %67",
1053 "OpFunctionEnd",
1054 // clang-format on
1055 };
1056
1057 const std::vector<const char*> before = {
1058 // clang-format off
1059 "%main = OpFunction %void None %16",
1060 "%39 = OpLabel",
1061 "%color1 = OpVariable %_ptr_Function_v4float Function",
1062 "%color2 = OpVariable %_ptr_Function_v4float Function",
1063 "%param = OpVariable %_ptr_Function_v4float Function",
1064 "%color3 = OpVariable %_ptr_Function_v4float Function",
1065 "%40 = OpLoad %26 %t2D",
1066 "%41 = OpLoad %28 %samp",
1067 "%42 = OpSampledImage %30 %40 %41",
1068 "%43 = OpImageSampleImplicitLod %v4float %42 %33",
1069 "%44 = OpImage %26 %42",
1070 "%45 = OpLoad %28 %samp2",
1071 "OpStore %color1 %43",
1072 "%46 = OpLoad %v4float %BaseColor",
1073 "OpStore %param %46",
1074 "%47 = OpFunctionCall %float %foo_vf4_ %param",
1075 "%48 = OpCompositeConstruct %v4float %47 %47 %47 %47",
1076 "OpStore %color2 %48",
1077 "%49 = OpSampledImage %30 %44 %45",
1078 "%50 = OpImageSampleImplicitLod %v4float %49 %36",
1079 "OpStore %color3 %50",
1080 "%51 = OpLoad %v4float %color1",
1081 "%52 = OpLoad %v4float %color2",
1082 "%53 = OpFAdd %v4float %51 %52",
1083 "%54 = OpLoad %v4float %color3",
1084 "%55 = OpFAdd %v4float %53 %54",
1085 "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
1086 "%57 = OpFDiv %v4float %55 %56",
1087 "OpStore %FragColor %57",
1088 "OpReturn",
1089 "OpFunctionEnd",
1090 // clang-format on
1091 };
1092
1093 const std::vector<const char*> after = {
1094 // clang-format off
1095 "%main = OpFunction %void None %16",
1096 "%39 = OpLabel",
1097 "%68 = OpVariable %_ptr_Function_float Function",
1098 "%69 = OpVariable %_ptr_Function_float Function",
1099 "%color1 = OpVariable %_ptr_Function_v4float Function",
1100 "%color2 = OpVariable %_ptr_Function_v4float Function",
1101 "%param = OpVariable %_ptr_Function_v4float Function",
1102 "%color3 = OpVariable %_ptr_Function_v4float Function",
1103 "%40 = OpLoad %26 %t2D",
1104 "%41 = OpLoad %28 %samp",
1105 "%42 = OpSampledImage %30 %40 %41",
1106 "%43 = OpImageSampleImplicitLod %v4float %42 %33",
1107 "%44 = OpImage %26 %42",
1108 "%45 = OpLoad %28 %samp2",
1109 "OpStore %color1 %43",
1110 "%46 = OpLoad %v4float %BaseColor",
1111 "OpStore %param %46",
1112 "%71 = OpAccessChain %_ptr_Function_float %param %uint_0",
1113 "%72 = OpLoad %float %71",
1114 "OpStore %68 %72",
1115 "%73 = OpLoad %float %68",
1116 "%74 = OpFOrdLessThan %bool %73 %float_0",
1117 "OpSelectionMerge %78 None",
1118 "OpBranchConditional %74 %75 %78",
1119 "%75 = OpLabel",
1120 "%76 = OpLoad %float %68",
1121 "%77 = OpFNegate %float %76",
1122 "OpStore %68 %77",
1123 "OpBranch %78",
1124 "%78 = OpLabel",
1125 "%79 = OpLoad %float %68",
1126 "OpStore %69 %79",
1127 "%47 = OpLoad %float %69",
1128 "%48 = OpCompositeConstruct %v4float %47 %47 %47 %47",
1129 "OpStore %color2 %48",
1130 "%80 = OpSampledImage %30 %40 %41",
1131 "%81 = OpImage %26 %80",
1132 "%49 = OpSampledImage %30 %81 %45",
1133 "%50 = OpImageSampleImplicitLod %v4float %49 %36",
1134 "OpStore %color3 %50",
1135 "%51 = OpLoad %v4float %color1",
1136 "%52 = OpLoad %v4float %color2",
1137 "%53 = OpFAdd %v4float %51 %52",
1138 "%54 = OpLoad %v4float %color3",
1139 "%55 = OpFAdd %v4float %53 %54",
1140 "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
1141 "%57 = OpFDiv %v4float %55 %56",
1142 "OpStore %FragColor %57",
1143 "OpReturn",
1144 "OpFunctionEnd",
1145 // clang-format on
1146 };
1147 SinglePassRunAndCheck<InlineExhaustivePass>(
1148 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
1149 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
1150 /* skip_nop = */ false, /* do_validate = */ true);
1151 }
1152
1153 TEST_F(InlineTest, OpImageAndOpSampledImageOutOfBlock) {
1154 // #version 450
1155 //
1156 // uniform texture2D t2D;
1157 // uniform sampler samp;
1158 // uniform sampler samp2;
1159 //
1160 // out vec4 FragColor;
1161 //
1162 // in vec4 BaseColor;
1163 //
1164 // float foo(vec4 bar)
1165 // {
1166 // float r = bar.x;
1167 // if (r < 0.0)
1168 // r = -r;
1169 // return r;
1170 // }
1171 //
1172 // void main()
1173 // {
1174 // vec4 color1 = texture(sampler2D(t2D, samp), vec2(1.0));
1175 // vec4 color2 = vec4(foo(BaseColor));
1176 // vec4 color3 = texture(sampler2D(t2D, samp2), vec2(0.5));
1177 // FragColor = (color1 + color2 + color3)/3;
1178 // }
1179 // Note: the before SPIR-V will need to be edited to create an OpImage
1180 // and subsequent OpSampledImage that is used across the function call.
1181 const std::vector<const char*> predefs = {
1182 // clang-format off
1183 "OpCapability Shader",
1184 "%1 = OpExtInstImport \"GLSL.std.450\"",
1185 "OpMemoryModel Logical GLSL450",
1186 "OpEntryPoint Fragment %main \"main\" %BaseColor %FragColor",
1187 "OpExecutionMode %main OriginUpperLeft",
1188 "OpSource GLSL 450",
1189 "OpName %main \"main\"",
1190 "OpName %foo_vf4_ \"foo(vf4;\"",
1191 "OpName %bar \"bar\"",
1192 "OpName %r \"r\"",
1193 "OpName %color1 \"color1\"",
1194 "OpName %t2D \"t2D\"",
1195 "OpName %samp \"samp\"",
1196 "OpName %color2 \"color2\"",
1197 "OpName %BaseColor \"BaseColor\"",
1198 "OpName %param \"param\"",
1199 "OpName %color3 \"color3\"",
1200 "OpName %samp2 \"samp2\"",
1201 "OpName %FragColor \"FragColor\"",
1202 "OpDecorate %t2D DescriptorSet 0",
1203 "OpDecorate %samp DescriptorSet 0",
1204 "OpDecorate %samp2 DescriptorSet 0",
1205 "%void = OpTypeVoid",
1206 "%16 = OpTypeFunction %void",
1207 "%float = OpTypeFloat 32",
1208 "%v4float = OpTypeVector %float 4",
1209 "%_ptr_Function_v4float = OpTypePointer Function %v4float",
1210 "%20 = OpTypeFunction %float %_ptr_Function_v4float",
1211 "%_ptr_Function_float = OpTypePointer Function %float",
1212 "%uint = OpTypeInt 32 0",
1213 "%uint_0 = OpConstant %uint 0",
1214 "%float_0 = OpConstant %float 0",
1215 "%bool = OpTypeBool",
1216 "%26 = OpTypeImage %float 2D 0 0 0 1 Unknown",
1217 "%_ptr_UniformConstant_26 = OpTypePointer UniformConstant %26",
1218 "%t2D = OpVariable %_ptr_UniformConstant_26 UniformConstant",
1219 "%28 = OpTypeSampler",
1220 "%_ptr_UniformConstant_28 = OpTypePointer UniformConstant %28",
1221 "%samp = OpVariable %_ptr_UniformConstant_28 UniformConstant",
1222 "%30 = OpTypeSampledImage %26",
1223 "%v2float = OpTypeVector %float 2",
1224 "%float_1 = OpConstant %float 1",
1225 "%33 = OpConstantComposite %v2float %float_1 %float_1",
1226 "%_ptr_Input_v4float = OpTypePointer Input %v4float",
1227 "%BaseColor = OpVariable %_ptr_Input_v4float Input",
1228 "%samp2 = OpVariable %_ptr_UniformConstant_28 UniformConstant",
1229 "%float_0_5 = OpConstant %float 0.5",
1230 "%36 = OpConstantComposite %v2float %float_0_5 %float_0_5",
1231 "%_ptr_Output_v4float = OpTypePointer Output %v4float",
1232 "%FragColor = OpVariable %_ptr_Output_v4float Output",
1233 "%float_3 = OpConstant %float 3",
1234 // clang-format on
1235 };
1236
1237 const std::vector<const char*> nonEntryFuncs = {
1238 // clang-format off
1239 "%foo_vf4_ = OpFunction %float None %20",
1240 "%bar = OpFunctionParameter %_ptr_Function_v4float",
1241 "%58 = OpLabel",
1242 "%r = OpVariable %_ptr_Function_float Function",
1243 "%59 = OpAccessChain %_ptr_Function_float %bar %uint_0",
1244 "%60 = OpLoad %float %59",
1245 "OpStore %r %60",
1246 "%61 = OpLoad %float %r",
1247 "%62 = OpFOrdLessThan %bool %61 %float_0",
1248 "OpSelectionMerge %63 None",
1249 "OpBranchConditional %62 %64 %63",
1250 "%64 = OpLabel",
1251 "%65 = OpLoad %float %r",
1252 "%66 = OpFNegate %float %65",
1253 "OpStore %r %66",
1254 "OpBranch %63",
1255 "%63 = OpLabel",
1256 "%67 = OpLoad %float %r",
1257 "OpReturnValue %67",
1258 "OpFunctionEnd",
1259 // clang-format on
1260 };
1261
1262 const std::vector<const char*> before = {
1263 // clang-format off
1264 "%main = OpFunction %void None %16",
1265 "%39 = OpLabel",
1266 "%color1 = OpVariable %_ptr_Function_v4float Function",
1267 "%color2 = OpVariable %_ptr_Function_v4float Function",
1268 "%param = OpVariable %_ptr_Function_v4float Function",
1269 "%color3 = OpVariable %_ptr_Function_v4float Function",
1270 "%40 = OpLoad %26 %t2D",
1271 "%41 = OpLoad %28 %samp",
1272 "%42 = OpSampledImage %30 %40 %41",
1273 "%43 = OpImageSampleImplicitLod %v4float %42 %33",
1274 "%44 = OpImage %26 %42",
1275 "%45 = OpLoad %28 %samp2",
1276 "%46 = OpSampledImage %30 %44 %45",
1277 "OpStore %color1 %43",
1278 "%47 = OpLoad %v4float %BaseColor",
1279 "OpStore %param %47",
1280 "%48 = OpFunctionCall %float %foo_vf4_ %param",
1281 "%49 = OpCompositeConstruct %v4float %48 %48 %48 %48",
1282 "OpStore %color2 %49",
1283 "%50 = OpImageSampleImplicitLod %v4float %46 %36",
1284 "OpStore %color3 %50",
1285 "%51 = OpLoad %v4float %color1",
1286 "%52 = OpLoad %v4float %color2",
1287 "%53 = OpFAdd %v4float %51 %52",
1288 "%54 = OpLoad %v4float %color3",
1289 "%55 = OpFAdd %v4float %53 %54",
1290 "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
1291 "%57 = OpFDiv %v4float %55 %56",
1292 "OpStore %FragColor %57",
1293 "OpReturn",
1294 "OpFunctionEnd",
1295 // clang-format on
1296 };
1297
1298 const std::vector<const char*> after = {
1299 // clang-format off
1300 "%main = OpFunction %void None %16",
1301 "%39 = OpLabel",
1302 "%68 = OpVariable %_ptr_Function_float Function",
1303 "%69 = OpVariable %_ptr_Function_float Function",
1304 "%color1 = OpVariable %_ptr_Function_v4float Function",
1305 "%color2 = OpVariable %_ptr_Function_v4float Function",
1306 "%param = OpVariable %_ptr_Function_v4float Function",
1307 "%color3 = OpVariable %_ptr_Function_v4float Function",
1308 "%40 = OpLoad %26 %t2D",
1309 "%41 = OpLoad %28 %samp",
1310 "%42 = OpSampledImage %30 %40 %41",
1311 "%43 = OpImageSampleImplicitLod %v4float %42 %33",
1312 "%44 = OpImage %26 %42",
1313 "%45 = OpLoad %28 %samp2",
1314 "%46 = OpSampledImage %30 %44 %45",
1315 "OpStore %color1 %43",
1316 "%47 = OpLoad %v4float %BaseColor",
1317 "OpStore %param %47",
1318 "%71 = OpAccessChain %_ptr_Function_float %param %uint_0",
1319 "%72 = OpLoad %float %71",
1320 "OpStore %68 %72",
1321 "%73 = OpLoad %float %68",
1322 "%74 = OpFOrdLessThan %bool %73 %float_0",
1323 "OpSelectionMerge %78 None",
1324 "OpBranchConditional %74 %75 %78",
1325 "%75 = OpLabel",
1326 "%76 = OpLoad %float %68",
1327 "%77 = OpFNegate %float %76",
1328 "OpStore %68 %77",
1329 "OpBranch %78",
1330 "%78 = OpLabel",
1331 "%79 = OpLoad %float %68",
1332 "OpStore %69 %79",
1333 "%48 = OpLoad %float %69",
1334 "%49 = OpCompositeConstruct %v4float %48 %48 %48 %48",
1335 "OpStore %color2 %49",
1336 "%80 = OpSampledImage %30 %40 %41",
1337 "%81 = OpImage %26 %80",
1338 "%82 = OpSampledImage %30 %81 %45",
1339 "%50 = OpImageSampleImplicitLod %v4float %82 %36",
1340 "OpStore %color3 %50",
1341 "%51 = OpLoad %v4float %color1",
1342 "%52 = OpLoad %v4float %color2",
1343 "%53 = OpFAdd %v4float %51 %52",
1344 "%54 = OpLoad %v4float %color3",
1345 "%55 = OpFAdd %v4float %53 %54",
1346 "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
1347 "%57 = OpFDiv %v4float %55 %56",
1348 "OpStore %FragColor %57",
1349 "OpReturn",
1350 "OpFunctionEnd",
1351 // clang-format on
1352 };
1353 SinglePassRunAndCheck<InlineExhaustivePass>(
1354 JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
1355 JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
1356 /* skip_nop = */ false, /* do_validate = */ true);
1357 }
1358
1359 TEST_F(InlineTest, EarlyReturnInLoopIsNotInlined) {
1360 // #version 140
1361 //
1362 // in vec4 BaseColor;
1363 //
1364 // float foo(vec4 bar)
1365 // {
1366 // while (true) {
1367 // if (bar.x < 0.0)
1368 // return 0.0;
1369 // return bar.x;
1370 // }
1371 // }
1372 //
1373 // void main()
1374 // {
1375 // vec4 color = vec4(foo(BaseColor));
1376 // gl_FragColor = color;
1377 // }
1378
1379 const std::string assembly =
1380 R"(OpCapability Shader
1381 %1 = OpExtInstImport "GLSL.std.450"
1382 OpMemoryModel Logical GLSL450
1383 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
1384 OpExecutionMode %main OriginUpperLeft
1385 OpSource GLSL 140
1386 OpName %main "main"
1387 OpName %foo_vf4_ "foo(vf4;"
1388 OpName %bar "bar"
1389 OpName %color "color"
1390 OpName %BaseColor "BaseColor"
1391 OpName %param "param"
1392 OpName %gl_FragColor "gl_FragColor"
1393 %void = OpTypeVoid
1394 %10 = OpTypeFunction %void
1395 %float = OpTypeFloat 32
1396 %v4float = OpTypeVector %float 4
1397 %_ptr_Function_v4float = OpTypePointer Function %v4float
1398 %14 = OpTypeFunction %float %_ptr_Function_v4float
1399 %bool = OpTypeBool
1400 %true = OpConstantTrue %bool
1401 %uint = OpTypeInt 32 0
1402 %uint_0 = OpConstant %uint 0
1403 %_ptr_Function_float = OpTypePointer Function %float
1404 %float_0 = OpConstant %float 0
1405 %_ptr_Input_v4float = OpTypePointer Input %v4float
1406 %BaseColor = OpVariable %_ptr_Input_v4float Input
1407 %_ptr_Output_v4float = OpTypePointer Output %v4float
1408 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1409 %main = OpFunction %void None %10
1410 %23 = OpLabel
1411 %color = OpVariable %_ptr_Function_v4float Function
1412 %param = OpVariable %_ptr_Function_v4float Function
1413 %24 = OpLoad %v4float %BaseColor
1414 OpStore %param %24
1415 %25 = OpFunctionCall %float %foo_vf4_ %param
1416 %26 = OpCompositeConstruct %v4float %25 %25 %25 %25
1417 OpStore %color %26
1418 %27 = OpLoad %v4float %color
1419 OpStore %gl_FragColor %27
1420 OpReturn
1421 OpFunctionEnd
1422 %foo_vf4_ = OpFunction %float None %14
1423 %bar = OpFunctionParameter %_ptr_Function_v4float
1424 %28 = OpLabel
1425 OpBranch %29
1426 %29 = OpLabel
1427 OpLoopMerge %30 %31 None
1428 OpBranch %32
1429 %32 = OpLabel
1430 OpBranchConditional %true %33 %30
1431 %33 = OpLabel
1432 %34 = OpAccessChain %_ptr_Function_float %bar %uint_0
1433 %35 = OpLoad %float %34
1434 %36 = OpFOrdLessThan %bool %35 %float_0
1435 OpSelectionMerge %37 None
1436 OpBranchConditional %36 %38 %37
1437 %38 = OpLabel
1438 OpReturnValue %float_0
1439 %37 = OpLabel
1440 %39 = OpAccessChain %_ptr_Function_float %bar %uint_0
1441 %40 = OpLoad %float %39
1442 OpReturnValue %40
1443 %31 = OpLabel
1444 OpBranch %29
1445 %30 = OpLabel
1446 %41 = OpUndef %float
1447 OpReturnValue %41
1448 OpFunctionEnd
1449 )";
1450
1451 SinglePassRunAndCheck<InlineExhaustivePass>(assembly, assembly, false, true);
1452 }
1453
1454 TEST_F(InlineTest, ExternalFunctionIsNotInlined) {
1455 // In particular, don't crash.
1456 // See report https://github.com/KhronosGroup/SPIRV-Tools/issues/605
1457 const std::string assembly =
1458 R"(OpCapability Addresses
1459 OpCapability Kernel
1460 OpCapability Linkage
1461 OpMemoryModel Physical32 OpenCL
1462 OpEntryPoint Kernel %1 "entry_pt"
1463 OpDecorate %2 LinkageAttributes "external" Import
1464 %void = OpTypeVoid
1465 %4 = OpTypeFunction %void
1466 %2 = OpFunction %void None %4
1467 OpFunctionEnd
1468 %1 = OpFunction %void None %4
1469 %5 = OpLabel
1470 %6 = OpFunctionCall %void %2
1471 OpReturn
1472 OpFunctionEnd
1473 )";
1474
1475 SinglePassRunAndCheck<InlineExhaustivePass>(assembly, assembly, false, true);
1476 }
1477
1478 TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCallee) {
1479 // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/787
1480 //
1481 // CFG structure is:
1482 // foo:
1483 // fooentry -> fooexit
1484 //
1485 // main:
1486 // entry -> loop
1487 // loop -> loop, merge
1488 // loop calls foo()
1489 // merge
1490 //
1491 // Since the callee has multiple blocks, it will split the calling block
1492 // into at least two, resulting in a new "back-half" block that contains
1493 // the instructions after the inlined function call. If the calling block
1494 // has an OpLoopMerge that points back to the calling block itself, then
1495 // the OpLoopMerge can't remain in the back-half block, but must be
1496 // moved to the end of the original calling block, and it continue target
1497 // operand updated to point to the back-half block.
1498
1499 const std::string predefs =
1500 R"(OpCapability Shader
1501 OpMemoryModel Logical GLSL450
1502 OpEntryPoint GLCompute %1 "main"
1503 OpSource OpenCL_C 120
1504 %bool = OpTypeBool
1505 %true = OpConstantTrue %bool
1506 %void = OpTypeVoid
1507 %5 = OpTypeFunction %void
1508 )";
1509
1510 const std::string nonEntryFuncs =
1511 R"(%6 = OpFunction %void None %5
1512 %7 = OpLabel
1513 OpBranch %8
1514 %8 = OpLabel
1515 OpReturn
1516 OpFunctionEnd
1517 )";
1518
1519 const std::string before =
1520 R"(%1 = OpFunction %void None %5
1521 %9 = OpLabel
1522 OpBranch %10
1523 %10 = OpLabel
1524 %11 = OpFunctionCall %void %6
1525 OpLoopMerge %12 %10 None
1526 OpBranchConditional %true %10 %12
1527 %12 = OpLabel
1528 OpReturn
1529 OpFunctionEnd
1530 )";
1531
1532 const std::string after =
1533 R"(%1 = OpFunction %void None %5
1534 %9 = OpLabel
1535 OpBranch %10
1536 %10 = OpLabel
1537 OpLoopMerge %12 %10 None
1538 OpBranch %14
1539 %14 = OpLabel
1540 OpBranchConditional %true %10 %12
1541 %12 = OpLabel
1542 OpReturn
1543 OpFunctionEnd
1544 )";
1545
1546 const std::string undef = "%11 = OpUndef %void\n";
1547
1548 SinglePassRunAndCheck<InlineExhaustivePass>(
1549 predefs + nonEntryFuncs + before, predefs + undef + nonEntryFuncs + after,
1550 false, true);
1551 }
1552
1553 TEST_F(InlineTest, MultiBlockLoopHeaderCallsMultiBlockCallee) {
1554 // Like SingleBlockLoopCallsMultiBlockCallee but the loop has several
1555 // blocks, but the function call still occurs in the loop header.
1556 // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/800
1557
1558 const std::string predefs =
1559 R"(OpCapability Shader
1560 OpMemoryModel Logical GLSL450
1561 OpEntryPoint GLCompute %1 "main"
1562 OpSource OpenCL_C 120
1563 %bool = OpTypeBool
1564 %true = OpConstantTrue %bool
1565 %int = OpTypeInt 32 1
1566 %int_1 = OpConstant %int 1
1567 %int_2 = OpConstant %int 2
1568 %int_3 = OpConstant %int 3
1569 %int_4 = OpConstant %int 4
1570 %int_5 = OpConstant %int 5
1571 %void = OpTypeVoid
1572 %11 = OpTypeFunction %void
1573 )";
1574
1575 const std::string nonEntryFuncs =
1576 R"(%12 = OpFunction %void None %11
1577 %13 = OpLabel
1578 %14 = OpCopyObject %int %int_1
1579 OpBranch %15
1580 %15 = OpLabel
1581 %16 = OpCopyObject %int %int_2
1582 OpReturn
1583 OpFunctionEnd
1584 )";
1585
1586 const std::string before =
1587 R"(%1 = OpFunction %void None %11
1588 %17 = OpLabel
1589 OpBranch %18
1590 %18 = OpLabel
1591 %19 = OpCopyObject %int %int_3
1592 %20 = OpFunctionCall %void %12
1593 %21 = OpCopyObject %int %int_4
1594 OpLoopMerge %22 %23 None
1595 OpBranchConditional %true %23 %22
1596 %23 = OpLabel
1597 %24 = OpCopyObject %int %int_5
1598 OpBranchConditional %true %18 %22
1599 %22 = OpLabel
1600 OpReturn
1601 OpFunctionEnd
1602 )";
1603
1604 const std::string after =
1605 R"(%1 = OpFunction %void None %11
1606 %17 = OpLabel
1607 OpBranch %18
1608 %18 = OpLabel
1609 %19 = OpCopyObject %int %int_3
1610 %26 = OpCopyObject %int %int_1
1611 OpLoopMerge %22 %23 None
1612 OpBranch %27
1613 %27 = OpLabel
1614 %28 = OpCopyObject %int %int_2
1615 %21 = OpCopyObject %int %int_4
1616 OpBranchConditional %true %23 %22
1617 %23 = OpLabel
1618 %24 = OpCopyObject %int %int_5
1619 OpBranchConditional %true %18 %22
1620 %22 = OpLabel
1621 OpReturn
1622 OpFunctionEnd
1623 )";
1624
1625 const std::string undef = "%20 = OpUndef %void\n";
1626 SinglePassRunAndCheck<InlineExhaustivePass>(
1627 predefs + nonEntryFuncs + before, predefs + undef + nonEntryFuncs + after,
1628 false, true);
1629 }
1630
1631 TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge) {
1632 // This is similar to SingleBlockLoopCallsMultiBlockCallee except
1633 // that calleee block also has a merge instruction in its first block.
1634 // That merge instruction must be an OpSelectionMerge (because the entry
1635 // block of a function can't be the header of a loop since the entry
1636 // block can't be the target of a branch).
1637 //
1638 // In this case the OpLoopMerge can't be placed in the same block as
1639 // the OpSelectionMerge, so inlining must create a new block to contain
1640 // the callee contents.
1641 //
1642 // Additionally, we have two extra OpCopyObject instructions to prove that
1643 // the OpLoopMerge is moved to the right location.
1644 //
1645 // Also ensure that OpPhis within the cloned callee code are valid.
1646 // We need to test that the predecessor blocks are remapped correctly so that
1647 // dominance rules are satisfied
1648
1649 const std::string predefs =
1650 R"(OpCapability Shader
1651 OpMemoryModel Logical GLSL450
1652 OpEntryPoint GLCompute %1 "main"
1653 OpSource OpenCL_C 120
1654 %bool = OpTypeBool
1655 %true = OpConstantTrue %bool
1656 %false = OpConstantFalse %bool
1657 %void = OpTypeVoid
1658 %6 = OpTypeFunction %void
1659 )";
1660
1661 // This callee has multiple blocks, and an OpPhi in the last block
1662 // that references a value from the first block. This tests that
1663 // cloned block IDs are remapped appropriately. The OpPhi dominance
1664 // requires that the remapped %9 must be in a block that dominates
1665 // the remapped %8.
1666 const std::string nonEntryFuncs =
1667 R"(%7 = OpFunction %void None %6
1668 %8 = OpLabel
1669 %9 = OpCopyObject %bool %true
1670 OpSelectionMerge %10 None
1671 OpBranchConditional %true %10 %10
1672 %10 = OpLabel
1673 %11 = OpPhi %bool %9 %8
1674 OpReturn
1675 OpFunctionEnd
1676 )";
1677
1678 const std::string before =
1679 R"(%1 = OpFunction %void None %6
1680 %12 = OpLabel
1681 OpBranch %13
1682 %13 = OpLabel
1683 %14 = OpCopyObject %bool %false
1684 %15 = OpFunctionCall %void %7
1685 OpLoopMerge %16 %13 None
1686 OpBranchConditional %true %13 %16
1687 %16 = OpLabel
1688 OpReturn
1689 OpFunctionEnd
1690 )";
1691
1692 // Note the remapped Phi uses %17 as the parent instead
1693 // of %13, demonstrating that the parent block has been remapped
1694 // correctly.
1695 const std::string after =
1696 R"(%1 = OpFunction %void None %6
1697 %12 = OpLabel
1698 OpBranch %13
1699 %13 = OpLabel
1700 %14 = OpCopyObject %bool %false
1701 OpLoopMerge %16 %13 None
1702 OpBranch %17
1703 %17 = OpLabel
1704 %19 = OpCopyObject %bool %true
1705 OpSelectionMerge %20 None
1706 OpBranchConditional %true %20 %20
1707 %20 = OpLabel
1708 %21 = OpPhi %bool %19 %17
1709 OpBranchConditional %true %13 %16
1710 %16 = OpLabel
1711 OpReturn
1712 OpFunctionEnd
1713 )";
1714 const std::string undef = "%15 = OpUndef %void\n";
1715 SinglePassRunAndCheck<InlineExhaustivePass>(
1716 predefs + nonEntryFuncs + before, predefs + undef + nonEntryFuncs + after,
1717 false, true);
1718 }
1719
1720 TEST_F(InlineTest,
1721 MultiBlockLoopHeaderCallsFromToMultiBlockCalleeHavingSelectionMerge) {
1722 // This is similar to SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge
1723 // but the call is in the header block of a multi block loop.
1724
1725 const std::string predefs =
1726 R"(OpCapability Shader
1727 OpMemoryModel Logical GLSL450
1728 OpEntryPoint GLCompute %1 "main"
1729 OpSource OpenCL_C 120
1730 %bool = OpTypeBool
1731 %true = OpConstantTrue %bool
1732 %int = OpTypeInt 32 1
1733 %int_1 = OpConstant %int 1
1734 %int_2 = OpConstant %int 2
1735 %int_3 = OpConstant %int 3
1736 %int_4 = OpConstant %int 4
1737 %int_5 = OpConstant %int 5
1738 %void = OpTypeVoid
1739 %11 = OpTypeFunction %void
1740 )";
1741
1742 const std::string nonEntryFuncs =
1743 R"(%12 = OpFunction %void None %11
1744 %13 = OpLabel
1745 %14 = OpCopyObject %int %int_1
1746 OpSelectionMerge %15 None
1747 OpBranchConditional %true %15 %15
1748 %15 = OpLabel
1749 %16 = OpCopyObject %int %int_2
1750 OpReturn
1751 OpFunctionEnd
1752 )";
1753
1754 const std::string before =
1755 R"(%1 = OpFunction %void None %11
1756 %17 = OpLabel
1757 OpBranch %18
1758 %18 = OpLabel
1759 %19 = OpCopyObject %int %int_3
1760 %20 = OpFunctionCall %void %12
1761 %21 = OpCopyObject %int %int_4
1762 OpLoopMerge %22 %23 None
1763 OpBranchConditional %true %23 %22
1764 %23 = OpLabel
1765 %24 = OpCopyObject %int %int_5
1766 OpBranchConditional %true %18 %22
1767 %22 = OpLabel
1768 OpReturn
1769 OpFunctionEnd
1770 )";
1771
1772 const std::string after =
1773 R"(%1 = OpFunction %void None %11
1774 %17 = OpLabel
1775 OpBranch %18
1776 %18 = OpLabel
1777 %19 = OpCopyObject %int %int_3
1778 OpLoopMerge %22 %23 None
1779 OpBranch %25
1780 %25 = OpLabel
1781 %27 = OpCopyObject %int %int_1
1782 OpSelectionMerge %28 None
1783 OpBranchConditional %true %28 %28
1784 %28 = OpLabel
1785 %29 = OpCopyObject %int %int_2
1786 %21 = OpCopyObject %int %int_4
1787 OpBranchConditional %true %23 %22
1788 %23 = OpLabel
1789 %24 = OpCopyObject %int %int_5
1790 OpBranchConditional %true %18 %22
1791 %22 = OpLabel
1792 OpReturn
1793 OpFunctionEnd
1794 )";
1795
1796 const std::string undef = "%20 = OpUndef %void\n";
1797 SinglePassRunAndCheck<InlineExhaustivePass>(
1798 predefs + nonEntryFuncs + before, predefs + undef + nonEntryFuncs + after,
1799 false, true);
1800 }
1801
1802 TEST_F(InlineTest, NonInlinableCalleeWithSingleReturn) {
1803 // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
1804 //
1805 // The callee has a single return, but cannot be inlined because the
1806 // return is inside a loop.
1807
1808 const std::string predefs =
1809 R"(OpCapability Shader
1810 %1 = OpExtInstImport "GLSL.std.450"
1811 OpMemoryModel Logical GLSL450
1812 OpEntryPoint Fragment %main "main" %_GLF_color
1813 OpExecutionMode %main OriginUpperLeft
1814 OpSource ESSL 310
1815 OpName %main "main"
1816 OpName %f_ "f("
1817 OpName %i "i"
1818 OpName %_GLF_color "_GLF_color"
1819 OpDecorate %_GLF_color Location 0
1820 %void = OpTypeVoid
1821 %7 = OpTypeFunction %void
1822 %float = OpTypeFloat 32
1823 %9 = OpTypeFunction %float
1824 %float_1 = OpConstant %float 1
1825 %bool = OpTypeBool
1826 %false = OpConstantFalse %bool
1827 %int = OpTypeInt 32 1
1828 %_ptr_Function_int = OpTypePointer Function %int
1829 %int_0 = OpConstant %int 0
1830 %int_1 = OpConstant %int 1
1831 %v4float = OpTypeVector %float 4
1832 %_ptr_Output_v4float = OpTypePointer Output %v4float
1833 %_GLF_color = OpVariable %_ptr_Output_v4float Output
1834 %float_0 = OpConstant %float 0
1835 %20 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
1836 %21 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
1837 )";
1838
1839 const std::string caller =
1840 R"(%main = OpFunction %void None %7
1841 %22 = OpLabel
1842 %i = OpVariable %_ptr_Function_int Function
1843 OpStore %i %int_0
1844 OpBranch %23
1845 %23 = OpLabel
1846 OpLoopMerge %24 %25 None
1847 OpBranch %26
1848 %26 = OpLabel
1849 %27 = OpLoad %int %i
1850 %28 = OpSLessThan %bool %27 %int_1
1851 OpBranchConditional %28 %29 %24
1852 %29 = OpLabel
1853 OpStore %_GLF_color %20
1854 %30 = OpFunctionCall %float %f_
1855 OpBranch %25
1856 %25 = OpLabel
1857 %31 = OpLoad %int %i
1858 %32 = OpIAdd %int %31 %int_1
1859 OpStore %i %32
1860 OpBranch %23
1861 %24 = OpLabel
1862 OpStore %_GLF_color %21
1863 OpReturn
1864 OpFunctionEnd
1865 )";
1866
1867 const std::string callee =
1868 R"(%f_ = OpFunction %float None %9
1869 %33 = OpLabel
1870 OpBranch %34
1871 %34 = OpLabel
1872 OpLoopMerge %35 %36 None
1873 OpBranch %37
1874 %37 = OpLabel
1875 OpReturnValue %float_1
1876 %36 = OpLabel
1877 OpBranch %34
1878 %35 = OpLabel
1879 OpUnreachable
1880 OpFunctionEnd
1881 )";
1882
1883 SinglePassRunAndCheck<InlineExhaustivePass>(
1884 predefs + caller + callee, predefs + caller + callee, false, true);
1885 }
1886
1887 TEST_F(InlineTest, Decorated1) {
1888 // Same test as Simple with the difference
1889 // that OpFAdd in the outlined function is
1890 // decorated with RelaxedPrecision
1891 // Expected result is an equal decoration
1892 // of the corresponding inlined instruction
1893 //
1894 // #version 140
1895 //
1896 // in vec4 BaseColor;
1897 //
1898 // float foo(vec4 bar)
1899 // {
1900 // return bar.x + bar.y;
1901 // }
1902 //
1903 // void main()
1904 // {
1905 // vec4 color = vec4(foo(BaseColor));
1906 // gl_FragColor = color;
1907 // }
1908
1909 const std::string predefs =
1910 R"(OpCapability Shader
1911 %1 = OpExtInstImport "GLSL.std.450"
1912 OpMemoryModel Logical GLSL450
1913 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
1914 OpExecutionMode %main OriginUpperLeft
1915 OpSource GLSL 140
1916 OpName %main "main"
1917 OpName %foo_vf4_ "foo(vf4;"
1918 OpName %bar "bar"
1919 OpName %color "color"
1920 OpName %BaseColor "BaseColor"
1921 OpName %param "param"
1922 OpName %gl_FragColor "gl_FragColor"
1923 OpDecorate %9 RelaxedPrecision
1924 )";
1925
1926 const std::string before =
1927 R"(%void = OpTypeVoid
1928 %11 = OpTypeFunction %void
1929 %float = OpTypeFloat 32
1930 %v4float = OpTypeVector %float 4
1931 %_ptr_Function_v4float = OpTypePointer Function %v4float
1932 %15 = OpTypeFunction %float %_ptr_Function_v4float
1933 %uint = OpTypeInt 32 0
1934 %uint_0 = OpConstant %uint 0
1935 %_ptr_Function_float = OpTypePointer Function %float
1936 %uint_1 = OpConstant %uint 1
1937 %_ptr_Input_v4float = OpTypePointer Input %v4float
1938 %BaseColor = OpVariable %_ptr_Input_v4float Input
1939 %_ptr_Output_v4float = OpTypePointer Output %v4float
1940 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1941 %main = OpFunction %void None %11
1942 %22 = OpLabel
1943 %color = OpVariable %_ptr_Function_v4float Function
1944 %param = OpVariable %_ptr_Function_v4float Function
1945 %23 = OpLoad %v4float %BaseColor
1946 OpStore %param %23
1947 %24 = OpFunctionCall %float %foo_vf4_ %param
1948 %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
1949 OpStore %color %25
1950 %26 = OpLoad %v4float %color
1951 OpStore %gl_FragColor %26
1952 OpReturn
1953 OpFunctionEnd
1954 )";
1955
1956 const std::string after =
1957 R"(OpDecorate %38 RelaxedPrecision
1958 %void = OpTypeVoid
1959 %11 = OpTypeFunction %void
1960 %float = OpTypeFloat 32
1961 %v4float = OpTypeVector %float 4
1962 %_ptr_Function_v4float = OpTypePointer Function %v4float
1963 %15 = OpTypeFunction %float %_ptr_Function_v4float
1964 %uint = OpTypeInt 32 0
1965 %uint_0 = OpConstant %uint 0
1966 %_ptr_Function_float = OpTypePointer Function %float
1967 %uint_1 = OpConstant %uint 1
1968 %_ptr_Input_v4float = OpTypePointer Input %v4float
1969 %BaseColor = OpVariable %_ptr_Input_v4float Input
1970 %_ptr_Output_v4float = OpTypePointer Output %v4float
1971 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1972 %main = OpFunction %void None %11
1973 %22 = OpLabel
1974 %32 = OpVariable %_ptr_Function_float Function
1975 %color = OpVariable %_ptr_Function_v4float Function
1976 %param = OpVariable %_ptr_Function_v4float Function
1977 %23 = OpLoad %v4float %BaseColor
1978 OpStore %param %23
1979 %34 = OpAccessChain %_ptr_Function_float %param %uint_0
1980 %35 = OpLoad %float %34
1981 %36 = OpAccessChain %_ptr_Function_float %param %uint_1
1982 %37 = OpLoad %float %36
1983 %38 = OpFAdd %float %35 %37
1984 OpStore %32 %38
1985 %24 = OpLoad %float %32
1986 %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
1987 OpStore %color %25
1988 %26 = OpLoad %v4float %color
1989 OpStore %gl_FragColor %26
1990 OpReturn
1991 OpFunctionEnd
1992 )";
1993
1994 const std::string nonEntryFuncs =
1995 R"(%foo_vf4_ = OpFunction %float None %15
1996 %bar = OpFunctionParameter %_ptr_Function_v4float
1997 %27 = OpLabel
1998 %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
1999 %29 = OpLoad %float %28
2000 %30 = OpAccessChain %_ptr_Function_float %bar %uint_1
2001 %31 = OpLoad %float %30
2002 %9 = OpFAdd %float %29 %31
2003 OpReturnValue %9
2004 OpFunctionEnd
2005 )";
2006 SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
2007 predefs + after + nonEntryFuncs,
2008 false, true);
2009 }
2010
2011 TEST_F(InlineTest, Decorated2) {
2012 // Same test as Simple with the difference
2013 // that the Result <id> of the outlined OpFunction
2014 // is decorated with RelaxedPrecision
2015 // Expected result is an equal decoration
2016 // of the created return variable
2017 //
2018 // #version 140
2019 //
2020 // in vec4 BaseColor;
2021 //
2022 // float foo(vec4 bar)
2023 // {
2024 // return bar.x + bar.y;
2025 // }
2026 //
2027 // void main()
2028 // {
2029 // vec4 color = vec4(foo(BaseColor));
2030 // gl_FragColor = color;
2031 // }
2032
2033 const std::string predefs =
2034 R"(OpCapability Shader
2035 %1 = OpExtInstImport "GLSL.std.450"
2036 OpMemoryModel Logical GLSL450
2037 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
2038 OpExecutionMode %main OriginUpperLeft
2039 OpSource GLSL 140
2040 OpName %main "main"
2041 OpName %foo_vf4_ "foo(vf4;"
2042 OpName %bar "bar"
2043 OpName %color "color"
2044 OpName %BaseColor "BaseColor"
2045 OpName %param "param"
2046 OpName %gl_FragColor "gl_FragColor"
2047 OpDecorate %foo_vf4_ RelaxedPrecision
2048 )";
2049
2050 const std::string before =
2051 R"(%void = OpTypeVoid
2052 %10 = OpTypeFunction %void
2053 %float = OpTypeFloat 32
2054 %v4float = OpTypeVector %float 4
2055 %_ptr_Function_v4float = OpTypePointer Function %v4float
2056 %14 = OpTypeFunction %float %_ptr_Function_v4float
2057 %uint = OpTypeInt 32 0
2058 %uint_0 = OpConstant %uint 0
2059 %_ptr_Function_float = OpTypePointer Function %float
2060 %uint_1 = OpConstant %uint 1
2061 %_ptr_Input_v4float = OpTypePointer Input %v4float
2062 %BaseColor = OpVariable %_ptr_Input_v4float Input
2063 %_ptr_Output_v4float = OpTypePointer Output %v4float
2064 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
2065 %main = OpFunction %void None %10
2066 %21 = OpLabel
2067 %color = OpVariable %_ptr_Function_v4float Function
2068 %param = OpVariable %_ptr_Function_v4float Function
2069 %22 = OpLoad %v4float %BaseColor
2070 OpStore %param %22
2071 %23 = OpFunctionCall %float %foo_vf4_ %param
2072 %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
2073 OpStore %color %24
2074 %25 = OpLoad %v4float %color
2075 OpStore %gl_FragColor %25
2076 OpReturn
2077 OpFunctionEnd
2078 )";
2079
2080 const std::string after =
2081 R"(OpDecorate %32 RelaxedPrecision
2082 %void = OpTypeVoid
2083 %10 = OpTypeFunction %void
2084 %float = OpTypeFloat 32
2085 %v4float = OpTypeVector %float 4
2086 %_ptr_Function_v4float = OpTypePointer Function %v4float
2087 %14 = OpTypeFunction %float %_ptr_Function_v4float
2088 %uint = OpTypeInt 32 0
2089 %uint_0 = OpConstant %uint 0
2090 %_ptr_Function_float = OpTypePointer Function %float
2091 %uint_1 = OpConstant %uint 1
2092 %_ptr_Input_v4float = OpTypePointer Input %v4float
2093 %BaseColor = OpVariable %_ptr_Input_v4float Input
2094 %_ptr_Output_v4float = OpTypePointer Output %v4float
2095 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
2096 %main = OpFunction %void None %10
2097 %21 = OpLabel
2098 %32 = OpVariable %_ptr_Function_float Function
2099 %color = OpVariable %_ptr_Function_v4float Function
2100 %param = OpVariable %_ptr_Function_v4float Function
2101 %22 = OpLoad %v4float %BaseColor
2102 OpStore %param %22
2103 %34 = OpAccessChain %_ptr_Function_float %param %uint_0
2104 %35 = OpLoad %float %34
2105 %36 = OpAccessChain %_ptr_Function_float %param %uint_1
2106 %37 = OpLoad %float %36
2107 %38 = OpFAdd %float %35 %37
2108 OpStore %32 %38
2109 %23 = OpLoad %float %32
2110 %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
2111 OpStore %color %24
2112 %25 = OpLoad %v4float %color
2113 OpStore %gl_FragColor %25
2114 OpReturn
2115 OpFunctionEnd
2116 )";
2117
2118 const std::string nonEntryFuncs =
2119 R"(%foo_vf4_ = OpFunction %float None %14
2120 %bar = OpFunctionParameter %_ptr_Function_v4float
2121 %26 = OpLabel
2122 %27 = OpAccessChain %_ptr_Function_float %bar %uint_0
2123 %28 = OpLoad %float %27
2124 %29 = OpAccessChain %_ptr_Function_float %bar %uint_1
2125 %30 = OpLoad %float %29
2126 %31 = OpFAdd %float %28 %30
2127 OpReturnValue %31
2128 OpFunctionEnd
2129 )";
2130 SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
2131 predefs + after + nonEntryFuncs,
2132 false, true);
2133 }
2134
2135 TEST_F(InlineTest, DeleteName) {
2136 // Test that the name of the result id of the call is deleted.
2137 const std::string before =
2138 R"(
2139 OpCapability Shader
2140 OpMemoryModel Logical GLSL450
2141 OpEntryPoint Vertex %main "main"
2142 OpName %main "main"
2143 OpName %main_entry "main_entry"
2144 OpName %foo_result "foo_result"
2145 OpName %void_fn "void_fn"
2146 OpName %foo "foo"
2147 OpName %foo_entry "foo_entry"
2148 %void = OpTypeVoid
2149 %void_fn = OpTypeFunction %void
2150 %foo = OpFunction %void None %void_fn
2151 %foo_entry = OpLabel
2152 OpReturn
2153 OpFunctionEnd
2154 %main = OpFunction %void None %void_fn
2155 %main_entry = OpLabel
2156 %foo_result = OpFunctionCall %void %foo
2157 OpReturn
2158 OpFunctionEnd
2159 )";
2160
2161 const std::string after =
2162 R"(OpCapability Shader
2163 OpMemoryModel Logical GLSL450
2164 OpEntryPoint Vertex %main "main"
2165 OpName %main "main"
2166 OpName %main_entry "main_entry"
2167 OpName %void_fn "void_fn"
2168 OpName %foo "foo"
2169 OpName %foo_entry "foo_entry"
2170 %void = OpTypeVoid
2171 %void_fn = OpTypeFunction %void
2172 %3 = OpUndef %void
2173 %foo = OpFunction %void None %void_fn
2174 %foo_entry = OpLabel
2175 OpReturn
2176 OpFunctionEnd
2177 %main = OpFunction %void None %void_fn
2178 %main_entry = OpLabel
2179 OpReturn
2180 OpFunctionEnd
2181 )";
2182
2183 SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
2184 }
2185
2186 TEST_F(InlineTest, SetParent) {
2187 // Test that after inlining all basic blocks have the correct parent.
2188 const std::string text =
2189 R"(
2190 OpCapability Shader
2191 OpMemoryModel Logical GLSL450
2192 OpEntryPoint Vertex %main "main"
2193 OpName %main "main"
2194 OpName %main_entry "main_entry"
2195 OpName %foo_result "foo_result"
2196 OpName %void_fn "void_fn"
2197 OpName %foo "foo"
2198 OpName %foo_entry "foo_entry"
2199 %void = OpTypeVoid
2200 %void_fn = OpTypeFunction %void
2201 %foo = OpFunction %void None %void_fn
2202 %foo_entry = OpLabel
2203 OpReturn
2204 OpFunctionEnd
2205 %main = OpFunction %void None %void_fn
2206 %main_entry = OpLabel
2207 %foo_result = OpFunctionCall %void %foo
2208 OpReturn
2209 OpFunctionEnd
2210 )";
2211
2212 std::unique_ptr<IRContext> context =
2213 BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
2214 InlineExhaustivePass pass;
2215 pass.Run(context.get());
2216
2217 for (Function& func : *context->module()) {
2218 for (BasicBlock& bb : func) {
2219 EXPECT_TRUE(bb.GetParent() == &func);
2220 }
2221 }
2222 }
2223
2224 TEST_F(InlineTest, OpVariableWithInit) {
2225 // Check that there is a store that corresponds to the initializer. This
2226 // test makes sure that is a store to the variable in the loop and before any
2227 // load.
2228 const std::string text = R"(
2229 ; CHECK: OpFunction
2230 ; CHECK-NOT: OpFunctionEnd
2231 ; CHECK: [[var:%\w+]] = OpVariable %_ptr_Function_float Function %float_0
2232 ; CHECK: OpLoopMerge [[outer_merge:%\w+]]
2233 ; CHECK-NOT: OpLoad %float [[var]]
2234 ; CHECK: OpStore [[var]] %float_0
2235 ; CHECK: OpFunctionEnd
2236 OpCapability Shader
2237 %1 = OpExtInstImport "GLSL.std.450"
2238 OpMemoryModel Logical GLSL450
2239 OpEntryPoint Fragment %main "main" %o
2240 OpExecutionMode %main OriginUpperLeft
2241 OpSource GLSL 450
2242 OpDecorate %o Location 0
2243 %void = OpTypeVoid
2244 %3 = OpTypeFunction %void
2245 %float = OpTypeFloat 32
2246 %7 = OpTypeFunction %float
2247 %_ptr_Function_float = OpTypePointer Function %float
2248 %float_0 = OpConstant %float 0
2249 %bool = OpTypeBool
2250 %float_1 = OpConstant %float 1
2251 %_ptr_Output_float = OpTypePointer Output %float
2252 %o = OpVariable %_ptr_Output_float Output
2253 %int = OpTypeInt 32 1
2254 %_ptr_Function_int = OpTypePointer Function %int
2255 %_ptr_Input_int = OpTypePointer Input %int
2256 %int_0 = OpConstant %int 0
2257 %int_1 = OpConstant %int 1
2258 %int_2 = OpConstant %int 2
2259 %main = OpFunction %void None %3
2260 %5 = OpLabel
2261 OpStore %o %float_0
2262 OpBranch %34
2263 %34 = OpLabel
2264 %39 = OpPhi %int %int_0 %5 %47 %37
2265 OpLoopMerge %36 %37 None
2266 OpBranch %38
2267 %38 = OpLabel
2268 %41 = OpSLessThan %bool %39 %int_2
2269 OpBranchConditional %41 %35 %36
2270 %35 = OpLabel
2271 %42 = OpFunctionCall %float %foo_
2272 %43 = OpLoad %float %o
2273 %44 = OpFAdd %float %43 %42
2274 OpStore %o %44
2275 OpBranch %37
2276 %37 = OpLabel
2277 %47 = OpIAdd %int %39 %int_1
2278 OpBranch %34
2279 %36 = OpLabel
2280 OpReturn
2281 OpFunctionEnd
2282 %foo_ = OpFunction %float None %7
2283 %9 = OpLabel
2284 %n = OpVariable %_ptr_Function_float Function %float_0
2285 %13 = OpLoad %float %n
2286 %15 = OpFOrdEqual %bool %13 %float_0
2287 OpSelectionMerge %17 None
2288 OpBranchConditional %15 %16 %17
2289 %16 = OpLabel
2290 %19 = OpLoad %float %n
2291 %20 = OpFAdd %float %19 %float_1
2292 OpStore %n %20
2293 OpBranch %17
2294 %17 = OpLabel
2295 %21 = OpLoad %float %n
2296 OpReturnValue %21
2297 OpFunctionEnd
2298 )";
2299
2300 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
2301 }
2302
2303 TEST_F(InlineTest, DontInlineDirectlyRecursiveFunc) {
2304 // Test that the name of the result id of the call is deleted.
2305 const std::string test =
2306 R"(OpCapability Shader
2307 OpMemoryModel Logical GLSL450
2308 OpEntryPoint Fragment %1 "main"
2309 OpExecutionMode %1 OriginUpperLeft
2310 OpDecorate %2 DescriptorSet 439418829
2311 %void = OpTypeVoid
2312 %4 = OpTypeFunction %void
2313 %float = OpTypeFloat 32
2314 %_struct_6 = OpTypeStruct %float %float
2315 %15 = OpConstantNull %_struct_6
2316 %7 = OpTypeFunction %_struct_6
2317 %1 = OpFunction %void Pure|Const %4
2318 %8 = OpLabel
2319 %2 = OpFunctionCall %_struct_6 %9
2320 OpKill
2321 OpFunctionEnd
2322 %9 = OpFunction %_struct_6 None %7
2323 %10 = OpLabel
2324 %11 = OpFunctionCall %_struct_6 %9
2325 OpReturnValue %15
2326 OpFunctionEnd
2327 )";
2328
2329 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2330 SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
2331 }
2332
2333 TEST_F(InlineTest, DontInlineInDirectlyRecursiveFunc) {
2334 // Test that the name of the result id of the call is deleted.
2335 const std::string test =
2336 R"(OpCapability Shader
2337 OpMemoryModel Logical GLSL450
2338 OpEntryPoint Fragment %1 "main"
2339 OpExecutionMode %1 OriginUpperLeft
2340 OpDecorate %2 DescriptorSet 439418829
2341 %void = OpTypeVoid
2342 %4 = OpTypeFunction %void
2343 %float = OpTypeFloat 32
2344 %_struct_6 = OpTypeStruct %float %float
2345 %15 = OpConstantNull %_struct_6
2346 %7 = OpTypeFunction %_struct_6
2347 %1 = OpFunction %void Pure|Const %4
2348 %8 = OpLabel
2349 %2 = OpFunctionCall %_struct_6 %9
2350 OpKill
2351 OpFunctionEnd
2352 %9 = OpFunction %_struct_6 None %7
2353 %10 = OpLabel
2354 %11 = OpFunctionCall %_struct_6 %12
2355 OpReturnValue %15
2356 OpFunctionEnd
2357 %12 = OpFunction %_struct_6 None %7
2358 %13 = OpLabel
2359 %14 = OpFunctionCall %_struct_6 %9
2360 OpReturnValue %15
2361 OpFunctionEnd
2362 )";
2363
2364 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2365 SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
2366 }
2367
2368 TEST_F(InlineTest, DontInlineFuncWithOpKillInContinue) {
2369 const std::string test =
2370 R"(OpCapability Shader
2371 %1 = OpExtInstImport "GLSL.std.450"
2372 OpMemoryModel Logical GLSL450
2373 OpEntryPoint Fragment %main "main"
2374 OpExecutionMode %main OriginUpperLeft
2375 OpSource GLSL 330
2376 OpName %main "main"
2377 OpName %kill_ "kill("
2378 %void = OpTypeVoid
2379 %3 = OpTypeFunction %void
2380 %bool = OpTypeBool
2381 %true = OpConstantTrue %bool
2382 %main = OpFunction %void None %3
2383 %5 = OpLabel
2384 OpBranch %9
2385 %9 = OpLabel
2386 OpLoopMerge %11 %12 None
2387 OpBranch %13
2388 %13 = OpLabel
2389 OpBranchConditional %true %10 %11
2390 %10 = OpLabel
2391 OpBranch %12
2392 %12 = OpLabel
2393 %16 = OpFunctionCall %void %kill_
2394 OpBranch %9
2395 %11 = OpLabel
2396 OpReturn
2397 OpFunctionEnd
2398 %kill_ = OpFunction %void None %3
2399 %7 = OpLabel
2400 OpKill
2401 OpFunctionEnd
2402 )";
2403
2404 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2405 SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
2406 }
2407
2408 TEST_F(InlineTest, DontInlineFuncWithDontInline) {
2409 // Check that the function with DontInline flag is not inlined.
2410 const std::string text = R"(
2411 ; CHECK: %foo = OpFunction %int DontInline
2412 ; CHECK: OpReturnValue %int_0
2413
2414 OpCapability Shader
2415 OpMemoryModel Logical GLSL450
2416 OpEntryPoint Fragment %main "main"
2417 OpExecutionMode %main OriginUpperLeft
2418 OpSource HLSL 600
2419 OpName %main "main"
2420 OpName %foo "foo"
2421 %int = OpTypeInt 32 1
2422 %int_0 = OpConstant %int 0
2423 %void = OpTypeVoid
2424 %6 = OpTypeFunction %void
2425 %7 = OpTypeFunction %int
2426 %main = OpFunction %void None %6
2427 %8 = OpLabel
2428 %9 = OpFunctionCall %int %foo
2429 OpReturn
2430 OpFunctionEnd
2431 %foo = OpFunction %int DontInline %7
2432 %10 = OpLabel
2433 OpReturnValue %int_0
2434 OpFunctionEnd
2435 )";
2436
2437 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
2438 }
2439
2440 TEST_F(InlineTest, InlineFuncWithOpKillNotInContinue) {
2441 const std::string before =
2442 R"(OpCapability Shader
2443 %1 = OpExtInstImport "GLSL.std.450"
2444 OpMemoryModel Logical GLSL450
2445 OpEntryPoint Fragment %main "main"
2446 OpExecutionMode %main OriginUpperLeft
2447 OpSource GLSL 330
2448 OpName %main "main"
2449 OpName %kill_ "kill("
2450 %void = OpTypeVoid
2451 %3 = OpTypeFunction %void
2452 %bool = OpTypeBool
2453 %true = OpConstantTrue %bool
2454 %main = OpFunction %void None %3
2455 %5 = OpLabel
2456 %16 = OpFunctionCall %void %kill_
2457 OpReturn
2458 OpFunctionEnd
2459 %kill_ = OpFunction %void None %3
2460 %7 = OpLabel
2461 OpKill
2462 OpFunctionEnd
2463 )";
2464
2465 const std::string after =
2466 R"(OpCapability Shader
2467 %1 = OpExtInstImport "GLSL.std.450"
2468 OpMemoryModel Logical GLSL450
2469 OpEntryPoint Fragment %main "main"
2470 OpExecutionMode %main OriginUpperLeft
2471 OpSource GLSL 330
2472 OpName %main "main"
2473 OpName %kill_ "kill("
2474 %void = OpTypeVoid
2475 %3 = OpTypeFunction %void
2476 %bool = OpTypeBool
2477 %true = OpConstantTrue %bool
2478 %16 = OpUndef %void
2479 %main = OpFunction %void None %3
2480 %5 = OpLabel
2481 OpKill
2482 %18 = OpLabel
2483 OpReturn
2484 OpFunctionEnd
2485 %kill_ = OpFunction %void None %3
2486 %7 = OpLabel
2487 OpKill
2488 OpFunctionEnd
2489 )";
2490
2491 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2492 SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
2493 }
2494
2495 TEST_F(InlineTest, DontInlineFuncWithOpTerminateInvocationInContinue) {
2496 const std::string test =
2497 R"(OpCapability Shader
2498 OpExtension "SPV_KHR_terminate_invocation"
2499 %1 = OpExtInstImport "GLSL.std.450"
2500 OpMemoryModel Logical GLSL450
2501 OpEntryPoint Fragment %main "main"
2502 OpExecutionMode %main OriginUpperLeft
2503 OpSource GLSL 330
2504 OpName %main "main"
2505 OpName %kill_ "kill("
2506 %void = OpTypeVoid
2507 %3 = OpTypeFunction %void
2508 %bool = OpTypeBool
2509 %true = OpConstantTrue %bool
2510 %main = OpFunction %void None %3
2511 %5 = OpLabel
2512 OpBranch %9
2513 %9 = OpLabel
2514 OpLoopMerge %11 %12 None
2515 OpBranch %13
2516 %13 = OpLabel
2517 OpBranchConditional %true %10 %11
2518 %10 = OpLabel
2519 OpBranch %12
2520 %12 = OpLabel
2521 %16 = OpFunctionCall %void %kill_
2522 OpBranch %9
2523 %11 = OpLabel
2524 OpReturn
2525 OpFunctionEnd
2526 %kill_ = OpFunction %void None %3
2527 %7 = OpLabel
2528 OpTerminateInvocation
2529 OpFunctionEnd
2530 )";
2531
2532 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2533 SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
2534 }
2535
2536 TEST_F(InlineTest, InlineFuncWithOpTerminateInvocationNotInContinue) {
2537 const std::string before =
2538 R"(OpCapability Shader
2539 OpExtension "SPV_KHR_terminate_invocation"
2540 %1 = OpExtInstImport "GLSL.std.450"
2541 OpMemoryModel Logical GLSL450
2542 OpEntryPoint Fragment %main "main"
2543 OpExecutionMode %main OriginUpperLeft
2544 OpSource GLSL 330
2545 OpName %main "main"
2546 OpName %kill_ "kill("
2547 %void = OpTypeVoid
2548 %3 = OpTypeFunction %void
2549 %bool = OpTypeBool
2550 %true = OpConstantTrue %bool
2551 %main = OpFunction %void None %3
2552 %5 = OpLabel
2553 %16 = OpFunctionCall %void %kill_
2554 OpReturn
2555 OpFunctionEnd
2556 %kill_ = OpFunction %void None %3
2557 %7 = OpLabel
2558 OpTerminateInvocation
2559 OpFunctionEnd
2560 )";
2561
2562 const std::string after =
2563 R"(OpCapability Shader
2564 OpExtension "SPV_KHR_terminate_invocation"
2565 %1 = OpExtInstImport "GLSL.std.450"
2566 OpMemoryModel Logical GLSL450
2567 OpEntryPoint Fragment %main "main"
2568 OpExecutionMode %main OriginUpperLeft
2569 OpSource GLSL 330
2570 OpName %main "main"
2571 OpName %kill_ "kill("
2572 %void = OpTypeVoid
2573 %3 = OpTypeFunction %void
2574 %bool = OpTypeBool
2575 %true = OpConstantTrue %bool
2576 %16 = OpUndef %void
2577 %main = OpFunction %void None %3
2578 %5 = OpLabel
2579 OpTerminateInvocation
2580 %18 = OpLabel
2581 OpReturn
2582 OpFunctionEnd
2583 %kill_ = OpFunction %void None %3
2584 %7 = OpLabel
2585 OpTerminateInvocation
2586 OpFunctionEnd
2587 )";
2588
2589 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2590 SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
2591 }
2592
2593 TEST_F(InlineTest, EarlyReturnFunctionInlined) {
2594 // #version 140
2595 //
2596 // in vec4 BaseColor;
2597 //
2598 // float foo(vec4 bar)
2599 // {
2600 // if (bar.x < 0.0)
2601 // return 0.0;
2602 // return bar.x;
2603 // }
2604 //
2605 // void main()
2606 // {
2607 // vec4 color = vec4(foo(BaseColor));
2608 // gl_FragColor = color;
2609 // }
2610
2611 const std::string predefs =
2612 R"(OpCapability Shader
2613 %1 = OpExtInstImport "GLSL.std.450"
2614 OpMemoryModel Logical GLSL450
2615 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
2616 OpExecutionMode %main OriginUpperLeft
2617 OpSource GLSL 140
2618 OpName %main "main"
2619 OpName %foo_vf4_ "foo(vf4;"
2620 OpName %bar "bar"
2621 OpName %color "color"
2622 OpName %BaseColor "BaseColor"
2623 OpName %param "param"
2624 OpName %gl_FragColor "gl_FragColor"
2625 %void = OpTypeVoid
2626 %10 = OpTypeFunction %void
2627 %float = OpTypeFloat 32
2628 %v4float = OpTypeVector %float 4
2629 %_ptr_Function_v4float = OpTypePointer Function %v4float
2630 %14 = OpTypeFunction %float %_ptr_Function_v4float
2631 %uint = OpTypeInt 32 0
2632 %uint_0 = OpConstant %uint 0
2633 %_ptr_Function_float = OpTypePointer Function %float
2634 %float_0 = OpConstant %float 0
2635 %bool = OpTypeBool
2636 %_ptr_Input_v4float = OpTypePointer Input %v4float
2637 %BaseColor = OpVariable %_ptr_Input_v4float Input
2638 %_ptr_Output_v4float = OpTypePointer Output %v4float
2639 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
2640 )";
2641
2642 const std::string foo =
2643 R"(%foo_vf4_ = OpFunction %float None %14
2644 %bar = OpFunctionParameter %_ptr_Function_v4float
2645 %27 = OpLabel
2646 %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
2647 %29 = OpLoad %float %28
2648 %30 = OpFOrdLessThan %bool %29 %float_0
2649 OpSelectionMerge %31 None
2650 OpBranchConditional %30 %32 %31
2651 %32 = OpLabel
2652 OpReturnValue %float_0
2653 %31 = OpLabel
2654 %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
2655 %34 = OpLoad %float %33
2656 OpReturnValue %34
2657 OpFunctionEnd
2658 )";
2659
2660 const std::string fooMergeReturn =
2661 R"(%foo_vf4_ = OpFunction %float None %14
2662 %bar = OpFunctionParameter %_ptr_Function_v4float
2663 %27 = OpLabel
2664 %41 = OpVariable %_ptr_Function_bool Function %false
2665 %36 = OpVariable %_ptr_Function_float Function
2666 OpSelectionMerge %35 None
2667 OpSwitch %uint_0 %38
2668 %38 = OpLabel
2669 %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
2670 %29 = OpLoad %float %28
2671 %30 = OpFOrdLessThan %bool %29 %float_0
2672 OpSelectionMerge %31 None
2673 OpBranchConditional %30 %32 %31
2674 %32 = OpLabel
2675 OpStore %41 %true
2676 OpStore %36 %float_0
2677 OpBranch %35
2678 %31 = OpLabel
2679 %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
2680 %34 = OpLoad %float %33
2681 OpStore %41 %true
2682 OpStore %36 %34
2683 OpBranch %35
2684 %35 = OpLabel
2685 %37 = OpLoad %float %36
2686 OpReturnValue %37
2687 OpFunctionEnd
2688 )";
2689
2690 const std::string before =
2691 R"(%main = OpFunction %void None %10
2692 %22 = OpLabel
2693 %color = OpVariable %_ptr_Function_v4float Function
2694 %param = OpVariable %_ptr_Function_v4float Function
2695 %23 = OpLoad %v4float %BaseColor
2696 OpStore %param %23
2697 %24 = OpFunctionCall %float %foo_vf4_ %param
2698 %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
2699 OpStore %color %25
2700 %26 = OpLoad %v4float %color
2701 OpStore %gl_FragColor %26
2702 OpReturn
2703 OpFunctionEnd
2704 )";
2705
2706 const std::string after =
2707 R"(%false = OpConstantFalse %bool
2708 %_ptr_Function_bool = OpTypePointer Function %bool
2709 %true = OpConstantTrue %bool
2710 %main = OpFunction %void None %10
2711 %22 = OpLabel
2712 %43 = OpVariable %_ptr_Function_bool Function %false
2713 %44 = OpVariable %_ptr_Function_float Function
2714 %45 = OpVariable %_ptr_Function_float Function
2715 %color = OpVariable %_ptr_Function_v4float Function
2716 %param = OpVariable %_ptr_Function_v4float Function
2717 %23 = OpLoad %v4float %BaseColor
2718 OpStore %param %23
2719 OpStore %43 %false
2720 OpSelectionMerge %55 None
2721 OpSwitch %uint_0 %47
2722 %47 = OpLabel
2723 %48 = OpAccessChain %_ptr_Function_float %param %uint_0
2724 %49 = OpLoad %float %48
2725 %50 = OpFOrdLessThan %bool %49 %float_0
2726 OpSelectionMerge %52 None
2727 OpBranchConditional %50 %51 %52
2728 %51 = OpLabel
2729 OpStore %43 %true
2730 OpStore %44 %float_0
2731 OpBranch %55
2732 %52 = OpLabel
2733 %53 = OpAccessChain %_ptr_Function_float %param %uint_0
2734 %54 = OpLoad %float %53
2735 OpStore %43 %true
2736 OpStore %44 %54
2737 OpBranch %55
2738 %55 = OpLabel
2739 %56 = OpLoad %float %44
2740 OpStore %45 %56
2741 %24 = OpLoad %float %45
2742 %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
2743 OpStore %color %25
2744 %26 = OpLoad %v4float %color
2745 OpStore %gl_FragColor %26
2746 OpReturn
2747 OpFunctionEnd
2748 )";
2749
2750 // The early return case must be handled by merge-return first.
2751 AddPass<MergeReturnPass>();
2752 AddPass<InlineExhaustivePass>();
2753 RunAndCheck(predefs + before + foo, predefs + after + fooMergeReturn);
2754 }
2755
2756 TEST_F(InlineTest, EarlyReturnNotAppearingLastInFunctionInlined) {
2757 // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/755
2758 //
2759 // Original example is derived from:
2760 //
2761 // #version 450
2762 //
2763 // float foo() {
2764 // if (true) {
2765 // }
2766 // }
2767 //
2768 // void main() { foo(); }
2769 //
2770 // But the order of basic blocks in foo is changed so that the return
2771 // block is listed second-last. There is only one return in the callee
2772 // but it does not appear last.
2773
2774 const std::string predefs =
2775 R"(OpCapability Shader
2776 OpMemoryModel Logical GLSL450
2777 OpEntryPoint Vertex %main "main"
2778 OpSource GLSL 450
2779 OpName %main "main"
2780 OpName %foo_ "foo("
2781 %void = OpTypeVoid
2782 %4 = OpTypeFunction %void
2783 %bool = OpTypeBool
2784 %true = OpConstantTrue %bool
2785 )";
2786
2787 const std::string foo =
2788 R"(%foo_ = OpFunction %void None %4
2789 %7 = OpLabel
2790 OpSelectionMerge %8 None
2791 OpBranchConditional %true %9 %8
2792 %8 = OpLabel
2793 OpReturn
2794 %9 = OpLabel
2795 OpBranch %8
2796 OpFunctionEnd
2797 )";
2798
2799 const std::string fooMergeReturn =
2800 R"(%uint = OpTypeInt 32 0
2801 %uint_0 = OpConstant %uint 0
2802 %false = OpConstantFalse %bool
2803 %_ptr_Function_bool = OpTypePointer Function %bool
2804 %11 = OpUndef %void
2805 %foo_ = OpFunction %void None %4
2806 %7 = OpLabel
2807 %18 = OpVariable %_ptr_Function_bool Function %false
2808 OpSelectionMerge %12 None
2809 OpSwitch %uint_0 %13
2810 %13 = OpLabel
2811 OpSelectionMerge %8 None
2812 OpBranchConditional %true %9 %8
2813 %8 = OpLabel
2814 OpStore %18 %true
2815 OpBranch %12
2816 %9 = OpLabel
2817 OpBranch %8
2818 %12 = OpLabel
2819 OpReturn
2820 OpFunctionEnd
2821 )";
2822
2823 const std::string before =
2824 R"(%main = OpFunction %void None %4
2825 %10 = OpLabel
2826 %11 = OpFunctionCall %void %foo_
2827 OpReturn
2828 OpFunctionEnd
2829 )";
2830
2831 const std::string after =
2832 R"(%main = OpFunction %void None %4
2833 %10 = OpLabel
2834 %19 = OpVariable %_ptr_Function_bool Function %false
2835 OpStore %19 %false
2836 OpSelectionMerge %24 None
2837 OpSwitch %uint_0 %21
2838 %21 = OpLabel
2839 OpSelectionMerge %22 None
2840 OpBranchConditional %true %23 %22
2841 %22 = OpLabel
2842 OpStore %19 %true
2843 OpBranch %24
2844 %23 = OpLabel
2845 OpBranch %22
2846 %24 = OpLabel
2847 OpReturn
2848 OpFunctionEnd
2849 )";
2850
2851 // The early return case must be handled by merge-return first.
2852 AddPass<MergeReturnPass>();
2853 AddPass<InlineExhaustivePass>();
2854 RunAndCheck(predefs + foo + before, predefs + fooMergeReturn + after);
2855 }
2856
2857 TEST_F(InlineTest, CalleeWithSingleReturnNeedsSingleTripLoopWrapper) {
2858 // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
2859 //
2860 // The callee has a single return, but needs single-trip loop wrapper
2861 // to be inlined because the return is in a selection structure.
2862
2863 const std::string predefs =
2864 R"(OpCapability Shader
2865 %1 = OpExtInstImport "GLSL.std.450"
2866 OpMemoryModel Logical GLSL450
2867 OpEntryPoint Fragment %main "main" %_GLF_color
2868 OpExecutionMode %main OriginUpperLeft
2869 OpSource ESSL 310
2870 OpName %main "main"
2871 OpName %f_ "f("
2872 OpName %i "i"
2873 OpName %_GLF_color "_GLF_color"
2874 OpDecorate %_GLF_color Location 0
2875 %void = OpTypeVoid
2876 %7 = OpTypeFunction %void
2877 %float = OpTypeFloat 32
2878 %9 = OpTypeFunction %float
2879 %float_1 = OpConstant %float 1
2880 %bool = OpTypeBool
2881 %false = OpConstantFalse %bool
2882 %true = OpConstantTrue %bool
2883 %int = OpTypeInt 32 1
2884 %_ptr_Function_int = OpTypePointer Function %int
2885 %int_0 = OpConstant %int 0
2886 %int_1 = OpConstant %int 1
2887 %v4float = OpTypeVector %float 4
2888 %_ptr_Output_v4float = OpTypePointer Output %v4float
2889 %_GLF_color = OpVariable %_ptr_Output_v4float Output
2890 %float_0 = OpConstant %float 0
2891 %21 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
2892 %22 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
2893 )";
2894
2895 const std::string new_predefs =
2896 R"(%_ptr_Function_float = OpTypePointer Function %float
2897 %uint = OpTypeInt 32 0
2898 %uint_0 = OpConstant %uint 0
2899 %_ptr_Function_bool = OpTypePointer Function %bool
2900 )";
2901
2902 const std::string main_before =
2903 R"(%main = OpFunction %void None %7
2904 %23 = OpLabel
2905 %i = OpVariable %_ptr_Function_int Function
2906 OpStore %i %int_0
2907 OpBranch %24
2908 %24 = OpLabel
2909 OpLoopMerge %25 %26 None
2910 OpBranch %27
2911 %27 = OpLabel
2912 %28 = OpLoad %int %i
2913 %29 = OpSLessThan %bool %28 %int_1
2914 OpBranchConditional %29 %30 %25
2915 %30 = OpLabel
2916 OpStore %_GLF_color %21
2917 %31 = OpFunctionCall %float %f_
2918 OpBranch %26
2919 %26 = OpLabel
2920 %32 = OpLoad %int %i
2921 %33 = OpIAdd %int %32 %int_1
2922 OpStore %i %33
2923 OpBranch %24
2924 %25 = OpLabel
2925 OpStore %_GLF_color %22
2926 OpReturn
2927 OpFunctionEnd
2928 )";
2929
2930 const std::string main_after =
2931 R"(%main = OpFunction %void None %7
2932 %23 = OpLabel
2933 %46 = OpVariable %_ptr_Function_bool Function %false
2934 %47 = OpVariable %_ptr_Function_float Function
2935 %48 = OpVariable %_ptr_Function_float Function
2936 %i = OpVariable %_ptr_Function_int Function
2937 OpStore %i %int_0
2938 OpBranch %24
2939 %24 = OpLabel
2940 OpLoopMerge %25 %26 None
2941 OpBranch %27
2942 %27 = OpLabel
2943 %28 = OpLoad %int %i
2944 %29 = OpSLessThan %bool %28 %int_1
2945 OpBranchConditional %29 %30 %25
2946 %30 = OpLabel
2947 OpStore %_GLF_color %21
2948 OpStore %46 %false
2949 OpSelectionMerge %53 None
2950 OpSwitch %uint_0 %50
2951 %50 = OpLabel
2952 OpSelectionMerge %52 None
2953 OpBranchConditional %true %51 %52
2954 %51 = OpLabel
2955 OpStore %46 %true
2956 OpStore %47 %float_1
2957 OpBranch %53
2958 %52 = OpLabel
2959 OpStore %46 %true
2960 OpStore %47 %float_1
2961 OpBranch %53
2962 %53 = OpLabel
2963 %54 = OpLoad %float %47
2964 OpStore %48 %54
2965 %31 = OpLoad %float %48
2966 OpBranch %26
2967 %26 = OpLabel
2968 %32 = OpLoad %int %i
2969 %33 = OpIAdd %int %32 %int_1
2970 OpStore %i %33
2971 OpBranch %24
2972 %25 = OpLabel
2973 OpStore %_GLF_color %22
2974 OpReturn
2975 OpFunctionEnd
2976 )";
2977
2978 const std::string callee =
2979 R"(%f_ = OpFunction %float None %9
2980 %34 = OpLabel
2981 OpSelectionMerge %35 None
2982 OpBranchConditional %true %36 %35
2983 %36 = OpLabel
2984 OpReturnValue %float_1
2985 %35 = OpLabel
2986 OpReturnValue %float_1
2987 OpFunctionEnd
2988 )";
2989
2990 const std::string calleeMergeReturn =
2991 R"(%f_ = OpFunction %float None %9
2992 %34 = OpLabel
2993 %45 = OpVariable %_ptr_Function_bool Function %false
2994 %39 = OpVariable %_ptr_Function_float Function
2995 OpSelectionMerge %37 None
2996 OpSwitch %uint_0 %41
2997 %41 = OpLabel
2998 OpSelectionMerge %35 None
2999 OpBranchConditional %true %36 %35
3000 %36 = OpLabel
3001 OpStore %45 %true
3002 OpStore %39 %float_1
3003 OpBranch %37
3004 %35 = OpLabel
3005 OpStore %45 %true
3006 OpStore %39 %float_1
3007 OpBranch %37
3008 %37 = OpLabel
3009 %40 = OpLoad %float %39
3010 OpReturnValue %40
3011 OpFunctionEnd
3012 )";
3013
3014 // The early return case must be handled by merge-return first.
3015 AddPass<MergeReturnPass>();
3016 AddPass<InlineExhaustivePass>();
3017 RunAndCheck(predefs + main_before + callee,
3018 predefs + new_predefs + main_after + calleeMergeReturn);
3019 }
3020
3021 TEST_F(InlineTest, ForwardReferencesInPhiInlined) {
3022 // The basic structure of the test case is like this:
3023 //
3024 // int foo() {
3025 // int result = 1;
3026 // if (true) {
3027 // result = 1;
3028 // }
3029 // return result;
3030 // }
3031 //
3032 // void main() {
3033 // int x = foo();
3034 // }
3035 //
3036 // but with modifications: Using Phi instead of load/store, and the
3037 // return block in foo appears before the "then" block.
3038
3039 const std::string predefs =
3040 R"(OpCapability Shader
3041 %1 = OpExtInstImport "GLSL.std.450"
3042 OpMemoryModel Logical GLSL450
3043 OpEntryPoint Vertex %main "main"
3044 OpSource GLSL 450
3045 OpName %main "main"
3046 OpName %foo_ "foo("
3047 OpName %x "x"
3048 %void = OpTypeVoid
3049 %6 = OpTypeFunction %void
3050 %int = OpTypeInt 32 1
3051 %8 = OpTypeFunction %int
3052 %bool = OpTypeBool
3053 %true = OpConstantTrue %bool
3054 %int_0 = OpConstant %int 0
3055 %_ptr_Function_int = OpTypePointer Function %int
3056 )";
3057
3058 const std::string callee =
3059 R"(%foo_ = OpFunction %int None %8
3060 %13 = OpLabel
3061 %14 = OpCopyObject %int %int_0
3062 OpSelectionMerge %15 None
3063 OpBranchConditional %true %16 %15
3064 %15 = OpLabel
3065 %17 = OpPhi %int %14 %13 %18 %16
3066 OpReturnValue %17
3067 %16 = OpLabel
3068 %18 = OpCopyObject %int %int_0
3069 OpBranch %15
3070 OpFunctionEnd
3071 )";
3072
3073 const std::string calleeMergeReturn =
3074 R"(%uint = OpTypeInt 32 0
3075 %uint_0 = OpConstant %uint 0
3076 %false = OpConstantFalse %bool
3077 %_ptr_Function_bool = OpTypePointer Function %bool
3078 %foo_ = OpFunction %int None %8
3079 %13 = OpLabel
3080 %29 = OpVariable %_ptr_Function_bool Function %false
3081 %22 = OpVariable %_ptr_Function_int Function
3082 OpSelectionMerge %21 None
3083 OpSwitch %uint_0 %24
3084 %24 = OpLabel
3085 %14 = OpCopyObject %int %int_0
3086 OpSelectionMerge %15 None
3087 OpBranchConditional %true %16 %15
3088 %15 = OpLabel
3089 %17 = OpPhi %int %14 %24 %18 %16
3090 OpStore %29 %true
3091 OpStore %22 %17
3092 OpBranch %21
3093 %16 = OpLabel
3094 %18 = OpCopyObject %int %int_0
3095 OpBranch %15
3096 %21 = OpLabel
3097 %23 = OpLoad %int %22
3098 OpReturnValue %23
3099 OpFunctionEnd
3100 )";
3101
3102 const std::string before =
3103 R"(%main = OpFunction %void None %6
3104 %19 = OpLabel
3105 %x = OpVariable %_ptr_Function_int Function
3106 %20 = OpFunctionCall %int %foo_
3107 OpStore %x %20
3108 OpReturn
3109 OpFunctionEnd
3110 )";
3111
3112 const std::string after =
3113 R"(%main = OpFunction %void None %6
3114 %19 = OpLabel
3115 %30 = OpVariable %_ptr_Function_bool Function %false
3116 %31 = OpVariable %_ptr_Function_int Function
3117 %32 = OpVariable %_ptr_Function_int Function
3118 %x = OpVariable %_ptr_Function_int Function
3119 OpStore %30 %false
3120 OpSelectionMerge %40 None
3121 OpSwitch %uint_0 %34
3122 %34 = OpLabel
3123 %35 = OpCopyObject %int %int_0
3124 OpSelectionMerge %36 None
3125 OpBranchConditional %true %38 %36
3126 %36 = OpLabel
3127 %37 = OpPhi %int %35 %34 %39 %38
3128 OpStore %30 %true
3129 OpStore %31 %37
3130 OpBranch %40
3131 %38 = OpLabel
3132 %39 = OpCopyObject %int %int_0
3133 OpBranch %36
3134 %40 = OpLabel
3135 %41 = OpLoad %int %31
3136 OpStore %32 %41
3137 %20 = OpLoad %int %32
3138 OpStore %x %20
3139 OpReturn
3140 OpFunctionEnd
3141 )";
3142
3143 AddPass<MergeReturnPass>();
3144 AddPass<InlineExhaustivePass>();
3145 RunAndCheck(predefs + callee + before, predefs + calleeMergeReturn + after);
3146 }
3147
3148 TEST_F(InlineTest, DebugSimple) {
3149 // Check that it correctly generates DebugInlinedAt and maps it to DebugScope
3150 // for the inlined function foo().
3151 const std::string text = R"(
3152 ; CHECK: [[main_name:%\d+]] = OpString "main"
3153 ; CHECK: [[foo_name:%\d+]] = OpString "foo"
3154 ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[main_name]] {{%\d+}} {{%\d+}} 4 1 {{%\d+}} [[main_name]] FlagIsProtected|FlagIsPrivate 4 [[main:%\d+]]
3155 ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[foo_name]] {{%\d+}} {{%\d+}} 1 1 {{%\d+}} [[foo_name]] FlagIsProtected|FlagIsPrivate 1 [[foo:%\d+]]
3156 ; CHECK: [[foo_bb:%\d+]] = OpExtInst %void {{%\d+}} DebugLexicalBlock {{%\d+}} 1 14 [[dbg_foo]]
3157 ; CHECK: [[inlined_at:%\d+]] = OpExtInst %void {{%\d+}} DebugInlinedAt 4 [[dbg_main]]
3158 ; CHECK: [[main]] = OpFunction %void None
3159 ; CHECK: {{%\d+}} = OpExtInst %void {{%\d+}} DebugScope [[foo_bb]] [[inlined_at]]
3160 ; CHECK: [[foo]] = OpFunction %v4float None
3161 OpCapability Shader
3162 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
3163 OpMemoryModel Logical GLSL450
3164 OpEntryPoint Fragment %main "main" %3 %4
3165 OpExecutionMode %main OriginUpperLeft
3166 %5 = OpString "ps.hlsl"
3167 OpSource HLSL 600 %5
3168 %6 = OpString "float"
3169 %main_name = OpString "main"
3170 %foo_name = OpString "foo"
3171 OpDecorate %3 Location 0
3172 OpDecorate %4 Location 0
3173 %uint = OpTypeInt 32 0
3174 %uint_32 = OpConstant %uint 32
3175 %float = OpTypeFloat 32
3176 %float_1 = OpConstant %float 1
3177 %v4float = OpTypeVector %float 4
3178 %14 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3179 %_ptr_Input_v4float = OpTypePointer Input %v4float
3180 %_ptr_Output_v4float = OpTypePointer Output %v4float
3181 %void = OpTypeVoid
3182 %18 = OpTypeFunction %void
3183 %19 = OpTypeFunction %v4float
3184 %3 = OpVariable %_ptr_Input_v4float Input
3185 %4 = OpVariable %_ptr_Output_v4float Output
3186 %20 = OpExtInst %void %1 DebugSource %5
3187 %21 = OpExtInst %void %1 DebugCompilationUnit 1 4 %20 HLSL
3188 %22 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 Float
3189 %23 = OpExtInst %void %1 DebugTypeVector %22 4
3190 %24 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %23 %23
3191 %25 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %23
3192 %dbg_main = OpExtInst %void %1 DebugFunction %main_name %24 %20 4 1 %21 %main_name FlagIsProtected|FlagIsPrivate 4 %main
3193 %dbg_foo = OpExtInst %void %1 DebugFunction %foo_name %25 %20 1 1 %21 %foo_name FlagIsProtected|FlagIsPrivate 1 %foo
3194 %29 = OpExtInst %void %1 DebugLexicalBlock %20 1 14 %dbg_foo
3195 %main = OpFunction %void None %18
3196 %30 = OpLabel
3197 %31 = OpExtInst %void %1 DebugScope %dbg_main
3198 %32 = OpFunctionCall %v4float %foo
3199 %33 = OpLoad %v4float %3
3200 %34 = OpFAdd %v4float %32 %33
3201 OpStore %4 %34
3202 OpReturn
3203 OpFunctionEnd
3204 %foo = OpFunction %v4float None %19
3205 %35 = OpExtInst %void %1 DebugScope %dbg_foo
3206 %36 = OpLabel
3207 %37 = OpExtInst %void %1 DebugScope %29
3208 OpReturnValue %14
3209 OpFunctionEnd
3210 )";
3211
3212 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3213 }
3214
3215 TEST_F(InlineTest, DebugNested) {
3216 // When function main() calls function zoo() and function zoo() calls
3217 // function bar() and function bar() calls function foo(), check that
3218 // the inline pass correctly generates DebugInlinedAt instructions
3219 // for the nested function calls.
3220 const std::string text = R"(
3221 ; CHECK: [[v4f1:%\d+]] = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3222 ; CHECK: [[v4f2:%\d+]] = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3223 ; CHECK: [[v4f3:%\d+]] = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
3224 ; CHECK: [[color:%\d+]] = OpVariable %_ptr_Input_v4float Input
3225 ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 10 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 10 [[main:%\d+]]
3226 ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 1 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 1 [[foo:%\d+]]
3227 ; CHECK: [[dbg_bar:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 4 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 4 [[bar:%\d+]]
3228 ; CHECK: [[dbg_zoo:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 7 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 7 [[zoo:%\d+]]
3229 ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 600 [[dbg_main]]
3230 ; CHECK: [[inlined_to_zoo:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 700 [[dbg_zoo]] [[inlined_to_main]]
3231 ; CHECK: [[inlined_to_bar:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 300 [[dbg_bar]] [[inlined_to_zoo]]
3232 ; CHECK: [[main]] = OpFunction %void None
3233 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_foo]] [[inlined_to_bar]]
3234 ; CHECK-NEXT: OpLine {{%\d+}} 100 0
3235 ; CHECK-NEXT: OpStore {{%\d+}} [[v4f1]]
3236 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_bar]] [[inlined_to_zoo]]
3237 ; CHECK-NEXT: OpLine {{%\d+}} 300 0
3238 ; CHECK-NEXT: [[foo_ret:%\d+]] = OpLoad %v4float
3239 ; CHECK-NEXT: OpLine {{%\d+}} 400 0
3240 ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[foo_ret]] [[v4f2]]
3241 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_zoo]] [[inlined_to_main]]
3242 ; CHECK-NEXT: OpLine {{%\d+}} 700 0
3243 ; CHECK-NEXT: [[bar_ret:%\d+]] = OpLoad %v4float
3244 ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[bar_ret]] [[v4f3]]
3245 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
3246 ; CHECK-NEXT: OpLine {{%\d+}} 600 0
3247 ; CHECK-NEXT: [[zoo_ret:%\d+]] = OpLoad %v4float
3248 ; CHECK-NEXT: [[color_val:%\d+]] = OpLoad %v4float [[color]]
3249 ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[zoo_ret]] [[color_val]]
3250 ; CHECK: [[foo]] = OpFunction %v4float None
3251 ; CHECK: [[bar]] = OpFunction %v4float None
3252 ; CHECK: [[zoo]] = OpFunction %v4float None
3253 OpCapability Shader
3254 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
3255 OpMemoryModel Logical GLSL450
3256 OpEntryPoint Fragment %main "main" %3 %4
3257 OpExecutionMode %main OriginUpperLeft
3258 %5 = OpString "ps.hlsl"
3259 OpSource HLSL 600 %5
3260 %6 = OpString "float"
3261 %7 = OpString "main"
3262 %8 = OpString "foo"
3263 %9 = OpString "bar"
3264 %10 = OpString "zoo"
3265 OpDecorate %3 Location 0
3266 OpDecorate %4 Location 0
3267 %uint = OpTypeInt 32 0
3268 %uint_32 = OpConstant %uint 32
3269 %float = OpTypeFloat 32
3270 %float_1 = OpConstant %float 1
3271 %float_2 = OpConstant %float 2
3272 %float_3 = OpConstant %float 3
3273 %v4float = OpTypeVector %float 4
3274 %18 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3275 %19 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3276 %20 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
3277 %_ptr_Input_v4float = OpTypePointer Input %v4float
3278 %_ptr_Output_v4float = OpTypePointer Output %v4float
3279 %void = OpTypeVoid
3280 %24 = OpTypeFunction %void
3281 %25 = OpTypeFunction %v4float
3282 %3 = OpVariable %_ptr_Input_v4float Input
3283 %4 = OpVariable %_ptr_Output_v4float Output
3284 %26 = OpExtInst %void %1 DebugSource %5
3285 %27 = OpExtInst %void %1 DebugCompilationUnit 1 4 %26 HLSL
3286 %28 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 Float
3287 %29 = OpExtInst %void %1 DebugTypeVector %28 4
3288 %30 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %29 %29
3289 %31 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %29
3290 %32 = OpExtInst %void %1 DebugFunction %7 %30 %26 10 1 %27 %7 FlagIsProtected|FlagIsPrivate 10 %main
3291 %33 = OpExtInst %void %1 DebugFunction %8 %31 %26 1 1 %27 %8 FlagIsProtected|FlagIsPrivate 1 %foo
3292 %35 = OpExtInst %void %1 DebugFunction %9 %31 %26 4 1 %27 %9 FlagIsProtected|FlagIsPrivate 4 %bar
3293 %37 = OpExtInst %void %1 DebugFunction %10 %31 %26 7 1 %27 %10 FlagIsProtected|FlagIsPrivate 7 %zoo
3294 %main = OpFunction %void None %24
3295 %39 = OpLabel
3296 %40 = OpExtInst %void %1 DebugScope %32
3297 OpLine %5 600 0
3298 %41 = OpFunctionCall %v4float %zoo
3299 %42 = OpLoad %v4float %3
3300 %43 = OpFAdd %v4float %41 %42
3301 OpStore %4 %43
3302 OpReturn
3303 OpFunctionEnd
3304 %foo = OpFunction %v4float None %25
3305 %44 = OpExtInst %void %1 DebugScope %33
3306 %45 = OpLabel
3307 OpLine %5 100 0
3308 OpReturnValue %18
3309 OpFunctionEnd
3310 OpLine %5 200 0
3311 %bar = OpFunction %v4float None %25
3312 %46 = OpExtInst %void %1 DebugScope %35
3313 %47 = OpLabel
3314 OpLine %5 300 0
3315 %48 = OpFunctionCall %v4float %foo
3316 OpLine %5 400 0
3317 %49 = OpFAdd %v4float %48 %19
3318 OpLine %5 500 0
3319 OpReturnValue %49
3320 OpFunctionEnd
3321 %zoo = OpFunction %v4float None %25
3322 %50 = OpExtInst %void %1 DebugScope %37
3323 %51 = OpLabel
3324 OpLine %5 700 0
3325 %52 = OpFunctionCall %v4float %bar
3326 %53 = OpFAdd %v4float %52 %20
3327 OpReturnValue %53
3328 OpFunctionEnd
3329 )";
3330
3331 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3332 }
3333
3334 TEST_F(InlineTest, DebugSimpleHLSLPixelShader) {
3335 const std::string text = R"(
3336 ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 1 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 1 %src_main
3337 ; CHECK: [[lex_blk:%\d+]] = OpExtInst %void [[ext]] DebugLexicalBlock {{%\d+}} 1 47 [[dbg_main]]
3338 ; CHECK: %main = OpFunction %void None
3339 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
3340 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare {{%\d+}} %param_var_color
3341 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[lex_blk]]
3342 ; CHECK: OpLine {{%\d+}} 2 10
3343 ; CHECK: {{%\d+}} = OpLoad %v4float %param_var_color
3344 ; CHECK: OpLine {{%\d+}} 2 3
3345 ; CHECK: OpFunctionEnd
3346 ; CHECK: %src_main = OpFunction %v4float None
3347 OpCapability Shader
3348 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
3349 OpMemoryModel Logical GLSL450
3350 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
3351 OpExecutionMode %main OriginUpperLeft
3352 %5 = OpString "ps.hlsl"
3353 OpSource HLSL 600 %5
3354 %14 = OpString "#line 1 \"ps.hlsl\"
3355 float4 main(float4 color : COLOR) : SV_TARGET {
3356 return color;
3357 }
3358 "
3359 %17 = OpString "float"
3360 %21 = OpString "src.main"
3361 %24 = OpString "color"
3362 OpName %in_var_COLOR "in.var.COLOR"
3363 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
3364 OpName %main "main"
3365 OpName %param_var_color "param.var.color"
3366 OpName %src_main "src.main"
3367 OpName %color "color"
3368 OpName %bb_entry "bb.entry"
3369 OpDecorate %in_var_COLOR Location 0
3370 OpDecorate %out_var_SV_TARGET Location 0
3371 %uint = OpTypeInt 32 0
3372 %uint_32 = OpConstant %uint 32
3373 %float = OpTypeFloat 32
3374 %v4float = OpTypeVector %float 4
3375 %_ptr_Input_v4float = OpTypePointer Input %v4float
3376 %_ptr_Output_v4float = OpTypePointer Output %v4float
3377 %void = OpTypeVoid
3378 %27 = OpTypeFunction %void
3379 %_ptr_Function_v4float = OpTypePointer Function %v4float
3380 %33 = OpTypeFunction %v4float %_ptr_Function_v4float
3381 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
3382 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
3383 %13 = OpExtInst %void %1 DebugExpression
3384 %15 = OpExtInst %void %1 DebugSource %5 %14
3385 %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
3386 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
3387 %19 = OpExtInst %void %1 DebugTypeVector %18 4
3388 %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %19 %19
3389 %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %src_main
3390 %25 = OpExtInst %void %1 DebugLocalVariable %24 %19 %15 1 20 %22 FlagIsLocal 0
3391 %26 = OpExtInst %void %1 DebugLexicalBlock %15 1 47 %22
3392 %main = OpFunction %void None %27
3393 %28 = OpLabel
3394 %param_var_color = OpVariable %_ptr_Function_v4float Function
3395 %31 = OpLoad %v4float %in_var_COLOR
3396 OpStore %param_var_color %31
3397 %32 = OpFunctionCall %v4float %src_main %param_var_color
3398 OpStore %out_var_SV_TARGET %32
3399 OpReturn
3400 OpFunctionEnd
3401 OpLine %5 1 1
3402 %src_main = OpFunction %v4float None %33
3403 %34 = OpExtInst %void %1 DebugScope %22
3404 %color = OpFunctionParameter %_ptr_Function_v4float
3405 %36 = OpExtInst %void %1 DebugDeclare %25 %color %13
3406 %bb_entry = OpLabel
3407 %38 = OpExtInst %void %1 DebugScope %26
3408 OpLine %5 2 10
3409 %39 = OpLoad %v4float %color
3410 OpLine %5 2 3
3411 OpReturnValue %39
3412 OpFunctionEnd
3413 )";
3414
3415 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3416 }
3417
3418 TEST_F(InlineTest, DebugDeclareForCalleeFunctionParam) {
3419 // Check that InlinePass correctly generates DebugDeclare instructions
3420 // for callee function's parameters and maps them to corresponding
3421 // local variables of caller function.
3422 const std::string text = R"(
3423 ; CHECK: [[add:%\d+]] = OpString "add"
3424 ; CHECK: [[a:%\d+]] = OpString "a"
3425 ; CHECK: [[b:%\d+]] = OpString "b"
3426 ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
3427 ; CHECK: [[dbg_a:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[a]]
3428 ; CHECK: [[dbg_b:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[b]]
3429 ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
3430 ; CHECK: OpStore [[param_a:%\d+]]
3431 ; CHECK: OpStore [[param_b:%\d+]]
3432 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
3433 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_a]] [[param_a]]
3434 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_b]] [[param_b]]
3435
3436 OpCapability Shader
3437 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3438 OpMemoryModel Logical GLSL450
3439 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
3440 OpExecutionMode %main OriginUpperLeft
3441 %file_name = OpString "ps.hlsl"
3442 OpSource HLSL 600 %file_name
3443 %float_name = OpString "float"
3444 %main_name = OpString "main"
3445 %add_name = OpString "add"
3446 %a_name = OpString "a"
3447 %b_name = OpString "b"
3448 OpDecorate %in_var_COLOR Location 0
3449 OpDecorate %out_var_SV_TARGET Location 0
3450 %uint = OpTypeInt 32 0
3451 %uint_32 = OpConstant %uint 32
3452 %float = OpTypeFloat 32
3453 %float_1 = OpConstant %float 1
3454 %float_2 = OpConstant %float 2
3455 %v4float = OpTypeVector %float 4
3456 %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3457 %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3458 %_ptr_Input_v4float = OpTypePointer Input %v4float
3459 %_ptr_Output_v4float = OpTypePointer Output %v4float
3460 %_ptr_Function_v4float = OpTypePointer Function %v4float
3461 %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
3462 %void = OpTypeVoid
3463 %void_fn_type = OpTypeFunction %void
3464 %v4f_fn_type = OpTypeFunction %v4float
3465 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
3466 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
3467 %null_expr = OpExtInst %void %ext DebugExpression
3468 %src = OpExtInst %void %ext DebugSource %file_name
3469 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3470 %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3471 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
3472 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3473 %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
3474 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3475 %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
3476 %dbg_a = OpExtInst %void %ext DebugLocalVariable %a_name %dbg_v4f %src 1 13 %dbg_add FlagIsLocal 0
3477 %dbg_b = OpExtInst %void %ext DebugLocalVariable %b_name %dbg_v4f %src 1 20 %dbg_add FlagIsLocal 1
3478 %add_lb = OpExtInst %void %ext DebugLexicalBlock %src 1 23 %dbg_add
3479 %main = OpFunction %void None %void_fn_type
3480 %main_bb = OpLabel
3481 %param_a = OpVariable %_ptr_Function_v4float Function
3482 %param_b = OpVariable %_ptr_Function_v4float Function
3483 %scope0 = OpExtInst %void %ext DebugScope %dbg_main
3484 OpStore %param_a %v4f1
3485 OpStore %param_b %v4f2
3486 %result = OpFunctionCall %v4float %add %param_a %param_b
3487 OpStore %out_var_SV_TARGET %result
3488 OpReturn
3489 OpFunctionEnd
3490 %add = OpFunction %v4float None %add_fn_type
3491 %scope1 = OpExtInst %void %ext DebugScope %dbg_add
3492 %a = OpFunctionParameter %_ptr_Function_v4float
3493 %b = OpFunctionParameter %_ptr_Function_v4float
3494 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_a %a %null_expr
3495 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_b %b %null_expr
3496 %add_bb = OpLabel
3497 %scope2 = OpExtInst %void %ext DebugScope %add_lb
3498 %a_val = OpLoad %v4float %a
3499 %b_val = OpLoad %v4float %b
3500 %res = OpFAdd %v4float %a_val %b_val
3501 OpReturnValue %res
3502 OpFunctionEnd
3503 )";
3504
3505 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3506 }
3507
3508 TEST_F(InlineTest, DebugDeclareForCalleeLocalVar) {
3509 // Check that InlinePass correctly generates DebugDeclare instructions
3510 // for callee function's local variables and maps them to corresponding
3511 // local variables of caller function.
3512 const std::string text = R"(
3513 ; CHECK: [[add:%\d+]] = OpString "add"
3514 ; CHECK: [[foo:%\d+]] = OpString "foo"
3515 ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
3516 ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[foo]] {{%\d+}} {{%\d+}} 2 2 [[dbg_add]]
3517 ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
3518
3519 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
3520 ; CHECK: [[new_foo:%\d+]] = OpVariable %_ptr_Function_v4float Function
3521
3522 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
3523 ; CHECK: [[a_val:%\d+]] = OpLoad %v4float
3524 ; CHECK: [[b_val:%\d+]] = OpLoad %v4float
3525 ; CHECK: [[res:%\d+]] = OpFAdd %v4float [[a_val]] [[b_val]]
3526 ; CHECK: OpStore [[new_foo]] [[res]]
3527 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_foo]] [[new_foo]]
3528
3529 OpCapability Shader
3530 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3531 OpMemoryModel Logical GLSL450
3532 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
3533 OpExecutionMode %main OriginUpperLeft
3534 %file_name = OpString "ps.hlsl"
3535 OpSource HLSL 600 %file_name
3536 %float_name = OpString "float"
3537 %main_name = OpString "main"
3538 %add_name = OpString "add"
3539 %foo_name = OpString "foo"
3540 OpDecorate %in_var_COLOR Location 0
3541 OpDecorate %out_var_SV_TARGET Location 0
3542 %uint = OpTypeInt 32 0
3543 %uint_32 = OpConstant %uint 32
3544 %float = OpTypeFloat 32
3545 %float_1 = OpConstant %float 1
3546 %float_2 = OpConstant %float 2
3547 %v4float = OpTypeVector %float 4
3548 %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3549 %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3550 %_ptr_Input_v4float = OpTypePointer Input %v4float
3551 %_ptr_Output_v4float = OpTypePointer Output %v4float
3552 %_ptr_Function_v4float = OpTypePointer Function %v4float
3553 %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
3554 %void = OpTypeVoid
3555 %void_fn_type = OpTypeFunction %void
3556 %v4f_fn_type = OpTypeFunction %v4float
3557 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
3558 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
3559 %null_expr = OpExtInst %void %ext DebugExpression
3560 %src = OpExtInst %void %ext DebugSource %file_name
3561 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3562 %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3563 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
3564 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3565 %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
3566 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3567 %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
3568 %dbg_foo = OpExtInst %void %ext DebugLocalVariable %foo_name %dbg_v4f %src 2 2 %dbg_add FlagIsLocal
3569 %main = OpFunction %void None %void_fn_type
3570 %main_bb = OpLabel
3571 %param_a = OpVariable %_ptr_Function_v4float Function
3572 %param_b = OpVariable %_ptr_Function_v4float Function
3573 %scope0 = OpExtInst %void %ext DebugScope %dbg_main
3574 OpStore %param_a %v4f1
3575 OpStore %param_b %v4f2
3576 %result = OpFunctionCall %v4float %add %param_a %param_b
3577 OpStore %out_var_SV_TARGET %result
3578 OpReturn
3579 OpFunctionEnd
3580 %add = OpFunction %v4float None %add_fn_type
3581 %scope1 = OpExtInst %void %ext DebugScope %dbg_add
3582 %a = OpFunctionParameter %_ptr_Function_v4float
3583 %b = OpFunctionParameter %_ptr_Function_v4float
3584 %add_bb = OpLabel
3585 %foo = OpVariable %_ptr_Function_v4float Function
3586 %a_val = OpLoad %v4float %a
3587 %b_val = OpLoad %v4float %b
3588 %res = OpFAdd %v4float %a_val %b_val
3589 OpStore %foo %res
3590 %decl = OpExtInst %void %ext DebugDeclare %dbg_foo %foo %null_expr
3591 %foo_val = OpLoad %v4float %foo
3592 OpReturnValue %foo_val
3593 OpFunctionEnd
3594 )";
3595
3596 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3597 }
3598
3599 TEST_F(InlineTest, DebugDeclareMultiple) {
3600 // Check that InlinePass correctly generates DebugDeclare instructions
3601 // for callee function's parameters and maps them to corresponding
3602 // local variables of caller function.
3603 const std::string text = R"(
3604 ; CHECK: [[add:%\d+]] = OpString "add"
3605 ; CHECK: [[a:%\d+]] = OpString "a"
3606 ; CHECK: [[b:%\d+]] = OpString "b"
3607 ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
3608 ; CHECK: [[dbg_a:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[a]]
3609 ; CHECK: [[dbg_b:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[b]]
3610 ; CHECK: OpFunction
3611 ; CHECK-NOT: OpFunctionEnd
3612 ; CHECK: OpStore [[param_a:%\d+]]
3613 ; CHECK: OpStore [[param_b:%\d+]]
3614 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]]
3615 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_a]] [[param_a]]
3616 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_b]] [[param_b]]
3617 ; CHECK: [[a_val:%\d+]] = OpLoad %v4float [[param_a]]
3618 ; CHECK: OpStore [[foo:%\d+]] [[a_val]]
3619 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugValue [[dbg_a]] [[foo]]
3620
3621 OpCapability Shader
3622 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3623 OpMemoryModel Logical GLSL450
3624 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
3625 OpExecutionMode %main OriginUpperLeft
3626 %file_name = OpString "ps.hlsl"
3627 OpSource HLSL 600 %file_name
3628 %float_name = OpString "float"
3629 %main_name = OpString "main"
3630 %add_name = OpString "add"
3631 %a_name = OpString "a"
3632 %b_name = OpString "b"
3633 OpDecorate %in_var_COLOR Location 0
3634 OpDecorate %out_var_SV_TARGET Location 0
3635 %uint = OpTypeInt 32 0
3636 %uint_32 = OpConstant %uint 32
3637 %float = OpTypeFloat 32
3638 %float_1 = OpConstant %float 1
3639 %float_2 = OpConstant %float 2
3640 %v4float = OpTypeVector %float 4
3641 %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3642 %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3643 %_ptr_Input_v4float = OpTypePointer Input %v4float
3644 %_ptr_Output_v4float = OpTypePointer Output %v4float
3645 %_ptr_Function_v4float = OpTypePointer Function %v4float
3646 %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
3647 %void = OpTypeVoid
3648 %void_fn_type = OpTypeFunction %void
3649 %v4f_fn_type = OpTypeFunction %v4float
3650 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
3651 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
3652 %null_expr = OpExtInst %void %ext DebugExpression
3653 %src = OpExtInst %void %ext DebugSource %file_name
3654 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3655 %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3656 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
3657 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3658 %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
3659 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3660 %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
3661 %dbg_a = OpExtInst %void %ext DebugLocalVariable %a_name %dbg_v4f %src 1 13 %dbg_add FlagIsLocal 0
3662 %dbg_b = OpExtInst %void %ext DebugLocalVariable %b_name %dbg_v4f %src 1 20 %dbg_add FlagIsLocal 1
3663 %main = OpFunction %void None %void_fn_type
3664 %main_bb = OpLabel
3665 %param_a = OpVariable %_ptr_Function_v4float Function
3666 %param_b = OpVariable %_ptr_Function_v4float Function
3667 %scope0 = OpExtInst %void %ext DebugScope %dbg_main
3668 OpStore %param_a %v4f1
3669 OpStore %param_b %v4f2
3670 %result = OpFunctionCall %v4float %add %param_a %param_b
3671 OpStore %out_var_SV_TARGET %result
3672 OpReturn
3673 OpFunctionEnd
3674 %add = OpFunction %v4float None %add_fn_type
3675 %scope1 = OpExtInst %void %ext DebugScope %dbg_add
3676 %a = OpFunctionParameter %_ptr_Function_v4float
3677 %b = OpFunctionParameter %_ptr_Function_v4float
3678 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_a %a %null_expr
3679 %add_bb = OpLabel
3680 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_b %b %null_expr
3681 %foo = OpVariable %_ptr_Function_v4float Function
3682 %a_val = OpLoad %v4float %a
3683 OpStore %foo %a_val
3684 %dbg_val = OpExtInst %void %ext DebugValue %dbg_a %foo %null_expr
3685 %b_val = OpLoad %v4float %b
3686 %res = OpFAdd %v4float %a_val %b_val
3687 OpReturnValue %res
3688 OpFunctionEnd
3689 )";
3690
3691 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3692 }
3693
3694 TEST_F(InlineTest, DebugValueForFunctionCallReturn) {
3695 // Check that InlinePass correctly generates DebugValue instruction
3696 // for function call's return value and maps it to a corresponding
3697 // value in the caller function.
3698 const std::string text = R"(
3699 ; CHECK: [[main:%\d+]] = OpString "main"
3700 ; CHECK: [[add:%\d+]] = OpString "add"
3701 ; CHECK: [[result:%\d+]] = OpString "result"
3702 ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[main]]
3703 ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
3704 ; CHECK: [[dbg_result:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[result]] {{%\d+}} {{%\d+}} 6 2 [[dbg_main]]
3705 ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
3706 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
3707 ; CHECK: [[a_val:%\d+]] = OpLoad %v4float
3708 ; CHECK: [[b_val:%\d+]] = OpLoad %v4float
3709 ; CHECK: [[res:%\d+]] = OpFAdd %v4float [[a_val]] [[b_val]]
3710 ; CHECK: OpStore [[new_result:%\d+]] [[res]]
3711 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
3712 ; CHECK: [[result_val:%\d+]] = OpLoad %v4float [[new_result]]
3713 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugValue [[dbg_result]] [[result_val]]
3714
3715 OpCapability Shader
3716 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3717 OpMemoryModel Logical GLSL450
3718 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
3719 OpExecutionMode %main OriginUpperLeft
3720 %file_name = OpString "ps.hlsl"
3721 OpSource HLSL 600 %file_name
3722 %float_name = OpString "float"
3723 %main_name = OpString "main"
3724 %add_name = OpString "add"
3725 %result_name = OpString "result"
3726 OpDecorate %in_var_COLOR Location 0
3727 OpDecorate %out_var_SV_TARGET Location 0
3728 %uint = OpTypeInt 32 0
3729 %uint_32 = OpConstant %uint 32
3730 %float = OpTypeFloat 32
3731 %float_1 = OpConstant %float 1
3732 %float_2 = OpConstant %float 2
3733 %v4float = OpTypeVector %float 4
3734 %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3735 %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3736 %_ptr_Input_v4float = OpTypePointer Input %v4float
3737 %_ptr_Output_v4float = OpTypePointer Output %v4float
3738 %_ptr_Function_v4float = OpTypePointer Function %v4float
3739 %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
3740 %void = OpTypeVoid
3741 %void_fn_type = OpTypeFunction %void
3742 %v4f_fn_type = OpTypeFunction %v4float
3743 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
3744 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
3745 %null_expr = OpExtInst %void %ext DebugExpression
3746 %src = OpExtInst %void %ext DebugSource %file_name
3747 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3748 %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3749 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
3750 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3751 %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
3752 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3753 %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
3754 %dbg_result = OpExtInst %void %ext DebugLocalVariable %result_name %dbg_v4f %src 6 2 %dbg_main FlagIsLocal
3755 %main = OpFunction %void None %void_fn_type
3756 %main_bb = OpLabel
3757 %param_a = OpVariable %_ptr_Function_v4float Function
3758 %param_b = OpVariable %_ptr_Function_v4float Function
3759 %scope0 = OpExtInst %void %ext DebugScope %dbg_main
3760 OpStore %param_a %v4f1
3761 OpStore %param_b %v4f2
3762 %result = OpFunctionCall %v4float %add %param_a %param_b
3763 %value = OpExtInst %void %ext DebugValue %dbg_result %result %null_expr
3764 OpStore %out_var_SV_TARGET %result
3765 OpReturn
3766 OpFunctionEnd
3767 %add = OpFunction %v4float None %add_fn_type
3768 %scope1 = OpExtInst %void %ext DebugScope %dbg_add
3769 %a = OpFunctionParameter %_ptr_Function_v4float
3770 %b = OpFunctionParameter %_ptr_Function_v4float
3771 %add_bb = OpLabel
3772 %a_val = OpLoad %v4float %a
3773 %b_val = OpLoad %v4float %b
3774 %res = OpFAdd %v4float %a_val %b_val
3775 OpReturnValue %res
3776 OpFunctionEnd
3777 )";
3778
3779 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3780 }
3781
3782 TEST_F(InlineTest, NestedWithAnExistingDebugInlinedAt) {
3783 // When a DebugScope instruction in a callee function already has a
3784 // DebugInlinedAt information, we have to create a recursive
3785 // DebugInlinedAt chain. See inlined_to_zoo and inlined_to_bar in
3786 // the following code.
3787 const std::string text = R"(
3788 ; CHECK: [[main:%\d+]] = OpString "main"
3789 ; CHECK: [[foo:%\d+]] = OpString "foo"
3790 ; CHECK: [[bar:%\d+]] = OpString "bar"
3791 ; CHECK: [[zoo:%\d+]] = OpString "zoo"
3792 ; CHECK: [[v4f1:%\d+]] = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3793 ; CHECK: [[v4f2:%\d+]] = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3794 ; CHECK: [[v4f3:%\d+]] = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
3795 ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[main]]
3796 ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[foo]]
3797 ; CHECK: [[dbg_bar:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[bar]]
3798 ; CHECK: [[dbg_zoo:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[zoo]]
3799 ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 10 [[dbg_main]]
3800 ; CHECK: [[inlined_to_zoo:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 7 [[dbg_zoo]] [[inlined_to_main]]
3801 ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 10 [[dbg_main]]
3802 ; CHECK: [[inlined_to_bar:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 4 [[dbg_bar]] [[inlined_to_zoo]]
3803 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_foo]] [[inlined_to_bar]]
3804 ; CHECK: OpStore [[foo_ret:%\d+]] [[v4f1]]
3805 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_bar]] [[inlined_to_zoo]]
3806 ; CHECK: [[foo_ret_val:%\d+]] = OpLoad %v4float [[foo_ret]]
3807 ; CHECK: [[bar_ret:%\d+]] = OpFAdd %v4float [[foo_ret_val]] [[v4f2]]
3808 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_zoo]] [[inlined_to_main]]
3809 ; CHECK: [[zoo_result:%\d+]] = OpFAdd %v4float [[bar_ret]] [[v4f3]]
3810 ; CHECK: OpStore [[zoo_ret:%\d+]] [[zoo_result]]
3811 ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
3812 ; CHECK: [[zoo_ret_val:%\d+]] = OpLoad %v4float [[zoo_ret]]
3813 ; CHECK: {{%\d+}} = OpFAdd %v4float [[zoo_ret_val]] {{%\d+}}
3814
3815 OpCapability Shader
3816 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3817 OpMemoryModel Logical GLSL450
3818 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
3819 OpExecutionMode %main OriginUpperLeft
3820 %file_name = OpString "ps.hlsl"
3821 OpSource HLSL 600 %file_name
3822 %float_name = OpString "float"
3823 %main_name = OpString "main"
3824 %foo_name = OpString "foo"
3825 %bar_name = OpString "bar"
3826 %zoo_name = OpString "zoo"
3827 OpDecorate %in_var_COLOR Location 0
3828 OpDecorate %out_var_SV_TARGET Location 0
3829 %uint = OpTypeInt 32 0
3830 %uint_32 = OpConstant %uint 32
3831 %float = OpTypeFloat 32
3832 %float_1 = OpConstant %float 1
3833 %float_2 = OpConstant %float 2
3834 %float_3 = OpConstant %float 3
3835 %v4float = OpTypeVector %float 4
3836 %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3837 %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
3838 %v4f3 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
3839 %_ptr_Input_v4float = OpTypePointer Input %v4float
3840 %_ptr_Output_v4float = OpTypePointer Output %v4float
3841 %void = OpTypeVoid
3842 %void_fn_type = OpTypeFunction %void
3843 %v4f_fn_type = OpTypeFunction %v4float
3844 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
3845 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
3846 %src = OpExtInst %void %ext DebugSource %file_name
3847 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3848 %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3849 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
3850 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3851 %foo_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f
3852 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 10 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3853 %dbg_foo = OpExtInst %void %ext DebugFunction %foo_name %foo_ty %src 1 1 %cu %foo_name FlagIsProtected|FlagIsPrivate 1 %foo
3854 %dbg_bar = OpExtInst %void %ext DebugFunction %bar_name %foo_ty %src 4 1 %cu %bar_name FlagIsProtected|FlagIsPrivate 4 %bar
3855 %dbg_zoo = OpExtInst %void %ext DebugFunction %zoo_name %foo_ty %src 7 1 %cu %zoo_name FlagIsProtected|FlagIsPrivate 7 %zoo
3856 %inlined_to_zoo = OpExtInst %void %ext DebugInlinedAt 7 %dbg_zoo
3857 %main = OpFunction %void None %void_fn_type
3858 %main_bb = OpLabel
3859 %scope0 = OpExtInst %void %ext DebugScope %dbg_main
3860 %zoo_val = OpFunctionCall %v4float %zoo
3861 %color = OpLoad %v4float %in_var_COLOR
3862 %result = OpFAdd %v4float %zoo_val %color
3863 OpStore %out_var_SV_TARGET %result
3864 OpReturn
3865 OpFunctionEnd
3866 %foo = OpFunction %v4float None %v4f_fn_type
3867 %scope1 = OpExtInst %void %ext DebugScope %dbg_foo
3868 %foo_bb = OpLabel
3869 OpReturnValue %v4f1
3870 OpFunctionEnd
3871 %zoo = OpFunction %v4float None %v4f_fn_type
3872 %scope3 = OpExtInst %void %ext DebugScope %dbg_zoo
3873 %zoo_bb = OpLabel
3874 %scope2 = OpExtInst %void %ext DebugScope %dbg_bar %inlined_to_zoo
3875 %foo_val = OpFunctionCall %v4float %foo
3876 %bar_val = OpFAdd %v4float %foo_val %v4f2
3877 %scope4 = OpExtInst %void %ext DebugScope %dbg_zoo
3878 %zoo_ret = OpFAdd %v4float %bar_val %v4f3
3879 OpReturnValue %zoo_ret
3880 OpFunctionEnd
3881 %bar = OpFunction %v4float None %v4f_fn_type
3882 %scope5 = OpExtInst %void %ext DebugScope %dbg_bar
3883 %bar_bb = OpLabel
3884 %foo_val0 = OpFunctionCall %v4float %foo
3885 %bar_ret = OpFAdd %v4float %foo_val0 %v4f2
3886 OpReturnValue %bar_ret
3887 OpFunctionEnd
3888 )";
3889
3890 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3891 }
3892
3893 TEST_F(InlineTest, UsingVoidFunctionResult) {
3894 const std::string text = R"(
3895 ; CHECK: [[undef:%\w+]] = OpUndef %void
3896 ; CHECK: OpFunction
3897 ; CHECK: OpCopyObject %void [[undef]]
3898 ; CHECK: OpFunctionEnd
3899 OpCapability Shader
3900 %1 = OpExtInstImport "GLSL.std.450"
3901 OpMemoryModel Logical GLSL450
3902 OpEntryPoint Fragment %4 "main"
3903 OpExecutionMode %4 OriginUpperLeft
3904 OpSource ESSL 320
3905 %2 = OpTypeVoid
3906 %3 = OpTypeFunction %2
3907 %4 = OpFunction %2 None %3
3908 %5 = OpLabel
3909 %8 = OpFunctionCall %2 %6
3910 %9 = OpCopyObject %2 %8
3911 OpReturn
3912 OpFunctionEnd
3913 %6 = OpFunction %2 None %3
3914 %7 = OpLabel
3915 OpReturn
3916 OpFunctionEnd
3917 )";
3918
3919 SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
3920 }
3921
3922 // TODO(greg-lunarg): Add tests to verify handling of these cases:
3923 //
3924 // Empty modules
3925 // Modules without function definitions
3926 // Modules in which all functions do not call other functions
3927 // Caller and callee both accessing the same global variable
3928 // Functions with OpLine & OpNoLine
3929 // Others?
3930
3931 // TODO(dneto): Test suggestions from code review
3932 // https://github.com/KhronosGroup/SPIRV-Tools/pull/534
3933 //
3934 // Callee function returns a value generated outside the callee,
3935 // e.g. a constant value. This might exercise some logic not yet
3936 // exercised by the current tests: the false branch in the "if"
3937 // inside the SpvOpReturnValue case in InlinePass::GenInlineCode?
3938 // SampledImage before function call, but callee is only single block.
3939 // Then the SampledImage instruction is not cloned. Documents existing
3940 // behaviour.
3941 // SampledImage after function call. It is not cloned or changed.
3942
3943 } // namespace
3944 } // namespace opt
3945 } // namespace spvtools