Debian Codegraph @

test/link/matching_imports_to_exports_test.cpp

raw
1 // Copyright (c) 2017 Pierre Moreau
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string>
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "test/link/linker_fixture.h"
20
21 namespace spvtools {
22 namespace {
23
24 using ::testing::HasSubstr;
25 using MatchingImportsToExports = spvtest::LinkerTest;
26
27 TEST_F(MatchingImportsToExports, Default) {
28 const std::string body1 = R"(
29 OpCapability Linkage
30 OpCapability Addresses
31 OpCapability Kernel
32 OpMemoryModel Physical64 OpenCL
33 OpDecorate %1 LinkageAttributes "foo" Import
34 %2 = OpTypeFloat 32
35 %1 = OpVariable %2 Uniform
36 %3 = OpVariable %2 Input
37 )";
38 const std::string body2 = R"(
39 OpCapability Linkage
40 OpCapability Addresses
41 OpCapability Kernel
42 OpMemoryModel Physical64 OpenCL
43 OpDecorate %1 LinkageAttributes "foo" Export
44 %2 = OpTypeFloat 32
45 %3 = OpConstant %2 42
46 %1 = OpVariable %2 Uniform %3
47 )";
48
49 spvtest::Binary linked_binary;
50 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
51 << GetErrorMessage();
52
53 const std::string expected_res =
54 R"(OpCapability Addresses
55 OpCapability Kernel
56 OpMemoryModel Physical64 OpenCL
57 OpModuleProcessed "Linked by SPIR-V Tools Linker"
58 %1 = OpTypeFloat 32
59 %2 = OpVariable %1 Input
60 %3 = OpConstant %1 42
61 %4 = OpVariable %1 Uniform %3
62 )";
63 std::string res_body;
64 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
65 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
66 << GetErrorMessage();
67 EXPECT_EQ(expected_res, res_body);
68 }
69
70 TEST_F(MatchingImportsToExports, NotALibraryExtraExports) {
71 const std::string body = R"(
72 OpCapability Linkage
73 OpCapability Addresses
74 OpCapability Kernel
75 OpMemoryModel Physical64 OpenCL
76 OpDecorate %1 LinkageAttributes "foo" Export
77 %2 = OpTypeFloat 32
78 %1 = OpVariable %2 Uniform
79 )";
80
81 spvtest::Binary linked_binary;
82 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body}, &linked_binary))
83 << GetErrorMessage();
84
85 const std::string expected_res =
86 R"(OpCapability Addresses
87 OpCapability Kernel
88 OpMemoryModel Physical64 OpenCL
89 OpModuleProcessed "Linked by SPIR-V Tools Linker"
90 %1 = OpTypeFloat 32
91 %2 = OpVariable %1 Uniform
92 )";
93 std::string res_body;
94 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
95 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
96 << GetErrorMessage();
97 EXPECT_EQ(expected_res, res_body);
98 }
99
100 TEST_F(MatchingImportsToExports, LibraryExtraExports) {
101 const std::string body = R"(
102 OpCapability Linkage
103 OpCapability Addresses
104 OpCapability Kernel
105 OpMemoryModel Physical64 OpenCL
106 OpDecorate %1 LinkageAttributes "foo" Export
107 %2 = OpTypeFloat 32
108 %1 = OpVariable %2 Uniform
109 )";
110
111 spvtest::Binary linked_binary;
112 LinkerOptions options;
113 options.SetCreateLibrary(true);
114 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body}, &linked_binary, options))
115 << GetErrorMessage();
116
117 const std::string expected_res = R"(OpCapability Linkage
118 OpCapability Addresses
119 OpCapability Kernel
120 OpMemoryModel Physical64 OpenCL
121 OpModuleProcessed "Linked by SPIR-V Tools Linker"
122 OpDecorate %1 LinkageAttributes "foo" Export
123 %2 = OpTypeFloat 32
124 %1 = OpVariable %2 Uniform
125 )";
126 std::string res_body;
127 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
128 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
129 << GetErrorMessage();
130 EXPECT_EQ(expected_res, res_body);
131 }
132
133 TEST_F(MatchingImportsToExports, UnresolvedImports) {
134 const std::string body1 = R"(
135 OpCapability Linkage
136 OpCapability Addresses
137 OpCapability Kernel
138 OpMemoryModel Physical64 OpenCL
139 OpDecorate %1 LinkageAttributes "foo" Import
140 %2 = OpTypeFloat 32
141 %1 = OpVariable %2 Uniform
142 )";
143 const std::string body2 = R"(
144 OpCapability Addresses
145 OpCapability Kernel
146 OpMemoryModel Physical64 OpenCL
147 )";
148
149 spvtest::Binary linked_binary;
150 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
151 AssembleAndLink({body1, body2}, &linked_binary));
152 EXPECT_THAT(GetErrorMessage(),
153 HasSubstr("Unresolved external reference to \"foo\"."));
154 }
155
156 TEST_F(MatchingImportsToExports, TypeMismatch) {
157 const std::string body1 = R"(
158 OpCapability Linkage
159 OpCapability Addresses
160 OpCapability Kernel
161 OpMemoryModel Physical64 OpenCL
162 OpDecorate %1 LinkageAttributes "foo" Import
163 %2 = OpTypeFloat 32
164 %1 = OpVariable %2 Uniform
165 %3 = OpVariable %2 Input
166 )";
167 const std::string body2 = R"(
168 OpCapability Linkage
169 OpCapability Addresses
170 OpCapability Kernel
171 OpMemoryModel Physical64 OpenCL
172 OpDecorate %1 LinkageAttributes "foo" Export
173 %2 = OpTypeInt 32 0
174 %3 = OpConstant %2 42
175 %1 = OpVariable %2 Uniform %3
176 )";
177
178 LinkerOptions options;
179 for (int i = 0; i < 2; i++) {
180 spvtest::Binary linked_binary;
181 options.SetAllowPtrTypeMismatch(i == 1);
182 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
183 AssembleAndLink({body1, body2}, &linked_binary))
184 << GetErrorMessage();
185 EXPECT_THAT(
186 GetErrorMessage(),
187 HasSubstr("Type mismatch on symbol \"foo\" between imported "
188 "variable/function %1 and exported variable/function %4"));
189 }
190 }
191
192 TEST_F(MatchingImportsToExports, MultipleDefinitions) {
193 const std::string body1 = R"(
194 OpCapability Linkage
195 OpCapability Addresses
196 OpCapability Kernel
197 OpMemoryModel Physical64 OpenCL
198 OpDecorate %1 LinkageAttributes "foo" Import
199 %2 = OpTypeFloat 32
200 %1 = OpVariable %2 Uniform
201 %3 = OpVariable %2 Input
202 )";
203 const std::string body2 = R"(
204 OpCapability Linkage
205 OpCapability Addresses
206 OpCapability Kernel
207 OpMemoryModel Physical64 OpenCL
208 OpDecorate %1 LinkageAttributes "foo" Export
209 %2 = OpTypeFloat 32
210 %3 = OpConstant %2 42
211 %1 = OpVariable %2 Uniform %3
212 )";
213 const std::string body3 = R"(
214 OpCapability Linkage
215 OpCapability Addresses
216 OpCapability Kernel
217 OpMemoryModel Physical64 OpenCL
218 OpDecorate %1 LinkageAttributes "foo" Export
219 %2 = OpTypeFloat 32
220 %3 = OpConstant %2 -1
221 %1 = OpVariable %2 Uniform %3
222 )";
223
224 LinkerOptions options;
225 for (int i = 0; i < 2; i++) {
226 spvtest::Binary linked_binary;
227 options.SetAllowPtrTypeMismatch(i == 1);
228 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
229 AssembleAndLink({body1, body2, body3}, &linked_binary))
230 << GetErrorMessage();
231 EXPECT_THAT(GetErrorMessage(),
232 HasSubstr("Too many external references, 2, were found "
233 "for \"foo\"."));
234 }
235 }
236
237 TEST_F(MatchingImportsToExports, SameNameDifferentTypes) {
238 const std::string body1 = R"(
239 OpCapability Linkage
240 OpCapability Addresses
241 OpCapability Kernel
242 OpMemoryModel Physical64 OpenCL
243 OpDecorate %1 LinkageAttributes "foo" Import
244 %2 = OpTypeFloat 32
245 %1 = OpVariable %2 Uniform
246 %3 = OpVariable %2 Input
247 )";
248 const std::string body2 = R"(
249 OpCapability Linkage
250 OpCapability Addresses
251 OpCapability Kernel
252 OpMemoryModel Physical64 OpenCL
253 OpDecorate %1 LinkageAttributes "foo" Export
254 %2 = OpTypeInt 32 0
255 %3 = OpConstant %2 42
256 %1 = OpVariable %2 Uniform %3
257 )";
258 const std::string body3 = R"(
259 OpCapability Linkage
260 OpCapability Addresses
261 OpCapability Kernel
262 OpMemoryModel Physical64 OpenCL
263 OpDecorate %1 LinkageAttributes "foo" Export
264 %2 = OpTypeFloat 32
265 %3 = OpConstant %2 12
266 %1 = OpVariable %2 Uniform %3
267 )";
268
269 spvtest::Binary linked_binary;
270 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
271 AssembleAndLink({body1, body2, body3}, &linked_binary))
272 << GetErrorMessage();
273 EXPECT_THAT(GetErrorMessage(),
274 HasSubstr("Too many external references, 2, were found "
275 "for \"foo\"."));
276 }
277
278 TEST_F(MatchingImportsToExports, DecorationMismatch) {
279 const std::string body1 = R"(
280 OpCapability Linkage
281 OpCapability Addresses
282 OpCapability Kernel
283 OpMemoryModel Physical64 OpenCL
284 OpDecorate %1 LinkageAttributes "foo" Import
285 OpDecorate %2 Constant
286 %2 = OpTypeFloat 32
287 %1 = OpVariable %2 Uniform
288 %3 = OpVariable %2 Input
289 )";
290 const std::string body2 = R"(
291 OpCapability Linkage
292 OpCapability Addresses
293 OpCapability Kernel
294 OpMemoryModel Physical64 OpenCL
295 OpDecorate %1 LinkageAttributes "foo" Export
296 %2 = OpTypeFloat 32
297 %3 = OpConstant %2 42
298 %1 = OpVariable %2 Uniform %3
299 )";
300
301 LinkerOptions options;
302 for (int i = 0; i < 2; i++) {
303 spvtest::Binary linked_binary;
304 options.SetAllowPtrTypeMismatch(i == 1);
305 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
306 AssembleAndLink({body1, body2}, &linked_binary))
307 << GetErrorMessage();
308 EXPECT_THAT(
309 GetErrorMessage(),
310 HasSubstr("Type mismatch on symbol \"foo\" between imported "
311 "variable/function %1 and exported variable/function %4"));
312 }
313 }
314
315 TEST_F(MatchingImportsToExports,
316 FuncParamAttrDifferButStillMatchExportToImport) {
317 const std::string body1 = R"(
318 OpCapability Linkage
319 OpCapability Addresses
320 OpCapability Kernel
321 OpMemoryModel Physical64 OpenCL
322 OpDecorate %1 LinkageAttributes "foo" Import
323 OpDecorate %2 FuncParamAttr Zext
324 %3 = OpTypeVoid
325 %4 = OpTypeInt 32 0
326 %5 = OpTypeFunction %3 %4
327 %1 = OpFunction %3 None %5
328 %2 = OpFunctionParameter %4
329 OpFunctionEnd
330 )";
331 const std::string body2 = R"(
332 OpCapability Linkage
333 OpCapability Addresses
334 OpCapability Kernel
335 OpMemoryModel Physical64 OpenCL
336 OpDecorate %1 LinkageAttributes "foo" Export
337 OpDecorate %2 FuncParamAttr Sext
338 %3 = OpTypeVoid
339 %4 = OpTypeInt 32 0
340 %5 = OpTypeFunction %3 %4
341 %1 = OpFunction %3 None %5
342 %2 = OpFunctionParameter %4
343 %6 = OpLabel
344 OpReturn
345 OpFunctionEnd
346 )";
347
348 spvtest::Binary linked_binary;
349 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
350 << GetErrorMessage();
351
352 const std::string expected_res = R"(OpCapability Addresses
353 OpCapability Kernel
354 OpMemoryModel Physical64 OpenCL
355 OpModuleProcessed "Linked by SPIR-V Tools Linker"
356 OpDecorate %1 FuncParamAttr Sext
357 %2 = OpTypeVoid
358 %3 = OpTypeInt 32 0
359 %4 = OpTypeFunction %2 %3
360 %5 = OpFunction %2 None %4
361 %1 = OpFunctionParameter %3
362 %6 = OpLabel
363 OpReturn
364 OpFunctionEnd
365 )";
366 std::string res_body;
367 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
368 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
369 << GetErrorMessage();
370 EXPECT_EQ(expected_res, res_body);
371 }
372
373 TEST_F(MatchingImportsToExports, FunctionCtrl) {
374 const std::string body1 = R"(
375 OpCapability Linkage
376 OpCapability Addresses
377 OpCapability Kernel
378 OpMemoryModel Physical64 OpenCL
379 OpDecorate %1 LinkageAttributes "foo" Import
380 %2 = OpTypeVoid
381 %3 = OpTypeFunction %2
382 %4 = OpTypeFloat 32
383 %5 = OpVariable %4 Uniform
384 %1 = OpFunction %2 None %3
385 OpFunctionEnd
386 )";
387 const std::string body2 = R"(
388 OpCapability Linkage
389 OpCapability Addresses
390 OpCapability Kernel
391 OpMemoryModel Physical64 OpenCL
392 OpDecorate %1 LinkageAttributes "foo" Export
393 %2 = OpTypeVoid
394 %3 = OpTypeFunction %2
395 %1 = OpFunction %2 Inline %3
396 %4 = OpLabel
397 OpReturn
398 OpFunctionEnd
399 )";
400
401 spvtest::Binary linked_binary;
402 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
403 << GetErrorMessage();
404
405 const std::string expected_res =
406 R"(OpCapability Addresses
407 OpCapability Kernel
408 OpMemoryModel Physical64 OpenCL
409 OpModuleProcessed "Linked by SPIR-V Tools Linker"
410 %1 = OpTypeVoid
411 %2 = OpTypeFunction %1
412 %3 = OpTypeFloat 32
413 %4 = OpVariable %3 Uniform
414 %5 = OpFunction %1 Inline %2
415 %6 = OpLabel
416 OpReturn
417 OpFunctionEnd
418 )";
419 std::string res_body;
420 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
421 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
422 << GetErrorMessage();
423 EXPECT_EQ(expected_res, res_body);
424 }
425
426 TEST_F(MatchingImportsToExports, UseExportedFuncParamAttr) {
427 const std::string body1 = R"(
428 OpCapability Linkage
429 OpCapability Addresses
430 OpCapability Kernel
431 OpMemoryModel Physical64 OpenCL
432 OpDecorate %1 LinkageAttributes "foo" Import
433 OpDecorate %2 FuncParamAttr Zext
434 %2 = OpDecorationGroup
435 OpGroupDecorate %2 %3 %4
436 %5 = OpTypeVoid
437 %6 = OpTypeInt 32 0
438 %7 = OpTypeFunction %5 %6
439 %1 = OpFunction %5 None %7
440 %3 = OpFunctionParameter %6
441 OpFunctionEnd
442 %8 = OpFunction %5 None %7
443 %4 = OpFunctionParameter %6
444 OpFunctionEnd
445 )";
446 const std::string body2 = R"(
447 OpCapability Linkage
448 OpCapability Addresses
449 OpCapability Kernel
450 OpMemoryModel Physical64 OpenCL
451 OpDecorate %1 LinkageAttributes "foo" Export
452 OpDecorate %2 FuncParamAttr Sext
453 %3 = OpTypeVoid
454 %4 = OpTypeInt 32 0
455 %5 = OpTypeFunction %3 %4
456 %1 = OpFunction %3 None %5
457 %2 = OpFunctionParameter %4
458 %6 = OpLabel
459 OpReturn
460 OpFunctionEnd
461 )";
462
463 spvtest::Binary linked_binary;
464 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
465 << GetErrorMessage();
466
467 const std::string expected_res = R"(OpCapability Addresses
468 OpCapability Kernel
469 OpMemoryModel Physical64 OpenCL
470 OpModuleProcessed "Linked by SPIR-V Tools Linker"
471 OpDecorate %1 FuncParamAttr Zext
472 %1 = OpDecorationGroup
473 OpGroupDecorate %1 %2
474 OpDecorate %3 FuncParamAttr Sext
475 %4 = OpTypeVoid
476 %5 = OpTypeInt 32 0
477 %6 = OpTypeFunction %4 %5
478 %7 = OpFunction %4 None %6
479 %2 = OpFunctionParameter %5
480 OpFunctionEnd
481 %8 = OpFunction %4 None %6
482 %3 = OpFunctionParameter %5
483 %9 = OpLabel
484 OpReturn
485 OpFunctionEnd
486 )";
487 std::string res_body;
488 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
489 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
490 << GetErrorMessage();
491 EXPECT_EQ(expected_res, res_body);
492 }
493
494 TEST_F(MatchingImportsToExports, NamesAndDecorations) {
495 const std::string body1 = R"(
496 OpCapability Linkage
497 OpCapability Addresses
498 OpCapability Kernel
499 OpMemoryModel Physical64 OpenCL
500 OpName %1 "foo"
501 OpName %3 "param"
502 OpDecorate %1 LinkageAttributes "foo" Import
503 OpDecorate %2 Restrict
504 OpDecorate %4 NonWritable
505 %2 = OpDecorationGroup
506 OpGroupDecorate %2 %3 %4
507 %5 = OpTypeVoid
508 %6 = OpTypeInt 32 0
509 %9 = OpTypePointer Function %6
510 %7 = OpTypeFunction %5 %9
511 %1 = OpFunction %5 None %7
512 %3 = OpFunctionParameter %9
513 OpFunctionEnd
514 %8 = OpFunction %5 None %7
515 %4 = OpFunctionParameter %9
516 OpFunctionEnd
517 )";
518 const std::string body2 = R"(
519 OpCapability Linkage
520 OpCapability Addresses
521 OpCapability Kernel
522 OpMemoryModel Physical64 OpenCL
523 OpName %1 "foo"
524 OpName %2 "param"
525 OpDecorate %1 LinkageAttributes "foo" Export
526 OpDecorate %2 Restrict
527 %3 = OpTypeVoid
528 %4 = OpTypeInt 32 0
529 %7 = OpTypePointer Function %4
530 %5 = OpTypeFunction %3 %7
531 %1 = OpFunction %3 None %5
532 %2 = OpFunctionParameter %7
533 %6 = OpLabel
534 OpReturn
535 OpFunctionEnd
536 )";
537
538 spvtest::Binary linked_binary;
539 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
540 << GetErrorMessage();
541
542 const std::string expected_res = R"(OpCapability Addresses
543 OpCapability Kernel
544 OpMemoryModel Physical64 OpenCL
545 OpName %1 "foo"
546 OpName %2 "param"
547 OpModuleProcessed "Linked by SPIR-V Tools Linker"
548 OpDecorate %3 Restrict
549 OpDecorate %4 NonWritable
550 %3 = OpDecorationGroup
551 OpGroupDecorate %3 %4
552 OpDecorate %2 Restrict
553 %5 = OpTypeVoid
554 %6 = OpTypeInt 32 0
555 %7 = OpTypePointer Function %6
556 %8 = OpTypeFunction %5 %7
557 %9 = OpFunction %5 None %8
558 %4 = OpFunctionParameter %7
559 OpFunctionEnd
560 %1 = OpFunction %5 None %8
561 %2 = OpFunctionParameter %7
562 %10 = OpLabel
563 OpReturn
564 OpFunctionEnd
565 )";
566 std::string res_body;
567 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
568 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
569 << GetErrorMessage();
570 EXPECT_EQ(expected_res, res_body);
571 }
572
573 TEST_F(MatchingImportsToExports, FunctionCall) {
574 const std::string body1 = R"(
575 OpCapability Linkage
576 OpCapability Addresses
577 OpCapability Kernel
578 OpMemoryModel Physical64 OpenCL
579 OpName %1 "foo"
580 OpName %3 "param"
581 OpDecorate %1 LinkageAttributes "foo" Import
582 %5 = OpTypeVoid
583 %6 = OpTypeInt 32 0
584 %9 = OpTypePointer Function %6
585 %7 = OpTypeFunction %5 %9
586 %1 = OpFunction %5 None %7
587 %3 = OpFunctionParameter %9
588 OpFunctionEnd
589 %8 = OpFunction %5 None %7
590 %4 = OpFunctionParameter %9
591 %10 = OpLabel
592 %11 = OpFunctionCall %5 %1 %4
593 OpReturn
594 OpFunctionEnd
595 )";
596 const std::string body2 = R"(
597 OpCapability Linkage
598 OpCapability Addresses
599 OpCapability Kernel
600 OpMemoryModel Physical64 OpenCL
601 OpName %1 "foo"
602 OpName %2 "param"
603 OpDecorate %1 LinkageAttributes "foo" Export
604 %3 = OpTypeVoid
605 %4 = OpTypeInt 32 0
606 %7 = OpTypePointer Function %4
607 %5 = OpTypeFunction %3 %7
608 %1 = OpFunction %3 None %5
609 %2 = OpFunctionParameter %7
610 %6 = OpLabel
611 OpReturn
612 OpFunctionEnd
613 )";
614
615 LinkerOptions options;
616 for (int i = 0; i < 2; i++) {
617 spvtest::Binary linked_binary;
618 options.SetAllowPtrTypeMismatch(i == 1);
619 ASSERT_EQ(SPV_SUCCESS,
620 AssembleAndLink({body1, body2}, &linked_binary, options))
621 << GetErrorMessage();
622
623 const std::string expected_res = R"(OpCapability Addresses
624 OpCapability Kernel
625 OpMemoryModel Physical64 OpenCL
626 OpName %1 "foo"
627 OpName %2 "param"
628 OpModuleProcessed "Linked by SPIR-V Tools Linker"
629 %3 = OpTypeVoid
630 %4 = OpTypeInt 32 0
631 %5 = OpTypePointer Function %4
632 %6 = OpTypeFunction %3 %5
633 %7 = OpFunction %3 None %6
634 %8 = OpFunctionParameter %5
635 %9 = OpLabel
636 %10 = OpFunctionCall %3 %1 %8
637 OpReturn
638 OpFunctionEnd
639 %1 = OpFunction %3 None %6
640 %2 = OpFunctionParameter %5
641 %11 = OpLabel
642 OpReturn
643 OpFunctionEnd
644 )";
645
646 std::string res_body;
647 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
648 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
649 << GetErrorMessage();
650 EXPECT_EQ(expected_res, res_body);
651 }
652 }
653
654 TEST_F(MatchingImportsToExports, FunctionSignatureMismatchPointer) {
655 const std::string body1 = R"(
656 OpCapability Linkage
657 OpCapability Addresses
658 OpCapability Kernel
659 OpMemoryModel Physical64 OpenCL
660 OpName %1 "foo"
661 OpName %3 "param"
662 OpDecorate %1 LinkageAttributes "foo" Import
663 %5 = OpTypeVoid
664 %6 = OpTypeInt 8 0
665 %9 = OpTypePointer Function %6
666 %7 = OpTypeFunction %5 %9
667 %1 = OpFunction %5 None %7
668 %3 = OpFunctionParameter %9
669 OpFunctionEnd
670 %8 = OpFunction %5 None %7
671 %4 = OpFunctionParameter %9
672 %10 = OpLabel
673 %11 = OpFunctionCall %5 %1 %4
674 OpReturn
675 OpFunctionEnd
676 )";
677 const std::string body2 = R"(
678 OpCapability Linkage
679 OpCapability Addresses
680 OpCapability Kernel
681 OpMemoryModel Physical64 OpenCL
682 OpName %1 "foo"
683 OpName %2 "param"
684 OpDecorate %1 LinkageAttributes "foo" Export
685 %3 = OpTypeVoid
686 %4 = OpTypeInt 32 0
687 %7 = OpTypePointer Function %4
688 %5 = OpTypeFunction %3 %7
689 %1 = OpFunction %3 None %5
690 %2 = OpFunctionParameter %7
691 %6 = OpLabel
692 OpReturn
693 OpFunctionEnd
694 )";
695
696 spvtest::Binary linked_binary;
697 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
698 AssembleAndLink({body1, body2}, &linked_binary))
699 << GetErrorMessage();
700 EXPECT_THAT(
701 GetErrorMessage(),
702 HasSubstr("Type mismatch on symbol \"foo\" between imported "
703 "variable/function %1 and exported variable/function %11"));
704
705 LinkerOptions options;
706 options.SetAllowPtrTypeMismatch(true);
707 ASSERT_EQ(SPV_SUCCESS,
708 AssembleAndLink({body1, body2}, &linked_binary, options))
709 << GetErrorMessage();
710
711 const std::string expected_res = R"(OpCapability Addresses
712 OpCapability Kernel
713 OpMemoryModel Physical64 OpenCL
714 OpName %1 "foo"
715 OpName %2 "param"
716 OpModuleProcessed "Linked by SPIR-V Tools Linker"
717 %3 = OpTypeVoid
718 %4 = OpTypeInt 8 0
719 %5 = OpTypePointer Function %4
720 %6 = OpTypeFunction %3 %5
721 %7 = OpTypeInt 32 0
722 %8 = OpTypePointer Function %7
723 %9 = OpTypeFunction %3 %8
724 %10 = OpFunction %3 None %6
725 %11 = OpFunctionParameter %5
726 %12 = OpLabel
727 %13 = OpBitcast %8 %11
728 %14 = OpFunctionCall %3 %1 %13
729 OpReturn
730 OpFunctionEnd
731 %1 = OpFunction %3 None %9
732 %2 = OpFunctionParameter %8
733 %15 = OpLabel
734 OpReturn
735 OpFunctionEnd
736 )";
737 std::string res_body;
738 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
739 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
740 << GetErrorMessage();
741 EXPECT_EQ(expected_res, res_body);
742 }
743
744 TEST_F(MatchingImportsToExports, FunctionSignatureMismatchValue) {
745 const std::string body1 = R"(
746 OpCapability Linkage
747 OpCapability Addresses
748 OpCapability Kernel
749 OpMemoryModel Physical64 OpenCL
750 OpName %1 "foo"
751 OpName %3 "param"
752 OpDecorate %1 LinkageAttributes "foo" Import
753 %5 = OpTypeVoid
754 %6 = OpTypeInt 8 0
755 %7 = OpTypeFunction %5 %6
756 %1 = OpFunction %5 None %7
757 %3 = OpFunctionParameter %6
758 OpFunctionEnd
759 %8 = OpFunction %5 None %7
760 %4 = OpFunctionParameter %6
761 %10 = OpLabel
762 %11 = OpFunctionCall %5 %1 %4
763 OpReturn
764 OpFunctionEnd
765 )";
766 const std::string body2 = R"(
767 OpCapability Linkage
768 OpCapability Addresses
769 OpCapability Kernel
770 OpMemoryModel Physical64 OpenCL
771 OpName %1 "foo"
772 OpName %2 "param"
773 OpDecorate %1 LinkageAttributes "foo" Export
774 %3 = OpTypeVoid
775 %4 = OpTypeInt 32 0
776 %5 = OpTypeFunction %3 %4
777 %1 = OpFunction %3 None %5
778 %2 = OpFunctionParameter %4
779 %6 = OpLabel
780 OpReturn
781 OpFunctionEnd
782 )";
783
784 LinkerOptions options;
785 for (int i = 0; i < 2; i++) {
786 spvtest::Binary linked_binary;
787 options.SetAllowPtrTypeMismatch(i == 1);
788 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
789 AssembleAndLink({body1, body2}, &linked_binary))
790 << GetErrorMessage();
791 EXPECT_THAT(
792 GetErrorMessage(),
793 HasSubstr("Type mismatch on symbol \"foo\" between imported "
794 "variable/function %1 and exported variable/function %10"));
795 }
796 }
797
798 TEST_F(MatchingImportsToExports, FunctionSignatureMismatchTypePointerInt) {
799 const std::string body1 = R"(
800 OpCapability Linkage
801 OpCapability Addresses
802 OpCapability Kernel
803 OpMemoryModel Physical64 OpenCL
804 OpName %1 "foo"
805 OpName %3 "param"
806 OpDecorate %1 LinkageAttributes "foo" Import
807 %5 = OpTypeVoid
808 %6 = OpTypeInt 64 0
809 %7 = OpTypeFunction %5 %6
810 %1 = OpFunction %5 None %7
811 %3 = OpFunctionParameter %6
812 OpFunctionEnd
813 %8 = OpFunction %5 None %7
814 %4 = OpFunctionParameter %6
815 %10 = OpLabel
816 %11 = OpFunctionCall %5 %1 %4
817 OpReturn
818 OpFunctionEnd
819 )";
820 const std::string body2 = R"(
821 OpCapability Linkage
822 OpCapability Addresses
823 OpCapability Kernel
824 OpMemoryModel Physical64 OpenCL
825 OpName %1 "foo"
826 OpName %2 "param"
827 OpDecorate %1 LinkageAttributes "foo" Export
828 %3 = OpTypeVoid
829 %4 = OpTypeInt 64 0
830 %7 = OpTypePointer Function %4
831 %5 = OpTypeFunction %3 %7
832 %1 = OpFunction %3 None %5
833 %2 = OpFunctionParameter %7
834 %6 = OpLabel
835 OpReturn
836 OpFunctionEnd
837 )";
838
839 LinkerOptions options;
840 for (int i = 0; i < 2; i++) {
841 spvtest::Binary linked_binary;
842 options.SetAllowPtrTypeMismatch(i == 1);
843 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
844 AssembleAndLink({body1, body2}, &linked_binary))
845 << GetErrorMessage();
846 EXPECT_THAT(
847 GetErrorMessage(),
848 HasSubstr("Type mismatch on symbol \"foo\" between imported "
849 "variable/function %1 and exported variable/function %10"));
850 }
851 }
852
853 TEST_F(MatchingImportsToExports, FunctionSignatureMismatchTypeIntPointer) {
854 const std::string body1 = R"(
855 OpCapability Linkage
856 OpCapability Addresses
857 OpCapability Kernel
858 OpMemoryModel Physical64 OpenCL
859 OpName %1 "foo"
860 OpName %3 "param"
861 OpDecorate %1 LinkageAttributes "foo" Import
862 %5 = OpTypeVoid
863 %6 = OpTypeInt 64 0
864 %9 = OpTypePointer Function %6
865 %7 = OpTypeFunction %5 %9
866 %1 = OpFunction %5 None %7
867 %3 = OpFunctionParameter %9
868 OpFunctionEnd
869 %8 = OpFunction %5 None %7
870 %4 = OpFunctionParameter %9
871 %10 = OpLabel
872 %11 = OpFunctionCall %5 %1 %4
873 OpReturn
874 OpFunctionEnd
875 )";
876 const std::string body2 = R"(
877 OpCapability Linkage
878 OpCapability Addresses
879 OpCapability Kernel
880 OpMemoryModel Physical64 OpenCL
881 OpName %1 "foo"
882 OpName %2 "param"
883 OpDecorate %1 LinkageAttributes "foo" Export
884 %3 = OpTypeVoid
885 %4 = OpTypeInt 64 0
886 %5 = OpTypeFunction %3 %4
887 %1 = OpFunction %3 None %5
888 %2 = OpFunctionParameter %4
889 %6 = OpLabel
890 OpReturn
891 OpFunctionEnd
892 )";
893
894 LinkerOptions options;
895 for (int i = 0; i < 2; i++) {
896 spvtest::Binary linked_binary;
897 options.SetAllowPtrTypeMismatch(i == 1);
898 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
899 AssembleAndLink({body1, body2}, &linked_binary))
900 << GetErrorMessage();
901 EXPECT_THAT(
902 GetErrorMessage(),
903 HasSubstr("Type mismatch on symbol \"foo\" between imported "
904 "variable/function %1 and exported variable/function %11"));
905 }
906 }
907
908 TEST_F(MatchingImportsToExports, LinkOnceODRLinkageVarSingle) {
909 const std::string body1 = R"(
910 OpCapability Linkage
911 OpCapability Addresses
912 OpCapability Kernel
913 OpExtension "SPV_KHR_linkonce_odr"
914 OpMemoryModel Physical64 OpenCL
915 OpDecorate %1 LinkageAttributes "foo" LinkOnceODR
916 %2 = OpTypeFloat 32
917 %3 = OpConstant %2 3.1415
918 %1 = OpVariable %2 Uniform %3
919 )";
920
921 const std::string body2 = R"(
922 OpCapability Linkage
923 OpCapability Addresses
924 OpCapability Kernel
925 OpExtension "SPV_KHR_linkonce_odr"
926 OpMemoryModel Physical64 OpenCL
927 OpDecorate %1 LinkageAttributes "foo" Import
928 %2 = OpTypeFloat 32
929 %1 = OpVariable %2 Uniform
930 )";
931
932 const std::string matchTemplate = R"(
933 ; CHECK-NOT: OpDecorate {{.*}} Import
934 ; CHECK-NOT: OpDecorate {{.*}} LinkOnceODR
935 )";
936
937 spvtest::Binary linked_binary;
938 EXPECT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
939 << GetErrorMessage();
940 Match(matchTemplate, linked_binary);
941 }
942
943 TEST_F(MatchingImportsToExports, LinkOnceODRLinkageFunMultiple) {
944 const std::string body1 = R"(
945 OpCapability Linkage
946 OpCapability Addresses
947 OpCapability Kernel
948 OpExtension "SPV_KHR_linkonce_odr"
949 OpMemoryModel Physical64 OpenCL
950 OpDecorate %1 LinkageAttributes "foo" LinkOnceODR
951 %2 = OpTypeVoid
952 %3 = OpTypeFunction %2
953 %1 = OpFunction %2 Inline %3
954 %4 = OpLabel
955 OpReturn
956 OpFunctionEnd
957 )";
958
959 const std::string body2 = R"(
960 OpCapability Linkage
961 OpCapability Addresses
962 OpCapability Kernel
963 OpExtension "SPV_KHR_linkonce_odr"
964 OpMemoryModel Physical64 OpenCL
965 OpDecorate %1 LinkageAttributes "foo" Import
966 %2 = OpTypeVoid
967 %3 = OpTypeFunction %2
968 %1 = OpFunction %2 None %3
969 OpFunctionEnd
970 )";
971
972 const std::string matchTemplate = R"(
973 ; CHECK-NOT: OpDecorate {{.*}} Import
974 ; CHECK-NOT: OpDecorate {{.*}} LinkOnceODR
975 )";
976
977 spvtest::Binary linked_binary;
978 EXPECT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body1, body2}, &linked_binary))
979 << GetErrorMessage();
980 Match(matchTemplate, linked_binary);
981 }
982
983 TEST_F(MatchingImportsToExports, LinkOnceODRAndExport) {
984 const std::string body1 = R"(
985 OpCapability Linkage
986 OpCapability Addresses
987 OpCapability Kernel
988 OpExtension "SPV_KHR_linkonce_odr"
989 OpMemoryModel Physical64 OpenCL
990 OpDecorate %1 LinkageAttributes "foo" LinkOnceODR
991 %2 = OpTypeFloat 32
992 %3 = OpConstant %2 3.1415
993 %1 = OpVariable %2 Uniform %3
994 )";
995
996 const std::string body2 = R"(
997 OpCapability Linkage
998 OpCapability Addresses
999 OpCapability Kernel
1000 OpExtension "SPV_KHR_linkonce_odr"
1001 OpMemoryModel Physical64 OpenCL
1002 OpDecorate %1 LinkageAttributes "foo" Export
1003 %2 = OpTypeFloat 32
1004 %3 = OpConstant %2 2.7183
1005 %1 = OpVariable %2 Uniform %3
1006 )";
1007
1008 const std::string body3 = R"(
1009 OpCapability Linkage
1010 OpCapability Addresses
1011 OpCapability Kernel
1012 OpExtension "SPV_KHR_linkonce_odr"
1013 OpMemoryModel Physical64 OpenCL
1014 OpDecorate %1 LinkageAttributes "foo" Import
1015 %2 = OpTypeFloat 32
1016 %1 = OpVariable %2 Uniform
1017 )";
1018 spvtest::Binary linked_binary;
1019 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
1020 AssembleAndLink({body1, body2, body3}, &linked_binary))
1021 << GetErrorMessage();
1022 EXPECT_THAT(
1023 GetErrorMessage(),
1024 HasSubstr("Combination of Export and LinkOnceODR is not allowed, found "
1025 "for \"foo\""));
1026 }
1027
1028 } // namespace
1029 } // namespace spvtools