aboutsummaryrefslogtreecommitdiff
path: root/test/opt/assembly_builder_test.cpp
blob: 55fbbe904624f693d9bd2ff5e4e824a338c69c37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "test/opt/assembly_builder.h"

#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"

namespace spvtools {
namespace opt {
namespace {

using AssemblyBuilderTest = PassTest<::testing::Test>;

TEST_F(AssemblyBuilderTest, MinimalShader) {
  AssemblyBuilder builder;
  std::vector<const char*> expected = {
      // clang-format off
                    "OpCapability Shader",
                    "OpCapability Float64",
               "%1 = OpExtInstImport \"GLSL.std.450\"",
                    "OpMemoryModel Logical GLSL450",
                    "OpEntryPoint Vertex %main \"main\"",
                    "OpName %void \"void\"",
                    "OpName %main_func_type \"main_func_type\"",
                    "OpName %main \"main\"",
                    "OpName %main_func_entry_block \"main_func_entry_block\"",
            "%void = OpTypeVoid",
  "%main_func_type = OpTypeFunction %void",
            "%main = OpFunction %void None %main_func_type",
"%main_func_entry_block = OpLabel",
                    "OpReturn",
                    "OpFunctionEnd",
      // clang-format on
  };

  SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
                                  /* skip_nop = */ false);
}

TEST_F(AssemblyBuilderTest, ShaderWithConstants) {
  AssemblyBuilder builder;
  builder
      .AppendTypesConstantsGlobals({
          // clang-format off
          "%bool = OpTypeBool",
      "%_PF_bool = OpTypePointer Function %bool",
            "%bt = OpConstantTrue %bool",
            "%bf = OpConstantFalse %bool",
           "%int = OpTypeInt 32 1",
       "%_PF_int = OpTypePointer Function %int",
            "%si = OpConstant %int 1",
          "%uint = OpTypeInt 32 0",
      "%_PF_uint = OpTypePointer Function %uint",
            "%ui = OpConstant %uint 2",
         "%float = OpTypeFloat 32",
     "%_PF_float = OpTypePointer Function %float",
             "%f = OpConstant %float 3.1415",
        "%double = OpTypeFloat 64",
    "%_PF_double = OpTypePointer Function %double",
             "%d = OpConstant %double 3.14159265358979",
          // clang-format on
      })
      .AppendInMain({
          // clang-format off
          "%btv = OpVariable %_PF_bool Function",
          "%bfv = OpVariable %_PF_bool Function",
           "%iv = OpVariable %_PF_int Function",
           "%uv = OpVariable %_PF_uint Function",
           "%fv = OpVariable %_PF_float Function",
           "%dv = OpVariable %_PF_double Function",
                 "OpStore %btv %bt",
                 "OpStore %bfv %bf",
                 "OpStore %iv %si",
                 "OpStore %uv %ui",
                 "OpStore %fv %f",
                 "OpStore %dv %d",
          // clang-format on
      });

  std::vector<const char*> expected = {
      // clang-format off
                "OpCapability Shader",
                "OpCapability Float64",
           "%1 = OpExtInstImport \"GLSL.std.450\"",
                "OpMemoryModel Logical GLSL450",
                "OpEntryPoint Vertex %main \"main\"",
                "OpName %void \"void\"",
                "OpName %main_func_type \"main_func_type\"",
                "OpName %main \"main\"",
                "OpName %main_func_entry_block \"main_func_entry_block\"",
                "OpName %bool \"bool\"",
                "OpName %_PF_bool \"_PF_bool\"",
                "OpName %bt \"bt\"",
                "OpName %bf \"bf\"",
                "OpName %int \"int\"",
                "OpName %_PF_int \"_PF_int\"",
                "OpName %si \"si\"",
                "OpName %uint \"uint\"",
                "OpName %_PF_uint \"_PF_uint\"",
                "OpName %ui \"ui\"",
                "OpName %float \"float\"",
                "OpName %_PF_float \"_PF_float\"",
                "OpName %f \"f\"",
                "OpName %double \"double\"",
                "OpName %_PF_double \"_PF_double\"",
                "OpName %d \"d\"",
                "OpName %btv \"btv\"",
                "OpName %bfv \"bfv\"",
                "OpName %iv \"iv\"",
                "OpName %uv \"uv\"",
                "OpName %fv \"fv\"",
                "OpName %dv \"dv\"",
        "%void = OpTypeVoid",
"%main_func_type = OpTypeFunction %void",
        "%bool = OpTypeBool",
 "%_PF_bool = OpTypePointer Function %bool",
          "%bt = OpConstantTrue %bool",
          "%bf = OpConstantFalse %bool",
         "%int = OpTypeInt 32 1",
     "%_PF_int = OpTypePointer Function %int",
          "%si = OpConstant %int 1",
        "%uint = OpTypeInt 32 0",
    "%_PF_uint = OpTypePointer Function %uint",
          "%ui = OpConstant %uint 2",
       "%float = OpTypeFloat 32",
   "%_PF_float = OpTypePointer Function %float",
           "%f = OpConstant %float 3.1415",
      "%double = OpTypeFloat 64",
  "%_PF_double = OpTypePointer Function %double",
           "%d = OpConstant %double 3.14159265358979",
        "%main = OpFunction %void None %main_func_type",
"%main_func_entry_block = OpLabel",
         "%btv = OpVariable %_PF_bool Function",
         "%bfv = OpVariable %_PF_bool Function",
          "%iv = OpVariable %_PF_int Function",
          "%uv = OpVariable %_PF_uint Function",
          "%fv = OpVariable %_PF_float Function",
          "%dv = OpVariable %_PF_double Function",
                "OpStore %btv %bt",
                "OpStore %bfv %bf",
                "OpStore %iv %si",
                "OpStore %uv %ui",
                "OpStore %fv %f",
                "OpStore %dv %d",
                "OpReturn",
                "OpFunctionEnd",
      // clang-format on
  };
  SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
                                  /* skip_nop = */ false);
}

TEST_F(AssemblyBuilderTest, SpecConstants) {
  AssemblyBuilder builder;
  builder.AppendTypesConstantsGlobals({
      "%bool = OpTypeBool",
      "%uint = OpTypeInt 32 0",
      "%int = OpTypeInt 32 1",
      "%float = OpTypeFloat 32",
      "%double = OpTypeFloat 64",
      "%v2int = OpTypeVector %int 2",

      "%spec_true = OpSpecConstantTrue %bool",
      "%spec_false = OpSpecConstantFalse %bool",
      "%spec_uint = OpSpecConstant %uint 1",
      "%spec_int = OpSpecConstant %int 1",
      "%spec_float = OpSpecConstant %float 1.25",
      "%spec_double = OpSpecConstant %double 1.2345678",

      // Spec constants defined below should not have SpecID.
      "%spec_add_op = OpSpecConstantOp %int IAdd %spec_int %spec_int",
      "%spec_vec = OpSpecConstantComposite %v2int %spec_int %spec_int",
      "%spec_vec_x = OpSpecConstantOp %int CompositeExtract %spec_vec 0",
  });
  std::vector<const char*> expected = {
      // clang-format off
                    "OpCapability Shader",
                    "OpCapability Float64",
               "%1 = OpExtInstImport \"GLSL.std.450\"",
                    "OpMemoryModel Logical GLSL450",
                    "OpEntryPoint Vertex %main \"main\"",
                    "OpName %void \"void\"",
                    "OpName %main_func_type \"main_func_type\"",
                    "OpName %main \"main\"",
                    "OpName %main_func_entry_block \"main_func_entry_block\"",
                    "OpName %bool \"bool\"",
                    "OpName %uint \"uint\"",
                    "OpName %int \"int\"",
                    "OpName %float \"float\"",
                    "OpName %double \"double\"",
                    "OpName %v2int \"v2int\"",
                    "OpName %spec_true \"spec_true\"",
                    "OpName %spec_false \"spec_false\"",
                    "OpName %spec_uint \"spec_uint\"",
                    "OpName %spec_int \"spec_int\"",
                    "OpName %spec_float \"spec_float\"",
                    "OpName %spec_double \"spec_double\"",
                    "OpName %spec_add_op \"spec_add_op\"",
                    "OpName %spec_vec \"spec_vec\"",
                    "OpName %spec_vec_x \"spec_vec_x\"",
                    "OpDecorate %spec_true SpecId 200",
                    "OpDecorate %spec_false SpecId 201",
                    "OpDecorate %spec_uint SpecId 202",
                    "OpDecorate %spec_int SpecId 203",
                    "OpDecorate %spec_float SpecId 204",
                    "OpDecorate %spec_double SpecId 205",
            "%void = OpTypeVoid",
  "%main_func_type = OpTypeFunction %void",
            "%bool = OpTypeBool",
            "%uint = OpTypeInt 32 0",
             "%int = OpTypeInt 32 1",
           "%float = OpTypeFloat 32",
          "%double = OpTypeFloat 64",
           "%v2int = OpTypeVector %int 2",
       "%spec_true = OpSpecConstantTrue %bool",
      "%spec_false = OpSpecConstantFalse %bool",
       "%spec_uint = OpSpecConstant %uint 1",
        "%spec_int = OpSpecConstant %int 1",
      "%spec_float = OpSpecConstant %float 1.25",
     "%spec_double = OpSpecConstant %double 1.2345678",
     "%spec_add_op = OpSpecConstantOp %int IAdd %spec_int %spec_int",
        "%spec_vec = OpSpecConstantComposite %v2int %spec_int %spec_int",
      "%spec_vec_x = OpSpecConstantOp %int CompositeExtract %spec_vec 0",
            "%main = OpFunction %void None %main_func_type",
"%main_func_entry_block = OpLabel",
                    "OpReturn",
                    "OpFunctionEnd",

      // clang-format on
  };

  SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
                                  /* skip_nop = */ false);
}

