aboutsummaryrefslogtreecommitdiff
path: root/test/val/val_webgpu_test.cpp
blob: 1eae0d39ce92a3388894d46d9128e556e15a53ad (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2018 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.

// Validation tests for WebGPU env specific checks

#include <string>

#include "gmock/gmock.h"
#include "test/val/val_fixtures.h"

namespace spvtools {
namespace val {
namespace {

using testing::HasSubstr;

using ValidateWebGPU = spvtest::ValidateBase<bool>;

TEST_F(ValidateWebGPU, OpUndefIsDisallowed) {
  std::string spirv = R"(
          OpCapability Shader
          OpCapability VulkanMemoryModelKHR
          OpExtension "SPV_KHR_vulkan_memory_model"
          OpMemoryModel Logical VulkanKHR
          OpEntryPoint Vertex %func "shader"
%float  = OpTypeFloat 32
%1      = OpUndef %float
%void   = OpTypeVoid
%void_f = OpTypeFunction %void
%func   = OpFunction %void None %void_f
%label  = OpLabel
          OpReturn
          OpFunctionEnd
)";

  CompileSuccessfully(spirv);

  // Control case: OpUndef is allowed in SPIR-V 1.3
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));

  // Control case: OpUndef is disallowed in the WebGPU env
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(), HasSubstr("OpUndef is disallowed"));
}

TEST_F(ValidateWebGPU, OpNameIsDisallowed) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
     OpName %1 "foo"
%1 = OpTypeFloat 32
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Debugging instructions are not allowed in the WebGPU "
                        "execution environment.\n  OpName %foo \"foo\"\n"));
}

TEST_F(ValidateWebGPU, OpMemberNameIsDisallowed) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
     OpMemberName %2 0 "foo"
%1 = OpTypeFloat 32
%2 = OpTypeStruct %1
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Debugging instructions are not allowed in the WebGPU "
                        "execution environment.\n  OpMemberName %_struct_1 0 "
                        "\"foo\"\n"));
}

TEST_F(ValidateWebGPU, OpSourceIsDisallowed) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
     OpSource GLSL 450
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Debugging instructions are not allowed in the WebGPU "
                        "execution environment.\n  OpSource GLSL 450\n"));
}

// OpSourceContinued does not have a test case, because it requires being
// preceded by OpSource, which will cause a validation error.

TEST_F(ValidateWebGPU, OpSourceExtensionIsDisallowed) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
     OpSourceExtension "bar"
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Debugging instructions are not allowed in the WebGPU "
                        "execution environment.\n  OpSourceExtension "
                        "\"bar\"\n"));
}

TEST_F(ValidateWebGPU, OpStringIsDisallowed) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
%1 = OpString "foo"
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Debugging instructions are not allowed in the WebGPU "
                        "execution environment.\n  %1 = OpString \"foo\"\n"));
}

// OpLine does not have a test case, because it requires being preceded by
// OpString, which will cause a validation error.

TEST_F(ValidateWebGPU, OpNoLineDisallowed) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
     OpNoLine
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Debugging instructions are not allowed in the WebGPU "
                        "execution environment.\n  OpNoLine\n"));
}

TEST_F(ValidateWebGPU, LogicalAddressingVulkanKHRMemoryGood) {
  std::string spirv = R"(
          OpCapability Shader
          OpCapability VulkanMemoryModelKHR
          OpExtension "SPV_KHR_vulkan_memory_model"
          OpMemoryModel Logical VulkanKHR
          OpEntryPoint Vertex %func "shader"
%void   = OpTypeVoid
%void_f = OpTypeFunction %void
%func   = OpFunction %void None %void_f
%label  = OpLabel
          OpReturn
          OpFunctionEnd
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
}

TEST_F(ValidateWebGPU, NonLogicalAddressingModelBad) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Physical32 VulkanKHR
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Addressing model must be Logical for WebGPU "
                        "environment.\n  OpMemoryModel Physical32 "
                        "Vulkan\n"));
}

TEST_F(ValidateWebGPU, NonVulkanKHRMemoryModelBad) {
  std::string spirv = R"(
     OpCapability Shader
     OpMemoryModel Logical GLSL450
     OpNoLine
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("Memory model must be VulkanKHR for WebGPU "
                        "environment.\n  OpMemoryModel Logical GLSL450\n"));
}

