• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Memory Instructions" section of
16 // the SPIR-V spec.
17 
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 
22 #include "gmock/gmock.h"
23 #include "test/test_fixture.h"
24 #include "test/unit_spirv.h"
25 
26 namespace spvtools {
27 namespace {
28 
29 using spvtest::EnumCase;
30 using spvtest::MakeInstruction;
31 using spvtest::TextToBinaryTest;
32 using ::testing::Eq;
33 
34 // Test assembly of Memory Access masks
35 
36 using MemoryAccessTest = spvtest::TextToBinaryTestBase<
37     ::testing::TestWithParam<EnumCase<SpvMemoryAccessMask>>>;
38 
TEST_P(MemoryAccessTest,AnySingleMemoryAccessMask)39 TEST_P(MemoryAccessTest, AnySingleMemoryAccessMask) {
40   std::stringstream input;
41   input << "OpStore %ptr %value " << GetParam().name();
42   for (auto operand : GetParam().operands()) input << " " << operand;
43   EXPECT_THAT(CompiledInstructions(input.str()),
44               Eq(MakeInstruction(SpvOpStore, {1, 2, GetParam().value()},
45                                  GetParam().operands())));
46 }
47 
48 INSTANTIATE_TEST_CASE_P(
49     TextToBinaryMemoryAccessTest, MemoryAccessTest,
50     ::testing::ValuesIn(std::vector<EnumCase<SpvMemoryAccessMask>>{
51         {SpvMemoryAccessMaskNone, "None", {}},
52         {SpvMemoryAccessVolatileMask, "Volatile", {}},
53         {SpvMemoryAccessAlignedMask, "Aligned", {16}},
54         {SpvMemoryAccessNontemporalMask, "Nontemporal", {}},
55     }), );
56 
TEST_F(TextToBinaryTest,CombinedMemoryAccessMask)57 TEST_F(TextToBinaryTest, CombinedMemoryAccessMask) {
58   const std::string input = "OpStore %ptr %value Volatile|Aligned 16";
59   const uint32_t expected_mask =
60       SpvMemoryAccessVolatileMask | SpvMemoryAccessAlignedMask;
61   EXPECT_THAT(expected_mask, Eq(3u));
62   EXPECT_THAT(CompiledInstructions(input),
63               Eq(MakeInstruction(SpvOpStore, {1, 2, expected_mask, 16})));
64 }
65 
66 // Test Storage Class enum values
67 
68 using StorageClassTest = spvtest::TextToBinaryTestBase<
69     ::testing::TestWithParam<EnumCase<SpvStorageClass>>>;
70 
TEST_P(StorageClassTest,AnyStorageClass)71 TEST_P(StorageClassTest, AnyStorageClass) {
72   const std::string input = "%1 = OpVariable %2 " + GetParam().name();
73   EXPECT_THAT(CompiledInstructions(input),
74               Eq(MakeInstruction(SpvOpVariable, {1, 2, GetParam().value()})));
75 }
76 
77 // clang-format off
78 #define CASE(NAME) { SpvStorageClass##NAME, #NAME, {} }
79 INSTANTIATE_TEST_CASE_P(
80     TextToBinaryStorageClassTest, StorageClassTest,
81     ::testing::ValuesIn(std::vector<EnumCase<SpvStorageClass>>{
82         CASE(UniformConstant),
83         CASE(Input),
84         CASE(Uniform),
85         CASE(Output),
86         CASE(Workgroup),
87         CASE(CrossWorkgroup),
88         CASE(Private),
89         CASE(Function),
90         CASE(Generic),
91         CASE(PushConstant),
92         CASE(AtomicCounter),
93         CASE(Image),
94     }),);
95 #undef CASE
96 // clang-format on
97 
98 // TODO(dneto): OpVariable with initializers
99 // TODO(dneto): OpImageTexelPointer
100 // TODO(dneto): OpLoad
101 // TODO(dneto): OpStore
102 // TODO(dneto): OpCopyMemory
103 // TODO(dneto): OpCopyMemorySized
104 // TODO(dneto): OpAccessChain
105 // TODO(dneto): OpInBoundsAccessChain
106 // TODO(dneto): OpPtrAccessChain
107 // TODO(dneto): OpArrayLength
108 // TODO(dneto): OpGenercPtrMemSemantics
109 
110 }  // namespace
111 }  // namespace spvtools
112