TEST_F(AssemblyBuilderTest, AppendNames) {
  AssemblyBuilder builder;
  builder.AppendNames({
      "OpName %void \"another_name_for_void\"",
      "I am an invalid OpName instruction and should not be added",
      "OpName %main \"another name for main\"",
  });
  std::vector<const char*> expected = {
      // clang-format off
                    "OpCapability Shader",
                    "OpCapability Float64",
               "%1 = OpExtInstImport \"GLSL.std.450\"",
                    "OpMemoryModel Logical GLSL450",
                    "OpEntryPoint Vertex %main \"main\"",
                    "OpName %void \"void\"",
                    "OpName %main_func_type \"main_func_type\"",
                    "OpName %main \"main\"",
                    "OpName %main_func_entry_block \"main_func_entry_block\"",
                    "OpName %void \"another_name_for_void\"",
                    "OpName %main \"another name for main\"",
            "%void = OpTypeVoid",
  "%main_func_type = OpTypeFunction %void",
            "%main = OpFunction %void None %main_func_type",
"%main_func_entry_block = OpLabel",
                    "OpReturn",
                    "OpFunctionEnd",
      // clang-format on
  };

  SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
                                  /* skip_nop = */ false);
}

}  // namespace
}  // namespace opt
}  // namespace spvtools