TEST_F(ValidateWebGPU, WhitelistedExtendedInstructionsImportGood) {
  std::string spirv = R"(
          OpCapability Shader
          OpCapability VulkanMemoryModelKHR
          OpExtension "SPV_KHR_vulkan_memory_model"
%1      = OpExtInstImport "GLSL.std.450"
          OpMemoryModel Logical VulkanKHR
          OpEntryPoint Vertex %func "shader"
%void   = OpTypeVoid
%void_f = OpTypeFunction %void
%func   = OpFunction %void None %void_f
%label  = OpLabel
          OpReturn
          OpFunctionEnd
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
}

TEST_F(ValidateWebGPU, NonWhitelistedExtendedInstructionsImportBad) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_vulkan_memory_model"
%1 = OpExtInstImport "OpenCL.std"
     OpMemoryModel Logical VulkanKHR
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("For WebGPU, the only valid parameter to "
                        "OpExtInstImport is \"GLSL.std.450\".\n  %1 = "
                        "OpExtInstImport \"OpenCL.std\"\n"));
}

TEST_F(ValidateWebGPU, NonVulkanKHRMemoryModelExtensionBad) {
  std::string spirv = R"(
     OpCapability Shader
     OpCapability VulkanMemoryModelKHR
     OpExtension "SPV_KHR_8bit_storage"
     OpExtension "SPV_KHR_vulkan_memory_model"
     OpMemoryModel Logical VulkanKHR
)";

  CompileSuccessfully(spirv);

  EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("For WebGPU, the only valid parameter to OpExtension "
                        "is \"SPV_KHR_vulkan_memory_model\".\n  OpExtension "
                        "\"SPV_KHR_8bit_storage\"\n"));
}

spv_binary GenerateTrivialBinary(bool need_little_endian) {
  // Smallest possible valid WebGPU SPIR-V binary in little endian. Contains all
  // the required boilerplate and a trivial entry point function.
  static const uint8_t binary_bytes[] = {
      // clang-format off
    0x03, 0x02, 0x23, 0x07, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00,
    0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0xE1, 0x14, 0x00, 0x00,
    0x0A, 0x00, 0x08, 0x00, 0x53, 0x50, 0x56, 0x5F, 0x4B, 0x48, 0x52, 0x5F,
    0x76, 0x75, 0x6C, 0x6B, 0x61, 0x6E, 0x5F, 0x6D, 0x65, 0x6D, 0x6F, 0x72,
    0x79, 0x5F, 0x6D, 0x6F, 0x64, 0x65, 0x6C, 0x00, 0x0E, 0x00, 0x03, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x05, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x73, 0x68, 0x61, 0x64,
    0x65, 0x72, 0x00, 0x00, 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
    0x21, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
    0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x02, 0x00,
    0x04, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00
      // clang-format on
  };
  static const size_t word_count = sizeof(binary_bytes) / sizeof(uint32_t);
  std::unique_ptr<spv_binary_t> result(new spv_binary_t);
  if (!result) return nullptr;

  result->wordCount = word_count;
  result->code = new uint32_t[word_count];
  if (!result->code) return nullptr;

  if (need_little_endian) {
    memcpy(result->code, binary_bytes, sizeof(binary_bytes));
  } else {
    uint8_t* code_bytes = reinterpret_cast<uint8_t*>(result->code);
    for (size_t word = 0; word < word_count; ++word) {
      code_bytes[4 * word] = binary_bytes[4 * word + 3];
      code_bytes[4 * word + 1] = binary_bytes[4 * word + 2];
      code_bytes[4 * word + 2] = binary_bytes[4 * word + 1];
      code_bytes[4 * word + 3] = binary_bytes[4 * word];
    }
  }

  return result.release();
}

TEST_F(ValidateWebGPU, LittleEndianGood) {
  DestroyBinary();
  binary_ = GenerateTrivialBinary(true);
  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
}

TEST_F(ValidateWebGPU, BigEndianBad) {
  DestroyBinary();
  binary_ = GenerateTrivialBinary(false);
  EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("WebGPU requires SPIR-V to be little endian."));
}

}  // namespace
}  // namespace val
}  // namespace spvtools