1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Assembler tests for instructions in the "Barrier Instructions" section
16 // of the SPIR-V spec.
18 #include "unit_spirv.h"
20 #include "test_fixture.h"
21 #include "gmock/gmock.h"
25 using ::testing::ElementsAre;
28 using spvtest::MakeInstruction;
29 using spvtest::TextToBinaryTest;
31 // Test OpMemoryBarrier
33 using OpMemoryBarrier = spvtest::TextToBinaryTest;
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));
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."));
48 TEST_F(OpMemoryBarrier, BadInvalidScopeId) {
49 const std::string input = "OpMemoryBarrier 99\n";
50 EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
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."));
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 %."));
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
79 using NamedMemoryBarrierTest = spvtest::TextToBinaryTest;
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'"));
87 TEST_F(NamedMemoryBarrierTest, ArgumentCount) {
88 EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
89 Eq("Expected operand, found end of stream."));
91 CompileFailure("OpMemoryNamedBarrier %bar", SPV_ENV_UNIVERSAL_1_1),
92 Eq("Expected operand, found end of stream."));
94 CompileFailure("OpMemoryNamedBarrier %bar %scope", SPV_ENV_UNIVERSAL_1_1),
95 Eq("Expected operand, found end of stream."));
97 CompiledInstructions("OpMemoryNamedBarrier %bar %scope %semantics",
98 SPV_ENV_UNIVERSAL_1_1),
99 ElementsAre(spvOpcodeMake(4, SpvOpMemoryNamedBarrier), _, _, _));
101 CompileFailure("OpMemoryNamedBarrier %bar %scope %semantics %extra",
102 SPV_ENV_UNIVERSAL_1_1),
103 Eq("Expected '=', found end of stream."));
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 %."));
115 using TypeNamedBarrierTest = spvtest::TextToBinaryTest;
117 TEST_F(TypeNamedBarrierTest, OpcodeUnrecognizedInV10) {
118 EXPECT_THAT(CompileFailure("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_0),
119 Eq("Invalid Opcode name 'OpTypeNamedBarrier'"));
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'."));
127 CompiledInstructions("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
128 ElementsAre(spvOpcodeMake(2, SpvOpTypeNamedBarrier), _));
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, "
135 using NamedBarrierInitializeTest = spvtest::TextToBinaryTest;
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'"));
143 TEST_F(NamedBarrierInitializeTest, ArgumentCount) {
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."));
151 CompiledInstructions("%bar = OpNamedBarrierInitialize %type %count",
152 SPV_ENV_UNIVERSAL_1_1),
153 ElementsAre(spvOpcodeMake(4, SpvOpNamedBarrierInitialize), _, _, _));
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\"'."));
161 } // anonymous namespace