Debian Codegraph @

third_party/spirv-tools/test/opt/legalize_vector_shuffle_test.cpp

raw
1 // Copyright (c) 2019 Google LLC.
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 #include <vector>
16
17 #include "test/opt/pass_fixture.h"
18 #include "test/opt/pass_utils.h"
19
20 namespace spvtools {
21 namespace opt {
22 namespace {
23
24 using LegalizeVectorShuffleTest = PassTest<::testing::Test>;
25
26 void operator+=(std::vector<const char*>& lhs, const char* rhs) {
27 lhs.push_back(rhs);
28 }
29
30 void operator+=(std::vector<const char*>& lhs,
31 const std::vector<const char*> rhs) {
32 for (auto elem : rhs) lhs.push_back(elem);
33 }
34
35 std::vector<const char*> header = {
36 "OpCapability Shader",
37 "OpCapability VulkanMemoryModel",
38 "OpExtension \"SPV_KHR_vulkan_memory_model\"",
39 "OpMemoryModel Logical Vulkan",
40 "OpEntryPoint Vertex %1 \"shader\"",
41 "%uint = OpTypeInt 32 0",
42 "%v3uint = OpTypeVector %uint 3"};
43
44 std::string GetTestString(const char* shuffle) {
45 std::vector<const char*> result = header;
46 result += {"%_ptr_Function_v3uint = OpTypePointer Function %v3uint",
47 "%void = OpTypeVoid",
48 "%6 = OpTypeFunction %void",
49 "%1 = OpFunction %void None %6",
50 "%7 = OpLabel",
51 "%8 = OpVariable %_ptr_Function_v3uint Function",
52 "%9 = OpLoad %v3uint %8",
53 "%10 = OpLoad %v3uint %8"};
54 result += shuffle;
55 result += {"OpReturn", "OpFunctionEnd"};
56 return JoinAllInsts(result);
57 }
58
59 TEST_F(LegalizeVectorShuffleTest, Changed) {
60 std::string input =
61 GetTestString("%11 = OpVectorShuffle %v3uint %9 %10 2 1 0xFFFFFFFF");
62 std::string expected =
63 GetTestString("%11 = OpVectorShuffle %v3uint %9 %10 2 1 0");
64
65 SinglePassRunAndCheck<LegalizeVectorShufflePass>(input, expected,
66 /* skip_nop = */ false);
67 }
68
69 TEST_F(LegalizeVectorShuffleTest, FunctionUnchanged) {
70 std::string input =
71 GetTestString("%11 = OpVectorShuffle %v3uint %9 %10 2 1 0");
72 std::string expected =
73 GetTestString("%11 = OpVectorShuffle %v3uint %9 %10 2 1 0");
74
75 SinglePassRunAndCheck<LegalizeVectorShufflePass>(input, expected,
76 /* skip_nop = */ false);
77 }
78
79 } // namespace
80 } // namespace opt
81 } // namespace spvtools