1// Copyright (c) 2016 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// Assembler tests for instructions in the "Barrier Instructions" section
16// of the SPIR-V spec.
17
18#include "test/unit_spirv.h"
19
20#include "gmock/gmock.h"
21#include "test/test_fixture.h"
22
23namespace spvtools {
24namespace {
25
26using ::spvtest::MakeInstruction;
27using ::testing::Eq;
28
29using OpGetKernelLocalSizeForSubgroupCountTest = spvtest::TextToBinaryTest;
30
31// We should be able to assemble it.  Validation checks are in another test
32// file.
33TEST_F(OpGetKernelLocalSizeForSubgroupCountTest, OpcodeAssemblesInV10) {
34  EXPECT_THAT(
35      CompiledInstructions("%res = OpGetKernelLocalSizeForSubgroupCount %type "
36                           "%sgcount %invoke %param %param_size %param_align",
37                           SPV_ENV_UNIVERSAL_1_0),
38      Eq(MakeInstruction(spv::Op::OpGetKernelLocalSizeForSubgroupCount,
39                         {1, 2, 3, 4, 5, 6, 7})));
40}
41
42TEST_F(OpGetKernelLocalSizeForSubgroupCountTest, ArgumentCount) {
43  EXPECT_THAT(CompileFailure("OpGetKernelLocalSizeForSubgroupCount",
44                             SPV_ENV_UNIVERSAL_1_1),
45              Eq("Expected <result-id> at the beginning of an instruction, "
46                 "found 'OpGetKernelLocalSizeForSubgroupCount'."));
47  EXPECT_THAT(CompileFailure("%res = OpGetKernelLocalSizeForSubgroupCount",
48                             SPV_ENV_UNIVERSAL_1_1),
49              Eq("Expected operand for OpGetKernelLocalSizeForSubgroupCount "
50                 "instruction, but found the end of the stream."));
51  EXPECT_THAT(
52      CompileFailure("%1 = OpGetKernelLocalSizeForSubgroupCount %2 %3 %4 %5 %6",
53                     SPV_ENV_UNIVERSAL_1_1),
54      Eq("Expected operand for OpGetKernelLocalSizeForSubgroupCount "
55         "instruction, but found the end of the stream."));
56  EXPECT_THAT(
57      CompiledInstructions("%res = OpGetKernelLocalSizeForSubgroupCount %type "
58                           "%sgcount %invoke %param %param_size %param_align",
59                           SPV_ENV_UNIVERSAL_1_1),
60      Eq(MakeInstruction(spv::Op::OpGetKernelLocalSizeForSubgroupCount,
61                         {1, 2, 3, 4, 5, 6, 7})));
62  EXPECT_THAT(
63      CompileFailure("%res = OpGetKernelLocalSizeForSubgroupCount %type "
64                     "%sgcount %invoke %param %param_size %param_align %extra",
65                     SPV_ENV_UNIVERSAL_1_1),
66      Eq("Expected '=', found end of stream."));
67}
68
69TEST_F(OpGetKernelLocalSizeForSubgroupCountTest, ArgumentTypes) {
70  EXPECT_THAT(CompileFailure(
71                  "%1 = OpGetKernelLocalSizeForSubgroupCount 2 %3 %4 %5 %6 %7",
72                  SPV_ENV_UNIVERSAL_1_1),
73              Eq("Expected id to start with %."));
74  EXPECT_THAT(
75      CompileFailure(
76          "%1 = OpGetKernelLocalSizeForSubgroupCount %2 %3 %4 %5 %6 \"abc\"",
77          SPV_ENV_UNIVERSAL_1_1),
78      Eq("Expected id to start with %."));
79}
80
81using OpGetKernelMaxNumSubgroupsTest = spvtest::TextToBinaryTest;
82
83TEST_F(OpGetKernelMaxNumSubgroupsTest, OpcodeAssemblesInV10) {
84  EXPECT_THAT(CompiledInstructions("%res = OpGetKernelMaxNumSubgroups %type "
85                                   "%invoke %param %param_size %param_align",
86                                   SPV_ENV_UNIVERSAL_1_0),
87              Eq(MakeInstruction(spv::Op::OpGetKernelMaxNumSubgroups,
88                                 {1, 2, 3, 4, 5, 6})));
89}
90
91TEST_F(OpGetKernelMaxNumSubgroupsTest, ArgumentCount) {
92  EXPECT_THAT(
93      CompileFailure("OpGetKernelMaxNumSubgroups", SPV_ENV_UNIVERSAL_1_1),
94      Eq("Expected <result-id> at the beginning of an instruction, found "
95         "'OpGetKernelMaxNumSubgroups'."));
96  EXPECT_THAT(CompileFailure("%res = OpGetKernelMaxNumSubgroups",
97                             SPV_ENV_UNIVERSAL_1_1),
98              Eq("Expected operand for OpGetKernelMaxNumSubgroups instruction, "
99                 "but found the end of the stream."));
100  EXPECT_THAT(CompileFailure("%1 = OpGetKernelMaxNumSubgroups %2 %3 %4 %5",
101                             SPV_ENV_UNIVERSAL_1_1),
102              Eq("Expected operand for OpGetKernelMaxNumSubgroups instruction, "
103                 "but found the end of the stream."));
104  EXPECT_THAT(CompiledInstructions("%res = OpGetKernelMaxNumSubgroups %type "
105                                   "%invoke %param %param_size %param_align",
106                                   SPV_ENV_UNIVERSAL_1_1),
107              Eq(MakeInstruction(spv::Op::OpGetKernelMaxNumSubgroups,
108                                 {1, 2, 3, 4, 5, 6})));
109  EXPECT_THAT(CompileFailure("%res = OpGetKernelMaxNumSubgroups %type %invoke "
110                             "%param %param_size %param_align %extra",
111                             SPV_ENV_UNIVERSAL_1_1),
112              Eq("Expected '=', found end of stream."));
113}
114
115TEST_F(OpGetKernelMaxNumSubgroupsTest, ArgumentTypes) {
116  EXPECT_THAT(CompileFailure("%1 = OpGetKernelMaxNumSubgroups 2 %3 %4 %5 %6",
117                             SPV_ENV_UNIVERSAL_1_1),
118              Eq("Expected id to start with %."));
119  EXPECT_THAT(
120      CompileFailure("%1 = OpGetKernelMaxNumSubgroups %2 %3 %4 %5 \"abc\"",
121                     SPV_ENV_UNIVERSAL_1_1),
122      Eq("Expected id to start with %."));
123}
124
125}  // namespace
126}  // namespace spvtools
127