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 "test/link/linker_fixture.h"
19
20 namespace spvtools {
21 namespace {
22
23 using ::testing::HasSubstr;
24
25 class EntryPoints : public spvtest::LinkerTest {};
26
TEST_F(EntryPoints,SameModelDifferentName)27 TEST_F(EntryPoints, SameModelDifferentName) {
28 const std::string body1 = R"(
29 OpCapability Shader
30 OpMemoryModel Logical GLSL450
31 OpEntryPoint GLCompute %3 "foo"
32 %1 = OpTypeVoid
33 %2 = OpTypeFunction %1
34 %3 = OpFunction %1 None %2
35 OpFunctionEnd
36 )";
37 const std::string body2 = R"(
38 OpCapability Shader
39 OpMemoryModel Logical GLSL450
40 OpEntryPoint GLCompute %3 "bar"
41 %1 = OpTypeVoid
42 %2 = OpTypeFunction %1
43 %3 = OpFunction %1 None %2
44 OpFunctionEnd
45 )";
46
47 spvtest::Binary linked_binary;
48 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
49 << GetErrorMessage();
50 EXPECT_THAT(GetErrorMessage(), std::string());
51 }
52
TEST_F(EntryPoints,DifferentModelSameName)53 TEST_F(EntryPoints, DifferentModelSameName) {
54 const std::string body1 = R"(
55 OpCapability Shader
56 OpMemoryModel Logical GLSL450
57 OpEntryPoint GLCompute %3 "foo"
58 %1 = OpTypeVoid
59 %2 = OpTypeFunction %1
60 %3 = OpFunction %1 None %2
61 OpFunctionEnd
62 )";
63 const std::string body2 = R"(
64 OpCapability Shader
65 OpMemoryModel Logical GLSL450
66 OpEntryPoint Vertex %3 "foo"
67 %1 = OpTypeVoid
68 %2 = OpTypeFunction %1
69 %3 = OpFunction %1 None %2
70 OpFunctionEnd
71 )";
72
73 spvtest::Binary linked_binary;
74 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
75 << GetErrorMessage();
76 EXPECT_THAT(GetErrorMessage(), std::string());
77 }
78
TEST_F(EntryPoints,SameModelAndName)79 TEST_F(EntryPoints, SameModelAndName) {
80 const std::string body1 = R"(
81 OpCapability Shader
82 OpMemoryModel Logical GLSL450
83 OpEntryPoint GLCompute %3 "foo"
84 %1 = OpTypeVoid
85 %2 = OpTypeFunction %1
86 %3 = OpFunction %1 None %2
87 OpFunctionEnd
88 )";
89 const std::string body2 = R"(
90 OpCapability Shader
91 OpMemoryModel Logical GLSL450
92 OpEntryPoint GLCompute %3 "foo"
93 %1 = OpTypeVoid
94 %2 = OpTypeFunction %1
95 %3 = OpFunction %1 None %2
96 OpFunctionEnd
97 )";
98
99 spvtest::Binary linked_binary;
100 EXPECT_EQ(SPV_ERROR_INTERNAL,
101 AssembleAndLink({body1, body2}, &linked_binary));
102 EXPECT_THAT(GetErrorMessage(),
103 HasSubstr("The entry point \"foo\", with execution model "
104 "GLCompute, was already defined."));
105 }
106
TEST_F(EntryPoints,LinkedVariables)107 TEST_F(EntryPoints, LinkedVariables) {
108 const std::string body1 = R"(
109 OpCapability Addresses
110 OpCapability Linkage
111 OpCapability Kernel
112 OpMemoryModel Physical64 OpenCL
113 OpDecorate %7 LinkageAttributes "foo" Export
114 %1 = OpTypeInt 32 0
115 %2 = OpTypeVector %1 3
116 %3 = OpTypePointer Input %2
117 %4 = OpVariable %3 Input
118 %5 = OpTypeVoid
119 %6 = OpTypeFunction %5
120 %7 = OpFunction %5 None %6
121 %8 = OpLabel
122 %9 = OpLoad %2 %4 Aligned 32
123 OpReturn
124 OpFunctionEnd
125 )";
126 const std::string body2 = R"(
127 OpCapability Linkage
128 OpCapability Kernel
129 OpMemoryModel Physical64 OpenCL
130 OpEntryPoint Kernel %4 "bar"
131 OpDecorate %3 LinkageAttributes "foo" Import
132 %1 = OpTypeVoid
133 %2 = OpTypeFunction %1
134 %3 = OpFunction %1 None %2
135 OpFunctionEnd
136 %4 = OpFunction %1 None %2
137 %5 = OpLabel
138 %6 = OpFunctionCall %1 %3
139 OpReturn
140 OpFunctionEnd
141 )";
142
143 spvtest::Binary linked_binary;
144 EXPECT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary));
145 EXPECT_THAT(GetErrorMessage(), std::string());
146 EXPECT_TRUE(Validate(linked_binary));
147 EXPECT_THAT(GetErrorMessage(), std::string());
148 }
149
150 } // namespace
151 } // namespace spvtools
152