1 // Copyright (c) 2015-2016 The Khronos Group 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 "unit_spirv.h"
19
20 #include "test_fixture.h"
21 #include "gmock/gmock.h"
22
23 namespace {
24
25 using ::testing::ElementsAre;
26 using ::testing::Eq;
27 using ::testing::_;
28 using spvtest::MakeInstruction;
29 using spvtest::TextToBinaryTest;
30
31 // Test OpMemoryBarrier
32
33 using OpMemoryBarrier = spvtest::TextToBinaryTest;
34
TEST_F(OpMemoryBarrier,Good)35 TEST_F(OpMemoryBarrier, Good) {
36 const std::string input = "OpMemoryBarrier %1 %2\n";
37 EXPECT_THAT(CompiledInstructions(input),
38 Eq(MakeInstruction(SpvOpMemoryBarrier, {1, 2})));
39 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
40 }
41
TEST_F(OpMemoryBarrier,BadMissingScopeId)42 TEST_F(OpMemoryBarrier, BadMissingScopeId) {
43 const std::string input = "OpMemoryBarrier\n";
44 EXPECT_THAT(CompileFailure(input),
45 Eq("Expected operand, found end of stream."));
46 }
47
TEST_F(OpMemoryBarrier,BadInvalidScopeId)48 TEST_F(OpMemoryBarrier, BadInvalidScopeId) {
49 const std::string input = "OpMemoryBarrier 99\n";
50 EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
51 }
52
TEST_F(OpMemoryBarrier,BadMissingMemorySemanticsId)53 TEST_F(OpMemoryBarrier, BadMissingMemorySemanticsId) {
54 const std::string input = "OpMemoryBarrier %scope\n";
55 EXPECT_THAT(CompileFailure(input),
56 Eq("Expected operand, found end of stream."));
57 }
58
TEST_F(OpMemoryBarrier,BadInvalidMemorySemanticsId)59 TEST_F(OpMemoryBarrier, BadInvalidMemorySemanticsId) {
60 const std::string input = "OpMemoryBarrier %scope 14\n";
61 EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
62 }
63
64 // TODO(dneto): OpControlBarrier
65 // TODO(dneto): OpGroupAsyncCopy
66 // TODO(dneto): OpGroupWaitEvents
67 // TODO(dneto): OpGroupAll
68 // TODO(dneto): OpGroupAny
69 // TODO(dneto): OpGroupBroadcast
70 // TODO(dneto): OpGroupIAdd
71 // TODO(dneto): OpGroupFAdd
72 // TODO(dneto): OpGroupFMin
73 // TODO(dneto): OpGroupUMin
74 // TODO(dneto): OpGroupSMin
75 // TODO(dneto): OpGroupFMax
76 // TODO(dneto): OpGroupUMax
77 // TODO(dneto): OpGroupSMax
78
79 using NamedMemoryBarrierTest = spvtest::TextToBinaryTest;
80
TEST_F(NamedMemoryBarrierTest,OpcodeUnrecognizedInV10)81 TEST_F(NamedMemoryBarrierTest, OpcodeUnrecognizedInV10) {
82 EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier %bar %scope %semantics",
83 SPV_ENV_UNIVERSAL_1_0),
84 Eq("Invalid Opcode name 'OpMemoryNamedBarrier'"));
85 }
86
TEST_F(NamedMemoryBarrierTest,ArgumentCount)87 TEST_F(NamedMemoryBarrierTest, ArgumentCount) {
88 EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
89 Eq("Expected operand, found end of stream."));
90 EXPECT_THAT(
91 CompileFailure("OpMemoryNamedBarrier %bar", SPV_ENV_UNIVERSAL_1_1),
92 Eq("Expected operand, found end of stream."));
93 EXPECT_THAT(
94 CompileFailure("OpMemoryNamedBarrier %bar %scope", SPV_ENV_UNIVERSAL_1_1),
95 Eq("Expected operand, found end of stream."));
96 EXPECT_THAT(
97 CompiledInstructions("OpMemoryNamedBarrier %bar %scope %semantics",
98 SPV_ENV_UNIVERSAL_1_1),
99 ElementsAre(spvOpcodeMake(4, SpvOpMemoryNamedBarrier), _, _, _));
100 EXPECT_THAT(
101 CompileFailure("OpMemoryNamedBarrier %bar %scope %semantics %extra",
102 SPV_ENV_UNIVERSAL_1_1),
103 Eq("Expected '=', found end of stream."));
104 }
105
TEST_F(NamedMemoryBarrierTest,ArgumentTypes)106 TEST_F(NamedMemoryBarrierTest, ArgumentTypes) {
107 EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier 123 %scope %semantics",
108 SPV_ENV_UNIVERSAL_1_1),
109 Eq("Expected id to start with %."));
110 EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier %bar %scope \"semantics\"",
111 SPV_ENV_UNIVERSAL_1_1),
112 Eq("Expected id to start with %."));
113 }
114
115 using TypeNamedBarrierTest = spvtest::TextToBinaryTest;
116
TEST_F(TypeNamedBarrierTest,OpcodeUnrecognizedInV10)117 TEST_F(TypeNamedBarrierTest, OpcodeUnrecognizedInV10) {
118 EXPECT_THAT(CompileFailure("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_0),
119 Eq("Invalid Opcode name 'OpTypeNamedBarrier'"));
120 }
121
TEST_F(TypeNamedBarrierTest,ArgumentCount)122 TEST_F(TypeNamedBarrierTest, ArgumentCount) {
123 EXPECT_THAT(CompileFailure("OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
124 Eq("Expected <result-id> at the beginning of an instruction, "
125 "found 'OpTypeNamedBarrier'."));
126 EXPECT_THAT(
127 CompiledInstructions("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
128 ElementsAre(spvOpcodeMake(2, SpvOpTypeNamedBarrier), _));
129 EXPECT_THAT(
130 CompileFailure("%t = OpTypeNamedBarrier 1 2 3", SPV_ENV_UNIVERSAL_1_1),
131 Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
132 "found '1'."));
133 }
134
135 using NamedBarrierInitializeTest = spvtest::TextToBinaryTest;
136
TEST_F(NamedBarrierInitializeTest,OpcodeUnrecognizedInV10)137 TEST_F(NamedBarrierInitializeTest, OpcodeUnrecognizedInV10) {
138 EXPECT_THAT(CompileFailure("%bar = OpNamedBarrierInitialize %type %count",
139 SPV_ENV_UNIVERSAL_1_0),
140 Eq("Invalid Opcode name 'OpNamedBarrierInitialize'"));
141 }
142
TEST_F(NamedBarrierInitializeTest,ArgumentCount)143 TEST_F(NamedBarrierInitializeTest, ArgumentCount) {
144 EXPECT_THAT(
145 CompileFailure("%bar = OpNamedBarrierInitialize", SPV_ENV_UNIVERSAL_1_1),
146 Eq("Expected operand, found end of stream."));
147 EXPECT_THAT(CompileFailure("%bar = OpNamedBarrierInitialize %ype",
148 SPV_ENV_UNIVERSAL_1_1),
149 Eq("Expected operand, found end of stream."));
150 EXPECT_THAT(
151 CompiledInstructions("%bar = OpNamedBarrierInitialize %type %count",
152 SPV_ENV_UNIVERSAL_1_1),
153 ElementsAre(spvOpcodeMake(4, SpvOpNamedBarrierInitialize), _, _, _));
154 EXPECT_THAT(
155 CompileFailure("%bar = OpNamedBarrierInitialize %type %count \"extra\"",
156 SPV_ENV_UNIVERSAL_1_1),
157 Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
158 "found '\"extra\"'."));
159 }
160
161 } // anonymous namespace
162