• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Tests for unique type declaration rules validator.
16 
17 #include <string>
18 
19 #include "gmock/gmock.h"
20 #include "test/unit_spirv.h"
21 #include "test/val/val_code_generator.h"
22 #include "test/val/val_fixtures.h"
23 
24 namespace spvtools {
25 namespace val {
26 namespace {
27 
28 using ::testing::HasSubstr;
29 using ::testing::Not;
30 using ::testing::Values;
31 
32 using ValidateConversion = spvtest::ValidateBase<bool>;
33 
GenerateShaderCode(const std::string & body,const std::string & capabilities_and_extensions="",const std::string & decorations="",const std::string & types="",const std::string & variables="")34 std::string GenerateShaderCode(
35     const std::string& body,
36     const std::string& capabilities_and_extensions = "",
37     const std::string& decorations = "", const std::string& types = "",
38     const std::string& variables = "") {
39   const std::string capabilities =
40       R"(
41 OpCapability Shader
42 OpCapability Int64
43 OpCapability Float64)";
44 
45   const std::string after_extension_before_decorations =
46       R"(
47 OpMemoryModel Logical GLSL450
48 OpEntryPoint Fragment %main "main"
49 OpExecutionMode %main OriginUpperLeft)";
50 
51   const std::string after_decorations_before_types =
52       R"(
53 %void = OpTypeVoid
54 %func = OpTypeFunction %void
55 %bool = OpTypeBool
56 %f32 = OpTypeFloat 32
57 %u32 = OpTypeInt 32 0
58 %s32 = OpTypeInt 32 1
59 %f64 = OpTypeFloat 64
60 %u64 = OpTypeInt 64 0
61 %s64 = OpTypeInt 64 1
62 %boolvec2 = OpTypeVector %bool 2
63 %s32vec2 = OpTypeVector %s32 2
64 %u32vec2 = OpTypeVector %u32 2
65 %u64vec2 = OpTypeVector %u64 2
66 %f32vec2 = OpTypeVector %f32 2
67 %f64vec2 = OpTypeVector %f64 2
68 %boolvec3 = OpTypeVector %bool 3
69 %u32vec3 = OpTypeVector %u32 3
70 %u64vec3 = OpTypeVector %u64 3
71 %s32vec3 = OpTypeVector %s32 3
72 %f32vec3 = OpTypeVector %f32 3
73 %f64vec3 = OpTypeVector %f64 3
74 %boolvec4 = OpTypeVector %bool 4
75 %u32vec4 = OpTypeVector %u32 4
76 %u64vec4 = OpTypeVector %u64 4
77 %s32vec4 = OpTypeVector %s32 4
78 %f32vec4 = OpTypeVector %f32 4
79 %f64vec4 = OpTypeVector %f64 4
80 
81 %f32_0 = OpConstant %f32 0
82 %f32_1 = OpConstant %f32 1
83 %f32_2 = OpConstant %f32 2
84 %f32_3 = OpConstant %f32 3
85 %f32_4 = OpConstant %f32 4
86 
87 %s32_0 = OpConstant %s32 0
88 %s32_1 = OpConstant %s32 1
89 %s32_2 = OpConstant %s32 2
90 %s32_3 = OpConstant %s32 3
91 %s32_4 = OpConstant %s32 4
92 %s32_m1 = OpConstant %s32 -1
93 
94 %u32_0 = OpConstant %u32 0
95 %u32_1 = OpConstant %u32 1
96 %u32_2 = OpConstant %u32 2
97 %u32_3 = OpConstant %u32 3
98 %u32_4 = OpConstant %u32 4
99 
100 %f64_0 = OpConstant %f64 0
101 %f64_1 = OpConstant %f64 1
102 %f64_2 = OpConstant %f64 2
103 %f64_3 = OpConstant %f64 3
104 %f64_4 = OpConstant %f64 4
105 
106 %s64_0 = OpConstant %s64 0
107 %s64_1 = OpConstant %s64 1
108 %s64_2 = OpConstant %s64 2
109 %s64_3 = OpConstant %s64 3
110 %s64_4 = OpConstant %s64 4
111 %s64_m1 = OpConstant %s64 -1
112 
113 %u64_0 = OpConstant %u64 0
114 %u64_1 = OpConstant %u64 1
115 %u64_2 = OpConstant %u64 2
116 %u64_3 = OpConstant %u64 3
117 %u64_4 = OpConstant %u64 4
118 
119 %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
120 %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
121 %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
122 %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
123 %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
124 %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
125 
126 %s32vec2_01 = OpConstantComposite %s32vec2 %s32_0 %s32_1
127 %s32vec2_12 = OpConstantComposite %s32vec2 %s32_1 %s32_2
128 %s32vec3_012 = OpConstantComposite %s32vec3 %s32_0 %s32_1 %s32_2
129 %s32vec3_123 = OpConstantComposite %s32vec3 %s32_1 %s32_2 %s32_3
130 %s32vec4_0123 = OpConstantComposite %s32vec4 %s32_0 %s32_1 %s32_2 %s32_3
131 %s32vec4_1234 = OpConstantComposite %s32vec4 %s32_1 %s32_2 %s32_3 %s32_4
132 
133 %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
134 %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
135 %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
136 %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
137 %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
138 %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
139 
140 %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
141 %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
142 %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
143 %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
144 %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
145 %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
146 
147 %true = OpConstantTrue %bool
148 %false = OpConstantFalse %bool
149 
150 %f32ptr_func = OpTypePointer Function %f32)";
151 
152   const std::string after_variables_before_body =
153       R"(
154 %main = OpFunction %void None %func
155 %main_entry = OpLabel)";
156 
157   const std::string after_body =
158       R"(
159 OpReturn
160 OpFunctionEnd)";
161 
162   return capabilities + capabilities_and_extensions +
163          after_extension_before_decorations + decorations +
164          after_decorations_before_types + types + variables +
165          after_variables_before_body + body + after_body;
166 }
167 
GenerateKernelCode(const std::string & body,const std::string & capabilities_and_extensions="")168 std::string GenerateKernelCode(
169     const std::string& body,
170     const std::string& capabilities_and_extensions = "") {
171   const std::string capabilities =
172       R"(
173 OpCapability Addresses
174 OpCapability Kernel
175 OpCapability Linkage
176 OpCapability GenericPointer
177 OpCapability Int64
178 OpCapability Float64)";
179 
180   const std::string after_extension_before_body =
181       R"(
182 OpMemoryModel Physical32 OpenCL
183 %void = OpTypeVoid
184 %func = OpTypeFunction %void
185 %bool = OpTypeBool
186 %f32 = OpTypeFloat 32
187 %u32 = OpTypeInt 32 0
188 %f64 = OpTypeFloat 64
189 %u64 = OpTypeInt 64 0
190 %boolvec2 = OpTypeVector %bool 2
191 %u32vec2 = OpTypeVector %u32 2
192 %u64vec2 = OpTypeVector %u64 2
193 %f32vec2 = OpTypeVector %f32 2
194 %f64vec2 = OpTypeVector %f64 2
195 %boolvec3 = OpTypeVector %bool 3
196 %u32vec3 = OpTypeVector %u32 3
197 %u64vec3 = OpTypeVector %u64 3
198 %f32vec3 = OpTypeVector %f32 3
199 %f64vec3 = OpTypeVector %f64 3
200 %boolvec4 = OpTypeVector %bool 4
201 %u32vec4 = OpTypeVector %u32 4
202 %u64vec4 = OpTypeVector %u64 4
203 %f32vec4 = OpTypeVector %f32 4
204 %f64vec4 = OpTypeVector %f64 4
205 
206 %f32_0 = OpConstant %f32 0
207 %f32_1 = OpConstant %f32 1
208 %f32_2 = OpConstant %f32 2
209 %f32_3 = OpConstant %f32 3
210 %f32_4 = OpConstant %f32 4
211 
212 %u32_0 = OpConstant %u32 0
213 %u32_1 = OpConstant %u32 1
214 %u32_2 = OpConstant %u32 2
215 %u32_3 = OpConstant %u32 3
216 %u32_4 = OpConstant %u32 4
217 
218 %f64_0 = OpConstant %f64 0
219 %f64_1 = OpConstant %f64 1
220 %f64_2 = OpConstant %f64 2
221 %f64_3 = OpConstant %f64 3
222 %f64_4 = OpConstant %f64 4
223 
224 %u64_0 = OpConstant %u64 0
225 %u64_1 = OpConstant %u64 1
226 %u64_2 = OpConstant %u64 2
227 %u64_3 = OpConstant %u64 3
228 %u64_4 = OpConstant %u64 4
229 
230 %u32vec2_01 = OpConstantComposite %u32vec2 %u32_0 %u32_1
231 %u32vec2_12 = OpConstantComposite %u32vec2 %u32_1 %u32_2
232 %u32vec3_012 = OpConstantComposite %u32vec3 %u32_0 %u32_1 %u32_2
233 %u32vec3_123 = OpConstantComposite %u32vec3 %u32_1 %u32_2 %u32_3
234 %u32vec4_0123 = OpConstantComposite %u32vec4 %u32_0 %u32_1 %u32_2 %u32_3
235 %u32vec4_1234 = OpConstantComposite %u32vec4 %u32_1 %u32_2 %u32_3 %u32_4
236 
237 %f32vec2_01 = OpConstantComposite %f32vec2 %f32_0 %f32_1
238 %f32vec2_12 = OpConstantComposite %f32vec2 %f32_1 %f32_2
239 %f32vec3_012 = OpConstantComposite %f32vec3 %f32_0 %f32_1 %f32_2
240 %f32vec3_123 = OpConstantComposite %f32vec3 %f32_1 %f32_2 %f32_3
241 %f32vec4_0123 = OpConstantComposite %f32vec4 %f32_0 %f32_1 %f32_2 %f32_3
242 %f32vec4_1234 = OpConstantComposite %f32vec4 %f32_1 %f32_2 %f32_3 %f32_4
243 
244 %f64vec2_01 = OpConstantComposite %f64vec2 %f64_0 %f64_1
245 %f64vec2_12 = OpConstantComposite %f64vec2 %f64_1 %f64_2
246 %f64vec3_012 = OpConstantComposite %f64vec3 %f64_0 %f64_1 %f64_2
247 %f64vec3_123 = OpConstantComposite %f64vec3 %f64_1 %f64_2 %f64_3
248 %f64vec4_0123 = OpConstantComposite %f64vec4 %f64_0 %f64_1 %f64_2 %f64_3
249 %f64vec4_1234 = OpConstantComposite %f64vec4 %f64_1 %f64_2 %f64_3 %f64_4
250 
251 %u64vec2_01 = OpConstantComposite %u64vec2 %u64_0 %u64_1
252 
253 %true = OpConstantTrue %bool
254 %false = OpConstantFalse %bool
255 
256 %f32ptr_func = OpTypePointer Function %f32
257 %u32ptr_func = OpTypePointer Function %u32
258 %f32ptr_gen = OpTypePointer Generic %f32
259 %f32ptr_inp = OpTypePointer Input %f32
260 %f32ptr_wg = OpTypePointer Workgroup %f32
261 %f32ptr_cwg = OpTypePointer CrossWorkgroup %f32
262 
263 %f32inp = OpVariable %f32ptr_inp Input
264 
265 %main = OpFunction %void None %func
266 %main_entry = OpLabel)";
267 
268   const std::string after_body =
269       R"(
270 OpReturn
271 OpFunctionEnd)";
272 
273   return capabilities + capabilities_and_extensions +
274          after_extension_before_body + body + after_body;
275 }
276 
TEST_F(ValidateConversion,ConvertFToUSuccess)277 TEST_F(ValidateConversion, ConvertFToUSuccess) {
278   const std::string body = R"(
279 %val1 = OpConvertFToU %u32 %f32_1
280 %val2 = OpConvertFToU %u32 %f64_0
281 %val3 = OpConvertFToU %u32vec2 %f32vec2_01
282 )";
283 
284   CompileSuccessfully(GenerateShaderCode(body).c_str());
285   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
286 }
287 
TEST_F(ValidateConversion,ConvertFToUWrongResultType)288 TEST_F(ValidateConversion, ConvertFToUWrongResultType) {
289   const std::string body = R"(
290 %val = OpConvertFToU %s32 %f32_1
291 )";
292 
293   CompileSuccessfully(GenerateShaderCode(body).c_str());
294   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
295   EXPECT_THAT(getDiagnosticString(),
296               HasSubstr("Expected unsigned int scalar or vector type as Result "
297                         "Type: ConvertFToU"));
298 }
299 
TEST_F(ValidateConversion,ConvertFToUWrongInputType)300 TEST_F(ValidateConversion, ConvertFToUWrongInputType) {
301   const std::string body = R"(
302 %val = OpConvertFToU %u32 %u32_1
303 )";
304 
305   CompileSuccessfully(GenerateShaderCode(body).c_str());
306   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
307   EXPECT_THAT(
308       getDiagnosticString(),
309       HasSubstr("Expected input to be float scalar or vector: ConvertFToU"));
310 }
311 
TEST_F(ValidateConversion,ConvertFToUDifferentDimension)312 TEST_F(ValidateConversion, ConvertFToUDifferentDimension) {
313   const std::string body = R"(
314 %val = OpConvertFToU %u32 %f32vec2_01
315 )";
316 
317   CompileSuccessfully(GenerateShaderCode(body).c_str());
318   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
319   EXPECT_THAT(getDiagnosticString(),
320               HasSubstr("Expected input to have the same dimension as Result "
321                         "Type: ConvertFToU"));
322 }
323 
TEST_F(ValidateConversion,ConvertFToSSuccess)324 TEST_F(ValidateConversion, ConvertFToSSuccess) {
325   const std::string body = R"(
326 %val1 = OpConvertFToS %s32 %f32_1
327 %val2 = OpConvertFToS %u32 %f64_0
328 %val3 = OpConvertFToS %s32vec2 %f32vec2_01
329 )";
330 
331   CompileSuccessfully(GenerateShaderCode(body).c_str());
332   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
333 }
334 
TEST_F(ValidateConversion,ConvertFToSWrongResultType)335 TEST_F(ValidateConversion, ConvertFToSWrongResultType) {
336   const std::string body = R"(
337 %val = OpConvertFToS %bool %f32_1
338 )";
339 
340   CompileSuccessfully(GenerateShaderCode(body).c_str());
341   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
342   EXPECT_THAT(
343       getDiagnosticString(),
344       HasSubstr(
345           "Expected int scalar or vector type as Result Type: ConvertFToS"));
346 }
347 
TEST_F(ValidateConversion,ConvertFToSWrongInputType)348 TEST_F(ValidateConversion, ConvertFToSWrongInputType) {
349   const std::string body = R"(
350 %val = OpConvertFToS %s32 %u32_1
351 )";
352 
353   CompileSuccessfully(GenerateShaderCode(body).c_str());
354   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
355   EXPECT_THAT(
356       getDiagnosticString(),
357       HasSubstr("Expected input to be float scalar or vector: ConvertFToS"));
358 }
359 
TEST_F(ValidateConversion,ConvertFToSDifferentDimension)360 TEST_F(ValidateConversion, ConvertFToSDifferentDimension) {
361   const std::string body = R"(
362 %val = OpConvertFToS %u32 %f32vec2_01
363 )";
364 
365   CompileSuccessfully(GenerateShaderCode(body).c_str());
366   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
367   EXPECT_THAT(getDiagnosticString(),
368               HasSubstr("Expected input to have the same dimension as Result "
369                         "Type: ConvertFToS"));
370 }
371 
TEST_F(ValidateConversion,ConvertSToFSuccess)372 TEST_F(ValidateConversion, ConvertSToFSuccess) {
373   const std::string body = R"(
374 %val1 = OpConvertSToF %f32 %u32_1
375 %val2 = OpConvertSToF %f32 %s64_0
376 %val3 = OpConvertSToF %f32vec2 %s32vec2_01
377 )";
378 
379   CompileSuccessfully(GenerateShaderCode(body).c_str());
380   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
381 }
382 
TEST_F(ValidateConversion,ConvertSToFWrongResultType)383 TEST_F(ValidateConversion, ConvertSToFWrongResultType) {
384   const std::string body = R"(
385 %val = OpConvertSToF %u32 %s32_1
386 )";
387 
388   CompileSuccessfully(GenerateShaderCode(body).c_str());
389   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
390   EXPECT_THAT(
391       getDiagnosticString(),
392       HasSubstr(
393           "Expected float scalar or vector type as Result Type: ConvertSToF"));
394 }
395 
TEST_F(ValidateConversion,ConvertSToFWrongInputType)396 TEST_F(ValidateConversion, ConvertSToFWrongInputType) {
397   const std::string body = R"(
398 %val = OpConvertSToF %f32 %f32_1
399 )";
400 
401   CompileSuccessfully(GenerateShaderCode(body).c_str());
402   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
403   EXPECT_THAT(
404       getDiagnosticString(),
405       HasSubstr("Expected input to be int scalar or vector: ConvertSToF"));
406 }
407 
TEST_F(ValidateConversion,ConvertSToFDifferentDimension)408 TEST_F(ValidateConversion, ConvertSToFDifferentDimension) {
409   const std::string body = R"(
410 %val = OpConvertSToF %f32 %u32vec2_01
411 )";
412 
413   CompileSuccessfully(GenerateShaderCode(body).c_str());
414   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
415   EXPECT_THAT(getDiagnosticString(),
416               HasSubstr("Expected input to have the same dimension as Result "
417                         "Type: ConvertSToF"));
418 }
419 
TEST_F(ValidateConversion,UConvertSuccess)420 TEST_F(ValidateConversion, UConvertSuccess) {
421   const std::string body = R"(
422 %val1 = OpUConvert %u32 %u64_1
423 %val2 = OpUConvert %u64 %s32_0
424 %val3 = OpUConvert %u64vec2 %s32vec2_01
425 )";
426 
427   CompileSuccessfully(GenerateShaderCode(body).c_str());
428   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
429 }
430 
TEST_F(ValidateConversion,UConvertWrongResultType)431 TEST_F(ValidateConversion, UConvertWrongResultType) {
432   const std::string body = R"(
433 %val = OpUConvert %s32 %s32_1
434 )";
435 
436   CompileSuccessfully(GenerateShaderCode(body).c_str());
437   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
438   EXPECT_THAT(getDiagnosticString(),
439               HasSubstr("Expected unsigned int scalar or vector type as Result "
440                         "Type: UConvert"));
441 }
442 
TEST_F(ValidateConversion,UConvertWrongInputType)443 TEST_F(ValidateConversion, UConvertWrongInputType) {
444   const std::string body = R"(
445 %val = OpUConvert %u32 %f32_1
446 )";
447 
448   CompileSuccessfully(GenerateShaderCode(body).c_str());
449   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
450   EXPECT_THAT(getDiagnosticString(),
451               HasSubstr("Expected input to be int scalar or vector: UConvert"));
452 }
453 
TEST_F(ValidateConversion,UConvertDifferentDimension)454 TEST_F(ValidateConversion, UConvertDifferentDimension) {
455   const std::string body = R"(
456 %val = OpUConvert %u32 %u32vec2_01
457 )";
458 
459   CompileSuccessfully(GenerateShaderCode(body).c_str());
460   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
461   EXPECT_THAT(getDiagnosticString(),
462               HasSubstr("Expected input to have the same dimension as Result "
463                         "Type: UConvert"));
464 }
465 
TEST_F(ValidateConversion,UConvertSameBitWidth)466 TEST_F(ValidateConversion, UConvertSameBitWidth) {
467   const std::string body = R"(
468 %val = OpUConvert %u32 %s32_1
469 )";
470 
471   CompileSuccessfully(GenerateShaderCode(body).c_str());
472   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
473   EXPECT_THAT(getDiagnosticString(),
474               HasSubstr("Expected input to have different bit width from "
475                         "Result Type: UConvert"));
476 }
477 
TEST_F(ValidateConversion,SConvertSuccess)478 TEST_F(ValidateConversion, SConvertSuccess) {
479   const std::string body = R"(
480 %val1 = OpSConvert %s32 %u64_1
481 %val2 = OpSConvert %s64 %s32_0
482 %val3 = OpSConvert %u64vec2 %s32vec2_01
483 )";
484 
485   CompileSuccessfully(GenerateShaderCode(body).c_str());
486   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
487 }
488 
TEST_F(ValidateConversion,SConvertWrongResultType)489 TEST_F(ValidateConversion, SConvertWrongResultType) {
490   const std::string body = R"(
491 %val = OpSConvert %f32 %s32_1
492 )";
493 
494   CompileSuccessfully(GenerateShaderCode(body).c_str());
495   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
496   EXPECT_THAT(
497       getDiagnosticString(),
498       HasSubstr("Expected int scalar or vector type as Result Type: SConvert"));
499 }
500 
TEST_F(ValidateConversion,SConvertWrongInputType)501 TEST_F(ValidateConversion, SConvertWrongInputType) {
502   const std::string body = R"(
503 %val = OpSConvert %u32 %f32_1
504 )";
505 
506   CompileSuccessfully(GenerateShaderCode(body).c_str());
507   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
508   EXPECT_THAT(getDiagnosticString(),
509               HasSubstr("Expected input to be int scalar or vector: SConvert"));
510 }
511 
TEST_F(ValidateConversion,SConvertDifferentDimension)512 TEST_F(ValidateConversion, SConvertDifferentDimension) {
513   const std::string body = R"(
514 %val = OpSConvert %s32 %u32vec2_01
515 )";
516 
517   CompileSuccessfully(GenerateShaderCode(body).c_str());
518   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
519   EXPECT_THAT(getDiagnosticString(),
520               HasSubstr("Expected input to have the same dimension as Result "
521                         "Type: SConvert"));
522 }
523 
TEST_F(ValidateConversion,SConvertSameBitWidth)524 TEST_F(ValidateConversion, SConvertSameBitWidth) {
525   const std::string body = R"(
526 %val = OpSConvert %u32 %s32_1
527 )";
528 
529   CompileSuccessfully(GenerateShaderCode(body).c_str());
530   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
531   EXPECT_THAT(getDiagnosticString(),
532               HasSubstr("Expected input to have different bit width from "
533                         "Result Type: SConvert"));
534 }
535 
TEST_F(ValidateConversion,FConvertSuccess)536 TEST_F(ValidateConversion, FConvertSuccess) {
537   const std::string body = R"(
538 %val1 = OpFConvert %f32 %f64_1
539 %val2 = OpFConvert %f64 %f32_0
540 %val3 = OpFConvert %f64vec2 %f32vec2_01
541 )";
542 
543   CompileSuccessfully(GenerateShaderCode(body).c_str());
544   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
545 }
546 
TEST_F(ValidateConversion,FConvertWrongResultType)547 TEST_F(ValidateConversion, FConvertWrongResultType) {
548   const std::string body = R"(
549 %val = OpFConvert %u32 %f32_1
550 )";
551 
552   CompileSuccessfully(GenerateShaderCode(body).c_str());
553   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
554   EXPECT_THAT(
555       getDiagnosticString(),
556       HasSubstr(
557           "Expected float scalar or vector type as Result Type: FConvert"));
558 }
559 
TEST_F(ValidateConversion,FConvertWrongInputType)560 TEST_F(ValidateConversion, FConvertWrongInputType) {
561   const std::string body = R"(
562 %val = OpFConvert %f32 %u64_1
563 )";
564 
565   CompileSuccessfully(GenerateShaderCode(body).c_str());
566   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
567   EXPECT_THAT(
568       getDiagnosticString(),
569       HasSubstr("Expected input to be float scalar or vector: FConvert"));
570 }
571 
TEST_F(ValidateConversion,FConvertDifferentDimension)572 TEST_F(ValidateConversion, FConvertDifferentDimension) {
573   const std::string body = R"(
574 %val = OpFConvert %f64 %f32vec2_01
575 )";
576 
577   CompileSuccessfully(GenerateShaderCode(body).c_str());
578   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
579   EXPECT_THAT(getDiagnosticString(),
580               HasSubstr("Expected input to have the same dimension as Result "
581                         "Type: FConvert"));
582 }
583 
TEST_F(ValidateConversion,FConvertSameBitWidth)584 TEST_F(ValidateConversion, FConvertSameBitWidth) {
585   const std::string body = R"(
586 %val = OpFConvert %f32 %f32_1
587 )";
588 
589   CompileSuccessfully(GenerateShaderCode(body).c_str());
590   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
591   EXPECT_THAT(getDiagnosticString(),
592               HasSubstr("Expected input to have different bit width from "
593                         "Result Type: FConvert"));
594 }
595 
TEST_F(ValidateConversion,QuantizeToF16Success)596 TEST_F(ValidateConversion, QuantizeToF16Success) {
597   const std::string body = R"(
598 %val1 = OpQuantizeToF16 %f32 %f32_1
599 %val2 = OpQuantizeToF16 %f32 %f32_0
600 %val3 = OpQuantizeToF16 %f32vec2 %f32vec2_01
601 )";
602 
603   CompileSuccessfully(GenerateShaderCode(body).c_str());
604   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
605 }
606 
TEST_F(ValidateConversion,QuantizeToF16WrongResultType)607 TEST_F(ValidateConversion, QuantizeToF16WrongResultType) {
608   const std::string body = R"(
609 %val = OpQuantizeToF16 %u32 %f32_1
610 )";
611 
612   CompileSuccessfully(GenerateShaderCode(body).c_str());
613   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
614   EXPECT_THAT(
615       getDiagnosticString(),
616       HasSubstr("Expected 32-bit float scalar or vector type as Result Type: "
617                 "QuantizeToF16"));
618 }
619 
TEST_F(ValidateConversion,QuantizeToF16WrongResultTypeBitWidth)620 TEST_F(ValidateConversion, QuantizeToF16WrongResultTypeBitWidth) {
621   const std::string body = R"(
622 %val = OpQuantizeToF16 %u64 %f64_1
623 )";
624 
625   CompileSuccessfully(GenerateShaderCode(body).c_str());
626   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
627   EXPECT_THAT(
628       getDiagnosticString(),
629       HasSubstr("Expected 32-bit float scalar or vector type as Result Type: "
630                 "QuantizeToF16"));
631 }
632 
TEST_F(ValidateConversion,QuantizeToF16WrongInputType)633 TEST_F(ValidateConversion, QuantizeToF16WrongInputType) {
634   const std::string body = R"(
635 %val = OpQuantizeToF16 %f32 %f64_1
636 )";
637 
638   CompileSuccessfully(GenerateShaderCode(body).c_str());
639   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
640   EXPECT_THAT(
641       getDiagnosticString(),
642       HasSubstr(
643           "Expected input type to be equal to Result Type: QuantizeToF16"));
644 }
645 
TEST_F(ValidateConversion,ConvertPtrToUSuccess)646 TEST_F(ValidateConversion, ConvertPtrToUSuccess) {
647   const std::string body = R"(
648 %ptr = OpVariable %f32ptr_func Function
649 %val1 = OpConvertPtrToU %u32 %ptr
650 %val2 = OpConvertPtrToU %u64 %ptr
651 )";
652 
653   CompileSuccessfully(GenerateKernelCode(body).c_str());
654   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
655 }
656 
TEST_F(ValidateConversion,ConvertPtrToUWrongResultType)657 TEST_F(ValidateConversion, ConvertPtrToUWrongResultType) {
658   const std::string body = R"(
659 %ptr = OpVariable %f32ptr_func Function
660 %val = OpConvertPtrToU %f32 %ptr
661 )";
662 
663   CompileSuccessfully(GenerateKernelCode(body).c_str());
664   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
665   EXPECT_THAT(getDiagnosticString(),
666               HasSubstr("Expected unsigned int scalar type as Result Type: "
667                         "ConvertPtrToU"));
668 }
669 
TEST_F(ValidateConversion,ConvertPtrToUNotPointer)670 TEST_F(ValidateConversion, ConvertPtrToUNotPointer) {
671   const std::string body = R"(
672 %val = OpConvertPtrToU %u32 %f32_1
673 )";
674 
675   CompileSuccessfully(GenerateKernelCode(body).c_str());
676   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
677   EXPECT_THAT(getDiagnosticString(),
678               HasSubstr("Expected input to be a pointer: ConvertPtrToU"));
679 }
680 
TEST_F(ValidateConversion,SatConvertSToUSuccess)681 TEST_F(ValidateConversion, SatConvertSToUSuccess) {
682   const std::string body = R"(
683 %val1 = OpSatConvertSToU %u32 %u64_2
684 %val2 = OpSatConvertSToU %u64 %u32_1
685 %val3 = OpSatConvertSToU %u64vec2 %u32vec2_12
686 )";
687 
688   CompileSuccessfully(GenerateKernelCode(body).c_str());
689   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
690 }
691 
TEST_F(ValidateConversion,SatConvertSToUWrongResultType)692 TEST_F(ValidateConversion, SatConvertSToUWrongResultType) {
693   const std::string body = R"(
694 %val = OpSatConvertSToU %f32 %u32_1
695 )";
696 
697   CompileSuccessfully(GenerateKernelCode(body).c_str());
698   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
699   EXPECT_THAT(getDiagnosticString(),
700               HasSubstr("Expected int scalar or vector type as Result Type: "
701                         "SatConvertSToU"));
702 }
703 
TEST_F(ValidateConversion,SatConvertSToUWrongInputType)704 TEST_F(ValidateConversion, SatConvertSToUWrongInputType) {
705   const std::string body = R"(
706 %val = OpSatConvertSToU %u32 %f32_1
707 )";
708 
709   CompileSuccessfully(GenerateKernelCode(body).c_str());
710   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
711   EXPECT_THAT(
712       getDiagnosticString(),
713       HasSubstr("Expected int scalar or vector as input: SatConvertSToU"));
714 }
715 
TEST_F(ValidateConversion,SatConvertSToUDifferentDimension)716 TEST_F(ValidateConversion, SatConvertSToUDifferentDimension) {
717   const std::string body = R"(
718 %val = OpSatConvertSToU %u32 %u32vec2_12
719 )";
720 
721   CompileSuccessfully(GenerateKernelCode(body).c_str());
722   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
723   EXPECT_THAT(
724       getDiagnosticString(),
725       HasSubstr("Expected input to have the same dimension as Result Type: "
726                 "SatConvertSToU"));
727 }
728 
TEST_F(ValidateConversion,ConvertUToPtrSuccess)729 TEST_F(ValidateConversion, ConvertUToPtrSuccess) {
730   const std::string body = R"(
731 %val1 = OpConvertUToPtr %f32ptr_func %u32_1
732 %val2 = OpConvertUToPtr %f32ptr_func %u64_1
733 )";
734 
735   CompileSuccessfully(GenerateKernelCode(body).c_str());
736   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
737 }
738 
TEST_F(ValidateConversion,ConvertUToPtrWrongResultType)739 TEST_F(ValidateConversion, ConvertUToPtrWrongResultType) {
740   const std::string body = R"(
741 %val = OpConvertUToPtr %f32 %u32_1
742 )";
743 
744   CompileSuccessfully(GenerateKernelCode(body).c_str());
745   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
746   EXPECT_THAT(getDiagnosticString(),
747               HasSubstr("Expected Result Type to be a pointer: ConvertUToPtr"));
748 }
749 
TEST_F(ValidateConversion,ConvertUToPtrNotInt)750 TEST_F(ValidateConversion, ConvertUToPtrNotInt) {
751   const std::string body = R"(
752 %val = OpConvertUToPtr %f32ptr_func %f32_1
753 )";
754 
755   CompileSuccessfully(GenerateKernelCode(body).c_str());
756   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
757   EXPECT_THAT(getDiagnosticString(),
758               HasSubstr("Expected int scalar as input: ConvertUToPtr"));
759 }
760 
TEST_F(ValidateConversion,ConvertUToPtrNotIntScalar)761 TEST_F(ValidateConversion, ConvertUToPtrNotIntScalar) {
762   const std::string body = R"(
763 %val = OpConvertUToPtr %f32ptr_func %u32vec2_12
764 )";
765 
766   CompileSuccessfully(GenerateKernelCode(body).c_str());
767   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
768   EXPECT_THAT(getDiagnosticString(),
769               HasSubstr("Expected int scalar as input: ConvertUToPtr"));
770 }
771 
TEST_F(ValidateConversion,PtrCastToGenericSuccess)772 TEST_F(ValidateConversion, PtrCastToGenericSuccess) {
773   const std::string body = R"(
774 %ptr_func = OpVariable %f32ptr_func Function
775 %val = OpPtrCastToGeneric %f32ptr_gen %ptr_func
776 )";
777 
778   CompileSuccessfully(GenerateKernelCode(body).c_str());
779   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
780 }
781 
TEST_F(ValidateConversion,PtrCastToGenericWrongResultType)782 TEST_F(ValidateConversion, PtrCastToGenericWrongResultType) {
783   const std::string body = R"(
784 %ptr_func = OpVariable %f32ptr_func Function
785 %val = OpPtrCastToGeneric %f32 %ptr_func
786 )";
787 
788   CompileSuccessfully(GenerateKernelCode(body).c_str());
789   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
790   EXPECT_THAT(
791       getDiagnosticString(),
792       HasSubstr("Expected Result Type to be a pointer: PtrCastToGeneric"));
793 }
794 
TEST_F(ValidateConversion,PtrCastToGenericWrongResultStorageClass)795 TEST_F(ValidateConversion, PtrCastToGenericWrongResultStorageClass) {
796   const std::string body = R"(
797 %ptr_func = OpVariable %f32ptr_func Function
798 %val = OpPtrCastToGeneric %f32ptr_func %ptr_func
799 )";
800 
801   CompileSuccessfully(GenerateKernelCode(body).c_str());
802   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
803   EXPECT_THAT(getDiagnosticString(),
804               HasSubstr("Expected Result Type to have storage class Generic: "
805                         "PtrCastToGeneric"));
806 }
807 
TEST_F(ValidateConversion,PtrCastToGenericWrongInputType)808 TEST_F(ValidateConversion, PtrCastToGenericWrongInputType) {
809   const std::string body = R"(
810 %ptr_func = OpVariable %f32ptr_func Function
811 %val = OpPtrCastToGeneric %f32ptr_gen %f32
812 )";
813 
814   CompileSuccessfully(GenerateKernelCode(body).c_str());
815   ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
816   EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 4[%float] cannot be a "
817                                                "type"));
818 }
819 
TEST_F(ValidateConversion,PtrCastToGenericWrongInputStorageClass)820 TEST_F(ValidateConversion, PtrCastToGenericWrongInputStorageClass) {
821   const std::string body = R"(
822 %val = OpPtrCastToGeneric %f32ptr_gen %f32inp
823 )";
824 
825   CompileSuccessfully(GenerateKernelCode(body).c_str());
826   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
827   EXPECT_THAT(getDiagnosticString(),
828               HasSubstr("Expected input to have storage class Workgroup, "
829                         "CrossWorkgroup or Function: PtrCastToGeneric"));
830 }
831 
TEST_F(ValidateConversion,PtrCastToGenericPointToDifferentType)832 TEST_F(ValidateConversion, PtrCastToGenericPointToDifferentType) {
833   const std::string body = R"(
834 %ptr_func = OpVariable %u32ptr_func Function
835 %val = OpPtrCastToGeneric %f32ptr_gen %ptr_func
836 )";
837 
838   CompileSuccessfully(GenerateKernelCode(body).c_str());
839   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
840   EXPECT_THAT(
841       getDiagnosticString(),
842       HasSubstr("Expected input and Result Type to point to the same type: "
843                 "PtrCastToGeneric"));
844 }
845 
TEST_F(ValidateConversion,GenericCastToPtrSuccess)846 TEST_F(ValidateConversion, GenericCastToPtrSuccess) {
847   const std::string body = R"(
848 %ptr_func = OpVariable %f32ptr_func Function
849 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
850 %ptr_func2 = OpGenericCastToPtr %f32ptr_func %ptr_gen
851 %ptr_wg = OpGenericCastToPtr %f32ptr_wg %ptr_gen
852 %ptr_cwg = OpGenericCastToPtr %f32ptr_cwg %ptr_gen
853 )";
854 
855   CompileSuccessfully(GenerateKernelCode(body).c_str());
856   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
857 }
858 
TEST_F(ValidateConversion,GenericCastToPtrWrongResultType)859 TEST_F(ValidateConversion, GenericCastToPtrWrongResultType) {
860   const std::string body = R"(
861 %ptr_func = OpVariable %f32ptr_func Function
862 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
863 %ptr_func2 = OpGenericCastToPtr %f32 %ptr_gen
864 )";
865 
866   CompileSuccessfully(GenerateKernelCode(body).c_str());
867   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
868   EXPECT_THAT(
869       getDiagnosticString(),
870       HasSubstr("Expected Result Type to be a pointer: GenericCastToPtr"));
871 }
872 
TEST_F(ValidateConversion,GenericCastToPtrWrongResultStorageClass)873 TEST_F(ValidateConversion, GenericCastToPtrWrongResultStorageClass) {
874   const std::string body = R"(
875 %ptr_func = OpVariable %f32ptr_func Function
876 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
877 %ptr_func2 = OpGenericCastToPtr %f32ptr_gen %ptr_gen
878 )";
879 
880   CompileSuccessfully(GenerateKernelCode(body).c_str());
881   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
882   EXPECT_THAT(getDiagnosticString(),
883               HasSubstr("Expected Result Type to have storage class Workgroup, "
884                         "CrossWorkgroup or Function: GenericCastToPtr"));
885 }
886 
TEST_F(ValidateConversion,GenericCastToPtrWrongInputType)887 TEST_F(ValidateConversion, GenericCastToPtrWrongInputType) {
888   const std::string body = R"(
889 %ptr_func = OpVariable %f32ptr_func Function
890 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
891 %ptr_func2 = OpGenericCastToPtr %f32ptr_func %f32_1
892 )";
893 
894   CompileSuccessfully(GenerateKernelCode(body).c_str());
895   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
896   EXPECT_THAT(getDiagnosticString(),
897               HasSubstr("Expected input to be a pointer: GenericCastToPtr"));
898 }
899 
TEST_F(ValidateConversion,GenericCastToPtrWrongInputStorageClass)900 TEST_F(ValidateConversion, GenericCastToPtrWrongInputStorageClass) {
901   const std::string body = R"(
902 %ptr_func = OpVariable %f32ptr_func Function
903 %ptr_func2 = OpGenericCastToPtr %f32ptr_func %ptr_func
904 )";
905 
906   CompileSuccessfully(GenerateKernelCode(body).c_str());
907   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
908   EXPECT_THAT(getDiagnosticString(),
909               HasSubstr("Expected input to have storage class Generic: "
910                         "GenericCastToPtr"));
911 }
912 
TEST_F(ValidateConversion,GenericCastToPtrPointToDifferentType)913 TEST_F(ValidateConversion, GenericCastToPtrPointToDifferentType) {
914   const std::string body = R"(
915 %ptr_func = OpVariable %f32ptr_func Function
916 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
917 %ptr_func2 = OpGenericCastToPtr %u32ptr_func %ptr_gen
918 )";
919 
920   CompileSuccessfully(GenerateKernelCode(body).c_str());
921   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
922   EXPECT_THAT(
923       getDiagnosticString(),
924       HasSubstr("Expected input and Result Type to point to the same type: "
925                 "GenericCastToPtr"));
926 }
927 
TEST_F(ValidateConversion,GenericCastToPtrExplicitSuccess)928 TEST_F(ValidateConversion, GenericCastToPtrExplicitSuccess) {
929   const std::string body = R"(
930 %ptr_func = OpVariable %f32ptr_func Function
931 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
932 %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %ptr_gen Function
933 %ptr_wg = OpGenericCastToPtrExplicit %f32ptr_wg %ptr_gen Workgroup
934 %ptr_cwg = OpGenericCastToPtrExplicit %f32ptr_cwg %ptr_gen CrossWorkgroup
935 )";
936 
937   CompileSuccessfully(GenerateKernelCode(body).c_str());
938   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
939 }
940 
TEST_F(ValidateConversion,GenericCastToPtrExplicitWrongResultType)941 TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongResultType) {
942   const std::string body = R"(
943 %ptr_func = OpVariable %f32ptr_func Function
944 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
945 %ptr_func2 = OpGenericCastToPtrExplicit %f32 %ptr_gen Function
946 )";
947 
948   CompileSuccessfully(GenerateKernelCode(body).c_str());
949   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
950   EXPECT_THAT(
951       getDiagnosticString(),
952       HasSubstr(
953           "Expected Result Type to be a pointer: GenericCastToPtrExplicit"));
954 }
955 
TEST_F(ValidateConversion,GenericCastToPtrExplicitResultStorageClassDiffers)956 TEST_F(ValidateConversion, GenericCastToPtrExplicitResultStorageClassDiffers) {
957   const std::string body = R"(
958 %ptr_func = OpVariable %f32ptr_func Function
959 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
960 %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %ptr_gen Workgroup
961 )";
962 
963   CompileSuccessfully(GenerateKernelCode(body).c_str());
964   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
965   EXPECT_THAT(getDiagnosticString(),
966               HasSubstr("Expected Result Type to be of target storage class: "
967                         "GenericCastToPtrExplicit"));
968 }
969 
TEST_F(ValidateConversion,GenericCastToPtrExplicitWrongResultStorageClass)970 TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongResultStorageClass) {
971   const std::string body = R"(
972 %ptr_func = OpVariable %f32ptr_func Function
973 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
974 %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_gen %ptr_gen Generic
975 )";
976 
977   CompileSuccessfully(GenerateKernelCode(body).c_str());
978   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
979   EXPECT_THAT(
980       getDiagnosticString(),
981       HasSubstr("Expected target storage class to be Workgroup, "
982                 "CrossWorkgroup or Function: GenericCastToPtrExplicit"));
983 }
984 
TEST_F(ValidateConversion,GenericCastToPtrExplicitWrongInputType)985 TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongInputType) {
986   const std::string body = R"(
987 %ptr_func = OpVariable %f32ptr_func Function
988 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
989 %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %f32_1 Function
990 )";
991 
992   CompileSuccessfully(GenerateKernelCode(body).c_str());
993   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
994   EXPECT_THAT(
995       getDiagnosticString(),
996       HasSubstr("Expected input to be a pointer: GenericCastToPtrExplicit"));
997 }
998 
TEST_F(ValidateConversion,GenericCastToPtrExplicitWrongInputStorageClass)999 TEST_F(ValidateConversion, GenericCastToPtrExplicitWrongInputStorageClass) {
1000   const std::string body = R"(
1001 %ptr_func = OpVariable %f32ptr_func Function
1002 %ptr_func2 = OpGenericCastToPtrExplicit %f32ptr_func %ptr_func Function
1003 )";
1004 
1005   CompileSuccessfully(GenerateKernelCode(body).c_str());
1006   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1007   EXPECT_THAT(getDiagnosticString(),
1008               HasSubstr("Expected input to have storage class Generic: "
1009                         "GenericCastToPtrExplicit"));
1010 }
1011 
TEST_F(ValidateConversion,GenericCastToPtrExplicitPointToDifferentType)1012 TEST_F(ValidateConversion, GenericCastToPtrExplicitPointToDifferentType) {
1013   const std::string body = R"(
1014 %ptr_func = OpVariable %f32ptr_func Function
1015 %ptr_gen = OpPtrCastToGeneric %f32ptr_gen %ptr_func
1016 %ptr_func2 = OpGenericCastToPtrExplicit %u32ptr_func %ptr_gen Function
1017 )";
1018 
1019   CompileSuccessfully(GenerateKernelCode(body).c_str());
1020   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1021   EXPECT_THAT(
1022       getDiagnosticString(),
1023       HasSubstr("Expected input and Result Type to point to the same type: "
1024                 "GenericCastToPtrExplicit"));
1025 }
1026 
TEST_F(ValidateConversion,CoopMatConversionSuccess)1027 TEST_F(ValidateConversion, CoopMatConversionSuccess) {
1028   const std::string body =
1029       R"(
1030 OpCapability Shader
1031 OpCapability Float16
1032 OpCapability Int16
1033 OpCapability CooperativeMatrixNV
1034 OpExtension "SPV_NV_cooperative_matrix"
1035 OpMemoryModel Logical GLSL450
1036 OpEntryPoint GLCompute %main "main"
1037 %void = OpTypeVoid
1038 %func = OpTypeFunction %void
1039 %bool = OpTypeBool
1040 %f16 = OpTypeFloat 16
1041 %f32 = OpTypeFloat 32
1042 %u16 = OpTypeInt 16 0
1043 %u32 = OpTypeInt 32 0
1044 %s16 = OpTypeInt 16 1
1045 %s32 = OpTypeInt 32 1
1046 
1047 %u32_8 = OpConstant %u32 8
1048 %subgroup = OpConstant %u32 3
1049 
1050 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
1051 %f32mat = OpTypeCooperativeMatrixNV %f32 %subgroup %u32_8 %u32_8
1052 %u16mat = OpTypeCooperativeMatrixNV %u16 %subgroup %u32_8 %u32_8
1053 %u32mat = OpTypeCooperativeMatrixNV %u32 %subgroup %u32_8 %u32_8
1054 %s16mat = OpTypeCooperativeMatrixNV %s16 %subgroup %u32_8 %u32_8
1055 %s32mat = OpTypeCooperativeMatrixNV %s32 %subgroup %u32_8 %u32_8
1056 
1057 %f16_1 = OpConstant %f16 1
1058 %f32_1 = OpConstant %f32 1
1059 %u16_1 = OpConstant %u16 1
1060 %u32_1 = OpConstant %u32 1
1061 %s16_1 = OpConstant %s16 1
1062 %s32_1 = OpConstant %s32 1
1063 
1064 %f16mat_1 = OpConstantComposite %f16mat %f16_1
1065 %f32mat_1 = OpConstantComposite %f32mat %f32_1
1066 %u16mat_1 = OpConstantComposite %u16mat %u16_1
1067 %u32mat_1 = OpConstantComposite %u32mat %u32_1
1068 %s16mat_1 = OpConstantComposite %s16mat %s16_1
1069 %s32mat_1 = OpConstantComposite %s32mat %s32_1
1070 
1071 %main = OpFunction %void None %func
1072 %main_entry = OpLabel
1073 
1074 %val11 = OpConvertFToU %u16mat %f16mat_1
1075 %val12 = OpConvertFToU %u32mat %f16mat_1
1076 %val13 = OpConvertFToS %s16mat %f16mat_1
1077 %val14 = OpConvertFToS %s32mat %f16mat_1
1078 %val15 = OpFConvert %f32mat %f16mat_1
1079 
1080 %val21 = OpConvertFToU %u16mat %f32mat_1
1081 %val22 = OpConvertFToU %u32mat %f32mat_1
1082 %val23 = OpConvertFToS %s16mat %f32mat_1
1083 %val24 = OpConvertFToS %s32mat %f32mat_1
1084 %val25 = OpFConvert %f16mat %f32mat_1
1085 
1086 %val31 = OpConvertUToF %f16mat %u16mat_1
1087 %val32 = OpConvertUToF %f32mat %u16mat_1
1088 %val33 = OpUConvert %u32mat %u16mat_1
1089 %val34 = OpSConvert %s32mat %u16mat_1
1090 
1091 %val41 = OpConvertSToF %f16mat %s16mat_1
1092 %val42 = OpConvertSToF %f32mat %s16mat_1
1093 %val43 = OpUConvert %u32mat %s16mat_1
1094 %val44 = OpSConvert %s32mat %s16mat_1
1095 
1096 OpReturn
1097 OpFunctionEnd)";
1098 
1099   CompileSuccessfully(body.c_str());
1100   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1101 }
1102 
TEST_F(ValidateConversion,CoopMatConversionShapesMismatchFail)1103 TEST_F(ValidateConversion, CoopMatConversionShapesMismatchFail) {
1104   const std::string body =
1105       R"(
1106 OpCapability Shader
1107 OpCapability Float16
1108 OpCapability Int16
1109 OpCapability CooperativeMatrixNV
1110 OpExtension "SPV_NV_cooperative_matrix"
1111 OpMemoryModel Logical GLSL450
1112 OpEntryPoint GLCompute %main "main"
1113 %void = OpTypeVoid
1114 %func = OpTypeFunction %void
1115 %bool = OpTypeBool
1116 %f16 = OpTypeFloat 16
1117 %f32 = OpTypeFloat 32
1118 %u16 = OpTypeInt 16 0
1119 %u32 = OpTypeInt 32 0
1120 %s16 = OpTypeInt 16 1
1121 %s32 = OpTypeInt 32 1
1122 
1123 %u32_8 = OpConstant %u32 8
1124 %u32_4 = OpConstant %u32 4
1125 %subgroup = OpConstant %u32 3
1126 
1127 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
1128 %f32mat = OpTypeCooperativeMatrixNV %f32 %subgroup %u32_4 %u32_4
1129 
1130 %f16_1 = OpConstant %f16 1
1131 
1132 %f16mat_1 = OpConstantComposite %f16mat %f16_1
1133 
1134 %main = OpFunction %void None %func
1135 %main_entry = OpLabel
1136 
1137 %val15 = OpFConvert %f32mat %f16mat_1
1138 
1139 OpReturn
1140 OpFunctionEnd)";
1141 
1142   CompileSuccessfully(body.c_str());
1143   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1144   EXPECT_THAT(
1145       getDiagnosticString(),
1146       HasSubstr(
1147           "Expected rows of Matrix type and Result Type to be identical"));
1148 }
1149 
TEST_F(ValidateConversion,CoopMatConversionShapesMismatchPass)1150 TEST_F(ValidateConversion, CoopMatConversionShapesMismatchPass) {
1151   const std::string body =
1152       R"(
1153 OpCapability Shader
1154 OpCapability Float16
1155 OpCapability Int16
1156 OpCapability CooperativeMatrixNV
1157 OpExtension "SPV_NV_cooperative_matrix"
1158 OpMemoryModel Logical GLSL450
1159 OpEntryPoint GLCompute %main "main"
1160 %void = OpTypeVoid
1161 %func = OpTypeFunction %void
1162 %bool = OpTypeBool
1163 %f16 = OpTypeFloat 16
1164 %f32 = OpTypeFloat 32
1165 %u16 = OpTypeInt 16 0
1166 %u32 = OpTypeInt 32 0
1167 %s16 = OpTypeInt 16 1
1168 %s32 = OpTypeInt 32 1
1169 
1170 %u32_8 = OpConstant %u32 8
1171 %u32_4 = OpSpecConstant %u32 4
1172 %subgroup = OpConstant %u32 3
1173 
1174 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
1175 %f32mat = OpTypeCooperativeMatrixNV %f32 %subgroup %u32_4 %u32_4
1176 
1177 %f16_1 = OpConstant %f16 1
1178 
1179 %f16mat_1 = OpConstantComposite %f16mat %f16_1
1180 
1181 %main = OpFunction %void None %func
1182 %main_entry = OpLabel
1183 
1184 %val15 = OpFConvert %f32mat %f16mat_1
1185 
1186 OpReturn
1187 OpFunctionEnd)";
1188 
1189   CompileSuccessfully(body.c_str());
1190   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1191 }
1192 
TEST_F(ValidateConversion,BitcastSuccess)1193 TEST_F(ValidateConversion, BitcastSuccess) {
1194   const std::string body = R"(
1195 %ptr = OpVariable %f32ptr_func Function
1196 %val1 = OpBitcast %u32 %ptr
1197 %val2 = OpBitcast %u64 %ptr
1198 %val3 = OpBitcast %f32ptr_func %u32_1
1199 %val4 = OpBitcast %f32ptr_wg %u64_1
1200 %val5 = OpBitcast %f32 %u32_1
1201 %val6 = OpBitcast %f32vec2 %u32vec2_12
1202 %val7 = OpBitcast %f32vec2 %u64_1
1203 %val8 = OpBitcast %f64 %u32vec2_12
1204 %val9 = OpBitcast %f32vec4 %f64vec2_12
1205 )";
1206 
1207   CompileSuccessfully(GenerateKernelCode(body).c_str());
1208   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1209 }
1210 
TEST_F(ValidateConversion,BitcastSuccessSPV1p5)1211 TEST_F(ValidateConversion, BitcastSuccessSPV1p5) {
1212   const std::string body = R"(
1213 %ptr = OpVariable %f32ptr_func Function
1214 %val1 = OpBitcast %u32 %ptr
1215 %val2 = OpBitcast %u64 %ptr
1216 %val3 = OpBitcast %f32ptr_func %u32_1
1217 %val4 = OpBitcast %f32ptr_wg %u64_1
1218 %val5 = OpBitcast %f32 %u32_1
1219 %val6 = OpBitcast %f32vec2 %u32vec2_12
1220 %val7 = OpBitcast %f32vec2 %u64_1
1221 %val8 = OpBitcast %f64 %u32vec2_12
1222 %val9 = OpBitcast %f32vec4 %f64vec2_12
1223 %val10 = OpBitcast %u32ptr_func %u32vec2_01
1224 %val11 = OpBitcast %u32vec2 %ptr
1225 )";
1226 
1227   CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
1228   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
1229 }
1230 
TEST_F(ValidateConversion,BitcastSuccessPhysicalStorageBufferKHR)1231 TEST_F(ValidateConversion, BitcastSuccessPhysicalStorageBufferKHR) {
1232   const std::string body = R"(
1233 %ptr = OpVariable %f32ptr_func Function
1234 %val1 = OpBitcast %u32 %ptr
1235 %val2 = OpBitcast %u64 %ptr
1236 %val3 = OpBitcast %f32ptr_func %u32_1
1237 %val4 = OpBitcast %f32ptr_wg %u64_1
1238 %val5 = OpBitcast %f32 %u32_1
1239 %val6 = OpBitcast %f32vec2 %u32vec2_12
1240 %val7 = OpBitcast %f32vec2 %u64_1
1241 %val8 = OpBitcast %f64 %u32vec2_12
1242 %val9 = OpBitcast %f32vec4 %f64vec2_12
1243 %val10 = OpBitcast %u32ptr_func %u32vec2_01
1244 %val11 = OpBitcast %u32vec2 %ptr
1245 )";
1246 
1247   CompileSuccessfully(
1248       GenerateKernelCode(body,
1249                          "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
1250           .c_str());
1251   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1252 }
1253 
TEST_F(ValidateConversion,BitcastInputHasNoType)1254 TEST_F(ValidateConversion, BitcastInputHasNoType) {
1255   const std::string body = R"(
1256 %val = OpBitcast %u32 %f32
1257 )";
1258 
1259   CompileSuccessfully(GenerateKernelCode(body).c_str());
1260   ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1261   EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 4[%float] cannot be a "
1262                                                "type"));
1263 }
1264 
TEST_F(ValidateConversion,BitcastWrongResultType)1265 TEST_F(ValidateConversion, BitcastWrongResultType) {
1266   const std::string body = R"(
1267 %val = OpBitcast %bool %f32_1
1268 )";
1269 
1270   CompileSuccessfully(GenerateKernelCode(body).c_str());
1271   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1272   EXPECT_THAT(
1273       getDiagnosticString(),
1274       HasSubstr("Expected Result Type to be a pointer or int or float vector "
1275                 "or scalar type: Bitcast"));
1276 }
1277 
TEST_F(ValidateConversion,BitcastWrongInputType)1278 TEST_F(ValidateConversion, BitcastWrongInputType) {
1279   const std::string body = R"(
1280 %val = OpBitcast %u32 %true
1281 )";
1282 
1283   CompileSuccessfully(GenerateKernelCode(body).c_str());
1284   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1285   EXPECT_THAT(getDiagnosticString(),
1286               HasSubstr("Expected input to be a pointer or int or float vector "
1287                         "or scalar: Bitcast"));
1288 }
1289 
TEST_F(ValidateConversion,BitcastPtrWrongInputType)1290 TEST_F(ValidateConversion, BitcastPtrWrongInputType) {
1291   const std::string body = R"(
1292 %val = OpBitcast %u32ptr_func %f32_1
1293 )";
1294 
1295   CompileSuccessfully(GenerateKernelCode(body).c_str());
1296   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1297   EXPECT_THAT(getDiagnosticString(),
1298               HasSubstr("Expected input to be a pointer or int scalar if "
1299                         "Result Type is pointer: Bitcast"));
1300 }
1301 
TEST_F(ValidateConversion,BitcastPtrWrongInputTypeSPV1p5)1302 TEST_F(ValidateConversion, BitcastPtrWrongInputTypeSPV1p5) {
1303   const std::string body = R"(
1304 %val = OpBitcast %u32ptr_func %f32_1
1305 )";
1306 
1307   CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
1308   ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1309             ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
1310   EXPECT_THAT(getDiagnosticString(),
1311               HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
1312                         "int vector if Result Type is pointer: Bitcast"));
1313 }
1314 
TEST_F(ValidateConversion,BitcastPtrWrongInputTypePhysicalStorageBufferKHR)1315 TEST_F(ValidateConversion, BitcastPtrWrongInputTypePhysicalStorageBufferKHR) {
1316   const std::string body = R"(
1317 %val = OpBitcast %u32ptr_func %f32_1
1318 )";
1319 
1320   CompileSuccessfully(
1321       GenerateKernelCode(body,
1322                          "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
1323           .c_str());
1324   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1325   EXPECT_THAT(getDiagnosticString(),
1326               HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
1327                         "int vector if Result Type is pointer: Bitcast"));
1328 }
1329 
TEST_F(ValidateConversion,BitcastPtrWrongInputTypeIntVectorSPV1p5)1330 TEST_F(ValidateConversion, BitcastPtrWrongInputTypeIntVectorSPV1p5) {
1331   const std::string body = R"(
1332 %val = OpBitcast %u32ptr_func %u64vec2_01
1333 )";
1334 
1335   CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
1336   ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1337             ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
1338   EXPECT_THAT(getDiagnosticString(),
1339               HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
1340                         "int vector if Result Type is pointer: Bitcast"));
1341 }
1342 
TEST_F(ValidateConversion,BitcastPtrWrongInputTypeIntVectorPhysicalStorageBufferKHR)1343 TEST_F(ValidateConversion,
1344        BitcastPtrWrongInputTypeIntVectorPhysicalStorageBufferKHR) {
1345   const std::string body = R"(
1346 %val = OpBitcast %u32ptr_func %u64vec2_01
1347 )";
1348 
1349   CompileSuccessfully(
1350       GenerateKernelCode(body,
1351                          "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
1352           .c_str());
1353   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1354   EXPECT_THAT(getDiagnosticString(),
1355               HasSubstr("Expected input to be a pointer, int scalar or 32-bit "
1356                         "int vector if Result Type is pointer: Bitcast"));
1357 }
1358 
TEST_F(ValidateConversion,BitcastPtrWrongResultType)1359 TEST_F(ValidateConversion, BitcastPtrWrongResultType) {
1360   const std::string body = R"(
1361 %val = OpBitcast %f32 %f32inp
1362 )";
1363 
1364   CompileSuccessfully(GenerateKernelCode(body).c_str());
1365   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1366   EXPECT_THAT(getDiagnosticString(),
1367               HasSubstr("Pointer can only be converted to another pointer or "
1368                         "int scalar: Bitcast"));
1369 }
1370 
TEST_F(ValidateConversion,BitcastPtrWrongResultTypeSPV1p5)1371 TEST_F(ValidateConversion, BitcastPtrWrongResultTypeSPV1p5) {
1372   const std::string body = R"(
1373 %val = OpBitcast %f32 %f32inp
1374 )";
1375 
1376   CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
1377   ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1378             ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
1379   EXPECT_THAT(getDiagnosticString(),
1380               HasSubstr("Pointer can only be converted to another pointer, int "
1381                         "scalar or 32-bit int vector: Bitcast"));
1382 }
1383 
TEST_F(ValidateConversion,BitcastPtrWrongResultTypePhysicalStorageBufferKHR)1384 TEST_F(ValidateConversion, BitcastPtrWrongResultTypePhysicalStorageBufferKHR) {
1385   const std::string body = R"(
1386 %val = OpBitcast %f32 %f32inp
1387 )";
1388 
1389   CompileSuccessfully(
1390       GenerateKernelCode(body,
1391                          "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
1392           .c_str());
1393   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1394   EXPECT_THAT(getDiagnosticString(),
1395               HasSubstr("Pointer can only be converted to another pointer, int "
1396                         "scalar or 32-bit int vector: Bitcast"));
1397 }
1398 
TEST_F(ValidateConversion,BitcastPtrWrongResultTypeIntVectorSPV1p5)1399 TEST_F(ValidateConversion, BitcastPtrWrongResultTypeIntVectorSPV1p5) {
1400   const std::string body = R"(
1401 %val = OpBitcast %u64vec2 %f32inp
1402 )";
1403 
1404   CompileSuccessfully(GenerateKernelCode(body).c_str(), SPV_ENV_UNIVERSAL_1_5);
1405   ASSERT_EQ(SPV_ERROR_INVALID_DATA,
1406             ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
1407   EXPECT_THAT(getDiagnosticString(),
1408               HasSubstr("Pointer can only be converted to another pointer, int "
1409                         "scalar or 32-bit int vector: Bitcast"));
1410 }
1411 
TEST_F(ValidateConversion,BitcastPtrWrongResultTypeIntVectorPhysicalStorageBufferKHR)1412 TEST_F(ValidateConversion,
1413        BitcastPtrWrongResultTypeIntVectorPhysicalStorageBufferKHR) {
1414   const std::string body = R"(
1415 %val = OpBitcast %u64vec2 %f32inp
1416 )";
1417 
1418   CompileSuccessfully(
1419       GenerateKernelCode(body,
1420                          "\nOpExtension \"SPV_KHR_physical_storage_buffer\"")
1421           .c_str());
1422   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1423   EXPECT_THAT(getDiagnosticString(),
1424               HasSubstr("Pointer can only be converted to another pointer, int "
1425                         "scalar or 32-bit int vector: Bitcast"));
1426 }
1427 
TEST_F(ValidateConversion,BitcastDifferentTotalBitWidth)1428 TEST_F(ValidateConversion, BitcastDifferentTotalBitWidth) {
1429   const std::string body = R"(
1430 %val = OpBitcast %f32 %u64_1
1431 )";
1432 
1433   CompileSuccessfully(GenerateKernelCode(body).c_str());
1434   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1435   EXPECT_THAT(
1436       getDiagnosticString(),
1437       HasSubstr(
1438           "Expected input to have the same total bit width as Result Type: "
1439           "Bitcast"));
1440 }
1441 
TEST_F(ValidateConversion,ConvertUToPtrInputIsAType)1442 TEST_F(ValidateConversion, ConvertUToPtrInputIsAType) {
1443   const std::string spirv = R"(
1444 OpCapability Addresses
1445 OpCapability Shader
1446 OpCapability Linkage
1447 OpMemoryModel Logical GLSL450
1448 %int = OpTypeInt 32 0
1449 %ptr_int = OpTypePointer Function %int
1450 %void = OpTypeVoid
1451 %voidfn = OpTypeFunction %void
1452 %func = OpFunction %void None %voidfn
1453 %entry = OpLabel
1454 %1 = OpConvertUToPtr %ptr_int %int
1455 OpReturn
1456 OpFunctionEnd
1457 )";
1458 
1459   CompileSuccessfully(spirv);
1460   EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1461   EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand 1[%uint] cannot be a "
1462                                                "type"));
1463 }
1464 
TEST_F(ValidateConversion,ConvertUToPtrPSBSuccess)1465 TEST_F(ValidateConversion, ConvertUToPtrPSBSuccess) {
1466   const std::string body = R"(
1467 OpCapability PhysicalStorageBufferAddressesEXT
1468 OpCapability Int64
1469 OpCapability Shader
1470 OpExtension "SPV_EXT_physical_storage_buffer"
1471 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
1472 OpEntryPoint Fragment %main "main"
1473 OpExecutionMode %main OriginUpperLeft
1474 %uint64 = OpTypeInt 64 0
1475 %u64_1 = OpConstant %uint64 1
1476 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
1477 %void = OpTypeVoid
1478 %voidfn = OpTypeFunction %void
1479 %main = OpFunction %void None %voidfn
1480 %entry = OpLabel
1481 %val1 = OpConvertUToPtr %ptr %u64_1
1482 OpReturn
1483 OpFunctionEnd
1484 )";
1485 
1486   CompileSuccessfully(body.c_str());
1487   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1488 }
1489 
TEST_F(ValidateConversion,ConvertUToPtrPSBStorageClass)1490 TEST_F(ValidateConversion, ConvertUToPtrPSBStorageClass) {
1491   const std::string body = R"(
1492 OpCapability PhysicalStorageBufferAddressesEXT
1493 OpCapability Int64
1494 OpCapability Shader
1495 OpExtension "SPV_EXT_physical_storage_buffer"
1496 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
1497 OpEntryPoint Fragment %main "main"
1498 OpExecutionMode %main OriginUpperLeft
1499 %uint64 = OpTypeInt 64 0
1500 %u64_1 = OpConstant %uint64 1
1501 %ptr = OpTypePointer Function %uint64
1502 %void = OpTypeVoid
1503 %voidfn = OpTypeFunction %void
1504 %main = OpFunction %void None %voidfn
1505 %entry = OpLabel
1506 %val1 = OpConvertUToPtr %ptr %u64_1
1507 %val2 = OpConvertPtrToU %uint64 %val1
1508 OpReturn
1509 OpFunctionEnd
1510 )";
1511 
1512   CompileSuccessfully(body.c_str());
1513   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1514   EXPECT_THAT(getDiagnosticString(),
1515               HasSubstr("Pointer storage class must be "
1516                         "PhysicalStorageBufferEXT: ConvertUToPtr"));
1517 }
1518 
TEST_F(ValidateConversion,ConvertPtrToUPSBSuccess)1519 TEST_F(ValidateConversion, ConvertPtrToUPSBSuccess) {
1520   const std::string body = R"(
1521 OpCapability PhysicalStorageBufferAddressesEXT
1522 OpCapability Int64
1523 OpCapability Shader
1524 OpExtension "SPV_EXT_physical_storage_buffer"
1525 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
1526 OpEntryPoint Fragment %main "main"
1527 OpExecutionMode %main OriginUpperLeft
1528 OpDecorate %val1 RestrictPointerEXT
1529 %uint64 = OpTypeInt 64 0
1530 %u64_1 = OpConstant %uint64 1
1531 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
1532 %pptr_f = OpTypePointer Function %ptr
1533 %void = OpTypeVoid
1534 %voidfn = OpTypeFunction %void
1535 %main = OpFunction %void None %voidfn
1536 %entry = OpLabel
1537 %val1 = OpVariable %pptr_f Function
1538 %val2 = OpLoad %ptr %val1
1539 %val3 = OpConvertPtrToU %uint64 %val2
1540 OpReturn
1541 OpFunctionEnd
1542 )";
1543 
1544   CompileSuccessfully(body.c_str());
1545   ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1546 }
1547 
TEST_F(ValidateConversion,ConvertPtrToUPSBStorageClass)1548 TEST_F(ValidateConversion, ConvertPtrToUPSBStorageClass) {
1549   const std::string body = R"(
1550 OpCapability PhysicalStorageBufferAddressesEXT
1551 OpCapability Int64
1552 OpCapability Shader
1553 OpExtension "SPV_EXT_physical_storage_buffer"
1554 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
1555 OpEntryPoint Fragment %main "main"
1556 OpExecutionMode %main OriginUpperLeft
1557 %uint64 = OpTypeInt 64 0
1558 %u64_1 = OpConstant %uint64 1
1559 %ptr = OpTypePointer Function %uint64
1560 %void = OpTypeVoid
1561 %voidfn = OpTypeFunction %void
1562 %main = OpFunction %void None %voidfn
1563 %entry = OpLabel
1564 %val1 = OpVariable %ptr Function
1565 %val2 = OpConvertPtrToU %uint64 %val1
1566 OpReturn
1567 OpFunctionEnd
1568 )";
1569 
1570   CompileSuccessfully(body.c_str());
1571   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1572   EXPECT_THAT(getDiagnosticString(),
1573               HasSubstr("Pointer storage class must be "
1574                         "PhysicalStorageBufferEXT: ConvertPtrToU"));
1575 }
1576 
1577 using ValidateSmallConversions = spvtest::ValidateBase<std::string>;
1578 
GetSmallConversionsCodeGenerator()1579 CodeGenerator GetSmallConversionsCodeGenerator() {
1580   CodeGenerator generator;
1581   generator.capabilities_ = R"(
1582 OpCapability Shader
1583 OpCapability Linkage
1584 OpCapability UniformAndStorageBuffer16BitAccess
1585 OpCapability UniformAndStorageBuffer8BitAccess
1586 )";
1587   generator.extensions_ = R"(
1588 OpExtension "SPV_KHR_16bit_storage"
1589 OpExtension "SPV_KHR_8bit_storage"
1590 )";
1591   generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
1592   generator.before_types_ = R"(
1593 OpDecorate %char_block Block
1594 OpMemberDecorate %char_block 0 Offset 0
1595 OpDecorate %short_block Block
1596 OpMemberDecorate %short_block 0 Offset 0
1597 OpDecorate %half_block Block
1598 OpMemberDecorate %half_block 0 Offset 0
1599 OpDecorate %int_block Block
1600 OpMemberDecorate %int_block 0 Offset 0
1601 OpDecorate %float_block Block
1602 OpMemberDecorate %float_block 0 Offset 0
1603 )";
1604   generator.types_ = R"(
1605 %void = OpTypeVoid
1606 %int = OpTypeInt 32 0
1607 %int_0 = OpConstant %int 0
1608 %int_1 = OpConstant %int 1
1609 %int2 = OpTypeVector %int 2
1610 %float = OpTypeFloat 32
1611 %float_0 = OpConstant %float 0
1612 %float2 = OpTypeVector %float 2
1613 %char = OpTypeInt 8 0
1614 %char2 = OpTypeVector %char 2
1615 %short = OpTypeInt 16 0
1616 %short2 = OpTypeVector %short 2
1617 %half = OpTypeFloat 16
1618 %half2 = OpTypeVector %half 2
1619 %char_block = OpTypeStruct %char2
1620 %short_block = OpTypeStruct %short2
1621 %half_block = OpTypeStruct %half2
1622 %int_block = OpTypeStruct %int2
1623 %float_block = OpTypeStruct %float2
1624 %ptr_ssbo_char_block = OpTypePointer StorageBuffer %char_block
1625 %ptr_ssbo_char2 = OpTypePointer StorageBuffer %char2
1626 %ptr_ssbo_char = OpTypePointer StorageBuffer %char
1627 %ptr_ssbo_short_block = OpTypePointer StorageBuffer %short_block
1628 %ptr_ssbo_short2 = OpTypePointer StorageBuffer %short2
1629 %ptr_ssbo_short = OpTypePointer StorageBuffer %short
1630 %ptr_ssbo_half_block = OpTypePointer StorageBuffer %half_block
1631 %ptr_ssbo_half2 = OpTypePointer StorageBuffer %half2
1632 %ptr_ssbo_half = OpTypePointer StorageBuffer %half
1633 %ptr_ssbo_int_block = OpTypePointer StorageBuffer %int_block
1634 %ptr_ssbo_int2 = OpTypePointer StorageBuffer %int2
1635 %ptr_ssbo_int = OpTypePointer StorageBuffer %int
1636 %ptr_ssbo_float_block = OpTypePointer StorageBuffer %float_block
1637 %ptr_ssbo_float2 = OpTypePointer StorageBuffer %float2
1638 %ptr_ssbo_float = OpTypePointer StorageBuffer %float
1639 %void_fn = OpTypeFunction %void
1640 %char_var = OpVariable %ptr_ssbo_char_block StorageBuffer
1641 %short_var = OpVariable %ptr_ssbo_short_block StorageBuffer
1642 %half_var = OpVariable %ptr_ssbo_half_block StorageBuffer
1643 %int_var = OpVariable %ptr_ssbo_int_block StorageBuffer
1644 %float_var = OpVariable %ptr_ssbo_float_block StorageBuffer
1645 )";
1646   generator.after_types_ = R"(
1647 %func = OpFunction %void None %void_fn
1648 %entry = OpLabel
1649 %char2_gep = OpAccessChain %ptr_ssbo_char2 %char_var %int_0
1650 %ld_char2 = OpLoad %char2 %char2_gep
1651 %char_gep = OpAccessChain %ptr_ssbo_char %char_var %int_0 %int_0
1652 %ld_char = OpLoad %char %char_gep
1653 %short2_gep = OpAccessChain %ptr_ssbo_short2 %short_var %int_0
1654 %ld_short2 = OpLoad %short2 %short2_gep
1655 %short_gep = OpAccessChain %ptr_ssbo_short %short_var %int_0 %int_0
1656 %ld_short = OpLoad %short %short_gep
1657 %half2_gep = OpAccessChain %ptr_ssbo_half2 %half_var %int_0
1658 %ld_half2 = OpLoad %half2 %half2_gep
1659 %half_gep = OpAccessChain %ptr_ssbo_half %half_var %int_0 %int_0
1660 %ld_half = OpLoad %half %half_gep
1661 %int2_gep = OpAccessChain %ptr_ssbo_int2 %int_var %int_0
1662 %ld_int2 = OpLoad %int2 %int2_gep
1663 %int_gep = OpAccessChain %ptr_ssbo_int %int_var %int_0 %int_0
1664 %ld_int = OpLoad %int %int_gep
1665 %float2_gep = OpAccessChain %ptr_ssbo_float2 %float_var %int_0
1666 %ld_float2 = OpLoad %float2 %float2_gep
1667 %float_gep = OpAccessChain %ptr_ssbo_float %float_var %int_0 %int_0
1668 %ld_float = OpLoad %float %float_gep
1669 )";
1670   generator.add_at_the_end_ = R"(
1671 OpReturn
1672 OpFunctionEnd
1673 )";
1674   return generator;
1675 }
1676 
TEST_P(ValidateSmallConversions,Instruction)1677 TEST_P(ValidateSmallConversions, Instruction) {
1678   CodeGenerator generator = GetSmallConversionsCodeGenerator();
1679   generator.after_types_ += GetParam() + "\n";
1680   CompileSuccessfully(generator.Build(), SPV_ENV_UNIVERSAL_1_3);
1681   EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1682             ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1683   EXPECT_THAT(
1684       getDiagnosticString(),
1685       HasSubstr(
1686           "8- or 16-bit types can only be used with width-only conversions"));
1687 }
1688 
1689 INSTANTIATE_TEST_SUITE_P(SmallConversionInstructions, ValidateSmallConversions,
1690                          Values("%inst = OpConvertFToU %char %ld_float",
1691                                 "%inst = OpConvertFToU %char2 %ld_float2",
1692                                 "%inst = OpConvertFToU %short %ld_float",
1693                                 "%inst = OpConvertFToU %short2 %ld_float2",
1694                                 "%inst = OpConvertFToU %int %ld_half",
1695                                 "%inst = OpConvertFToU %int2 %ld_half2",
1696                                 "%inst = OpConvertFToS %char %ld_float",
1697                                 "%inst = OpConvertFToS %char2 %ld_float2",
1698                                 "%inst = OpConvertFToS %short %ld_float",
1699                                 "%inst = OpConvertFToS %short2 %ld_float2",
1700                                 "%inst = OpConvertFToS %int %ld_half",
1701                                 "%inst = OpConvertFToS %int2 %ld_half2",
1702                                 "%inst = OpConvertSToF %float %ld_char",
1703                                 "%inst = OpConvertSToF %float2 %ld_char2",
1704                                 "%inst = OpConvertSToF %float %ld_short",
1705                                 "%inst = OpConvertSToF %float2 %ld_short2",
1706                                 "%inst = OpConvertSToF %half %ld_int",
1707                                 "%inst = OpConvertSToF %half2 %ld_int2",
1708                                 "%inst = OpConvertUToF %float %ld_char",
1709                                 "%inst = OpConvertUToF %float2 %ld_char2",
1710                                 "%inst = OpConvertUToF %float %ld_short",
1711                                 "%inst = OpConvertUToF %float2 %ld_short2",
1712                                 "%inst = OpConvertUToF %half %ld_int",
1713                                 "%inst = OpConvertUToF %half2 %ld_int2",
1714                                 "%inst = OpBitcast %half %ld_short",
1715                                 "%inst = OpBitcast %half2 %ld_short2",
1716                                 "%inst = OpBitcast %short %ld_half",
1717                                 "%inst = OpBitcast %short2 %ld_half2"));
1718 
1719 }  // namespace
1720 }  // namespace val
1721 }  // namespace spvtools
1722