1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 #include <vector>
17 
18 #include "gmock/gmock.h"
19 #include "test/link/linker_fixture.h"
20 
21 namespace spvtools {
22 namespace {
23 
24 using UniqueIds = spvtest::LinkerTest;
25 
TEST_F(UniqueIds,UniquelyMerged)26 TEST_F(UniqueIds, UniquelyMerged) {
27   std::vector<std::string> bodies(2);
28   bodies[0] =
29       // clang-format off
30                "OpCapability Shader\n"
31           "%1 = OpExtInstImport \"GLSL.std.450\"\n"
32                "OpMemoryModel Logical GLSL450\n"
33                "OpEntryPoint Vertex %main \"main\"\n"
34                "OpSource ESSL 310\n"
35                "OpName %main \"main\"\n"
36                "OpName %f_ \"f(\"\n"
37                "OpName %gv1 \"gv1\"\n"
38                "OpName %gv2 \"gv2\"\n"
39                "OpName %lv1 \"lv1\"\n"
40                "OpName %lv2 \"lv2\"\n"
41                "OpName %lv1_0 \"lv1\"\n"
42        "%void = OpTypeVoid\n"
43          "%10 = OpTypeFunction %void\n"
44       "%float = OpTypeFloat 32\n"
45          "%12 = OpTypeFunction %float\n"
46  "%_ptr_Private_float = OpTypePointer Private %float\n"
47         "%gv1 = OpVariable %_ptr_Private_float Private\n"
48    "%float_10 = OpConstant %float 10\n"
49         "%gv2 = OpVariable %_ptr_Private_float Private\n"
50   "%float_100 = OpConstant %float 100\n"
51  "%_ptr_Function_float = OpTypePointer Function %float\n"
52        "%main = OpFunction %void None %10\n"
53          "%17 = OpLabel\n"
54       "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
55                "OpStore %gv1 %float_10\n"
56                "OpStore %gv2 %float_100\n"
57          "%18 = OpLoad %float %gv1\n"
58          "%19 = OpLoad %float %gv2\n"
59          "%20 = OpFSub %float %18 %19\n"
60                "OpStore %lv1_0 %20\n"
61                "OpReturn\n"
62                "OpFunctionEnd\n"
63          "%f_ = OpFunction %float None %12\n"
64          "%21 = OpLabel\n"
65         "%lv1 = OpVariable %_ptr_Function_float Function\n"
66         "%lv2 = OpVariable %_ptr_Function_float Function\n"
67          "%22 = OpLoad %float %gv1\n"
68          "%23 = OpLoad %float %gv2\n"
69          "%24 = OpFAdd %float %22 %23\n"
70                "OpStore %lv1 %24\n"
71          "%25 = OpLoad %float %gv1\n"
72          "%26 = OpLoad %float %gv2\n"
73          "%27 = OpFMul %float %25 %26\n"
74                "OpStore %lv2 %27\n"
75          "%28 = OpLoad %float %lv1\n"
76          "%29 = OpLoad %float %lv2\n"
77          "%30 = OpFDiv %float %28 %29\n"
78                "OpReturnValue %30\n"
79                "OpFunctionEnd\n";
80   // clang-format on
81   bodies[1] =
82       // clang-format off
83                "OpCapability Shader\n"
84           "%1 = OpExtInstImport \"GLSL.std.450\"\n"
85                "OpMemoryModel Logical GLSL450\n"
86                "OpSource ESSL 310\n"
87                "OpName %main \"main2\"\n"
88                "OpName %f_ \"f(\"\n"
89                "OpName %gv1 \"gv12\"\n"
90                "OpName %gv2 \"gv22\"\n"
91                "OpName %lv1 \"lv12\"\n"
92                "OpName %lv2 \"lv22\"\n"
93                "OpName %lv1_0 \"lv12\"\n"
94        "%void = OpTypeVoid\n"
95          "%10 = OpTypeFunction %void\n"
96       "%float = OpTypeFloat 32\n"
97          "%12 = OpTypeFunction %float\n"
98  "%_ptr_Private_float = OpTypePointer Private %float\n"
99         "%gv1 = OpVariable %_ptr_Private_float Private\n"
100    "%float_10 = OpConstant %float 10\n"
101         "%gv2 = OpVariable %_ptr_Private_float Private\n"
102   "%float_100 = OpConstant %float 100\n"
103  "%_ptr_Function_float = OpTypePointer Function %float\n"
104        "%main = OpFunction %void None %10\n"
105          "%17 = OpLabel\n"
106       "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
107                "OpStore %gv1 %float_10\n"
108                "OpStore %gv2 %float_100\n"
109          "%18 = OpLoad %float %gv1\n"
110          "%19 = OpLoad %float %gv2\n"
111          "%20 = OpFSub %float %18 %19\n"
112                "OpStore %lv1_0 %20\n"
113                "OpReturn\n"
114                "OpFunctionEnd\n"
115          "%f_ = OpFunction %float None %12\n"
116          "%21 = OpLabel\n"
117         "%lv1 = OpVariable %_ptr_Function_float Function\n"
118         "%lv2 = OpVariable %_ptr_Function_float Function\n"
119          "%22 = OpLoad %float %gv1\n"
120          "%23 = OpLoad %float %gv2\n"
121          "%24 = OpFAdd %float %22 %23\n"
122                "OpStore %lv1 %24\n"
123          "%25 = OpLoad %float %gv1\n"
124          "%26 = OpLoad %float %gv2\n"
125          "%27 = OpFMul %float %25 %26\n"
126                "OpStore %lv2 %27\n"
127          "%28 = OpLoad %float %lv1\n"
128          "%29 = OpLoad %float %lv2\n"
129          "%30 = OpFDiv %float %28 %29\n"
130                "OpReturnValue %30\n"
131                "OpFunctionEnd\n";
132   // clang-format on
133 
134   spvtest::Binary linked_binary;
135   LinkerOptions options;
136   options.SetVerifyIds(true);
137   spv_result_t res = AssembleAndLink(bodies, &linked_binary, options);
138   EXPECT_EQ(SPV_SUCCESS, res);
139 }
140 
141 }  // namespace
142 }  // namespace spvtools
143