1 // Copyright (c) 2019 Google LLC.
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 // Validation tests for misc instructions
16 
17 #include <string>
18 #include <vector>
19 
20 #include "gmock/gmock.h"
21 #include "test/unit_spirv.h"
22 #include "test/val/val_fixtures.h"
23 
24 namespace spvtools {
25 namespace val {
26 namespace {
27 
28 using ::testing::Eq;
29 using ::testing::HasSubstr;
30 
31 using ValidateMisc = spvtest::ValidateBase<bool>;
32 
TEST_F(ValidateMisc,UndefRestrictedShort)33 TEST_F(ValidateMisc, UndefRestrictedShort) {
34   const std::string spirv = R"(
35 OpCapability Shader
36 OpCapability Linkage
37 OpCapability StorageBuffer16BitAccess
38 OpExtension "SPV_KHR_16bit_storage"
39 OpMemoryModel Logical GLSL450
40 %short = OpTypeInt 16 0
41 %undef = OpUndef %short
42 )";
43 
44   CompileSuccessfully(spirv);
45   EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
46   EXPECT_THAT(
47       getDiagnosticString(),
48       HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
49 }
50 
TEST_F(ValidateMisc,UndefRestrictedChar)51 TEST_F(ValidateMisc, UndefRestrictedChar) {
52   const std::string spirv = R"(
53 OpCapability Shader
54 OpCapability Linkage
55 OpCapability StorageBuffer8BitAccess
56 OpExtension "SPV_KHR_8bit_storage"
57 OpMemoryModel Logical GLSL450
58 %char = OpTypeInt 8 0
59 %undef = OpUndef %char
60 )";
61 
62   CompileSuccessfully(spirv);
63   EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
64   EXPECT_THAT(
65       getDiagnosticString(),
66       HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
67 }
68 
TEST_F(ValidateMisc,UndefRestrictedHalf)69 TEST_F(ValidateMisc, UndefRestrictedHalf) {
70   const std::string spirv = R"(
71 OpCapability Shader
72 OpCapability Linkage
73 OpCapability StorageBuffer16BitAccess
74 OpExtension "SPV_KHR_16bit_storage"
75 OpMemoryModel Logical GLSL450
76 %half = OpTypeFloat 16
77 %undef = OpUndef %half
78 )";
79 
80   CompileSuccessfully(spirv);
81   EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
82   EXPECT_THAT(
83       getDiagnosticString(),
84       HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
85 }
86 
87 const std::string ShaderClockSpriv = R"(
88 OpCapability Shader
89 OpCapability Int64
90 OpCapability ShaderClockKHR
91 OpExtension "SPV_KHR_shader_clock"
92 %1 = OpExtInstImport "GLSL.std.450"
93 OpMemoryModel Logical GLSL450
94 OpEntryPoint Fragment %main "main"
95 OpExecutionMode %main OriginUpperLeft
96 OpSource GLSL 450
97 OpSourceExtension "GL_ARB_gpu_shader_int64"
98 OpSourceExtension "GL_ARB_shader_clock"
99 OpSourceExtension "GL_EXT_shader_realtime_clock"
100 OpName %main "main"
101 OpName %time1 "time1"
102 %void = OpTypeVoid
103 )";
104 
TEST_F(ValidateMisc,ShaderClockInt64)105 TEST_F(ValidateMisc, ShaderClockInt64) {
106   const std::string spirv = ShaderClockSpriv + R"(
107 %3 = OpTypeFunction %void
108 %uint = OpTypeInt 32 0
109 %_ptr_Function_uint = OpTypePointer Function %uint
110 %uint_3 = OpConstant %uint 3
111 %uint_1 = OpConstant %uint 1
112 %main = OpFunction %void None %3
113 %5 = OpLabel
114 %time1 = OpVariable %_ptr_Function_uint Function
115 %11 = OpReadClockKHR %uint %uint_3
116 OpStore %time1 %11
117 OpReturn
118 OpFunctionEnd)";
119 
120   CompileSuccessfully(spirv);
121   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
122   EXPECT_THAT(getDiagnosticString(), HasSubstr("or 64bit unsigned integer"));
123 }
124 
TEST_F(ValidateMisc,ShaderClockVec2)125 TEST_F(ValidateMisc, ShaderClockVec2) {
126   const std::string spirv = ShaderClockSpriv + R"(
127 %3 = OpTypeFunction %void
128 %ulong = OpTypeInt 64 0
129 %_ptr_Function_ulong = OpTypePointer Function %ulong
130 %uint = OpTypeInt 32 0
131 %uint_3 = OpConstant %uint 3
132 %v2uint = OpTypeVector %ulong 2
133 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
134 %main = OpFunction %void None %3
135 %5 = OpLabel
136 %time1 = OpVariable %_ptr_Function_v2uint Function
137 %15 = OpReadClockKHR %v2uint %uint_3
138 OpStore %time1 %15
139 OpReturn
140 OpFunctionEnd)";
141 
142   CompileSuccessfully(spirv);
143   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
144   EXPECT_THAT(getDiagnosticString(), HasSubstr("vector of two components"));
145 }
146 
TEST_F(ValidateMisc,ShaderClockInvalidScopeValue)147 TEST_F(ValidateMisc, ShaderClockInvalidScopeValue) {
148   const std::string spirv = ShaderClockSpriv + R"(
149 %3 = OpTypeFunction %void
150 %ulong = OpTypeInt 64 0
151 %uint = OpTypeInt 32 0
152 %_ptr_Function_ulong = OpTypePointer Function %ulong
153 %uint_10 = OpConstant %uint 10
154 %uint_1 = OpConstant %uint 1
155 %main = OpFunction %void None %3
156 %5 = OpLabel
157 %time1 = OpVariable %_ptr_Function_ulong Function
158 %11 = OpReadClockKHR %ulong %uint_10
159 OpStore %time1 %11
160 OpReturn
161 OpFunctionEnd)";
162 
163   CompileSuccessfully(spirv);
164   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
165   EXPECT_THAT(getDiagnosticString(), HasSubstr("Invalid scope value"));
166 }
167 
TEST_F(ValidateMisc,ShaderClockSubgroupScope)168 TEST_F(ValidateMisc, ShaderClockSubgroupScope) {
169   const std::string spirv = ShaderClockSpriv + R"(
170 %3 = OpTypeFunction %void
171 %ulong = OpTypeInt 64 0
172 %uint = OpTypeInt 32 0
173 %_ptr_Function_ulong = OpTypePointer Function %ulong
174 %subgroup = OpConstant %uint 3
175 %uint_1 = OpConstant %uint 1
176 %main = OpFunction %void None %3
177 %5 = OpLabel
178 %time1 = OpVariable %_ptr_Function_ulong Function
179 %11 = OpReadClockKHR %ulong %subgroup
180 OpStore %time1 %11
181 OpReturn
182 OpFunctionEnd)";
183 
184   CompileSuccessfully(spirv);
185   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
186 }
187 
TEST_F(ValidateMisc,ShaderClockDeviceScope)188 TEST_F(ValidateMisc, ShaderClockDeviceScope) {
189   const std::string spirv = ShaderClockSpriv + R"(
190 %3 = OpTypeFunction %void
191 %ulong = OpTypeInt 64 0
192 %uint = OpTypeInt 32 0
193 %_ptr_Function_ulong = OpTypePointer Function %ulong
194 %device = OpConstant %uint 1
195 %uint_1 = OpConstant %uint 1
196 %main = OpFunction %void None %3
197 %5 = OpLabel
198 %time1 = OpVariable %_ptr_Function_ulong Function
199 %11 = OpReadClockKHR %ulong %device
200 OpStore %time1 %11
201 OpReturn
202 OpFunctionEnd)";
203 
204   CompileSuccessfully(spirv);
205   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
206 }
207 
TEST_F(ValidateMisc,ShaderClockWorkgroupScope)208 TEST_F(ValidateMisc, ShaderClockWorkgroupScope) {
209   const std::string spirv = ShaderClockSpriv + R"(
210 %3 = OpTypeFunction %void
211 %ulong = OpTypeInt 64 0
212 %uint = OpTypeInt 32 0
213 %_ptr_Function_ulong = OpTypePointer Function %ulong
214 %workgroup = OpConstant %uint 2
215 %uint_1 = OpConstant %uint 1
216 %main = OpFunction %void None %3
217 %5 = OpLabel
218 %time1 = OpVariable %_ptr_Function_ulong Function
219 %11 = OpReadClockKHR %ulong %workgroup
220 OpStore %time1 %11
221 OpReturn
222 OpFunctionEnd)";
223 
224   CompileSuccessfully(spirv);
225   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
226   EXPECT_THAT(getDiagnosticString(),
227               HasSubstr("Scope must be Subgroup or Device"));
228 }
229 
TEST_F(ValidateMisc,VulkanShaderClockWorkgroupScope)230 TEST_F(ValidateMisc, VulkanShaderClockWorkgroupScope) {
231   const std::string spirv = ShaderClockSpriv + R"(
232 %3 = OpTypeFunction %void
233 %ulong = OpTypeInt 64 0
234 %uint = OpTypeInt 32 0
235 %_ptr_Function_ulong = OpTypePointer Function %ulong
236 %workgroup = OpConstant %uint 2
237 %uint_1 = OpConstant %uint 1
238 %main = OpFunction %void None %3
239 %5 = OpLabel
240 %time1 = OpVariable %_ptr_Function_ulong Function
241 %11 = OpReadClockKHR %ulong %workgroup
242 OpStore %time1 %11
243 OpReturn
244 OpFunctionEnd)";
245 
246   CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
247   ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
248   EXPECT_THAT(getDiagnosticString(),
249               AnyVUID("VUID-StandaloneSpirv-OpReadClockKHR-04652"));
250   EXPECT_THAT(getDiagnosticString(),
251               HasSubstr("Scope must be Subgroup or Device"));
252 }
253 
TEST_F(ValidateMisc,UndefVoid)254 TEST_F(ValidateMisc, UndefVoid) {
255   const std::string spirv = R"(
256                OpCapability Shader
257           %1 = OpExtInstImport "GLSL.std.450"
258                OpMemoryModel Logical GLSL450
259                OpEntryPoint Fragment %4 "main"
260                OpExecutionMode %4 OriginUpperLeft
261                OpSource ESSL 320
262           %2 = OpTypeVoid
263          %10 = OpUndef %2
264           %3 = OpTypeFunction %2
265           %4 = OpFunction %2 None %3
266           %5 = OpLabel
267                OpReturn
268                OpFunctionEnd
269 )";
270 
271   CompileSuccessfully(spirv);
272   EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
273   EXPECT_THAT(getDiagnosticString(),
274               HasSubstr("Cannot create undefined values with void type"));
275 }
276 }  // namespace
277 }  // namespace val
278 }  // namespace spvtools
279